function updateCartFromCall(transport) {

//	document.getElementById('addtocartmsg').style.display = "block";

	var data = eval('('+ transport.responseText +')');
    $('cart_items').innerHTML =	data.total_items ;
	$('cart_total').innerHTML = data.total_amount;
	//alert(data.total_items+data.total_amount);
	if($('view_cart').style.display == "block"){
		$('view_cart').innerHTML = '<img style="margin-left:420px;width:100px;height:100px;" src="'+base+'images/ajax-loader-thick-bw.gif" /></div>';
		refreshcart();
  	}
  	document.getElementById('addtocartmsg').style.display="block";
  	return;
}

function usbcbox()
{
	html="<h2>Are you a current USBC member?</h2><br><br><center><span style='margin: 0pt; float: left;' class='skin_delete'><a href='#' onclick=''>Yes</a></span><span style='margin: 0pt; float: left;' class='skin_delete'><a href='#' onclick=''>No</a></span></center>";	
	
	showLightbox('Added to cart');
	return false;
	
}


function refreshCartCounter(){
	var start = new Date();
	new Ajax.Request(base+shop_id+"/cart/refresh?cashbuster="+start.getTime(),{ onSuccess: updateCartFromCall , method: 'get'});
	
}

function addToCart(product_id,price,a,skin_type,skin_id){
	showLightboxHTML('Added to cart');

	
	//document.documentElement.scrollTop = 0;
	// Alternative prototype call
	//alert(base+shop_id+"/cart/add?id="+product_id+"&skin_type="+skin_type+"&skin_id="+skin_id);
	new Ajax.Request(base+shop_id+"/cart/add?id="+product_id+"&skin_type="+skin_type+"&skin_id="+skin_id,{ onSuccess: updateCartFromCall , method: 'get' });
	
	return false;
}

function addToCartRefresh(product_id,price,a,skin_type,skin_id){
	showLightboxHTML('Added to cart');
	
	//document.documentElement.scrollTop = 0;
	// Alternative prototype call
	//alert(base+shop_id+"/cart/add?id="+product_id+"&skin_type="+skin_type+"&skin_id="+skin_id);
	new Ajax.Request(base+shop_id+"/cart/add?id="+product_id+"&skin_type="+skin_type+"&skin_id="+skin_id,{ onSuccess: updateCartFromCall , method: 'get' });
	
	window.location.reload();return false;
	
	//return false;
}

function addToCartNew(product_id,price,a,skin_type,skin_id){
	
		var qty_id = "qty_"+product_id;
	var qty = document.getElementById(qty_id).value;
	
	
		var wght = "attr_"+product_id;
	var wght_val = document.getElementById(wght).value;
	
	if(wght_val==0){
		alert('Please select weight for the product');
		return false;
	}
	showLightboxHTML('Added to cart',shop_id);
	var FormName="frm";	
	
	new Ajax.Request(base+shop_id+"/cart/add?id="+product_id+"&qty="+qty+"&wght="+wght_val+"&skin_type="+skin_type+"&skin_id="+skin_id,{ onSuccess: updateCartFromCall , method: 'get' });
	
	//alert("hi")
	
	if(document.forms['frm'].chk)
	for(i=0; i<document.forms['frm'].chk.length; i++)
	{
		if(document.forms['frm'].chk[i].checked==true)
		{
			p_id=document.forms['frm'].chk[i].value;
			
			
			
			p_price=document.forms['frm'].price[i].value;
			
			p_freight=document.forms['frm'].freight[i].value;
			//alert("/cart/add?id="+p_id+"&parent_id="+product_id+"&price="+p_price);
			if(p_price=='undefined')
				p_price=0;
			new Ajax.Request(base+shop_id+"/cart/add?id="+p_id+"&parent_id="+product_id+"&price="+p_price+"&freight="+p_freight+"&skin_type="+skin_type+"&skin_id="+skin_id,{ onSuccess: updateCartFromCall , method: 'get' });			
		
		}		
	}
	
	if(document.forms['frm'].chk)	
	if(!isNaN(document.forms['frm'].chk.length) || document.forms['frm'].chk.checked==true)
	{
	
			p_id=document.forms['frm'].chk.value;
			p_price=document.forms['frm'].price.value;
			p_freight=document.forms['frm'].freight.value;
	//		alert("/cart/add?id="+p_id+"&parent_id="+product_id+"&price="+p_price);
			new Ajax.Request(base+shop_id+"/cart/add?id="+p_id+"&parent_id="+product_id+"&price="+p_price+"&freight="+p_freight+"&skin_type="+skin_type+"&skin_id="+skin_id,{ onSuccess: updateCartFromCall , method: 'get' });		
	}		
		
	// document.documentElement.scrollTop = 0;
	// Alternative prototype call
	// alert(base+shop_id+"/cart/add?id="+product_id+"&skin_type="+skin_type+"&skin_id="+skin_id);
	// new Ajax.Request(base+shop_id+"/cart/add?id="+product_id+"&skin_type="+skin_type+"&skin_id="+skin_id,{ onSuccess: updateCartFromCall , method: 'get' });
	
	return false;
}

 function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
  }



