﻿function AjaxLocationItems(AjaxHandlerPath) {
    this.RequestUrl = AjaxHandlerPath;
    this.objXmlHttp;
    this.getHttpObject = getHttpObject;
    this.getResponseXml = getResponseXml;
}

function getHttpObject() {
    try {
        this.objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
        try {
            this.objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            this.objXmlHttp = new XMLHttpRequest();
        }
    }
}

function getResponseXml(paramValues) {
    var XmlDataObject;

    this.getHttpObject();
    if (this.objXmlHttp) {
        this.objXmlHttp.open("POST", this.RequestUrl, false);
        this.objXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        this.objXmlHttp.send(paramValues);

        XmlDataObject = this.objXmlHttp;
    }
    return XmlDataObject;
}

function GetCountryItems(ActivityId, ddlCountries) {
    ddlCountries.options.length = 0;
    ddlCountries[0] = new Option("Any country", "");
    ddlCountries.disabled = true;

    if (ActivityId.length > 0) {
        var Url = "/ActivityCountries.ashx";

        var oAjax = new AjaxLocationItems(Url);
        var XmlDataObject = oAjax.getResponseXml("activityid=" + ActivityId);
        var XmlContent = XmlDataObject.responseXML;

        // Parse the Xml and load the dropdown
        try {
            var XMLRS = XmlContent.documentElement.getElementsByTagName("Country");

            if (XMLRS.length > 0) {
                var Id, Name;
                for (i = 0; i < XMLRS.length; i++) {
                    Id = XMLRS[i].childNodes[0].childNodes[0].nodeValue;
                    Name = XMLRS[i].childNodes[1].childNodes[0].nodeValue;
                    ddlCountries[i + 1] = new Option(Name, Id);
                }

                ddlCountries.disabled = false;
            }
        }
        catch (err) {
            ddlCountries.disabled = true
        }
    }
}
