//  Removed CartId from Line No. 197, 199 by Armour - 23/12/2004

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: This function validates the required fields.
//  @   Pages Affected  : accregister
//  @		Function Name		: validate(option)									
//	@		Input Parameters: option=1->NonSecurepath,2->Securepath
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function validate(option){
	var element = new Array('EMAIL','PASSWORD1','PASSWORD2','FIRSTNAME','LASTNAME','PHONE','STREET','CITY');		
	if (bSecretQues) {
		var element = new Array('EMAIL','PASSWORD1','PASSWORD2','FIRSTNAME','LASTNAME','PHONE','STREET','CITY','SECRET_ANSWER');		
	}
	var index,retval;
	var regExpression;
	
	// ### Making sure Mandatory fields are not null.
	for(index=0;index<=element.length-1;index++)
		if(eval("document.frmNewuser." + element[index] + ".value") == "")
		{
			alert(getMessage("iMandatory"));
			eval("document.frmNewuser." + element[index] + ".focus();");
			eval("document.frmNewuser." + element[index] + ".select();");
			return;
		}
		if (document.frmNewuser.elements["COUNTRY"].options != null){	
			if((document.frmNewuser.COUNTRY.options[document.frmNewuser.COUNTRY.selectedIndex].value == "<To be selected>" )|| (document.frmNewuser.COUNTRY.options[document.frmNewuser.COUNTRY.selectedIndex].value == ""))
				{
				 alert(getMessage("iMandatory"));
				 document.frmNewuser.COUNTRY.focus();
				 return false;
				}
			}
	// ### Validating login name
	if(document.frmNewuser.LOGINNAME.value != ""){
		if(document.frmNewuser.LOGINNAME.value.length < 4){
			alert(getMessage("iLogin4Char"));
			document.frmNewuser.LOGINNAME.focus();
			document.frmNewuser.LOGINNAME.select();
			return;
		}
		if(document.frmNewuser.LOGINNAME.value.indexOf(" ") > -1){
			alert(getMessage("iLoginNoSpace"));
			document.frmNewuser.LOGINNAME.focus();
			document.frmNewuser.LOGINNAME.select();
			return;
		}
		if(document.frmNewuser.LOGINNAME.value.toLowerCase() == "guest"){
			alert(getMessage("iLoginName") + " " + document.frmNewuser.LOGINNAME.value + ".");
			document.frmNewuser.LOGINNAME.focus();
			document.frmNewuser.LOGINNAME.select();
			return;
		}
		regExpression = /^[0-9A-Za-z#:;,\/_><-]*$/ ;
		if(!regExpression.test(document.frmNewuser.LOGINNAME.value)){
			alert(getMessage("iInvalidLoginId"));
			document.frmNewuser.LOGINNAME.focus();
			document.frmNewuser.LOGINNAME.select();
			return false;
		}
	}	

	// ### Validating email.
	ret_val = IsEmail(document.frmNewuser.EMAIL.value);
	if (ret_val == 1){
		document.frmNewuser.EMAIL.focus();
		document.frmNewuser.EMAIL.select();
		return;
	}
	

	// ### Validating password
	if(document.frmNewuser.PASSWORD1.value.length < 4){
		alert(getMessage("iPass4Char"));
		document.frmNewuser.PASSWORD1.focus();
		document.frmNewuser.PASSWORD1.select();
		return;
	}
	if(document.frmNewuser.PASSWORD1.value != document.frmNewuser.PASSWORD2.value){
		alert(getMessage("iPassCPassSame"));
		document.frmNewuser.PASSWORD2.focus();
		document.frmNewuser.PASSWORD2.select();
		return;
	}
	if(document.frmNewuser.PASSWORD1.value.indexOf(" ") > -1){
		alert(getMessage("iPassNoSpace"));
		document.frmNewuser.PASSWORD1.focus();
		document.frmNewuser.PASSWORD1.select();
		return;
	}
	
	// ### Validating First name
	if(document.frmNewuser.FIRSTNAME.value.charAt(0)==" "){	
			alert(getMessage("iValidText"));
			document.frmNewuser.FIRSTNAME.focus();
			document.frmNewuser.FIRSTNAME.select();
			return;
	}

	// ### Validating Last name
	if(document.frmNewuser.LASTNAME.value.charAt(0)==" "){
			alert(getMessage("iValidText"));	
			document.frmNewuser.LASTNAME.focus();
			document.frmNewuser.LASTNAME.select();
			return;
	}
	// ### Validating Company removed(5th Sep 2002)
	/*
	if(document.frmNewuser.COMPANYNAME.value.charAt(0)==" "){
			alert(getMessage("iValidText"));	
			document.frmNewuser.COMPANYNAME.focus();
			document.frmNewuser.COMPANYNAME.select();
			return;
	}
	*/

	// ### Validating City
	if(document.frmNewuser.CITY.value.charAt(0)==" "){	
			alert(getMessage("iValidText"));
			document.frmNewuser.CITY.focus();
			document.frmNewuser.CITY.select();
			return;
	}
	// ### Validating State/ Province
	if(document.frmNewuser.STATE.value == "" )
	{	
			alert(getMessage("iValidText"));
			document.frmNewuser.STATE.focus();
			//document.frmNewuser.STATE.select();
			return;
	}
	/*if(document.frmNewuser.STATE.value != "" && document.frmNewuser.STATE.value.charAt(0)==" "){	
			alert(getMessage("iValidText"));
			document.frmNewuser.STATE.focus();
			document.frmNewuser.STATE.select();
			return;
	}*/	
	

	// ### Validating Phone
	retval=IsTelephone(document.frmNewuser.PHONE.value)
	if(retval == 1)
	{
		alert(getMessage("iValidPhoneNo"));
		document.frmNewuser.PHONE.focus();
		document.frmNewuser.PHONE.select();
		return false;
	}
	
	// ### Validating Fax
	if (document.frmNewuser.FAX.value != "")
	{
	ret_faxval=IsTelephone(document.frmNewuser.FAX.value)
	if(ret_faxval == 1)
	{
		alert(getMessage("iValidFaxNo"));
		document.frmNewuser.FAX.focus();
		document.frmNewuser.FAX.select();
		return false;
	}
	}
	// ### Validating address type
	if (document.frmNewuser.RESI.options[document.frmNewuser.RESI.selectedIndex].value == 0)
	{
		alert(getMessage("iAddressType"));
		document.frmNewuser.RESI.focus();
		return false;
	}
	// ### Validating Street address
	if(document.frmNewuser.STREET.value.length > 255){
		alert(getMessage("iStAddrGT255"));
		document.frmNewuser.STREET.focus();
		return false;
	}
	if(document.frmNewuser.STREET.value.charAt(0)==" "){
		alert(getMessage("iStAddrStartSpace"));
		document.frmNewuser.STREET.focus();
		return false;
	}			
	else
	{
		document.frmNewuser.STREET.value = StreetVal(document.frmNewuser.STREET.value);
	}
	// ### Validating Zipcode
	if(zip_validate("frmNewuser") == false) return false

	
	if (bSecretQues) {
		//### Validating Secret Answer
		if(document.frmNewuser.SECRET_ANSWER.value.indexOf(" ") == 0){
			alert(getMessage("iSecretAnsNoSpace"));
			document.frmNewuser.SECRET_ANSWER.focus();
			document.frmNewuser.SECRET_ANSWER.select();
			return false;
		}
	}
	
// Validating custom tracking fields
	if((document.frmHiddenvalues.allcheckbox.value != "") || (document.frmHiddenvalues.allselbox.value != ""))
	{
		if(validate_mandatory()==0)
			return;
	}
	if(option==2)
		document.frmNewuser.action = sSecurePath+"accregister.asp?msg="+sMessage;
	else
		document.frmNewuser.action = sNonSecurePath+"accregister.asp?msg="+sMessage;
	document.frmNewuser.submit();
}

// End of the function validate(option)


//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description		  	: Validation for Telephone fields  
//  @                       ( Allowed values are +,-,(,),# and from 0 to 9 )
//  @		Function Name		  : IsTelephone(value)								
//	@		Input Parameters	: Value to be tested							
//	@		Return Value		  : 0 - Success											
//	@											  1 - Failure											
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function IsTelephone(value)
{
var sPhone = /^(((\d*)[\s#+\-\(\)]*(\d+))+)$/
   
  if (!(sPhone.test(value)))
		return 1;
  else
		return 0;	
}
//End of the function IsTelephone(Value)



//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: Validation for EMail									@
//  @		Function Name		: IsEmail()												@
//	@		Input Parameters	: Value to be tested									@
//	@		Return Value		: 0 - Success											@
//	@							  1 - Failure											@
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function IsEmail(strEmail)
{
var intReturn =1;
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]'"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=strEmail.match(emailPat)
	if (matchArray==null) {
		alert(getMessage("iValidEmail"));
		return intReturn;
	}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null) {
    alert(getMessage("iValidEmail"));
    return intReturn;
}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert(getMessage("iValidEmail"));
			return intReturn;
			}
		}
		intReturn=0;
		return intReturn;
	}
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert(getMessage("iValidEmail"));
    return intReturn;
}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>4) {
	alert(getMessage("iValidEmail"));
	return intReturn;
	}
