//================================
// NOP Design JavaScript Shopping Cart
// - functions for adding items to cart from product web pages.
//================================

//--------------------------------
// PARAMETERS:  Quantity to, object.
// RETURNS:     Quantity as a number, and possible alert
// PURPOSE:     Make sure quantity is represented as a number
//--------------------------------

function CKquantity(checkString, qtyObj) {
   if (isNaN(checkString)) {
      alert(strErrQty);
      checkString = 1;
   }
   else {
      var testQuantity = Math.floor(checkString);
      if ((checkString != testQuantity) && (checkString != 0)) {
         alert("Only whole number quantities may be entered.");
         if (testQuantity == 0) testQuantity = 1;
         checkString = testQuantity;
      }
      else if (testQuantity <= 0) {
         alert(strErrQty);
         checkString = 1;
      }
   }
   return(checkString);
}

//--------------------------------
// PARAMETERS:  Form Object
// RETURNS:     Cookie to user's browser, with prompt
// PURPOSE:     Adds a product to the user's shopping cart
//--------------------------------

var cartChanged; // Global var

function AddToCart(thisForm) {
   var iOrderLines = 0;
   var bAlreadyInCart = false;
   var notice = "";
   var cartFull = false;
   var testCookieStr = '';
   var oldcartChanged = cartChanged;
   cartChanged = false;

   iOrderLines = GetCookie("OrdLines");
   if (iOrderLines == null) iOrderLines = 0;

   if (thisForm.ID_NUM == null) strID_NUM = "";
   else strID_NUM = thisForm.ID_NUM.value;

   if (thisForm.QUANTITY == null) strQUANTITY = "1";
   else strQUANTITY  = thisForm.QUANTITY.value;

   if (thisForm.PRICE == null) strPRICE = "0.00";
   else strPRICE = thisForm.PRICE.value;

   if (thisForm.NAME == null) strNAME = "";
   else strNAME = thisForm.NAME.value;

   if (thisForm.SHIPPING == null) strSHIPPING = "0.00";
   else strSHIPPING = thisForm.SHIPPING.value;

   if (thisForm.WEIGHT == null) strWEIGHT = "0.00";
   else strWEIGHT = thisForm.WEIGHT.value;

   if (thisForm.POSTPKG == null) strPOSTPKG = "PPP500-EPI2000"; // <AUS pkg>-<OS pkg>
   else strPOSTPKG = thisForm.POSTPKG.value;

   if (thisForm.PRICEINCTAX == null) strPRICEINCTAX = "0.00";
   else strPRICEINCTAX = thisForm.PRICEINCTAX.value;

   if (thisForm.EXTRAINFO == null) strEXTRAINFO = "";
   else strEXTRAINFO = thisForm.EXTRAINFO[thisForm.EXTRAINFO.selectedIndex].value;
   if (thisForm.EXTRAINFO2 != null) strEXTRAINFO += "; " + thisForm.EXTRAINFO2[thisForm.EXTRAINFO2.selectedIndex].value;
   if (thisForm.EXTRAINFO3 != null) strEXTRAINFO += "; " + thisForm.EXTRAINFO3[thisForm.EXTRAINFO3.selectedIndex].value;
   if (thisForm.EXTRAINFO4 != null) strEXTRAINFO += "; " + thisForm.EXTRAINFO4[thisForm.EXTRAINFO4.selectedIndex].value;

   for (i=1; i<=iOrderLines; i++) {
      NewOrder = "OrdLn-" + i;
      getDatabaseFields(NewOrder);

      //Is this product already in the cart?  If so, increment quantity instead of adding another.

      if ((fields[0] == strID_NUM) && (fields[2] == strPRICE) && (fields[3] == strNAME) && (fields[8] == strEXTRAINFO)) {
         bAlreadyInCart = true;
         dbUpdatedOrder = strID_NUM + "|" + (parseInt(strQUANTITY)+parseInt(fields[1])) + "|" + strPRICE + "|" + strNAME + "|" + strSHIPPING + "|" + strWEIGHT + "|" + strPOSTPKG + "|" + strPRICEINCTAX + "|" + strEXTRAINFO;
         strNewOrder = "OrdLn-" + i;
         DeleteCookie(strNewOrder, "/");
         SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
         notice = strQUANTITY + " " + strNAME + strAdded;

         cartChanged = true;
         break;
      }
   }

   if (!bAlreadyInCart) {
      iOrderLines++;

      if (iOrderLines > MaxOrderLines) cartFull = true;
      else {
         dbUpdatedOrder = strID_NUM + "|" + strQUANTITY + "|" + strPRICE + "|" + strNAME + "|" + strSHIPPING + "|" + strWEIGHT + "|" + strPOSTPKG + "|" + strPRICEINCTAX + "|"  + strEXTRAINFO;
         strNewOrder = "OrdLn-" + iOrderLines;

         testCookieStr = strNewOrder + "=" + escape(dbUpdatedOrder) + "; ";
         if (iOrderLines == 1) testCookieStr += "OrdLines" + "=" + escape(iOrderLines);
         else if (iOrderLines == 10) testCookieStr += ";  "; // Add an extra char for extra digit in orderlines.
         if ((document.cookie.length + testCookieStr.length) > maxChars) cartFull = true;
         else {
            SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
            SetCookie("OrdLines", iOrderLines, null, "/");
            notice = strQUANTITY + " " + strNAME + strAdded;

            cartChanged = true;
         }
      }
   }
   if (cartFull) {
      if (oldcartChanged) {
         cartChanged = true; // Otherwise it won't refresh View Cart.
         oldcartChanged = false;
      }
      alert(strSorry); // Full cart.
   }
   else if ((DisplayNotice) && (notice != '')) alert(notice);
}
