
var chkDot = true;
   var usEmail = true;
   var errors;
function MM_validateForm() 
        { //v4.0
        errors = '';
                var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
                for (i=0; i<(args.length-2); i+=3)
                { 
                  test=args[i+2]; 
                  element=document.getElementById(args[i]);
				  
				  //Test for null/blanks
				  if(element.value == null || element.value == ""){
				  	errors = "Please fill in the Required Field(s), they are outlined in RED on the form below..";
					element.style.borderColor = 'red';
					element.focus();
					break;   
				  }else{
				  		element.style.borderColor = '#CCC';
				  }
				  //Test email address
				  if(element.id == "from"){
				  	errors += validateEmail(element);
					if(errors){
					element.style.borderColor = 'red';
					element.focus();
					break;
					}else{
						element.style.borderColor = '#CCC';  
					}
				  }
				  if(element.id == "verif_box"){
				  	
					if(element.value.length !== 4){ errors += "Number must be four digits";element.focus();element.style.borderColor = 'red';element.value="";break;}
					if(/\D/.test(element.value)){  errors += "Entry must be numeric";element.focus();element.style.borderColor = 'red';element.value="";break;}
					
				  }
				  
				  
				}
		  if (errors) {				 
				  document.getElementById('result').innerHTML = '<p>'+errors+'</p>';				   
				  $("div#result").show("slow");		   				  				   
                   document.MM_returnValue = (errors == '');
          }else{
			  	 $("#form1").slideUp("slow");
				
		  		var url = 'mailer/captcha_test.php';
 				//Set up the parameters of our AJAX call
 				var postStr = "verif_box=" + encodeURIComponent( $('#verif_box').val() )+ "&" +  
 	 		   "from=" + encodeURIComponent( $('#from').val() )+ "&" +
			   "subject=" + encodeURIComponent( $('#subject').val() )+ "&" +
			   "message=" + encodeURIComponent( $('#message').val() );
			 //Call the function that initiate the AJAX request
			makeRequest(url, postStr);
		  }
		 
}
  
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();

//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 

   //Add HTTP headers to the request
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {
	//Test if the email passed or failed
	if(receiveReq.responseText == "passed"){
	  //Set the content of the DIV element with the response text  
	  document.getElementById('result').innerHTML = '<p>Your message has been sent to us. We\'ll respond within 24 hours.</p>';
	   $("#result").css({'border-color' : 'green'});
	   $("div#result").show("slow");	
	}else{
		 document.getElementById('result').innerHTML = receiveReq.responseText;
		$("div#result").show("slow");	
	}
   //Get a reference to CAPTCHA image
   img = document.getElementById('veri_img'); 
   //Change the image
   img.src = 'mailer/verificationimage.php?'+ Math.random();
 }
}

