// Copyright 2007 Time.com

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {importXML()}); 

function importXML()
{
	var ajax = new ajaxObj ();
	ajax.setMimeType ('text/xml');
	ajax.doGet ('/time/global_business/problematic_factors.xml', createGraph, 'xml');
}

var first=true;
var append=true;

function createGraph(xmlDoc)
{		
	var x = xmlDoc.getElementsByTagName('country');
	var i;
	if (first)
	{
		var countryName = document.getElementById('countryName').firstChild.nodeValue;
		for (r=0;r<x.length;r++)
		{
			var xmlCountryValue = x[r].attributes.getNamedItem('value').value;			
			var xmlCountryName = x[r].attributes.getNamedItem('name').value;		
			if (countryName==xmlCountryName)
			{
				i = xmlCountryValue;	
			}
		}
	}
	else
	{
		if (append)
		{
			appendDiv();
		}
		append=false;
		i = document.getElementById('selCountry').value;
		for (r=0;r<x.length;r++)
		{
			var xmlCountryValue = x[r].attributes.getNamedItem('value').value;			
			var xmlCountryName = x[r].attributes.getNamedItem('name').value;		
			if (i==xmlCountryValue)
			{
				var selCName = xmlCountryName;	
				var c = document.getElementById('compareCountry');
				c.style.visibility = 'visible';				
				c.innerHTML = selCName;
			}
		}		
	}
	// Iterate through child nodes
	var l=0, t=0;
	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;
		if (first)
		{
		var t = 'b'+l;
		}
		else
		{
		var t = 'c'+l;	
		}
		var y = document.getElementById(t);
		y.style.width = parseInt(theData*4.2)+'px';
		l++;
	}
}
function createDiv()
{			 
	var g = document.getElementById('compareGraph');	
	//Create first bar
	for (var k=0;k<14;k++)
	{
		var d = document.createElement('div');
		//Set an attribute to div
		d.id = 'g'+k;
		d.className = 'compare' ;

		var e = document.createElement('div');
		//Set an attribute to div
		e.id = 'b'+k;
		e.className = 'col1';			

		importXML();
		d.appendChild(e);
		g.appendChild(d);
	}	
}

function appendDiv()
{
	//Create second bar and append to first bar
	for (var m=0;m<14;m++)
	{
		var n = document.createElement('div');
		n.id = 'c'+m;
		n.className = 'col2';			
		var h = document.getElementById('g'+m);	
		h.appendChild(n);
	}
}


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);
				}
				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;
		}
	};
}
