// CODE TO CHECK CAPTCHA VALIDATION ..................................//
var url = 'captcheck.php?code='; 
var captchaOK = 2;
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(); 
function handleHttpResponse() 
{ //alert("testing");
	if (http.readyState == 4) 
	{ 
	captchaOK = http.responseText; 
	//alert(captchaOK);
	if(captchaOK != 1) 
	{ 
		alert('Security Code is not correct. Please try again'); 
		document.cform.security_code.value='';
		document.cform.security_code.focus(); 
		 return false; 
	}
document.cform.submit(); 
	} 
} 
function checkcode(thecode) 
{ 
	http.open("POST", url + escape(thecode), true); 
	http.onreadystatechange = handleHttpResponse; 
	http.send(null); 
} 
// CAPTCHA VALIDATION CODE END.....................................//




function is_email(email)
	{
		if(!email.match(/^[A-Za-z0-9\._\-+]+@[A-Za-z0-9_\-+]+(\.[A-Za-z0-9_\-+]+)+$/))
			return false;
		return true;
	}

function is_empty(str)
	{
  		 str=trim(str);
		 if ((str.length==0)||(str==null))
			return true;
		 return false;
	}

	function trim(inputString) 
	{
	   // Removes leading and trailing spaces from the passed string. Also removes
	   // consecutive spaces and replaces it with one space. If something besides
	   // a string is passed in (null, custom object, etc.) then return the input.
	   if (typeof inputString != "string") { return inputString; }
	   var retValue = inputString;
	   var ch = retValue.substring(0, 1);
	   while (ch == " ") { // Check for spaces at the beginning of the string
		  retValue = retValue.substring(1, retValue.length);
		  ch = retValue.substring(0, 1);
	   }
	   ch = retValue.substring(retValue.length-1, retValue.length);
	   while (ch == " ") { // Check for spaces at the end of the string
		  retValue = retValue.substring(0, retValue.length-1);
		  ch = retValue.substring(retValue.length-1, retValue.length);
	   }
	   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
		  retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	   }
	   return retValue; // Return the trimmed string back to the user
	} // Ends the "trim" function

function is_number(number)
	{
		if(!number.match(/^[\-\+0-9e1-9]+$/))
			return false;
		return true;
	}	
	
function is_passpot(alfanumber)
	{
		if(!alfanumber.match(/^[0-9a-bA-B]+$/))
			return false;
		return true;
	}	
function is_pass(abc)
{
	if(!abc.match(/^[A-Za-z\d]{6,8}$/))
	return false;
	return true;
}
function is_alpha(str)
	{
		if(!str.match(/^[A-Za-z ]+$/))
			return false;
		return true;
	}

//Phone Validation
function isValidPhoneNo(strng)
{
//strip out acceptable non-numeric characters
var stripped = strng.replace(/[\(\)\.\+\-\ ]/g, '');
	return !isNaN(stripped);
}

//##  Online Quote Form Validation ####/
//##  Online Quote Form Validation ####/
function validation1()
{ 
 if(is_empty(document.loginform.log.value))
	{
     alert("Please Enter Your Username");
	 document.loginform.log.focus();
	 return false;

	}
	
	if(is_empty(document.loginform.pwd.value))
	{
     alert("Please Enter Your Password");
	 document.loginform.pwd.focus();
	 return false;
	}
	
}


///#### New user registration form validation.
function validation()
{ 

if(is_empty(document.registerform.username.value))
	{
     document.registerform.username.value="";  
     alert("Please Enter Username");
	 document.registerform.username.focus();
	 return false;

	}
 if(is_empty(document.registerform.first_name.value))
	{
     document.registerform.first_name.value="";  
     alert("Please Enter Your First Name");
	 document.registerform.first_name.focus();
	 return false;

	}
	if(!is_alpha(document.registerform.first_name.value))
	{
     alert("Please enter a valid name.");
	 document.registerform.first_name.focus();
	 return false;
	}	 
	
	
	if(is_empty(document.registerform.last_name.value))
	{
     document.registerform.last_name.value="";  
     alert("Please Enter Your Last Name");
	 document.registerform.last_name.focus();
	 return false;

	}
	if(!is_alpha(document.registerform.last_name.value))
	{
     alert("Please enter a valid Last name.");
	 document.registerform.last_name.focus();
	 return false;
	}	 
	if(document.registerform.user_email.value=="")
	{
     alert("Please enter your email address.");
	 document.registerform.user_email.focus();
	 return false;

	}

	if(!is_email(document.registerform.user_email.value))
	{
     alert("Please enter a valid email address.");
	 document.registerform.user_email.focus();
	 return false;
    }
	if(document.registerform.phone_number.value=="")
	{
     alert("Please enter your phone number.");
	 document.registerform.phone_number.focus();
	 return false;

	}

	if(!isValidPhoneNo(document.registerform.phone_number.value))
	{
     alert("Please enter a valid phone number.");
	 document.registerform.phone_number.focus();
	 return false;
    }
	
	if(document.registerform.idnumber .value=="")
	{
     alert("Please enter your passport number");
	 document.registerform.idnumber .focus();
	 return false;

	}
	if(document.registerform.user_password.value=="")
	{
     alert("Please enter password");
	 document.registerform.user_password.focus();
	 return false;

	}
	if(document.registerform.user_password1.value=="")
	{
     alert("Please enter confirm password");
	 document.registerform.user_password1.focus();
	 return false;

	}
	if(document.registerform.user_password.value != document.registerform.user_password1.value)
	{
     alert("Password and confirm password must be same");
	 document.registerform.user_password1.focus();
	 return false;
	}
}

