var fxnewwin, fxnewwin2, winOrders, winAllClients, winStress, winStatement, winClientPos, winPricer, winBizReport;
var winCS1, winCS2, winCS3;


function OpenWindowFull(theURL, winName, features)
{
	var winSize = setWindowSize();
	var winIsOpened=false;
	var isOK=true;
	try { if (!fxnewwin.closed) winIsOpened=true; } catch(e){}
	if (winIsOpened && theURL==getUrl('01070204') && fxnewwin.GLTable+''!='undefined')
	{
		try {fxnewwin.Init(); fxnewwin.frNavigation.GetReportUsername(); fxnewwin.GetSqlString();} catch(e){isOK=false;}
		if (isOK)
		{
			fxnewwin.window.moveTo(0,0);
			fxnewwin.window.resizeTo( screen.availWidth, screen.availHeight-20);
			fxnewwin.focus();
			return;
		}
	}

	if (winIsOpened) CloseWindowFull();
	var srte="status=yes,scrollbars=yes,resizable=yes,top=0,left=0,height="+winSize[1]+",width="+winSize[0];
	fxnewwin=window.open(theURL,winName,srte);
	try {fxnewwin.focus();} catch(e){}
}

function CloseWindowFull()
{  
	try {fxnewwin.CloseAllWindows();} catch(e){}
	try {fxnewwin.QueryDet.newwin.close();} catch(e){}  
	try {fxnewwin.QueryDet.modwin.close();} catch(e){} 
	try {fxnewwin.close();} catch(e){} 
}

function OpenMiniReportWindow()
{
	GLTable=-1;
	GLTitle='For';
	GLFromLocation='Mini';
	GLHidsql='';
	var srte="status=no,scrollbars=yes,resizable=yes,top=4000,left=0,height=100,width=100";
	try{ fxnewwin=window.open(getUrl('01070204'),'aa',srte); } catch(e){}
	window.focus();
}

//----------- SECONDARY REPORT WINDOW opened from the primary report window -----------

function OpenWindowFull2(x)
{
	var srte
	if (x==1) srte="status=yes,scrollbars=yes,resizable=yes,top=100,left=100,height=450,width=350";
	else if (x==2) srte="status=yes,scrollbars=yes,resizable=yes,top=20,left=20,height=400,width="+eval(screen.availWidth-60);
	else srte="status=yes,scrollbars=yes,resizable=yes,top=100,left=30,height=200,width="+eval(screen.availWidth-60);
	fxnewwin2=window.open(getUrl('01070204'),'winRep2',srte);
	fxnewwin2.focus();
}

//------------------ REPORTS WINDOW and not close others (and more other) ---------------

function OpenWindowFullNoClose (theURL, winName, features)
{
	var srte="status=yes,scrollbars=yes,resizable=yes,top=0,left=0,height="+eval(screen.availHeight-60)+",width="+eval(screen.availWidth-10);
	fxnewwin=window.open(theURL,winName,srte);
	fxnewwin.focus();
}

//------------------ REPORTS WINDOW  (and more other) ---------------

function OpenWindowFullUtv (theURL, winName, features)
{
	CloseWindowFull()
	var srte="status=no,scrollbars=yes,resizable=yes,top=62,left=0,height="+eval(screen.availHeight-95)+",width="+eval(screen.availWidth-10);
	//uniqe fot tol:
	if (winName=='application') {winName+='_tol'}
	//alert(winName);
	fxnewwin=window.open(theURL,winName,srte);
	fxnewwin.focus();
	return fxnewwin
}

//------------------ WINDOW WIZARD  (and more other) ---------------

function OpenWizard(theURL, winName, features)
{
	CloseWindowFull()
	var srte= "height=300,width=340,status=yes,toolbar=no,menubar=no,location=no";
	fxnewwin=window.open(theURL,winName,srte);
	fxnewwin.focus();
}

//------------------ STRESS WINDOW ----------------------------------------
function OpenStressWindow(spot,vol)
{
	var winSize = setWindowSize();
	var srte="status=yes,scrollbars=NO,resizable=yes,top=0,left=0,height="+winSize[1]+",width="+winSize[0];
	winStress=window.open(getUrl('01060902'),'OrdersReport',srte);
	winStress.focus();
}
 
