//********************************************************************
//*-------------------------------------------------------------------
//* 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
//////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////
// 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;
}



/////////////////////////////////////////////////////////////////////
//   BeuatyControl 2007 11
//
/////////////////////////////////////////////////////////////////////

function isValidName(name){
		var regex=/^[a-zA-Z]+$/; 
		if ( regex.test(name) == false) {
			alert(" El nombre debe contener caracteres alfab\351ticos.");
			return false;
		}
		return true;
	}

//////////////////////////////////////////////////////////
// 
// validate Password
//
// 
//////////////////////////////////////////////////////////


function isValidPassword(StrPassword) {
  var count_digit = 0;
  var count_letter = 0;
          
     if(StrPassword.length < 6)
           {
              alert("La contrase\361a debe tener al menos 6 caracteres");
              return false;
           }
           for (var i=0; i< StrPassword.length; i++)
           {
             var ch= StrPassword.charAt(i);
              if(( ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z"))
              {
                count_letter = count_letter +1; 
              }
               if(ch >= "0" && ch <= "9")
              {
                count_digit = count_digit +1; 
              }

           }
           if(count_letter >=1  && count_digit >= 1)
           {
             return true;
           }
           else {
             alert("Ingresa al menos un d\355gito y una letra ");
             return false;
           }
}

function isValidPassword_0(StrPassword) {
  var count_digit = 0;
  var count_letter = 0;
          
     if(StrPassword.length < 6)
           {
              return false;
           }
           for (var i=0; i< StrPassword.length; i++)
           {
             var ch= StrPassword.charAt(i);
              if(( ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z"))
              {
                count_letter = count_letter +1; 
              }
               if(ch >= "0" && ch <= "9")
              {
                count_digit = count_digit +1; 
              }

           }
           if(count_letter >=1  && count_digit >= 1)
           {
             return true;
           }
           else {             
             return false;
           }
}


function isValidEmail(strEmail){
		var regex = /^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;
		if(regex.test(strEmail)== false){
		   alert("Ingresa una direcci\363n de email v\341lida");
		   return false;
		}
            return true;
	}

function isValidEmail_0(strEmail){
		var regex = /^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;
		if(regex.test(strEmail)== false){		   
		   return false;
		}
            return true;
	}

//////////////////////////////////////////////////////////
// 
// validate Zip code for USA
//
// 
//////////////////////////////////////////////////////////

function isValidZipUS(zip){
		var regex=/^\d{5}$/; // Match for 5 numbers
          
		if ( regex.test(zip) == false) {
			alert("\241Ingresa un c\363digo postal v\341lido!");
         		return false;
		}
		      return true;
		}

function isValidZipUS_0(zip){
		var regex=/^\d{5}$/; // Match for 5 numbers
          
		if ( regex.test(zip) == false) {			
         		return false;
		}
		      return true;
		}



//////////////////////////////////////////////////////////
// 
// Convert Zip code for Canada
// 
//    ######  are converted to ### ###
//    ###-### are converted to ### ###
// 
//////////////////////////////////////////////////////////

function  convertCAZipCode(zipCode)
  {    
    var newZipCode = "";
    if(zipCode == "" || zipCode == null)
    {
       return zipCode;
    }
    else      // zipCode is not empty
    {
    if(zipCode.length == 6)
    {
      newZipCode = zipCode.substr(0,3) + " " + zipCode.substr(3,6);
    }
    else if(zipCode.length == 7)
    {
      newZipCode = zipCode.substr(0,3) + " " + zipCode.substr(4,7);
    }
    else
    {
      newZipCode = zipCode;
    }
     return newZipCode.toUpperCase();
    }  //end else;
  }


//////////////////////////////////////////////////////////
// 
// validate Zip code for Canada
//
// 
//////////////////////////////////////////////////////////
function isValidZipCA(zip){          
          var regex = /^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/;
  
          if(regex.test(zip)== false)
          {
              alert("Ingresa un c\363digo postal v\341lido.");
              return false;
          }
          return true;
      }

function isValidZipCA_0(zip){          
          var regex = /^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/;
  
          if(regex.test(zip)== false)
          {              
              return false;
          }
          return true;
      }



//////////////////////////////////////////////////////////
// 
// validate Phone number
//
// 
//////////////////////////////////////////////////////////

function isValidPhone(phoneNumber){
 	var regex = /^\(?\d{3}\)?-?\s*\d{3}\s*-?\d{4}$/;
	if(regex.test(phoneNumber)== false){
	   alert("Ingresa un n\372mero de tel\351fono v\341lido");
	   return false;
	}
      return true;
}


function isValidPhone_0(phoneNumber){
 	var regex = /^\(?\d{3}\)?-?\s*\d{3}\s*-?\d{4}$/;
	if(regex.test(phoneNumber)== false){	   
	   return false;
	}
      return true;
}



/////////////////////////////////////////////////////////////////////////
// 
//  trim phone number, only digit number are valid , remove rest of char
//
// 
/////////////////////////////////////////////////////////////////////////

function trimNumber(inNumber)
{
  var i;
  var newNumber ="";
  for(i=0; i< inNumber.length; i++)
  {
   var char = inNumber.charAt(i);
   
     if( (char <"0") || (char > "9"))
      {
       // skip
      } 
     else
      {
        newNumber = newNumber + char;
      }
  }

    return newNumber;
}


function BillNickName(form)
{
   var firstName = form.firstName.value;
   var lastName  = form.lastName.value;
   var address1  = form.address1.value;
   var nickName  = firstName + "_" + lastName + "_" + address1;
   return nickName;
}

function ShipNickName(form)
{
   var firstName = form.sfirstName.value;            
   var lastName  = form.slastName.value;
   var address1  = form.saddress1.value;
   var nickName  = firstName + "_" + lastName + "_" + address1;
   return nickName;
}


function isValidPosIntQty(strQty)
 {
   if (strQty == "" || strQty == null)
   {
    alert("Ingresa una cantidad v\341lida.");
    return false;
   }
	
        for (var i=0; i<strQty.length; i++)
	 {
          var ch = strQty.substring(i,i+1);
          if (ch < "0" || ch > "9")
		{
		  alert("Ingresa un n\372mero entero positivo para la cantidad");	        
		  return false;
		}
        }
         
        if (strQty == "0")
		{	      
		  alert("Ingresa un n\372mero entero positivo para la cantidad");
		  return false;
		}               
        return true;
 }




//////////////////////////////////////////////////////////
// 
// 1,2,3 .... are valid quantity
//
// 
//////////////////////////////////////////////////////////

function isValidPositiveQty(strQty)    
{
 if (strQty == "" || strQty == null)
   {
    alert("Ingresa una cantidad v\341lida.");
    return false;
   }

  else if (isNaN(strQty) == true)
   {
    alert("Ingresa una cantidad v\341lida.");
    return false;
   }
  else if(strQty < 1)
   {
    alert("Ingresa un n\372mero entero positivo.");
    return false;
   }
   return true;
}

//////////////////////////////////////////////////////////
// 
// 0,1,2,3 .... are valid quantity
//
// 
//////////////////////////////////////////////////////////


function isValidQuantity(strQty)    
{
  if (strQty == "" || strQty == null)
   {
    alert("Ingresa una cantidad v\341lida.");
    return false;
   }
  else if (isNaN(strQty) == true )
   {
    alert("Ingresa una cantidad v\341lida.");
    return false;
   }
  else if(strQty < 0)
   {
    alert("Ingresa una cantidad v\341lida.");
    return false;
   }
   return true;
}

//////////////////////////////////////////////////////////
// 
// Validate Birthday
//
// 
//////////////////////////////////////////////////////////

function validBirthday(form)
  {
    if(form.month.value!="0" && form.day.value!="0" && form.year.value != "0")     // provide birthday, OK
     {
       return true;
     }
    if(form.month.value=="0" && form.day.value=="0" && form.year.value == "0")     // did not provide birthday, OK
     {
       return true;
     }
     else
     {
       alert("Ingresa una fecha de cumplea\361os v\341lida.");
       return false;
     }
  }




function trim(inword)
{
   word = inword.toString();
   var i=0;
   var j=word.length-1;
   while(word.charAt(i) == " ") i++;
   while(word.charAt(j) == " ") j=j-1;
   if (i > j) {
		return word.substring(i,i);
	} else {
		return word.substring(i,j+1);
	}
}


//////////////////////////////////////////////////////////
// 
//  character # is not allowed for address field.
//
// 
//////////////////////////////////////////////////////////


function isValidAddress(strAddress)
  {
    var myString = strAddress;
    var regex = /#/;
    if (regex.test(myString))
     {
       alert("Direcci\363n inv\341lida: # es un caracter inv\341lido en la direcci\363n.");
       return false;
     }
    else
     {
       return true;
     }
  }

 

////////////////////////////////////////////////////////////////////
// 
//  character ; : "  are invalid characters in address field in US.
//
// 
////////////////////////////////////////////////////////////////////


function isValidAddress_US(strAddress)
  {
    var myString = strAddress;   
    var regex2 = /;/;
    var regex3 = /:/;
    var regex4 = /"/;
    var regex5 = /&/;
    
    if ( regex2.test(myString) || regex3.test(myString) 
          || regex4.test(myString) || regex5.test(myString))
     {    
       return false;
     }
    else
     {
       return true;
     }
  }



/////////////////////////////////////////////////////////////////////
// 
//  character # ; : "  are invalid characters in address field for CA
//
// 
/////////////////////////////////////////////////////////////////////
function isValidAddress_CA(strAddress)
  {
    var myString = strAddress;
    var regex1 = /#/;
    var regex2 = /;/;
    var regex3 = /:/;
    var regex4 = /"/;
    var regex5 = /&/;

   
    if (regex1.test(myString) || regex2.test(myString) || regex3.test(myString) 
          || regex4.test(myString) || regex5.test(myString))
     {    
       return false;
     }
    else
     {
       return true;
     }
    }
 

//////////////////////////////////////////////////////////
// 
//  character &  is invalid characters in input field.
//
// 
//////////////////////////////////////////////////////////


function isValidInput(inString)
  {
    var myString = inString ;
    var regex1 = /&/;   
    if (regex1.test(myString))
     {    
       return false;
     }
    else
     {
       return true;
     }
  }





//////////////////////////////////////////////////////////
// 
//  return Object based on ElementId.
//
// 
//////////////////////////////////////////////////////////

function findObj(n, d) { 
	var p,i,x;  
	if(!d) d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
	   d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n);
	return x;
}



//////////////////////////////////////////////////////////
// 
// Hide and display division "errorMessage_j"
// Total division length is based on param "length"
//
//////////////////////////////////////////////////////////

function displayMessage(number, length) { 
	var i,p,v,obj, j;
	var divName;    
    
   for(j=0; j<length; j++)
   {              
     var  divName = 'errorMessage_' + j ;     
	if ((obj= findObj(divName))!=null) {                      
	  if (obj.style) { 
		obj=obj.style;
		if (j == number) {
		   obj.visibility='visible';
		   obj.display='block';
		} else {
		        obj.visibility='hidden';
		        obj.display='none';
			 }
		} 
	} 
   }	
}



function toLower( fld)
	{
		fld.value = fld.value.toLowerCase();
	}



function CategoryDisplay(form, _parent_category_rn,_categoryId)
{    
   form.action = "CategoryDisplay";  
   form.parent_category_rn.value = _parent_category_rn;
   form.categoryId.value = _categoryId;
   form.submit();  
}

function ProductDisplay(form ,_parent_category_rn,_productId)
{    
   form.action = "ProductDisplay";
   form.parent_category_rn.value = _parent_category_rn;
   form.productId.value = _productId;
   form.submit();  
}


function ContentDisplay(form ,pageName, showBG)
{    
   form.action = "ContentView";   
   form.pageID.value = pageName;
   form.showBG.value = showBG;   
   form.submit();  
}

function PageDisplay(form, pageName)
{   
   var URLLink = "";
   var cnum = form.cnum.value;
   if(pageName== 'canada')
   {    
    form.action = "TopCategoriesDisplay";
    form.storeId.value = "11051";
    form.catalogId.value = "10051";     
    form.submit();          
   }

  /* else if(pageName== 'hostSpaUS')
   {
    URLLink= "http://www.beautipage.com/info/hostess/party.asp"; 
    URLLink = URLLink + "?cn=" + cnum; 
    location.href = URLLink;
   }

   else if(pageName== 'hostSpaCA')
   {
    URLLink= "http://www.beautipage.ca/info/hostess/party.asp";  
    URLLink = URLLink + "?cn=" + cnum; 
    location.href = URLLink;            
   } */ 
}



//////////////////////////////////////////////////////////
// 
//  return quantity value based on quantity index
//
// 
//////////////////////////////////////////////////////////
function getQTY(form, index)
{
 var QtyName= "qty_" + index;
 var QtyValue = 0;
     for(var i=0; i<form.elements.length; i++)
      {
        if(form.elements[i].name == QtyName && form.elements[i].type == "text")
         {
           QtyValue = form.elements[i].value;
           break;
         }
      }
  QtyValue = trim(QtyValue);
  return QtyValue;
}


/***********************************************
* <b style="color:black;background-color:#ffff66">Textarea</b> Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length > mlength)
		obj.value=obj.value.substring(0,mlength)
}


//////////////////////////////////////////////////////////
// 
//  Disable the Enter key on HTML form 
// 
//////////////////////////////////////////////////////////

function stopRKey(evt) { 
var evt = (evt) ? evt : ((event) ? event : null); 
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
if ((evt.keyCode == 13) && (node.type=="text")) {return false;} 
} 



//////////////////////////////////////////////////////////
// 
//  remove character <'"&> from the string
//  remove extra empty space from the string
// 
//////////////////////////////////////////////////////////
function trimMessage(strMessage)
{
 var regex = /[<>'"&]+/g; 
 var newMessage;
 var tempMessage;

 if(strMessage != ""  && strMessage != null)
 {
   tempMessage  = strMessage.replace(regex, " ");  
   newMessage = tempMessage.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');   
   return newMessage;
 }
 else
  {
    return strMessage;
  }
}

//////////////////////////////////////////////////////////
// This function will return the value of a cookie
//
// arg1 = cookie name
// Return the value of the cookie
//////////////////////////////////////////////////////////
function getCookie(name) {

	var cookies = document.cookie.split(';');
	var value   = null;

	for ( var i=0; i < cookies.length; i++ ) {
		var cookie = cookies[i];

		while ( cookie.charAt(0) == ' ' ) {
			cookie = cookie.substring(1, cookie.length );
		}

		if ( cookie.indexOf(name+"=") == 0 ) {
			value = cookie.substring(name.length+1, cookie.length);
			value =	unescape(value);
		}
	}

	return value;
}


//////////////////////////////////////////////////////////
// This function will hide the error message
// that is displayed in previous submit
// while stay in same page
// 
//////////////////////////////////////////////////////////
function clearErrorMessage(length) { 
	var i,p,v,obj, j;
	var divName;    
    
   for(j=0; j<length; j++)
   {              
     var  divName = 'errorMessage_' + j ;     
	if ((obj= findObj(divName))!=null) {                      
	  if (obj.style) 
        { 
		obj=obj.style;		
	      obj.visibility='hidden';
		obj.display='none';			 
	  } 
	} 
   }	
}



