﻿
// ajax call to get city and state from entered zipcode
function GetCity()
{
    //var zipBox = $get("zip");
    var zipBox = document.getElementById("zip");
    zipService.GetCityState(zipBox.value,OnRequestComplete);
}
// return function to fill in form fields from zip code call
function OnRequestComplete(result)
{
    //var lstCity = $get("lstCity");
    //var theState = $get("theState");
    var lstCity = document.getElementById("lstCity");
    var theState = document.getElementById("theState");
    
    lstCity.innerHTML = "";
    theState.value = "";
    
    for (var n = 0; n < result.length; n++)
    {
        var option = document.createElement("option");
        option.value = result[n].cityName;
        option.innerHTML = result[n].cityName;
        lstCity.appendChild(option);
        if (n == 0)
        {
            theState.value = result[n].stateAbbr;
        }
    }
    fillHidden();
}
// for error handling not implemented
function OnError(result)
{
    var lbl = $get("lblInfo");
    lbl.innerHTML = "<b>" + result.get_message() + "</b>";
}

// validate a proper zipcode
function valZip()
{
    var RegExp = /^\d{5}([\-]\d{4})?$/;
    var zipBox = document.getElementById("zip");
    
    if (!RegExp.test(zipBox.value))
    {
        alert("Invalid Zipcode");
        return false;
    }
    //return true;
}
function isZip(s) 
{

     // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

     if (!reZip.test(s)) {
          alert("Zip Code Is Not Valid");
          return false;
     }

    //return true;
}

// ajax call to get available years for a particular car maker
function GetYear(whichOne)
{
    if (whichOne == "1")
    {
        var indexValue = document.getElementById("make1").selectedIndex;
        var makeid = document.getElementById("make1");
        makemodelService.GetMakeYear(makeid.options[indexValue].value,OnRequestCompleteYear1);
    }
    else if (whichOne == "2")
    {
        var indexValue = document.getElementById("make2").selectedIndex;
        var makeid = document.getElementById("make2");
        makemodelService.GetMakeYear(makeid.options[indexValue].value,OnRequestCompleteYear2);
    }
    else if (whichOne == "3")
    {
        var indexValue = document.getElementById("make3").selectedIndex;
        var makeid = document.getElementById("make3");
        makemodelService.GetMakeYear(makeid.options[indexValue].value,OnRequestCompleteYear3);
    }
    else if (whichOne == "4")
    {
        var indexValue = document.getElementById("make4").selectedIndex;
        var makeid = document.getElementById("make4");
        makemodelService.GetMakeYear(makeid.options[indexValue].value,OnRequestCompleteYear4);
    }
    else
    {
        return false;
    }
}

function OnRequestCompleteYear1(result)
{
    //var lstCity = $get("lstCity");
    //var theState = $get("theState");
    var theyear = document.getElementById("year1");
    var themodel = document.getElementById("model1");
    
    theyear.innerHTML = "";
    themodel.innerHTML = "";
    
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    theyear.appendChild(option);
    for (var n = 0; n < result.length; n++)
    {
        var option = document.createElement("option");
        option.value = result[n].theYear;
        //option.value = "1";
        option.innerHTML = result[n].theYear;
        theyear.appendChild(option);
        
    }
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    themodel.appendChild(option);
    
    fillHiddenAuto('1');
}
function OnRequestCompleteYear2(result)
{
    //var lstCity = $get("lstCity");
    //var theState = $get("theState");
    var theyear = document.getElementById("year2");
    var themodel = document.getElementById("model2");
    
    theyear.innerHTML = "";
    themodel.innerHTML = "";
    
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    theyear.appendChild(option);
    for (var n = 0; n < result.length; n++)
    {
        var option = document.createElement("option");
        option.value = result[n].theYear;
        option.innerHTML = result[n].theYear;
        theyear.appendChild(option);
        
    }
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    themodel.appendChild(option);
    
    fillHiddenAuto('2');
}
function OnRequestCompleteYear3(result)
{
    //var lstCity = $get("lstCity");
    //var theState = $get("theState");
    var theyear = document.getElementById("year3");
    var themodel = document.getElementById("model3");
    
    theyear.innerHTML = "";
    themodel.innerHTML = "";
    
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    theyear.appendChild(option);
    for (var n = 0; n < result.length; n++)
    {
        var option = document.createElement("option");
        option.value = result[n].theYear;
        option.innerHTML = result[n].theYear;
        theyear.appendChild(option);
        
    }
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    themodel.appendChild(option);
    
    fillHiddenAuto('3');
}
function OnRequestCompleteYear4(result)
{
    //var lstCity = $get("lstCity");
    //var theState = $get("theState");
    var theyear = document.getElementById("year4");
    var themodel = document.getElementById("model4");
    
    theyear.innerHTML = "";
    themodel.innerHTML = "";
    
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    theyear.appendChild(option);
    for (var n = 0; n < result.length; n++)
    {
        var option = document.createElement("option");
        option.value = result[n].theYear;
        option.innerHTML = result[n].theYear;
        theyear.appendChild(option);
        
    }
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    themodel.appendChild(option);
    
    fillHiddenPurAuto();
}


