
var EKCART_CONSTANTS = { item_pad_digits:1, default_currency:'USD', default_taxable:true, dlm:'|', debug_div:'cart_contents' };
/*

Documentation on above constants:

EKCART_CONSTANTS.item_pad_digits:
- All cart items are prefixed with a number and an underscore, which represents the "line number" in the cart.
- This constant defines how many digits that line number is, and therefore, defines the maximum number of items.
- I have set it to '2' to restrict the cart to 99 items to balance performance.

EKCART_CONSTANTS.dlm:
- Delimiter character used to sequentialize cookie data array.

EKCART_CONSTANTS.default_currency:
- The cart is designed to handle multiple currencies, though the actual calculations will be done in
  the associated PHP scripts.
- This simple sets the default currency when adding an item to the cart.
- This will need some more development to be fully functional.

EKCART_CONSTANTS.default_*:
- The other default values are simply temporary placeholders when adding a new item to the cart.

*/

function EKCartObject() {

  this.items = new Array();
  this.last_item = 0;
  this.set_cart_item = set_cart_item;
  this.get_cookie_items = get_cookie_items;
  this.get_line_num = get_line_num;
  this.update_cookie_item = update_cookie_item;

  function set_cart_item(itm_id,itm_qty,unit_price,itm_desc,qty_mode) {
    EKUtil.Debug(EKCART_CONSTANTS.debug_div,'Adding Item',itm_id);
    var qty_mult = 0;
    if ( qty_mode == null ) { qty_mult = 1; }
    else if ( qty_mode == "ADD" ) { qty_mult = 1; }
    else if ( qty_mode == "DEL" ) { qty_mult = -1; }
    else if ( qty_mode == "UPDATE" ) { qty_mult = 0; }
    if ( ( itm_desc == null ) || ( itm_desc == "" ) ) { itm_desc = "(desc not set)"; }
    if ( ( ( itm_id == null ) || ( itm_id == "" ) ) || ( parseFloat(itm_qty) == 0 ) ) { return; }
    var line_num = this.get_line_num(itm_id,unit_price);
    var line_qty = parseFloat( ( (this.items[line_num]==null) || (this.items[line_num].quantity==null) ? 0 : this.items[line_num].quantity ) );
    this.items[line_num] = new Array();
    this.items[line_num].id = itm_id;
    this.items[line_num].desc = itm_desc;
    if ( ! this.items[line_num].quantity ) { this.items[line_num].quantity = 0; }
    this.items[line_num].quantity = ( qty_mult == 0 ? parseFloat(itm_qty) : ( ( parseFloat(itm_qty) * qty_mult ) + line_qty ) );
    if ( this.items[line_num].quantity < 0 ) { this.items[line_num].quantity = 0; }
    this.items[line_num].unit_price = parseFloat(unit_price);
    this.items[line_num].currency = EKCART_CONSTANTS.default_currency;
    this.items[line_num].taxable = EKCART_CONSTANTS.default_taxable;
    this.update_cookie_item(line_num);
  }
  
  function get_line_num(itm_id,unit_price) {
    var line_num = this.items.length;
    var line_qty = 0;
    for( i=0; i<=this.items.length; i++ ) {
      if ( this.items[i] ) {
        //EKUtil.Debug(EKCART_CONSTANTS.debug_div,"ITEM COMPARE: '" + this.items[i].id + "' TO '" + itm_id + "' IS :'" + ( ( this.items[i].id == itm_id ) ? 'TRUE' : 'FALSE' ) + "'\n" + "PRICE COMPARE: '" +this.items[i].unit_price + "' TO '" + parseFloat(unit_price) + "' IS :'" + ( ( this.items[i].unit_price == parseFloat(unit_price) ) ? 'TRUE' : 'FALSE' ) + "'\n","");
        if ( ( this.items[i].id == itm_id ) && ( this.items[i].unit_price == parseFloat(unit_price) ) ) {
          //EKUtil.Debug(EKCART_CONSTANTS.debug_div,'FOUND ITEM ' + itm_id + ' on line ',i);
          line_num = i;
          break;
        }
      }
      else {
        //EKUtil.Debug(EKCART_CONSTANTS.debug_div,'get_line_num() BREAK ON LINE',i);
        break;
      } // No need to loop if no more items
    }
    return line_num;
  }
  
  function update_cookie_item(line_num) {
    var item_cookie = "";
    // The field order here must match get_cookie_items() function
    item_cookie += this.items[line_num].id;
    item_cookie += EKCART_CONSTANTS.dlm;
    item_cookie += this.items[line_num].desc;
    item_cookie += EKCART_CONSTANTS.dlm;
    item_cookie += this.items[line_num].quantity;
    item_cookie += EKCART_CONSTANTS.dlm;
    item_cookie += this.items[line_num].unit_price;
    item_cookie += EKCART_CONSTANTS.dlm;
    item_cookie += this.items[line_num].currency;
    item_cookie += EKCART_CONSTANTS.dlm;
    item_cookie += this.items[line_num].taxable;
    var itm_pfx = EKUtil.zeropad(line_num,EKCART_CONSTANTS.item_pad_digits).toString();
    var cart_line_id = 'CART_LINE_' + itm_pfx;
    //EKUtil.Debug(EKCART_CONSTANTS.debug_div,"CART_LINE_ID",cart_line_id);
    EKCookie.Set(cart_line_id,item_cookie);
  }

  function get_cookie_items(reduce_line_num) {
    var tmp_items = new Array();
    var max_items = parseInt(EKUtil.str_repeat(9,EKCART_CONSTANTS.item_pad_digits));
    //EKUtil.Debug(EKCART_CONSTANTS.debug_div,"GETTING MAX_ITEMS",max_items);
    this.items = new Array(); // Clear out
    c = 0;
    for ( i=0; i<max_items; i++ ) {
      var itm_pfx = EKUtil.zeropad(i,EKCART_CONSTANTS.item_pad_digits).toString();
      var cart_line_id = 'CART_LINE_' + itm_pfx;
      var tmp_val = EKCookie.Get(cart_line_id);
      if ( tmp_val == "" ) { break; }
      var tmp_itm = tmp_val.split(EKCART_CONSTANTS.dlm);
      // The field order here must match add_cart_item() function
      var fld = 0; // Did this so that fields can be easily rearranged without having to recode the array id.
      tmp_items[i] = new Array();
      tmp_items[i].id = tmp_itm[(fld++)];
      tmp_items[i].desc = tmp_itm[(fld++)];
      tmp_items[i].quantity = tmp_itm[(fld++)];
      tmp_items[i].unit_price = tmp_itm[(fld++)];
      tmp_items[i].currency = tmp_itm[(fld++)];
      tmp_items[i].taxable = tmp_itm[(fld++)];
      // Remove items that have no quantity
      EKCookie.Delete(cart_line_id);
      if ( parseFloat(tmp_items[i].quantity) > 0 ) { this.items[c] = tmp_items[i]; c++; }
    }
    // *************************************************
    // UPDATE COOKIES TO REMOVE ZERO QUANTITY ITEMS
    // *************************************************
    for( i=0; i<this.items.length; i++ ) {
      this.update_cookie_item(i);
      tmp_items[i] = this.items[i];
      EKUtil.Debug(EKCART_CONSTANTS.debug_div,'',"CART_LINE_ID (" + i + "): " + tmp_items[i].id + "\nDESC:'" +tmp_items[i].desc + "'" + "\nQTY:'" +tmp_items[i].quantity + "'" + "\nPRICE:'" +tmp_items[i].unit_price + "'" + "\nCUR:'" +tmp_items[i].currency + "'" + "\nTAX:'" +tmp_items[i].taxable + "'" );
    }
    // *************************************************
  }

}

var EKCART = new EKCartObject();
EKCART.get_cookie_items();