function update_cart(){
    $('#cart_form').attr('action', Path + 'cart/update');
    $('#cart_form').submit();
}

function del(id){
    if (confirm('Delete "' + $('#product_name_' + id).text() + '" ?')) {
        $('#cart_' + id).val(0);
        update_cart();
    }
}

function ship_address(){
    $('#cart_form').attr('action', Path + 'cart/ship_address');
    $('#cart_form').submit();
}

function get_prov(){
    $('#prov_id').length = 0;
    $('#prov_id').html('<option>-- ' + str_choose_country + ' --</option>');
    $('#prov_id').attr('disabled', true);
    $('#region_id').length = 0;
    $('#region_id').html('<option>-- ' + str_choose_prov + ' --</option>');
    $('#region_id').attr('disabled', true);
    $('#city_id').length = 0;
    $('#city_id').html('<option>-- ' + str_choose_region + ' --</option>');
    $('#city_id').attr('disabled', true);
    if ($('#country_id').val() != 0) {
        $.ajax({
            type: "POST",
            url: Path + 'cart/get_prov',
            data: {
                id: $('#country_id').val()
            },
            beforeSend: function(){
                $('#prov_id').html('<option>' + str_loading + '</option>');
            },
            success: function(response){
                $('#prov_id').html(response);
            },
            complete: function(){
                if ($('#prov_id').length == 0) {
                    $('#prov_id').update('<option>' + str_not_found + '</option>');
                }
                else {
                    if ($('#prov_id').val() == 'no_prov') {
                        $('#prov_id').length = 0;
                        $('#prov_id').html('<option>' + str_prov_disabled + '</option>');
                        $('#prov_id').attr('disabled', true);
                        $('#region_id').length = 0;
                        $('#region_id').html('<option>' + str_region_disabled + '</option>');
                        $('#region_id').attr('disabled', true);
                        $('#city_id').length = 0;
                        $('#city_id').html('<option>' + str_city_disabled + '</option>');
                        $('#city_id').attr('disabled', true);
                    }
                    else {
                        $('#prov_id').attr('disabled', false);
                        get_region();
                    }
                }
            }
        });
        /*
         new Ajax.Updater('prov_id', Path + 'cart/get_prov', {
         parameters: {
         id: $('#country_id')
         },
         onLoading: function(){
         $('#prov_id').update('<option>' + str_loading + '</option>');
         },
         onComplete: function(){
         if ($('#prov_id').length == 0) {
         $('#prov_id').update('<option>' + str_not_found + '</option>');
         }
         else {
         if ($('#prov_id').val() == 'no_prov') {
         $('#prov_id').length = 0;
         $('#prov_id').update('<option>' + str_prov_disabled + '</option>');
         $('#prov_id').disable();
         $('#region_id').length = 0;
         $('#region_id').update('<option>' + str_region_disabled + '</option>');
         $('#region_id').disable();
         $('#city_id').length = 0;
         $('#city_id').update('<option>' + str_city_disabled + '</option>');
         $('#city_id').disable();
         }
         else {
         $('#prov_id').enable();
         get_region();
         }
         }
         }
         })
         */
    }
}

function get_region(){
    $('#region_id').length = 0;
    $('#region_id').html('<option>-- ' + str_choose_prov + ' --</option>');
    $('#region_id').attr('disabled', true);
    $('#city_id').length = 0;
    $('#city_id').html('<option>-- ' + str_choose_region + ' --</option>');
    $('#city_id').attr('disabled', true);
    if ($('#prov_id').val() != 0) {
        $.ajax({
            type: "POST",
            url: Path + 'cart/get_region',
            data: {
                id: $('#prov_id').val()
            },
            beforeSend: function(){
                $('#region_id').html('<option>' + str_loading + '</option>');
            },
            success: function(response){
                $('#region_id').html(response);
            },
            complete: function(){
                if ($('#region_id').length == 0) {
                    $('#region_id').html('<option>' + str_not_found + '</option>');
                }
                else {
                    $('#region_id').attr('disabled', false);
                    get_city();
                }
            }
        });
        /*
         new Ajax.Updater('region_id', Path + 'cart/get_region', {
         parameters: {
         id: $('#prov_id').val()
         },
         onLoading: function(){
         $('#region_id').html('<option>' + str_loading + '</option>');
         },
         onComplete: function(){
         if ($('#region_id').length == 0) {
         $('#region_id').html('<option>' + str_not_found + '</option>');
         }
         else {
         $('#region_id').attr('disabled', false);
         get_city();
         }
         }
         })
         */
    }
}

function get_city(){
    $('#city_id').length = 0;
    $('#city_id').attr('disabled', true);
    if ($('#prov_id').val() != 0) {
        $.ajax({
            type: "POST",
            url: Path + 'cart/get_city',
            data: {
                id: $('#region_id').val()
            },
            beforeSend: function(){
                $('#city_id').html('<option>' + str_loading + '</option>');
            },
            success: function(response){
                $('#city_id').html(response);
            },
            complete: function(){
                if ($('#city_id').length == 0) {
                    $('#city_id').html('<option>' + str_not_found + '</option>');
                }
                else {
                    $('#city_id').attr('disabled', false);
                }
            }
        })
        /*
         new Ajax.Updater('city_id', Path + 'cart/get_city', {
         parameters: {
         id: $F('region_id')
         },
         onLoading: function(){
         $('city_id').update('<option>' + str_loading + '</option>');
         },
         onComplete: function(){
         if ($('city_id').length == 0) {
         $('city_id').update('<option>' + str_not_found + '</option>');
         }
         else {
         $('city_id').attr('disabled', false);
         }
         }
         })
         */
    }
}

function validate_new_address(){
    // name
    // country != 0
    // street
    // zip == 0 || 5
    // phone
}

function ship_address_set(id){
    if (id == 'new') {
        $('#address_form').submit();
    }
    else {
        window.location = Path + 'cart/ship_address_set/' + id;
    }
}

function send_order(){
    window.location = Path + 'cart/send_order';
}

