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

//function to loop and populate a designated variable with all checked answers from a specified checkbox group
function loopPop(targetInput){
	
	var vArray = new Array();
	var counter = 0;

			for (i=0, n=targetInput.length; i<n; i++) {
			  if (targetInput[i].checked) {
				vArray[counter] = targetInput[i].value;
				counter++;
			  }
			}
			
		

return vArray;
	
	
}


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

//###############################################################################################################################
error += checkfeild(path.name.value, "Please enter your name.\n"); 						// name
error += checkemail(path.email.value, "Your"); 	

if(loopPop(path.enewsType) == ""){
		error += "Please select a type of newsletter to receive.\n";
}

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+'&varType='+loopPop(path.enewsType)+'&fmpv='+path.fmpv.value+'&challenge_response='+path.challenge_response.value;
	
	document.getElementById('enewsDiv').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/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('enewsDiv').innerHTML = '<center>'+response+'</center>';

      }

   }

}

