function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function clearField(fld) {
	if(fld.value == "Please enter a valid email."){
		fld.value = "";
		fld.focus();
	}
}

function validateOnSubmit(fld) {
	
	var tfld = trim(document.getElementById('email').value);  // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;	
	
	// include IF if newsletter signup included -> 
	//if(document.ctacontact.news_signup_check.checked || document.ctacontact.query.value != ""){
	
		if(document.getElementById('email').value == "") {
			alert("Please enter an email address");
			return false;
		} else if(!emailFilter.test(tfld)) {
			alert("Please enter a valid email address");
			return false;
		} else if(document.getElementById('email').value.match(illegalChars)) {
			alert("The email address contains illegal characters");
			return false;
		} else {
			var email = document.getElementById('email').value;

			sendEmail(email);
		} 
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
	}catch (e){
		try{// Internet Explorer
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function sendEmail(email){
	document.getElementById('email_list').innerHTML = '<img src="images/email.gif" border="0" alt="Email" /><br/> <img src="images/loader.gif" height="15" width="128" alt="" style="position:relative; top:2px; left:20px;">'; 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		//alert("Your browser does not support AJAX!");
		document.getElementById('email_list').innerHTML = '<img src="images/email.gif" border="0" alt="Email" /><br/> Your browser does not support AJAX'; 
		return;
	}
	var url="subscribe.php";
	url=url+"?email="+email;
	url=url+"&rand="+Math.random();
	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
			if (xmlHttp.status == 200) {
				mailStatus=xmlHttp.responseText;
				if(mailStatus=='1'){
					mailStatus='<img src="images/email.gif" border="0" alt="Email" /><br/><span style="position:relative; top:-5px;">Thank you for subscribing.</span>';
				}
				document.getElementById('email_list').innerHTML = mailStatus;
			} else if(mailStatus == 'false'){
					document.getElementById('email').value = 'Please enter a valid email.';
			} else {
				mailStatus='<img src="images/email.gif" border="0" alt="Email" /><br/> Subscription error. Please try again.';
				document.getElementById('email_list').innerHTML = mailStatus;
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}