cartDivs= new Object();
cartDivs['CurrentCart']='current';
cartDivs['CartCheckout']='checkout-items';


function Add(type,name,price,code,options,quantity) {
  if(!code)     code=name;
  if(!options)  options=false;
  if(!quantity) quantity=1;
  var templates=Array();
  for(var id in cartDivs)
    if(ByID(id)) templates.push(id+','+cartDivs[id]);
  var cart=new jscart(CB_cart);
  cart.add(templates,type,name,price,code,options,quantity);
  cartCalled(code);
}

function Remove(key) {
  var templates=Array();
  for(var id in cartDivs)
    if(ByID(id)) templates.push(id+','+cartDivs[id]);
    
  var cart=new jscart(CB_cart);
  cart.remove(templates,key);
}

var CB_cart = {
  add: function(html_blocks) {
    insertHtmlBlocks(html_blocks);
  }
  ,
  remove: function(html_blocks) {
    insertHtmlBlocks(html_blocks);
  }
}

function insertHtmlBlocks(html_blocks) {
    for(id in html_blocks)
      ByID(id).innerHTML=html_blocks[id];
}

function ByID(id) {
  return (document.getElementById && document.getElementById(id)) ? document.getElementById(id) : false;
}


// Math tools --------------------------

function toPrice(srcPrice) {
	/* This function returns srcPrice with two forced decimal places. */
	srcPrice = srcPrice + "";
	if (srcPrice.indexOf(".") < 0) {
		srcPrice += ".00";
	} else if ((srcPrice.length - 2) == srcPrice.substr(srcPrice.indexOf(".")).length) {
		srcPrice += "0";
	}
	return(srcPrice);
}

function roundReal(srcReal, decPlaces) {
	/* This function returns srcReal rounded to decPlaces decimal places. */
	decPlaces = (!decPlaces ? 2 : decPlaces);
	return(Math.round(srcReal * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces));
}

function safeParseInt(x) {
  var cleanNumReg = new RegExp ('[^0-9\.]+', 'g') ;
  x=x.replace(cleanNumReg, '');
  if(x=='' || isNaN(x))
    return 0;
  return parseInt(x);
}

function safeParseFloat(x) {
  x=String(x);
  var cleanNumReg = new RegExp ('[^0-9\.]+', 'g') ;
  x=x.replace(cleanNumReg, '');
  if(x=='' || isNaN(x))
    return 0;
  return parseFloat(x);
}

// Site specific  ----------------------

function SelectedIndex(s) {
  if(typeof s != "object") {
    s=ByID(s);
    if (!s) return false;
  }
  return s.options[s.options.selectedIndex].value;
}


function cartCalled(code) {
  var button=ByID('cartAdd'+code);
  if(button) {
    if(button.href) {
      button.href="checkout";
      button.innerHTML='Checkout '+String.fromCharCode(0xBB); //(Unicode &raquo;)
    }
    else {
      button.value='Checkout '+String.fromCharCode(0xBB); //(Unicode &raquo;)
      button.onclick=function () {
        document.location=BASE_URL+'checkout';
      };
    }
    return;
  }
  
  var button=ByID('cartAddZoom'+code);
  button.value='Checkout '+String.fromCharCode(0xBB); //(Unicode &raquo;)
  button.onclick=function () {
    window.opener.document.location=BASE_URL+'checkout';
    window.close();
  };
}

function qtyChanged(key) {
  var delivery=SelectedIndex('options'+key);
  var price=deliveryPrices[delivery];
  var quantity=safeParseInt(ByID('qty'+key).value);
  ByID('subtotal'+key).innerHTML=toPrice(quantity*price);
  var cart=new jscart(CB_cart_update);
  cart.update(key,'quantity',quantity);
}

function deliveryChanged(key) {
  var delivery=SelectedIndex('options'+key);
  var price=deliveryPrices[delivery];
  var quantity=safeParseInt(ByID('qty'+key).value);
  //ByID('price'+key).innerHTML=price;
  //ByID('subtotal'+key).innerHTML=toPrice(quantity*price);
  var cart=new jscart(CB_cart_update);
  cart.update(key,'price',price,'option','delivery',delivery);
}

var CB_cart_update = {
  update: function(prices) {
    var key=prices['key'];
    ByID('price'+key).innerHTML=toPrice(prices['price']);
    ByID('subtotal'+key).innerHTML=toPrice(prices['subtotal']);
    ByID('CurrentCartTotal').innerHTML=prices['total'];
    ByID('total').innerHTML=toPrice(prices['total']);
    ByID('shipping').innerHTML=toPrice(prices['shipping']);
  }
}
