﻿	//-DINH NGHIA CAC THAM SO MAU
	
	var redColor = "#f8b87a";
	var greenColor = "lightgreen";
	var downColor = "red";
	var upColor = "#00FF00";
	var refColor = "#FFFF00";	
	var ceColor = "#0FF9F9";
	var flColor = "#FF00FF";	
	var volColor = "#FFFFFF";   //-Mau cot khoi luong
    var atColor = "#FFFF00";    //-Mau cua ATC, ATO
    var stockColor ="#FFFFFF";  //-Mau ma chung khoan
    var bgColor = "#000000";    //-Mau nen
    function changePage(groupNumber) {		
		groupArray = page1;
		var groupString = "";
		var st = 0;
		for (st = 0; st <= numstocks; st++) {
			groupString += "item" + groupArray[st] + " ";
		}
		actGroup = groupArray;	
		var newTable = new OverwriteTable(groupString, schema, "MERGE");
		newTable.setSnapshotRequired(true);
		newTable.setRequestedMaxFrequency(0.5);
		newTable.setClearOnDisconnected(true); //default is false
		newTable.setClearOnRemove(true); //default is false
		newTable.onItemUpdate = updateItem;
		newTable.onChangingValues = formatValues;
		newTable.setPushedHtmlEnabled(true);		
		lsPage.addTable(newTable, "1");
	}
	
	//////////////////////////Event handlers		
	var matchedColor;
	var marketstat;
	var marketindex, changeindex, totalvalue, totalvolume;
	
	function updateItem(item, updateInfo) {
		if (updateInfo == null) return;		

		if (item == 1) {			
			var today = document.getElementById("today");
			today.innerHTML = updateInfo.getNewValue(1).substring(0,10);
			marketstat = updateInfo.getNewValue(2);
			marketindex = updateInfo.getNewValue(13);
			changeindex = updateInfo.getNewValue(16);
			totalvalue = updateInfo.getNewValue(19);
			totalvolume = updateInfo.getNewValue(22);

			var m = document.getElementById("marketstatus");
			if (marketstat == "P") {
				m.innerHTML = eval('aStat.P');
			} else if (marketstat == "O") {
				m.innerHTML = eval('aStat.O');
			} else if (marketstat == "A") {
				m.innerHTML = eval('aStat.A');
			} else if (marketstat == "C") {
				m.innerHTML = eval('aStat.C');
			} else {
				m.innerHTML = eval('aStat.K');
			}
			
			changeindex = new Number(changeindex);
			var percent = 0;
			if (marketindex!=changeindex)
				percent = changeindex*100/(marketindex-changeindex);
			var colorindex;
			var imgVNIndex = document.getElementById("imgVNIndex");
			if (changeindex == 0) {
				imgVNIndex.innerHTML = imgNone;
				colorindex = refColorvn;
			} else if (changeindex < 0) {
				imgVNIndex.innerHTML = imgDown;
				colorindex = downColorvn;
			} else if (changeindex > 0) {
				imgVNIndex.innerHTML = imgUp;
				colorindex = upColorvn;
			}
						
			document.getElementById("vnchangeindex1").innerHTML = "<font color='" + colorindex + "'>" + changeindex.toFixed(2) + " (" + percent.toFixed(2) + "%)</font>"; 

			var nf = new NumberFormat(marketindex);
			nf.setPlaces(2);
			document.getElementById("vnindex1").innerHTML = "<font color='" + colorindex + "'>" + nf.toFormatted() + "</font>";

			if (totalvolume != 0) {
				var nf = new NumberFormat(totalvolume);
				nf.setPlaces(0);
				document.getElementById("totalshares1").innerHTML = nf.toFormatted();
			} else {
				document.getElementById("totalshares1").innerHTML = "";
			}

			if (totalvalue != 0) {
				var nf = new NumberFormat(totalvalue*1000000);
				nf.setPlaces(0);			
				document.getElementById("totalvalues1").innerHTML = nf.toFormatted();
			} else {
				document.getElementById("totalvalues1").innerHTML = "";
			}							
			return;
		}
		
		
		//10 = change
		if (updateInfo.isValueChanged(10)) {
			var newValue9 = parseFloat(updateInfo.getNewValue(9));
			if (newValue9 == updateInfo.getNewValue(3)) {
				updateInfo.addField(31,imgCeiling);
			} else if (newValue9 == updateInfo.getNewValue(4)) {
				updateInfo.addField(31,imgFloor);
			} else if (newValue9 == updateInfo.getNewValue(5)) {
				updateInfo.addField(31,imgRef);
			} else if (newValue9 > updateInfo.getNewValue(5)) {
				updateInfo.addField(31,imgUp);
			} else if ((newValue9 < updateInfo.getNewValue(5)) && (newValue9 > 0)) {
				updateInfo.addField(31,imgDown);
			} else {
				updateInfo.addField(31,imgNone);
			}
		}
		
		if (updateInfo.isValueChanged(29)){
			oldrooms[item] = parseFloat(updateInfo.getOldValue(29));
		}
		
		//9 = matchedprice
		var oldLast = updateInfo.getOldValue(9);		
		var newColor;
		if (oldLast == null) { //first update for this item		//33 = ON-OFF
			if (doFade) {
				updateInfo.addField(33,"OFF",true);
			}
		} 
		else if (updateInfo.isValueChanged(9)) {
			oldprices[item] = parseFloat(oldLast);
			//at least second update
			if (oldprices[item] > updateInfo.getNewValue(9)) {
				updateInfo.addField(32,redColor,true);
			} else {
				updateInfo.addField(32,greenColor,true);
			}
			if (doFade) {
				updateInfo.addField(33,"ON",true);
			}
		}
	} 
			
	//-----------------------------------------------------------------------
	function carryBackUnchanged(itemUpdate) {
		//29 fields have value
		for (var i=1; i<=29; i++) {
			itemUpdate.setFormattedValue(i,itemUpdate.getServerValue(i));
		}
	}
	
	//-----------------------------------------------------------------------
	function formatValues(item, itemUpdate) {
	 	if (itemUpdate == null) return;	 	
		
	 	if (doFade) {
		 	if (itemUpdate.getServerValue(33) == "ON") {
				itemUpdate.setHotToColdTime(2000);
			} 
		}
	 	itemUpdate.setHotTime(3000);
	 	
		//30 = item_status
	 	if (itemUpdate.getServerValue(30) == "inactive") {
			carryBackUnchanged(itemUpdate);
	 	} 
	 	else {
	 		if (itemUpdate.getFormattedValue(30) != null) {
	 			carryBackUnchanged(itemUpdate);
	 		}	 				 	
		 	var backC = null;
		 	var backH = "#666666"; //itemUpdate.getServerValue(32);
			itemUpdate.setRowAttribute(backH, backC, "backgroundColor");		 			 			 			 			 	
	 	}
	 	
		//format the "number" fields
		matchedColor=null;
		for (var i = 3; i <= 29; i++) {
			    
			if(i==3){                   //--CeilingPrice
			    itemUpdate.setAttribute(i, null, ceColor, "color");
			}
			
			if(i==4){
			    itemUpdate.setAttribute(i, null, flColor, "color");
			}
			
			if(i==5){
			    itemUpdate.setAttribute(i, null, refColor, "color");
			}
			
			var newValue = itemUpdate.getServerValue(i);
			if (newValue == null) continue;		

			if (isNaN(newValue)) {
				// ATO, ATC
				itemUpdate.setAttribute(i,null,atColor,"color");
				itemUpdate.setAttribute(i+6,null,volColor,"color");
				continue;
			}		
			if (newValue == 0){
				itemUpdate.setFormattedValue(i,"");
				continue;
			}
			
			var formattedVal = parseFloat(newValue);

			if (i >= 19 && i <= 29) {
				// Mau sac se do cot gia quyet dinh
				var nf = new NumberFormat(newValue);
				nf.setPlaces(0);
				formattedVal=nf.toFormatted();
				
				itemUpdate.setAttribute(i, null, volColor, "color");	
				//continue; //"vol" fields
			}
			
			
			
			// format prices
			if (i == 9) {
				if (formattedVal == itemUpdate.getServerValue(3)) {
					matchedColor=ceColor;
				} else if (formattedVal == itemUpdate.getServerValue(4)) {
					matchedColor=flColor;
				} else if (formattedVal == itemUpdate.getServerValue(5)) {
					matchedColor=refColor;
				} else if (formattedVal > itemUpdate.getServerValue(5)) {
					matchedColor=upColor;
				} else if (formattedVal < itemUpdate.getServerValue(5)) {
					matchedColor=downColor;
				}
				itemUpdate.setAttribute(9,null,matchedColor,"color");
				itemUpdate.setAttribute(10,null,matchedColor,"color");
			}
	
			if (i == 10) {
				//var formattedVal = formatDecimal(newValue, 1, true);
				if (formattedVal > 0) {
					formattedVal = "+" + formattedVal;
				}
				if (itemUpdate.getServerValue(9) == itemUpdate.getServerValue(3)) {
					//ceiling price
					formattedVal = formattedVal;
				} else if (itemUpdate.getServerValue(9) == itemUpdate.getServerValue(4)) {
					//floor price
					formattedVal = formattedVal;
				}
			}

			var mycolor;
			if ((i >= 6) && (i <= 12) && (i != 10) && (i != 9)){				
				if (formattedVal == itemUpdate.getServerValue(3)) {
					mycolor=ceColor;
				} else if (formattedVal == itemUpdate.getServerValue(4)) {
					mycolor=flColor;
				} else if (formattedVal == itemUpdate.getServerValue(5)) {
					mycolor=refColor;
				} else if (formattedVal > itemUpdate.getServerValue(5)) {
					mycolor=upColor;
				} else if (formattedVal < itemUpdate.getServerValue(5)) {
					mycolor=downColor;
				} else {
					mycolor=null;
				}
				itemUpdate.setAttribute(i,null,mycolor,"color");
			}

			if ((i >= 13) && (i <= 18)){
				if (formattedVal == itemUpdate.getServerValue(3)) {
					mycolor=ceColor;
				} else if (formattedVal == itemUpdate.getServerValue(4)) {
					mycolor=flColor;
				} else if (formattedVal == itemUpdate.getServerValue(5)) {
					mycolor=refColor;
				} else if (formattedVal > itemUpdate.getServerValue(5)) {
					mycolor=upColor;
				} else if (formattedVal < itemUpdate.getServerValue(5)) {
					mycolor=downColor;
				} else {
					mycolor=null;
				}
				itemUpdate.setAttribute(i,null,mycolor,"color");
				//itemUpdate.setAttribute(i+6,null,mycolor,"color");
			}
				
			itemUpdate.setFormattedValue(i,formattedVal);

	 	}
		
	}	
	
