
function getElem(elName)
{
	return document.getElementById?document.getElementById(elName):null;
}

function gotosite(loc)
{
	if(loc != null && loc != '')
	{
		location.href = loc;
	}
}
var requests = new Object();
var xmlReqInProgress = false;
function RequestFactory()
{
	this.createRequest = function(rID, url, needsLoading)
	{
		var r;
		if(!xmlReqInProgress)
		{
			xmlReqInProgress = true;
			if(!requests[rID])
			{
				if(window.XMLHttpRequest)
				{
					r = new XMLHttpRequest();
					requests[rID] = new XMLRequest(r, needsLoading);
	        		requests[rID].req.open('GET', url, true);
					requests[rID].req.send(null);
				}
				else if(window.ActiveXObject)
				{
					r = new ActiveXObject('Microsoft.XMLHTTP');
					if(r)
					{
						requests[rID] = new XMLRequest(r, needsLoading);
						requests[rID].req.open('GET', url, true);
						requests[rID].req.send();
					}
				}
				else
				{
					requests[rID] = null;
				}
			}
			return requests[rID];
		}
		else
		{
			return null;
		}
	}
}
function getOffSets()
{
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	
	return new Array(x,y);
}
var selfXMLR;
function XMLRequest(req, needsLoading)
{
	selfXMLR = this;
	this.req = req;
	this.isInit = false;
	this.interval = 0;
	this.executing = false;
	this.isFirstRun = true;
	this.func = null;
	this.args = new Array();
	this.div = null;
	this.needsLoading = needsLoading;
	
	this.init = function()
	{
		if(this.req.readyState == 4)
		{
			if(this.req.status == 200)
			{
				this.isInit = true;
			}
		}
		
		if(!this.isInit)
		{
			if(this.isFirstRun)
			{
				this.interval = setInterval('selfXMLR.init()', 2);
			}
		}
		else
		{ 
			clearInterval(this.interval);
			if(this.executing)
			{
				this.execute();
			}
		}
		this.isFirstRun = false;
	}
	
	this.createLoadingHolder = function()
	{
		/*
		var body = document.getElementsByTagName('body')[0];
		var y = getOffSets()[1];
		this.div = document.createElement('div');
		this.div.setAttribute('id', 'loadingDiv');
		this.div.style.width = '100%';
		this.div.style.fontWeight = 'bold';
		this.div.style.backgroundColor = '#FFFFFF';
		this.div.style.color = '#000000';
		this.div.style.padding = '10px';
		this.div.style.display = 'none';
		this.div.style.position = 'absolute';
		this.div.style.top = y + 'px';
		this.div.style.left = '0px';
		this.div.style.zIndex = 100;
		this.div.appendChild(document.createTextNode('Loading...'));
		if(body)
		{
			body.appendChild(this.div);
		}
		*/
	}
	
	this.hideLoading = function()
	{
		//this.div.style.display = 'none';
	}
	
	this.showLoading = function()
	{
		//this.div.style.display = 'block';
	}
	
	this.createMethod = function(func, args)
	{
		this.func = func;
		this.args = args;
	}
	
	this.getXMLDoc = function()
	{
		return this.req.responseXML;
	}
	
	this.execute = function()
	{
		this.executing = true;
		if(this.needsLoading)
		{
			this.showLoading();
		}
		if(this.isInit)
		{
			clearInterval(this.interval);
			this.func.apply(this,this.args);
			this.hideLoading();
			xmlReqInProgress = false;
		}
		else
		{
			this.init();
		}
	}
	
	this.createLoadingHolder();
	this.init();
}
var rf = new RequestFactory();
var rque = new Array();
var ids = new Object();
var currentReq = null;

function proxyReq()
{
	this.url = null;
	this.id = null;
	this.nl = null;
	this.f = null;
	this.args = null;
}

