<!--

//this function is called when the checkout/update/keep Shopping button is clicked.
function checkBasketForm(){
  var basketForm = document.BasketForm;
  var msgs = '';
 
  //update shipTogether value (TODO do we need to do that? why is the value being set there in html?
  document.subtotalForm.shipTogether.value = document.BasketForm.shipTogether[0].checked;
  
  
  //for each element on basketForm...
  for(var i = 0; i < basketForm.elements.length; i++)
  {
   
    var ossId = trimPrefix('GCquantity_', basketForm.elements[i].name);
    	
    if(ossId) //if this value is 1 then we are dealing with GC 
    {
      var partitionId = ossId.substring(0, ossId.indexOf('_'));
      recipient = basketForm.elements["recipient_" + partitionId].value;
      if (recipient == 'me') recipient = 'your';
      else recipient += '\'s';
      amountStr = basketForm.elements["GCamount_" + ossId].value;
      msgs += validateGCAmount(amountStr, recipient); //validate GC amount and return a message if needed. TODO do we need a recipient it is never used
    	//Why do we need to call it here if this is already done onChnage for text field?
    }
  }
  
  var quantities = new Object();
  
  var partition_count = parseInt(basketForm.elements["partition_count"].value);
  
  for(var p = 0; p < partition_count; p++) //for each partition...
  {
    var item_count = parseInt(basketForm.elements["item_count_" + p].value);
    for(var i = 0; i < item_count; i++) //for each item in a partition...
    {
      var fieldId = p + "_" + i;
      //if it is a GC item...
      if (basketForm.elements["is_gift_certificate_item_" + fieldId].value != "true")
      {
        var qtyField = basketForm.elements["quantity_" + fieldId];
        var qty = parseInt(qtyField.options[qtyField.selectedIndex].value);
        var skuField = basketForm.elements["sku_" + fieldId];
        var sku = skuField.value;
        if (quantities[sku]) 
        	quantities[sku] += qty;
        else 
        	quantities[sku] = qty;
      }
    }
  }


  invForm = document.skuInventoryCache;
  for(var os in quantities)
  {
    var qty = quantities[os];
    var inv = parseInt(invForm["inv_" + os].value);
    var bo = parseInt(invForm["bo_" + os].value);
    var itemName = invForm["itemName_" + os].value;
    var size = invForm["size_" + os].value;
    var color = invForm["color_" + os].value;
    //invStr += qty + " / " + inv + "\n";
    if (qty > inv)
    {
      if (qty > bo)
      {
        var itemDesc = itemName + ", " + size + " " + color;
        var msg = "We only have " + inv + " units of the " + itemDesc + " available.\n";
        msg += "You currently have a total of " + qty + " in your basket. Please reduce some quantities.\n";
        msgs += msg;
      }
    }
  }
  //msgs += 'inv = ' + invStr + '\n';
  //msgs += 'debug, Not submitting\n';
  if (msgs)
  {
    alert(msgs);
    return false;
  }
  else
  {
    return true;
  }
    
}

function checkGCAmount() {
//alert("checkGCAmount");
  var el = arguments[0];
  var msgs = validateGCAmount(el.value);
  if(msgs)
  {
    alert(msgs);
    return false;
  }
  else
  {
    updateValues();
    return true;
  }
}

function validateGCAmount() {
 //alert("validateGCAmount");
  var amt = arguments[0];
  var recipient = arguments[1];
  var namt = 1 * amt;
  var msgs = '';

  if(!amt.match(/^[\d\.]+$/))
  {
    msgs += 'Please enter the dollar amount of the Gift Certificate.\n';
    msgs += 'The value must be a whole dollar amount between $5 and $1000.\n';
  }
  else if(namt < 5 || namt > 1000) 
  {
    msgs += 'The value of the Gift Certificate must be a whole dollar amount between $5 and $1000.\n';
    msgs += 'Please enter the whole dollar amount you would like.\n';
  }
  else
  {
  }
  return msgs;
}