//------------------ HELP WINDOW ----------------------------------------

function WindowDirect(direction)
{
	try{ fnhideall(); } catch(e){}
	//var x= top.pathHelp + '/Home/helpframe.htm?'+direction;
	// alert(direction)
	var x
	if (direction)
	{ 
		//x= top.pathHelp + '/spot_transactions.htm ' 
		RH_ShowHelp(0, top.pathHelp + '/forexmanage.htm>forexmanage.htm ' , HH_HELP_CONTEXT, direction)
	}
	else
	{
		RH_ShowHelp(0, top.pathHelp + '/forexmanage.htm>forexmanage.htm ' , HH_DISPLAY_TOC, 100)
		//x= top.pathHelp + '/forexmanage.htm?'+direction;
	}
	//var srte="status=yes,scrollbars=NO,resizable=yes,top=0,left=0,height="+eval(screen.availHeight-60)+",width="+eval(screen.availWidth-10);
	//var MyHelpWin=window.open(x,'HelpWin',srte)
	//MyHelpWin.focus();
}


//------------------ ORDERS WINDOW ----------------------------------------

function OpenOrdersReportWindow()
{
/*
	GLTable=19
	GLTitle="COrders";
	GLFromLocation = "COrders";
	GLHidsql = "AND (Status=0 OR Status=1) ORDER BY MarketRatio";
	showRightMenu=1;
*/
	var srte="status=yes,scrollbars=NO,resizable=yes,top=0,left=0,height="+eval(screen.availHeight-60)+",width="+eval(screen.availWidth-10);
//	winOrders=window.open(getUrl('01070204'),'OrdersReport',srte);
	var pathFile = top.pathSite+"/app/Reports/Orders/OrdersReportFrame.htm"
	winOrders=window.open(pathFile,'OrdersReport',srte);
	winOrders.focus();
}

function CloseOrdersReportWindow()
{ try {winOrders.close();} catch(e){} }


//------------------ ALL CLIENTS WINDOW ------------------------------------

function OpenAllClientsWindow()
{
	var winSize = setWindowSize();
	GLTable='18';
	GLTitle='Acc';
	GLFromLocation='Serv';
	GLHidsql='0';
	showRightMenu=1;

	var srte="status=yes,scrollbars=NO,resizable=yes,top=0,left=0,height="+winSize[1]+",width="+winSize[0];
	winAllClients=window.open(getUrl('01070204'),'AllClients',srte);
//	winAllClients=window.open(top.pathSite+'/app/REPORTS/Query/pgAllAccFrame.htm','AllClients',srte);
	winAllClients.focus();
}

function CloseAllClientsWindow()
{ try {winAllClients.close();} catch(e){} }

//------------------ CLIENT POSITION WINDOW -----------------------------

var flagCP=0;
function OpenClientPositionWindow(x, acc)
{
	var winSize = setWindowSize();
	flagCP = (x==1)? 1:0;
	GLCustom = (x==1) ? acc+',':'';

	var srte="status=yes,scrollbars=yes,resizable=yes,top=0,left=0,height="+winSize[1]+",width="+winSize[0];
	if (x==1) srte="status=yes,scrollbars=yes,resizable=yes,top=0,left=0,height=500,width=750";
	winClientPos=window.open(getUrl('01070202'), "ClientPosition",srte) 
}

//------------------ UNION REPORTS WINDOW -----------------------------

function OpenUnionReportsWindow()
{
	CloseAllClientsWindow();
	OpenWindowFull (parent.pathSite+'/app/reports/UnionReports.htm', "aa","") 
}

//------------------ STATEMENT REPORT WINDOW -----------------------------

function OpenStatementReport()
{
	var winSize = setWindowSize();
	var srte="status=yes,scrollbars=NO,resizable=yes,top=0,left=0,height="+winSize[1]+",width="+winSize[0];
	winStatement=window.open(getUrl('01010401'),'Statement',srte);
}

//--------------------------- PRICER WINDOW --------------------------------

