// JavaScript Document
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 4;






function validateCustomerSignup()
{
	var title,fname,lname,dob_dd,dob_mm,dob_yy,address1,city,country_id,zipcode,telephone,mobile,customer_email,customer_pwd,customer_pwd_confirm;
	
	title = document.frmCustomerSignUp.title.value;
	fname = document.frmCustomerSignUp.fname.value;
	lname = document.frmCustomerSignUp.lname.value;
	dob_dd = document.frmCustomerSignUp.dob_dd.value;
	dob_mm = document.frmCustomerSignUp.dob_mm.value;
	dob_yy = document.frmCustomerSignUp.dob_yy.value;
	address1 = document.frmCustomerSignUp.address1.value;
	city = document.frmCustomerSignUp.city.value;
	country_id = document.frmCustomerSignUp.country_id.value;
	zipcode = document.frmCustomerSignUp.zipcode.value;
	telephone = document.frmCustomerSignUp.telephone.value;
	mobile = document.frmCustomerSignUp.mobile.value;
	customer_email = document.frmCustomerSignUp.customer_email.value;
	customer_pwd = document.frmCustomerSignUp.customer_pwd.value;
	customer_pwd_confirm = document.frmCustomerSignUp.customer_pwd_confirm.value;
	
	
	if(title == "" || title == null)
	{
		alert("Title field cannot be left unselected.\n Please select title");
		document.frmCustomerSignUp.title.focus();
		return false;
	}
	
	if(fname == "" || fname == null || fname == "First Name")
	{
		alert("FirstName field cannot be left empty.\n Please enter Firstname");
		document.frmCustomerSignUp.fname.focus();
		return false;
	}
	
	if(lname == "" || lname == null || lname == "Last Name")
	{
		alert("LastName field cannot be left empty.\n Please enter Lastname");
		document.frmCustomerSignUp.lname.focus();
		return false;
	}
	
	if(dob_dd == "" || dob_dd == null )
	{
		alert("Day field cannot be left unselected.\n Please select Day");
		document.frmCustomerSignUp.dob_dd.focus();
		return false;
	}
	
	if(dob_mm == "" || dob_mm == null )
	{
		alert("Month field cannot be left unselected.\n Please select Month");
		document.frmCustomerSignUp.dob_mm.focus();
		return false;
	}
	
	if(dob_yy == ""  || dob_yy == null )
	{
		alert("Year field cannot be left unselected.\n Please select Year");
		document.frmCustomerSignUp.dob_yy.focus();
		return false;
	}
	
	if(address1 == "" || address1 == null )
	{
		alert("Address field cannot be left empty.\n Please enter Address");
		document.frmCustomerSignUp.address1.focus();
		return false;
	}
	
	if(city == "" || city == null )
	{
		alert("City field cannot be left empty.\n Please enter City");
		document.frmCustomerSignUp.city.focus();
		return false;
	}
	
	if(country_id == "" || country_id == null )
	{
		alert("Country field cannot be left unselected.\n Please select Country");
		document.frmCustomerSignUp.country_id.focus();
		return false;
	}
	
	if(zipcode == "" || zipcode == null )
	{
		alert("Zipcode field cannot be left empty.\n Please enter Zipcode");
		document.frmCustomerSignUp.zipcode.focus();
		return false;
	}
	else
	{
		if( ValidateForm() == false )
			return false;
	}
	
	
	if(telephone == "" || telephone == null )
	{
		alert("Telephone field cannot be left empty.\n Please enter Telephone");
		document.frmCustomerSignUp.telephone.focus();		
		return false;
	}
	else
	{
		if( ValidateForm() == false )
			return false;
	}
	
	if(mobile == "" || mobile == null  )
	{
		
	}
	else
	{
		if( ValidateForm() == false )
			return false;
	}
	
	
	
	if(customer_email.indexOf('@')=='-1'||customer_email.indexOf('.')=='-1')
	{
		alert("Invalid email id.");	
		document.frmCustomerSignUp.customer_email.focus();
		return false;
	}
	
	if(customer_pwd == "")
	{
		alert("Password field cannot be left empty.\n Please enter Password");
		document.frmCustomerSignUp.customer_pwd.focus();
		return false;
	}
	
	if(customer_pwd_confirm == "")
	{
		alert("Password Confirm field cannot be left empty.\n Please re-type Password");
		document.frmCustomerSignUp.customer_pwd_confirm.focus();
		return false;
	}
	
	
	
	return true;
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateForm()
{
	var telephone =document.frmCustomerSignUp.telephone.value;
	var zipcode =document.frmCustomerSignUp.zipcode.value;
	var mobile = document.frmCustomerSignUp.mobile.value;
	
	if ((zipcode==null)||(zipcode==""))
	{
		alert("Please Enter your Zip Code")
		document.frmCustomerSignUp.zipcode.focus()
		return false
	}
	if (checkInternationalPhone(zipcode)==false)
	{
		alert("Please Enter a Valid Zip Code")
		document.frmCustomerSignUp.zipcode.value=""
		document.frmCustomerSignUp.zipcode.focus()
		return false
	}
	
	if ((telephone==null)||(telephone==""))
	{
		alert("Please Enter your Phone Number")
		document.frmCustomerSignUp.telephone.focus()
		return false
	}
	if (checkInternationalPhone(telephone)==false)
	{
		alert("Please Enter a Valid Mobile Number")
		document.frmCustomerSignUp.telephone.value=""
		document.frmCustomerSignUp.telephone.focus()
		return false
	}
	
	
	if (checkInternationalPhone(mobile)==false)
	{
		alert("Please Enter a Valid Mobile Number")
		document.frmCustomerSignUp.mobile.value=""
		document.frmCustomerSignUp.mobile.focus()
		return false
	}
	

	return true
 }