//updates price values when the user changes giftbox options or quantity of the items. 
function updateValues() {
  //alert("updateValues");
  var GIFTPRICE = 4.95;

  var basketForm = document.BasketForm;
  var subtotal, endtotal = 0.00;
  var n;

  var debug='';
  
  var partition_count = parseInt(basketForm.elements["partition_count"].value); //number of partitions
  //alert("partition_count = " + partition_count);
  for(var p = 0; p < partition_count; p++)
  {
    endtotal = 0.00;
    var item_count = parseInt(basketForm.elements["item_count_" + p].value);
    var hasGiftBoxablesField = basketForm.elements["has_gift_boxables_" + p];
    var hasGiftBoxablesValue = hasGiftBoxablesField.value;
    if (hasGiftBoxablesValue == "true") 
    {
      var gbtfield = basketForm.elements["gift_box_together_" + p];
      var gbtvalue = gbtfield.checked?true:false;
      //debug += "\nfor partition "+p+" gift-box-together = "+gbtvalue+"\n";
      var look_for_gift_box = true;
      //var found_gift_box = false;
      if(gbtvalue) {
        look_for_gift_box = false;
        basketForm.elements["gift_box_together_charge_" + p].value = GIFTPRICE;
        endtotal += GIFTPRICE;
      }else{
        look_for_gift_box = true;
        basketForm.elements["gift_box_together_charge_" + p].value = '';
      }
    }
    else
    {
      look_for_gift_box = false;
    }
      
    //alert("item_count = " + item_count);
    if(document.subtotalForm)
    {
      document.subtotalForm.elements["gift_box_together_" + p].value = gbtvalue;
      /*
      var gpfield = basketForm.elements["gift_pix_" + p];
      var gpvalue = gpfield.checked?true:false;
      document.subtotalForm.elements["gift_pix_" + p].value = gpvalue;
      */
    }
    for(var i = 0; i < item_count; i++)
    {
      var fieldId = p + "_" + i;
      if (basketForm.elements["is_gift_certificate_item_" + fieldId].value != "true")
      {
        //look_for_gift_box = false;
        //found_gift_box = false;
        var qfield = basketForm.elements["quantity_" + fieldId];
        var quantity = qfield.options[qfield.selectedIndex].value;
        var price = basketForm.elements["price_" + fieldId].value;
        var giftBoxable = basketForm.elements["gift_boxable_" + fieldId].value=="yes"?true:false;
        var giftBox;


        subtotal = (quantity * price) + 0.0;
        //debug += "  giftBoxable = "+giftBoxable+"\n";

        //debug += "  SUBTOTAL = "+subtotal+"\n  for sku #"+i+" giftBoxable = "+giftBoxable;

        if(giftBoxable && look_for_gift_box)
          if(basketForm.elements["gift_box_" + fieldId].checked)
            subtotal += (quantity * GIFTPRICE) - 0.001;
          
      
          giftBox = basketForm.elements["gift_box_" + fieldId].checked;
  /*
          giftBox = basketForm.elements["gift_box_" + fieldId].checked;
          if(look_for_gift_box){
            subtotal += GIFTPRICE + 0.0;
            //debug += "...found gift box!\n  SUBTOTAL = "+subtotal;
            look_for_gift_box = false;
            found_gift_box = true;
  */
  /*
          }else if(!found_gift_box) {
            subtotal += (quantity * GIFTPRICE) + 0.0;
            debug += "...not all together\n  SUBTOTAL = "+s;
          }
  */

        n = qfield.name.substring(8,qfield.name.length);
        //if(giftBoxable && !gbtvalue && giftBox){ subtotal += (quantity * GIFTPRICE) + 0.0;
            //debug += "...not all together\n  SUBTOTAL = "+subtotal;
        //}
        s = makeDollar(subtotal);
        basketForm.elements["linetotal_" + fieldId].value = s; 
        endtotal += subtotal;
        if(document.subtotalForm){
          document.subtotalForm.elements["quantity_" + fieldId].value = quantity;
          document.subtotalForm.elements["gift_box_" + fieldId].value = giftBox;
          debug += "subtotalForm.quantity_" + fieldId + " = " + document.subtotalForm.elements["quantity_" + fieldId].value + "\nsubtotalForm.gift_box_" + fieldId + " = " + document.subtotalForm.elements["gift_box_" + fieldId].value + "\nsubtotalForm.gift_box_together_" + p + " = " + document.subtotalForm.elements["gift_box_together_" + p].value + "\n\n";
        }
      }
      else
      {
        subtotal = (1 * basketForm.elements["GCamount_" + fieldId].value);
        s = makeDollar(subtotal);
        basketForm.elements["linetotal_" + fieldId].value = s;
        endtotal += subtotal;
        if(document.subtotalForm){
          document.subtotalForm.elements["amount_" + fieldId].value = subtotal;
        }
      }
    }
    e = makeDollar(endtotal);
    basketForm.elements["subtotal_" +p].value = e;
  }
//alert(debug);

  e = makeDollar(endtotal);
  if(document.subtotalForm && document.subtotalForm.total)
    document.subtotalForm.total.value = e;
  else if(basketForm.total)
    basketForm.total.value = e;
}

/*
    Function to format a number as a dollar amount, ie with 2 decimal places.  
    If no decimal place, add decimal and 2 zeros.  If a decimal, but only one
    number ot the right of decimal, add a zero.
*/

function makeDollar(amount)
{
     // correct rounding error on small decimal places
    amount = amount * 100;      
    amount = Math.round(amount);
    amount = amount/100;    

    n = amount.toString().length - amount.toString().indexOf(".");
    if (amount.toString().indexOf(".") == -1)
    {
        amount = amount + ".00";
    }
    else if (n == 2)
    {
        amount = amount + "0";
    } 
    return amount; 
}

function remove(recipient, sku) {
  document.BasketForm.removeSku.value = sku;
  document.BasketForm.removeRecipient.value = recipient;
}

function checkBasket() {
  if (document.BasketForm.total.value == 0) {
     alert("You have nothing in your basket");
     return false;
  }
}
function popGiftBox() {
  window.open("AdvancedGiftBoxing1.jsp","AdvancedGiftBox","height=400,width=400,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no");
}

function moveToWishList(recipient, sku, quantity)
{
  document.BasketForm.action.value = 'move';
  document.BasketForm.moveToWishListSku.value = sku;
  document.BasketForm.moveToWishListQuantity.value = quantity;
  document.BasketForm.moveToWishListRecipient.value = recipient;
  document.BasketForm.submit();
}

//when the user clicks update button on the checkout page
function update()
{
  document.subtotalForm.action.value = 'update';
  document.subtotalForm.submit();
}
//-->
