function ValidateNo(NumStr, String) 
{ 
    for(var Idx=0; Idx<NumStr.length; Idx++) 
    { 
        var Char = NumStr.charAt(Idx); 
        var Match = false; 

        for(var Idx1=0; Idx1<String.length; Idx1++) 
        { 
            if(Char == String.charAt (Idx1)) 
                Match = true; 
        } 

        if (!Match) 
            return false; 
    } 
    return true; 
}
//========================   start functions for Phone check =======
 
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 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;
}
 
 
//========================   end functions for Phone check =======
//========================   start functions for Phone check =======
 
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}
 
function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
/*
	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	

*/
//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) {
	
	
	if(document.frmCaptcha.name.value=="")
	{
		  alert('Please enter your name');
		  document.frmCaptcha.name.focus();
		  return false;
	}
	var name=document.frmCaptcha.name.value;
	var company=document.frmCaptcha.company.value;
	var url=document.frmCaptcha.url.value;
	
	
	
	if (document.frmCaptcha.phone.value!=""){
	if(!ValidateNo(document.frmCaptcha.phone.value,"1234567890+- ")) 
	{ 
		alert("Please Enter Only Number"); 
		document.frmCaptcha.phone.focus(); 
		document.frmCaptcha.phone.select();
		//document.getElementById('contactno').style.border = '1px #FF0000 solid';
		return false; 
	}}
	var phone=document.frmCaptcha.phone.value;
	
	if(document.frmCaptcha.email.value=="")
	   {
		  alert('Please enter email address');
		  document.frmCaptcha.email.focus();
		  return false;
	   }
	else if(! isValidEmail(document.frmCaptcha.email.value)) 
	 {
        alert("Please enter a valid email address");
        return false;
    }
	var email=document.frmCaptcha.email.value;
	var organization=document.frmCaptcha.organization.value;
	if(document.frmCaptcha.comment.value=="")
	{
		  alert('Please enter your Enquiry');
		  document.frmCaptcha.comment.focus();
		  return false;
	}
	var comment=document.frmCaptcha.comment.value;
	if(document.frmCaptcha.txtCaptcha.value=="")
	{
		  alert('Please enter a valid code');
		  document.frmCaptcha.txtCaptcha.focus();
		  return false;
	}
	//var params="tname="+name+"&company="+company+"&url="+url+"&phone="+phone+"&email="+email+"&org="+organization+"&comment="+comment+"num="+num;
	
 //Set the URL
 //var url = 'captcha.php';

 //Set up the parameters of our AJAX call
 //var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value);
 var postStr = "txtCaptcha="+encodeURIComponent( theForm.txtCaptcha.value)+"&tname="+name+"&company="+company+"&url="+url+"&phone="+phone+"&email="+email+"&org="+organization+"&comment="+comment;
 //var params=

 //alert(postStr);
  //alert(params);
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr);
 //makeRequest(url, params);
 
 
}
