//Window offsets
var TopOffset = 50;
var LeftOffset = 50;
var mbMenuShown = true;

// Create the window collection
var wc;
wc = new JSCollection;

// Create chart collection
var cc;
cc = new JSCollection;

var mw;

function CloseQuickAnalysis(){

	// Remove any unused chart references
	if (mw != null)
		mw.close();
}

/**
 * SPG: DM035
 * Win XP SP2 does not allow openning of windows from anywhere but the originating click.
 * To handle this, we open the window from the click, then set a reference to it so we can close it
 * again.
 */
function SetQuickAnalysisReference(poWindow){
	// Remove any unused chart references
	CloseQuickAnalysis();
	mw = poWindow;
}

function AddPage(oWin){

	// Remove any unused chart references
	RemoveUnusedCharts();
	wc.add(oWin);

}


/**
 * SPG: DM035
 * Replaces OpenChart2, in conjunction with SetChartRef().
 * Have to force the owning window to open the chart for XP SP2, 
 * otherwise the popup is blocked.
 * Here we simply do the checks as before then return a url to use 
 * when opening the window.
 * Once opened, the window must be registered by using SetChartRef()
 */
function CreateChartUrl2(plProductID1, plProductID2, plProductID3, plInstrumentID1, plInstrumentID2, plInstrumentID3, plChartField1, plChartField2, plChartField3, psFromDate, psToDate, psOperator, psContract1, psContract2, psContract3, psIntrumentType1, psIntrumentType2, psIntrumentType3, pbOneAxis, piGraphOpt, psVal1, psVal2, psVal3){
	var bSuccess;
	// Remove any unused chart references
	RemoveUnusedCharts();
	var sChartData = null;
	
	//Do the no Data Check.
	var re=/No%20Data/gi;
	bSuccess = false;
	
	if (psContract1 != '')
		if (psContract1.search(re) == -1)
			bSuccess = true;
	if (psContract2 != '')
		if (psContract2.search(re) == -1)
			bSuccess = true;
	if (psContract3 != '')
		if (psContract3.search(re) == -1)
			bSuccess = true;	
	
	if (bSuccess == true)
	{
		// Check if we too many charts open
		HandleChartCount();	
		sChartData = './VolmarkerChart.asp?PROD1=' + plProductID1 + '&PROD2=' + plProductID2 + '&PROD3=' + plProductID3 + '&INST1=' + plInstrumentID1 + '&INST2=' + plInstrumentID2 + '&INST3=' + plInstrumentID3 + '&FIELD1=' + plChartField1 + '&FIELD2=' + plChartField2 + '&FIELD3=' + plChartField3 + '&INSTANCE=' + String(FirstAvailableInstance()) + '&FROMDATE=' + psFromDate + '&TODATE=' + psToDate + '&OPERATOR=' + escape(psOperator) + '&Instrument1=' + psContract1 + '&Instrument2=' + psContract2 + '&Instrument3=' + psContract3 + '&InstrumentType1=' + psIntrumentType1 + '&InstrumentType2=' + psIntrumentType2 + '&InstrumentType3=' + psIntrumentType3 + '&OneAxis=' + pbOneAxis + "&GraphOpt=" + piGraphOpt + "&val1=" + psVal1 + "&val2=" + psVal2 + "&val3=" + psVal3;
	}
	else
	{
		sChartData = null;
		alert('There is no data available for this instrument');
	}
	return(sChartData);
}


function GetPageReferences(){
	var windowArray = new Array();
	var icount = 0;
	for(var i=0; i < cc.count(); i++){
		windowArray[i] = cc.item(i);
		icount++;
	}	
	var goStockPages = window.frames['fraMenu'].goStockPages;
	
	//this will include other windows in goStockPages - so must ignore those
	for (var j=0; j < wc.count(); j++)
	{
		var bExists = false;
		var key = 0;
		for (key in goStockPages)
		{
			if (goStockPages[key]!=null && wc.item(j)==goStockPages[key].Window)
			{
				bExists =true;
				break;
			}
		}
		if (!bExists)
		{				
			windowArray[icount] = wc.item(j);			
			icount++;
		}
	}
	return windowArray;
}

