﻿var cascadingDropDown_Range_OnPopulatedHandlerAdded = false;

function AddCascadingDropDown_New_Range_OnPopulatedHandler() {    
    
    if (!cascadingDropDown_Range_OnPopulatedHandlerAdded) {

        var cascadingDropDown_Range = $find('Behavoir_CascadingDropDown_New_Range');
        cascadingDropDown_Range.add_populated(CascadingDropDown_New_Range_OnPopulated);

        cascadingDropDown_Range_OnPopulatedHandlerAdded = true;
    }
}

function MakeModelChanged()
{
    var Brand = document.getElementById("ctl00_ContentPlaceHolder_MainContent_Hidden_Make").value;
    var Make = Brand;
    
    if (document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Make") != null)
    {
        Make = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Make").value;
    }
    
    var Range = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Range").value;
    var Distance = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Distance").value;
    
    VehicleSearch.GetVehicleCount(Brand, Make, Range, '', '', '', '', '', '', '', '', true, Distance, ShowVehicleCount);
}

function SearchCritiriaChanged()
{
    var Brand = document.getElementById("ctl00_ContentPlaceHolder_MainContent_Hidden_Make").value;
    var Make = Brand;
    
    if (document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Make") != null)
    {
        Make = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Make").value;
    }
    
    var Range = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Range").value;
    var BodyStyle = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Style").value;
    var TransmissionType = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Transmission").value;
    var FuelType = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Fuel").value;
    var MinPrice = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_MinPrice").value;
    var MaxPrice = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_MaxPrice").value;
    var Age = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Age").value;
    var Mileage = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Mileage").value;
    var Keyword = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Keyword").value;    
    var Distance = document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Distance").value;    
    
    VehicleSearch.GetVehicleCount(Brand, Make, Range, BodyStyle, TransmissionType, FuelType, MinPrice, MaxPrice, Age, Mileage, Keyword, true, Distance, ShowVehicleCount);
}

function ShowVehicleCount(_Result)
{
    document.getElementById("ctl00_ContentPlaceHolder_MainContent_Label_VehicleCount").innerHTML = _Result;
}

function ValidateUsedSearch()
{
    if (document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Range").value == "")
    {
        alert("Please select a Model");
        return false;
    }
    
    if(document.getElementById("ctl00_ContentPlaceHolder_MainContent_txtPostCode").value == "" || document.getElementById("ctl00_ContentPlaceHolder_MainContent_txtPostCode").value == "Postcode")
    {
        alert("Please enter a postcode");
        return false;
    }
}

function ValidateNewSearch()
{
    if (document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_New_Range").value == "")
    {
        alert("Please select a Model");
        return false;
    }

    if (document.getElementById("ctl00_ContentPlaceHolder_MainContent_txtPostCode_NewCars").value == ""
        || document.getElementById("ctl00_ContentPlaceHolder_MainContent_txtPostCode_NewCars").value == "Postcode") {
        alert("Please enter a postcode");
        return false;
    }
}

function ResetRange()
{
    document.getElementById("ctl00_ContentPlaceHolder_MainContent_DropDownList_Range").value = "";
}
