// signIn()
// --------------------------------------------------------------------------------
// Validate username and password and login
//
function signIn(){
	
	// Trim the username and password if required
	document.getElementById("userName").value = trim(document.getElementById("userName").value);
	document.getElementById("password").value = trim(document.getElementById("password").value);
	// Validate
	var validationError = false;
    if (document.getElementById("userName").value.length == 0) {
        ddrivetip(rm.userName, 170, 15, findPosY(document.getElementById("userName")) - 17, findPosX(document.getElementById("userName")), findPosY(document.getElementById("userName")) - 20, findPosX(document.getElementById("userName")) + 20);
        document.getElementById("userName").focus();
        validationError = true;
    }
    if (document.getElementById("userName").value.indexOf(" ") != -1) {
        ddrivetip(rm.userNameBlank, 200, 15, findPosY(document.getElementById("userName")) - 17, findPosX(document.getElementById("userName")), findPosY(document.getElementById("userName")) - 20, findPosX(document.getElementById("userName")) + 20);
        document.getElementById("userName").focus();
        validationError = true;
    }
    if (document.getElementById("userName").value.length < 4) {
        ddrivetip(rm.userNameLess, 200, 30, findPosY(document.getElementById("userName")) - 17, findPosX(document.getElementById("userName")), findPosY(document.getElementById("userName")) - 20, findPosX(document.getElementById("userName")) + 20);
        document.getElementById("userName").focus();
        validationError = true;
    }
    if (document.getElementById("password").value.length == 0) {
        ddrivetip(rm.password, 170, 15, findPosY(document.getElementById("password")) - 17, findPosX(document.getElementById("password")), findPosY(document.getElementById("password")) - 20, findPosX(document.getElementById("password")) + 20);
        document.getElementById("password").focus();
        validationError = true;
    }
    if (document.getElementById("password").value.indexOf(" ") != -1) {
        ddrivetip(rm.passwordBlank, 200, 15, findPosY(document.getElementById("password")) - 17, findPosX(document.getElementById("password")), findPosY(document.getElementById("password")) - 20, findPosX(document.getElementById("password")) + 20);
        document.getElementById("password").focus();
        validationError = true;
    }

	if (validationError == false) {
		var userName = document.getElementById( "userName" ).value;
		var password = document.getElementById( "password" ).value;
		document.getElementById( "displayMessage" ).innerHTML = "Validating your login...";
		LoadAjaxCallFunction('/jsp/checkout/loginSS.jsp', "s=" + sessionId + "&userName=" + userName + "&password=" + password, logInCallback);
	}
}


// logInCallback()
// --------------------------------------------------------------------------------
// Process the response
//
function logInCallback(result) {
	var returnArgs = trim(result).split("|");
	var result = returnArgs[0];
	var newSecurityToken = returnArgs[1];

	if (result != "FAILED") {
		logInSucessSC(document.getElementById("userName").value);
		setCookie('security', newSecurityToken);
		var url2Open = "/jsp/userprofile/myaccounthomeSS.jsp?s=" + sessionId;
		window.open(url2Open, "_self");			
	}
	else {
		document.getElementById( "password" ).value = "";
		document.getElementById( "displayMessage" ).innerHTML = "Invalid username and/or password";
	}
}


// callPasswdResetDis()
// --------------------------------------------------------------------------------
// Password reset request
//
function callPasswdResetDis(username) {
	var arguments = "s=" + sessionId + "&userName=" + userName ;
	LoadAjaxCallFunction('/jsp/userprofile/getAccountStatus.jsp', arguments, getAccountStatusCallback);

	
}


// getAccountStatusCallback()
// --------------------------------------------------------------------------------
// Password reset callback
//
function getAccountStatusCallback(userProfileRes) 
{
	var activeFlag = userProfileRes.accountStatus;
	if (!(userProfileRes.accountStatus == "PASSWORDRESET"))
	{
		document.getElementById("passwdbox").style.display='none';
		document.getElementById("ResetPasswdButton").style.display='none';
		document.getElementById("displayPassStatus").innerHTML = rm.alreadyReset;
		document.getElementById("LoginPageLink").style.display='block';
	}
}


