function update_shipping_prices(fieldset){
    $(fieldset).find("input[name=shipping2]").val( $(fieldset).find("input[name=s_"+$.cookie("delivery_location")+"]").val().split("|")[1] );
    $(fieldset).find("input[name=shipping]").val( $(fieldset).find("input[name=s_"+$.cookie("delivery_location")+"]").val().split("|")[0] );
}

$(document).ready(function(){

    function delivery_location_valid(location)
    {   // chceck if given delivery location (e.g. from cookie) is valid (in shipping object)
        for (var key in shipping_zones) {
            if (key == location){
                return true;
            }
        }
        return false;
    }

    // fill the <select> with items based on above shipping object
    for(var key in shipping_zones)
    {
        $("#delivery_location").append('<option value="'+key+'">'+shipping_zones[key].label+'</option>');
    }

    if (!delivery_location_valid($.cookie("delivery_location"))){
        $.cookie("delivery_location", null);
    }

    // clicks on "BUY NOW" button
    $("form fieldset input:image:not(.contact_submit, .btn), form .buy_btn2, form .buy_btn1, form .side_panel").click(function(){
        // save clicked object's parent (fieldset) into global variable
        clicked_item = this.parentNode;

        // if there's no delivery option choosen (in cookie) let user choose
        if(!delivery_location_valid($.cookie("delivery_location"))){
            $("#delivery_box").overlay({ expose: { color: '#000', loadSpeed: 200, opacity: 0.55 },
                                            closeOnClick: false,
                                            closeOnEsc: false,
                                            api: true }).load();
            // cart is called by submitting form in delivery box

        // but if the choice was in the cookie, immediately show cart
        } else {
            update_shipping_prices(clicked_item);
            EJEJC_lc(clicked_item);
        }
    });


    $("#delivery_box form").submit(function(e){
        // get user input
        var selected = $("#delivery_location", this).val();
        //alert(selected);

        // save it in cookie
        $.cookie("delivery_location", selected);

        // update shipping information
        update_shipping_prices(clicked_item);
        EJEJC_lc(clicked_item);

        // do not submit the form
        return e.preventDefault(); 
    });

    $(".newsletter_btn").click(function(){
        $("#newsletter_confirmation_box").overlay({ expose: { color: '#000', loadSpeed: 400, opacity: 0.55 },
                                            closeOnEsc: true,
                                            api: true }).load();
        //$("#newsletter_frame").attr("src", "index.html");
    });

});
