﻿	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");
	}	
	
	//-------------------------------------------------------------------
	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 oI1 = document.getElementById("phiengd1");
			var oI2 = document.getElementById("phiengd2");
			var m = document.getElementById("marketstatus");
			if (marketstat == "P") {
				oI1.innerHTML = eval('aString.DK1');
				oI2.innerHTML = eval('aString.DK1');
				m.innerHTML = eval('aStat.P');
			} else if (marketstat == "O") {
				oI1.innerHTML = eval('aString.LT');
				oI2.innerHTML = eval('aString.LT');
				m.innerHTML = eval('aStat.O');
			} else if (marketstat == "A") {
				oI1.innerHTML = eval('aString.DK2');
				oI2.innerHTML = eval('aString.DK2');
				m.innerHTML = eval('aStat.A');
			} else if (marketstat == "C") {
				oI1.innerHTML = eval('aString.DC');
				oI2.innerHTML = eval('aString.DC');
				m.innerHTML = eval('aStat.C');
			} else {
				oI1.innerHTML = eval('aString.DC');
				oI2.innerHTML = eval('aString.DC');
				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 = "";
				colorindex = refColorvn;
			} else if (changeindex < 0) {
				imgVNIndex.innerHTML = imgDown;
				colorindex = downColor;
			} else if (changeindex > 0) {
				imgVNIndex.innerHTML = imgUp;
				colorindex = upColor;
			}
			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(33,imgCeiling);
			} else if (newValue9 == updateInfo.getNewValue(4)) {
				updateInfo.addField(33,imgFloor);
			} else if (newValue9 == updateInfo.getNewValue(5)) {
				updateInfo.addField(33,imgRef);
			} else if (newValue9 > updateInfo.getNewValue(5)) {
				updateInfo.addField(33,imgUp);
			} else if ((newValue9 < updateInfo.getNewValue(5)) && (newValue9 > 0)) {
				updateInfo.addField(33,imgDown);
			} else {
				updateInfo.addField(33,imgNone);
			}
		}
		
		if (updateInfo.isValueChanged(30)){
			oldrooms[item] = parseFloat(updateInfo.getOldValue(30));
		}
		stockstatus[item] = updateInfo.getNewValue(31);

		//9 = matchedprice
		var oldLast = updateInfo.getOldValue(9);
		var newColor;
		if (oldLast == null) {//35 = ON-OFF
			if (doFade) {
				updateInfo.addField(35,"OFF",true);
			}
		} 
		else if (updateInfo.isValueChanged(9)) {
			oldprices[item] = parseFloat(oldLast);
			//at least second update
			if (oldprices[item] > updateInfo.getNewValue(9)) {
				updateInfo.addField(34,redColor,true);
			} else {
				updateInfo.addField(34,greenColor,true);
			}
			if (doFade) {
				updateInfo.addField(35,"ON",true);
			}
		}
	} 
			
	//-----------------------------------------------------------------------
	function carryBackUnchanged(itemUpdate) {
		//30  fields have value
		for (var i=1; i<=30; i++) {
			itemUpdate.setFormattedValue(i, itemUpdate.getServerValue(i));
		}
	}
	
	//-----------------------------------------------------------------------
	function formatValues(item, itemUpdate) {
	 	if (itemUpdate == null) return;	 	
		
	 	if (doFade) {
		 	if (itemUpdate.getServerValue(35) == "ON") {
				itemUpdate.setHotToColdTime(2000);  //Khoảng thời gian các ô có sự thay đổi biến đổi màu
			} 
		}
	 	itemUpdate.setHotTime(3000);
	 	
		
		//32 = item_status
	 	if (itemUpdate.getServerValue(32) == "inactive") {
			carryBackUnchanged(itemUpdate);
	 	} 
	 	else {
	 		if (itemUpdate.getFormattedValue(32) != null) {
	 			carryBackUnchanged(itemUpdate);
	 		}	 				
	 		 	
		 	var backC = null;       //backC la mau nen mac dinh
		 	var backH ="#666666";   //backH la mau nen cua chung khoan khi co su thay doi, mau nay ton tai trong 2 s			
			itemUpdate.setRowAttribute(backH, backC, "backgroundColor");
			
			/*	 	
		 	//choose the "change" field stylesheet		 	
		 	var newChng;
		 	if ((newChng = itemUpdate.getFormattedValue(10)) != null) {
		 		var hotTxtCol = (newChng.charAt(0) == '-') ? "#dd0000" :  "#00ff00";
		 		itemUpdate.setAttribute(10,"black",hotTxtCol,"color");
		 	}
		 	*/		 	
	 	}
	 	
	 	// Set mau CK thuc hien quyen
	 	var l = itemUpdate.getServerValue(31);
	 	if ((l == 'A')||(l == 'R')||(l == 'D')||(l == '*')) {
	 		itemUpdate.setFormattedValue(2,itemUpdate.getServerValue(2)+" *");
	 	}	 	
	 	
	 	//--Set mau cho ma CK
	 	itemUpdate.setAttribute(2, null, stockColor, "color");
	 	
		//format the "number" fields
		matchedColor=null;
		for (var i = 3; i <= 30; 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 <= 30) {							    
			    var nf = new NumberFormat(newValue);
				nf.setPlaces(0);
				formattedVal=nf.toFormatted();		
				itemUpdate.setAttribute(i, null, volColor, "color");	
				//continue;		
			}						
			
			// 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) {				
				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.setFormattedValue(i,formattedVal);
	 	}		
	}	

	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.priorroom 
//30.room 31.status 32.item_status 33.arrowpicture 34.color 35.ON-OFF 36.{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 room status 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="floor" width="16" height="8" border="0">';
	var imgCeiling = '<img src="images/quotes_ce2.gif" alt="ceiling" width="16" height="8" border="0">';
	var imgRef = '<img src="images/quotes_ref.gif" alt="base" width="16" height="8" border="0">';
	var imgNone = '<img src="images/spacer_4px.gif" width="16" height="8" border="0">';
	var doFade = true;
	
	if (location.search.indexOf("fade=ON") > -1) {
		doFade = true;
	}
	
    /////////////////////////////////PushPage Configuration
		//window.alert("lshost0:" + lshost);
	var lsPage = new PushPage();
	lsPage.context.setDebugAlertsOnClientError(false);
	//lsPage.context.setDomain(lsdomain);
	lsPage.onEngineCreation = startEngine;
	lsPage.bind();
	lsPage.createEngine("SLEngine", "ls/");	
	changePage(1);

function startEngine(eng) {
	eng.onClientAlert = null;
	eng.context.setDebugAlertsOnClientError(false);
	//eng.policy.setMaxBandwidth(30);
	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 ;
}

/////////////////////////////////additional features		
	function openPopup(row) {
		var item = actGroup[row-1];
		var fadeStr = "";
		if (doFade) {
			fadeStr = "&fade=ON";
		}
		wdt=window.open("popup2.html?item=" + item + fadeStr, "SLDpopup" + item, "width=535,height=238,history=0,resizable,status=1,menubar=1");
		wdt.focus();
		return false;
	}