// validateSignUp()
// --------------------------------------------------------------------------------
// Validate the sign up form
//
function validateSignUp() {
    if ((document.getElementById("salutation").value.length == 0) || document.getElementById("salutation").value == "Select") {
        ddrivetip(rm.salutations, 190, 15, findPosY(document.getElementById("salutation")) - 17, findPosX(document.getElementById("salutation")) + 55, findPosY(document.getElementById("salutation")) - 20, findPosX(document.getElementById("salutation")) + 73);
        document.getElementById("salutation").focus();
        return false;
    }
    if (document.getElementById("firstName").value.length == 0) {
        ddrivetip(rm.firstName, 170, 15, findPosY(document.getElementById("firstName")) - 17, findPosX(document.getElementById("firstName")) + 108, findPosY(document.getElementById("firstName")) - 20, findPosX(document.getElementById("firstName")) + 130);
        document.getElementById("firstName").focus();
        return false;
    }
    if (document.getElementById("lastName").value.length == 0) {
        ddrivetip(rm.lastName, 170, 15, findPosY(document.getElementById("lastName")) - 17, findPosX(document.getElementById("lastName")) + 108, findPosY(document.getElementById("lastName")) - 20, findPosX(document.getElementById("lastName")) + 130);
        document.getElementById("lastName").focus();
        return false;
    }
    if (document.getElementById("streetAddress").value.length == 0) {
        ddrivetip(rm.address, 150, 15, findPosY(document.getElementById("streetAddress")) - 17, findPosX(document.getElementById("streetAddress")) + 285, findPosY(document.getElementById("streetAddress")) - 20, findPosX(document.getElementById("streetAddress")) + 305);
        document.getElementById("streetAddress").focus();
        return false;
    }
    if (document.getElementById("city").value.length == 0) {
        ddrivetip(rm.city, 130, 15, findPosY(document.getElementById("city")) - 17, findPosX(document.getElementById("city")) + 130, findPosY(document.getElementById("city")) - 20, findPosX(document.getElementById("city")) + 152);
        document.getElementById("city").focus();
        return false;
    }
    if (document.getElementById("state").value.length == 0 || document.getElementById("state").value == "Select") {
        ddrivetip(rm.state, 130, 15, findPosY(document.getElementById("state")) - 17, findPosX(document.getElementById("state")) + 152, findPosY(document.getElementById("state")) - 27, findPosX(document.getElementById("state")) + 172);
        document.getElementById("state").focus();
        return false;
    }
    if ((document.getElementById("country").value.length == 0) || document.getElementById("country").value == "Select") {
        ddrivetip(rm.country, 130, 15, findPosY(document.getElementById("country")) - 17, findPosX(document.getElementById("country")) + 152, findPosY(document.getElementById("country")) - 20, findPosX(document.getElementById("country")) + 172);
        document.getElementById("country").focus();
        return false;
    }
    if (document.getElementById("postalCode").value.length == 0) {
        ddrivetip(rm.postalCode, 270, 15, findPosY(document.getElementById("postalCode")) - 17, findPosX(document.getElementById("postalCode")) + 132, findPosY(document.getElementById("postalCode")) - 20, findPosX(document.getElementById("postalCode")) + 152);
        document.getElementById("postalCode").focus();
        return false;
    }
    if (document.getElementById("country").value=='US') {
    	if (!validateZIP(document.getElementById("postalCode").value)) {
        	ddrivetip(rm.postalCodeProper, 250, 15, findPosY(document.getElementById("postalCode")) - 17, findPosX(document.getElementById("postalCode")) + 132, findPosY(document.getElementById("postalCode")) - 20, findPosX(document.getElementById("postalCode")) + 152);
        	document.getElementById("postalCode").focus();
       		return false;
    	}
    }
    else if(document.getElementById("country").value=='CA'){
    	if (!validateZipCodeCanada(document.getElementById("postalCode").value)) {
        	ddrivetip(rm.postalCodeProper, 250, 15, findPosY(document.getElementById("postalCode")) - 17, findPosX(document.getElementById("postalCode")) + 132, findPosY(document.getElementById("postalCode")) - 20, findPosX(document.getElementById("postalCode")) + 152);
        	document.getElementById("postalCode").focus();
       		return false;
    	}   	
    }
    if ((document.getElementById("email").value.length == 0) || (document.getElementById("email").value.indexOf(" ") != -1)) {
        ddrivetip(rm.email, 130, 15, findPosY(document.getElementById("email")) - 17, findPosX(document.getElementById("email")) + 285, findPosY(document.getElementById("email")) - 20, findPosX(document.getElementById("email")) + 305);
        document.getElementById("email").focus();
        return false;
    }
    if (!isEmailAddr(document.getElementById("email").value)) {
        ddrivetip(rm.invalidEmail, 190, 15, findPosY(document.getElementById("email")) - 17, findPosX(document.getElementById("email")) + 285, findPosY(document.getElementById("email")) - 20, findPosX(document.getElementById("email")) + 305);
        document.getElementById("email").focus();
        return false;
    }
    if (document.getElementById("userName").value.length == 0) {
        ddrivetip(rm.userName, 150, 15, findPosY(document.getElementById("userName")) - 17, findPosX(document.getElementById("userName")) + 285, findPosY(document.getElementById("userName")) - 20, findPosX(document.getElementById("userName")) + 305);
        document.getElementById("userName").focus();
        return false;
    }
    if ((document.getElementById("userName").value.substring(0, 1) < "a" || document.getElementById("userName").value.substring(0, 1) > "z") && (document.getElementById("userName").value.substring(0, 1) < "A" || document.getElementById("userName").value.substring(0, 1) > "Z")) {
        ddrivetip(rm.userNameAlpha, 220, 30, findPosY(document.getElementById("userName")) - 17, findPosX(document.getElementById("userName")) + 285, findPosY(document.getElementById("userName")) - 20, findPosX(document.getElementById("userName")) + 305);
        document.getElementById("userName").focus();
        return false;
    }
    if (document.getElementById("userName").value.indexOf(" ") != -1) {
        ddrivetip(rm.userNameBlank, 200, 15, findPosY(document.getElementById("userName")) - 17, findPosX(document.getElementById("userName")) + 285, findPosY(document.getElementById("userName")) - 20, findPosX(document.getElementById("userName")) + 305);
        document.getElementById("userName").focus();
        return false;
    }
    if (document.getElementById("userName").value.length < 4) {
        ddrivetip(rm.userNameLess, 200, 30, findPosY(document.getElementById("userName")) - 17, findPosX(document.getElementById("userName")) + 285, findPosY(document.getElementById("userName")) - 20, findPosX(document.getElementById("userName")) + 305);
        document.getElementById("userName").focus();
        return false;
    }

	//checkUserName()This function validates the userName given by the user at the time of account creation.
    if (!checkUserName(document.getElementById("userName").value)) {
        ddrivetip(rm.userNameCheck, 330, 30, findPosY(document.getElementById("userName")) - 17, findPosX(document.getElementById("userName")) + 285, findPosY(document.getElementById("userName")) - 20, findPosX(document.getElementById("userName")) + 305);
        document.getElementById("userName").focus();
        return false;
    }

    function checkUserName(userName) {
        for (var i = 1; i < userName.length; i++) {
            var ch = userName.substring(i, i + 1);
            if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != "_") && (ch != ".")) {
                return false;
            }
        }
        return true;
    }

    if (document.getElementById("password").value.length == 0) {
        ddrivetip(rm.password, 150, 15, findPosY(document.getElementById("password")) - 17, findPosX(document.getElementById("password")) + 128, findPosY(document.getElementById("password")) - 20, findPosX(document.getElementById("password")) + 150);
        document.getElementById("password").focus();
        return false;
    }
	
    if (document.getElementById("password").value.indexOf(" ") != -1) {
        ddrivetip(rm.passwordBlank, 220, 15, findPosY(document.getElementById("password")) - 17, findPosX(document.getElementById("password")) + 128, findPosY(document.getElementById("password")) - 20, findPosX(document.getElementById("password")) + 150);
        document.getElementById("password").focus();
        return false;
    }

    if (document.getElementById("password").value.length < 6) {
        ddrivetip(rm.passwordLess, 220, 30, findPosY(document.getElementById("password")) - 17, findPosX(document.getElementById("password")) + 128, findPosY(document.getElementById("password")) - 20, findPosX(document.getElementById("password")) + 150);
        document.getElementById("password").focus();
        return false;
    }

    if (document.getElementById("passwordRetype").value.length < 6) {
        ddrivetip(rm.passwordRetype, 230, 15, findPosY(document.getElementById("passwordRetype")) - 17, findPosX(document.getElementById("passwordRetype")) + 128, findPosY(document.getElementById("passwordRetype")) - 20, findPosX(document.getElementById("passwordRetype")) + 150);
        document.getElementById("passwordRetype").focus();
        return false;
    }

    if (document.getElementById("password").value != document.getElementById("passwordRetype").value) {
        ddrivetip(rm.passwordIncorrect, 220, 30, findPosY(document.getElementById("passwordRetype")) - 17, findPosX(document.getElementById("passwordRetype")) + 128, findPosY(document.getElementById("passwordRetype")) - 20, findPosX(document.getElementById("passwordRetype")) + 150);
        document.getElementById("passwordRetype").focus();
        return false;
    }

    if (document.getElementById("password").value == document.getElementById("userName").value) {
        ddrivetip("Your password should not be your username", 220, 30, findPosY(document.getElementById("passwordRetype")) - 17, findPosX(document.getElementById("passwordRetype")) + 128, findPosY(document.getElementById("passwordRetype")) - 20, findPosX(document.getElementById("passwordRetype")) + 150);
        document.getElementById("passwordRetype").focus();
        return false;
    }	

    if (!document.getElementById("acceptTerms").checked) {
        ddrivetip(rm.acceptTerms, 280, 15, findPosY(document.getElementById("acceptTerms")) - 17, findPosX(document.getElementById("acceptTerms")) + 10, findPosY(document.getElementById("acceptTerms")) - 20, findPosX(document.getElementById("acceptTerms")) + 30);
        document.getElementById("acceptTerms").focus();
        return false;
    }
    return true;
}


