
/*SHOPPING CART*/

function applyPromotionalCode(callback){
	callback = callback == undefined?false:callback; 
	var code  = $('#promotional_code').val();
	var total =  parseFloat($('.cart-promotional-code .td3').html());
	$.ajax({
	   async: true,
	   type: "POST",
	   url: "_ajax.php",
	   data: "opc=applyPromotionalCode&code="+code,
	   success: function(data){
			$.ajax({
			   async: true,
			   type: "POST",
			   url: "_ajax.php",
			   data: "opc=cartTotal",
			   success: function(total){
				   if(data!="0"){
					   $('.cart-promotional-code .td2 span').html("$"+total);
					   $('.cart-promotional-code .td3').html("$"+data);
				   }
				   else{
					   $('.cart-promotional-code .td2 span').html("");
					   $('.cart-promotional-code .td3').html("$"+total);
				   }
				   if(callback){
				      callback.apply(this,[]);
				   }
			   }
			});
	   }
	});
}
function applyShipping(callback){
	callback = callback == undefined?false:callback; 
	var code  = $('#promotional_code').val();
	$.ajax({
	   async: true,
	   type: "POST",
	   url: "_ajax.php",
	   data: "opc=applyShipping&code="+code,
	   success: function(data){
		   $('.cart-promotional-code td').removeClass("color3"); 
		   if(data!="0"){
			   $('.cart-shipping td').addClass("color3");
			   $('.cart-shipping .td2').html("Shipping");
			   $('.cart-shipping .td3').html("$"+data);
		   }
		   else{
			   $('.cart-shipping td:first').addClass("color3");
			   $('.cart-shipping .td2').html("Free Shipping");
			   $('.cart-shipping .td3').html("");
		   }
		   if(callback){
		      callback.apply(this,[]);
		   }
	   }
	});
}

function applyStateTax(callback){
	callback = callback == undefined?false:callback; 
	var idState = $('#shipping_state').val();
	var code  = $('#promotional_code').val();
	$.ajax({
	   async: true,
	   type: "POST",
	   url: "_ajax.php",
	   data: "opc=applyStateTax&code="+code+"&idState="+idState,
	   success: function(data){
		   $('.cart-tax td').removeClass("color3");
		   if(data!="0"){
			   $('.cart-tax .td3').addClass("color3");
			   $('.cart-tax .td3').html("$"+data);
		   }
		   else{
			   $('.cart-tax .td3').html("$0.00");
		   }
		   if(callback){
		      callback.apply(this,[]);
		   }
		   
	   }
	});
}


function updateCartAmounts(){
	$(".form_basket").addAjaxLoader();
	var callbackApplyShipping = function(){
		var callbackApplyStateTax = function(){
			var callback = function(){
				var idState = $('#shipping_state').val();
				var code  = $('#promotional_code').val();
		   		var subtotal = parseFloat(stripAlphaChars($('.cart-promotional-code .td3').html()));
		   		var shipping = parseFloat(stripAlphaChars($('.cart-shipping .td3').html()));
		   		var tax = parseFloat(stripAlphaChars($('.cart-tax .td3').html()));
				subtotal = isNaN(subtotal)?0:subtotal;
				shipping = isNaN(shipping)?0:shipping;
				tax = isNaN(tax)?0:tax;
		   		var total = subtotal + shipping + tax;
		   		if( Number(total).toString().indexOf(".")!=-1){
					$(".cart-total-amount .td3").html("$"+new Number(total).toPrecision( Number(total).toString().indexOf(".")+2));
		   		}
		   		else{
		   			$(".cart-total-amount .td3").html("$"+new Number(total).toPrecision( Number(total).toString().length+2));
			   	}
		   		$(".form_basket").removeAjaxLoader(); 
			};
			applyStateTax(callback);//3º
		};
		applyShipping(callbackApplyStateTax);//2º
	};
	applyPromotionalCode(callbackApplyShipping);//1º
}

