function CloseMarket(){
	// empty all dsos, hide everything, show a message, and wait for new array
	var totalDSOs = document.getElementsByTagName("XML");
	var e2 = document.getElementById("GlobalEmpty"); //global object to be used to access object model
		
	// get doCount attribute
	for (var i=0; i<totalDSOs.length; i++){
		if (totalDSOs[i].doCount == 'true'){
			// empty
			totalDSOs[i].XMLDocument.loadXML(e2.xml);
			
			// Hide market name DIVs
			var oMDiv = document.getElementById(totalDSOs[i].id.substr(3,2))
			if (oMDiv) oMDiv.style.display="none";
		}
	}
			
	document.getElementById("MSummary").style.display = 'none';
	document.getElementById("MarketClosed").style.display  = 'block';
}

function OpenMarket(){
	document.getElementById("MSummary").style.display = 'block';
	document.getElementById("MarketClosed").style.display  = 'none';
}

function UpdateTotals(dsoSetID,toID)
{
	var tObj = document.getElementById(toID);
	if (!tObj) return;
	
	// first read the employees xml
	var e = document.getElementById(dsoSetID);

	// use the xml object
	var nn = e.selectNodes("//Company");
	
	var ee, nValue, j, atts, eExists;
	var tTotals = tObj.tFoot.getElementsByTagName("B");
	
	var totalSec = 0;
	var totalTrades = 0;
	var totalVol =0;
	var totalValue =0;
	
	// check here if xml object is completely empty, evacuate all fields and show message
	// when table builds up, make sure message disappears
	
	for (i=0; i<nn.length; i++){
		// get me nn nnode companny id, and choose the one with same id from original, update if exist, else create
		
		// read value of code and accumilate
		totalSec++;
		totalValue += parseFloat(nn[i].getAttribute("Turnover").replace(/,/g, ''));
		totalTrades += parseInt(nn[i].getAttribute("Trades").replace(/,/g, ''));
		totalVol += parseInt(nn[i].getAttribute("Vol").replace(/,/g, ''));
	}

	// update totalsss
	tTotals[1].innerText = addSeparatorsNF(totalSec, '.', '.', ',');
	tTotals[2].innerText = addSeparatorsNF(totalTrades, '.', '.', ',');
	tTotals[3].innerText = addSeparatorsNF(totalVol, '.', '.', ',');
	tTotals[4].innerText = addSeparatorsNF(totalValue.toFixed(3), '.', '.', ',');
}

function UpdateSource(dsoSetID,toID, xmlString){
	OpenMarket();
	
	var tObj = document.getElementById(toID);
	HoldData(tObj);
	
	// first read the employees xml
	var e = document.getElementById(dsoSetID);
		
	// new xml container
	var n = document.getElementById("Global"); //global object to be used to access object model
	n.XMLDocument.loadXML(xmlString);
	
	// use the xml object
	var nn = n.selectNodes("//Company");
	var ee, nValue, j, atts, eExists;
	
	var tTotals = tObj.tFoot.getElementsByTagName("B");
	var totalSec = 0;
	var totalTrades = 0;
	var totalVol =0;
	var totalValue =0;
	
	// Remove nodes from the original xml (if they do not exist in the new xml)
	var oldNodes = e.selectNodes("//Company");
	for (i=0; i<oldNodes.length; i++){
		if (n.selectSingleNode("//Company[@CompanyID='"+ oldNodes[i].getAttribute("CompanyID") +"']") == null)
			e.documentElement.removeChild(oldNodes[i]);
	}
	
	// check here if xml object is completely empty, evacuate all fields and show message
	// when table builds up, make sure message disappears
	for (i=0; i<nn.length; i++){
		// get me nn nnode companny id, and choose the one with same id from original, update if exist, else create
		ee = e.selectSingleNode("//Company[@CompanyID='"+ nn[i].getAttribute("CompanyID") +"']");
		
		// read value of code and accumilate
		totalSec++;
		totalValue += parseFloat(nn[i].getAttribute("Turnover").replace(/,/g, ''));
		totalTrades += parseInt(nn[i].getAttribute("Trades").replace(/,/g, ''));
		totalVol += parseInt(nn[i].getAttribute("Vol").replace(/,/g, ''));
		
		eExists = (ee) ? true : false;
		
		if (!eExists) { ee = e.createNode(1,"Company",""); }

		atts = nn[i].attributes;
		for (j=0; j< atts.length; j++)
			ee.setAttribute(atts[j].name,atts[j].value);
		
		if(!eExists)
			x = e.documentElement.appendChild(ee);
	}
	// Update Totals
	tTotals[1].innerText = addSeparatorsNF(totalSec, '.', '.', ',');
	tTotals[2].innerText = addSeparatorsNF(totalTrades, '.', '.', ',');
	tTotals[3].innerText = addSeparatorsNF(totalVol, '.', '.', ',');
	tTotals[4].innerText = addSeparatorsNF(totalValue.toFixed(3), '.', '.', ',');
	
	// if i reflect directly, the behavior is attached and loaded, but the element is not bound yet
	Reflect(tObj,false);

	return (nn.length>0);
}

