var IndexVal; // used in sec_ans_validate(frmname), secretValidate() & checkreguser()

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: This function will validate the secret answer(s). It passes the 
//  @                     Indexval to the main function through global variable.
//	@	  Pages Affected  : acclogin.asp
//  @		Function Name		: sec_ans_validate(frmname)									
//	@		Input Parameters: form name
//	@		Output Parameters: 0-success, 1-failure
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function sec_ans_validate(frmname){
	var iIndex, len, answer_count, selected_index, name;
	answer_count = 0;
	
	frm = eval("document." + frmname);
	len = frm.elements.length;
	for(iIndex=0;iIndex<=parseInt(len)-1;iIndex++){
		if(frm.elements[iIndex].type.substring(0,4) == "text"){
			if((frm.elements[iIndex].value != "") && (frm.elements[iIndex].value.indexOf(" ") < 0)){
				answer_count++
				selected_index = iIndex 
			}
		}
	}
	
	if(answer_count < 1){
		alert(getMessage("iEnterSecretAns"));
		return 1;//failure
	}
	if(answer_count > 1){
		alert(getMessage("iMoreThanOneSecretAns"));
		return 1;//failure
	}
	name = frm.elements[selected_index].name;
	IndexVal = name.substring(13,name.length);
	return 0;//success
}// End of the function sec_ans_validate(frmname)

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: This function will validate the secret answer(s). It calls  
//  @                     sec_ans_validate() for validating and assigns the acc name and 
//  @                     secret answer to another form and submits the form.
//	@	  Pages Affected  : acclogin.asp
//  @		Function Name		: secretValidate(option)								
//	@		Input Parameters: option=1->NonSecurepath,2->Securepath
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function secretValidate(option)
{

	if(document.frmForgot.secret_answer.value=="") {
		alert(getMessage("iEnterSecretAns"));
		return false;
	}
	if(document.frmForgot.secret_answer.value.indexOf(" ") == 0){
		alert(getMessage("iSecretAnsNoSpace"));
		document.frmForgot.secret_answer.focus();
		document.frmForgot.secret_answer.select();
		return false;
	}
	document.frmdumForgot.accname.value = document.frmForgot.accname.value;
	document.frmdumForgot.addr_code.value = document.frmForgot.addr_code.value;
	document.frmdumForgot.secret_answer.value = document.frmForgot.secret_answer.value
	if(option==2)
		document.frmdumForgot.action = sSecurePath + "sendmail.asp?msg=" + sMessage + "&url=AL&Tp=" + sTpCatalog;
	else
		document.frmdumForgot.action = sNonSecurePath + "sendmail.asp?msg=" + sMessage + "&url=AL&Tp=" + sTpCatalog;
	document.frmdumForgot.submit();
	return false;
}// End of the function secretValidate(option)

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@	Description			: This function will validate the the login field for forgot password part.
//	@	Pages Affected  : acclogin.asp
//  @	Function Name		: validateMail()
//	@	Input Parameters: null
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function validateMail()
{
	var regExpression;
	regExpression = /^[^'^"]*$/;	// Checking for the existence of ' and " in login name
	if((document.frmForgot.forgotLogin.value == "") || (document.frmForgot.forgotLogin.value.indexOf(" ") > -1) || (!regExpression.test(document.frmForgot.forgotLogin.value))){
		alert(getMessage("iLoginEmail"));
		document.frmForgot.forgotLogin.focus();
		document.frmForgot.forgotLogin.select();
		return false;
	}
	document.frmForgot.submit();
}// End of the function validateMail()

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: This function will validate the the login field and the password 
//  @                     field for the main login part.
//	@	  Pages Affected  : acclogin.asp
//  @		Function Name		: loginme(option)								
//	@		Input Parameters: option=1->NonSecurepath,2->Securepath
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function loginme(option)
{
	var radSelValue;
	//for (var x=0; x<document.frmLogin.radpass.length; x++)
	//{
		//if (document.frmLogin.radpass[x].checked) {
		//	alert(document.frmLogin.radpass[x].value);
		//	radSelValue = document.frmLogin.radpass[x].value ;
		//	document.frmLogin.radSelValue.value = radSelValue;
		//}
	//}
	radSelValue = 2;
	document.frmLogin.radSelValue.value = radSelValue;
	if (document.frmLogin.EMAIL.value == "")
	{
		alert("Please enter E-mail Address");
		document.frmLogin.EMAIL.focus();
		return ;
	}
	if(document.frmLogin.EMAIL.value.indexOf(" ") > -1)
	{
		alert("E-mail Adddress or Password can not have spaces");
		document.frmLogin.EMAIL.focus();
		return ;
	}
	if(option==2)
		document.frmLogin.action = sSecurePath + "SmartLogin.asp?msg=" + sMessage + "&ic=" + ic + "&EqCode=" + EqCode + "&Qty=" + Qty + "&type=" + sDocType + "&order=" + sDocNum + "&url=AL&SSL=1&Tp=" + sTpCatalog;
	else
		document.frmLogin.action = sNonSecurePath + "SmartLogin.asp?msg=" + sMessage + "&ic=" + ic + "&EqCode=" + EqCode + "&Qty=" + Qty + "&type=" + sDocType + "&order=" + sDocNum + "&url=AL&Tp=" + sTpCatalog;
//	alert(document.frmLogin.action);
	document.frmLogin.submit();
}

function validate(option)
{
	
	var element = new Array('EMAIL','FIRSTNAME','LASTNAME','ADDRLINE1','CITY','STATE','ZIPCODE');
	var index;

	// ### Making sure Mandatory fields are not null.
	for(index=0;index<=element.length-1;index++)
		if(eval("document.frmNewuser." + element[index] + ".value") == "")
		{
			alert("Please enter value. The labels in bold are mandatory fields.");
			eval("document.frmNewuser." + element[index] + ".focus();");
			eval("document.frmNewuser." + element[index] + ".select();");
			return;
		}
	// ### Validating email.
	var strEmail = document.frmNewuser.EMAIL.value;
	if (validEmail(strEmail)== 0){
		document.frmNewuser.EMAIL.focus();
		return;
	}
	
	// ### Validating Zipcode
	if(isNaN(document.frmNewuser.ZIPCODE.value))
	{
		alert("Please enter valid zipcode");
		document.frmNewuser.ZIPCODE.focus();
		document.frmNewuser.ZIPCODE.select();
		return;
	}
	
	if(option==2)
		document.frmNewuser.action = sSecurePath + "SmartLogin.asp?msg=" + sMessage + "&SSL=1&url=SL";
	else
		document.frmNewuser.action = sNonSecurePath + "SmartLogin.asp?msg=" + sMessage + "&url=SL";
	document.frmNewuser.submit();
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//
//@		Description			: This function validates the Email Address.										@//
//@   Pages Affected  : Accregister,ContactAddress,UpdateAddress,Savecart and other   @//
//@											pages where Email validation is required.											@//
//@		Function Name		: validEmail(strEmail)																					@//
//@		Input Parameter	: strEmail:- The Email to be validated.													@//
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//

function validEmail(strEmail){
	var intReturn =0;
	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("Please enter a valid Email address.");
			return intReturn;
		}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		alert("Please enter a valid Email address.");
		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("Please enter a valid Email address.");
				return intReturn;
				}
			}
			intReturn=1;
			return intReturn;
		}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("Please enter a valid Email address.");
		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>3) {
		alert("Please enter a valid Email address.");
		return intReturn;
		}
	var len=domArr.length	
	if (len<2) {
	   var errStr="Please enter a valid Email address."
	   alert(errStr)
	   return intReturn;
	}
	intReturn=1;
	return intReturn;
} // End of the function validEmail(strEmail)

