$(document).ready(function(){ 
    var domain = $('#HTTP_DOMAIN').val();
	//$("#cart_block dt:first").hide();

	$("#country_id").live("change", function(event) { 
		//alert('hi');
		var country = $("#country_id").val();
		get_state(domain,country,'state_id');
	
		
	});

	$("#state_id").live("change", function(event) { 
		//alert('hi');
		var state = $("#state_id").val();
		if(state != '')
			$("#tr_other_state").hide("slow");
		else
			$("#tr_other_state").show("slow");
		
	});


	$("#shipping_country_id").live("change", function(event) { 
		
		var country = $("#shipping_country_id").val();
		update_total(domain,country);
		get_state(domain,country,'shipping_state_id');
		
	
		
	});

	$("#shipping_state_id").live("change", function(event) { 
		//alert('hi');
		var state = $("#shipping_state_id").val();
		if(state != '')
			$("#tr_shipping_other_state").hide("slow");
		else
			$("#tr_shipping_other_state").show("slow");

        set_shipping_state(domain,state);
		update_cart_total(domain);
		
		
		
	});


});

function get_state(domain,country_id,state_id,id)
{
	    
		$.ajax({  
			type: "POST",  
			url: domain+"states/get/cid:"+country_id+"/id:"+id, 
			async : false,  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				$("#"+ state_id).html(theResponse);


			}  
		});  
}

function set_states(state_id,shipping_state_id)
{
   var domain = $('#HTTP_DOMAIN').val();
   //alert(shipping_state_id);
	var country = $("#country_id").val();
	if(country != '')
		get_state(domain,country,'state_id',state_id);
	//alert(country);

	var shipping_country = $("#shipping_country_id").val();
    if(shipping_country != '')
		get_state(domain,shipping_country,'shipping_state_id',shipping_state_id);

	   if(state_id != '')
	   {
		  document.getElementById("other_state").value = '';
		  $("#tr_other_state").hide("slow");
	   }
	   else
		  $("#tr_other_state").show("slow");

	   if(shipping_state_id != '')
	   {
		  document.getElementById("shipping_other_state").value = '';
		  $("#tr_shipping_other_state").hide("slow");
	   }
	   else
		  $("#tr_shipping_other_state").show("slow");

}

function set_shipping(domain)
{
	var shipping = $('#shipping_method').val();
	
		$.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/set_shipping/shipping:"+shipping,  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				
				
			
			}  
		});  
		
}

 function set_shipping_state(domain,state)
 {
	
	
		$.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/set_state/state:"+state,  
			//data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				$(this).remove();
				//set_shipping(domain);

			
			}  
		});  
		
}



function set_address_for_shipping()
{
	var domain = $('#HTTP_DOMAIN').val();
	var country = jQuery('#country_id').val();
    var state = jQuery('#state_id').val();
	if(document.getElementById("chk_sameshippingaddress").checked == 1)
	{
		 //alert('hi');
		document.getElementById("shipping_address").value = document.getElementById("address").value;
		document.getElementById("shipping_country_id").value = document.getElementById("country_id").value;
		get_state(domain,country,'shipping_state_id',state);
		//document.getElementById("shipping_state_id").value = document.getElementById("state_id").value;
		document.getElementById("shipping_city").value = document.getElementById("city").value;
		document.getElementById("shipping_other_state").value = document.getElementById("other_state").value;
		document.getElementById("shipping_zip_code").value = document.getElementById("zip_code").value;
        //set_states('shipping_state_id',state);
		if(state != '')
	   {
		  document.getElementById("shipping_other_state").value = '';
		  $("#tr_shipping_other_state").hide("slow");
	   }
	   else
		 $("#tr_shipping_other_state").show("slow");

	   
		
	}
	else
	{
		document.getElementById("shipping_address").value = '';
		document.getElementById("shipping_country_id").value = '';
		//document.getElementById("shipping_state_id").value = '';
		document.getElementById("shipping_city").value = '';
		document.getElementById("shipping_other_state").value = '';
		document.getElementById("shipping_zip_code").value = '';
		 $("#tr_shipping_other_state").show("slow");
		
		
	}
}

function update_total(domain,country)
{
	$.ajax({  
			type: "GET",  
			url: domain+"shopping_carts/total/country:"+country,  
			  
			success: function(theResponse) {
				var theResponseVal 	= (theResponse).split("_");
				
				$("#cart_block_shipping_cost").html(theResponseVal[1]).show("slow");
				$("#cart_block_tax").html(theResponseVal[2]).show("slow");
				$("#cart_block_total").html(theResponseVal[3]).show("slow");
				$("#header_cart").html(theResponseVal[0]+" item(s) | "+theResponseVal[3]).show("slow");
				
				if(theResponseVal[3] == "$0")
				{
					$(".products").html("<dt id='first_item' class='first_item'><span class='price' style='float:left;'>Cart is Empty.</span></dt>").show("slow");; 
					$("#cart-buttons").hide("slow");
					$("#paypal_button").hide("slow");
				}
				$("#notificationsLoader").empty();
			
			}  
		});
}



