var blogURL = 'http://www.divercitycafe.ro';
var incPath = '/wp-content/themes/divercity/inc/';

function createCookie(name,value,minutes) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(minutes*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function validateRec() {
	reason = "";

	var tfld = trim(document.recpost.friend_email.value);
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

	
	if (document.recpost.friend_email.value == "") {
		reason += "Campul Email este gol.<br />";
	}
	else if (!emailFilter.test(tfld)) {              //test email for illegal characters
		reason += "Introduceti o adresa de email valida.<br />";
	}
	if (document.recpost.friend_email.value.match(illegalChars)) {
		reason += "Campul Email contine caractere nepermise.<br />";
	}

	if (reason != "") {
		document.getElementById("errorRec").innerHTML = reason;
	}

	else {
		postTitle = document.recpost.post_title.value;
		postURL = document.recpost.post_url.value;
		friendEmail = document.recpost.friend_email.value;
		doRec(postTitle,postURL,friendEmail);
	}
}
function doRec(postTitle,postURL,friendEmail) {
	var xmlHttp=null;

	try {
		// Firefox, Opera 8.0+, Safari, IE7
		xmlHttp=new XMLHttpRequest();
	}

	catch(e) {
		// Old IE
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		catch(e) {
			alert ("Your browser does not support XMLHTTP!");
			return;  
		}
	}

	var message = "Salut,\n\nUn prieten ti-a recomandat urmatorul articol:\n" + postTitle + "\n" + postURL + "\n\nLectura placute,\nEchipa DivercityCafe.ro\nhttp://www.divercitycafe.ro";
	var fileURL = blogURL + incPath + "recpost.inc.php";
	var params = "email=" + friendEmail + "&message=" + message;
	
	xmlHttp.open("POST",fileURL,false);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	xmlHttp.send(params);

	if (xmlHttp.responseText == 'success'){
		document.getElementById("errorRec").innerHTML = "Recomandarea ta a fost trimisa!";
	}
	else {
		document.getElementById("errorRec").innerHTML = xmlHttp.responseText;
	}
}
function trim(str, chars) {

        return ltrim(rtrim(str, chars), chars);

}

function ltrim(str, chars) {

        chars = chars || "\\s";

        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");

} 

function rtrim(str, chars) {

        chars = chars || "\\s";

        return str.replace(new RegExp("[" + chars + "]+$", "g"), "");

}