///////////////////block escape key	
	document.onkeydown = checkEscape;
	document.onkeypress = checkEscape;
	function checkEscape(e) {
		if(!e) e = event;
		if(e.keyCode == 27) return false;
	}
	
	//////////////////////////Format functions
		
	//time format from [0-24] to [0-12] (without AM / PM )
	function formatTime(val) {
		var a = new Number(val.substring(0,val.indexOf(":")));
		if (a > 12) {
			a -= 12;
		}
		var b = val.substring(val.indexOf(":"),val.length);
		return a + b;
	}
	
	//format a decimal number to a fixed number of decimals 
	function formatDecimal(value, decimals, keepZero) {
		var mul = new String("1");
		var zero = new String("0");
		for (var i = decimals; i > 0; i--) {
			mul += zero;
		}
		value = Math.round(value * mul);
		value = value / mul;
		var strVal = new String(value);
		if (!keepZero) {
			return strVal;	
		}
		
		var nowDecimals = 0;
		var dot = strVal.indexOf(".");
		if (dot == -1) {
			strVal += ".";
		} else {
		 	nowDecimals = strVal.length - dot - 1;
		}
		for (var i = nowDecimals; i < decimals; i++) {
			strVal = strVal + zero;
		}
			
		return strVal;
	}
	
///////////////Global var declaration
//1.time 2.ticker 
//3.ceiling 4.floor 5.ref 6.max 7.min 8.ave 9.matched 10.change 11.open 12.close
//13.bid1 14.bid2 15.bid3 16.ask1 17.ask2 18.ask3 
//19.bid1vol 20.bid2vol 21.bid3vol 22.ask1vol 23.ask2vol 24.ask3vol 25.matchedvol 26.openvol 27.closevol 28.totalvol
//29.room 30.item_status 31.arrowpicture 32.color 33.ON-OFF 34.{more};
	var schema = "time ticker ceiling floor ref max min ave matched change open close bid1 bid2 bid3 ask1 ask2 ask3 bid1vol bid2vol bid3vol ask1vol ask2vol ask3vol matchedvol openvol closevol totalvol priorroom item_status";

	var actGroup = null;
	var imgString = '<img src="images/popup.gif" alt="Graphic Chart" width="16" height="16" align="left" border="0" hspace="1">';
	var imgUp = '<img src="images/quotes_up.gif" alt="up" width="16" height="8" border="0">';
	var imgDown = '<img src="images/quotes_down.gif" alt="down" width="16" height="8" border="0">';
	var imgFloor = '<img src="images/quotes_fl2.gif" alt="down" width="16" height="8" border="0">';
	var imgCeiling = '<img src="images/quotes_ce2.gif" alt="down" width="16" height="8" border="0">';
	var imgRef = '<img src="images/quotes_ref.gif" alt="down" width="16" height="8" border="0">';
	var imgNone = '<img src="images/spacer_4px.gif" width="16" height="6" border="0">';
	var doFade = true;
	
	if (location.search.indexOf("fade=ON") > -1) {
		doFade = true;
	}
	
/////////////////////////////////PushPage Configuration
	var lsPage = new PushPage();
	lsPage.context.setDebugAlertsOnClientError(false);
	//lsPage.context.setDomain(lsdomain);
	lsPage.onEngineCreation = startEngine;
	lsPage.bind();
	lsPage.createEngine("SLEngine", "ls/");
	//starting stocks
	changePage(1);

function startEngine(eng) {
	eng.onClientAlert = null;
	eng.context.setDebugAlertsOnClientError(false);
	eng.policy.setIdleTimeout(30000);
	eng.policy.setPollingInterval(10000);
	eng.connection.setLSHost(null);
	eng.connection.setLSPort(lsport);
	eng.connection.setAdapterName("HOSE");
	eng.changeStatus("STREAMING");
}
function myalert(str){
	document.getElementById("canhbao").innerHTML = document.getElementById("canhbao").innerHTML + "<p>" + str ;
}