function OpenPricer()
{
	var winSize = setWindowSize();
	var srte="status=yes,scrollbars=NO,resizable=yes,top=0,left=0,height="+winSize[1]+",width="+winSize[0];
	winPricer=window.open(getUrl('01030501'),'Pricer',srte);
}
//---------------------------------------------------------------------------------

function CloseAllWindows()
{ 
	try {fxnewwin.CloseAllWindows();} catch(e){}
	try {fxnewwin.QueryDet.newwin.close();} catch(e){} 
	try {fxnewwin.QueryDet.modwin.close();} catch(e){} 
	try {fxnewwin.close();} catch(e){} 
	try {fxnewwin2.close();} catch(e){} 
	try {winOrders.close();} catch(e){}
	try {winAllClients.CloseAllWindows();} catch(e){}
	try {winAllClients.close();} catch(e){}
	try {winStress.close();} catch(e){}
	try {winStatement.close();} catch(e){}
	try {winClientPos.close();} catch(e){}
	try {winPricer.close();} catch(e){}
	try {winBizReport.close();} catch(e){}
	try {winCS1.close();} catch(e){}
	try {winCS2.close();} catch(e){}
	try {winCS3.close();} catch(e){}
	try{	if(top.E4xWin) top.E4xWin.close();	}catch(e){}
	try{	if(top.SubsidiariesWin) top.SubsidiariesWin.close();	}catch(e){}
	try{	if(top.keeperWin) top.keeperWin.close();	}catch(e){}
	try{	if(top.CounterReport) top.CounterReport.close();	}catch(e){}
	try{ if(top.CommW) top.CommW.close(); }catch(e){}
	try{ if(top.CounterPartCcyW) top.CounterPartCcyW.close(); }catch(e){}
	try{ if(top.Limit_fxW) top.Limit_fxW.close(); }catch(e){}
	try{ if(top.LimitMonitoringW) top.LimitMonitoringW.close(); }catch(e){}
	try{ if(top.MarginsReportsW) top.MarginsReportsW.close(); }catch(e){}
	try{ if(top.OptDeskW) top.OptDeskW.close(); }catch(e){}
	try{ if(top.RcmReportsW) top.RcmReportsW.close(); }catch(e){}
	try{ if(top.VolumeW) top.VolumeW.close(); }catch(e){}
	try{ if(top.OptDeskSimW) top.OptDeskSimW.close(); }catch(e){}
	try{ if(top.SensitivityTestW) top.SensitivityTestW.close(); }catch(e){}

}

//=======================================================

var gbNav6=false;
var gbNav61=false;
var gbNav4=false;
var gbIE4=false;
var gbIE=false;
var gbIE5=false;
var gbIE55=false;

var gAgent=navigator.userAgent.toLowerCase();
var gbMac=(gAgent.indexOf("mac")!=-1);
var gbSunOS=(gAgent.indexOf("sunos")!=-1);
var gbOpera=(gAgent.indexOf("opera")!=-1);

var HH_DISPLAY_TOPIC = 0;
var HH_DISPLAY_TOC = 1;
var HH_DISPLAY_INDEX = 2;
var HH_DISPLAY_SEARCH = 3;
var HH_HELP_CONTEXT = 15;

var gVersion=navigator.appVersion.toLowerCase();

var gnVerMajor=parseInt(gVersion);
var gnVerMinor=parseFloat(gVersion);

gbIE=(navigator.appName.indexOf("Microsoft")!=-1);
if(gnVerMajor>=4)
{
	if(navigator.appName=="Netscape")
	{
		gbNav4=true;
		if(gnVerMajor>=5)
			gbNav6=true;
	}
	gbIE4=(navigator.appName.indexOf("Microsoft")!=-1);
}
if(gbNav6)
{
	document.gnPageWidth=innerWidth;
	document.gnPageHeight=innerHeight;
	var nPos=gAgent.indexOf("netscape");
	if(nPos!=-1)
	{
		var nVersion=parseFloat(gAgent.substring(nPos+10));
		if(nVersion>=6.1)
			gbNav61=true;
	}
}else if(gbIE4)
{
	var nPos=gAgent.indexOf("msie");
	if(nPos!=-1)
	{
		var nVersion=parseFloat(gAgent.substring(nPos+5));
		if(nVersion>=5)
			gbIE5=true;
		if(nVersion>=5.5)
			gbIE55=true;
	}
}

