function ClearZipText(strOriginalText) {
    if (document.AbtForm.zipCode.value == strOriginalText) {
        document.AbtForm.zipCode.value = "";
    }
}

function RestoreZipText(strOriginalText) {
    if (document.AbtForm.zipCode.value === "") {
        document.AbtForm.zipCode.value = strOriginalText;
    }
}
function getId(str) {
    pos = str.indexOf("|");
    if(-1 !== pos) {
        return str.substring(0,pos);
    }
    return str;
}

function getSeoName(str) {
    pos = str.indexOf("|");
    if(-1 !== pos) {
        return str.substring(pos+1);
    }
    return str;
}

function validate() {
    var passed = true;
    //make sure the car is selected
    var yearSelect = $('#af_year_cc').get(0);
    if( !yearSelect.selectedIndex) {
        passed = false;
    }
    //make sure we have five digits for zip
    if($('#zipCode').get(0)) {
        var zipRE=/^\d{5}$/;
        if($('#zipCode').val().search(zipRE)===-1) {
            passed = false;
        }
    }
    if( !passed) {
        alert("Please select make/model/year and zip code !");
    } else {
        $('#year').val($('#af_year_cc option:selected').val());
        $('#make').val(getSeoName($('#af_make_cc option:selected').val()));
        $('#model').val(getSeoName($('#af_model_cc option:selected').val()));
        $('#hiddenAction').val('Ping');
    }
    return passed;
}

$(document).ready(function() {
	$("#af_make_cc").change(function(){
        $('#make').val(getId($('#af_make_cc option:selected').val()));
        $('#model').val('');
        $("#af_model_cc").get(0).options.length = 1;
        $("#af_year_cc").get(0).options.length = 1;
        submitDQForm();
    });
    $("#af_model_cc").change(function(){
        $('#make').val(getId($('#af_make_cc option:selected').val()));
        $('#model').val(getId($('#af_model_cc option:selected').val()));
        $("#af_year_cc").get(0).options.length = 1;
        submitDQForm();
    });

    $('#zipCode').keypress( function(evt) {
        var charCd = (evt.which !== null) ? evt.which : event.keyCode;
        if (charCd > 31 && (charCd < 48 || charCd > 57)) {
            return false;
        }
        return true;
    });

    function submitDQForm() {
        $('#AbtForm').ajaxSubmit({
            success: function(responseText, statusText){
                data = eval("("+responseText+")");
                var modelSelect = $("#af_model_cc").get(0);
                var yearSelect = $("#af_year_cc").get(0);
                var i;
                if(data.models) {
                    var models = data.models;
                    yearSelect.options.length = 1;
                    modelSelect.options.length = 1;
                    for(i in models) {
                        if(models.hasOwnProperty(i)) {
                            modelSelect.options[modelSelect.options.length] = new Option(models[i], i);
                        }
                    }
                } else {
                    var years = data.years;
                    for(i in years) {
                        if(years.hasOwnProperty(i)) {
                            yearSelect.options[yearSelect.options.length] = new Option(years[i], years[i]);
                        }
                    }
                }
            }
        });
    }
});