
var url = 'captcheck.php?code=';
        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;
	}
        
        var http = getHTTPObject(); // We create the HTTP Object        
        
        
		function handleHttpcontactResponse() {
        if (http.readyState == 4) {
            captchaOK = http.responseText;
            if(captchaOK != 1) {
			
			
              alert('The entered code was not correct. Please try again');
              document.contactform.code.value='';
              document.contactform.code.focus();
              return false;
              }
              document.contactform.submit();
           }
        }

        function checkcodecontact(thecode) {
		
        http.open("GET", url + escape(thecode), true);
		http.onreadystatechange = handleHttpcontactResponse;
        http.send(null);
        }

function validator(theForm)
{
	var theForm=document.contactform;

  if (theForm.Name.value == "")
  {
    alert("Please enter a value for the \"Name\" field.");
    theForm.Name.focus();
    return (false);
  }

  if (theForm.Name.value.length < 1)
  {
    alert("Please enter at least 1 characters in the \"Name\" field.");
    theForm.Name.focus();
    return (false);
  }

  if (theForm.Email.value == "")
  {
    alert("Please enter a value for the \"Email\" field.");
    theForm.Email.focus();
    return (false);
  }

  if (theForm.Email.value == "")
		  {
			alert("Please enter a value for the \"Email\" field.");
			theForm.Email.focus();
			return (false);
		  }
		
		  if (theForm.Email.value.length < 1)
		  {
			alert("Please enter at least 1 characters in the \"Email\" field.");
			theForm.Email.focus();
			return (false);
		  }
		  
		  if(!IsEmail(theForm.Email,'Please enter valid email'))
		  {
			return false;
		  }
		  
		  if(!CheckCompanyid(theForm.Email,'Please enter a valid company email address'))
		  {
			return false;
		  }
		  
  if (theForm.Company.value == "")
  {
    alert("Please enter a value for the \"Company\" field.");
    theForm.Company.focus();
    return (false);
  }
   if (theForm.State.value == '0')
  {
    alert("Please Select the State.");
    theForm.State.focus();
    return (false);
  }
   if (theForm.Country.value == '-1')
  {
    alert("Please Select the Country.");
    theForm.Country.focus();
    return (false);
  }

   if (theForm.Phone.value == "")
  {
    alert("Please enter a value for the \"Phone No\" field.");
    theForm.Phone.focus();
    return (false);
  }
  
  if(theForm.code.value=='') 
		  {
          alert('Please enter the string from the displayed image');
          theForm.code.value='';
          theForm.code.focus();
          return false;
          }
          // Now the Ajax CAPTCHA validation
          checkcodecontact(theForm.code.value);
          return false;     
}

function IsEmail(fld,msg)
{
	var regex = /^[\w]+(\.[\w]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/ ;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

/***************************** *****************************/

function CheckCompanyid(fld,msg)
{
		
		email_id = fld.value;
		start = email_id.indexOf("@") + 1;
		end = email_id.indexOf(".");
		email = email_id.substr(start,end - start);

		var array_email =  new 	Array('gmail','google','aol','yahoo','hotmail','facebook');
		
		for(i=0;i<=5;i++)
		{
		 
			 if(email == array_email[i])
			 {
				alert(msg);
				fld.focus();
				return false;
			 }
			
			
		}
			
		return true;

}


