//  Add item (idToAdd) to Shopping Cart

function addToCart(idToAdd) {

  document.cookie = idToAdd + "=" + idToAdd + "; path=/";
  document.cookie = "qty" + idToAdd + "=1; path=/";


}


function addToCartWithQty(idToAdd, qtyToAdd) {

  document.cookie = idToAdd + "=" + idToAdd + "; path=/";
  document.cookie = "qty" + idToAdd + "=" + qtyToAdd + "; path=/";

}


//  Utility function for display contents of cookie

function showCookie() {

  document.writeln("<html><body><pre> " + document.cookie + "</pre></body</html>");

}




function clearCart() {

  var expire     = new Date();
  var inCookie   = document.cookie;
  var outCookie  = "";
  var eqIndex;
  var semiIndex;
  var cName;
  var cValue;

  expire.setTime (expire.getTime() - (100 * 24 * 60 * 60 * 1000));

  eqIndex  = inCookie.indexOf("=");
  while (eqIndex != -1) {
    cName     = inCookie.substr(0, eqIndex); 

    semiIndex = inCookie.indexOf(";");
    if (semiIndex != -1) {
      cValue    = inCookie.substring((eqIndex + 1), semiIndex);
      inCookie  = inCookie.substr((semiIndex + 2));
    }
    else {
      cValue    = inCookie.substring((eqIndex + 1));
      inCookie  = "";
    }

    outCookie = cName + "=" + cValue + "; path=/; expires=" + expire.toGMTString();

    document.cookie = outCookie
 
    eqIndex  = inCookie.indexOf("=");
  }

}


//  Re-calculate extened price, subtotal, shipping, and total
//    -  quant is a field
//    -  price is a field
//    -  ext is a field

function reCalc(quant, price, ext) {

  var shipping = 0;
  var total = 0;

  var subTotal = new String(parseFloat(document.checkout.subTotal.value) - parseFloat(ext.value));
  var extPrice = new String(parseFloat(quant.value) * parseFloat(price.value));

  extPrice = decimalFix(extPrice);
 
  subTotal = parseFloat(subTotal) + parseFloat(extPrice);
  subTotal = decimalFix(subTotal);

  shipping = calcShipping(subTotal);
  shipping = decimalFix(shipping)
  total    = parseFloat(shipping) + parseFloat(subTotal);
  
  shipping = decimalFix(shipping);
  total    = decimalFix(total);
  
  ext.value = extPrice;
  document.checkout.subTotal.value = subTotal;
  document.checkout.shipping.value = shipping;
  document.checkout.total.value    = total;

}


//  Calculate shipping cost

function calcShipping(subTotal) {

  subTotal = parseFloat(subTotal);

  if (subTotal <= 0) {
    return 0;
  }
  else if (subTotal <= 8) {
    return 3.95;
  }
  else if (subTotal <= 15) {
    return 5.95;
  }
  else if (subTotal <= 30) {
    return 7.95;
  }
  else if (subTotal <= 60) {
    return 8.95;
  }
  else if (subTotal <= 100) {
    return 9.95;
  }
  else {
    return subTotal *.09;
  }
  
}


//  Convert data to number with 2 decimal places

function decimalFix(theData) {

  var decOut;
  var hasDecimal;
  var decDigits;


  decOut = new String(theData);
  hasDecimal = false;
  decDigits = 0;

  //  Create a number rounded to 100th place
  decOut = parseInt(Math.floor(theData * 100 + .5)) / 100;

  decOut = new String(decOut);

  for (x = 0; x < decOut.length; x++) {
    if (decOut.charAt(x) == ".") {
      hasDecimal = true;
    }
    else {
      if (hasDecimal == true) {
        decDigits += 1;
      }
    }
  }

  if (hasDecimal == false) {
    decOut += ".00";  
  }
  else {
    if (decDigits == 1) {
      decOut += "0";
    }
    else if (decDigits == 0) {
      decOut += "00";
    }
  }

  return decOut;

}