function SetChartRef(chartWin){
	// Add the window to the collection
	if(chartWin != null)
	{
		cc.add(chartWin);
	}
}

function SetPageRef(pageWin){
	// Add the window to the collection
	if(pageWin != null)
	{
		wc.add(pageWin);
	}
}

function CreateChartUrl3(pData){
	var bSuccess;

	// Remove any unused chart references
	RemoveUnusedCharts();
	
	var re=/No%20Data/gi;
	bSuccess = false;
	
	if (pData.Contract1 != '' || pData.InstrumentID1 != '')
		if (pData.Contract1.search(re) == -1 || pData.InstrumentID1 != '')
			bSuccess = true;
	if (pData.Contract2 != '' || pData.InstrumentID2 != '')
		if (pData.Contract2.search(re) == -1 || pData.InstrumentID2 != '')
			bSuccess = true;
	if (pData.Contract3 != '' || pData.InstrumentID3 != '')
		if (pData.Contract3.search(re) == -1 || pData.InstrumentID3 != '')
			bSuccess = true;	

	if (bSuccess == true)
	{
		// Check if we too many charts open
		HandleChartCount();

		//Create the URL
		var chartURL = './VolmarkerChart.asp?' + 
			'BookID=' + pData.BookID + 
			'&BookName=' + pData.BookName + 
			'&UserID=' + pData.UserID +  
			'&PROD1=' + pData.ProductID1 + '&PROD2=' + pData.ProductID2 + '&PROD3=' + pData.ProductID3 + '&PROD4=' + pData.ProductID4 + '&PROD5=' + pData.ProductID5 + '&PROD6=' + pData.ProductID6 + '&PROD7=' + pData.ProductID7 + 
			'&INST1=' + pData.InstrumentID1 + '&INST2=' + pData.InstrumentID2 + '&INST3=' + pData.InstrumentID3 + '&INST4=' + pData.InstrumentID4 + '&INST5=' + pData.InstrumentID5 + '&INST6=' + pData.InstrumentID6 + '&INST7=' + pData.InstrumentID7 + 
			'&FIELD1=' + pData.GraphField1 + '&FIELD2=' + pData.GraphField2 + '&FIELD3=' + pData.GraphField3 + '&FIELD4=' + pData.GraphField4 + '&FIELD5=' + pData.GraphField5 + '&FIELD6=' + pData.GraphField6 + '&FIELD7=' + pData.GraphField7 + 
			'&INSTANCE=' + String(FirstAvailableInstance()) + '&FROMDATE=' + pData.Start + '&TODATE=' + pData.End + 
			'&COMPARISON3=' + pData.Comparison3 + '&COMPARISON4=' + pData.Comparison4 + '&COMPARISON5=' + pData.Comparison5 + '&COMPARISON6=' + pData.Comparison6 + '&COMPARISON7=' + pData.Comparison7 +
			'&Instrument1=' + pData.Contract1 + '&Instrument2=' + pData.Contract2 + '&Instrument3=' + pData.Contract3 + '&Instrument4=' + pData.Contract4 + '&Instrument5=' + pData.Contract5 + '&Instrument6=' + pData.Contract6 + '&Instrument7=' + pData.Contract7 + 
			'&InstrumentType1=' + pData.DataField1 + '&InstrumentType2=' + pData.DataField2 + '&InstrumentType3=' + pData.DataField3 + '&InstrumentType4=' + pData.DataField4 + '&InstrumentType5=' + pData.DataField5 + '&InstrumentType6=' + pData.DataField6 + '&InstrumentType7=' + pData.DataField7 + 
			'&Val1=' + pData.Val1 + '&Val2=' + pData.Val2 + '&Val3=' + pData.Val3 + '&Val4=' + pData.Val4 + '&Val5=' + pData.Val5 + '&Val6=' + pData.Val6 + '&Val7=' + pData.Val7 + 
			'&OneAxis=' + pData.OneAxis +
			'&GraphStudies='+ pData.GraphStudies;
		// Add the window to the collection
		return chartURL;
	}
	else
	{
		alert('There is no data available for this instrument');
	}
	return(null);
}