function checkoutCart(){
	if(checkFormFields('frm_personal_info',1,1,'#808080','#DBF2E8')){
		$(".form_basket").addAjaxLoader(); 	
		var idState = $('#shipping_state').val();
		var code  = $('#promotional_code').val();
		var vars  = $('#frm_personal_info').serializeForm()+"&"+$('#frm_personal_info').serializeFormDescriptions(); 
		
		$.ajax({
			   async: true,
			   type: "POST",
			   url: "_ajax.php",
			   data: "opc=paypalFormFields",
			   success: function(data){
				   $("#paypalFormFields").html(data);
					$.ajax({
						   async: true,
						   type: "POST",
						   url: "_ajax.php",
						   data: "opc=sendCart&"+vars+"&code="+code+"&idState="+idState,
						   success: function(data){
								var transaction_id = data!=''?parseInt(data):0;
								$("#paypal_form_custom").val(transaction_id);
								$(".form_basket").trigger("submit"); 
						   }
					});
			   }
		});
		
	}
}

function updateBasketItemsCount(){
	$.ajax({
	   async: true,
	   type: "POST",
	   url: "_ajax.php",
	   data: "opc=cartUnitCount",
	   success: function(data){
		  items = !isNaN(parseInt(data))?parseInt(data):0;
		  
		  $(".basket_items").html(items+" "+(items==1?"item":"items"));
	   }
	});
}


function addToBasket(el,callback){
	callback = callback!=undefined?callback:false;
	var parent = el.parents(".box_detail_product_right");
	parent.addAjaxLoader();
	var idProduct = parent.find("input[name='id_product']").val();
	var quantity = "";
	var size = "";
	
	
	parent.find(".id_size").each(function(){
		if(parseInt($(this).parent().find("input[name='quantity']").val())>0){
			size += (size!=''?',':'')+$(this).val();
			quantity += (quantity!=''?',':'')+$(this).parent().find("input[name='quantity']").val();
		}
	});
		
	if(quantity!=''){
		$.ajax({
		   async: true,
		   type: "POST",
		   url: "_ajax.php",
		   data: "opc=addItemToCart&idProduct="+idProduct+"&size="+size+"&quantity="+quantity,
		   success: function(data){
				parent.find("input[name='quantity']").val(0);
				
				$.ajax({
				   async: true,
				   type: "POST",
				   url: "_ajax.php",
				   data: "opc=itemAddedMessage&idProduct="+idProduct,
				   success: function(data){
					  showAlertMessage(data,"info");
					  updateBasketItemsCount();
					  parent.removeAjaxLoader();
				   }
				});
					
				
		   }
		});
	}
	else{
		$.ajax({
		   async: true,
		   type: "POST",
		   url: "_ajax.php",
		   data: "opc=translate&word=specify_quantity",
		   success: function(data){
			  showAlertMessage(data,"info");
			  updateBasketItemsCount();
			  parent.removeAjaxLoader();
		   }
		});
	}
} 

function removeFromBasket(el){
	var table = el.parents(".table_basket");
	table.addAjaxLoader();
	var idItem = el.parents("td").find("input[name='id_item']").val();
	var type	  = el.parents("td").find("input[name='product_type']").val();
	$.ajax({
	   async: true,
	   type: "POST",
	   url: "_ajax.php",
	   data: "opc=removeCartItem&id="+idItem+"&type="+type,
	   success: function(data){
			el.parents("tr").remove();
			if(table.find("tr").length==1){
				$.ajax({
				   async: true,
				   type: "POST",
				   url: "_ajax.php",
				   data: "opc=cartHtml",
				   success: function(html){
						table.parents("form").html(html);
				   }
				});
				
			}
			else{
				updateCartAmounts();
			}
			updateBasketItemsCount();
			
	   }
	});
	
}

function updateCartItemQuantity(id,type,quantity,container){
	$('#'+container).addAjaxLoader();
	$.ajax({
	   async: true,
	   type: "POST",
	   url: "_ajax.php",
	   data: "opc=updateCartItemQuantity&id="+id+"&type="+type+"&quantity="+quantity+"&container="+container,
	   success: function(data){
			$('#'+container).html(data);
	   		$('#'+container).removeAjaxLoader();
	   		$('#'+container).find("input[name='id_item'][value='"+id+"']").each(function(){
	   			$(this).parents("tr").find("input[name='quantity']").trigger("focus");
	   			$(this).css("border","1px solid red");
	   		});
	   		updateCartAmounts();
	   }
	});
}


/* END OF SHOPPING CART*/
