﻿function chkEmail(eml)
{
	var re = /^[0-9a-zA-Z\-\.\_]+@[0-9a-zA-Z\-]+\.[0-9a-zA-Z\-\.]+$/;
	if (!eml.match(re))
		return 0;
	else
		return 1;

}

  var captchaOK1 = 2; // 2 - not yet checked, 1 - correct, 0 - failed
  function getHTTPObject()
  {
	  try
		{
			req = new XMLHttpRequest();
		}
	  catch (err1)
	  {
			try
			  {
				req = new ActiveXObject("Msxml12.XMLHTTP");
			  }
			catch (err2)
			{
				   try
				   {
				   req = new ActiveXObject("Microsoft.XMLHTTP");
				   }
				   catch (err3)
				   {
				   req = false;
				   }
			}
	 	}
 		return req;
  }
 var http = getHTTPObject(); // We create the HTTP Object

function handleHttpResponse()
   {
		  if (http.readyState == 1)
		  {
			  //document.getElementById('sent1').innerHTML="<img src='images/ajax-loader.gif' alt='Loading....' border=0>";
			  //document.getElementById('sent1').innerHTML="";
		  }
		  if (http.readyState == 4)
		  {
		  		//alert(http.responseText);
			  captchaOK1 = http.responseText;
			  if(captchaOK1 != 'yes')
			   {

				   alert('The entered code was not correct. Please try again');
				   /*
				   document.subs.codenewsletter.value='';
				   document.subs.codenewsletter.focus();
				   */
				   return false;
			   }
			   else
			   {
			    alert("Your information has been sent.");
			//	window.location = "thank-you";
			   	return true;
			   }
		   //document.contact.submit();
		   }
   }



function LTrim(str)
{
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(0)) != -1) {
    // We have a string with leading blank(s)...

    var j=0, i = s.length;

    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
    j++;


    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }

  return s;
}



//2------------------------------------------------------------------------------*/




//////////////////////



 var captchaOK = 2; // 2 - not yet checked, 1 - correct, 0 - failed
  function getHTTPObject()
  {
	  try
		{
			req = new XMLHttpRequest();
		}
	  catch (err1)
	  {
			try
			  {
				req = new ActiveXObject("Msxml12.XMLHTTP");
			  }
			catch (err2)
			{
				   try
				   {
				   req = new ActiveXObject("Microsoft.XMLHTTP");
				   }
				   catch (err3)
				   {
				   req = false;
				   }
			}
	 	}
 		return req;
  }


 function checkshortcode(thecode){
	var email 	= LTrim(document.getElementById('email').value);
	var name 	= LTrim(document.getElementById('name').value);
	var phone 	= LTrim(document.getElementById('phone').value);
 	var comments 	= LTrim(document.getElementById('comments').value);
    var code = LTrim(document.shrtcontact.code.value);


  var url = "http://wtleggett.com/w04oct/captcheck.php?phone="+phone+"&email="+email+"&name="+name+"&comments="+comments+"&code=";
 // alert(url + escape(thecode))	;
  http.open("GET", url + escape(thecode), true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);

	 }

 function checkshortform(){

 var name = LTrim(document.getElementById('name').value);
  if(name=="" || name=='Name:') {
   alert('Please enter your name.');
   document.shrtcontact.name.focus(); return false;
     }

  if(LTrim(document.shrtcontact.email.value)=='') {
   alert('Please complete the "required" field.');
   document.shrtcontact.email.focus(); return false;
   }

  if(chkEmail(document.shrtcontact.email.value)==0) {
   alert('Please enter valid email address.');
   document.shrtcontact.email.focus(); return false;
   }

  var phone = LTrim(document.getElementById('phone').value);
  if(phone=="" || phone=='Name:') {
   alert('Please enter your phone number.');
   document.shrtcontact.phone.focus(); return false;
     }

   if(document.shrtcontact.code.value=='') {
	 alert('Please enter the string from the displayed image.');
	 document.shrtcontact.code.value='';
	 document.shrtcontact.code.focus();
	 return false;
	 } // Now the Ajax CAPTCHA validation
	 //alert(document.contact.code.value);
   checkshortcode(document.shrtcontact.code.value);
   return false;
	 }
	 
function checkcontactform(){
 var name = LTrim(document.maincontact.cname.value);
  if(name=="" || name=='Name:') {
   alert('Please enter your name.');
   document.maincontact.cname.focus(); return false;
     }

  if(LTrim(document.maincontact.cemail.value)=='') {
   alert('Please complete the "required" field.');
   document.maincontact.cemail.focus(); return false;
   }

  if(chkEmail(document.maincontact.cemail.value)==0) {
   alert('Please enter valid email address.');
   document.maincontact.cemail.focus(); return false;
   }

   if(document.maincontact.ccode.value=='') {
	 alert('Please enter the string from the displayed image.');
	 document.maincontact.ccode.value='';
	 document.maincontact.ccode.focus();
	 return false;
	 } // Now the Ajax CAPTCHA validation
	 //alert(document.contact.code.value);
   checkcode(document.maincontact.ccode.value);
   return false;
	 }
	 
function checkcode(thecode){
	var email 	= LTrim(document.maincontact.cemail.value);
	var name 	= LTrim(document.maincontact.cname.value);
	var subject	= LTrim(document.maincontact.subject.value);
 	var comments 	= LTrim(document.maincontact.ccomments.value);
    var code = LTrim(document.maincontact.ccode.value);


  var url = "http://wtleggett.com/w04oct/captcheck.php?subject="+subject+"&email="+email+"&name="+name+"&comments="+comments+"&code=";
 // alert(url + escape(thecode))	;
  http.open("GET", url + escape(thecode), true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);

	 }
