/*** ITU Country Code List
 * (c) 2010 by IceDragon of QuickFox.org
 *
 * <icedragon@quickfox.org>
 * http://www.icerealm.org/
 */

 
//--- WRAPPERS --------------------------------------------------------------//
// Get an AJAX object or FALSE if the browser doesn't support them.
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
		return new XMLHttpRequest()
		
	// Old IE uses this instead.
	else if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP") // Old IE browsers
	
	return false
}

// Add an event handler to an object.
// NOTE: Using names like "onload" and "onclick" instead of "click" and "load"!
function AddEventListener(obj, evtName, func, capture)
{
	if (capture == null)
		capture = false
	
	// W3C
	if (obj.addEventListener)
		return obj.addEventListener(evtName.substr(2), func, capture)
	
	// Microsoft
	else if (obj.attachEvent)
		return obj.attachEvent(evtName, func)
	
	// Unable to comply
	else
		return false
}
//---------------------------------------------------------------------------//


var g_XmlHttp = null;
var g_CountryList = new Array();
var g_UpdateTimer = null;

function GetXmlFile()
{
    g_XmlHttp = GetXmlHttpObject()
    g_XmlHttp.onreadystatechange = function()
    {
        if (g_XmlHttp.readyState == 4)
        {
            ProcessItuData(g_XmlHttp.responseXML.documentElement)
            DisplayItuData(g_CountryList)
        }
    }
    g_XmlHttp.open("GET", "/lookup/itudb.xml", true)
    g_XmlHttp.send(null)
}

function ProcessItuData(xmlData)
{
    g_CountryList = new Array()
    
    var l = xmlData.getElementsByTagName("dict")
    for (var i = 0; i < l.length; i++)
    {
        var dict = l[i]
        var country = new Array()
        
        for (var node = dict.firstChild; node; node = node.nextSibling)
        {
            if (node.tagName == undefined)
                continue
            country.push(node.tagName)
            country.push(document.all ? node.innerText : node.textContent)
        }
        
        g_CountryList.push(country)
    }
}

function DisplayItuData(countryList)
{
    var div = document.getElementById("itu")
    var buffer = "<table id='itu_table'><thead><tr><th>Country Name</th><th>IANA Code</th><th>Int.Prefix</th><th>ITU Code</th><th>Nat.Prefix</th></tr></thead><tbody>"

    for (var i = 0; i < countryList.length; i++)
    {
        var country = countryList[i]
        var hilight = ((i % 2) > 0) ? "class='hl'" : ""

        buffer += "<tr " + hilight + ">"
        for (var j = 1; j < country.length; j += 2)
        {
            var align = (j == 1) ? "align='left'" : ""
            buffer += "<td " + align + ">" + country[j] + "</td>"
        }
        buffer += "</tr>"
    }

    buffer += "</tbody></table>"
    div.innerHTML = buffer
}

function FindItuDataByCountry(country)
{
    var found = new Array()
    
    for (var i = 0; i < g_CountryList.length; i++)
    {
        var countryElem = g_CountryList[i]
        var countryName = GetKeyFromCountry(countryElem, "fullname")
        
        if (country.toLowerCase() == countryName.toLowerCase().substring(0,country.length))
            found.push(countryElem)
    }
    
    return found
}

function FindItuDataByCode(itucode)
{
    var found = new Array()
    
    for (var i = 0; i < g_CountryList.length; i++)
    {
        var countryElem = g_CountryList[i]
        var itu = GetKeyFromCountry(countryElem, "itucode")
        
        if (itucode == itu.substring(0,itucode.length))
            found.push(countryElem)
    }
    
    return found
}

function GetKeyFromCountry(country, key, failover)
{
    for (var i = 0; i < country.length; i += 2)
    {
        if (country[i] == key)
            return country[i+1]
    }
    
    return failover
}

function GetNumericString(s)
{
    result = ""
    for (var i = 0; i < s.length; i++)
        if (s.charCodeAt(i) >= 0x30 && s.charCodeAt(i) <= 0x39)
            result += s[i]
    return result
}

// Clear the lookup textbox and focus on it.
function doResetLookup()
{
    tbLookup = document.getElementById("lookup")
    tbLookup.value = ""
    tbLookup.focus()
    doUpdateTable()
}

// Reset the table update timer.
function doResetTimer()
{
    clearTimeout(g_UpdateTimer)
    g_UpdateTimer = setTimeout("doUpdateTable()", 500)
}

// Update the lookup table based on what's in lookup controls.
function doUpdateTable()
{
    var mode = (document.getElementById("phone").checked) ? "phone" : "country"
    var lookup = document.getElementById("lookup").value

    if (mode == "phone")
    {
        var numeric = GetNumericString(lookup)
        DisplayItuData((numeric != "") ? FindItuDataByCode(numeric) : g_CountryList)
    }
    else
    {
        DisplayItuData((lookup != "") ? FindItuDataByCountry(lookup) : g_CountryList)
    }
}

AddEventListener(window, "onload", GetXmlFile, false)

