//	GLOBAL VARIABLES //
var oxmlhttp = null;
var aBuffer = new Array();
var interval = 3000;
var url = null;
var divId = new Array();
var divInnerHtml = new Array();
//debuging variables//
//var noofwrites = 0;
//var noofstocks = 0;
//debugging variables//
window.onload = startRequest;
//	GLOBAL VARIABLES //

function startRequest()
{
	if (!oxmlhttp) {oxmlhttp = zXmlHttp.createRequest();}
	url = "/markets/athfeed1.asp?dummy=" + Math.random()*10000000000000000;
	oxmlhttp.open("get", url, true);
	oxmlhttp.onreadystatechange = function() {
		if (oxmlhttp.readyState == 4) {
			if (oxmlhttp.status == 200) {
				var aRows = oxmlhttp.responseText.split("\r");
				for(i=0;i<aRows.length-1;i++){
					var aTabs = aRows[i].split("\t");
					var tableRow = document.getElementById(aTabs[1]);
					if (tableRow){

						//start writing
						if (aTabs[0] == 1){
							aBuffer["value"] = parseFloat(tableRow.cells[2].innerHTML.replace(/\./,'').replace(/\,/,'.'));
							//console.log(aTabs[1] + " previous value: " + aBuffer["value"]);
							aBuffer["pct"]   = tableRow.cells[3].innerHTML;
							aBuffer["vol"]   = parseInt(tableRow.cells[4].innerHTML);
							if (!isNaN(aTabs[2]) || aTabs[2] != "&nbsp;"){
								if (parseFloat(aTabs[2].replace(/\./,'').replace(/\,/,".")) != aBuffer["value"]){
									tableRow.cells[2].innerHTML = aTabs[2];
									//noofwrites++;
									//noofstocks++;
								}
									if (aTabs[5] != aBuffer["pct"]){
									tableRow.cells[3].innerHTML = aTabs[5];
									//noofwrites++;
								}
									if (parseInt(aTabs[4]) != aBuffer["vol"]){
									tableRow.cells[4].innerHTML = aTabs[4];
									//noofwrites++;
								}
							}
						}
						else if (aTabs[0] == 2){
							var index = 0;
							divId[index] = aTabs[1];
							divInnerHtml[index] = aTabs[2];
							if (document.getElementById(divId[index]))
							{
					      		document.getElementById(divId[index]).innerHTML = divInnerHtml[index];
					       	}
							index++;
						//finished writing
						}

						//start color coding
						if (aTabs[0] == 1){
							if (!isNaN(aBuffer["vol"]) || aBuffer["vol"] != "&nbsp;"){

								if (parseInt(aTabs[4])-aBuffer["vol"] > 0){
									//green
									if (parseFloat(aTabs[2].replace(/\./,'').replace(/\,/,".")) > aBuffer["value"]){
										tableRow.style.backgroundColor = "#ccff66";
										tableRow.cells[0].style.backgroundColor = "#ccff66";
										tableRow.cells[1].innerHTML = "<img src='xa_up.gif'>";
									}

									//red
									else if (parseFloat(aTabs[2].replace(/\./,'').replace(/\,/,".")) < aBuffer["value"]){
										tableRow.style.backgroundColor = "#ff7979";
										tableRow.cells[0].style.backgroundColor = "#ff7979";
										tableRow.cells[1].innerHTML = "<img src='xa_down.gif'>";
									}

									//grey
									else if (parseFloat(aTabs[2].replace(/\./,'').replace(/\,/,".")) == aBuffer["value"]){
										tableRow.style.backgroundColor = "#cccccc";
										tableRow.cells[0].style.backgroundColor = "#cccccc";
										tableRow.cells[1].innerHTML = "&nbsp;";
									}
									setTimeout("resetRowColor('" + aTabs[1] + "')", 1800);
								}
								else
								{
									//green
									if (parseFloat(aTabs[2].replace(/\./,'').replace(/\,/,".")) > aBuffer["value"]){
										tableRow.style.backgroundColor = "#ccff66";
										tableRow.cells[0].style.backgroundColor = "#ccff66";
										tableRow.cells[1].innerHTML = "<img src='xa_up.gif'>";
									}

									//red
									else if (parseFloat(aTabs[2].replace(/\./,'').replace(/\,/,".")) < aBuffer["value"]){
										tableRow.style.backgroundColor = "#ff7979";
										tableRow.cells[0].style.backgroundColor = "#ff7979";
										tableRow.cells[1].innerHTML = "<img src='xa_down.gif'>";
									}
									setTimeout("resetRowColor('" + aTabs[1] + "')", 1800);
								}

								var trigger = aTabs[3].toString();
								switch(trigger){
									case '1':
									tableRow.cells[0].style.color = "#000000";
									tableRow.cells[2].style.color = "#000000";
									tableRow.cells[3].style.color = "#000000";
									break;

									case '2':
									tableRow.cells[0].style.color = "#008800";
									tableRow.cells[2].style.color = "#008800";
									tableRow.cells[3].style.color = "#008800";
									break;

									case '3':
									tableRow.cells[0].style.color = "#bb0000";
									tableRow.cells[2].style.color = "#bb0000";
									tableRow.cells[3].style.color = "#bb0000";
									break;
								}
							}
							//finish color coding
						}
					}
				}
			}
			//console.log("Stocks that chamged %s", noofwrites);
			//noofwrites = 0;
			//noofstocks = 0;
			setTimeout(startRequest, interval);
		}
	};
	oxmlhttp.send(null);
}

function resetRowColor(tableRowId)
{
	oRow = document.getElementById(tableRowId);
	oRow.style.backgroundColor = "#ffffff";
	oRow.cells[0].style.backgroundColor = "#ffffcc";
}
