// Copyright 2008 Time.com

// Event handler for loading WEF Module as soon as the element wefSection is loaded
tii_callFunctionOnElementLoad('wefSection', importWEFXML);

// Link to XML file
function importWEFXML()
{
	var ajax = new ajaxObj ();
	ajax.setMimeType ('text/xml');
	ajax.doGet ('/time/global_business/gci.xml', createTable, 'xml');
}

function createTable(xmlDoc)
{
	var g = document.getElementById("wefTableBody");
	var x = xmlDoc.getElementsByTagName('country');
	var i;

	for (var i=0;i<x.length;i++)
	{
		var e = document.createElement("tr");
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;	
			var theData = x[i].childNodes[j].firstChild.nodeValue;
			var p = document.createElement("td");
			if (x[i].getAttribute('display')=="yes" && x[i].childNodes[j].tagName=="name")
			{
				//p.setAttribute("class","first");
				var q = document.createElement("a");	
				//Set an attribute to div
				q.setAttribute("href","/time/global_business/"+ x[i].getAttribute('id'));
				q.appendChild(document.createTextNode(theData));	
				p.appendChild(q);
			}
			else
			{
				p.appendChild(document.createTextNode(theData));
			}
			e.appendChild(p);
			g.appendChild(e);		
		}
	  }
}

function ajaxObj ()
{
	this.req = null;
	this.url = null;
	this.status = null;
	this.statusText = '';
	this.method = 'GET';
	this.async = true;
	this.dataPayload = null;
	this.readyState = null;
	this.responseText = null;
	this.responseXML = null;
	this.handleResp = null;
	this.responseFormat = 'text', // 'text', 'xml', 'object'
	this.mimeType = null;
	this.headers = [];
	this.init = function ()
	{
		var i = 0;
		var reqTry = [ 
			function() { return new XMLHttpRequest (); },
			function() { return new ActiveXObject ('Msxml2.XMLHTTP'); },
			function() { return new ActiveXObject ('Microsoft.XMLHTTP'); }];
			
		while (!this.req && (i < reqTry.length))
		{
			try
			{
				this.req = reqTry [i++] ();
			}
			catch (e) {}
		}
		return true;
	};
	this.doGet = function (url, handler, format)
	{
		this.url = url;
		this.handleResp = handler;
		this.responseFormat = format || 'text';
		this.doReq ();
	}
	this.doReq = function ()
	{
		if (!this.init ())
		{
			alert ('Could not create XMLHttpRequest object.');
			return;
		}
		this.req.open (this.method, this.url, this.async);
		if (this.mimeType)
		{
			try
			{
				req.overrideMimeType (this.mimeType);
			}
			catch (e)
			{
				// Couldn't override MIME type -- IE6 or Opera?
			}
		}
		var self = this; // Fix loss-of-scope in inner function
		this.req.onreadystatechange = function ()
		{
			var resp = null;
			if (self.req.readyState == 4)
			{
				switch (self.responseFormat)
				{
					case 'text':
						resp = self.req.responseText;
						break;
					case 'xml':
						resp = self.req.responseXML;
						break;
					case 'object':
						resp = req;
						break;
				}
				if (self.req.status >= 200 && self.req.status <= 299)
				{
					self.handleResp (resp);
					document.getElementById('wefSection').style.visibility = "visible";
				}
				else
				{
					self.handleErr (resp);
				}
			}
		}
		this.req.send (this.postData);
	};
	this.setMimeType = function (mimeType)
	{
		this.mimeType = mimeType;
	};
	this.handleErr = function () {};
	this.abort = function ()
	{
		if (this.req)
		{
			this.req.onreadystatechange = function () {};
			this.req.abort ();
			this.req = null;
		}
	};
}