//********************************************************************
//*-------------------------------------------------------------------
//* Licensed Materials - Property of IBM
//*
//* WebSphere Commerce
//*
//* (c) Copyright International Business Machines Corporation. 2003
//*     All rights reserved.
//*
//* US Government Users Restricted Rights - Use, duplication or
//* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//*
//*-------------------------------------------------------------------
//*

//////////////////////////////////////////////////////////
// Checks whether a string contains a double byte character
// target = the string to be checked
//
// Return true if target contains a double byte char; false otherwise
//////////////////////////////////////////////////////////
function containsDoubleByte (target) {
     var str = new String(target);
     var oneByteMax = 0x007F;

     for (var i=0; i < str.length; i++){
        chr = str.charCodeAt(i);
        if (chr > oneByteMax) {return true;}
     }
     return false;
}

//////////////////////////////////////////////////////////
// A simple function to validate an email address
// It does not allow double byte characters
// strEmail = the email address string to be validated
//
// Return true if the email address is valid; false otherwise
//////////////////////////////////////////////////////////
function isValidEmail(strEmail){
	// check if email contains dbcs chars
	if (containsDoubleByte(strEmail)){
		return false;
	}
	
	if(strEmail.length == 0) {
		return true;
	} else if (strEmail.length < 5) {
             return false;
       	}else{
           	if (strEmail.indexOf(" ") > 0){
                      	return false;
               	}else{
                  	if (strEmail.indexOf("@") < 1) {
                            	return false;
                     	}else{
                           	if (strEmail.lastIndexOf(".") < (strEmail.indexOf("@") + 2)){
                                     	return false;
                                }else{
                                        if (strEmail.lastIndexOf(".") >= strEmail.length-2){
                                        	return false;
                                        }
                              	}
                       	}
              	}
       	}
      	return true;
}



//////////////////////////////////////////////////////////
// This function will count the number of bytes
// represented in a UTF-8 string
//
// arg1 = the UTF-16 string
// arg2 = the maximum number of bytes allowed in your input field
// Return false is this input string is larger then arg2
// Otherwise return true...
//////////////////////////////////////////////////////////
function isValidUTF8length(UTF16String, maxlength) {
    if (utf8StringByteLength(UTF16String) > maxlength) return false;
    else return true;
}

//////////////////////////////////////////////////////////
// This function will count the number of bytes
// represented in a UTF-8 string
//
// arg1 = the UTF-16 string you want a byte count of...
// Return the integer number of bytes represented in a UTF-8 string
//////////////////////////////////////////////////////////
function utf8StringByteLength(UTF16String) {
  if (UTF16String === null) return 0;
  var str = String(UTF16String);
  var oneByteMax = 0x007F;
  var twoByteMax = 0x07FF;
  var byteSize = str.length;

  for (i = 0; i < str.length; i++) {
    chr = str.charCodeAt(i);
    if (chr > oneByteMax) byteSize = byteSize + 1;
    if (chr > twoByteMax) byteSize = byteSize + 1;
  }  
  return byteSize;
}