/* Username name availability checking.*/
function AjaxFunction1(username)
{
var httpxml;

try

{

// Firefox, Opera 8.0+, Safari

httpxml=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

httpxml=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

try

{

httpxml=new ActiveXObject("Microsoft.XMLHTTP");

}

catch (e)

{

alert("Your browser does not support AJAX!");

return false;

}

}

}

function stateck()

{

if(httpxml.readyState==4)

{

document.getElementById("msgid").innerHTML=httpxml.responseText;



}

}

var url="avail-ajax.php";

url=url+"?username="+username;

url=url+"&sid="+Math.random();

httpxml.onreadystatechange=stateck;

httpxml.open("GET",url,true);

httpxml.send(null);

}

/* Email id availability checking */
function AjaxFunction2(email)
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateck()
{
if(httpxml.readyState==4)
{
document.getElementById("emailid").innerHTML=httpxml.responseText;
}
}
var url="avail-ajax.php";
url=url+"?email="+email;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
}

//### Forgot Password

function ForgetPasswordRecovery()
{
	//alert(document.lostpasswordform.user_login.value);
	 if(is_empty(document.lostpasswordform.user_login.value))
	{
	 document.lostpasswordform.user_login.value="";
     alert("Please enter your email address.");
	 document.lostpasswordform.user_login.focus();
	 return false;
	}
	if(!is_email(document.lostpasswordform.user_login.value))
	{
     alert("Please enter a valid email address.");
	 document.lostpasswordform.user_login.value="";
	 document.lostpasswordform.user_login.focus();
	 return false;
	}
	var captchaOK=2;
	var str=document.lostpasswordform.user_login.value;
	//alert(str);
	if (window.XMLHttpRequest)
  	{	xmlhttp=new XMLHttpRequest();
		//alert("INXML");
  	}
	else
  	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		//alert("INMICRO");
  	}
	xmlhttp.onreadystatechange=function()
  	{
		//alert("onreadystatechange");
  		if (xmlhttp.readyState==4 )
    	{
    		//document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
			captchaOK = xmlhttp.responseText; 
			//alert(captchaOK);
			if(captchaOK !=1) 
			{ 
				alert('Your Email doesnot exist. Please try again'); 
				document.lostpasswordform.user_login.value='';
				document.lostpasswordform.user_login.focus(); 
				return false; 
			}
			alert("Your User Name & Password has been sent. Please check to Login...");
			document.lostpasswordform.submit(); 
		} 
  
	}
	xmlhttp.open("GET","EmailCheck.php?email="+str,true);
	xmlhttp.send(null);
	return false;
}

///#### Contact us validation.
function cvalid()
{ 
 if(is_empty(document.cform.fname.value))
	{
     document.cform.fname.value="";  
     alert("Please Enter Your First Name");
	 document.cform.fname.focus();
	 return false;

	}
	if(!is_alpha(document.cform.fname.value))
	{
     alert("Please enter a valid name.");
	 document.cform.fname.focus();
	 return false;
	}	 
	
	
	if(is_empty(document.cform.lname.value))
	{
     document.cform.lname.value="";  
     alert("Please Enter Your Last Name");
	 document.cform.lname.focus();
	 return false;

	}
	if(!is_alpha(document.cform.lname.value))
	{
     alert("Please enter a valid Last name.");
	 document.cform.lname.focus();
	 return false;
	}	 
	if(document.cform.email.value=="")
	{
     alert("Please enter your email address.");
	 document.cform.email.focus();
	 return false;

	}

	if(!is_email(document.cform.email.value))
	{
     alert("Please enter a valid email address.");
	 document.cform.email.focus();
	 return false;
    }
	if(document.cform.phone.value=="")
	{
     alert("Please enter your phone number.");
	 document.cform.phone.focus();
	 return false;

	}

	if(!isValidPhoneNo(document.cform.phone.value))
	{
     alert("Please enter a valid phone number.");
	 document.cform.phone.focus();
	 return false;
    }
	
	if(is_empty(document.cform.message.value))
	{
	 document.cform.message.value="";
     alert("Please enter your message.");
	 document.cform.message.focus();
	 return false;
	}
	if(is_empty(document.cform.security_code.value))
	{
	 document.cform.security_code.value="";
     alert("Please enter your Security Code.");
	 document.cform.security_code.focus();
	 return false;
	}
	checkcode(document.cform.security_code.value); 
	return false;
	
}
