/*
  20.08.2006 SK:
    Some fixed variables to prevent typos.
*/
var strCartName = 'cart';
var chrItemSplitter = ',';
var chrValueSplitter = ':';

/**
 * @description Parses the item to be updated in cart.
 *
 * @author Sami Keskinen
 * @since 27.08.2006
 *
 * @return Nothing.
 */
function addToCartButton(objItem) {
  var strProductID = objItem.form.product_number.value;
  switch (strProductID) {
    case '':
    case ' ':
    case null:
      return;
  }

  var intAmount = objItem.form.amount.value;
  objItem.form.amount.value = "1";

  addToCart(strProductID, intAmount);

  updateVisibeCart();
}

/**
 * @description Adds / updates an item in cart.
 *
 * @author Sami Keskinen
 * @since 20.08.2006
 *
 * @return Nothing.
 */
function addToCart(strID, intAmount) {
  /*
    20.08.2006 SK:
      Get the given unit amount.

    22.08.2006 SK:
      Check and replace commas with dots.
      Also check that the amount of items is positive and above zero.
  */
  // var objItem = document.forms['product' + strID];
  // var strAmount = objItem.amount.value.toString();
  var strAmount = intAmount.toString();
  var intAmount = Number(strAmount.replace(',', '.'));

  if (isNaN(intAmount)) {
    intAmount = 1;
  }
  if (intAmount <= 0) {
    intAmount = 1;
  }

  /*
    20.08.2006 SK:
      Check if there's old cart available.
  */
  var strCart = '';
  if (isCookieSet(strCartName)) {
    strCart = getCookie(strCartName);
  }

  /*
    20.08.2006 SK:
      Split old cart to items.
  */
  var lstCart = new Array();
  if (strCart.length > 0) {
    lstCart = strCart.split(chrItemSplitter);
  }

  /*
    20.08.2006 SK:
      Increase item count if already in cart.
  */
  var strNewCart = '';
  var boolItemFound = false;
  var intItems = 0;
  if (lstCart.length > 0) {
    for (var iItem = 0; iItem < lstCart.length; iItem++) {
      var lstItem = lstCart[iItem].split(chrValueSplitter);

      /*
        22.08.2006 SK:
          Skip empty items.
      */
      if (lstItem[0] == '') {
        continue;
      }
      if (lstItem.length != 2) {
        continue;
      }

      /*
        22.08.2006 SK:
          Check if there are items and add an item separator.
      */
      if (intItems > 0) {
        strNewCart += chrItemSplitter;
      }
      intItems++;

      /*
        22.08.2006 SK:
          Just add other non-matching items.
      */
      if (lstItem[0] != strID) {
        strNewCart += lstCart[iItem];
        continue;
      }

      var intNewAmount = Number(lstItem[1]) + intAmount;

      /*
        27.08.2006 SK:
          Check the sum, just in case.
      */
      if (isNaN(intNewAmount)) {
        intNewAmount = 1;
      }
      if (intNewAmount <= 0) {
        intNewAmount = 1;
      }

      strNewCart += lstItem[0] + chrValueSplitter + intNewAmount;
      boolItemFound = true;
    }
  }

  /*
    20.08.2006 SK:
      Add item to cart, if not in it.
  */
  if (!(boolItemFound)) {
    if (strCart.length > 0) {
      strNewCart += chrItemSplitter;
    }

    strNewCart += strID + chrValueSplitter + intAmount;
  }

  /*
    20.08.2006 SK:
      Store the updated cart.
  */
  setCookie(strCartName, strNewCart);
}

/**
 * @description Discards the cart.
 *
 * @author Sami Keskinen
 * @since 20.08.2006
 *
 * @return Nothing.
 */
function discardCart() {
  deleteCookie(strCartName);
}

/**
 * @description Counts the items in cart.
 *
 * @author Sami Keskinen
 * @since 20.08.2006
 *
 * @return An integer, the number of different kind of items in cart.
 */
function countItemsInCart() {
  /*
    20.08.2006 SK:
      Check if there's no cart.
  */
  if (!(isCookieSet(strCartName))) {
    return 0;
  }

  /*
    20.08.2006 SK:
      Count and return the number of items in cart.
  */
  var strCartCookie = getCookie(strCartName);
  return (strCartCookie.split(chrItemSplitter).length);
}

/**
 * @description Counts the total amount of units in cart.
 *
 * @author Sami Keskinen
 * @since 20.08.2006
 *
 * @return An integer, the total number of units over all items in cart.
 */
function countItemUnitsInCart() {
  /*
    20.08.2006 SK:
      Check if there's no cart.
  */
  if (!(isCookieSet(strCartName))) {
    return 0;
  }

  /*
    20.08.2006 SK:
      Count and return the total number of item units in cart.
  */
  var strCartCookie = getCookie(strCartName);

  lstCart = strCartCookie.split(chrItemSplitter);
  if (lstCart.length < 1) {
    return (0);
  }

  var intAmount = 0;
  for (var iItem = 0; iItem < lstCart.length; iItem++) {
    var lstItem = lstCart[iItem].split(chrValueSplitter);

    /*
      22.08.2006 SK:
        Skip empty items.
    */
    if (lstItem[0] == '') {
      continue;
    }
    if (lstItem.length != 2) {
      continue;
    }

    intAmount += Number(lstItem[1]);
  }

  return (intAmount);
}

