// JavaScript Document
var noErrors=0;
var str;
str= Array(13);
for(i=0;i<str.length;i++)
	str[i]="";
function validate()
{
	noErrors =0;
	for(i=0;i<str.length;i++)
		str[i]="";
	isFname();isLname();isEmail();matchEmail();isPwd();isPwdMatch();isPhone();isMobile();isLoc();isLEarnings();isJobfield();isSubscription();
	if(noErrors==0)
	{
		ertd.innerHTML = '';
		document.forms['regfrm'].submit();
	}
	else
	{
		var tempstr ="";
		for(i=0;i<str.length;i++)
		{
			if(str[i]!="")
				tempstr +=str[i];
		}
		ertd.innerHTML = '<table class="errortable" cellpadding="0" cellspacing="0" border="0" align="center"><tr valign="top"><td><span>You must correct the following errors before you may continue.</span><ul>'+tempstr+'</ul></td></tr></table>';
		return false;
	}
}
function setError(erno,erstr)
{
	str[erno]="<li>"+erstr+"</li>";
	var tempstr ="";
	for(i=0;i<str.length;i++)
	{
		if(str[i]!="")
			tempstr +=str[i];
	}
	if(tempstr!="")
		ertd.innerHTML = '<table class="errortable" cellpadding="0" cellspacing="0" border="0" align="center"><tr valign="top"><td><span>You must correct the following errors before you may continue.</span><ul>'+tempstr+'</ul></td></tr></table>';
	else
		ertd.innerHTML="";
}
function hideError(ch)
{
	switch(ch)
	{
		case 1:str[ch-1]="";document.forms['regfrm'].fname.style.border="solid 1px #dedede";break;
		case 2:str[ch-1]="";document.forms['regfrm'].lname.style.border="solid 1px #dedede";break;
		case 3:str[ch-1]="";document.forms['regfrm'].email.style.border="solid 1px #dedede";break;
		case 4:str[ch-1]="";document.forms['regfrm'].cemail.style.border="solid 1px #dedede";break;
		case 5:str[ch-1]="";document.forms['regfrm'].pwd.style.border="solid 1px #dedede";break;
		case 6:str[ch-1]="";document.forms['regfrm'].pwd.style.border="solid 1px #dedede";document.forms['regfrm'].cpwd.style.border="solid 1px #dedede";break;
		case 7:str[ch-1]="";document.forms['regfrm'].std.style.border="solid 1px #dedede";break;
		case 8:str[ch-1]="";document.forms['regfrm'].phone.style.border="solid 1px #dedede";break;
		case 9:str[ch-1]="";document.forms['regfrm'].mobile.style.border="solid 1px #dedede";break;
		case 10:str[ch-1]="";document.forms['regfrm'].lastearnings.style.color="#000000";break;
		case 11:str[ch-1]="";document.forms['regfrm'].location.style.color="#000000";break;
		case 12:str[ch-1]="";document.forms['regfrm'].locname.style.border="solid 1px #dedede";break;
		
		case 13:str[ch-1]="";break;
		case 14:str[ch-1]="";break;
	}
	var tempstr ="";
	for(i=0;i<str.length;i++)
	{
		if(str[i]!="")
			tempstr +=str[i];
	}
	if(tempstr!="")
		ertd.innerHTML = '<table class="errortable" cellpadding="0" cellspacing="0" border="0" align="center"><tr valign="top"><td><span>You must correct the following errors before you may continue.</span><ul>'+tempstr+'</ul></td></tr></table>';
	else
		ertd.innerHTML="";
}
function isFname()
{
	if(document.forms['regfrm'].fname.value=="")
	{noErrors=1;setError(0,"First name cannot be empty");document.forms['regfrm'].fname.style.border="solid 1px #de0000";}
	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)=="."))
			{noErrors=1;setError(0,"First name is not valid");document.forms['regfrm'].fname.style.border="solid 1px #de0000";break;}
		}
	}
}
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)=="."))
			{noErrors=1;setError(1,"Last name is not valid");document.forms['regfrm'].lname.style.border="solid 1px #de0000";break;}
		}
	}
}
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\"  onFocus=\"hideError(12)\" onBlur=\"isLoc()\">";
		}
		else
		{
			locnamelabeltd.innerHTML = "";
			locnametd.innerHTML = "";
		}
	}
}
function isLoc()
{
	if(document.forms['regfrm'].location.value=="-")
	{noErrors=1;setError(10,"Please select location");document.forms['regfrm'].location.style.color="#de0000";}
	else
	{
		if(document.forms['regfrm'].location.value=="0")
		{
			if(document.getElementById("locname").value=="")
			{noErrors=1;setError(11,"Specify location name");document.getElementById("locname").style.border="solid 1px #de0000";}
		}
	}
}
function isEmail()
{
	if(document.forms['regfrm'].email.value=="")
	{noErrors=1;setError(2,"E-Mail address can not be empty");document.forms['regfrm'].email.style.border="solid 1px #de0000";}
	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))
		{
			noErrors=1;setError(2,"Invalid E-Mail address");document.forms['regfrm'].email.style.border="solid 1px #de0000";
		}
		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") 
		{noErrors=1;setError(2,"E-Mail already exists.");document.forms['regfrm'].email.style.border="solid 1px #de0000";}
	}
}
function matchEmail()
{
	if(document.forms['regfrm'].email.value!="")
	{
		if(document.forms['regfrm'].cemail.value=="")
		{noErrors=1;setError(3,"Confirm E-Mail can not be empty");document.forms['regfrm'].cemail.style.border="solid 1px #de0000";}
		else{
			if(document.forms['regfrm'].email.value!=document.forms['regfrm'].cemail.value)
			{noErrors=1;setError(3,"E-Mail and Confirm E-Mail do not match");document.forms['regfrm'].cemail.style.border="solid 1px #de0000";}
		}
	}
}
function isPwd() {
		if(document.forms['regfrm'].pwd.value.length==0)
		{noErrors=1;setError(4,"Password can not be empty");document.forms['regfrm'].pwd.style.border="solid 1px #de0000";}
		else 
		{
			if(document.forms['regfrm'].pwd.value.length<6)
			{noErrors=1;setError(4,"Password is too short");document.forms['regfrm'].pwd.style.border="solid 1px #de0000";}
			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")))
				{noErrors=1;setError(4,"Staring letter of password should be alpha numeric only");document.forms['regfrm'].pwd.style.border="solid 1px #de0000";}
				
				for(var i=0;i<str.length;i++)
				{
					if(str.charAt(i)==" ")
					{noErrors=1;setError(4,"Password cannot be allow special character space");document.forms['regfrm'].pwd.style.border="solid 1px #de0000";break;}
				}
			}
		}
	}
