﻿function fillCountryCode(a){
if(a.options[a.options.selectedIndex].value!="-1")
{
	a=a.options[a.options.selectedIndex].value.split("-");
	document.getElementById("txtContactCountryCode").value=a[2]
}
else 
	document.getElementById("txtContactCountryCode").value=""
} 

function validateSignUp()
{
	var a=document.getElementById("ddltitle");
	if(a.options[a.options.selectedIndex].value=="-1")
	{
		alert("Please Select Title");
		a.focus();
		return false
	}
	a=document.getElementById("txtSignUpName");
	if(!(chkEmpty(a,"first name")&&chkLength(a,"First name",3,20,"Please enter a minimum of 3 and a maximum of 20 characters for first name")&&isSplChar(a.value)&&isNumber(a.value)&&chkPattern(a,"First name",/^\s*([A-Z]|[a-z])/,"alpha character")))
		return false;
	a=document.getElementById("txtLastName"); 
	if(!(chkEmpty(a,"last name")&&chkLength(a,"Last name",3,20,"Please enter a minimum of 3 and a maximum of 20 characters for last name")&&isSplChar(a.value)&&isNumber(a.value)&&chkPattern(a,"last name",/^\s*([A-Z]|[a-z])/,"alpha character")))
		return false;
	a=document.getElementById("ddlCountry");
	if(a.options[a.options.selectedIndex].value=="-1")
	{
		alert("Please select your country of residence");
		a.focus();
		return false
	}
	a=document.getElementById("txtContactAreaCode");
	if(!(chkEmpty(a,"area code")&&chkSpace(a, "Please delete space(s)")&&chkLength(a,"Area code",1,3,"Area code should be three numerals")&&chkPattern(a,"area code",/^\d*[0-9]?$/,"numeric","Please provide numeric values only")))
		return false;
	a=document.getElementById("txtContactNo");
	if(!(chkEmpty(a,"contact number")&&chkSpace(a,"Please delete space(s) between numbers")&&chkLength(a,"Contact number",7,7,"Please enter seven digits only in Contact No")&&chkPattern(a,"contact number",/^\d*[0-9]?$/,"numeric","Please provide numeric values only in Contact No")))
		return false; 
	a=document.getElementById("txtSignUpEmail");
	if(!(chkEmpty(a,"email address")&&chkPattern(a,"email address",/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,"email address","Please provide email address in the format smith@sxbh.com or smith@sxbh.net")))
		return false;a=document.getElementById("txtSignUpPassword");
	if(!chkEmpty(a,"password"))
		return false;
	a=document.getElementById("txtSignUpConfirmPassword");
	if(!chkEmpty(a,"Confirm Password"))
		return false;
	a=document.getElementById("txtSignUpPassword"); 
	var b=document.getElementById("txtSignUpConfirmPassword");
	if(a.value!=b.value)
	{
		alert("Please re-type your password correctly");
		return false
	}
	CheckExUser(document.getElementById("txtSignUpEmail").value, document.getElementById("Country_pk").value);
		return false
}

function chkEmpty(a,b)
{
	if(a.value=="")
	{
		alert("Please enter your "+b);
		a.focus();
		return false
	}
	else 
		return true
}

function chkLength(a,b,c,d,e)
{
	b=parseInt(c);
	d=parseInt(d);
	if(a.value.length<b||a.value.length>d)
	{
		alert(e);
		a.focus();
		return false
	}
	else 
		return true
} 

function chkPattern(a,b,c,d,e)
{
	if(c.test(a.value))
		return true;
	else
	{
		e!=undefined?alert(e):alert("Please provide a proper "+b);
		a.focus();
		return false
	}
}

function isSplChar(a)
{
	var b;
	b="No";
	for(var c=0;c<a.length;c++)
		for(var d=0;d<39;d++)
			if(a.charAt(c)=='`()(\\~!@^&*+"|%:=,<>'.charAt(d))
			{
				b="Yes";
				break
			}
			else 
				a.charAt(c);
			
			if(b=="Yes")
			{
				alert("Please delete special characters");
				return false
			}
			else 
				if(b=="No")
					return true
} 

function isNumber(a)
{
	var b;
	b="No";
	for(var c=0;c<a.length;c++)
		for(var d=0;d<21;d++)
			if(a.charAt(c)=="0123456789".charAt(d))
			{
				b="Yes";
				break
			}
			else 
				a.charAt(c);
			if(b=="Yes")
			{
				alert("Please delete numeric characters");
				return false
			}
			else 
				if(b=="No")
					return true
}
function chkSpace(a,b)
{
	if(a.value.indexOf(" ")!=-1)
	{
		alert(b);
		return false
	}
	return true
} 
function CheckExUser(a,c)
{
	var b="/MyAccount/formPost.aspx?type=CheckExistingUser";
	CreateXmlHttpRequest();
	b+="&txtEmail="+a+"&Country_pk="+c;
	
	request.onreadystatechange=CheckExUserCallBack;
	request.open("GET",b,true);
	request.send(null)
}
function CheckExUserCallBack()
{
	if(request.readyState==4)
		if(request.status==200)
			if(request.responseText=="0")
			{
				var a=document.getElementById("SignUpForm");
				a.setAttribute("onsubmit","return true");
				a.submit()
			}
			else 
				alert("A user with this email address already regesitered")
}
