// 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.contactfrm;	

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

	if(error != ""){
		alert(error);
	}else{
		
	var varString = 'varName='+path.name.value+'&varEmail='+path.email.value+'&varPhone='+path.phone.value+'&varMessage='+path.message.value+'&fmpv='+path.fmpv.value+'&challenge_response='+path.challenge_response.value;
	
	document.getElementById('contactDiv').innerHTML = '<p><center><img src="images/loading.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/contact.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('contactDiv').innerHTML = ''+response+'';

      }

   }

}
