// document.emailform.debug.value  is not used in function sendMail

function pause(milliseconds) {
	var dt = new Date();
	while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}

function ltrim(str) {
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

// Get the HTTP Object
       function getHTTPObject(){
        //alert("create http object")
        if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
        else if (window.XMLHttpRequest) return new XMLHttpRequest();
             else {
                  alert("Your browser does not support AJAX.");
                  return null;
             }

        }



function sendMail() {
    document.emailform.message.value=""
    //document.emailform.debug.value=""
    var visitor=document.emailform.visitor.value
    var visitormail=document.emailform.visitormail.value
    var notes=document.emailform.notes.value
    var attn=document.emailform.attn.value
    document.getElementById('msg').style.color = 'red';
    visitor = ltrim(rtrim(visitor));
    if(!(visitor.length > 0)){
          document.emailform.message.value="Name is missing"
          return;
     }
     
     NEWvisitor=visitor.toLowerCase();
     //alert("newvisitor = " + NEWvisitor)
     document.emailform.visitor.value=NEWvisitor
     visitor=encodeURIComponent(NEWvisitor)

     visitormail = ltrim(rtrim(visitormail));
	 var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	 if(!(visitormail.match(emailRegEx))){
	   document.emailform.message.value="Invalid e-mail address format";
	   return;
	 }
	 
     NEWvisitormail=visitormail.toLowerCase();
     document.emailform.visitormail.value=NEWvisitormail
     visitormail=encodeURIComponent(NEWvisitormail)
     
     notes = ltrim(rtrim(notes));
	 if(!(notes.length > 0)){
	    document.emailform.message.value="E-mail message is missing"
	    return;
	 }
     document.emailform.notes.value=notes
     notes=encodeURIComponent(notes)
     document.getElementById('msg').style.color = 'black';
     httpObject = getHTTPObject();
    // alert("httpObject= " + httpObject);
	 if (httpObject != null) {
	        document.getElementById('msg').style.color = 'red';
	        document.emailform.message.value="your e-mail message is pending"
	        pause(3000) // 3 second pause
	         var url = "sendMail.php";
	         //var url = "testpost.php";
	         var queryString = "Visitor=" + visitor + "&Visitormail=" + visitormail + "&Notes=" + notes + "&Attn=" + attn;
	       //  alert(queryString);
		 httpObject.open("POST", url, true)
		 httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
                 httpObject.send(queryString)
	         httpObject.onreadystatechange = function (){
	        // alert("ready state= " + httpObject.readyState);
                if(httpObject.readyState == 4){
                   document.emailform.debug.value = httpObject.responseText;
                   document.getElementById('msg').style.color = 'blue';
                   document.emailform.message.value="your e-mail message has been sent"
                }
                else {
                    document.emailform.debug.value = httpObject.responseText;
                //    alert("ready state= " + httpObject.readyState);
                }
             }// onreadystatechange
     } // httpObject
} // sendMail
