var url = 'captcheck.php?code='; var captchaOK = 2; var x=0;// 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 == 4) 
	{ 
		captchaOK = http.responseText; 
		if(captchaOK != 1) 
		{ 
			alert('The entered code was not correct. Please try again'); 
			document.form1.code.value=''; 
			document.form1.code.focus(); 
			return false; 
		} 
		alert("Enquiry Submitted Successfuly");
		document.form1.submit(); 
	} 	
} 
function checkcode(thecode) 
{ 
	 // Now the Ajax CAPTCHA validation 

	http.open("GET", url + escape(thecode), true); 
	http.onreadystatechange = handleHttpResponse; 
	http.send(null); 
} 
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
function validate() {
		var form = document.forms[0];
		var bRequired = true;
		if ((form.name.value.length < 1) ||
		(form.email.value.length < 1)) {
			alert("Please fill out all the required fields.");
			bRequired = false;
		}
		else  if (echeck(form.email.value)==false){
		form.email.value=""
		form.email.focus()
		bRequired = false
		}
		else if(form.contact.value.length < 8) 
		{
			alert("Please enter proper contact no. ");
			form1.contact.value="";
			form1.contact.focus();
			bRequired=false;
		}
		else if(document.form1.code.value=='') 
		{ 
			alert('Please enter the string from the displayed image'); 
			document.form1.code.value=''; 
			document.form1.code.focus(); 
			bRequired = false;
	
		}
		else{
			bRequired=checkcode(document.form1.code.value); 		
			bRequired= false;
		}
		return bRequired;
		
}