function HandleChartCount()
{
	// Check if we too many charts open
	if (cc.count() == 4)
	{
		// Close the first item
		w =	cc.item(0);
		if( w != null){
			w.close();
		}
		// Remove the first item
		cc.remove(0);
	}
}

function RemoveUnusedCharts(){
	// Removes unused references to chart closed windows

	var i;
	var w;
	
	if (cc.count() > 0)
	{
		for (i=(cc.count()-1); i>=0; i--) 
		{
			w = cc.item(i);
			if (w.closed == true || w == null) 
			{	
				// Remove the item
				cc.remove(i);
			}	
		}
	}
}
function RemoveUnusedWindows(){
	// Removes unused references to closed windows
	var i;
	var w;
	
	if (wc.count() > 0)
	{
		for (i=(wc.count()-1); i>=0; i--) 
		{
			w = wc.item(i);
			if (w.closed == true || w == null) 
			{	
				// Remove the item
				wc.remove(i);
			}	
		}
	}
}
function FirstAvailableInstance(){

	// Find the first available Instance to use
	InstanceArray = new Array;

	var w;
	var i;
	
	if (cc.count() > 0)
	{
	
		try
		{
			// Loop through the used instances
			for (i=0; i<=(cc.count() -1); i++) 
			{	
				w =	cc.item(i);
				InstanceArray[w.Instance] = 1;
			}


			// Find first available instance
			for (i=0; i<=(InstanceArray.length); i++) 
			{
				if (InstanceArray[i] == null)
				{
					return i;
				}
			}
		}
		catch (e)
		{
			return 0;
		}
	}
	else
	{
		return 0;
	}
}


function GetWindow(WindowIndex){

	// Return the item from the correct collection
	// process Windows before charts

	if (WindowIndex < wc.count())
	{
		return wc.item(WindowIndex);
	}		
	else
	{
		return cc.item(WindowIndex - wc.count());
	}
}


function TileWindows(){

	var iTotalWindows;
	var iTotalWindowsSqrt;
	var iVerticalPanes;
	var iHorizontalPanes;
	var iPanesDrawn =0;	
	
	var iVerticalPaneCounter;
	var iHorizontalPaneCounter;

	var i;
	var iWindowHeight;
	var iWindowWidth;
	var iTotalWindows;

	var w;


	// Remove any unused chart references
	RemoveUnusedCharts();

	// Remove any unused window references
	RemoveUnusedWindows();

	// Set this window to the top left
//	self.resizeTo(screen.availWidth,screen.availHeight);
//	self.moveTo(0,0);
	self.focus();

	// Calculate how many windows we need to tile
	iTotalWindows = cc.count() + wc.count();

	// Check to see if we have some windows to tile
	if (iTotalWindows == 0)
	{
		return;
	}	

//	alert('iTotalWindows' + iTotalWindows);

	// Find the square root of the total windows
	iTotalWindowsSqrt = Math.sqrt(iTotalWindows);

//	alert('iTotalWindowsSqrt' + iTotalWindowsSqrt);

	// Calculate how many vertical panes we should use
	iVerticalPanes = Math.floor(iTotalWindowsSqrt);

	// Calculate how many horizontal panes we should use
	iHorizontalPanes = Math.floor(iTotalWindows / iVerticalPanes);

//	alert('Horizontal: ' + iHorizontalPanes);
//	alert('Vertical: ' + iVerticalPanes);

	// Set the window width
//	iWindowWidth = screen.availWidth / iVerticalPanes;
	iWindowWidth = document.body.offsetWidth / iVerticalPanes;

	
	// Loop through the Vertical panes from right to left
	for (iVerticalPaneCounter = iVerticalPanes; iVerticalPaneCounter > 0; iVerticalPaneCounter--) 
	{
		var iPanesToDraw;

//		alert('iVerticalPaneCounter: ' + iVerticalPaneCounter);
		
		// Check if we draw all the remaining panes as required we would not fall short
		if (iPanesDrawn + (iHorizontalPanes * iVerticalPaneCounter) == iTotalWindows)
		{
			iPanesToDraw = iHorizontalPanes;
		}
		else
		{
			iPanesToDraw = iHorizontalPanes + 1;
		}
		
//		alert('iPanesToDraw: ' + iPanesToDraw);
		
		// Set the window height
		iWindowHeight = (document.body.offsetHeight / iPanesToDraw);

		// Draw each of the panes from the top down
		iCurrentTop = window.screenTop;

		iCurrentLeft = ((iVerticalPaneCounter - 1) * iWindowWidth) + window.screenLeft;

		for (i=1; i<=iPanesToDraw; i++)
		{
			
			// Get the item
			w = GetWindow(iPanesDrawn);

			try
			{
				w.resizeTo(iWindowWidth,iWindowHeight);
				w.moveTo(iCurrentLeft,iCurrentTop);
				w.focus();
				//w.setTimeout("self.focus();",100);
			}
			catch (e)
			{
				//alert(e.Message);
			}
			
			// Set the top position of the next window
			iCurrentTop = iCurrentTop + iWindowHeight;
			
			// Increment the panes drawn counter
			iPanesDrawn++;

		}
	}
}


