
/* Spreads */
function openSpreads(id) {
    window.open(app_config.base_url + 'spreads/' + id, "Spreads", "menubar=no,width=840,height=600,toolbar=no,location=no");
}

$(document).ready(function() {
    var search_string = "Search...";

    /* Fix IE enter submit bug */
/*    $('#terms').keydown(function(e){
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
            return false;
        }
    }); */
    
    $('#terms').focus(function() {
        if($(this).val() == search_string) {
            $(this).val('');
        }
    }).blur(function() {
       if($(this).val() == '') {
           $(this).val(search_string);
       }
    });
    
    /* Shopping basket stuff */
    /* TODO: Move to separate file */
    $('#use_home').each(function() {
        if($(this).attr('checked')) {
            $('#delivery_details').hide();
        }
    });
    
    $('#use_home').click(function() {
        if($(this).attr('checked')) {
            $('#delivery_details').hide();
        }
        else {
            $('#delivery_details').show();
        }
    });

    $('#register_details').hide();

    $('#save_details').each(function() {
        if($(this).attr('checked')) {
            $('#register_details').show();
        }
    });
    
    $('#save_details').click(function() {
        if($(this).attr('checked')) {
            $('#register_details').show();
        }
        else {
            $('#register_details').hide();
        }
    });

    $('input[name=delivery_method]:checked').each(function() {
        updateTotal($(this));
    });

    $('input[name=delivery_method]').change(function() {
        updateTotal($(this));
    });
    
    $('select[name=home_country_select]').change(function() {
        label =     $('label[for=home_region_select]');
        select =    $('select[name=home_region_select]');
        if($(this).val() != '0') {
            label.hide();
            select.hide();
        }
        else {
            label.show();
            select.show();
        }
    }).change();
    
    $('select[name=delivery_country_select]').change(function() {
        label =     $('label[for=delivery_region_select]');
        select =    $('select[name=delivery_region_select]');
        if($(this).val() != '0') {
            label.hide();
            select.hide();
        }
        else {
            label.show();
            select.show();
        }
    }).change();    

    $(".qty_field").keypress(function (e) {
        if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
            return false;
        }
    });

    function updateTotal(e) {
        /* Get the strings we need */
        total = $('#subtotal').text().substring(1);
        delivery = e.next().text();
        
        priceRegExp = /\d[\d\,\.]+/;
        match = delivery.match(priceRegExp);
        result = "";
        
        if(match != -1) {
            result = match[0];
        }
        
        /* Get new total */
        f1 = parseFloat(total);
        f2 = parseFloat(result);
        f3 = f1 + f2;
        f3 = f3.toFixed(2);
        
        /* Set new total */
        $('#total').text(f3);
    }
    
});