function executeXML(url, id, needsLoading, func, args)
{
	var req = null;
	var newArgs;
	var success = false;
		
	if(id != null && id != '')
	{
		if(ids[id] == null)
		{
			ids[id] = new proxyReq();
			ids[id].url = url;
			ids[id].id = id;
			ids[id].nl = needsLoading;
			ids[id].f = func;		
			
			if(!args || typeof args != 'object')
			{
				newArgs = 'new Array(\'\')';
			}
			else
			{
				newArgs = 'new Array(';
				for(var i = 0; i < i.length; i++)
				{
					if(i < i.length -1)
					{
						newArgs += '\'' + args[i] + '\',';
					}
					else
					{
						newArgs += '\'' + args[i] + '\'';
					}
				}
				newArgs += ')';
			}
			ids[id].args = newArgs;
		}
		
		req = rf.createRequest(id, url, needsLoading);
		if(req != null)
		{
			req.createMethod(func,args);
			req.execute(req);
			success = true;
		}
		else if(ids[id] != null)
		{
			if(currentReq == ids[id] || currentReq == null)
			{
				setTimeout('executeXML(\'' + ids[id].url + '\',\'' + ids[id].id + '\',' + ids[id].nl + ',' + ids[id].f + ',' + ids[id].args + ');',2);
			}
		}
	}
	currentReq = ids[id];
	return success;
}

zSr = null;

var thisHack = new Object;
var hackCount = 0;

function makeChart(xmlLocation,holder)
{
	return new CodistaChart(xmlLocation,holder);
}

function CodistaChart(xmlLocation,holder)
{
	var func;
	if(hackCount == 0 || thisHack[hackCount].loaded)
	{
		hackCount++;
		thisHack[hackCount] = this;
		thisHack[hackCount].xmlLocation = xmlLocation;
		thisHack[hackCount].holder = holder;
		thisHack[hackCount].chartHTML = '';
		thisHack[hackCount].scale = 1;
		thisHack[hackCount].margin = 5;
		thisHack[hackCount].loaded = false;
		thisHack[hackCount].init();
	}
	else
	{
		setTimeout('makeChart(\'' + xmlLocation + '\', \'' + holder + '\');',1000);
	}
}

CodistaChart.prototype.init = function()
{
	executeXML(thisHack[hackCount].xmlLocation,thisHack[hackCount].xmlLocation,false,thisHack[hackCount].loadChart,[]);
}

CodistaChart.prototype.loadChart = function()
{
	var doc = this.getXMLDoc();
	var chart = doc.getElementsByTagName('chart')[0];
	var width = parseInt(chart.getAttribute('width'));
	var items = doc.getElementsByTagName('items');
	var legendEl = doc.getElementsByTagName('legend');
	var titleEl = doc.getElementsByTagName('title');
	var xLabels = doc.getElementsByTagName('xLabels')[0];
	var yLabels = doc.getElementsByTagName('yLabels')[0];
	var yMin = parseInt(chart.getAttribute('yMin'));
	var chartTextColor = chart.getAttribute('textColor');
	var chartBGColor = chart.getAttribute('bgColor');
	var chartBorderColor = chart.getAttribute('borderColor');
	var legendTextColor;
	var legendBGColor;
	var yMax = parseInt(chart.getAttribute('yMax'));
	var yLabelWidth = parseInt(yLabels.getAttribute('width'));
	var yAxis;
	var xAxis;
	var fontSize = parseInt(chart.getAttribute('fontSize'));
	var itemHTML;
	var legendWidth = 0;
	var legendHTML = '';
	var title = '';
	var marginTop = 20;
	var legend;
	
	thisHack[hackCount].scale = parseFloat(chart.getAttribute('yScale'));
	
	if(titleEl && titleEl.length > 0)
	{
		title = '<div style="left:' + (yLabelWidth + thisHack[hackCount].margin) +'px; position:absolute; top:-' + ((fontSize * 1.5) * 3) + 'px; text-align:center;width:' + (width - thisHack[hackCount].margin) + 'px;font-weight:bold;font-size:' + (fontSize * 1.5) + 'px;">' + titleEl[0].childNodes[0].nodeValue + '</div>';
		marginTop = (fontSize * 1.5) + 40;
	}	
	
	if(legendEl && legendEl.length > 0)
	{
		legend = legendEl[0];
		legendWidth = parseInt(legend.getAttribute('width'));
		width = width - legendWidth;
		legendHTML = thisHack[hackCount].getLegendHTML(legend,items,width);
	}
	
	yAxis = thisHack[hackCount].getYAxis(yLabels,yMax,yLabelWidth,width,fontSize,chartBorderColor);
	xAxis = thisHack[hackCount].getXAxis(xLabels,width,yLabelWidth,chartBorderColor);
	itemHTML = thisHack[hackCount].getItemHTML(xLabels,items,width,yLabelWidth,fontSize);
	
	thisHack[hackCount].chartHTML = title + xAxis + yAxis + itemHTML + legendHTML;
	
	thisHack[hackCount].fillChart(yMax,width,chartTextColor,chartBGColor,marginTop);
}

