// JavaScript Document


// global flag
var isIE = false;

// global request and XML document objects
var req;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            //clearTopicList();
            buildTopicList();
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}

// invoked by "Category" select element change;
// loads chosen XML document, clears Topics select
// element, loads new items into Topics select element
function loadFxDoc() {

            try {
                loadXMLDoc("http://www.bermuda-bcb.com/ticker/action.aspx?get=fx");
            }
            catch(e) {
                var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
                alert("Unable to get XML data:\n" + msg);
                return;
            }
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

// empty Topics select list content
function clearTopicList() {
    var select = document.getElementById("topics");
    while (select.length > 0) {
        select.remove(0);
    }
}

// add item to select element the less
// elegant, but compatible way.
function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}

// fill Topics select list with items from
// the current XML document
function buildTopicList() {
   // var select = document.getElementById("topics");
    var items = req.responseXML.getElementsByTagName("fxrate");
	var tableBody = "";
	tableBody = tableBody + "<table cellpadding='0' cellspacing='0' width='170' align='center'>";
	tableBody = tableBody + "<tr bgcolor='#cccccc' height='18px'> ";
	
	
	for (j=0;j<items[0].childNodes.length;j++)
	{
		if (items[0].childNodes[j].nodeType != 1) continue;
		tableBody = tableBody + "<td align='center'>";
		tableBody = tableBody + "<font style='font-size:12px; font-weight:bold; color:#00467f;'>";
		tableBody = tableBody + items[0].childNodes[j].nodeName;
		tableBody = tableBody + "</font></td>";
	}
	
	tableBody = tableBody + "</tr>";
	tableBody = tableBody + "<tr><td>&nbsp;</td></tr>";
	for (i=0;i<items.length;i++)
	{
		tableBody = tableBody + "<tr height='18px'>";
		for (j=0;j<items[i].childNodes.length;j++)
		{
			if (items[i].childNodes[j].nodeType != 1) continue;
			
			tableBody = tableBody + "<td align='center'>";
			
			text=items[i].childNodes[j].firstChild.nodeValue
			tableBody = tableBody + text;
			tableBody = tableBody + "</td>";
			
		}
		tableBody = tableBody + "</tr>";
	}
	tableBody = tableBody + "</table>";
	
	
	document.getElementById("fxTable").innerHTML = tableBody;

}

/* OLD CODE - DIDN'T WORK IN SAFARI

function importXML()
{

try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e)
    {
    alert(e.message);
    return;
    }
  }
xmlDoc.async=false;
xmlDoc.load("./data/fx.xml");

var x = xmlDoc.getElementsByTagName('fxrate');
document.write("<table cellpadding='0' cellspacing='0' width='170' align='center'>");
document.write("<tr bgcolor='#cccccc' height='18px'> ");
	
	
	for (j=0;j<x[0].childNodes.length;j++)
	{
		if (x[0].childNodes[j].nodeType != 1) continue;
		document.write("<td align='center'>");
		document.write("<font style='font-size:12px; font-weight:bold; color:#00467f;'>");
		document.write(x[0].childNodes[j].nodeName);
		document.write("</font></td>");
	}
	
	document.write("</tr>");
	document.write("<tr><td>&nbsp;</td></tr>");
	for (i=0;i<x.length;i++)
	{
		document.write("<tr height='18px'>");
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;
			
			document.write("<td align='center'>");
			
			text=x[i].childNodes[j].firstChild.nodeValue
			document.write(text);
			document.write("</td>");
			
		}
		document.write("</tr>");
	}
	document.write("</table>");
}
*/