cartDivs= new Object();
cartDivs['CurrentCart']='current';
cartDivs['CurrentCartThumbs']='current-thumbs';
cartDivs['CartCheckout']='checkout-items';

currentFB = '';

function Add(type,name,price,code,options,quantity) {
  var templates=Array();
  var cart=new jscart(CB_cart);
  cart.fb_addtocart('domParent',code);
}

function fb_close(id) {
  ByID('FB_AddtoCart_'+id).style.display = 'none';
}

function AddItem(type,name,price,code,options,quantity) {
  
  elem = ByID('FBs')
  elem.innerHTML = '';
  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 = {
  fb_addtocart: function(domSrc) {

    elem = ByID('FBs')
    elem.innerHTML = '';
    domParent = elem;
    eval(domSrc);
    pageSize = size();
    for(i =0; i<elem.childNodes.length; i++) {
      //alert(elem.childNodes[i]);
      
      
      if(elem.childNodes[i].nodeType != 3) { // Not text node
//      elem.childNodes.style.display = '';
        elemSize = [0,0];
        elemSize[0] = elem.childNodes[i].offsetWidth/2;
        elemSize[1] = elem.childNodes[i].offsetHeight/2;

        //window.status = document.clientWidth;
        elem.childNodes[i].style.top = (pos()+pageSize[1]/2-elemSize[1]+(i*5))+'px';
        elem.childNodes[i].style.left = (pageSize[0]/2-elemSize[0]+(i*5))+'px';
      }

    }
    //window.status = pos();
    
  },
  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];
    setCartThumbPos();

}

function show_right() {
  pageSize = size();
  window.status = pageSize[0];
  if(pageSize[0] > 1000)  {
    ByID('CurrentCart').style.display = 'none';
    ByID('CurrentCartThumbs').style.display = '';
  } else {
    ByID('CurrentCart').style.display = '';
    ByID('CurrentCartThumbs').style.display = 'none';

  }
  setCartThumbPos();
}

//wevent.onresize.push('show_right()');
//wevent.onload.push('show_right()');

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=_Lang['Lex']['CHECKOUT_LABEL']+String.fromCharCode(0xBB); //(Unicode &raquo;)
    }
    else {
      button.value=_Lang['Lex']['CHECKOUT_LABEL']+String.fromCharCode(0xBB); //(Unicode &raquo;)
      button.onclick=function () {
        document.location=BASE_URL+'checkout';
      };
    }
    return;
  }
  ByID('FB_AddtoCart_'+code).style.display = 'none';
  var button=ByID('cartAddZoom'+code);
  button.value=_Lang['Lex']['CHECKOUT_LABEL']+'Checkout '+String.fromCharCode(0xBB); //(Unicode &raquo;)
  alert('Hello ' + _Lang['Lex']['CHECKOUT_LABEL']); 
  button.onclick=function () {
    window.opener.document.location=BASE_URL+'checkout';
    window.close();
  };
}

function qtyChanged(key) {
  var delivery=SelectedIndex('options'+key);
  
  // kludge - assume that if no delivery select box is shown, the product is a DVD
  if (delivery == false)
  	delivery = 'DVD';
  	
  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 shippingChanged(shipping) {
  var cart=new jscart(CB_cart_update);
  //alert(shipping);
  cart.updateshipping(shipping);
}
function deliveryChanged(key) {
  //alert(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) {
    
    ByID('CCTotal').innerHTML=     prices['total'];
    ByID('CCShipping').innerHTML=  prices['shipping'];

    ByID('CCTTotal').innerHTML=    prices['total'];
    ByID('CCTShipping').innerHTML= prices['shipping'];
    //ByID('CCTDiscount').innerHTML= prices['discount'];

    ByID('total').innerHTML=       prices['total'];
    ByID('shipping').innerHTML=    prices['shipping'];
    //ByID('discount').innerHTML=    prices['discount'];
  },
  updateshipping: function(prices) {
    //window.status = prices['shipping'];
    ByID('CCTotal').innerHTML=     prices['total'];
    ByID('CCShipping').innerHTML=  prices['shipping'];

    ByID('CCTTotal').innerHTML=    prices['total'];
    ByID('CCTShipping').innerHTML= prices['shipping'];
    //ByID('CCTDiscount').innerHTML= prices['discount'];

    ByID('total').innerHTML=       prices['total'];
    ByID('shipping').innerHTML=    prices['shipping'];
    //ByID('discount').innerHTML=    prices['discount'];
    //window.status = prices.status_msg;
  }
}