function CloneToCart(product_id,price,a,skin_type,skin_id){
	//showCloneLightboxHTML("<a href='#'>Continue Shopping</a> <br> <a href='#' style='pointer:cursor;' onclick='window.location=\"/shop/0/cart/checkout\"'>Proceed to Check Out</a>");
	
	//document.documentElement.scrollTop = 0;
	// Alternative prototype call
	//alert(base+shop_id+"/cart/add?id="+product_id+"&skin_type="+skin_type+"&skin_id="+skin_id);
	new Ajax.Request(base+shop_id+"/cart/add?id="+product_id+"&skin_type="+skin_type+"&skin_id="+skin_id,{ onSuccess: updateCartFromCall , method: 'get' });
	
	return false;
}

function updateCart(cart_id,skin_type,skin_id){
	if(confirm('Do you want update your product in cart') ){
		showLightboxHTML('Cart is updated');
	
	   $('view_cart').innerHTML = '<img style="margin-left:420px;width:100px;height:100px;" src="'+base+'images/ajax-loader-thick-bw.gif" /></div>';
	   new Ajax.Request(base+shop_id+"/cart/update/"+cart_id+"?skin_type="+skin_type+"&skinid="+skin_id,{ onSuccess: refreshcart , method: 'get' });
	}
}

function setProductSkin(src){
	   	 $('previewArea').innerHTML =	"<img id='imgCrop_testImage' style='position:absolute;float;left;margin-top:-115px;margin-left:10px;z-index:-1;width:120px;heigth:120px;' src='"+src+"'/>";
}

function updateCartView(transport) {
	$('view_cart').innerHTML= transport.responseText;
	document.getElementById('addtocartmsg').style.display="block";
	initLightbox();
}

function viewcart(){
  if($('view_cart').style.display == "block"){
     hidecart();
  }else{
	   $('view_cart').style.display = "block";
	   $('view_cart').innerHTML = '<img style="margin-left:420px;width:100px;height:100px;" src="'+base+'images/ajax-loader-thick-bw.gif" /></div>';
	   new Ajax.Request(base+shop_id+"/cart/view",{ onSuccess: updateCartView , method: 'get' });
	
	   
   }
return false;

}

function refreshcart(){
 new Ajax.Request(base+shop_id+"/cart/view?rand="+escape(Math.floor(Math.random()*11)),{ onSuccess: updateCartView , method: 'get' });

return false;
}

function hidecart(){
	$('view_cart').style.display = "none";
}
function swapImg(img,from,to){
	if(img.src.indexOf(from) > 0 ) img.src = to;
}


function deleteFromCart(id){
	if(confirm('Do you want remove that item from cart ? Please note that all the upsell items related to this item will also be deleted.') ){
	   $('view_cart').innerHTML = '<img style="margin-left:420px;width:100px;height:100px;" src="'+base+'images/ajax-loader-thick-bw.gif" /></div>';
	   new Ajax.Request(base+shop_id+"/cart/del/"+id,{ onSuccess: refreshCartCounter , method: 'get' });
	   
	}
return false;
}

function saveInCart(id){
	
	if(document.getElementById('attr_'+id)){
		new Ajax.Request(base+shop_id+"/cart/set/"+id+"?qty="+parseInt(document.getElementById('qty_'+id).value)+"&attr="+escape(document.getElementById('attr_'+id).value)+"&rand="+escape(Math.floor(Math.random()*11)),{ onSuccess: refreshCartCounter , method: 'get' });
	}else{
		new Ajax.Request(base+shop_id+"/cart/set/"+id+"?qty="+parseInt(document.getElementById('qty_'+id).value)+"&rand="+escape(Math.floor(Math.random()*11)),{ onSuccess: refreshCartCounter , method: 'get' });
	}
}

function checkFileUpload(){

  var filename = document.getElementById('userfile').value;
	if(filename.length > 0){
		  if(filename.toLowerCase().indexOf('.jpeg') > 0 || filename.toLowerCase().indexOf('.jpg') > 0){
		  	showLightboxHTML("uploading");
		  	setTimeout("document.getElementById('fileUploaderForm').submit();",2000);
		  }else{
		  	alert("Your image type is not supported, please use jpg file format");
		  }
	 }else{
	   	alert("Please select file first");
	 }
}


function updateTax(state,order_id, total,shipping){
	
	document.getElementById('tax_f').innerHTML = '';	
	
	new Ajax.Request(base+shop_id+"/cart/updatetax?state="+state+"&orderid="+order_id+"&total="+total+"&shipping="+shipping+"&cashbuster="+escape(Math.floor(Math.random()*11)),{ onSuccess: updatePageFromCall , method: 'get'});
	
}

function updatePageFromCall(transport) {

	//alert("hi");
	//alert(transport.responseText );
//	document.getElementById('addtocartmsg').style.display = "block";
	//var data = eval('('+ transport.responseText +')');
	var str=transport.responseText
	var words = str.split('|');
   
	document.getElementById('tax_f').innerHTML = "$ "+words[0];	
	document.getElementById('total_f').innerHTML = "$ "+words[1];	
		document.getElementById('total_g').innerHTML = "<b>$ "+words[1]+"</b>";
  	
  	return;
}
