var leftselector;
var leftGroup;
var leftvalues = "", leftpixels = "", lefttype = "";
var rightvalues = "", rightpixels = "", righttype = "";
var axis = "", unit = "";
var currentTable = 1;


function initcompare(lang) {
//	init(lang);
	commentsSupported = false;
//	inittable(false);
	leftselector = document.getElementById('leftselector');	
}

function eById(e){return document.getElementById(e);}

function swapComparegroup(anchor, index) {
	var tableId = 'comparetable';
	var tableToView = eById(tableId + index);
	var numberOfTables = 4;
	for (var i = 1;i<=numberOfTables;i++) 		{
		eById(tableId + i).className = 'datatable scrollable inlinebars hidden';	
		eById(tableId + i).display = 'none';	
		eById('tab' + i).className = "";	
		
	}	
	var header = '';	
	if (lang = 'sv') {		
		switch(index) {
			case 1:
				header = 'Nyckeltal';
				break;
			case 2:
				header = 'Resultaträkning';
				break;
			case 3:
				header = 'Balansräkning';
				break;
			case 4:
				header = 'Kassaflödesanalys';										
		}
	}
	if (lang = 'en') {		
		switch(index) {
			case 1:
				header = 'Key figures';
				break;
			case 2:
				header = 'Income statement';
				break;
			case 3:
				header = 'Balance sheet';
				break;
			case 4:
				header = 'Cash flow statement';										
		}
	}
	eById('tab' + index).className = 'selected';
	eById('currHeader').innerHTML = header;
	tableToView.className = 'datatable scrollable inlinebars ';
	tablelayout();
	
}


function changecompare(input, index) {
	values = input.parentNode.parentNode.childNodes[3].innerHTML;
	values = values.substring(values.indexOf('|') + 1);
	var re = /\./g; // replace all English decimal points to comma
	values = values.replace(re, ',');
	
	if (currentTable != 1) {
		// if not key ratios: divide each figure with 1000
		tmpvalues = values.split(';');
		values = '';
		for (var i = 0; i < tmpvalues.length; i++) {
			values += (parseInt((parseInt(tmpvalues[i].replace(' ', '')) / 1000)) + ';');
			
		}
	}
	
	if (index < 0) {
		side = "Left";
		leftvalues = values;
	}
	else {
		side = "Right";
		rightvalues = values;
	}
	
	if (input.parentNode.parentNode.childNodes[0].className == "Line")
		type = "line";
	else
		type = "bar";
		
	node = input.parentNode.parentNode.childNodes[0];
	while (node.childNodes.length > 0) {
		label = node.innerHTML;
		node = node.childNodes[0];
	}
	var headerControl = document.getElementById(side + 'axisheader');
	if(headerControl)
	{
	    document.getElementById(side + 'axisheader').innerHTML = label + unit;
	    document.getElementById(side + 'axisheader').style.display = 'block';
	}
	else
	{
	    alert('Page is missing an element with ID ' + side + 'axisheader.');
	}
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		try	{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				//alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			scaledata = xmlHttp.responseText.split('|');
			leftpixels = scaledata[0];
			rightpixels = scaledata[1]
			axis = scaledata[2];
			
			if (side == "Left") {
				leftvalues = values;
				lefttype = type;
			}
			else {
				rightvalues = values;
				righttype = type;
			}
			axis = axis.replace(/,/g,".");
			fetchCompareChart();
		
		}
	}
	xmlHttp.open('GET', '../../flash/scaling.aspx?lv=' + leftvalues + '&rv=' + rightvalues + '&side=' + side + '&d=' + Date(), true);
	oldvalues = values;
	xmlHttp.send(null);
}

function fetchCompareChart() {		
	
   html = '<object '
        + '\nclassid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
        + '\ncodebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" '
        + '\nwidth="590" '
        + '\nheight="250" '
        + '\nid="flashdiagram">'
		+ '\n<param name="movie" value="../../flash/compare.swf?lv=' + leftvalues + '&lp=' + leftpixels + '&as=' + axis + '&rv=' + rightvalues + '&rp=' + rightpixels + '&ltype=' + lefttype + '&rtype=' + righttype + '" />'
		+ '\n<param name="scale" value="noborder" />'
		+ '\n<param name="menu" value="false" />'
		+ '\n<param name="quality" value="high" />'
		+ '\n<param name="bgcolor" value="#F0F0F0" />'
		+ '\n<embed src="../../flash/compare.swf?lv=' + leftvalues + '&lp=' + leftpixels + '&as=' + axis + '&rv=' + rightvalues + '&rp=' + rightpixels + '&ltype=' + lefttype + '&rtype=' + righttype + '"'
        + '\nquality="high"'
        + '\nbgcolor="#F0F0F0"'
        + '\nscale="noborder"'
        + '\nwidth="590"'
        + '\nheight="250"'
        + '\nname="flashdiagram"'
        + '\ntype="application/x-shockwave-flash" '
        + '\npluginspace="http://www.macromedia.com/go/getflashplayer">'
    + '\n</embed>'
	+ '\n</object>';
	document.getElementById('flashobject').innerHTML = html;
}

