﻿var locationData = "";
var noJQdata = "";

$(document).ready(function () {
    GetRegions(); 
});


function GetRegions() {
    $.when(AJAX("RegionsPage.aspx/GetRegions", "{}")).done(function (data) {
        $.when(renderRegions(data)).done(function() {
            GetLocations();
        });
    })
};

function GetLocations() {
    $.when(AJAX("LocationsPage.aspx/GetLocations", '{regionValue: "' + document.getElementById('MainContent_Regions').value + '"}')).done(function (data) {
        $.when(renderLocations(data)).done(function () {
            Description(document.getElementById('MainContent_Locations').value);
        });
    });
};

function renderRegions(data) {
    $('#MainContent_Regions').children().remove();
    $.each(data, function (key, item) {
        for (i in item) {
            $('#MainContent_Regions').append('<option Value="' + item[i].regionValue + '">' + item[i].regionName + '</option>')
        }
    });
};

function renderLocations(data) {
    $('#MainContent_Locations').children().remove();
    $.each(data, function (key, item) {
        locationData = item;
        for (i in item) {
            $('#MainContent_Locations').append('<option Value="' + item[i].Value + '">' + item[i].Name + '</option>')
        }
    });

};

function Description(locationValue) {
    for (i in locationData) {
        if (locationData[i].Value === locationValue) {
            $('#description').html(locationData[i].Description);
            break;
        }
    };
};

function AJAX(urlMethod, dataTransfer) {
    return $.ajax({
        type: "POST",
        url: urlMethod,
        data: dataTransfer,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        failure: function (response) {
            alert(response.d);
        }
    });
};

function noJQajax(urlMethod, dataTransfer) {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (request.readyState == XMLHttpRequest.DONE) {
            return request.responseText;
        }
    }
    request.open('POST', urlMethod, true);
    request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
    request.send(dataTransfer);
};
@@ -930,32 +930,115 @@
       %7D%0A    %7D);%0A
+    $('#MainContent_Regions').change(function () %7B%0A        GetLocations();%0A    %7D);%0A
 %7D;%0A%0Afunction ren
@@ -1341,16 +1341,154 @@
 %0A    %7D);
+%0A    $('#MainContent_Locations').change(function () %7B%0A        Description(document.getElementById('MainContent_Locations').value);%0A    %7D);
 %0A%0A%7D;%0A%0Afu

update on change