/*
 *  Javascript functions to manipulate the searchboxes for Sunset Imports.
 *
 *  @copyright Synotac 2010
 *  @version $Id: searchbox.js 148 2010-02-16 00:15:29Z bill $
 */

// set the existing selects based on any checkboxes checked
jQuery(document).ready(function(){
    set_makes();
});

// change the visible makes when the checkboxes are toggled
jQuery('#type-new').change(function() {
    set_makes();
})

jQuery('#type-used').change(function () {
    set_makes();
})

jQuery('#type-cert').change(function () {
    set_makes();
})

// change the models when the make select changes
jQuery('#make-select').change(function() {
    set_models();
});

// set the value of the make select field depending on checkboxes
function set_makes() {
    var make_selected = jQuery('#make-select').val();
    var new_chk = jQuery('#type-new').attr('checked');
    var used_chk = jQuery('#type-used').attr('checked');
    var cert_chk = jQuery('#type-cert').attr('checked');
    if (new_chk && used_chk && cert_chk) all_makes();
    else if (new_chk && used_chk && !cert_chk) all_makes();
    else if (new_chk && !used_chk && cert_chk) cert_makes();
    else if (new_chk && !used_chk && !cert_chk) new_makes();
    else if (!new_chk && used_chk && cert_chk) used_makes();
    else if (!new_chk && used_chk && !cert_chk) used_makes();
    else if (!new_chk && !used_chk && cert_chk) cert_makes();
    else if (!new_chk && !used_chk && !cert_chk) all_makes();
    jQuery('#make-select').val(make_selected);
    set_models();
}

// set the value of the model select field depending on selected make and checkboxes
function set_models() {
    var make_selected = jQuery('#make-select').val();
    var model_selected = jQuery('#model-select').val();
    var new_chk = jQuery('#type-new').attr('checked');
    var used_chk = jQuery('#type-used').attr('checked');
    var cert_chk = jQuery('#type-cert').attr('checked');
    if (new_chk && used_chk && cert_chk) all_models(make_selected);
    else if (new_chk && used_chk && !cert_chk) all_models(make_selected);
    else if (new_chk && !used_chk && cert_chk) cert_models(make_selected);
    else if (new_chk && !used_chk && !cert_chk) new_models(make_selected);
    else if (!new_chk && used_chk && cert_chk) used_models(make_selected);
    else if (!new_chk && used_chk && !cert_chk) used_models(make_selected);
    else if (!new_chk && !used_chk && cert_chk) cert_models(make_selected);
    else if (!new_chk && !used_chk && !cert_chk) all_models(make_selected);
    jQuery('#model-select').val(model_selected);
}

function all_makes() {
    jQuery('#make-select').html(all_makes_html);
}

function new_makes() {
    jQuery('#make-select').html(new_makes_html);
}
function used_makes() {
    jQuery('#make-select').html(used_makes_html);
}

function cert_makes() {
    jQuery('#make-select').html(cert_makes_html);
}



function all_models(make_selected_val) {
    if (all_models_html[make_selected_val]) {
        jQuery('#model-select').html(all_models_html[make_selected_val]);
    } else {
        jQuery('#model-select').html('<option value="0">--Model--</option>');
    }
}

function new_models(make_selected_val) {
    if (new_models_html[make_selected_val]) {
        jQuery('#model-select').html(new_models_html[make_selected_val]);
    } else {
        jQuery('#model-select').html('<option value="0">--Model--</option>');
    }
}

function used_models(make_selected_val) {
    if (used_models_html[make_selected_val]) {
        jQuery('#model-select').html(used_models_html[make_selected_val]);
    } else {
        jQuery('#model-select').html('<option value="0">--Model--</option>');
    }
}

function cert_models(make_selected_val) {
    if (cert_models_html[make_selected_val]) {
        jQuery('#model-select').html(cert_models_html[make_selected_val]);
    } else {
        jQuery('#model-select').html('<option value="0">--Model--</option>');
    }
}
