// JavaScript Document
var noErrors=0;
var str;
str= Array(16);
for(i=0;i<str.length;i++)
	str[i]="";
function validate()
{
	noErrors =0;
	for(i=0;i<str.length;i++)
		str[i]="";
	
	isEmail();matchEmail();isPwd();isMatchPwd();isFname();isLname();isCname();isCType();isWebsite();isIndustry();isStd();isPhone();isMobile();isAddress();isCity();isState();isPin();
	if(noErrors==0)
	{
		ertd.innerHTML = '';
		document.forms[0].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 hideError(ch)
{
	switch(ch)
	{
		case 1:str[ch-1]="";document.forms[0].email.style.border="solid 1px #dedede";break;
		case 2:str[ch-1]="";document.forms[0].cemail.style.border="solid 1px #dedede";break;
		case 3:str[ch-1]="";document.forms[0].pwd.style.border="solid 1px #dedede";break;
		case 4:str[ch-1]="";document.forms[0].cpwd.style.border="solid 1px #dedede";break;
		case 5:str[ch-1]="";document.forms[0].fname.style.border="solid 1px #dedede";break;
		case 6:str[ch-1]="";document.forms[0].lname.style.border="solid 1px #dedede";break;
		case 7:str[ch-1]="";document.forms[0].cname.style.border="solid 1px #dedede";break;
		case 8:str[ch-1]="";document.forms[0].ctype.style.color="#000000";break;
		case 9:str[ch-1]="";document.forms[0].website.style.border="solid 1px #dedede";break;
		case 10:str[ch-1]="";document.forms[0].industry.style.color="#000000";break;
		case 11:str[ch-1]="";document.forms[0].std.style.border="solid 1px #dedede";break;
		case 12:str[ch-1]="";document.forms[0].phone.style.border="solid 1px #dedede";break;
		case 13:str[ch-1]="";document.forms[0].mobile.style.border="solid 1px #dedede";break;
		case 14:str[ch-1]="";document.forms[0].address.style.border="solid 1px #dedede";break;
		case 15:str[ch-1]="";document.forms[0].city.style.border="solid 1px #dedede";break;
		case 16:str[ch-1]="";document.forms[0].state.style.border="solid 1px #dedede";break;
		case 17:str[ch-1]="";document.forms[0].pin.style.border="solid 1px #dedede";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 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 isEmail()
{
	if(document.forms[0].email.value=="")
	{noErrors=1;setError(0,"E-Mail address can not be empty");document.forms[0].email.style.border="solid 1px #de0000";}
	else
	{
		var str=document.forms[0].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(0,"Invalid E-Mail address");document.forms[0].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(0,"E-Mail already exists.");document.forms[0].email.style.border="solid 1px #de0000";}
	}
}
function matchEmail()
{
	if(document.forms[0].email.value!="")
	{
		if(document.forms[0].cemail.value=="")
		{noErrors=1;setError(1,"Confirm E-Mail can not be empty");document.forms[0].cemail.style.border="solid 1px #de0000";}
		else{
			if(document.forms[0].email.value!=document.forms[0].cemail.value)
			{noErrors=1;setError(1,"E-Mail and Confirm E-Mail did not match");document.forms[0].cemail.style.border="solid 1px #de0000";}
		}
	}
}
function isPwd()
{
	if(document.forms[0].pwd.value.length==0)
	{noErrors=1;setError(2,"Password can not be empty");document.forms[0].pwd.style.border="solid 1px #de0000";}
	else 
	{
		if(document.forms[0].pwd.value.length<6)
		{noErrors=1;setError(2,"Password is too short");document.forms[0].pwd.style.border="solid 1px #de0000";}
		else
		{
			var str=document.forms[0].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[0].pwd.style.border="solid 1px #de0000";}
			for(var i=0;i<str.length;i++)
			{
				if(str.charAt(i)==" ")
				{noErrors=1;setError(2,"Password cannot be allow special character space");document.forms[0].pwd.style.border="solid 1px #de0000";break;}
			}
		}
	}
}
function isMatchPwd()
{
	if(document.forms[0].pwd.value!="")
	{
		if(document.forms[0].cpwd.value=="")
		{noErrors=1;setError(3,"Please retype the password");document.forms[0].cpwd.style.border="solid 1px #de0000";}
		else{
			if(document.forms[0].pwd.value!=document.forms[0].cpwd.value)
			{noErrors=1;setError(2,"Passwords did not match");document.forms[0].pwd.style.border="solid 1px #de0000";}
		}
	}
}
function isFname()
{
	if(document.forms[0].fname.value=="")
	{noErrors=1;setError(4,"Fist name cannot be empty");document.forms[0].fname.style.border="solid 1px #de0000";}
	else
	{
		var str=document.forms[0].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(4,"First name is not valid");document.forms[0].fname.style.border="solid 1px #de0000";break;}
		}
	}
}
function isLname()
{
	if(document.forms[0].lname.value!="")
	{
		var str=document.forms[0].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(5,"Last name is not valid");document.forms[0].lname.style.border="solid 1px #de0000";break;}
		}
	}
}
function isCname()
{
	if(document.forms[0].cname.value=="")
	{noErrors=1;setError(6,"Company name cannot be empty");document.forms[0].cname.style.border="solid 1px #de0000";}
	else
	{
		var str=document.forms[0].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(6,"Company name is not valid");document.forms[0].cname.style.border="solid 1px #de0000";break;}
		}
	}
}
function isCType()
{
	if(document.forms[0].ctype.value=="-")
	{noErrors=1;setError(7,"Please select company type");document.forms[0].ctype.style.color="#de0000";}
}
function isWebsite()
{
	if(document.forms[0].website.value=="")
	{noErrors=1;document.forms[0].website.style.border="solid 1px #de0000";setError(8,"Please specify website");}
	else
	{
		var str = document.forms[0].website.value; 
		filter = /^(((www.){1}))[-a-zA-Z0-9@:%_\+.~#?&]+\.([A-Za-z]{2,5})$/;
		if(!filter.test(str))
		{
			noErrors=1;setError(8,"Website URL is not valid");document.forms[0].email.style.border="solid 1px #de0000";
		}

	}
}
function isIndustry()
{
	if(document.forms[0].industry.value=="-")
	{noErrors=1;setError(9,"Please select industry");document.forms[0].industry.style.color="#de0000";}
}
function isStd()
{
	if(document.forms[0].std.value=="")
	{
		noErrors=1;setError(10,"STD code can not be empty");document.forms[0].std.style.border="solid 1px #de0000";	
	}
	var num = document.forms[0].std.value;
	if(document.forms[0].std.value!="")
	{
			for(var i=0;i<num.length;i++)
				if(!((num.charAt(i)>="0" && num.charAt(i)<="9")))
				{noErrors=1;setError(10,"STD code is not valid.");document.forms[0].std.style.border="solid 1px #de0000";break;}
	}
}
function isPhone()
{
	if(document.forms[0].phone.value=="")
	{
		noErrors=1;setError(11,"Telphone number can not be empty.");document.forms[0].phone.style.border="solid 1px #de0000";	
	}
	var num = document.forms[0].phone.value;
	var num1 = document.forms[0].std.value;
	if(document.forms[0].phone.value!="")
	{
		if(num.length+num1.length<11)
		{noErrors=1;setError(11,"Telphone number should have minimum 11 digits(STD + Phone number).");document.forms[0].phone.style.border="solid 1px #de0000";}
		else{
			for(var i=0;i<num.length;i++)
			if(!((num.charAt(i)>="0" && num.charAt(i)<="9")))
			{noErrors=1;setError(11,"Telephone number is not valid.");document.forms[0].phone.style.border="solid 1px #de0000";break;}
		}
	}
}
function isMobile()
{
	if(document.forms[0].mobile.value!="")
	{
		var num = document.forms[0].mobile.value;
		if(num.length<10)
		{noErros=1;document.forms[0].mobile.style.border="solid 1px #de0000";setError(12,"Mobile number should have 10 digits");}
		else
		{
			for(var i=0;i<num.length;i++)
			if(!((num.charAt(i)>="0" && num.charAt(i)<="9")||num.charAt(i)=="-"))
			{noErrors=1;document.forms[0].mobile.style.border="solid 1px #de0000";setError(12,"Mobile number is not valid");}
		}
	}
}
function isAddress()
{
	if(document.forms[0].address.value=="")
	{noErrors=1;document.forms[0].address.style.border="solid 1px #de0000";setError(13,"Address cannot be empty");}
}
function isCity()
{
	if(document.forms[0].city.value=="")
	{noErrors=1;document.forms[0].city.style.border="solid 1px #de0000";setError(14,"City cannot be empty");}
}
function isState()
{
	if(document.forms[0].state.value=="")
	{noErrors=1;document.forms[0].state.style.border="solid 1px #de0000";setError(15,"State cannot be empty");}
}
function isPin()
{
	if(document.forms[0].pin.value=="")
	{noErrors=1;document.forms[0].pin.style.border="solid 1px #de0000";setError(16,"Pin/Zip code cannot be empty");}
	else
	{
		var num = document.forms[0].pin.value;
		for(var i=0;i<num.length;i++)
		if(!((num.charAt(i)>="0" && num.charAt(i)<="9")))
		{noErrors=1;document.forms[0].pin.style.border="solid 1px #de0000";setError(16,"Pin/Zip code is not valid");break;}
	}
}
function catchKey(event)
{
	if (event.keyCode == 13) 
		validate();
}
