function resetSelect(selectId, legend)
{
    var select = document.getElementById(selectId);
    select.options.length = 1;
    select.options[0] = new Option(legend, "");
    return select;
}

function SelectMakeResponseHandler(response, targetModel)
{
    var modelSelect = resetSelect(targetModel , "and choose a model");
    
    var models = eval('(' + response + ')');
    for(var modelName in models)
    {
        var id = models[modelName].id;
        var name = models[modelName].modelName;
        modelSelect.options[modelSelect.options.length] = new Option(name, id);
    }
    modelSelect.selectedIndex = 0;
    
}


function OnSelectMake(selectObj, targetModel)
{ 
    console.log(targetModel);
    var modelSelect = resetSelect(targetModel, "loading....");
    var idx = selectObj.selectedIndex; 
    var makeId = selectObj.options[idx].value; 
    $.ajax({
       type: "GET",
       url: ajaxUrl ,
       data: "makeId=" + makeId,
       success: function(response){SelectMakeResponseHandler(response, targetModel)}
    });
    
}

// select car form validation

$(document).ready(function() {

$('#frmQS select, #findAPrice select, #searchTargetPrice select, #selectVideo select, #searchNewsForm select, #selectPriceFinder select, #searchCO2Form select').each(function(idx, el) {
    el.selectedIndex = 0;
});

    $('#SelectForm').submit(function() {

        if ($('#makeId :selected').text() == 'Choose a make' || $('#modelId :selected').text() == 'and choose a model' || $('#makeId :selected').text() == 'loading....' || $('#modelId :selected').text() == 'loading....') {
            alert('Please select a make, model and version combination');
            return false;
        }
        if ($('#editionId :selected').text() == 'and choose a Version' || $('#editionId :selected').text() == 'loading....') {
            alert('Please select a make, model and version combination');
            return false;
        }

        if ($('#yearId') && ($('#yearId :selected').text() == 'loading....' || $('#yearId :selected').text() == 'and choose a year')) {
            alert('Please choose a year');
            return false;
        }

        if ($('#mileage').length) {
            if ($('#mileage').val() != "") {

                var theValue = $('#mileage').val();


                if (isNaN(theValue) || theValue < 0 || theValue > 999999) {
                    alert("Your mileage must be a whole number containing no commas or decimal points");
                    $('#mileage').focus();
                    return false;
                }
            }
        }

    }

		);



});


$(document).ready(function(){
var notePadHeight = $('.notePad:first').height();
$('.notePad:last').height(notePadHeight);
});

