var xmlHttp;
var requestType;

function hideit() {
	emailfriendid.style.display = 'none';
	email_success.innerHTML = '';
}
function showit() {
	emailfriendid.style.display = 'block';
	if (document.getElementById('email_to').value == '') {
		document.getElementById('email_to').focus();
	}
}

function createXMLHttpRequest() {
	if(window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}	else if(window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}
function startRequest( url, qry, type) {
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open(type, url, true);
	if(type == 'POST') {
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
		xmlHttp.send(qry);
	} else {
		xmlHttp.send(null);
	}
}
function handleStateChange() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			document.getElementById('email_success').innerHTML = xmlHttp.responseText;
			setTimeout ( hideit, 1500 );
		}
	} else {
		document.getElementById('email_success').innerHTML = 'Sending...';
	}
}
function sendEmail()
{
	var email_to = document.getElementById("email_to").value;
	var email_from = document.getElementById("email_from").value;
	if (!validemail(email_to)) {
		document.getElementById('email_success').innerHTML = 'Invalid To: email';
		return false;
	}
	if (!validemail(email_from)) {
		document.getElementById('email_success').innerHTML = 'Invalid From: email';
		return false;
	}
	qry = '&url=' + document.location.href + '&email_to=' + email_to + '&email_from=' + email_from + '&email_note=' + document.getElementById("email_note").value;
	startRequest(serverScriptURL,qry,'POST');
}
function validemail(text)
{
	var atidx, dotidx, re;
	if ((atidx = text.indexOf("@")) > 0) {
		if ((dotidx = text.indexOf("." , atidx)) > atidx+1) {
			if (dotidx < text.length-1) {
				re = /[^a-zA-Z0-9\@\\.\\-\\_]/;
				var str = new String(text);
				var i = str.search(re);
				if (i == -1) {
						return true;
				}
			}
		}
	}
	return false;
}