function isPwdMatch() {
	if(document.forms['regfrm'].pwd.value!="")
	{
		if(document.forms['regfrm'].cpwd.value=="")
		{noErrors=1;setError(5,"Please retype the password");document.forms['regfrm'].cpwd.style.border="solid 1px #de0000";}
		else{
			if(document.forms['regfrm'].pwd.value!=document.forms['regfrm'].cpwd.value)
			{noErrors=1;setError(4,"Passwords do not match");document.forms['regfrm'].pwd.style.border="solid 1px #de0000";}
			else
			{hideError(6);}
		}
	}
}
function isLEarnings()
{
	if(document.forms['regfrm'].lastearnings.value=="-")
	{noErrors=1;setError(9,"Please select last earnings");document.forms['regfrm'].lastearnings.style.color="#de0000";}
}
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)
	{noErrors=1;setError(13,"Please choose subscription type");}
}
function isJobfield()
{
	flag=0;
	for(i=0;i<document.forms['regfrm'].jfield.length;i++)
	{
		if(document.forms['regfrm'].jfield[i].checked==true)
			flag=1;
	}
	if(flag==0)
	{noErrors=1;setError(12,"Please select your field");}
}
function isPhone() {
		var num = document.forms['regfrm'].phone.value;
		var num1 = document.forms['regfrm'].std.value;
		if(document.forms['regfrm'].std.value!="" || document.forms['regfrm'].phone.value!="")
		{
			if(document.forms['regfrm'].std.value=="")
			{noErrors=1;setError(6,"STD code can not be empty");document.forms['regfrm'].std.style.border="solid 1px #de0000";}
			for(var i=0;i<num1.length;i++)
			if(!((num1.charAt(i)>="0" && num1.charAt(i)<="9")))
				{noErrors=1;setError(6,"STD code is not valid");document.forms['regfrm'].std.style.border="solid 1px #de0000";break;}
			if(document.forms['regfrm'].phone.value=="")
			{noErrors=1;setError(7,"Telephone number not be empty");document.forms['regfrm'].phone.style.border="solid 1px #de0000";}
			else
			{
				if(num.length+num1.length<11)
				{noErrors=1;setError(7,"Telphone number should have minimum 11 Digits(STD + phone number).");document.forms['regfrm'].phone.style.border="solid 1px #de0000";document.forms['regfrm'].std.style.border="solid 1px #de0000";}
				else
				{
					for(var i=0;i<num.length;i++)
					if(!((num.charAt(i)>="0" && num.charAt(i)<="9")||num.charAt(i)=="-"))
					{noErrors=1;setError(7,"Telephone number is not valid.");document.forms['regfrm'].phone.style.border="solid 1px #de0000";break;}
				}
			}
		}
	}
	function isMobile() {
		var num = document.forms['regfrm'].mobile.value;
		if(document.forms['regfrm'].mobile.value=="")
		{noErrors=1;setError(8,"Mobile number can not be empty");document.forms['regfrm'].mobile.style.border="solid 1px #de0000";}
		else 
		{
				if(num.length<10)
				{noErrors=1;setError(8,"Mobile number should have minimum 10 digits");document.forms['regfrm'].mobile.style.border="solid 1px #de0000";}
				else
				{
					for(var i=0;i<num.length;i++)
						if(!((num.charAt(i)>="0" && num.charAt(i)<="9")||num.charAt(i)=="-"))
						{noErrors=1;setError(8,"Mobile number is not valid");document.forms['regfrm'].mobile.style.border="solid 1px #de0000";break;}
				}
		}
	}