jQuery(document).ready(function(){
    jQuery("#city_id").change( function() {
        jQuery("#result").html('Retrieving ...');
        jQuery.ajax({
            type: "POST",
            data: "city_id=" + jQuery(this).val(),
            url: "/shopping-cart?action=getshippingcost",
            success: function(shippingcost){
                if (shippingcost != ''){
                    var finalweight = jQuery("#finalweight").val();
                    var totalamount = jQuery("#totalamount").val();
                    jQuery("#selectionresult").html(shippingcost).show();
                    var totalshippingcost = parseInt(finalweight) * shippingcost;
                    jQuery("#totalshippingcost").html(totalshippingcost).show();
                    var finalamount = parseInt(totalamount) + parseInt(totalshippingcost);
                    jQuery("#finalamount").html(finalamount).show();
                }
                else{
                    jQuery("#selectionresult").html('<em>Tidak Ada Data **</em>');
                }
            }
        });
    });
    jQuery("#d_city").change( function() {
        jQuery("#selectionresult").html('Loading..');
        jQuery.ajax({
            type: "POST",
            data: "city_id=" + jQuery(this).val(),
            url: "/shopping-cart?action=getshippingcost",
            success: function(shippingcost){
                if (shippingcost != ''){
                    var finalweight = jQuery("#finalweight").val();
                    var totalamount = jQuery("#totalamount").val();
                    jQuery("#selectionresult").html(shippingcost).show();
                    var totalshippingcost = parseInt(finalweight) * shippingcost;
                    jQuery("#totalshippingcost").html(totalshippingcost).show();
                    var finalamount = parseInt(totalamount) + parseInt(totalshippingcost);
                    jQuery("#finalamount").html(finalamount).show();
                }
                else{
                    jQuery("#selectionresult").html('<em>Tidak Ada Data **</em>');
                }
            }
        });
    });
	/*
    //Checkout Form Sliding
    jQuery('#checkoutform').hide();

    // toggle slide
    jQuery('#checkout').click(function(){
    // by calling sibling, we can use same div for all demos
    jQuery('#checkoutform').slideToggle();
    });
*/
	//when the checkbox is checked or unchecked
	jQuery('#sameasbuyer').click(function() {

		// If checked
		if (jQuery("#sameasbuyer").is(":checked")) {

			//for each input field
			jQuery('.destination input', ':visible', document.body).each(function(i) { 
				//copy the values from the billing_fields inputs
				//to the equiv inputs on the shipping_fields
				jQuery(this).val( jQuery('.buyer input').eq(i).val() );
	   	 	});
			//won't work on drop downs, so get those values
			var b_city = jQuery("#city_id").val();
			jQuery("#d_city").selectOptions(b_city);
            var b_address = jQuery("textarea#b_address").val();
            jQuery("textarea#d_address").val(b_address);

		} else {

			//for each input field
			jQuery('.destination input', ':visible', document.body).each(function(i) { 
				//set shipping_fields inputs to blank
				jQuery(this).val(""); 
	   	 	});
			jQuery("#d_city").selectOptions("");
		}
	});
});
