/*
	This is the JavaScript file for the How to Create CAPTCHA Protection using PHP and AJAX Tutorial

	You may use this code in your own projects as long as this 
	copyright is left in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.WebCheatSheet.com
	
	Copyright 2006 WebCheatSheet.com	

*/
//----------------------------Telephone Numbers ------------------------------------------//
	
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trimString(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function checkInternationalPhone(strPhone)
{
	var bracket=3
	strPhone=trimString(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	//if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	//if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
//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; 

   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) {
   //Set the content of the DIV element with the response text
   //document.getElementById('result').innerHTML = receiveReq.responseText;
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'create_image.php?' + Math.random();
 }
}

//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = 'captcha.php';
 //Set up the parameters of our AJAX call
 var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr);
}

//CAPTCHA VALIDATION
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function validate_captcha(sec_code,xurl)
{ 
  //alert("validate");
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url=xurl;
	url=url+ "?sec_code="+sec_code+"&sid="+Math.random();
	//url=url+"&sid="+Math.random()
	//alert(url);
	xmlHttp.onreadystatechange=validate_captcha_function

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)

}

function validate_captcha_function() 
{ 
//alert(xmlHttp.responseText);
  var captcha_status=0;
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	  captcha_status = xmlHttp.responseText ;
		//alert("hi"+captcha_status);
		if(parseInt(captcha_status) == 1)
		 {
		   document.frm_main.submit(); 
		 }
		 else
		  {
			   
			   alert("The characters you entered do not match with the image.\nPlease re-enter or refresh the image.");
				//alert("Hello");
			   
			}
	} 
} 




function validate_captcha_mail(sec_code,xurl)
{ 
  //alert("validate");
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url=xurl;
	url=url+ "?sec_code="+sec_code+"&sid="+Math.random();
	//url=url+"&sid="+Math.random()
	//alert(url);
	xmlHttp.onreadystatechange=validate_captcha_function_mail

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)

}

function validate_captcha_function_mail() 
{ 
  var captcha_status=0;
	//alert("inside");
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	  captcha_status = xmlHttp.responseText ;
		//alert("hi"+captcha_status);
		if(parseInt(captcha_status) == 1)
		 {
		   document.frm_main.submit(); 
		 }
		 else
		  {
			   
			   alert("The characters you entered do not match with the image.\nPlease re-enter or refresh the image.");
				//alert("Hello");
			   
			}
	} 
} 

//EN INERT RECOR
function validate_contact_1()
	 {
	 
	    //alert("hi");
			if(document.frm_main.name.value=="")
			 {
			   alert("Please enter your name");
			   document.frm_main.name.focus();
				 return false;
			 }
			if(document.frm_main.email_id.value=="")
			{
			alert("Please enter your email address");
			document.frm_main.email_id.focus();
			return false;
			}



	
	
				var emailaddr = document.frm_main.email_id.value;
			if (emailaddr == '') {
					alert("Email address is mandatory.");
					document.frm_main.email_id.focus();
					 return false;
			}
		
			if (emailaddr == '') return true;
			var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
			for (i=0; i<invalidChars.length; i++) {
				 if (emailaddr.indexOf(invalidChars.charAt(i),0) > -1) {
					 alert("Email address contains invalid characters.");
					document.frm_main.email_id.focus();
					return false;
				 }
			}
			for (i=0; i<emailaddr.length; i++) {
				 if (emailaddr.charCodeAt(i)>127) {
					 alert("Email address contains non ASCII characters.");
					document.frm_main.email_id.focus();
					return false;
				 }
	 
				 
			}
		
			var atPos = emailaddr.indexOf('@',0);
			if (atPos == -1) {
					alert("Email address must contain an @.");
				 document.frm_main.email_id.focus();
				 return false;
			}
			if (atPos == 0) {
				alert("Email address must not start with @.");
				document.frm_main.email_id.focus();
				 return false;
			}
			if (emailaddr.indexOf('@', atPos + 1) > - 1) {
				 alert("Email address must contain only one @");
				 document.frm_main.email_id.focus();
				 return false;
			}
			if (emailaddr.indexOf('.', atPos) == -1) {
				//alert(alertMessages[246]);
				alert("Please Enter Valid Email Address.");
				document.frm_main.email_id.focus();
				 return false;
			}
			if (emailaddr.indexOf('@.',0) != -1) {
				 alert("Period must not immediately follow @ in the email address");
				 document.frm_main.email_id.focus();
				 return false;
			}
			if (emailaddr.indexOf('.@',0) != -1){
				 alert("Period must not immediately precede @ in the email address.");
				 document.frm_main.email_id.focus();
				 return false;
			}
			if (emailaddr.indexOf('..',0) != -1) {
				 alert("Two periods must not be adjacent in the email address.");
				 document.frm_main.email_id.focus();
				 return false;
			}

	
		//alert(document.frm_main.phone.value);
		
			var Phone=document.frm_main.phone
		if (Phone.value!="")
		{
		if (checkInternationalPhone(Phone.value)==false){
			alert("Please Enter a Valid Telephone Number")
			Phone.value=""
			Phone.focus()
			return false
		}			
		}
		
	 
	 if(document.frm_main.security_code.value=="")
	  {
		  alert("Please enter the characters you see in the picture below");
			//alert("TEST");
			document.frm_main.security_code.focus();
			return false;
		}
	 
	 //validate_captcha(document.frm_main.security_code.value,'check_for_captcha.php');
	 
	 
	 }


//END CAPTCHA VALIDATION
