$(document).ready(function() {


	$("form.list_basket").submit( function() {
		
		productID = $(this).find("input[name$='product']").val();
		productURL = $(this).find("input[name$='where']").val();
		
		$(this).find('button').hide();
		parentContainer = $(this).parent();

		$.post("/incs/actions/add_to_cart.php", { product: productID, quantity: 1, type: "product", where: productURL, ajax: "true" },
			function(data){
				// show in basket
				inCart = $("<strong></strong>");
				inCart.addClass('in_cart');
				inCart.html('In your <a href="/basket/">Basket</a>');
				parentContainer.html(inCart);
				// update total items
				$("#header p.basket_summary").html(data);
			}
		);
		return false;
	});
	
	$("form.product_form").submit( function() {
		
		productID = $(this).find("input[name$='product']").val();
		productURL = $(this).find("input[name$='where']").val();
		
		productQuantity = $(this).find("select[name$='quantity']").val();
		
		parentContainer = $(this).parent();
		parentContainer.addClass("loading").html("");
		
		$.post("/incs/actions/add_to_cart.php", { product: productID, quantity: productQuantity, type: "product", where: productURL, ajax: "true" },
			function(data){
				// show in basket
				parentContainer.removeClass("loading").html('<p class="in_cart"><strong>In your <a href="/basket/">Basket</a></strong></p>');
				
				// update total items
				$("#header p.basket_summary").html(data);
			}
		);
		
		return false;
	});
	
	//$("#")
	var deliveryHide = $("#delivery_same_as").attr('checked');
	if(deliveryHide) {
		$("#delivery_address_hide").hide();
	}
	
	$("#delivery_same_as").click( function() {
		var isCheck = $(this).attr('checked');
		//alert(isCheck);
		if (isCheck) {
			//alert(isCheck);
			$("#delivery_address_hide").hide();
			
			//$("#delivery_address_hide").css('display','none');
		}
		else {
			$("#delivery_address_hide").slideDown("fast");
			$("#delivery_address1").focus();
		}
	});


    if ($('#user_state').length) {
        // check value of country
        var userCountry = $('#user_country').val();
        if (userCountry != 235) {
            $('#user_state').parent().hide();
        }
        //alert(userCountry);
    }

    if ($('#delivery_state').length) {
        // check value of country
        var deliveryCountry = $('#delivery_country').val();
        if (deliveryCountry != 235) {
            $('#delivery_state').parent().hide();
        }
    }

    $('#user_country').change( function() {
        var uc = $('#user_country').val();
        if (uc == 235) {
            $('#user_state').parent().show();
        }
        else {
            $('#user_state').parent().hide();
        }
    });
    $('#delivery_country').change( function() {
        var dc = $('#delivery_country').val();
        if (dc == 235) {
            $('#delivery_state').parent().show();
        }
        else {
            $('#delivery_state').parent().hide();
        }
    });

	

});
