//-----------------------------------------------------------------
// Standardised Cookie based Shopping Cart
// ©2009 Dave Probert
// Modifications are needed in the dcart_display function to show cart items that use specific 'data' chunks
//-----------------------------------------------------------------
var the_dcart = [];
var curr_dcart_idx = 0;

//-----------------------------------------------------------------
function dcart_init()
{
var c = 0;
var tc = $.cookie.list('^dcart',true);
  if(tc)
  {
    for(var v in tc)
      the_dcart[c++] = tc[v];
    curr_dcart_idx = c-1;
  }
}

//-----------------------------------------------------------------
function dcart_add(typ, id, name, amt, pr, data)
{
var idx =	dcart_get_idx(id, typ);
	if(idx >= 0)
		the_dcart[idx].a = amt;		// set the amount assuming all else is the same
	else
	  the_dcart[ ++curr_dcart_idx ] = {t:typ, i:id, n:name, a:amt, p:pr, d:data} ;
  dcart_to_store();
  return curr_dcart_idx;
}

//-----------------------------------------------------------------
function dcart_get_idx(id)
{
var typ = arguments[1] || null;
  for(var i in the_dcart)
  {
    if(the_dcart[i])
    {
      if(the_dcart[i].i == id)
      {
      	if(typ && the_dcart[i].t != typ)
      		continue;
     		return i;
      }
    }
  }
  return -1;
}

//-----------------------------------------------------------------
function dcart_remove(id)
{
var idx = dcart_get_idx(id);
	if(idx >= 0)
	{
		the_dcart[idx].t = null;
		the_dcart[idx] = null;
    the_dcart.splice(idx,1);
		$.cookie.clear('dcart'+idx);
		dcart_to_store();
	}
}

//-----------------------------------------------------------------
function dcart_get_total()
{
var _tot = 0;
var typ = arguments[0] || null;
	for(var i in the_dcart)
  {
    if(the_dcart[i])
    {
     	if(typ && the_dcart[i].t != typ)
     		continue;
    	_tot += (the_dcart[i].a *  the_dcart[i].p);
    }
  }
	return _tot;
}

//-----------------------------------------------------------------
function dcart_count_type(typ)
{
var c = 0;
  for(var i in the_dcart)
  {
    if(the_dcart[i])
    {
      if(the_dcart[i].t == typ)
      	c++;
		}
	}
	return c;
}

//-----------------------------------------------------------------
function dcart_to_store()
{
  for(var i in the_dcart)
  {
    if(the_dcart[i] && the_dcart[i].t && the_dcart[i].t != '')
      $.cookie('dcart'+i, the_dcart[i].toSource() );
  }
}

//-----------------------------------------------------------------
function dcart_display(ele)
{
var x = '';
  for(var i in the_dcart)
  {
    if(the_dcart[i])
    {
    var c = the_dcart[i];
    var cd = c.d;
      // make cart elem
      switch(c.t)
      {
				case 'ch':
					x += "<div id='cart_"+c.i+"' class='cart_item'>";
					x += "<table cellspacing='0' cellpadding='1' class='cart_element'>";
					x += "<tr><td colspan='2' class='cart_head'>"+cd.ddcnt+"</td></tr>";
					x += "<tr><td class='cart_c1'>Price:</td>";
					x += "<td class='cart_c2'>"+cur_sym + format2(cd.iprice) + " each</td></tr>";
					x += "<tr><td class='cart_c1'>Style:</td>";
					x += "<td class='cart_c2'>"+tstyles[cd.istyle].name+"</td></tr>";
					x += "<tr><td class='cart_c1'>Colour:</td>";
					x += "<td class='cart_c2'>"+colours[cd.icol].name+"</td></tr>";
					x += "<tr><td class='cart_c1'>Size:</td>";
					x += "<td class='cart_c2'>"+sizes[cd.isiz].name+"</td></tr>";
					x += "<tr><td class='cart_c1'>Qty:</td>";
					x += "<td class='cart_c2' id='num__"+c.i+"'>"+c.a+"</td></tr>";
					x += "<tr><td class='cart_c1'>Cost:</td><td class='cart_c2'>";
					x += cur_sym + format2(c.a * cd.iprice);
					x += "</td></tr>";
					x += "<tr><td class='cart_c2' colspan='2'><input type='image' src='images/remove2.gif' value='Remove' onclick=\"remove_cart_item('"+c.i+"')\">";
					x += "</td></tr>";
					x += "</table>";
					x += "</div>";
					break;

				case 'co':
					x += "<div id='cart_"+c.i+"' class='cart_item'>";
					x += "<table cellspacing='0' cellpadding='1' class='cart_element'>";
					x += "<tr><td colspan='2' class='cart_head'>"+cd.ddcnt+"</td></tr>";
					x += "<tr><td class='cart_c1'>Price:</td>";
					x += "<td class='cart_c2'>"+cur_sym + format2(cd.iprice) + " each</td></tr>";
		  		if(colours[cd.icol].name != 'None')
		  		{
						x += "<tr><td class='cart_c1'>Colour:</td>";
						x += "<td class='cart_c2'>"+colours[cd.icol].name+"</td></tr>";
					}
					x += "<tr><td class='cart_c1'>Qty:</td>";
					x += "<td class='cart_c2' id='num__"+c.i+"'>"+c.a+"</td></tr>";
					x += "<tr><td class='cart_c1'>Cost:</td><td class='cart_c2'>";
					x += cur_sym + format2(c.a * cd.iprice);
					x += "</td></tr>";
					x += "<tr><td class='cart_c2' colspan='2'><input type='image' src='images/remove2.gif' value='Remove' onclick=\"remove_cart_item('"+c.i+"')\">";
					x += "</td></tr>";
					x += "</table>";
					x += "</div>";
					break;
      }
    }
  }
  x += '<hr>';
  $('#'+ele).html(x);
}

//-----------------------------------------------------------------
function dcart_iterate(typ, func)
{
	for(var i in the_dcart)
  {
    if(the_dcart[i])
    {
      if(the_dcart[i].t == typ)
      	func(i, the_dcart[i]);
    }
  }
}

//-----------------------------------------------------------------