function CascadeWindows(){

	var i;
	var iWindowHeight;
	var iWindowWidth;
	var iTotalWindows;
	var iCurrentTop;
	var iCurrentLeft;

	// Remove any unused chart references
	RemoveUnusedCharts();

	// Remove any unused window references
	RemoveUnusedWindows();

	// Set this window to the top left
//	self.resizeTo(screen.availWidth,screen.availHeight);
//	self.moveTo(0,0);

	self.focus();

	iCurrentTop = window.screenTop;
	iCurrentLeft = window.screenLeft; + LeftOffset;

	iTotalWindows = cc.count() + wc.count();

	// Set Window Height
	iWindowHeight = (document.body.offsetHeight - (iTotalWindows * TopOffset)) + TopOffset;


	// Set Window Width
	iWindowWidth = (document.body.offsetWidth - (iTotalWindows * LeftOffset)) + LeftOffset;

	
	// Loop through the windows
	for (i=0; i<=(iTotalWindows - 1); i++) 
	{
		w = GetWindow(i);
		
		try
		{
			w.resizeTo(iWindowWidth,iWindowHeight);
			w.moveTo(iCurrentLeft,iCurrentTop);
			//w.location.reload(true); //JF 20031003 D:M
			w.focus();
		}
		catch (e)
		{
			//alert(e.Message);
		}
		
		iCurrentTop = iCurrentTop + TopOffset;
		iCurrentLeft = iCurrentLeft + LeftOffset;
	}
}

function DestroyChildWindows(poWin){

	var i;
	var w;
	
	// Remove any unused chart references
	RemoveUnusedCharts();

	// Remove any unused window references
	RemoveUnusedWindows();

	// Loop through the windows
	for (i=0; i<=(wc.count()-1); i++) 
	{
		w = wc.item(i);
		if((poWin==null) || poWin != w){
			w.close();
		}
	}

	
	// Loop through the charts
	for (i=0; i<=(cc.count()-1); i++) 
	{
		w = cc.item(i);
		if((poWin==null) || poWin != w){
			w.close();		
		}
	}
	
}


	function ShowMenu(plDisplay)
	{

		if (plDisplay==0) 
		{			
			mbMenuShown = false;
			
			Columns.cols = "16,*,0"; // JF 20031015 D:M
		}
		else
		{
			mbMenuShown = true;

			Columns.cols = "180,*,0"; // JF 20031015 D:M
			
		}
	}
	
	// JF 20031015 D:M
	function SetFrameLayout(plMenuWidth)
	{
		Columns.cols = plMenuWidth + ",*,0"; // JF 20031015 D:M
	}
	
	function showReconnect()
	{
		if (mbMenuShown)
		{
			Columns.cols = "180,0,*"; // JF 20031015 D:M
		}
		else
		{
			Columns.cols = "16,0,*"; // JF 20031015 D:M		
		}
	}
	
	function hideReconnect()
	{
		if (mbMenuShown)
		{
			Columns.cols = "180,*,0"; // JF 20031015 D:M	
		}
		else
		{
			Columns.cols = "16,*,0"; // JF 20031015 D:M	
		}
	}