function loadCartResult(resp) {
    $('#checkout_cart_container').html(resp);
    $('.checkout_cart_qty').change(setQty);
    $('#id_delivery_city').change(setCity);
}

function setQty() {
    // alert($(this).attr('id'));
    vals = $(this).attr('id').match(/sel_([0-9]+)_([0-9]+)/);
    id_item = vals[1];
    id_option = vals[2];
    qty = $(this).val();
    cartAction = qty == 0 ? 'drop_item' : 'set_qty';
    $.ajax({
        type: 'POST',
        url: '/ajax_cart.php',
        data: cartAction + '=1&id_item=' + id_item + '&id_option=' + id_option + '&qty=' + qty,
        success: function(resp) {
            loadCartResult(resp);
        }
    });
}

function setCity() {
    $.ajax({
        type: 'POST',
        url: '/ajax_cart.php',
        data: 'set_city=1&id_delivery_city=' + $(this).val(),
        success: function(resp) {
            loadCartResult(resp);
        }
    });
    cityname = $('#id_delivery_city :selected').text();
    $('#delivery_city_target').html(cityname);
}

function recycle() {
    elms = $('.del');
    $.each(elms, function(){
        mySource = this.name.match(/delivery_(.*)/)[1];
        this.value = $('#' + mySource).val();
    });
}

$(document).ready(function(){

    $('.checkout_items_add2cart').click(function(){
        container_div = $(this).parents('div');
        item_id = container_div.attr('id').match(/[0-9]+/);
        option_id = $('#id_option_' + item_id).val();
        //alert(item_id + ', ' + option_id + ', ' + lang);
        $.ajax({
            type: 'POST',
            url: '/ajax_cart.php',
            data: 'add_cart=1&id_item=' + item_id + '&id_option=' + option_id + '&qty=1',
            success: function(resp) {
                loadCartResult(resp);
            }
        });
    });
    $('select[name="id_option"]').change(function(){
        //alert($(this).val());
        selected_val = ($(this).val());
        container_div = $(this).parents('div');
        item_id = container_div.attr('id').match(/[0-9]+/);
        $('#id_option_' + item_id).val(selected_val);
    });

    $('.checkout_cart_qty').change(setQty);
    $('#id_delivery_city').change(setCity);
    $('#delivery_date').datepicker();
    $.datepicker.setDefaults({
        minDate: 'today',
        maxDate: '+4w',
        monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Setiembre','Octubre','Noviembre','Diciembre'],
        firstDay: 1,
        dayNamesMin: ['do','lu','ma','mi','ju','vi','sa'],
        dateFormat: 'dd/mm/yy'
    });
    $('#order_button').click(function(){
        $('#orderForm').submit();
    });

    $('#pay_caixa').click(function(){
        $('#payment_method').val('caixa');
        $('#payment_form').submit();
    });
    $('#pay_phone').click(function(){
        $('#payment_method').val('phone');
        $('#payment_form').submit();
    });
    $('#pay_paypal').click(function(){
        $('#payment_method').val('paypal');
        $('#payment_form').submit();
    });
    $('#recycle').click(recycle);



});
