var xmlHttp
var xmlHttpTime
var contentUpdate;
var modules = new Array('hotList', 'indexAction', 'indexIntensity', 'biggestTrades');

function showContent(str, affiliateId) {
	if(contentUpdate) {
		clearInterval(contentUpdate);
	}
	
	if (str == 'HOT_LIST') {
		var url="hotList.do?affiliateId=" + affiliateId;
		setSelectedModule("hotList");
	}
	else if (str == 'INDEX_ACTION') {
		var url="indexAction.do?affiliateId=" + affiliateId;
		setSelectedModule("indexAction");
	}
	else if (str == 'INDEX_INTENSITY') {
		var url="indexIntensity.do?affiliateId=" + affiliateId;
		setSelectedModule("indexIntensity");
	}
	else if (str == 'EQUITY_BIG_PLAYS') {
		var url="biggestTrades.do?affiliateId=" + affiliateId;
		setSelectedModule("biggestTrades");
	}

	xmlHttp = GetXmlHttpObject();
	
	if (xmlHttp == null) {
		alert ("Your browser does not support this feature");
		return;
	} 
	
	url = url + "&sid=" + Math.random();
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	showTime(str);
	this.contentUpdate = setInterval("showContent('" + str +"')",120000);
}

function showTime(str) {
	var url='updateTime.do';
	xmlHttpTime = GetXmlHttpObject();
	
	if (xmlHttpTime == null) {
		alert ("Your browser does not support this feature");
		return;
	}
	
	url = url + "?sid=" + Math.random() + "&module=" + str;
	xmlHttpTime.onreadystatechange = stateChangedTime;
	xmlHttpTime.open("GET", url, true);
	xmlHttpTime.send(null);
}

function hold_showChart(str) {
	var url='/ti/getChart.jsp';
	xmlHttpChart = GetXmlHttpObject();
	
	if (xmlHttpChart == null) {
		alert ("Your browser does not support this feature");
		return;
	}
	
	url = url + "?sid=" + Math.random() + "&symbol=" + str;
	xmlHttpChart.onreadystatechange = stateChangedChart;
	xmlHttpChart.open("GET", url, true);
	xmlHttpChart.send(null);
}

function showChart(symbol, affiliateId, tab) {
	var url = "/ti/getChart.jsp?sid=" + Math.random() + "&symbol=" + symbol + "&affiliateId=" + affiliateId + "&tab=" + tab;
	window.frames['chart_box_frame'].location = url;
	document.getElementById("loading_chart").style.display = "block";
	document.getElementById("chart_box").style.display = "block";
}

function showChartFrame() {
	document.getElementById("loading_chart").style.display = "none";
	document.getElementById("chart_box_frame").style.visibility = "visible";
	
}

function hideChart() {
	document.getElementById("chart_box").style.display = "none";
	document.getElementById("chart_box_frame").style.visibility = "hidden";
}

function showAdvert(affiliateId) {
	var url = "/cms/messages/actiontracker_ad/index.html?affiliateId=" + affiliateId;
	xmlHttpAdvert = GetXmlHttpObject();
	
	if (xmlHttpAdvert == null) {
		return;
	}
	
	xmlHttpAdvert.onreadystatechange = stateChangedAdvert;
	xmlHttpAdvert.open("GET", url, true);
	xmlHttpAdvert.send(null);
}

function hideAdvert() {
	document.getElementById("footer_ad").style.display = "none";
	document.getElementById("footer_ad").innerHTML = "";
}

function stateChanged() {
	if (xmlHttp.readyState == 4) {
		document.getElementById("page_text").innerHTML = xmlHttp.responseText;
	}
}

function stateChangedTime() {
	if (xmlHttpTime.readyState == 4) {
		document.getElementById("date_time").innerHTML = xmlHttpTime.responseText;
	}
}

function stateChangedChart() {
	if (xmlHttpChart.readyState == 4) {
		document.getElementById("chart_box").innerHTML = xmlHttpChart.responseText;
		document.getElementById("chart_box").style.visibility = "visible";
	}	
}

function stateChangedAdvert() {
	if (xmlHttpAdvert.readyState == 4) {
		document.getElementById("footer_ad").innerHTML = xmlHttpAdvert.responseText;
		if (document.getElementById('om_at_ad')) {
			document.getElementById('om_at_ad').href += "&" + document.location.search.substring(1);
		}
		document.getElementById("footer_ad").style.display = "block";
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function setSelectedModule(moduleName) {
	for (idx in modules) {
		var curModule = modules[idx];
		var curElem = document.getElementById(curModule); 
		if (curElem != null) {
			if (curModule == moduleName) {
				document.getElementById(curModule).style.backgroundColor = "#376c98";
				document.getElementById(curModule).style.color = "#fff";
				document.getElementById(curModule).style.padding = "4px 0 1px 0";
				document.getElementById(curModule).style.margin = "-2px 2px 0 0";
				document.getElementById(curModule).style.borderBottom = "1px solid #376c98";
			} else {
				document.getElementById(curModule).style.backgroundColor = "#9bc";
				document.getElementById(curModule).style.color = "#666";
				document.getElementById(curModule).style.padding = "1px 0 1px 0";
				document.getElementById(curModule).style.margin = "1px 2px 0 0";
				document.getElementById(curModule).style.borderBottom = "1px solid #ccc";
			}
		}
	}
}