// ajax call to get available models for a particular make and year
function GetModel(whichOne)
{
    if (whichOne == "1")
    {
        var indexValueM = document.getElementById("make1").selectedIndex;
        var makeid = document.getElementById("make1");
        var indexValueY = document.getElementById("year1").selectedIndex;
        var theyear = document.getElementById("year1");
        makemodelService.GetMakeModel(makeid.options[indexValueM].value,theyear.options[indexValueY].value, OnRequestCompleteYearM1);
    }
    else if (whichOne == "2")
    {
        var indexValueM = document.getElementById("make2").selectedIndex;
        var makeid = document.getElementById("make2");
        var indexValueY = document.getElementById("year2").selectedIndex;
        var theyear = document.getElementById("year2");
        makemodelService.GetMakeModel(makeid.options[indexValueM].value,theyear.options[indexValueY].value,OnRequestCompleteYearM2);
    }
    else if (whichOne == "3")
    {
        var indexValueM = document.getElementById("make3").selectedIndex;
        var makeid = document.getElementById("make3");
        var indexValueY = document.getElementById("year3").selectedIndex;
        var theyear = document.getElementById("year3");
        makemodelService.GetMakeModel(makeid.options[indexValueM].value,theyear.options[indexValueY].value,OnRequestCompleteYearM3);
    }
    else if (whichOne == "4")
    {
        var indexValueM = document.getElementById("make4").selectedIndex;
        var makeid = document.getElementById("make4");
        var indexValueY = document.getElementById("year4").selectedIndex;
        var theyear = document.getElementById("year4");
        makemodelService.GetMakeModel(makeid.options[indexValueM].value,theyear.options[indexValueY].value,OnRequestCompleteYearM4);
    }
    else
    {
        return false;
    }
}

function OnRequestCompleteYearM1(result)
{
    //var lstCity = $get("lstCity");
    //var theState = $get("theState");
    //var theyear = document.getElementById("year1");
    var themodel = document.getElementById("model1");
    
    //theyear.innerHTML = "";
    themodel.innerHTML = "";
    
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    themodel.appendChild(option);
    for (var n = 0; n < result.length; n++)
    {
        var option = document.createElement("option");
        option.value = result[n].modelID;
        option.innerHTML = result[n].modelName;
        themodel.appendChild(option);
        
    }
     
    fillHiddenAuto('1');
}
function OnRequestCompleteYearM2(result)
{
    //var lstCity = $get("lstCity");
    //var theState = $get("theState");
    //var theyear = document.getElementById("year2");
    var themodel = document.getElementById("model2");
    
    //theyear.innerHTML = "";
    themodel.innerHTML = "";
    
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    themodel.appendChild(option);
    for (var n = 0; n < result.length; n++)
    {
        var option = document.createElement("option");
        option.value = result[n].modelID;
        option.innerHTML = result[n].modelName;
        themodel.appendChild(option);
        
    }
     
    fillHiddenAuto('2');
}
function OnRequestCompleteYearM3(result)
{
    //var lstCity = $get("lstCity");
    //var theState = $get("theState");
    //var theyear = document.getElementById("year3");
    var themodel = document.getElementById("model3");
    
    //theyear.innerHTML = "";
    themodel.innerHTML = "";
    
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    themodel.appendChild(option);
    for (var n = 0; n < result.length; n++)
    {
        var option = document.createElement("option");
        option.value = result[n].modelID;
        option.innerHTML = result[n].modelName;
        themodel.appendChild(option);
        
    }
     
    fillHiddenAuto('3');
}
function OnRequestCompleteYearM4(result)
{
    //var lstCity = $get("lstCity");
    //var theState = $get("theState");
    //var theyear = document.getElementById("year3");
    var themodel = document.getElementById("model4");
    
    //theyear.innerHTML = "";
    themodel.innerHTML = "";
    
    var option = document.createElement("option");
    option.value = "";
    option.innerHTML = "Select One";
    themodel.appendChild(option);
    for (var n = 0; n < result.length; n++)
    {
        var option = document.createElement("option");
        option.value = result[n].modelID;
        option.innerHTML = result[n].modelName;
        themodel.appendChild(option);
        
    }
     
    fillHiddenPurAuto();
}