//********************************************************************
//*-------------------------------------------------------------------
//* 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;
}

function setText(theText, instructionsText){
	var min=2;
	if(theText.value == ""){
		theText.value = instructionsText;
	}
}

function doClear(theText) {
	if (theText.value == theText.defaultValue)
	{
		theText.value = ""
	}
}

var COOKIE_NAME= null;
var MAX_ITEMS = 8;
var MAX_LIFE_IN_DAYS = 30; 
var ENTRY_SPLIT = "<SEP>";
var VALUE_SPLIT = "|";
var MAX_DISPLAY_ITEMS=3;

/* add cookie for current catentry */

function createCookie(name,value,days) {
 if (days) {
  var date = new Date();
  date.setTime(date.getTime()+(days*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
 } else {
  var expires = "";
 }
 document.cookie = name+"="+value+expires+"; path=/";
}
function createSessionLongCookie(name,value) {
  var expires = "; expires=0";
 document.cookie = name+"="+value+"; path=/";
}
 
function readCookie(name) {
 var nameEQ = name + "=";
 var ca = document.cookie.split(';');
 for(var i=0;i < ca.length;i++) {
  var c = ca[i];
  while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 }
 return null;
}
 
function eraseCookie(name) {
 createCookie(name,"",-1);
}


function maskKeyPress(objEvent, reValidChars) {
	var iKeyCode, strKey;
	var strUserAgent = navigator.userAgent.toLowerCase();
	var isIE = strUserAgent.indexOf("msie") > -1;
	var reKeyboardChars = /[\x00\x08\x09\x0D]/; // HOME, END, ARROWS, DELETE, BACKSPACE, TAB, and ENTER

	if (isIE) {
		iKeyCode = objEvent.keyCode;
	} else {
		iKeyCode = objEvent.which;
	}
	strKey = String.fromCharCode(iKeyCode);
	if (!reValidChars.test(strKey) && !reKeyboardChars.test(strKey)) {
	return false;
	}
} 

function isSecure()
	{
   		return window.location.protocol == 'https:';
	}
	
	function getCookie(c_name)
	{
		if (document.cookie.length>0)
		  {
		  c_start=document.cookie.indexOf(c_name + "=");
		  if (c_start!=-1)
		    {
		    c_start=c_start + c_name.length+1;
		    c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) c_end=document.cookie.length;
		    return unescape(document.cookie.substring(c_start,c_end));
		    }
		  }
		return "";
	}			
	
	function checkAuthenticationCookie()
	{
		wcxAuthState=getCookie('WCX_AUTHSTATE');
		if (wcxAuthState!=null && wcxAuthState!="")
		{					
			return wcxAuthState;
		} else {
			return 0;
		}
	}
	
	function checkRememberMeCookie()
	{
		wcxRememberMe=getCookie('WCX_RMBRME');
		if (wcxRememberMe!=null && wcxRememberMe!="")
		{					
			return wcxRememberMe;
		} else {
			return 0;
		}
	}	
	
	var guest_user = false;
	var loggedin_user = false;
	var remembered_user = false;
	var wcxAuthStateCookie = checkAuthenticationCookie();
	var rememberMeCookie = checkRememberMeCookie()
	
	if(wcxAuthStateCookie == 0) {
		guest_user = true;
		loggedin_user = false;
	} else {
		guest_user = false;
		loggedin_user = true;
	}
	
	function displayAlert(target){
	    var spacer = new Number(2);
	    var titem = jq(target);
	    var offsetArray = titem.offset();
	    var targetLeft = offsetArray.left;
	    var targetTop = offsetArray.top;
	    var targetId = titem.attr('id');
//	    console.log(targetId);
	    var alertId = targetId + "_alert";
	    var alertString = '<div id="' + alertId + '" class="checkoutError"></div>';
	   jq('body').append(alertString);
	   jq("#" + alertId).css('float' ,'left')
	    var alertWidth = jq("#" + alertId).width() + spacer;
	   targetLeft = targetLeft - alertWidth;
	   jq("#" + alertId).css('position' ,'absolute').css('left', targetLeft);
	   jq("#" + alertId).css('top', targetTop);
	}
	