function RH_ShowHelp(hParent, a_pszHelpFile, uCommand, dwData)
{
	// this function only support WebHelp
	var strHelpPath = a_pszHelpFile;
	var strWnd = "";
	var nPos = a_pszHelpFile.indexOf(">");
	if (nPos != -1)
	{
		strHelpPath = a_pszHelpFile.substring(0, nPos);
		strWnd = a_pszHelpFile.substring(nPos+1); 
	}
	if (isServerBased(strHelpPath))
		RH_ShowWebHelp_Server(hParent, strHelpPath, strWnd, uCommand, dwData);
	else
		RH_ShowWebHelp(hParent, strHelpPath, strWnd, uCommand, dwData);
}

function RH_ShowWebHelp_Server(hParent, strHelpPath, strWnd, uCommand, dwData)
{
	// hParent never used.
	ShowWebHelp_Server(strHelpPath, strWnd, uCommand, dwData);
}

function RH_ShowWebHelp(hParent, strHelpPath, strWnd, uCommand, dwData)
{
	// hParent never used.
	ShowWebHelp(strHelpPath, strWnd, uCommand, dwData);
}


function ShowWebHelp_Server(strHelpPath, strWnd, uCommand, nMapId)
{
	var a_pszHelpFile = "";
	if (uCommand == HH_HELP_CONTEXT)
	{
		if (strHelpPath.indexOf("?") == -1)
			a_pszHelpFile = strHelpPath + "?ctxid=" + nMapId;
		else
			a_pszHelpFile = strHelpPath + "&ctxid=" + nMapId;
	}
	else
	{
		if (strHelpPath.indexOf("?") == -1)
			a_pszHelpFile = strHelpPath + "?ctxid=0";
		else
			a_pszHelpFile = strHelpPath + "&ctxid=0";
	}

	if (strWnd)
		a_pszHelpFile += ">" + strWnd;

	if (gbIE4)
	{
		a_pszHelpFile += "&cmd=newwnd&rtype=iefrm";
		loadData(a_pszHelpFile);
	}
	else if (gbNav4)
	{
		a_pszHelpFile += "&cmd=newwnd&rtype=nswnd";
		var sParam = "left="+screen.width+",top="+screen.height+",width=100,height=100";
		window.open(a_pszHelpFile, "__webCshStub", sParam);
	}
	else
	{
		var sParam = "left="+screen.width+",top="+screen.height+",width=100,height=100";
		if (gbIE5)
			window.open("about:blank", "__webCshStub", sParam);
		window.open(a_pszHelpFile, "__webCshStub");
	}
}


function ShowWebHelp(strHelpPath, strWnd, uCommand, nMapId)
{
	var a_pszHelpFile = "";
	if (uCommand == HH_DISPLAY_TOPIC)
	{
		a_pszHelpFile = strHelpPath + "#<id=0";
	}
	if (uCommand == HH_HELP_CONTEXT)
	{
		a_pszHelpFile = strHelpPath + "#<id=" + nMapId;
	}
	else if (uCommand == HH_DISPLAY_INDEX)
	{
		a_pszHelpFile = strHelpPath + "#<cmd=idx";
	}
	else if (uCommand == HH_DISPLAY_SEARCH)
	{
		a_pszHelpFile = strHelpPath + "#<cmd=fts";
	}
	else if (uCommand == HH_DISPLAY_TOC)
	{
		a_pszHelpFile = strHelpPath + "#<cmd=toc";
	}
	if (strWnd)
		a_pszHelpFile += ">>wnd=" + strWnd;

	if (a_pszHelpFile)
	{
		if (gbIE4)
			loadData(a_pszHelpFile);
		else if (gbNav4)
		{
			var sParam = "left="+screen.width+",top="+screen.height+",width=100,height=100";
			window.open(a_pszHelpFile, "__webCshStub", sParam);
		}
		else
		{
			var sParam = "left="+screen.width+",top="+screen.height+",width=100,height=100";
			if (gbIE5)
				window.open("about:blank", "__webCshStub", sParam);
			window.open(a_pszHelpFile, "__webCshStub");
		}
	}
}