CodistaChart.prototype.getLegendHTML = function(legend,items,chartWidth)
{
	var textColor = legend.getAttribute('textColor');
	var bgColor = legend.getAttribute('bgColor');
	var borderColor = legend.getAttribute('borderColor');
	var width = parseInt(legend.getAttribute('width'));
	var result = '<div style="width:' + width + 'px; color:#' + textColor + '; background:#' + bgColor + '; border: 1px solid #' + borderColor + '; position:absolute; left:' + (chartWidth + (width/1.5)) +'px;">';
	var slug;
	var itemColor;
	
	for(var i=0; i<items.length; i++)
	{
		slug = items[i].getElementsByTagName('slug')[0].childNodes[0].nodeValue;
		itemColor = items[i].getAttribute('color');
		result += '<div style="font-weight:bold; margin:10px;"><div style="margin: 0 6px 0 0; float:left; width:12px; line-height: 12px; height: 12px; background:#' + itemColor + ';">&nbsp;</div><span style="line-height:12px;height: 12px; ">' + slug + '</span></div>';
	}
	
	result += '</div>';
	return result;
}

CodistaChart.prototype.getItemHTML = function(xLabels,items,width,start,fontSize)
{
	var barWidth;
	var color;
	var result = '<div style="position: absolute; bottom:1px; left:' + start + 'px; width:' + width + 'px;">';
	var loopNode;
	var xUnitSeperation;
	var length = 0;
	var count = 0;
	var barNumber = 0;
	var unitWidth;
	var xLabelPadding;
	var height;
	var leftPos;
	var labelText;
	var useableWidth = width - thisHack[hackCount].margin;
	
	for(var i=0; i<xLabels.childNodes.length; i++)
	{
		if(xLabels.childNodes[i].nodeName == 'label')
		{
			length++;
		}
	}
	
	xUnitSeperation = Math.floor(parseInt((useableWidth)/length));
	xLabelPadding = Math.floor((parseInt((useableWidth))/length)/10);
	unitWidth = xUnitSeperation - xLabelPadding;
	barWidth = unitWidth/items.length;
	
	for(var i=0; i<items.length; i++)
	{
		color = items[i].getAttribute('color');
		count = 0;
		for(var j=0; j<items[i].childNodes.length; j++)
		{
			if(items[i].childNodes[j].nodeName == 'item')
			{
				leftPos = ((barNumber * barWidth) + (xUnitSeperation * count)) + thisHack[hackCount].margin;
				height = items[i].childNodes[j].getAttribute('point');
				labelText = items[i].childNodes[j].childNodes[0].nodeValue;
				height = ((parseInt(height) * thisHack[hackCount].scale)-(fontSize/2));
				result += '<div style="background-color:#' + color + ';height:' + (height+4) + 'px; bottom:0; text-align:center; position:absolute;left: ' + leftPos + 'px; width: ' + barWidth + 'px;"></div>';
				result += '<div style="bottom:' + (height + 10) + 'px; text-align:center; position:absolute;left: ' + leftPos + 'px; width: ' + barWidth + 'px;" class="bi">' + labelText + '</div>';
				count++;
			}
		}
		barNumber++;
	}
	result += '</div>';
	
	return result;
}