/**
 * @description Changes an item in the cart.
 *
 * @author Sami Keskinen
 * @since 21.08.2006
 *
 * @param productID The productID of the item to be changed.
 */
function inCartChange(productID) {
  /*
    21.08.2006 SK:
      Read cart from a cookie.
  */
  var strCart = getCookie(strCartName);

  /*
    21.08.2006 SK:
      Return if there's no cart.
  */
  if (strCart.length < 1) {
    return;
  }

  /*
    21.08.2006 SK:
      Split the cart to items to ease modify.
  */
  var lstCart = strCart.split(chrItemSplitter);

  var strNewCart = '';
  if (lstCart.length > 0) {
    for (var iItem = 0; iItem < lstCart.length; iItem++) {
      var lstItem = lstCart[iItem].split(chrValueSplitter);

      /*
        22.08.2006 SK:
          Skip empty items.
      */
      if (lstItem[0] == '') {
        continue;
      }
      if (lstItem.length != 2) {
        continue;
      }

      if (lstItem[0] == productID) {
        /*
          22.08.2006 SK:
            Convert all commas in input to decimal dots.
        */
        var strProduct = 'product' + productID;
        var objItem = document.forms['order-cart-form'][strProduct];
        var strNewAmount = objItem.value.toString();
        var intNewAmount = Number(strNewAmount.replace(',', '.'));

        /*
          22.08.2006 SK:
            Convert smaller or equal to zero amounts to one.
        */
        if (isNaN(intNewAmount)) {
          intNewAmount = 1;
        }

        if (intNewAmount <= 0) {
          intNewAmount = 1;
        }

        strNewCart += lstItem[0] + chrValueSplitter + intNewAmount;
      }
      else {
        strNewCart += lstCart[iItem];
      }

      if (iItem < (lstCart.length - 1)) {
        strNewCart += chrItemSplitter;
      }
    }
  }

  setCookie(strCartName, strNewCart);
  document.forms['order-cart-form'].submit();
}

/**
 * @description Deletes an item from the cart.
 *
 * @author Sami Keskinen
 * @since 21.08.2006
 *
 * @param productID The productID of the item to be removed from the cart.
 */
function inCartRemove(productID) {
  /*
    21.08.2006 SK:
      Read cart from a cookie.
  */
  var strCart = getCookie(strCartName);

  /*
    21.08.2006 SK:
      Return if there's no cart.
  */
  if (strCart.length < 1) {
    return;
  }

  /*
    21.08.2006 SK:
      Split the cart to items to ease modify.
  */
  var lstCart = strCart.split(chrItemSplitter);

  var strNewCart = '';
  var intItems = 0;
  if (lstCart.length > 0) {
    for (var iItem = 0; iItem < lstCart.length; iItem++) {
      var lstItem = lstCart[iItem].split(chrValueSplitter);

      /*
        22.08.2006 SK:
          Skip empty items.
      */
      if (lstItem[0] == '') {
        continue;
      }
      if (lstItem.length != 2) {
        continue;
      }

      if (lstItem[0] != productID) {
        if (intItems > 0) {
          strNewCart += chrItemSplitter;
        }

        strNewCart += lstCart[iItem];
        intItems++;
      }
    }
  }

  setCookie(strCartName, strNewCart);
  document.forms['order-cart-form'].submit();
}

/**
 * @description Queries and discards the cart.
 *
 * @author Sami Keskinen
 * @since 22.08.2006
 *
 * @return Nothing.
 */
function inCartDiscard() {
  /*
    22.08.2006 SK:
      Check if there's no cart or the cart is empty.
  */
  if (!(isCookieSet(strCartName))) {
    return;
  }
  if (getCookie(strCartName) == '') {
    return;
  }

  if (confirm('Haluatko varmasti tyhjentää ostoskorin?')) {
    deleteCookie(strCartName);
    document.forms['order-cart-form'].submit();
  }
}

/**
 * @description Updates the item count in the cart.
 *
 * @author Sami Keskinen
 * @since 27.08.2006
 *
 * @return Nothing.
 */
function updateVisibeCart() {
  /*
    27.08.2006 SK:
      Updae the number of distinct items in cart.
  */
  var intProducts = 0;
  var intUnits = 0;

  var objItem = document.getElementById('incart-item-count');
  if (!((typeof objItem) == 'object')) {
    return;
  }

  intProducts = countItemsInCart();
  intUnits = countItemUnitsInCart();

  /*
    27.08.2006 SK:
      Update the number of units in cart.
  */
  if (intProducts < 1) {
    objItem.innerHTML = 'Korissa ei ole tuotteita.';
  }
  else {
    var strCartText = 'Korissa on ' + intProducts;

    if (intProducts == 1) {
      strCartText += ' tuote, ';
    }
    else {
      strCartText+= ' erilaista tuotetta, ';
    }
    strCartText += 'yhteensä ' + intUnits + ' yksikkö';
    
    if (intUnits > 1) {
      strCartText += 'ä';
    }
    strCartText += '.';

    objItem.innerHTML = strCartText;
  }
}