var WireCells;
// hold data only first time
function HoldData(tObj){
	// get wirecells in the obj passed
	if (!WireCells) WireCells = document.getElementsByName("WireCell");
	if (!WireCells) return;
	
	for (i=0; i<WireCells.length; i++){
		if(tObj.contains(WireCells[i])){
			WireCells[i].HoldData();
		}
	}
}

var gTotalInObj = 0;
function Reflect(tObj,bDoHoldData){
	// get wirecells in the obj passed
	// here you can have the actual number of rows to update, pass it
	
	if (!WireCells) WireCells = document.getElementsByName("WireCell");
	if (!WireCells) return;
	
	// to distinguish our fake onblur from real one, make custom properties
	tObj.HasRows = false;
	var bIsDummy = false;
	
	for (i=0; i<WireCells.length; i++){
		if(tObj.contains(WireCells[i])){
			
			// check if element is ready, if not, recall again
			if (WireCells[i].readyState != "complete") { 
				WireCells[i].onreadystatechange = doInitiateElement;
				// if it reaches this point, there got to be records
				tObj.HasRows = true;
				continue;
			}
			
			// if reflection returns a false at least once its enough to have rows
			bIsDummy = WireCells[i].Reflect(bDoHoldData);
	
			if (!bIsDummy){
				tObj.HasRows = true;
			}
		}
	}
	
	if (!tObj.HasRows){
		// norows, hide table
		tObj.style.display = 'none';
	}else{
		// show table
		tObj.style.display = 'block';

		// show market name DIV
		var oMDiv = document.getElementById(tObj.id.substr(0,2))
		if (oMDiv) oMDiv.style.display="block";
		OpenMarket();
	}
}

function doInitiateElement(){
	// make sure the element is initiated before you call the onblur to reflect
	// the problem is that it still takes time for the binding to take place
	if ( event.srcElement.readyState != "complete") return;
	event.srcElement.Reflect(true);
}

function UpdateUI(){
	
	// get the event source elments wire cells
	// here is where the table if first bound
	// when xml object is empty, show a message, when it starts to build up, remove message

	if (event.srcElement.readyState != "complete") return;
	var src = event.srcElement;
	
	Reflect(src,true);
}

function FillUp(){
	
	var e = event.srcElement;
	
	if (e.selectNodes("//Company").length == 0){
		
		// if no companies in this node, populate freom empty xml
		var e2 = document.getElementById("GlobalEmpty"); //global object to be used to access object model
		
		e.XMLDocument.loadXML(e2.xml);
	}
}

function addSeparatorsNF(nStr, inD, outD, sep)
{
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}