CodistaChart.prototype.getXAxis = function(xLabels,width,start,borderColor)
{
	var color = xLabels.getAttribute('textColor');
	var result = '<div style="position: absolute; bottom:0; left:' + start + 'px; border-top:1px solid #' + borderColor + '; width:' + width + 'px; color:#' + color + ';">';
	var loopNode;
	var xUnitSeperation;
	var length = 0;
	var count = 0;
	var unitWidth;
	var xLabelPadding;
	var useableWidth = width - thisHack[hackCount].margin;
	
	for(var i=0; i<xLabels.childNodes.length; i++)
	{
		if(xLabels.childNodes[i].nodeName == 'label')
		{
			length++;
		}
	}
	
	xUnitSeperation = Math.floor(parseInt((useableWidth)/length));
	xLabelPadding = Math.floor((parseInt((useableWidth))/length)/10);
	unitWidth = xUnitSeperation - xLabelPadding;
	
	for(var i=0; i<xLabels.childNodes.length; i++)
	{
		if(xLabels.childNodes[i].nodeName == 'label')
		{
			result += '<div style="text-align:center; position:absolute;left: ' + ((xUnitSeperation * count) + thisHack[hackCount].margin) + 'px; width: ' + unitWidth + 'px;">' + xLabels.childNodes[i].childNodes[0].nodeValue + '</div>';
			count++;
		}
	}
	result += '</div>';
	
	return result;
}

CodistaChart.prototype.getYAxis = function(yLabels,height,width,chartWidth,fontSize,borderColor)
{
	var color = yLabels.getAttribute('textColor');
	var result = '<div style="position:absolute; bottom:0; width:' + width + 'px;border-right:1px solid #000; height:' + (parseInt(height) * thisHack[hackCount].scale) + 'px; color:#' + color + ';">';
	var point;
	var count = 0;
	
	for(var i=0; i<yLabels.childNodes.length; i++)
	{
		if(yLabels.childNodes[i].nodeName == 'label')
		{
			point = parseFloat(yLabels.childNodes[i].getAttribute('point'));
			result += '<div style="width:' + width + 'px; text-align:right; position:absolute; bottom: ' + ((point * thisHack[hackCount].scale)-(fontSize/2)) + 'px;">' + yLabels.childNodes[i].childNodes[0].nodeValue + '&nbsp</div>';
			if(count > 0)
			{
				result += '<div style="width:' + chartWidth + 'px; position:absolute; left:' + width + 'px; bottom: ' + (point * thisHack[hackCount].scale) + 'px; border-bottom:1px dashed #' + borderColor + ';"></div>';
			}
			count++;
		}
	}
	result += '</div>';
	
	return result;
}

CodistaChart.prototype.fillChart = function(height,width,textColor,bgColor,marginTop)
{
	var el = getElem(thisHack[hackCount].holder);
	if(el)
	{
		el.style.position = 'relative';
		el.style.height = (parseInt(height) * thisHack[hackCount].scale) + 'px';
		el.style.width = width + 'px';
		el.style.color = '#' + textColor;
		el.style.backgroundColor = '#' + bgColor;
		el.style.marginTop = marginTop + 'px';
		el.style.marginBottom = '20px';
		el.innerHTML = thisHack[hackCount].chartHTML;
		thisHack[hackCount].loaded = true;
	}
	else
	{
		setTimeout('thisHack[hackCount].fillChart(' + height + ');',1000);
	}
}