function SmLogin(option){
	var regExpression, radVal;
	regExpression = /^[^'^"]*$/;		// Checking for the existence of ' and " in login name
	if (document.frmLogin.accname.value == ""){
		alert(getMessage("iLoginEmpty"));
		document.frmLogin.accname.focus();
		return false;
	}
	if (!regExpression.test(document.frmLogin.accname.value)){
		alert("Please enter valid login name.");
		document.frmLogin.accname.focus();
		return false;
	}
	if((document.frmLogin.accname.value.indexOf(" ") > -1) || (document.frmLogin.mypassword.value.indexOf(" ") > -1)){
			alert(getMessage("iLoginPassNoSpace"));
			document.frmLogin.accname.focus();
			return false;
		}
	if (document.frmLogin.radpass) {
		for (var i = 0; i < document.frmLogin.radpass.length; i++) {
			if (document.frmLogin.radpass[i].checked == true)
				radVal = document.frmLogin.radpass[i].value;
		}
	}
	if(option==2)
		if (radVal == 1) 
			document.frmLogin.action = sSecurePath + "smartlogin.asp?msg=" + sMessage + "&ic=" + ic + "&EqCode=" + EqCode + "&Qty=" + Qty + "&type=" + sDocType + "&order=" + sDocNum + "&url=AL&SSL=1&Tp=" + sTpCatalog;
		else if (radVal == 2)
			document.frmLogin.action = sSecurePath + "account_registration.asp?msg=" + sMessage + "&CookieMsg=100";
	else
		if (radVal == 1) 
			document.frmLogin.action = sNonSecurePath + "smartlogin.asp?msg=" + sMessage + "&ic=" + ic + "&EqCode=" + EqCode + "&Qty=" + Qty + "&type=" + sDocType + "&order=" + sDocNum + "&url=AL&Tp=" + sTpCatalog;
		else if (radVal == 2)
			document.frmLogin.action = sNonSecurePath + "account_registration.asp?msg=" + sMessage + "&CookieMsg=100";
	document.frmLogin.submit();
	return false;
}// End of the function loginme(option)

//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	@		Description			: This function will validate the secret answer(s). It calls  
//  @                     sec_ans_validate() for validating and assigns the acc name, 
//  @                     address code, address type and secret answer to another form and
//  @                     submits the form.
//	@	  Pages Affected  : acclogin.asp
//  @		Function Name		: checkreguser(option)								
//	@		Input Parameters: option=1->NonSecurepath,2->Securepath
//	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function checkreguser(option)
{
	var retVal;
	retVal = sec_ans_validate("frmmaillogin")
	if(retVal==1) return false;
	
	document.frmdummaillogin.accname.value = eval("document.frmmaillogin.accname" + IndexVal + ".value");
	document.frmdummaillogin.addr_code.value = eval("document.frmmaillogin.addr_code" + IndexVal + ".value")
	document.frmdummaillogin.addr_type.value = eval("document.frmmaillogin.addr_type" + IndexVal + ".value")
	document.frmdummaillogin.secret_answer.value = eval("document.frmmaillogin.secret_answer" + IndexVal + ".value")
	if(option==2)
		document.frmdummaillogin.action = sSecurePath + "smartlogin.asp?msg=" + sMessage + "&url=AL&Tp=" + sTpCatalog;
	else
		document.frmdummaillogin.action = sNonSecurePath + "smartlogin.asp?msg=" + sMessage + "&url=AL&Tp=" + sTpCatalog;
	document.frmdummaillogin.submit();
	return false;
}// End of the function checkreguser(option)
