// JavaScript Document
function validate()
{
	if(isFname() && isLname() && isEmail() && matchEmail() && isPwd() && isPwdMatch() && isLoc() && isLEarnings() && isSubscription())
	{
		document.forms['regfrm'].submit();
	}
}
function hideError(ch)
{
}
function isFname()
{
	if(document.forms['regfrm'].fname.value=="")
	{alert("Fist name cannot be empty");document.forms['regfrm'].fname.focus();return false;}
	else
	{
		var str=document.forms['regfrm'].fname.value;
		for(var i=0;i<str.length;i++)
		{
			if(!((str.charAt(i)>="A" && str.charAt(i)<="Z") || (str.charAt(i)>="a" && str.charAt(i)<="z") || str.charAt(i)==" " ||str.charAt(i)=="."))
			{alert("First name is not valid");document.forms['regfrm'].fname.focus();return false;}
		}
	}
	return true;
}
function isLname()
{
	if(document.forms['regfrm'].lname.value!="")
	{
		var str=document.forms['regfrm'].lname.value;
		for(var i=0;i<str.length;i++)
		{
			if(!((str.charAt(i)>="A" && str.charAt(i)<="Z") || (str.charAt(i)>="a" && str.charAt(i)<="z") || str.charAt(i)==" " ||str.charAt(i)=="."))
			{alert("Last name is not valid");document.forms['regfrm'].lname.focus();return false;}
		}
	}
	return true;
}
function changeLoc()
{
	var loc = document.forms['regfrm'].location.value;
	if(loc!="-")
	{
		if(loc==0) // Selected is Other location
		{
			locnamelabeltd.innerHTML = "Specify Location";
			locnametd.innerHTML = "<input type=text id=\"locname\" name=\"locname\" class=\"textbox\">";
		}
		if(loc!=0)
		{
			locnamelabeltd.innerHTML = "";
			locnametd.innerHTML = "";
		}
	}
}
function isLoc()
{
	if(document.forms['regfrm'].location.value=="-")
	{alert("Please select location");document.forms['regfrm'].location.focus();return false;}
	else
	{
		if(document.forms['regfrm'].location.value=="0")
		{
			if(document.getElementById("locname").value=="")
			{alert("Specify location name");document.getElementById("locname").focus();return false;}
		}
	}
	return true;
}
function isEmail()
{
	if(document.forms['regfrm'].email.value=="")
	{alert("E-Mail address can not be empty");document.forms['regfrm'].email.focus();return false;}
	else
	{
		var str=document.forms['regfrm'].email.value;
		var filter =/^[a-zA-Z_]([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,5})$/;
		if(!filter.test(str))
		{
			alert("Invalid E-Mail address");document.forms['regfrm'].email.focus();return false;
		}
		var oXMLHTTP;
		if (window.XMLHttpRequest) // Mozilla, Safari, ...
			var oXMLHTTP = new XMLHttpRequest();
		else if (window.ActiveXObject) // IE
			var oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		// Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
		var sURL = "ajax/reg_verifyemail.php?email="+str;
		oXMLHTTP.open( "POST", sURL, false );
		// Execute the request
		oXMLHTTP.send(sURL);
		var str=oXMLHTTP.responseText;
		var str1 = str;
		if (str1 == "a") 
		{alert("E-Mail already exists.");document.forms['regfrm'].email.focus();return false;}
	}
	return true;
}
function matchEmail()
{
	if(document.forms['regfrm'].email.value!=document.forms['regfrm'].cemail.value)
	{alert("E-Mail and confirm E-Mail did not match.");document.forms['regfrm'].email.focus();return false;}
	return true;
}
function isPwd() {
		
		if(document.forms['regfrm'].pwd.value=="")
		{alert("Password can not be empty");document.forms['regfrm'].pwd.focus();return false;}
		else if(document.forms['regfrm'].pwd.value.length<6)
		{alert("Password is too short");document.forms['regfrm'].pwd.focus();return false;}
		else
		{
			var str=document.forms['regfrm'].pwd.value;
			if(!((str.charAt(0)>="a" && str.charAt(0)<="z") || (str.charAt(0)>="A" && str.charAt(0)<="Z") || (str.charAt(0)>="0" && str.charAt(i)<="9")))
				{alert("First letter of password should be alphanumeric");document.forms['regfrm'].pwd.focus();return false;}
			for(var i=0;i<str.length;i++)
			{
				if(str.charAt(i)==" ")
				{alert("Password cannot be allow special character space");document.forms['regfrm'].pwd.focus();return false;}
			}
		}
		return true;
	}
function isPwdMatch() {
	if(document.forms['regfrm'].cpwd.value=="")
	{alert("Please retype password.");document.forms['regfrm'].cpwd.focus();return false;}
	if(document.forms['regfrm'].pwd.value!=document.forms['regfrm'].cpwd.value)
	{alert("Passwords did not match");document.forms['regfrm'].pwd.focus();return false;}
	return true;
}
function isLEarnings()
{
	if(document.forms['regfrm'].lastearnings.value=="-")
	{alert("Please select last earnings");document.forms['regfrm'].lastearnings.focus();return false;}
	return true;
}
function isSubscription()
{
	flag=0;
	for(i=0;i<document.forms['regfrm'].subtype.length;i++)
	{
		if(document.forms['regfrm'].subtype[i].checked==true)
			flag=1;
	}
	if(flag==0)
	{alert("Please select subscription type");return false;}
	return true;
}