var len=domArr.length	
if (len<2) {
   var errStr = getMessage("iValidEmail");
   alert(errStr)
   return intReturn;
}
intReturn=0;
return intReturn;
}
//	End of the function IsEmail(strEmail)


//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: This function is to validate the mandatory fields. For all the
//	@		                  custom tracking fields, name has been given as cust1, cust2 and so on...
//	@		                  And the mandatory fields number (only 1 ,2 like that) stored in a 
//  @                     hidden field(mandatoryfields)Get that number and concatenate it 
//	@		                  with string "cust", validating mandatory fields will be easy.
//  @   Pages Affected  : accregister
//  @		Function Name		: validate_mandatory()									
//	@		Input Parameters: null
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function validate_mandatory()
{
	var t_Count, t_Store,t_Val;
	var chk_arr = new Array(), sel_arr = new Array();

	t_Store = 1;
	chk_arr = document.frmHiddenvalues.allcheckbox.value.split(",");
	sel_arr = document.frmHiddenvalues.allselbox.value.split(",");
	// ### Checking, is there any mandatory select box..
	if(sel_arr[0] !="")	{
		for(t_Count=0;t_Count<sel_arr.length;t_Count++)	{
			if(eval("document.frmNewuser.custsel" + sel_arr[t_Count] + ".options[document.frmNewuser.custsel" + sel_arr[t_Count] + ".selectedIndex].value") == "novalue"){
					eval("document.frmNewuser.custsel" + sel_arr[t_Count] + ".focus()");
					t_Store = 0;
					break;
			}
		}
	}
	if(t_Store == 0){
		alert(getMessage("iEnterCustSurvDet"));
		return(t_Store);
	}

	// ### Checking, is there any mandatory check box..
	if(chk_arr[0] !="")	{
		for(t_Count=0;t_Count<chk_arr.length;t_Count++)	{
			t_Store = 0;
			var checkBox = eval("document.frmNewuser.custcheck" + chk_arr[t_Count]);
			if(eval("checkBox.length"))		{
				for(t_Val=0;t_Val<eval("checkBox.length");t_Val++)	{
					if(eval("checkBox[" + t_Val + "]"  +".checked"))
						t_Store = 1;
				}
				if(t_Store==0)	{
					eval("checkBox[0].focus()");
					break;
				}
			}
			else	{
				if(eval("checkBox.checked"))
					t_Store = 1;
				if(t_Store==0)	{
					eval("checkBox.focus()");
					break;					
				}
			}
		}
	}
	if(t_Store == 0)
		alert(getMessage("iEnterCustSurvDet"));
	return(t_Store);
}

