// 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 "";
		}
	
}

//fucntion to convert checkbox array to postable array string
function srlzCheckBox(path){

	var chkbxString = '';
	var vLength = path.length;
	
	for (var idx = 0; idx < vLength; idx++) {
		
		if (eval(path[idx].checked) == true) {
			
			if(idx > 0){ chkbxString += ', ';}
			
			chkbxString += path[idx].value;
		 }
		 
	}	
	
	return chkbxString;
	
}


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

//###############################################################################################################################
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(srlzCheckBox(path.dayofweek));
		alert(error);
	}else{
		
	//var varString = 'name='+path.name.value+'&email='+path.email.value+'&phone='+path.phone.value+'&message='+path.message.value+'&frshpv='+path.frshpv.value+'&challenge_response='+path.challenge_response.value+'&address='+path.address.value+'&address2='+path.address2.value+'&city='+path.city.value+'&prov='+path.prov.value+'&postal='+path.postal.value+'&dayofweek='+srlzCheckBox(path.dayofweek)+'&timeofday='+srlzCheckBox(path.timeofday)+'&location='+srlzCheckBox(path.location)+'&apptype='+srlzCheckBox(path.apptype)+'&contacts='+srlzCheckBox(path.contacts)+'&worktel='+path.worktel.value+'&message='+path.message.value; // With Appointment type
	var varString = 'name='+path.name.value+'&email='+path.email.value+'&phone='+path.phone.value+'&message='+path.message.value+'&frshpv='+path.frshpv.value+'&challenge_response='+path.challenge_response.value+'&address='+path.address.value+'&address2='+path.address2.value+'&city='+path.city.value+'&prov='+path.prov.value+'&postal='+path.postal.value+'&dayofweek='+srlzCheckBox(path.dayofweek)+'&timeofday='+srlzCheckBox(path.timeofday)+'&location='+srlzCheckBox(path.location)+'&contacts='+srlzCheckBox(path.contacts)+'&worktel='+path.worktel.value+'&message='+path.message.value; // no apointment type
	
	document.getElementById('contactfrm').innerHTML = '<p><center><img src="images/loading.gif"></center></p>';
   
    setTimeout("DelayAjaxContact('"+varString+"')",500);

	}	
	
}

function createRequestObject() {

   var req;

   if(window.XMLHttpRequest){
      // Firefox, Safari, Opera...
      req = new XMLHttpRequest();
   } else if(window.ActiveXObject) {
      // Internet Explorer 5+
      req = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
      // There is an error creating the object,
      // just as an old browser is being used.
      alert('Problem creating the XMLHttpRequest object');
   }

   return req;

} 

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

function DelayAjaxContact(varString){
	
   // Open PHP script for requests
   //http.abort;
   http2.open('post', 'email/appointments.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('contactfrm').innerHTML = '<center>'+response+'</center>';

      }

   }

}
