$(document).ready(function(){ 
    var domain = $('#HTTP_DOMAIN').val();
    var domain_index = $('#HTTP_DOMAIN_WEBROOT').val();

	$(".std .edit_link").live("click", function(event) { 
		//alert('hi');
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 		= productIDValSplitter[1];	

		//$("#notificationsLoader").html('<img src="'+domain+'img/user/loader.gif">');
	
		$.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/edit/id:"+productIDVal,  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				$("#quantity_"+ productIDVal).html(theResponse);
			}  
		});  
		
	});

	$(".std .modifyProduct").live("click", function(event) { 
		//alert('hi');
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	

		//$("#notificationsLoader").html('<img src="'+domain+'img/user/loader.gif">');
		var qty = $('#edit_quantity_'+productIDVal).val();
        //alert(qty);
	    var status = IsNumeric(qty);
		//alert(status);
		 
		 if(qty == '' || qty == 0)
	     {
		 	alert("Please enter quantity");
       		return false;
		 }
	     else if(!status)
	     {
			alert("Please enter valid quantity"); 
			return false;
	     }
		$.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/modify/id:"+productIDVal+"/quantity:"+qty,  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				$("#product_row_"+ productIDVal).html(theResponse);
				$("#shipping_method").html("<option value=''> Please select </option>");
				update_item(domain,productIDVal);
				update_cart_total(domain);
				
			}  
		});  
		
	});

	$(".std .remove_link").live("click", function(event) { 
		//alert('hi');
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	

		//$("#notificationsLoader").html('<img src="'+domain+'img/user/loader.gif">');
	
		$.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/remove/id:"+productIDVal,  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
                   
                $("#product_row_" + productIDVal).hide("slow",  function() {$(this).remove();});
				$("#cart_block_product_" + productIDVal).hide("slow",  function() {$(this).remove();});
				$("#shipping_method").html("<option value=''> Please select </option>");
				$("#notificationsLoader").empty();
				update_cart_total(domain);
				
			}  
		});  
		
	});

	

});

 function calculate_shipping() { 
           var domain = $('#HTTP_DOMAIN').val();
          var shipping_zip_code = $('#shipping_zip_code').val();

		  $("#loader_image").html('<img src="'+domain+'img/user/loader.gif">');

		  $.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/set_state/zip_code:"+shipping_zip_code,  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				 
				set_shipping(domain); // location.js//
				set_shipping_rates(); //shopping_carts//
				
			
			}  
		}); 
		  
		
 }

function update_cart_total(domain)
{
	$.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/total",  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				//alert(theResponse);
				var theResponseVal 	= (theResponse).split("_");

				$("#shopping_cart_shipping_charges").html(theResponseVal[1]).show("slow");
				$("#cart_block_shipping_cost").html(theResponseVal[1]).show("slow");

				$("#shopping_cart_tax").html(theResponseVal[2]).show("slow");
				$("#cart_block_tax").html(theResponseVal[2]).show("slow");

                $("#shopping_cart_grand_total").html(theResponseVal[3]).show("slow");
				$("#cart_block_total").html(theResponseVal[3]).show("slow");

				$("#shopping_cart_sub_total").html(theResponseVal[4]).show("slow");

				$("#header_cart").html(theResponseVal[0]+" item(s) | "+theResponseVal[3]).show("slow");
				
				if(theResponseVal[3] == "$0")
				{
					$("#first_item").show("slow"); 
					$("#cart-buttons").hide("slow");
					$("#check_out_button").hide("slow");
				}
				$("#notificationsLoader").empty();
			
			}  
		});
}

function update_item(domain,id)
{
	$.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/update_item/id:"+id,  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				

				$("#cart_block_product_"+id).html(theResponse).show("slow");
				
				
				$("#notificationsLoader").empty();
			
			}  
		});
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function get_shipping_rates()
{
       var domain = $('#HTTP_DOMAIN').val();
       var domain_index = $('#HTTP_DOMAIN_WEBROOT').val();
	   $("#loader_image").html('<img src="'+domain_index+'img/user/loader.gif">');

	   $.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/get_shipping",  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				$("#shipping_method").html(theResponse);
				$("#loader_image").empty();
			}  
		});
}

function set_shipping_rates()
{
       var domain = $('#HTTP_DOMAIN').val();

	   $.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/set_shipping_rates",  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				get_shipping_rates();
				update_cart_total(domain);
			

			}  
		});
}


