cartDivs= new Object();
//cartDivs['CurrentCart']='current';
cartDivs['CurrentCartThumbs']='current-thumbs';
cartDivs['CartCheckout']='checkout-items';

//Find a list of templates we need to update divs in the current page
function prepTemplates() {
  var templates=Array();
  for(var id in cartDivs)
    if(ByID(id)) templates.push(id+','+cartDivs[id]);
  return templates;
}

function Add(type,name,price,code,options,quantity,mode) {
  if(!code)     code=name;
  if(!options)  options=false;
  if(!quantity) quantity=1;
  if(!mode)     mode="async";

  if(mode=="async") {
    var cart=new jscart(CB_cart);
    cart.add(prepTemplates(),type,name,price,code,options,quantity);
    cartCalled(code);
  }
  else {
    var cart=new jscart();
    cartCalled(code);
    var result=cart.add(prepTemplates(),type,name,price,code,options,quantity);
    CB_cart.add(result);
  }
}


function Remove(key) {
  var cart=new jscart(CB_cart);
  cart.remove(prepTemplates(),key);
}


var CB_cart = {
  add: function(html_blocks) {
    //ByID('footer').innerHTML=html_blocks.toString();
    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];
    setCartThumbPos();
    show_right();
}



function show_right() {
  if(trimString(ByID('CurrentCartThumbs').innerHTML)=='')
    ByID('cart').style.display = 'none';
  else
    ByID('cart').style.display = '';
  return;
}



wevents.onresizes.push('show_right()');
wevents.onloads.push('show_right()');


function trimString (str) {
  if(typeof(str)!='string')
    return '';
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

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=0;
  var quantity=safeParseInt(ByID('qty'+key).value);
  var cart=new jscart(CB_cart_update);
  cart.update(prepTemplates(),key,'quantity',quantity);
}


function deliveryChanged(key) {
  var delivery=SelectedIndex('options'+key);
  var price=0;
  var quantity=safeParseInt(ByID('qty'+key).value);
  var cart=new jscart(CB_cart_update);
  cart.update(prepTemplates(),key,'price',price,'option','delivery',delivery);
}

var CB_cart_update = {
  update: function(html_blocks) {
    insertHtmlBlocks(html_blocks);
  }
}

