		
		function Checkout (userType)
		{
        	$.ajax({
        		type: "POST",
        		beforeSend: startLoading ("cartheader"),
        		url: '/cartCheckBoxesIsCompleted.php',
				success: function (data){
               		if (data == "1" && userType == "C") 
               		{
              			 ViewCartBoxCheckout();
               		}
               		else if (data == "1" && userType != "C")
               		{
               			stopLoading ("cartheader");
               			showLoginCheckout ();
               		}
               		else
               		{
               			stopLoading ("cartheader");               			
               			$('#cartBoxesAlert').show();
                    	$.scrollTo("#cartPanel", 800);               			
               		}
        		}
        	});
		}
		
		function ViewCartBoxCheckout ()
		{
        	$.ajax({
        		url: '/cartBox.php?checkout=1',
				success: function (data){
        			$("#cartTitle").hide();
        			$("#cartTitleCheckout").show();
        			$("#cartBox").html(data);
        			stopLoading ("cartheader");
    			}
        	});
		}
		
		function EditCart ()
		{
        	$.ajax({
        		beforeSend: startLoading ("cartheader"),
        		url: '/cartBox.php',
				success: function (data){
        			if ($("#cartBox").html(data))
        			{
            			$("#cartTitle").show();
            			$("#cartTitleCheckout").hide();
        				stopLoading ("cartheader");
        			}
    			}
        	});
		}

		function showCart ()
        {
        	$('#cartheader').fadeIn();
        	$.scrollTo('#cartheader', 800);
        }

		function AddToCart(productId, itemQty)
        {
            $.ajax({
              type: "POST",
              beforeSend: startLoading ("cartheader"),
              url: "/ws/cartAddItem.php",
              data: "cmd=addItem&productId="+productId+"&itemQty="+itemQty,
              success: function(data) {
            		if ($('#thanks_order').val() == "1")
            		{
            			$('#cartPage').html("");
            			if ($('#cartPage').load("/cart.php"))
            			{
            				stopLoading ("cartheader");
            			}
            		}
            		else
            		{
                		EditCart ();
            		}
             }
            });
        }
        
        function UpdateCartItem(id)
        {
            $.ajax({
              type: "POST",
              beforeSend:  function() { 
            		$("#"+id).fadeOut(); 
            		$("#boxes div").css({"border-color":"#D4D5D7", "background-color":"#D4D5D7"}); 
            		startLoading ("cartheader");
            		},
              url: "/ws/cartUpdateItem.php",
              data: "cmd=updateItem&cartItemId="+id+"&itemQty_"+id+"="+$("#itemQty_"+id).val(),
              success: function(data) {
            		data = data.split("|");
            		/*stopLoading ("itemPrice_"+id);*/
            		$("#itemPrice_"+id).html(data[0]);
            		$("#cartSubtotal").html(data[1]);
            		$("#cartTotalVat").html(data[2]);
            		$("#cartTotal").html(data[3]);
              	    $('#boxes').load("/cartViewBoxes.php");
            	  	$("#"+id).fadeIn();
            	  	$("#boxes").fadeIn();
	        	  	$("#"+id).animate({ backgroundColor: "#E0EAEF" }, 1000);
	        	  	$("#"+id).animate({ backgroundColor: "#D4D5D7" }, 1000);
	        	  	stopLoading ("cartheader");	        	  	
                }
            });
        }
        
        function ChangeCartItem(id, productId, qty)
        {
            $.ajax({
              type: "POST",
              beforeSend: function() {$("#"+id).fadeOut();},
              url: "/ws/cartChangeItem.php",
              data: "cmd=changeItem&cartItemId="+id+"&itemQty="+qty+"&productId="+productId,
              success: function(data) {
	          		data = data.split("|");
	        		$("#itemUnitPrice_"+id).html(data[0]);
	        		$("#itemPrice_"+id).html(data[1]);
	    			$("#itemImage_"+id).attr({src: "/productImages/"+data[2]});
            		$("#color_"+id).css("background-color", data[6]);
            		$("#cartSubtotal").html(data[3]);
            		$("#cartTotalVat").html(data[4]);
            		$("#cartTotal").html(data[5]);
	        	  	$("#"+id).fadeIn();
	        	  	$("#"+id).animate({ backgroundColor: "#E0EAEF" }, 1000);
	        	  	$("#"+id).animate({ backgroundColor: "#D4D5D7" }, 1000);
                }
            });
        }
        
        
        function DeleteItem (id, labelConfirm)
        {
        	$("#cartDeleteItemAlert").show();
        	$.scrollTo("#cartPanel", 800);
        	
        	$("#cartDeleteItemConfirm").click(function () {
                $.ajax({
                  type: "POST",
                  beforeSend: function() {
                	$("#boxes").fadeOut();
                	$("#"+id).animate({ backgroundColor: "#E0EAEF" }, 1000);
                	$("#"+id).animate({ backgroundColor: "#D4D5D7" }, 1000);
                	},
                  url: "/ws/cartDeleteItem.php",
                  data: "cmd=deleteItem&cartItemId="+id,
                  success: function(data) {
                	  $("#"+id).fadeOut();
                	  $("#boxes").fadeIn();
                	  $("#cartSubtotal").html(data);
                	  $('#cartBox').load('/cartBox.php');                	  
                  }
                });
        	 });
        }
        
        function DeleteAll ()
        {
            if (confirm('Delete all?'))
            {
                $.ajax({
                  type: "POST",
                  beforeSend: function(){ ajaxLoadingScript ("cart"); },
                  url: "cartDeleteAllItems.php",
                  data: "cmd=deleteAllItems",
                  success: function() {
                        ajaxLoadingScript ("cart");
                        $('#cart').load("webPart/cart.php");
                        $("#cartMenu").load("webPart/cartMenu.php");
                        $("#cartPageHeader").load("webPart/cartPageHeader.php");
                    }
                });
            }
        }
        
        function CartToOrder ()
        {
    		$("#paymentError").hide();
    		
    		
    		var checkedPayment = $('#payments input:radio:checked').val();
    		//$("input[@name='paymentTypeIdRB']:checked").val();
    		
        	if (checkedPayment != undefined)
        	{
        		if (checkedPayment == "1100" || checkedPayment == "1300")
        		{
        			GestPayConnection();        			
        		}
        		else
        		{
	            	//url = '/ws/paymentUpdate.php';
	            	//data = 'paymentType='+checkedPayment;
	            	//if ($.post(url, data))
	            	//{
	        			//alert('ok');
	        		
		        		$.ajax({
		        			type: "POST",
		        			beforeSend: startLoading ("cartheader"),
		        			url: '/ws/cartToOrder.php',
		        			data: 'cmd=saveOrder',
		        			success: function (data){
		        				thanksType = "";
		    					
		        				if (checkedPayment == "1000")
		        					thanksType = "bonifico";
		        				else if (checkedPayment == "1200")
		        					thanksType = "contrassegno";
		        				else
		            				thanksType = "";
		        				
		        				$('#cartPanel').load("/thanks.php?thanksType="+thanksType+"&orderId="+data);
		        				stopLoading ("cartheader");
		        				
		        			}
		        		});
		        		
	        		//}
        		}
        	}
        	else
        	{
        		$("#paymentError").show();
        		$.scrollTo('.groupTotals', 800);
        	}
        }
        
        
        function InstanWin (dataToSend)
        {
        	dataToSend = "";
        	
        	orderId = "";
        	name = "test";
        	surname = "test";
        	mail = "";
        	pIva = "";
        	telephone = "";
        	prizeCategory = "0";
        	/*
        	$.ajax({
        		type: "POST",
        		beforeSend: '',
        		url: '/ws/instantWin.php',
        		data: 'action=save&mobile=0&orderId='+orderId+'&name='+name+'&surname='+surname+'&mail='+mail+'&pIva='+pIva+'&telephone='+telephone+'&prizeCategory='+prizeCategory,
				dataType: "html",
				complete: function (data){
               		alert(data.responseText);
        		}
        	});
        	*/
        }
        
        function PaymentUpdate (paymentTypeId)
        {
        	resultUpdate = 0;
        	url = '/ws/paymentUpdate.php';
        	data = 'paymentType='+paymentTypeId;
        	if ($.post(url, data))
        	{
        		resultUpdate = 1;
        	}
        }
        
        function CheckBoxesIsCompleted ()
        {
        	$.ajax({
        		type: "POST",
        		beforeSend: '',
        		url: '/cartCheckBoxesIsCompleted.php',
				success: function (data){
               		if (data == "1") 
               		{
               			$("#isClompleted").val(data)
               		}
        		}
        	});
        }
        
        function CartToPos ()
        {
        	$("#orderToPos").bind("click", function(){
        		if ($("#paymentTypeSelect").val() == "210")
        		{
        			CartToOrder ();
        		}
        		else
        		{
        			$("#xPay").submit();
        		}
            });	
        }
        
        function PosToOrder ()
        {
    		$.ajax({
    			type: "POST",
    			beforeSend: startLoading ("cart"),
    			url: '/ws/cartToOrder.php',
    			data: 'cmd=saveOrder',
    			success: function (data){
    				stopLoading ("cart");
                    $('#cart').load("/thanks.php");
    			}
    		});
        }
        
        $(document).ready(function(){
        	//CartToOrder ();
        	CartToPos ();
        });

