//********************************************************************
//*-------------------------------------------------------------------
//* 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 setFavoritesCookie(sku){
	var cookieValue = getCookie("LC_FAVORITE");
	if (cookieValue == '' || cookieValue == null) {
		cookieValue = sku;
	} else {
		cookieValue = cookieValue + '|' + sku;
	}
	setCookie("LC_FAVORITE", cookieValue);
	setHeaderFavCount();
}

function setCookie(cookieName,cookieValue,cookieExpires){
	var expires = 30 * 1000 * 60 * 60 * 24;
	if(cookieExpires != null) expires = cookieExpires;
	var today = new Date();
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie=cookieName+ "=" +escape(cookieValue)+ ";expires=" + expires_date.toGMTString() + ";path=/";
}

function getCookie(cookieName){
	if (document.cookie.length>0){
		  c_start=document.cookie.indexOf(cookieName + "=")
		  if (c_start!=-1){ 
		    c_start=c_start + cookieName.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 setHeaderFavCount() {
	var count = 0;
	var cookieValue = getCookie("LC_FAVORITE");
	if (cookieValue == '' || cookieValue == null) {
		count = 0;
	} else {
		cookieValue.ReplaceAll("%7C","|");
		var indvValues = cookieValue.split("|");
		count = indvValues.length;
	}
	
	if (count == 0) {
		var hyperlink = document.getElementById("fav_link");
		hyperlink.display = "none";
		hyperlink.removeAttribute('href');
		var fav_div = document.getElementById("count_fav");
		fav_div.innerHTML= "";
		
	} else {
		var hyperlink = document.getElementById("fav_link");
		hyperlink.disabled = false;
		var fav_div = document.getElementById("count_fav");
		fav_div.display = "block";
		fav_div.innerHTML = "("+count+")";
		
	}
	
}

String.prototype.ReplaceAll = function(stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);
        while(index != -1){
            temp = temp.replace(stringToFind,stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
}

