﻿var xmlCountries = "/bunzl/siteware/xml_countries";
var xmlCompanies = "/bunzl/siteware/xml_companies2";

//var xmlCountries = "/BunzlDropdown/Countries.xml";
//var xmlCompanies = "/BunzlDropdown/Companies.xml";

var Countries = new Array();
var Companies = new Array();

var xmlhttpCountries = null;
var xmlhttpCompanies = null;

function loadXmlCountries(xmlFile)
{
    if (window.XMLHttpRequest)
    {
        // code for all new browsers
        xmlhttpCountries = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE5 and IE6
        xmlhttpCountries = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if (xmlhttpCountries != null)
    {
        xmlhttpCountries.onreadystatechange = parseCountries;            
        xmlhttpCountries.open("GET",xmlFile,true);
        xmlhttpCountries.send(null);
    }
    else
    {
        alert("Your browser does not support XMLHTTP.");
    }
}

function parseCountries()
{
    if (xmlhttpCountries.readyState == 4)
    {

        if (xmlhttpCountries.status == 200)
        {
            var domCountries = xmlhttpCountries.responseXML;
            populateCountries(domCountries);
        }
        else
        {
            alert("Problem retrieving XML data");
        }
    }
}

function parseCompanies()
{     
    if (xmlhttpCompanies.readyState == 4)
    {
        if (xmlhttpCompanies.status == 200)
        {
            var domCompanies = xmlhttpCompanies.responseXML;
            populateCompanies(domCompanies);
        }
        else
        {
            alert("Problem retrieving XML data");
        }
    }
}

function loadXmlCompanies(xmlFile)
{
    if (window.XMLHttpRequest)
    {
        // code for all new browsers
        xmlhttpCompanies = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE5 and IE6
        xmlhttpCompanies = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if (xmlhttpCompanies != null)
    {
        xmlhttpCompanies.onreadystatechange = parseCompanies;
        xmlhttpCompanies.open("GET",xmlFile,true);
        xmlhttpCompanies.send(null);
    }
    else
    {
        alert("Your browser does not support XMLHTTP.");
    }
}

function country(nm)
{
    this.name = nm;
    this.sectors = null;
}

function company(nm, uri)
{
    this.name = nm;
    this.url = uri;
    this.countries = null;
    this.sectors = null;
}

function populateCountries(xdoc)
{    
    setGoto(false);

    var rootnode = xdoc.documentElement;

    var ddCompanies = document.getElementById("cbCompanies");

    ddCompanies.options.length = 0;    
    ddCompanies.options[0] = new Option("N/A");
    
    var ddSectors = document.getElementById("cbSectors");

    ddSectors.options.length = 0;
    ddSectors.options[0] = new Option("N/A");
      
    var ddCountries = document.getElementById("cbCountries");
    ddCountries.options.length = 0;
    var num = 0;
       
	for (i = 0; i < rootnode.childNodes.length; i++)
	{
	    var node = rootnode.childNodes[i];
		var ddCountry = document.createElement('option');
        
        if (node.tagName == "Country")
        {
            var Country = new country(node.attributes[0].nodeValue);
            Country.sectors = new Array();
            
            for (j = 0; j < node.childNodes.length; j++)
            {
                var child = node.childNodes[j];
                
				if (child.tagName == "Sector")
                    Country.sectors.push(child.firstChild.data);
            }
            
            ddCountries.options[num] = new Option(Country.name, Country.name);
            num = num + 1;
            Countries.push(Country);
        }        
	}
}

function populateSectors()
{
    var eleSource = document.getElementById("cbCountries");
    var eleValue = eleSource.options[eleSource.selectedIndex].text;
    
    var eleTarget = document.getElementById("cbSectors");
    eleTarget.options.length = 0;
    
    switch (eleValue)
    {
        case "Select a country...":
            var eleSectors = document.getElementById("cbSectors");
            eleSectors.options.length = 0;
            eleSectors.options[0] = new Option("N/A");
            
            eleSectors = document.getElementById("cbCompanies");
            eleSectors.options.length = 0;
            eleSectors.options[0] = new Option("N/A");
            
            setGoto(false);
            break;
        
        default:
            for (i = 0; i < Countries.length; i++)
            {
                if (Countries[i].name == eleValue)
                {
                    if (Countries[i].sectors.length <= 0)
                        eleTarget.options[0] = new Option("Not found");
                        
                    for (j = 0; j < Countries[i].sectors.length; j++)
                        eleTarget.options[j] = new Option(Countries[i].sectors[j]);
                }
            }    
        
    }
    
    ddSectorsOnClick()
}

function populateCompanies(xdoc)
{
    var root = xdoc.documentElement;  

    for (i = 0; i < root.childNodes.length; i++)
    {
        var node = root.childNodes[i];
                
        if (node.tagName == "Company")
        {          
            var Company = new company(node.attributes[0].nodeValue, node.attributes[1].nodeValue);
            
            Company.countries = new Array();
            Company.sectors = new Array();
            
            for (j = 0; j < node.childNodes.length; j++)
            {
                var child = node.childNodes[j];

                if (child.tagName == "Country")
                    Company.countries.push(child.attributes[0].nodeValue);
                
                if (child.tagName == "Sector")
                    Company.sectors.push(child.attributes[0].nodeValue);
            }
            
            Companies.push(Company);
        }
    }
}

function ddSectorsOnClick()
{
    var eleCountries = document.getElementById("cbCountries");
    var eleSectors = document.getElementById("cbSectors");
    var eleCompanies = document.getElementById("cbCompanies");
    
    var selCountry = eleCountries.options[eleCountries.selectedIndex].text;
    var selSector = eleSectors.options[eleSectors.selectedIndex].text;
    
    if (selCountry != "Select a country..." && selCountry != "" && selSector != "" && selSector != "N/A")
    {
        eleCompanies.options.length = 0;
            
        for (i = 0; i < Companies.length; i++)
        {
            var countryFound = false;
            var sectorFound = false;
            
            for (j = 0; j < Companies[i].countries.length; j++)
            {
                if (selCountry == Companies[i].countries[j])
                    countryFound = true;
            }
            
            for (j = 0; j < Companies[i].sectors.length; j++)
            {
                if (selSector == Companies[i].sectors[j])
                    sectorFound = true;
            }
            
            if (countryFound && sectorFound)
            {
                eleCompanies.options[eleCompanies.options.length] = new Option(Companies[i].name, Companies[i].name);
                setGoto(true);
            }
        }
    }
    
    if (eleCompanies.options.length <= 0)
        setGoto(false);
}

function btnGotoOnClick()
{
    var url = null;
    
    var eleCompanies = document.getElementById("cbCompanies");
    var selCompany = eleCompanies.options[eleCompanies.selectedIndex].text;
    
    var eleCountries = document.getElementById("cbCountries");
    var selCountry = eleCountries.options[eleCountries.selectedIndex].text;
    
    for (i = 0; i < Companies.length; i++)
    {
        for (j = 0; j < Companies[i].countries.length; j++)
        {
            if ((selCompany == Companies[i].name) && (selCountry == Companies[i].countries[j]))
            {
                url = Companies[i].url;
            }
        }
    }
    if (url != null)
        window.open(url);    
}

function setGoto(b)
{
    var btn = document.getElementById("btnGoto");
    btn.disabled = !b;
}

function initPage(ddCountries, ddSectors, ddCompanies, btnGoto)
{
    loadXmlCountries(xmlCountries);
    loadXmlCompanies(xmlCompanies);
}