// validateZIP()
// --------------------------------------------------------------------------------
// Validate a US postal code
//
function validateZIP(field) {
    var valid = "0123456789-";
    var hyphencount = 0;
    if (field.length != 5 && field.length != 10) {
        return false;
    }
    for (var i = 0; i < field.length; i++) {
        temp = "" + field.substring(i, i + 1);
        if (temp == "-") {
            hyphencount++;
        }
        if (valid.indexOf(temp) == "-1") {
            return false;
        }
        if ((hyphencount > 1) || ((field.length == 10) && "" + field.charAt(5) != "-")) {
            return false;
        }
    }
    return true;
}


// validateZipCodeCanada()
// --------------------------------------------------------------------------------
// Validate a CA postal code
//
function validateZipCodeCanada(entry){ // CANADIAN CODES ONLY
	var strlen=entry.length; if(strlen!=6) {return false;}
	entry=entry.toUpperCase();    // in case of lowercase characters
	// Check for legal characters in string - note index starts at zero
	if('ABCEGHJKLMNPRSTVXY'.indexOf(entry.charAt(0))<0) {return false;}
	if('0123456789'.indexOf(entry.charAt(1))<0) {return false;}
	if('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2))<0) {return false;}
	if('0123456789'.indexOf(entry.charAt(3))<0) {return false;}
	if('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(4))<0) {return false;}
	if('0123456789'.indexOf(entry.charAt(5))<0) {return false;}
	return true;	
}


// callcreateUserAccount()
// --------------------------------------------------------------------------------
// Create a new user account
//
function callcreateUserAccount() {
    if (validateSignUp()) {
    	
        document.getElementById("stautsofCreataion").innerHTML = "";
        document.getElementById("SearchingWait").style.display = "block";
		document.getElementById("userName").value = trim(document.getElementById("userName").value);
		document.getElementById("password").value = trim(document.getElementById("password").value);
		var accountStatus = document.getElementById("hidAccountStatus").value;
	    var accountType = document.getElementById("hidAccountType").value;
	    var userName = document.getElementById("userName").value;
	    var password = document.getElementById("password").value;
	    var firstname = document.getElementById("firstName").value;
	    var middlename = document.getElementById("middleName").value;
	    var lastname = document.getElementById("lastName").value;
	    var salutation = document.getElementById("salutation").value;
	    var companyname = document.getElementById("companyName").value;
	    var streetaddress = document.getElementById("streetAddress").value;
	    var streetAddressTwo = document.getElementById("streetAddressTwo").value;
	    var country = document.getElementById("country").value;
	    var state = document.getElementById("state").value;
	    var city = document.getElementById("city").value;
	    var postalcode = document.getElementById("postalCode").value;
	    var email = document.getElementById("email").value;
            var arguments = "s=" + sessionId + "&userName=" + userName + "&password=" + password + "&firstName=" + firstname + "&middleName=" + middlename + "&lastName=" + lastname + "&salutation=" + salutation + "&companyName=" + companyname;
            arguments = arguments + "&streetAddress=" + streetaddress + "&streetAddressTwo=" + streetAddressTwo + "&country=" + country + "&state=" + state  +"&city=" + city + "&postalCode=" + postalcode + "&emailAddress=" + email;
	    LoadAjaxCallFunction('/jsp/userprofile/createAccountSS.jsp', arguments, creatAccountCallback);
	}
}

// creatAccountCallback()
// --------------------------------------------------------------------------------
// Callback for creation of a new user account
//
function creatAccountCallback(result) {
	var returnArgs = trim(result).split("|");
	// success then goto profile home
	
	if (returnArgs[0] == "success") {
		setCookie('security', returnArgs[1]);
		var queryString = contextPath + "/jsp/userprofile/myaccounthomeSS.jsp?s=" + sessionId;
		window.open(queryString, "_self");
    	} 
	else {
		document.getElementById("SearchingWait").style.display = "none";
		document.getElementById("stautsofCreataion").innerHTML = document.getElementById("userName").value + rm.userAlreadyExists;
		return false;
     } 
     document.getElementById("SearchingWait").style.display = "none";
}

// callNewPasswordCheck()
// --------------------------------------------------------------------------------
// Check the password
//
function callNewPasswordCheck() {
    var newPassword = document.getElementById("newPassword").value;
    var conformPassword = document.getElementById("confirmPassword").value;
    if ((document.getElementById("oldPassword").value.length == 0)) {
        ddrivetip(rm.password, 180, 15, findPosY(document.getElementById("oldPassword")) - 17, findPosX(document.getElementById("oldPassword")) + 140, findPosY(document.getElementById("oldPassword")) - 20, findPosX(document.getElementById("oldPassword")) + 160);
        document.getElementById("oldPassword").focus();
        return;
    }
    if (document.getElementById("newPassword").value.length == 0) {
        ddrivetip(rm.newPassword, 200, 15, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 140, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 160);
        document.getElementById("newPassword").focus();
        return;
    }
    if (document.getElementById("newPassword").value.indexOf(" ") != -1) {
        ddrivetip(rm.newPasswordBlank, 200, 15, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 140, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 160);
        document.getElementById("newPassword").focus();
        return;
    }
    if (document.getElementById("newPassword").value.length < 6) {
        ddrivetip(rm.newPasswordLen, 200, 30, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 140, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 160);
        document.getElementById("newPassword").focus();
        return;
    }
    if (document.getElementById("oldPassword").value == (document.getElementById("newPassword").value)) {
        ddrivetip(rm.oldPasswordaresame, 250, 30, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 140, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 160);
        document.getElementById("newPassword").focus();
        return;
    }
    if (document.getElementById("confirmPassword").value.length == 0) {
        ddrivetip(rm.confirmPassword, 200, 15, findPosY(document.getElementById("confirmPassword")) - 17, findPosX(document.getElementById("confirmPassword")) + 140, findPosY(document.getElementById("confirmPassword")) - 17, findPosX(document.getElementById("confirmPassword")) + 160);
        document.getElementById("confirmPassword").focus();
        return;
    }
    if (document.getElementById("newPassword").value != document.getElementById("confirmPassword").value) {
        ddrivetip(rm.newAndConformareDeffrent, 250, 30, findPosY(document.getElementById("confirmPassword")) - 17, findPosX(document.getElementById("confirmPassword")) + 140, findPosY(document.getElementById("confirmPassword")) - 20, findPosX(document.getElementById("confirmPassword")) + 160);
        document.getElementById("confirmPassword").focus();
        return;
    }
    if (document.getElementById("newPassword").value == userID) {
        ddrivetip("Your password should not be your username", 220, 30, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 128, findPosY(document.getElementById("newPassword")) - 20, findPosX(document.getElementById("newPassword")) + 150);
        document.getElementById("newPassword").focus();
        return;
    }
	
    if (newPassword == conformPassword) {
        document.getElementById("displayPassStatus").innerHTML = "";
        document.getElementById("SearchingWait").style.display = "block";
        var oldPassword = document.getElementById("oldPassword").value;
        var conformPassword = document.getElementById("confirmPassword").value;
        var userName = userID;
        var arguments = "s=" + sessionId + "&userName=" + userName + "&newPassword=" + conformPassword + "&oldPassword=" + oldPassword ;
		LoadAjaxCallFunction('/jsp/userprofile/updatePassword.jsp', arguments, ChangePasswordCallback);

	}
}

// ChangePasswordReq()
// --------------------------------------------------------------------------------
// Check the password callback
//
function ChangePasswordCallback(ChangePasswordRes) {
	ChangePasswordRes = trim(ChangePasswordRes);
    if (ChangePasswordRes=='SUCCESS') {
        document.getElementById("displayPassStatus").innerHTML = rm.confirmUpdate;
    } else if (ChangePasswordRes=='NOPROFILE'){
        document.getElementById("displayPassStatus").innerHTML = rm.confirmUpdateFail;
    } else if (ChangePasswordRes=='NOPASSWORDMATCH'){
        document.getElementById("displayPassStatus").innerHTML = "Your old password did not match the one we have on record.  We cannot update your password.";
    }	
    else{
    	document.getElementById("displayPassStatus").innerHTML = "<p style='color:#ff682d;font-weight:bold;'>Sorry, we are facing some technical problems. Please try again later.</p>";
    }
    document.getElementById("SearchingWait").style.display = "none";
} 

function callPasswordResetCheck() {	
    var newPassword = document.getElementById("newPassword").value;
    var conformPassword = document.getElementById("confirmPassword").value;
    	
	if (document.getElementById("newPassword").value.length == 0) {
	    ddrivetip(rm.newPassword, 200,15, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 140, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 160);
	    document.getElementById("newPassword").focus();
	    return;
	} 
	if (document.getElementById("newPassword").value.indexOf(" ") != -1) {
	    ddrivetip(rm.newPasswordBlank, 200,15, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 140, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 160);
	    document.getElementById("newPassword").focus();
	    return;
	}  
	if (document.getElementById("newPassword").value.length < 6) {
		ddrivetip(rm.newPasswordLen,200,30, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 140, findPosY(document.getElementById("newPassword")) - 17, findPosX(document.getElementById("newPassword")) + 160);
		document.getElementById("newPassword").focus();
		return;            
	}
		
	if (document.getElementById("confirmPassword").value.length == 0 ) {
	    ddrivetip(rm.confirmPassword, 200,15, findPosY(document.getElementById("confirmPassword")) - 17, findPosX(document.getElementById("confirmPassword")) + 140, findPosY(document.getElementById("confirmPassword")) - 17, findPosX(document.getElementById("confirmPassword")) + 160);
	    document.getElementById("confirmPassword").focus();
	    return;
	}
	if (document.getElementById("newPassword").value != document.getElementById("confirmPassword").value) {
	    ddrivetip(rm.newAndConformareDeffrent, 250,30, findPosY(document.getElementById("confirmPassword")) - 17, findPosX(document.getElementById("confirmPassword")) + 140, findPosY(document.getElementById("confirmPassword")) -20, findPosX(document.getElementById("confirmPassword")) + 160);
	    document.getElementById("confirmPassword").focus();
	    return;
	 } 
	 
	if (newPassword == conformPassword) {
		document.getElementById("displayPassStatus").innerHTML="";
		document.getElementById("SearchingWait").style.display='block';
	    var oldPassword = "";
	    var conformPassword = document.getElementById("confirmPassword").value;
	    var hintQuestion = "";
	    var hintAns = "";  
	    var userName = document.getElementById("userName").value;
	    
        var arguments = "s=" + sessionId + "&userName=" + userName + "&newPassword=" + conformPassword 
		LoadAjaxCallFunction('/jsp/userprofile/updatePasswordByEmail.jsp', arguments, ChangePasswordCallback);
	}
}

function ResetPasswordReq(ChangePasswordRes) {
	ChangePasswordRes = trim(ChangePasswordRes);
	if (ChangePasswordRes=='SUCCESS') {
		document.getElementById("displayPassStatus").innerHTML = rm.confirmAfterReset;		
		document.getElementById("passwdbox").style.display='none';		
		document.getElementById("LoginPageLink").style.display='block';
    } else if (ChangePasswordRes=='NOPROFILE'){
        ddrivetip("Please enter a valid user name", 200,15, findPosY(document.getElementById("userName")) - 17, findPosX(document.getElementById("userName")) + 140, findPosY(document.getElementById("userName")) - 17, findPosX(document.getElementById("userName")) + 160);
	    document.getElementById("userName").focus();
    }
    else{
    	document.getElementById("displayPassStatus").innerHTML = "<p style='color:#ff682d;font-weight:bold;'>Sorry, we are facing some technical problems. Please try again later.</p>";
    }	
    document.getElementById("SearchingWait").style.display='none';
} 


function callUserInfoDwr() {
    var firstName = document.getElementById("firstName").value;
    var middleName = document.getElementById("middleName").value;
    var lastName = document.getElementById("lastName").value;
    var companyName = document.getElementById("companyName").value;
    var streetaddress = document.getElementById("streetAddress").value;
    var streetAddressTwo = document.getElementById("streetAddressTwo").value;
    var country = document.getElementById("country").value;
    var state = trim(document.getElementById("state").value);
    var city = document.getElementById("city").value;
    var postalcode = document.getElementById("postalCode").value;
    var email = document.getElementById("email").value;
    
	//validateMyProfile()This function is used to validate the information in My Profile
    if (validateMyProfile()) {
        document.getElementById("displayProfileStatus").innerHTML = "";
        document.getElementById("SearchingWait").style.display = "block";

        var arguments = "s=" + sessionId + "&userName=" + userID +  "&firstName=" + firstName + "&middleName=" + middleName + "&lastName=" + lastName + "&companyName=" + companyName;
        arguments = arguments + "&streetAddress=" + streetaddress + "&streetAddressTwo=" + streetAddressTwo + "&country=" + country + "&state=" + state  +"&city=" + city + "&postalCode=" + postalcode + "&emailAddress=" + email;
	LoadAjaxCallFunction('/jsp/userprofile/updateAccountSS.jsp', arguments, updateAccountCallback);

    }
}

function updateAccountCallback(updateProfileRes) {
	updateProfileRes = trim(updateProfileRes);
    if (trim(updateProfileRes) == "SUCCESS") {
        document.getElementById("displayProfileStatus").innerHTML = rm.confirmUpdateProfile;
    } else {
        document.getElementById("displayProfileStatus").innerHTML = rm.confirmUpdateProfileFail;
    }
    document.getElementById("SearchingWait").style.display = "none";
}


// getStateCallback()
// --------------------------------------------------------------------------------
// callback function for getState ajax call. 
//
function getStateCallback(response) {
	
	response = trim(response);
	
	var stateSelectObject = document.getElementById("state");
	if(stateSelectObject != null) {
		stateSelectObject.options.length = 0;
		var stateList=response.split("!");
		for(var i=0;i <stateList.length;i++) {
			var stateObj = stateList[i].split("~");
			if(stateCode == stateObj[0]){
				stateSelectObject.options[i] = new Option(stateObj[1], stateObj[0], true, true);
			}else{
				stateSelectObject.options[i] = new Option(stateObj[1], stateObj[0], false, false);
			}	
		}
	}
}

// getState(countryCode)
// --------------------------------------------------------------------------------
// get the states based on country code
//
function getState(countryCode) {
    LoadAjaxCallFunction('/jsp/checkout/retrieveState.jsp', 'countryCode=' + countryCode, getStateCallback);
}


function validateMyProfile() {
    if (document.getElementById("firstName").value.length == 0) {
        ddrivetip(rm.firstName, 170, 15, findPosY(document.getElementById("firstName")) - 17, findPosX(document.getElementById("firstName")) + 110, findPosY(document.getElementById("firstName")) - 20, findPosX(document.getElementById("firstName")) + 132);
        document.getElementById("firstName").focus();
        return false;
    }
    if (document.getElementById("lastName").value.length == 0) {
        ddrivetip(rm.lastName, 170, 15, findPosY(document.getElementById("lastName")) - 17, findPosX(document.getElementById("lastName")) + 110, findPosY(document.getElementById("lastName")) - 20, findPosX(document.getElementById("lastName")) + 132);
        document.getElementById("lastName").focus();
        return false;
    }
    if (document.getElementById("streetAddress").value.length == 0) {
        ddrivetip(rm.address, 150, 15, findPosY(document.getElementById("streetAddress")) - 17, findPosX(document.getElementById("streetAddress")) + 285, findPosY(document.getElementById("streetAddress")) - 20, findPosX(document.getElementById("streetAddress")) + 305);
        document.getElementById("streetAddress").focus();
        return false;
    }
    if (document.getElementById("city").value.length == 0) {
        ddrivetip(rm.city, 130, 15, findPosY(document.getElementById("city")) - 17, findPosX(document.getElementById("city")) + 130, findPosY(document.getElementById("city")) - 20, findPosX(document.getElementById("city")) + 152);
        document.getElementById("city").focus();
        return false;
    }
    if (document.getElementById("state").value.length == 0 || document.getElementById("state").value == "Select") {
        ddrivetip(rm.state, 130, 15, findPosY(document.getElementById("state")) - 17, findPosX(document.getElementById("state")) + 285, findPosY(document.getElementById("state")) - 27, findPosX(document.getElementById("state")) + 305);
        document.getElementById("state").focus();
        return false;
    }
    if ((document.getElementById("country").value.length == 0) || document.getElementById("country").value == "Select") {
        ddrivetip(rm.country, 130, 15, findPosY(document.getElementById("country")) - 17, findPosX(document.getElementById("country")) + 285, findPosY(document.getElementById("country")) - 20, findPosX(document.getElementById("country")) + 305);
        document.getElementById("country").focus();
        return false;
    }
    if (document.getElementById("postalCode").value.length == 0) {
        ddrivetip(rm.postalCode, 270, 15, findPosY(document.getElementById("postalCode")) - 17, findPosX(document.getElementById("postalCode")) + 130, findPosY(document.getElementById("postalCode")) - 20, findPosX(document.getElementById("postalCode")) + 152);
        document.getElementById("postalCode").focus();
        return false;
    } 
			//validateZIP()This function validates the zip code entered by the user at the time of account creation.
   if(document.getElementById("country").value=='US'){
    	if (!validateZIP(document.getElementById("postalCode").value)) {
        	ddrivetip(rm.postalCodeProper, 220, 15, findPosY(document.getElementById("postalCode")) - 17, findPosX(document.getElementById("postalCode")) + 130, findPosY(document.getElementById("postalCode")) - 20, findPosX(document.getElementById("postalCode")) + 152);
       		document.getElementById("postalCode").focus();
        	return false;
    	}
    }
    else if (document.getElementById("country").value=='CA'){
    	if (!validateZipCodeCanada(document.getElementById("postalCode").value)) {
        	ddrivetip(rm.postalCodeProper, 220, 15, findPosY(document.getElementById("postalCode")) - 17, findPosX(document.getElementById("postalCode")) + 130, findPosY(document.getElementById("postalCode")) - 20, findPosX(document.getElementById("postalCode")) + 152);
       		document.getElementById("postalCode").focus();
        	return false;
    	}
    }
    if ((document.getElementById("email").value.length == 0) || (document.getElementById("email").value.indexOf(" ") != -1)) {
        ddrivetip(rm.email, 130, 15, findPosY(document.getElementById("email")) - 17, findPosX(document.getElementById("email")) + 285, findPosY(document.getElementById("email")) - 20, findPosX(document.getElementById("email")) + 305);
        document.getElementById("email").focus();
        return false;
    }
    if (!isEmailAddr(document.getElementById("email").value)) {
        ddrivetip(rm.invalidEmail, 200, 15, findPosY(document.getElementById("email")) - 17, findPosX(document.getElementById("email")) + 285, findPosY(document.getElementById("email")) - 20, findPosX(document.getElementById("email")) + 305);
        document.getElementById("email").focus();
        return false;
    }
    return true;
}


function userCommunicationsReq(user) {
    document.getElementById("defaultEmail").value = ((new String(user[0]) == "undefined") ? "" : user[0]);
    var selobj = document.getElementById("emailformat");
    if (user[1]==""||user[1]==null||user[1]=="null"||user[1]=="Html"||user[1]=="HTML"||user[1]=="E2"){
     for (var jj = 0; jj < selobj.length; jj++) {     
    	if (selobj[jj].value == "E2"||selobj[jj].value == "Html"||selobj[jj].value == "HTML"){
    	  selobj[jj].selected = true;
    	 }
    	}
    }   
    else  if (user[1]=="Text"||user[1]=="TEXT"||user[1]=="E1"){
    	for (var i = 0; i < selobj.length; i++) {   	 
        	if (selobj[i].value == "E1"||selobj[i].value == "Text"||selobj[i].value == "TEXT"){
            	selobj[i].selected = true;
        	}       	
    	}
    }
    document.getElementById("CCEmail").checked = ((user[2] == "Y") ? true : false);
    document.getElementById("remainder").checked = ((user[3] == "Y") ? true : false);
    document.getElementById("specialsDeals").checked = ((user[4] == "Y") ? true : false);
}

function userCommunicationsPhoneReq(userProfileRes) {
	if(userProfileRes != null && userProfileRes.mainPhone != null) { 
	    document.getElementById("countryCode").value = userProfileRes.mainPhone.country; 
	    document.getElementById("stateCode").value = userProfileRes.mainPhone.state; 
	    document.getElementById("phoneNo").value = userProfileRes.mainPhone.phoneNumber; 	
    }
    if(userProfileRes != null && userProfileRes.alternatePhone != null) {
    	document.getElementById("countryCodeSecond").value = userProfileRes.alternatePhone.country;
	    document.getElementById("stateCodeSecond").value = userProfileRes.alternatePhone.state;	
	    document.getElementById("phoneNoSecond").value = userProfileRes.alternatePhone.phoneNumber;
    }
}

var transUserName;

function saveUserComunicationsDwr(userName) {
    var defaultEmail = document.getElementById("defaultEmail").value;
    var emailformat = document.getElementById("emailformat").value;
    var CCEmail = document.getElementById("CCEmail").checked;
    var remainder = document.getElementById("remainder").checked;
    var specialsDeals = document.getElementById("specialsDeals").checked;
    //validateCommunication()validates the communication information given by the user
    if (validateCommunication()) {
        document.getElementById("displayComunicationStatus").innerHTML = "";
        document.getElementById("SearchingWait").style.display = "block";
		transUserName = userName;
		

		var countryCode = document.getElementById("countryCode").value; 
    		var stateCode = document.getElementById("stateCode").value; 
    		var phoneNo = document.getElementById("phoneNo").value; 	
    		var countryCodeSecond = document.getElementById("countryCodeSecond").value;
    		var stateCodeSecond = document.getElementById("stateCodeSecond").value;	
    		var phoneNoSecond = document.getElementById("phoneNoSecond").value;
        	var arguments = "s=" + sessionId + "&userName=" + userName + "&defaultEmail=" + defaultEmail + "&emailformat=" + emailformat + "&ccEmail=N&reminder=N&specialsDeals=" + specialsDeals ;
        	arguments = arguments + "&homeCountryCode=" + countryCode +  "&homeStateCode=" + stateCode +  "&homePhoneNo=" + phoneNo +  "&workCountryCode=" + countryCodeSecond +  "&workStateCode=" + stateCodeSecond + "&workPhoneNo=" + phoneNoSecond;
		LoadAjaxCallFunction('/jsp/userprofile/saveCommunications.jsp', arguments, saveComunicationsCallback);
	}
}

function saveComunicationsCallback(saveComunicationsRes) {
	saveComunicationsRes = trim(saveComunicationsRes);
    if (saveComunicationsRes != "SUCCESS") {
        document.getElementById("displayComunicationStatus").innerHTML = rm.confirmUpdateProfileFail;
    }else {
    	document.getElementById("displayComunicationStatus").innerHTML = rm.confirmUpdateProfile;
    }
    
    document.getElementById("SearchingWait").style.display = "none";
}

function validateCommunication() {
    if (document.getElementById("defaultEmail").value.length == 0) {
        ddrivetip(rm.emailAdd, 250, 15, findPosY(document.getElementById("defaultEmail")) - 17, findPosX(document.getElementById("defaultEmail")) + 285, findPosY(document.getElementById("defaultEmail")) - 20, findPosX(document.getElementById("defaultEmail")) + 305);
        document.getElementById("defaultEmail").focus();
        return false;
    }
    if (document.getElementById("defaultEmail").value.indexOf(" ") != -1) {
        ddrivetip(rm.passwordBlank, 190, 15, findPosY(document.getElementById("defaultEmail")) - 17, findPosX(document.getElementById("defaultEmail")) + 285, findPosY(document.getElementById("defaultEmail")) - 22, findPosX(document.getElementById("defaultEmail")) + 305);
        document.getElementById("defaultEmail").focus();
        return false;
    }
    if (!isEmailAddr(document.getElementById("defaultEmail").value)) {
        ddrivetip(rm.emailAdd, 250, 15, findPosY(document.getElementById("defaultEmail")) - 17, findPosX(document.getElementById("defaultEmail")) + 285, findPosY(document.getElementById("defaultEmail")) - 22, findPosX(document.getElementById("defaultEmail")) + 305);
        document.getElementById("defaultEmail").focus();
        return false;
    }
    if ((document.getElementById("emailformat").value.length == 0) || document.getElementById("emailformat").value == "Select") {
        ddrivetip(rm.emailForamt, 220, 30, findPosY(document.getElementById("emailformat")) - 17, findPosX(document.getElementById("emailformat")) + 42, findPosY(document.getElementById("emailformat")) - 20, findPosX(document.getElementById("emailformat")) + 60);
        document.getElementById("emailformat").focus();
        return false;
    }
    return true;
}

function forgotPassword() {
    if (document.getElementById("fuserName").value.length == 0) {
        ddrivetip(rm.forgotPass, 170, 15, findPosY(document.getElementById("fuserName")) - 15, findPosX(document.getElementById("fuserName")) + 30, findPosY(document.getElementById("fuserName")) - 25, findPosX(document.getElementById("fuserName")) + 50);
        document.getElementById("fuserName").focus();
		return;
    }
    document.getElementById("displayForgotPassStatus").innerHTML = "";
    document.getElementById("SearchingWait").style.display = "block";
    var arguments = "s=" + sessionId + "&userName=" + document.getElementById("fuserName").value ;
	LoadAjaxCallFunction('/jsp/userprofile/retrievePassword.jsp', arguments, forgotPasswordCallback);


}

function sendPasswordResetEmail() {
	if (document.getElementById("fuserName").value.length == 0) 
   	{
   		ddrivetip(rm.forgotPass ,170,15,findPosY(document.getElementById("fuserName"))-15,findPosX(document.getElementById("fuserName"))+30,findPosY(document.getElementById("fuserName"))-25,findPosX(document.getElementById("fuserName"))+50);       		
   	}
	else {
		hideddrivetip();
		document.getElementById("displayForgotPassStatus").innerHTML="";
		document.getElementById("SearchingWait").style.display='block'
		var arguments = "s=" + sessionId + "&userName=" + document.getElementById("fuserName").value ;
		LoadAjaxCallFunction('/jsp/userprofile/retrievePassword.jsp', arguments, forgotPasswordCallback);
  
	}
}

function forgotPasswordCallback(forgotPasswordRes) {
    forgotPasswordRes = trim(forgotPasswordRes);
    if (forgotPasswordRes == "Success") {
        document.getElementById("displayForgotPassStatus").innerHTML = rm.displayForgotPassStatus;
    } else {
        if (forgotPasswordRes == "Invalid"||forgotPasswordRes == "Failure") {
            document.getElementById("displayForgotPassStatus").innerHTML = rm.displayForgotPassStatusInvalid;
			document.getElementById("fuserName").focus();
        } else {
            document.getElementById("displayForgotPassStatus").innerHTML = rm.displayForgotPassStatusFail;
			document.getElementById("fuserName").focus();
        }
    }
    document.getElementById("SearchingWait").style.display = "none";
}

function refresh() {
    window.open(window.location, "_self");
}

function redirecttologin(){	
	var kk = getCookie("VayamaToken");
	tokenValueJs = null;
	tokenValue = null;
	window.location.href = "/jsp/userprofile/login.jsp?s=" + sessionId;
}

function createAnAccount(){
	
	var url2Open = "";
 	url2Open =  "/jsp/userprofile/signup.jsp?s=" + sessionId;
	window.open(url2Open, "_self");
}

function forgotUserName() 
{
	document.getElementById("displayMessage").innerHTML = "";
	document.getElementById("login").style.display = "none";
	document.getElementById("forgotusername").style.display = "block";
}

// Get username by email.
function getUserNameByEmail() {
    if (document.getElementById("useremail").value.length == 0) {
        document.getElementById("displayForgotPassStatus").innerHTML = "please enter a valid email address";
        document.getElementById("useremail").focus();
        return ;
    }
    if (!isEmailAddr(document.getElementById("useremail").value)) {
        document.getElementById("displayForgotPassStatus").innerHTML = "please enter a valid email address";
        document.getElementById("useremail").focus();
        return ;
    }
	
	document.getElementById("SearchingWait").style.display = "";
    LoadAjaxCallFunction('/jsp/userprofile/retrieveUsername.jsp', "email=" + document.getElementById("useremail").value,  forgotUserNameCallback);
}


//Forgot username callback function
function forgotUserNameCallback(result)
{
	var returnArgs = trim(result).split("|");
	var result = returnArgs[0];

	if (result != "SUCCESS") {
		document.getElementById("displayForgotPassStatus").innerHTML = "vayama account with this email does not exist.";
	} else {
		window.location.href = "/jsp/userprofile/login.jsp?s=" + sessionId + "&userName=" + returnArgs[1];
	}

	document.getElementById("SearchingWait").style.display = "none";
}

// getCookie()
// --------------------------------------------------------------------------------
// Read a cookie
//
function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) {
        return null;
    }
    var end = document.cookie.indexOf(";", len);
    if (end == -1) {
        end = document.cookie.length;
    }
    return unescape(document.cookie.substring(len, end));
}

// setCookie()
// --------------------------------------------------------------------------------
// set a cookie
//
function setCookie(name, value) {
    document.cookie = name + "=" + value + ";path=/";
}