// End of the function validate_mandatory()

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: Function is to validate the zip code. 
//  @   Pages Affected  : accregister									
//  @		Function Name		: zip_validate(form)									
//	@		Input Parameters: formname = name of the form
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function zip_validate(formname){
	
	sForm = eval("document." + formname);
	// ### Validating Zipcode	
		if(sForm.COUNTRY.options[sForm.COUNTRY.selectedIndex].value == "null"){
			alert(getMessage("iSelectCountry"));
			sForm.COUNTRY.focus();
			return false;
		}
		if(sForm.ZIPCODE.value.length == 0){
				alert(getMessage("iValidZipCode"));
				sForm.ZIPCODE.focus();
				sForm.ZIPCODE.select();
				return false;
				}
		if((sForm.COUNTRY.options[sForm.COUNTRY.selectedIndex].value == "USA")||(sForm.COUNTRY.options[sForm.COUNTRY.selectedIndex].value.toLowerCase() == "united states")){
			retval=IsNumeric(sForm.ZIPCODE.value)
			if((retval == 1)||(sForm.ZIPCODE.value.length > 5)){
				alert(getMessage("iValidZipCode"));
				sForm.ZIPCODE.focus();
				sForm.ZIPCODE.select();
				return false;
			}
			if((sForm.STATE.value.length > 2) || (sForm.STATE.value == "") || (sForm.STATE.value.indexOf(" ")>-1)){
				alert(getMessage("iValidStateCode"));
				sForm.STATE.focus();
				//sForm.STATE.select();
				return false;
			}
		}	
	return true;
}
// End of the function zip_validate(formname)

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: Function IsNumeric checks whether the entered value is a 
//	@											numeric and greater than Zero
//  @		Function Name		: IsNumeric(value)									
//	@		Input Parameters: value to be tested
//	@		Return Value		: 0 - Success	  1 - Failure			
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function IsNumeric(value)
{
	var validate=/(^\d+$)/
	if ((validate.test(value)) && (value > 0))
		return 0; 
	else
		return 1; 
}
// End of the function IsNumeric(value)


