//	********************************************************************************
//	JavaScript functions to handle generic browser events for most browsers
//	Also uses AJAX to post email requests to server objects.
//  ********************************************************************************

//	The INIT function which MUST BE CALLED from the body tag of the host page i.e.
//	<body onload="init();">
function init()
{
//	Check if the subscribe to email button has been clicked
	img=window.document.getElementById("emailSubscribeButton");
	img.clicked = false;
	img.onclick=doSubmit;
}
function doSubmit()
{
//	****************************************************************************************
//	Here we proceed after the email button/image has been clicked (from the INIT() function)
//	to retrieve the text entries on the page such as email, from and a re-direct page if
//	required (leaving blank just comes up with an alert window to confirm)
//	****************************************************************************************

//	Setup the variables to hold the current entered email details on the page
	var email = document.all.email.value;
	var emailsendto = document.all.sendto.value;
	var returnurl = document.all.resulturl.value;
	
//	Check regex to ensure we have a valid email address format	
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email))
	{
		// All ok
	}
	else {
		alert('Incorrect email address. Please type again.');
		return;
	}

//	Setup the AJAX functions
	xmlHttp=null;
	if (window.XMLHttpRequest)
	{
		xmlHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP AJAX Requests");
		return;
	} 	
	
//	Now send the details through to the desired server page to process the email send request
	//var url="phpmail.php";	This one is an example of calling PHP
	var url="ASPcdoMailer.asp";
	url=url+"?email=" + email + "&sendto=" + emailsendto + "&resulturl=" + returnurl;
	xmlHttp.onreadystatechange=ajaxPostback;
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)	
}

function ajaxPostback()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		alert(xmlHttp.responseText);
	}
}
