// NebuCart.js

var Cart        = new Array();
var tmpArray    = new Array();
var HPM_ID      = '';
var chargeTax   = false;
var shipDesc    = '';
var cardDesc    = '';
var cardName    = '';
var cardNo      = '';
var cardMonth   = '';
var cardYear    = '';
var postAction  = '';
var delim       = '|';
var paymentWin;
var shopperArray = new Array();
var shipeeArray  = new Array();
var discArray    = discountArray;

function StrConvert(string) {
  var fixedStr = '';
  for (i = 0; i < string.length; i++) {
    if (i == 0) {
      fixedStr = string.charAt(i).toUpperCase();
    }
    else if (string.charAt(i-1) == ' ' || string.charAt(i-1) == '-' || string.charAt(i-1) == '.') {
      fixedStr += string.charAt(i).toUpperCase();
    }
    else {
      fixedStr += string.charAt(i);
    }
  }
  return fixedStr;
}
function calcDiscount(HPM_ID) {
  if (useDiscount && discArray.length > 0) {
    for (i = 0; i < discArray.length; i++) {
      tmpArray = discArray[i].split('|');
      if (tmpArray[0].toUpperCase() == HPM_ID.toUpperCase()) {
        return (tmpArray[3]);
      }
    }
  }
  return 0;
}
function CartItem(RECREATE, prodID, qty, desc, price, opt, limit){
  if(RECREATE){
    this.prodID = prodID;
    this.qty    = qty;
    this.price  = price;
    this.desc   = desc;
    this.opt    = opt;
    this.limit  = limit;
  } else {
    this.prodID = prodID;
    this.qty    = qty;
    this.price  = getValue(prodID,'price');
    this.desc   = getValue(prodID,'desc');
    this.opt    = getValue(prodID,'opt');
    tmpXtra     = getExtraCosts(prodID,'opt');
    tmpOpt      = getMoreOptions(prodID,'opt');
    if(tmpOpt != null && tmpOpt != ''){
      if(this.opt == ''){
        this.opt += tmpOpt;
      } else {
        this.opt += ', ' + tmpOpt;
      }
    }
    if(tmpXtra != 0){
      this.price = Number(this.price) + Number(tmpXtra);
    }
    this.limit = getValue(prodID,'limit');
  }
}
function AddItem(ItemNo){
  var madeChange = false;
  var alreadyExists = false;
  var newItem = false;
  var gotQty = false;
  var Qty;
  var inputLocation;
  var verb = 'has';
  inputLocation = eval('document.NC_form.' + ItemNo);
  if(inputLocation.type == 'select-one'){
    Qty = inputLocation.options[inputLocation.selectedIndex].value;
    if(Qty == ''){
      Qty = inputLocation.options[inputLocation.selectedIndex].text;
    }
  } else {
    Qty = inputLocation.value;
  }
  if(!Number(Qty) || Qty.indexOf('.') != -1){
    alert('Please enter a whole\nnumerical value for item quantity.');
    inputLocation.focus();
  } else {
    gotQty = true;
  }
  if(gotQty){
    if(Cart.length > 0){
      for(i=0; i < Cart.length; i++){
        if(Cart[i].prodID == ItemNo){
          var newOption = getValue(ItemNo,'opt');
          var moreOptions = getMoreOptions(ItemNo,'opt');
          if(moreOptions != null && moreOptions != ''){
            if(newOption == ''){
              newOption = moreOptions;
            } else {
              newOption = + ', ' + moreOptions;
            }
          }
          if(newOption != Cart[i].opt){
            madeChange = false;
            alreadyExists = false;
//          } else if(Cart[i].qty != Qty){
          } else {
            ChangeQty(i,Qty,1);
            madeChange = true;
            alreadyExists = true;
            break;
          }
//          else {
//            alreadyExists = true;
//            alert('Item ' + Cart[i].prodID + ', (' + Cart[i].desc + ')\nalready exists in your cart.');
//            displayCart();
//            break;
//          }
        }
      }
      if(!madeChange && !alreadyExists){
        newItem = true;
      }
    } else {
      newItem = true;
    }
    if(newItem){
      Cart[Cart.length] = new CartItem(false,ItemNo,Qty);
      if(Qty > Number(Cart[Cart.length - 1].limit) && Cart[Cart.length - 1].limit != ''){
        alert('You may only order up to ' + Cart[Cart.length - 1].limit + ' of\n' + Cart[Cart.length - 1].desc + ' per order.\nQuantity will be changed to limit.');
        ChangeQty(Cart.length - 1,Cart[Cart.length - 1].limit,0);
      }
      if(supressCart){
        alert('Item ' + Cart[Cart.length - 1].prodID + ', (' + Cart[Cart.length - 1].desc + ')\nhas been added to your cart.');
        cartToCookie();
      } else {
        displayCart();
      }
    }
  }
}
function DeleteItem(arrayNum){
  var deletedItem;
  tmpArray = new Array();
  for(i=0; i < Cart.length; i++){
    if(i != arrayNum){
      tmpArray[tmpArray.length] = Cart[i];
    } else {
      deletedItem = Cart[i];
    }
  }
  Cart = new Array();
  for(i=0; i < tmpArray.length; i++){
    Cart[i] = tmpArray[i];
  }
  tmpArray = new Array();
  if(deletedItem){
    if(supressCart && String(location).indexOf(cartPage) == -1){
      cartToCookie();
      alert('Item ' + Cart[arrayNum].prodID + ', (' + Cart[arrayNum].desc + ')\nhas been deleted from your cart.');
    } else {
      displayCart();
    }
  } else {
    alert('Item not found in cart.');
  }
}
function UpdateItem(arrayNum){
  oldQty = Cart[arrayNum].qty;
  newQty = getValue(Cart[arrayNum].prodID,arrayNum);
  if(newQty == 0){
    DeleteItem(arrayNum);
    return true;
  } else if(!Number(newQty) || newQty < 0){
    alert('Please enter a whole\nnumerical value.');
    return false;
  } else if(newQty != oldQty){
    ChangeQty(arrayNum,newQty,0);
  }
}
function ChangeQty(arrayNum, newQty, addQty){
  var OK = false;
  var underLimit = true;
  var numQty     = 1;
  if(newQty > Number(Cart[arrayNum].limit) && Cart[arrayNum].limit != ''){
    alert('You may not order more than ' + Cart[arrayNum].limit + '\nof these during a session.\nYour quantity will be changed\nto the maximum amount.');
    Cart[arrayNum].qty = Cart[arrayNum].limit;
  } else {
    numQty *= newQty;
    if(addQty == 1) {
      numQty += (Cart[arrayNum].qty * 1);
    }
    Cart[arrayNum].qty = numQty;
  }

  if(supressCart && String(location).indexOf(cartPage) == -1){
    cartToCookie();
    alert('Item ' + Cart[arrayNum].prodID + ', (' + Cart[arrayNum].desc + ')\nhas been updated in your cart.');
  } else {
    displayCart();
  }
}
function getValue(itemID,valType){
  optionVal = '';
  objVal = eval('document.NC_form.' + itemID + '_' + valType);
  if(Number(valType)){
    return objVal.value;
  }
  if(typeof objVal != 'undefined'){
    if(valType.indexOf('opt') != -1){
      switch (objVal.type){
      case 'select-one':
        optionVal = objVal.options[objVal.selectedIndex].value;
        if(optionVal == ''){
          optionVal = objVal.options[objVal.selectedIndex].text;
        }
        if(optionVal.indexOf(delim) != -1){
          optionVal = optionVal.substring(0,optionVal.indexOf(delim))
        }
      break
      case 'select-multiple':
        for(j = 0; j < objVal.length; j ++){

          if(objVal.options[j].selected){
            currVal = objVal[j].value;
            if(currVal == ''){
              currVal = objVal[j].text;
            }
            if(currVal.indexOf(delim) != -1){
              currVal = currVal.substring(0,currVal.indexOf(delim))
            }
            if(optionVal == ''){
              optionVal = currVal;
            } else {
              optionVal = optionVal + ', ' + currVal;
            }
          }

        }
      break
      }
    } else {
      optionVal = objVal.value
    }
  }
  return optionVal;
}
function getMoreOptions(itemID, valType){
  retStr = '';
  for(optCount = 0; optCount < 20; optCount ++){
    optName = valType + String(optCount + 1);
    tmpStr = getValue(itemID,optName);
    if(tmpStr == '' || tmpStr == null){
      break;
    } else {
      if(retStr == ''){
        retStr = tmpStr;
      } else {
        retStr += ', ' + tmpStr;
      }
    }
  }
  return retStr;
}
function getExtraCosts(itemID,valType){
  tmpCost   = 0;
  optionVal = '';

  for(optCount = 0; optCount < 20; optCount ++){

    if(optCount > 0){
      objVal = eval('document.NC_form.' + itemID + '_' + valType + optCount);
    } else {
      objVal = eval('document.NC_form.' + itemID + '_' + valType);
    }

    if(typeof objVal != 'undefined'){
      if(valType.indexOf('opt') != -1){
        switch (objVal.type){
        case 'select-one':
          optionVal = objVal.options[objVal.selectedIndex].value;
          if(optionVal.indexOf(delim) != -1){
            tmpCost += Number(optionVal.substring(optionVal.indexOf(delim)+1,optionVal.length));
          }
        break
        case 'select-multiple':
          for(j = 0; j < objVal.length; j ++){

            if(objVal.options[j].selected){
              currVal = objVal[j].value;
              if(currVal.indexOf(delim) != -1){
                tmpCost += Number(currVal.substring(currVal.indexOf(delim)+1,currVal.length));
              }
            }

          }
        break
        }
      }
    }
  }
  return tmpCost;
}
function DeleteCart(){
  if(confirm('Are you sure you want\nto clear the entire Shopping Cart?')){
    Cart = new Array();
    displayCart();
  }
}
function displayCart(){
  cartToCookie();
  document.location = cartPage;
}
function getShipDetails(){
  var i = getSelectedRadio(document.NC_form.ship_option);
  var shipDetails = shipOptions[i].split(delim);
  shipDesc        = shipDetails[0];
  shipAmt         = shipDetails[1];
  shipPerItem     = shipDetails[2];
}
function getSelectedRadio(radioGroup){
  for(i = 0; i < radioGroup.length; i ++){
    if(radioGroup[i].checked){
      return i;
    }
  }
  return 0;
}
function fillShopperForm(){
  shopperArray = getCookieVal(myStoreName + '_shopper');
  if(shopperArray == null || shopperArray == ''){
      shopperArray = new Array();
  }
  shipeeArray = getCookieVal(myStoreName + '_shipee');
  if(shipeeArray == null || shipeeArray == ''){
      shipeeArray = new Array();
  }
  if(shopperArray.length > 0){
    if(shopperArray[0] != null && shopperArray[0] != ''){
      document.NC_form.fname.value = shopperArray[0];
    }
    if(shopperArray[1] != null && shopperArray[1] != ''){
      document.NC_form.lname.value = shopperArray[1];
    }
    if(shopperArray[2] != null && shopperArray[2] != ''){
      document.NC_form.email.value = shopperArray[2];
    }
    if(shopperArray[3] != null && shopperArray[3] != ''){
      document.NC_form.add1.value = shopperArray[3];
    }
    if(shopperArray[4] != null && shopperArray[4] != ''){
      document.NC_form.add2.value = shopperArray[4];
    }
    if(shopperArray[5] != null && shopperArray[5] != ''){
      document.NC_form.city.value = shopperArray[5];
    }
    if(shopperArray[6] != null && shopperArray[6] != ''){
      document.NC_form.state.value = shopperArray[6];
    }
    if(shopperArray[7] != null && shopperArray[7] != ''){
      document.NC_form.zip.value = shopperArray[7];
    }
    if(shopperArray[8] != null && shopperArray[8] != ''){
      document.NC_form.country.value = shopperArray[8];
    }
    if(shopperArray[9] != null && shopperArray[9] != ''){
      document.NC_form.phone.value = shopperArray[9];
    }
    if(shopperArray[10] != null && shopperArray[10] != ''){
      document.NC_form.hpmID.value = shopperArray[10];
    }
  }
  if(shipeeArray.length > 0){
    if(shipeeArray[0] != null && shipeeArray[0] != ''){
      document.NC_form.Sfname.value = shipeeArray[0];
    }
    if(shipeeArray[1] != null && shipeeArray[1] != ''){
      document.NC_form.Slname.value = shipeeArray[1];
    }
    if(shipeeArray[2] != null && shipeeArray[2] != ''){
      document.NC_form.Sadd1.value = shipeeArray[2];
    }
    if(shipeeArray[3] != null && shipeeArray[3] != ''){
      document.NC_form.Sadd2.value = shipeeArray[3];
    }
    if(shipeeArray[4] != null && shipeeArray[4] != ''){
      document.NC_form.Scity.value = shipeeArray[4];
    }
    if(shipeeArray[5] != null && shipeeArray[5] != ''){
      document.NC_form.Sstate.value = shipeeArray[5];
    }
    if(shipeeArray[6] != null && shipeeArray[6] != ''){
      document.NC_form.Szip.value = shipeeArray[6];
    }
    if(shipeeArray[7] != null && shipeeArray[7] != ''){
      document.NC_form.Scountry.value = shipeeArray[7];
    }
    if(shipeeArray[8] != null && shipeeArray[8] != ''){
      document.NC_form.Semail.value = shipeeArray[8];
    }
  }
}
function Validate(orderOption, secureWin){
  var countrySel;
  var AllOk     = true;
  var tmpPhone  = '';
  var phoneChar = new Array('-','(',')',' ');
  var goodChar  = true;
  countrySel = document.NC_form.country;
  shopperArray[0] = document.NC_form.fname.value;
  shopperArray[1] = document.NC_form.lname.value;
  shopperArray[2] = document.NC_form.email.value;
  shopperArray[3] = document.NC_form.add1.value;
  shopperArray[4] = document.NC_form.add2.value;
  shopperArray[5] = document.NC_form.city.value;
  shopperArray[6] = document.NC_form.state.value;
  shopperArray[7] = document.NC_form.zip.value;
  if(countrySel.selectedIndex < 0 || countrySel.selectedIndex == 0){
    alert('Please select your country.');
    AllOk = false;
  } else {
    shopperArray[8] = countrySel.options[countrySel.selectedIndex].text;
  }
  shopperArray[9] = document.NC_form.phone.value;
  shopperArray[10] = document.NC_form.hpmID.value;
  for(w = 0; w < shopperArray.length; w++){
    shopperArray[w] = shopperArray[w].trim();
  }
  if(getAltShipping){
    if(document.NC_form.diffShip.checked){
      countrySel = document.NC_form.Scountry;
      shipeeArray[0] = document.NC_form.Sfname.value;
      shipeeArray[1] = document.NC_form.Slname.value;
      shipeeArray[2] = document.NC_form.Sadd1.value;
      shipeeArray[3] = document.NC_form.Sadd2.value;
      shipeeArray[4] = document.NC_form.Scity.value;
      shipeeArray[5] = document.NC_form.Sstate.value;
      shipeeArray[6] = document.NC_form.Szip.value;
      shipeeArray[8] = document.NC_form.Semail.value;
      if(countrySel.selectedIndex < 0 || countrySel.selectedIndex == 0){
        alert('Please select your country.');
        AllOk = false;
      } else {
        shipeeArray[7] = countrySel.options[countrySel.selectedIndex].text;
      }
      for(w = 0; w < shipeeArray.length; w++){
        shipeeArray[w] = shipeeArray[w].trim();
      }
    }
  }
  if(shopperArray[0] == '' && AllOk){
    alert('Please fill out the "Full Name" field');
    document.NC_form.fname.focus();
    AllOk = false;
  }
  shopperArray[0] = StrConvert(shopperArray[0]);
  if(AllOk){
    if(shopperArray[2] == ''){
      alert('Please fill out the "Email" field');
      document.NC_form.email.focus();
      AllOk = false;
    } else {
      if(shopperArray[2].indexOf('@') == -1){
        alert('Please enter a valid Email address.');
        document.NC_form.email.focus();
        AllOk = false;
      }
    }
  }
  if(shopperArray[3] == '' && AllOk){
    alert('Please fill out the "Address" field');
    document.NC_form.add1.focus();
    AllOk = false;
  }
  shopperArray[3] = StrConvert(shopperArray[3]);
  shopperArray[4] = StrConvert(shopperArray[4]);
  if(shopperArray[5] == '' && AllOk){
    alert('Please fill out the "City" field');
    document.NC_form.city.focus();
    AllOk = false;
  }
  shopperArray[5] = StrConvert(shopperArray[5]);
  if(shopperArray[8].toLowerCase() == 'us' ||
     shopperArray[8].toLowerCase() == 'usa' ||
     shopperArray[8].toLowerCase() == 'united states' ||
     shopperArray[8].toLowerCase() == 'united states of america' ||
     shopperArray[8].toLowerCase() == 'unitedstates'){
    if(shopperArray[6] == '' && AllOk){
      alert('Please fill out the "State" field'); //HPM
      document.NC_form.state.focus();
      AllOk = false;
    }
    shopperArray[6] = shopperArray[6].toUpperCase();
    if(shopperArray[7] == '' && AllOk){
      alert('Please fill out the "Zip Code" field'); //HPM
      document.NC_form.zip.focus();
      AllOk = false;
    }
  }
  if(shopperArray[8] == '' && AllOk){
    alert('Please fill out the "Country" field');
    document.NC_form.country.focus();
    AllOk = false;
  }
  if(shopperArray[9] == '' && AllOk){
    if(shopperArray[9] == ''){
      alert('Please fill out the "Phone Number" field');
      document.NC_form.phone.focus();
      AllOk = false;
    } else {
      for(i = 0; i < shoppArray[9].length; i ++){
        for(j = 0; j < phoneChar.length; j ++){
          if(shopperArray[9].charAt(i) != phoneChar[j]){
            goodChar = true;
          } else {
            goodChar = false;
          }
        }
        if(goodChar){
          tmpPhone = tmpPhone + shopperArray[9].charAt(i);
        }
      }
      if(!Number(tmpPhone)){
        alert('Please enter a valid Phone number');
        document.NC_form.phone.focus();
        allOk = false;
      }
    }
  }
  if (shopperArray[10] == '(Optional)') { shopperArray[10] = ''; }
  if(shopperArray[10] != null && shopperArray[10] != '' && AllOk){ //HPM
    if (HpmDiscount = calcDiscount(shopperArray[10]) == 0) {
      alert('Invalid CODE "' + shopperArray[10] + '". \nPlease re-enter a Valid CODE or leave it Blank.\nFor detail, contact HippoMall toll free 1-888-41-Hippo'); //HPM
      document.NC_form.hpmID.focus(); //HPM
      AllOk = false; //HPM
    } //HPM
    shopperArray[10] = shopperArray[10].toUpperCase();
  } //HPM
  if(getAltShipping){
    if(document.NC_form.diffShip.checked){
      if(shipeeArray[0] == '' && AllOk){
        alert('Please fill out the "Ship to Full Name" field'); //HPM
        document.NC_form.Sfname.focus();
        AllOk = false;
      }
      shipeeArray[0] = StrConvert(shipeeArray[0]);
      if(shipeeArray[2] == '' && AllOk){
        alert('Please fill out the "Ship to Address" field');
        document.NC_form.Sadd1.focus();
        AllOk = false;
      }
      shipeeArray[2] = StrConvert(shipeeArray[2]);
      shipeeArray[3] = StrConvert(shipeeArray[3]);
      if(shipeeArray[4] == '' && AllOk){
        alert('Please fill out the "Ship to City" field');
        document.NC_form.Scity.focus();
        AllOk = false;
      }
      shipeeArray[4] = StrConvert(shipeeArray[4]);
      if(shipeeArray[5] == '' && AllOk){
        alert('Please fill out the "Ship to State" field'); //HPM
        document.NC_form.Sstate.focus();
        AllOk = false;
      }
      shipeeArray[5] = shipeeArray[5].toUpperCase();
      if(shipeeArray[6] == '' && AllOk){
        alert('Please fill out the "Ship to Zip Code" field'); //HPM
        document.NC_form.Szip.focus();
        AllOk = false;
      }
      if(shipeeArray[7] == '' && AllOk){
        alert('Please fill out the "Ship to Country" field');
        document.NC_form.Scountry.focus();
        AllOk = false;
      }
    }
  }
  if(AllOk){
    setCookie(myStoreName + '_shopper',shopperArray.join(delim),eval(customerTime),cookiePath,unsecureDomain);
    if(shipeeArray.length > 0){
      setCookie(myStoreName + '_shipee',shipeeArray.join(delim),null,cookiePath,unsecureDomain);
    }
    routeToPayment(orderOption, secureWin);
  } else {
    //return false;
  }
}
function calcShipping(){
  if(!shipPercent && !shipPerItem && !useShipRules && !useShipOptions){
    return shipAmt;
  }
  if(!shipPercent && shipPerItem && !useShipRules && !useShipOptions){
    return (shipAmt * totalQty);
  }
  if(shipPercent && !shipPerItem && !useShipRules && !useShipOptions){
    return (shipAmt * totalCost);
  }
  if(!useShipRules && useShipOptions){
      if(shipPerItem){
          return (shipAmt * totalQty);
      } else {
          return shipAmt;
      }
  }
  if(useShipRules && !useShipOptions){
    return getShipRule(totalCost,totalQty);
  }
  return 0;
}
function routeToPayment(orderOption, secureWin){
  chargeTax = false;
  if(shipeeArray[5] != null && shipeeArray[5] != '') {
    if (shipeeArray[5].toLowerCase() == myState1 || shipeeArray[5].toLowerCase() == myState2) {
      chargeTax = true;
    }
  }
  else if (shopperArray[6].toLowerCase() == myState1 || shopperArray[6].toLowerCase() == myState2){
      chargeTax = true;
  }
  if(useShipOptions){
    getShipDetails();
  }
  killCookie(myStoreName + '_chargeTax');
  killCookie(myStoreName + '_shipDesc');
  killCookie(myStoreName + '_shipAmt');
  killCookie(myStoreName + '_shipPerItem');
//killCookie(myStoreName + '_HpmDiscount');
  setCookie(myStoreName  + '_chargeTax'   ,chargeTax     ,eval(cartTime),cookiePath,unsecureDomain);
  setCookie(myStoreName  + '_shipDesc'    ,shipDesc      ,eval(cartTime),cookiePath,unsecureDomain);
  setCookie(myStoreName  + '_shipAmt'     ,shipAmt       ,eval(cartTime),cookiePath,unsecureDomain);
  setCookie(myStoreName  + '_shipPerItem' ,shipPerItem   ,eval(cartTime),cookiePath,unsecureDomain);
//setCookie(myStoreName  + '_HpmDiscount' ,HpmDiscount   ,eval(cartTime),cookiePath,unsecureDomain);
  if(orderOption == '' || orderOption == null){
    document.location = COprintVerify;
  } else {
    if(secureWin && securePath != ''){
      if(securePath.charAt(securePath.length-1) != '/'){
        securePath += '/';
      }
      var securePost;
      var prodID = new Array();
      var qty    = new Array();
      var price  = new Array();
      var desc   = new Array();
      var opt    = new Array();
      var limit  = new Array();
      for(i = 0; i < Cart.length; i ++){
        prodID[i] = Cart[i].prodID;
        qty[i]    = Cart[i].qty;
        price[i]  = Cart[i].price;
        desc[i]   = Cart[i].desc;
        opt[i]    = Cart[i].opt;
        limit[i]  = Cart[i].limit;
      }
      securePost  =  securePath + orderOption + '?';
      securePost += 'NC_A='  + escape(shopperArray.join(delim));
      securePost += '&NC_B=' + escape(shipeeArray.join(delim));
      securePost += '&NC_C=' + escape(shipDesc);
      securePost += '&NC_D=' + escape(shipAmt);
      securePost += '&NC_E=' + shipPerItem;
      securePost += '&NC_F=' + shipPercent;
      securePost += '&NC_G=' + useShipOptions;
      securePost += '&NC_H=' + useShipRules;
      securePost += '&NC_I=' + chargeTax;
      securePost += '&NC_J=' + escape(prodID.join(delim));
      securePost += '&NC_K=' + escape(qty.join(delim));
      securePost += '&NC_L=' + escape(price.join(delim));
      securePost += '&NC_M=' + escape(opt.join(delim));
      securePost += '&NC_N=' + escape(desc.join(delim));
      securePost += '&NC_O=' + escape(limit.join(delim));
      parent.document.location = securePost;
    } else {
      document.location = orderOption;
    }
  }
}
function cartToCookie(){
  for (cc = 0; cc < 10000; cc++){
    cookieVal = getCookieVal(myStoreName + '_item' + cc);
    if(cookieVal != null && cookieVal != ''){
      killCookie(myStoreName + '_item' + cc);
    } else {
      break;
    }
  }
  if(Cart.length > 0){
    for(cc = 0; cc < Cart.length; cc++){
      itemContents  = '';
      itemContents += Cart[cc].prodID + delim;
      itemContents += Cart[cc].qty    + delim;
      itemContents += Cart[cc].desc   + delim;
      itemContents += Cart[cc].price  + delim;
      itemContents += Cart[cc].opt    + delim;
      itemContents += Cart[cc].limit  + delim;
      setCookie(myStoreName + '_item' + cc,itemContents,eval(cartTime),cookiePath,unsecureDomain);
    }
  }
}
function cookieToCart(){
  shopperArray = getCookieVal(myStoreName + '_shopper');
  if(shopperArray == null || shopperArray == ''){
      shopperArray = new Array();
  }
  shipeeArray = getCookieVal(myStoreName + '_shipee');
  if(shipeeArray == null || shipeeArray == ''){
      shipeeArray = new Array();
  }
  chargeTax = getCookieVal(myStoreName + '_chargeTax');
  chargeTax = (chargeTax == 'true') ? true : false;
  shipDesc = getCookieVal(myStoreName + '_shipDesc');
  tmpShipAmt = getCookieVal(myStoreName + '_shipAmt');
  if(tmpShipAmt != null && tmpShipAmt != ''){
    shipAmt = tmpShipAmt;
  }
  tmpShipPerItem = getCookieVal(myStoreName + '_shipPerItem');
  if(tmpShipPerItem != null && tmpShipPerItem != ''){
    tmpShipPerItem = (tmpShipPerItem == 'true') ? true : false;
    shipPerItem    = tmpShipPerItem;
  }
//  tmpDiscount = getCookieVal(myStoreName + '_HpmDiscount'); //HPM
//  if(tmpDiscount != null && tmpDiscount != ''){ //HPM
//    HpmDiscount = tmpDiscount; //HPM
//  } //HPM
  if(shipPercent || useShipRules){
    shipPerItem = false;
  }
  for (cc = 0; cc < 10000; cc++){
    cookieVal = getCookieVal(myStoreName + '_item' + cc);
    if(cookieVal != null && cookieVal != ''){
      itemVal  = getCookieVal(myStoreName + '_item' + cc);
      Cart[cc] = new CartItem(true,itemVal[0],itemVal[1],itemVal[2],itemVal[3],itemVal[4],itemVal[5]);
    } else {
      break;
    }
  }
}
function getCookie(key){
  theCookie = '';
  if(document.cookie && document.cookie.indexOf(key) != -1){
    theCookie = document.cookie;
    theCookie = theCookie.substring(theCookie.indexOf(key),theCookie.length);
    if(theCookie.indexOf(';') != -1){
      theCookie = theCookie.substring(0,theCookie.indexOf(';'));
    }
    return theCookie;
  }
}
function getCookieVal(key){
  theCookie = getCookie(key);
  if(theCookie != null && theCookie != ''){
    theVal = theCookie.substring(theCookie.indexOf(key) + key.length + 1,theCookie.length);
    if(theVal.indexOf(delim) != -1){
      theVal = theVal.split(delim);
    }
    return theVal;
  } else {
    return '';
  }
}
function setCookie(key,val,exp,path,site){

  theCookie = key + '=' + val;
  if(exp != '' && exp != null){
    theCookie += ';expires=' + exp.toGMTString();
  }
  if(path != '' && path != null){
    theCookie += ';path=' + path;
  }
  if(site != '' && site != null){
    theCookie += ';domain=' + site;
  }
  currLocation = String(location).toLowerCase();
  if(currLocation.indexOf('https') != -1){
    theCookie += ';secure';
  }
  document.cookie = theCookie;

}
function killCookie(key){
  setCookie(key,'',setExp(0,0,0,0,0,0,-2),cookiePath,unsecureDomain);
}
function setExp(S,M,H,D,W,Mo,Y){
  exp = new Date();
  sec = 1000;
  min = 60  * sec;
  hr  = 60  * min;
  day = 24  * hr;
  wk  = 7   * day;
  mo  = 30  * day;
  yr  = 365 * day;
  theLen  = exp.getTime();
  theLen += (S * sec) + (M * min) + (H * hr);
  theLen += (D * day) + (W * wk) + (Mo * mo);
  theLen += (Y * yr);
  exp.setTime(theLen);
  return exp;
}
function trim(){
  tmpTxt = this.toString();

  while(tmpTxt.charAt(0) == ' '){
    tmpTxt = tmpTxt.substring(1,tmpTxt.length);
  }
  while(tmpTxt.charAt(tmpTxt.length - 1) == ' '){
    tmpTxt = tmpTxt.substring(0,tmpTxt.length-1);
  }
  return tmpTxt;
}
String.prototype.trim = trim;
if(String(location).indexOf('checkedOut') != -1){
  Cart = new Array();
  cartToCookie();
} else {
  cookieToCart();
}