//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: Function is to validate the alphanumeric characters and  string.  
//  	@   		Pages Affected  			: accregister, contactaddress,manageaddress							
//  	@		Function Name			: isAlphanumeric(str,cSpace)
//	@		Input Parameters			: formname = name of the form
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function isAlphanumeric(str,cSpace){
     var sSpacepat = /^(((\w+)[\s,\-*\.]*)+)$/
     var sPat = /^(((\w+)[*,\-\.]*)+)$/
     if (cSpace=="1"){
		if(!(sSpacepat.test(str)))
		   {
			 alert(getMessage("iValidText"));
			 return false;
	       }	
		}
	 else
	 {
		if(!(sPat.test(str)))  
			{
				alert(getMessage("iValidText"));
				return false;
	        }
	 }
     return true;
}
// End of the function isAlphanumeric(str,cSpace)

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: Function is to validate the street address if tags are entered 
//  @   Pages Affected  : accregister							
//  @		Function Name		: StreetVal(strval)
//	@		Input Parameters: strval = street field
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//Function for street address field which should accept tags like<xml> as normal text

function StreetVal(strval)
{
var cLt = /</g;
var cGt = />/g;

strval = strval.replace(cLt,"&lt;");
sAdd = strval.replace(cGt,"&gt;");

return sAdd
}
// End of the function StreetVal(strval)

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: Function is to list the cities for the entered zipcode.
//  @   Pages Affected  : accregister							
//  @		Function Name		: ListCity(strval)
//	@		Input Parameters: City = city field
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function ListCity(sStateVal)
{
if (document.frmNewuser.selCity.options[document.frmNewuser.selCity.selectedIndex].value == 0)
	{
	//If the user selects no city ,it alerts to select the  city
	alert(getMessage("iSelectCity"));
	return false;
	}
else
	{
	//City selected will be populated in the text field
	document.frmNewuser.CITY.value = document.frmNewuser.selCity.options[document.frmNewuser.selCity.selectedIndex].value
	document.frmNewuser.STATE.value=sStateVal;
	}
}

// End of the function ListCity()

