// JavaScript Document
//check a text feild for content
function checkfeild(strng, stmnt){
	if(strng == ""){
			return stmnt;
	}else{
			return "";
	}
}

//check email feild
function checkemail(strng, xVar){
var stmnt = "";
var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) { 
		   stmnt = xVar+" email address is not valid.\n";
	}
	
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
	   stmnt += xVar+" email address contains illegal characters.\n";	
	}
	
	if(stmnt != ""){
		return stmnt;
	}else{
			return "";
	}
}

//check phon number
function checkPhone(strng, location){
	var stmnt = "";
	var stripped = strng.replace(/[\(\)\.\-\ a-z A-Z]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	   stmnt += "The "+location+" phone number contains illegal characters.\n";
	}
	
	if (!(stripped.length == 10)) {
	stmnt += "The "+location+" phone number is the wrong length. Make sure you included an area code.\n";
	}
	
		if(stmnt != ""){
		return stmnt;
		}else{
		return "";
		}
	
}


//########### Validate Form
function fValidate(){	
	
	var error = "";
	var path = document.newsfrm;	

//###############################################################################################################################
error += checkfeild(path.name.value, "Please enter your name.\n"); 						// name
error += checkemail(path.email.value, "Your"); 													//email
//error += checkPhone(path.phone.value, ""); 														//Tel
//error += checkfeild(path.challenge_response.value, "You must answer the anti spam question.\n"); //name
//###############################################################################################################################

	if(error != ""){
		alert(error);
	}else{
	
	var varString = 'email='+path.email.value+'&firstname='+path.firstname.value+'&lastname='+path.lastname.value+'&street='+path.street.value+'&box='+path.box.value+'&country='+path.country.value+'&city='+path.city.value+'&province='+path.province.value+'&postal='+path.postal.value+'&choices='+path.choices.value;
	//var varString = 'name='+path.name.value+'&email='+path.email.value+'&choices='+path.choices.value+'&comments='+path.comments.value;
	
	document.getElementById('newsfrm').innerHTML = '<p style="margin-top:75px;"><center><img src="../shell_images/ajax-loader.gif"/></center></p>';
   
    setTimeout("DelayAjaxContact('"+varString+"')",500);

	}	
	
}


// Make the XMLHttpRequest object for Contact Form
var http2 = createRequestObject(); 

function DelayAjaxContact(varString){
	
   // Open PHP script for requests
   //http.abort;
   http2.open('post', 'email/enews.email.php');
   http2.onreadystatechange = handleResponseContact; 
   http2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   http2.send(varString);
	
}

function handleResponseContact() {

   if(http2.readyState == 4 && http2.status == 200){

      // Text returned FROM the PHP script
      var response = http2.responseText;

      if(response) {

		 document.getElementById('newsfrm').innerHTML = '<center>'+response+'</center>';

      }

   }

}
