﻿function GetCountriesByContinent(continent) {
    var xhReq = GetXMLHttpRequest();
    xhReq.open("GET", "WebModelingSearchMethods.asmx/GetCountriesByContinent?continent=" + continent, true);
    xhReq.onreadystatechange = function() {
        if (xhReq.readyState != 4) { return; }
        else {
            if (xhReq.status == 200) {
                try {
                    var selection = document.getElementById("Country");

                    if (selection == null) {
                        // no country selection found...
                    }
                    else {
                        selection.innerHTML = "";
                        var oRoot = xhReq.responseXML;

                        for (i = 0; i < oRoot.getElementsByTagName("ProfileFieldValues").length; i++) {

                            var value = "";
                            var text = "";
                            if (navigator.appName == "Microsoft Internet Explorer") {
                                value = oRoot.getElementsByTagName("ProfileFieldValues")[i].childNodes[0].childNodes[0].nodeValue;
                                text = oRoot.getElementsByTagName("ProfileFieldValues")[i].childNodes[1].childNodes[0].nodeValue;
                            }
                            else {
                                value = oRoot.getElementsByTagName("ProfileFieldValues")[i].childNodes[1].childNodes[0].nodeValue;
                                text = oRoot.getElementsByTagName("ProfileFieldValues")[i].childNodes[3].childNodes[0].nodeValue;
                            }

                            var optn = document.createElement("OPTION");
                            optn.text = text;
                            optn.value = value;
                            selection.options.add(optn);
                        }
                    }

                    OnChangeCountryEvent();
                }
                catch (oError) {
                    alert("Error while parsing xml in GetCountriesByContinent. msg=" + oError.message);
                }
            }
        }
    }
    xhReq.send(null);
}

function OnChangeContinentEvent() {
    var selection = document.getElementById("Continent");

    if (selection == null) {
        // wrong!
    }
    else {
        GetCountriesByContinent(selection.value);
    }
}

function OnChangeCountryEvent() {
    var selection = document.getElementById("Country");

    if (selection == null) {
        // wrong!
    }
    else {
        var stateRow = document.getElementById("State");

        if (stateRow != null) {
            stateRow.disabled = true;

            if (selection.value == "US") {
                stateRow.disabled = false;
            }
        }
    }
}

function GetFieldValues() {

    GetCountriesByContinent("Asia");
}
