// The document's ready handler
$(document).ready(function() {
    // set the data handler url here

    var serverUrl = "http://www.lendingtreeautos.com/"
  // var serverUrl = "http://65.200.128.163:8083/";
    var dataHandlerUrl = serverUrl + "DataHandler.ashx?callback=?";
    
    // initial the year dropdown list
     $.getJSON(dataHandlerUrl,
            function(json) {
                $.each(json, function(i){
                    $("<option value='" + json[i].id + "'>" + json[i].displayName + "</option>").appendTo(".yearInput");
                });                
        });

    // the event handler for year user input
    // it updates the make dropdown list
    $(".yearInput").change(function(event) {
        $(".makeInput").attr("disabled", true);
        $(".modelInput").attr("disabled", true);
        
        $(".makeInput").empty();
        $(".modelInput").empty();
            
        var value = $(this).val();
        
        if (value.length == 0) {
            return;
        }

        $.getJSON(dataHandlerUrl, { year: value },
            function(json) {
                $(".makeInput").empty();
                $("<option value=''>Select a make</option>").appendTo(".makeInput");
                $(".makeInput").attr("disabled", false);
                
                $.each(json, function(i){
                    $("<option value='" + json[i].id + "'>" + json[i].displayName + "</option>").appendTo(".makeInput");
                });                
        });            
    });    

    // the event handler for make user input
    // it updates the model dropdown list
    $(".makeInput").change(function(event) {
        $(".modelInput").attr("disabled", true);
        $(".modelInput").empty();
    
        var value = $(this).val();
        var yearValue = $(".yearInput").val();
        
        if (value.length == 0) {
            return;
        }
        
        $.getJSON(dataHandlerUrl, { year: yearValue, make: value },
            function(json){ 
                $(".modelInput").empty();
                $("<option value=''>Select a model</option>").appendTo(".modelInput");
                $(".modelInput").attr("disabled", false);
                        
                $.each(json, function(i){
                    $("<option value='" + json[i].id + "'>" + json[i].displayName + "</option>").appendTo(".modelInput");
                });
            });
    });
    
    $(".zipInput").keypress(function(event) {
        if(event.keyCode==13){return validateAndNavigate();}
    });
    
    // the event handler for "" image button
    // it redirects to the specified page
    $(".submitBtn").click(function(event) {
          return validateAndNavigate();
    });
});
function validateAndNavigate()
{
    if (validate())
    {
        navigateToResults();  
    }
    return false;
    
}
function validate() {

        if ($(".yearInput").val().length == 0) {
            alert('Please select the auto year.');
            return;
        }
        

        if ($(".makeInput").val() != null && $(".makeInput").val().length == 0) {
            alert('Please select a make.');
            return;
        }
        
        if ($(".modelInput").val() != null && $(".modelInput").val().length == 0) {
            alert('Please select a model.');
            return;
        }
        
        if ($(".zipInput").val() != null && $(".zipInput").val().length == 0) {
            alert('Please enter a zip code.');
            return;
        }
        
        var regEx_zipcode = /(^\d{5}$)/;
        var zipValue;
        zipValue = document.getElementById("getautoquotezip").value;
        
        if (!regEx_zipcode.test(zipValue))
        {
            alert ("Please enter a valid 5-digit ZIP Code.");
            return;
        }
        
        return true;  
}

function navigateToResults() {

    var url = "http://www.lendingtreeautos.com/NewCars/DealerQuote.aspx?"
            + "year=" + $(".yearInput option:selected").text()
            + "&make=" + $(".makeInput option:selected").text()
            + "&model=" + $(".modelInput option:selected").text()
            + "&zip=" + $(".zipInput").val()
            + "&Esource=3489680" ;
    window.open(url);  
}