function isServerBased(a_pszHelpFile)
{
	if (a_pszHelpFile.length > 0)
	{
		var nPos = a_pszHelpFile.lastIndexOf('.');
		if (nPos != -1 && a_pszHelpFile.length >= nPos + 4)
		{
			var sExt = a_pszHelpFile.substring(nPos, nPos + 4);
			if (sExt.toLowerCase() == ".htm")
				return false;
		}
	}
	return true;
}

function getElement(sID)
{
	if(document.getElementById)
		return document.getElementById(sID);
	else if(document.all)
		return document.all(sID);
	return null;
}

function loadData(sFileName)
{
	if(!getElement("dataDiv"))
	{
		if(!insertDataDiv())
		{
			gsFileName=sFileName;
			return;
		}
	}
	var sHTML="";
	if(gbMac)
		sHTML+="<iframe name=\"__WebHelpCshStub\" src=\""+sFileName+"\"></iframe>";
	else
		sHTML+="<iframe name=\"__WebHelpCshStub\" style=\"visibility:hidden;width:0;height:0\" src=\""+sFileName+"\"></iframe>";

	var oDivCon=getElement("dataDiv");
	if(oDivCon)
	{
		if(gbNav6)
			if(oDivCon.getElementsByTagName&&oDivCon.getElementsByTagName("iFrame").length>0)
				oDivCon.getElementsByTagName("iFrame")[0].src=sFileName;
			else
				oDivCon.innerHTML=sHTML;
		else
			oDivCon.innerHTML=sHTML;
	}
}

function insertDataDiv()
{
	var sHTML="";
	if(gbMac)
		sHTML+="<div id=dataDiv style=\"display:none;\"></div>";
	else
		sHTML+="<div id=dataDiv style=\"visibility:hidden\"></div>";

	document.body.insertAdjacentHTML("beforeEnd",sHTML);
	return true;
}


function replaceMainHome(sw)
{
	var url=pathSite+"/home/";
	switch(sw)
	{
		case 'ch':
			url+="PGnewHOMEformulaCH.htm"
			break;
		case 'en':
			url+="PGnewHOMEformula.htm"
			break;
	}
	parent.frames("main").location.replace(url)
}


function OpenClientStatementsWindow(x, uid)
{
	var srte="status=yes,scrollbars=yes,resizable=yes,top=0,left=0,height="+eval(screen.availHeight-60)+",width="+eval(screen.availWidth-10);
	switch(x)
	{
		case 1:	//winCS1 = window.open(top.pathSite+'/TOL/app/ExOutPut/infowin.aspx?FromWhere=PRM&userID='+uid,'winCS1',srte); 
		winCS1 = window.open(top.pathSite+'/TOL/app/ExOutPut/infowinNew.aspx?FromWhere=PRM&userID='+uid,'winCS1',srte); 
		break;
		case 2:	winCS2 = window.open(top.pathSite+'/MFM/Reports(aspx)/TradActivity.aspx?FromWhere=PRM&userID='+uid,'winCS2',srte); break;
		//case 3:	winCS3 = window.open(top.pathSite+'/MFM/Reports(aspx)/ReportListRpt.aspx','winCS3',srte); break;
	}
}


//-----------------------------------------------------------
function setWindowSize()
{
	var h = (screen.availHeight>768) ? 708:screen.availHeight-60;
	var w = (screen.availWidth>1024) ? 1014:screen.availWidth-10;
	return new Array(w, h)
}
//-----------------------------------------------------------

function ShiftCurrentWin(theURL)
{
	top.window.location.href.replace(theURL);
}


function openBizReport()
{
	var winSize = setWindowSize();
	GLTable=80;
	GLTitle="BsnsExp";
	GLHidsql = ",999,9999,1,9999";
	GLFromLocation='Query';
	showRightMenu=0;
	var srte="status=yes,scrollbars=yes,resizable=yes,top=0,left=0,height="+winSize[1]+",width="+winSize[0];
	winBizReport = window.open('../../app/Reports/BusinessReports/BusinessReports.htm','winBizReport',srte);
}