﻿
var RegionID = 0;
var CountyID = 0;
var TownID = 0;

var RegionComboBoxID = "AgencyRegionDropDown";
var CountyComboBoxID = "AgencyCountyDropDown";
var TownComboBoxID = "AgencyTownDropDown";


$(document).ready(function () {

    RegionID = $('#AgencyRegionDropDown option:selected').attr('value');
    CountyID = $('#AgencyCountyDropDown option:selected').attr('value');
    TownID = $('#AgencyTownDropDown option:selected').attr('value');

    // check that the dropdowns dont already contain values, if so call the ajax functions
    if ($('#AgencyRegionDropDown option:selected').text() != "Select Region") {

        // store the RegionID
        if (RegionID == 0) {
            RegionID = $('#AgencyRegionDropDown option:selected').attr('value');
        }
        if (RegionID > 0) {

            // remove the disabled attribute
            $('#AgencyCountyDropDown').removeAttr("disabled");

            // call the get Counties web service
            getCounties(RegionID, '#AgencyCountyDropDown');

            if (CountyID > 0) {

                // remove the disabled attribute
                $('#AgencyTownDropDown').removeAttr("disabled");

                // call the get towns web service
                getTowns(CountyID, '#AgencyTownDropDown');

            }
        }
    }
    else {
        getRegion('#AgencyRegionDropDown');
    }


    // click event for the region dropdown
    $("#AgencyRegionDropDown").click(function () {

        // region dropdown
        if (document.getElementById("AgencyRegionDropDown").options.length == 2) getRegion('#AgencyRegionDropDown');

        if (document.getElementById("AgencyCountyDropDown").options.length > 1) {

            // if the text box contains data, clear it and add the default option
            clearComboBox("AgencyCountyDropDown");

            // now add the default - "select a county" and make it disabled
            setDefaultAttribute("AgencyCountyDropDown", "Select a County");

            // add disabled attributes 
            $('#AgencyCountyDropDown').attr('disabled', 'disabled');

        }

        if (document.getElementById("AgencyTownDropDown").options.length > 1) {
            // if the town box contains data, clear it and add the default option
            clearComboBox("AgencyTownDropDown");

            // now add the default - "select a county" and make it disabled
            setDefaultAttribute("AgencyTownDropDown", "Select a Town");

            // add disabled attributes 
            $('#AgencyTownDropDown').attr('disabled', 'disabled');
        }
    });


    $("#AgencyCountyDropDown").click(function () {
        if (document.getElementById("AgencyTownDropDown").options.length > 1) {
            // if the town box contains data, clear it and add the default option
            clearComboBox("AgencyTownDropDown");

            // now add the default - "select a county" and make it disabled
            setDefaultAttribute("AgencyTownDropDown", "Select a Town");

            // add disabled attributes 
            $('#AgencyTownDropDown').attr('disabled', 'disabled');
        }
    });


    // region change function
    $("#AgencyRegionDropDown").change(function () {

        // store the RegionID from the dropdown
        RegionID = $('#AgencyRegionDropDown option:selected').val()

        // remove the disabled attribute
        var element = document.getElementById("AgencyCountyDropDown");
        element.removeAttribute("disabled");

        // call the get countys web service
        getCounties(RegionID, '#AgencyCountyDropDown');
    });

    // region change function
    $("#AgencyCountyDropDown").change(function () {

        // store the RegionID from the dropdown
        CountyID = $('#AgencyCountyDropDown option:selected').val()

        // remove the disabled attribute
        var element = document.getElementById("AgencyTownDropDown");
        element.removeAttribute("disabled");

        // call the get countys web service
        getTowns(CountyID, '#AgencyTownDropDown');
    });


});



// create the region dropdowns
function getRegion(RegionDropDownID) {

        $.ajax({
        type: "Get",
        url: "/RegionData/GetAllRegions",
        success: function (data) {

            $.each(data, function (key, value) {

                var region = value.Name;
                var regionID = value.ID;

                //console.log("region " + region);
                //console.log("regionID " + regionID);

                // create a new option object
                var newOption = document.createElement('option');
                var ValueAttr = document.createAttribute('value');

                ValueAttr.value = regionID;

                newOption.setAttributeNode(ValueAttr);
                newOption.innerHTML = region;

            $(RegionDropDownID).append(newOption);

            });
        },
        error: function () { },
        beforeSend: function () { },
        complete: function () { },
        dataType: "json",
        cache: false
        });
    }

function clearDuplicate(ID) {
    document.getElementById(ID).options[1] = null;
}

function getCounties(RegionID, countyDropDownID) {
        $.ajax({
        type: "Get",
        url: "/RegionData/GetCountyByRegionID/" + RegionID,
        success: function (data) {

            $.each(data, function (key, value) {

                // store the name of the counties
                var countyName = value.Name;
                var countyID = value.ID;

                // create a new option object
                var newOption = document.createElement('option');
                var ValueAttr = document.createAttribute('value');
                ValueAttr.value = countyID;
                newOption.setAttributeNode(ValueAttr);

                newOption.innerHTML = countyName;

            $(countyDropDownID).append(newOption);
        });
    },
    error: function () { },
    beforeSend: function () { },
    complete: function () { },
    dataType: "json",
    cache: false
});
}

function getTowns(CountyID, townDropDownID) {

        $.ajax({
        type: "Get",
        url: "/RegionData/GetTownByCountyID/" + CountyID,
        success: function (data) {

            $.each(data, function (key, value) {

            // store the name of the counties
            var townName = value.Name;
            var TownID = value.ID;

            // create a new option object
            var newOption = document.createElement('option');
            var ValueAttr = document.createAttribute('value');
            ValueAttr.value = TownID;
            newOption.setAttributeNode(ValueAttr);

            newOption.innerHTML = townName;

            $(townDropDownID).append(newOption);

        });
    },
    error: function () { },
    beforeSend: function () { },
    complete: function () { },
    dataType: "json",
    cache: false
    });
}

function clearComboBox(BoxToClear) {
    for (var i = document.getElementById(BoxToClear).options.length; i > 0; i--) {
        document.getElementById(BoxToClear).options[i - 1] = null;
    }
}

function setDefaultAttribute(box, stringName) {
    // now add the default - "select a county" and make it disabled
    var newOption = document.createElement('option');
    var ValueAttr = document.createAttribute('value');
    ValueAttr.value = 0;
    newOption.innerHTML = stringName;
    newOption.setAttributeNode(ValueAttr);
    $('#' + box).append(newOption);
}