//Layout Improvement Checkout Login & Registration common functions - START

	//Function used to validate email entered in Login form
	function testEmail(email,errorId,emailErrorMsg1,emailErrorMsg2){
		var regex1 = /^[^\s@]+@([A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]\.|[A-Za-z0-9]\.)+([A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]|[A-Za-z0-9])$/;
	    var regex2 = /^(root@|abuse@|spam@)/;
	    var emailLC = email.toLowerCase();
	    var retVal = true;   
	    var err;
	    if(email.match("#")){
	    	err = document.getElementById(errorId);
	   		err.style.display= "";
	     	err.innerHTML = errorMsgParaAppend("We're sorry, but your e-mail address cannot contain \"#\". Please enter a different e-mail address.") ;        
	     	retVal = false;
	    } else if(!email.match(regex1)){
	   	 err = document.getElementById(errorId);
	   	 err.style.display= "";
	     err.innerHTML = errorMsgParaAppend(emailErrorMsg1) ;        
	     retVal = false;
	    } else if(emailLC.match(regex2)){
			err = document.getElementById(errorId);     
			err.style.display= "";          
	        err.innerHTML = errorMsgParaAppend(emailErrorMsg2);          
	        retVal = false;
	    }
	    return retVal;      
	}
	//Function used to validate Password entered in Login form
	function checkPasswords(txtPassword,errorId,pwdErrorMsg1,pwdErrorMsg2){     
		var err;	
		var retVal=true;
		for(loop=0;loop<txtPassword.length;loop++){
			if(txtPassword.charCodeAt(loop)== 32){
				err = document.getElementById(errorId);
				err.style.display= "";
				err.innerHTML = errorMsgParaAppend(pwdErrorMsg1);	
				retVal=false;
			}
		}
		if(txtPassword.length==0)	{
			err = document.getElementById(errorId);
			err.style.display= "";
			err.innerHTML = errorMsgParaAppend(pwdErrorMsg2);
			retVal=false;
		}
		return retVal;
	}
	
	//Function used to Trim the extra spaces in sting
	function TrimString(sInString) {
	  if ( sInString ) {
	    sInString = sInString.replace( /^\s+/g, "" );// strip leading
	    return sInString.replace( /\s+$/g, "" );// strip trailing
	  }
	} 
	
	//Function used to validate invalid characters
	function hasInvalidChars(sText){
	   	var retVal=false;
	    var invalidChars = "!?";
		for(loop=0;loop<sText.length;loop++){
		 if(sText.charCodeAt(loop)== 32){
		 	retVal=true;
		 	}
		 if(invalidChars.indexOf(sText.charAt(loop))!= -1){
		 	retVal=true;
		 	}
		 }
		 if(sText.length < 6){
		 	retVal=true;
		 }
		 return retVal;
	}
	
	//Function used to verify numeric or not
	function hasNumeric(sText){
	   var ValidChars = "0123456789";
	   var IsNumber=false;
	   var Char;
	   for (i = 0; i < sText.length && IsNumber == false; i++){ 
      	Char = sText.charAt(i); 
      	if (ValidChars.indexOf(Char) != -1){
	        IsNumber = true;
        }
      }
  	 return IsNumber;
   }
   
   //Function used to vreify letters
   function hasLetter(sText){
	  var ValidChars = "0123456789@#$%^&*(),.:;+-_~{}[]|<>/";
	   var IsLetter=false;
	   var Char;
	   for (i = 0; i < sText.length && IsLetter == false; i++){ 
      	Char = sText.charAt(i); 
      	if (ValidChars.indexOf(Char) == -1){
	        IsLetter = true;
        }
      }
  	 return IsLetter;
   }
   
     /***
  * This javascript function is used by the update registration 'Submit' button.
  * This store does not prompt users for the logon ID during registration.
  * The email address is treated as the logon ID in our case.
  * a) This function will set the logon ID and the email address to the same value.
  * b) If the password fields are empty, this function sets the fields with the values retrieved from UserRegistrationDataBean
  ***/
 
	function getObjectInnerTextUserReg(obj, errString) {
     obj.style.display="";
	 obj.className="errorBox";
	 obj.innerHTML = errString;
	}
	
	   /***
  * This javascript function is used to append <p> to error message

  ***/
 
	function errorMsgParaAppend(errString) {     
	 	//errString = "<p>"+errString+"</p>";
	 	return errString;
	}

//Layout Improvement Checkout Login & Registration common functions - END

//check if the length of loyaltyId is 11 and starts with 43 (sears)
function isValidLoyaltyId(loyaltyId){	
	loyaltyId = TrimString(loyaltyId);
	if (loyaltyId.length==11 && loyaltyId.substring(0,2)=="43") {
    	return true;
    }
    return false;
}

function fnIsNumeric(value){
	var regEx = /^[0-9]+$/;
	if (!regEx.test(value)){
		return false;
	}
	return true;	
}