var const_Amp           = "&";
var AccntGroupEquiv 	= "AccntGroup=";
var AccntEquiv	 		= "Action=";
var const_GD		  	= "GD";
var const_GEO		  	= "GEO";
var const_Null		  	= "NULL";
var const_ConfirmLofOff = "Do you want to close all applications and logoff?";

var action = 'Trade';
var gd = 'Domestic';

var const_Img_MsgNewNo = "/tpe/modules/site/img/msg_newno.gif";
var const_Img_MsgNew = "/tpe/modules/site/img/msg_new.gif";
var const_Img_MsgNewUrgent = "/tpe/modules/site/img/msg_newurgent.gif";

// used for bottom frame to lighting td in top frame
var SelectedObject = null;

//expItemNum value of selected td object
var SelectedItem = -1;

var const_DMGRNotLoaded = 0;
var const_DMGRSyncRequest = 1;
var const_DMGRAsyncRequest = 2;

var nIntervalID_InboxStatus = -1;
var nIntervalID_SignOut = -1;

var const_InaccessibleAction = "InaccessibleAction";

window.onresize = OnResize;

/*-----------------------------------------------------------------*/
/*                             Cancel Event bubbling               */
/*-----------------------------------------------------------------*/
function doNothing()
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}

  if(top.browser == top.IE)
    window.event.cancelBubble = true;
  return false;
}	

/*-----------------------------------------------------------------*/
/*                             Error handling                      */
/*-----------------------------------------------------------------*/
window.onerror = displayError;

function displayError(msg, url, line)
{
	alert("MSG=" + msg + "\nurl=" + url + "\nline=" + line + "\n");
	return true;
}

/*-----------------------------------------------------------------*/
/*                             Menu Lighting                       */
/*-----------------------------------------------------------------*/
function MenuItemLighting(obj)
{

	
  	var TR = obj.parentElement;
  	//get td menu objects count
  	var count = TR.children.length;
  	for(var i = 1; i < count-2; i+=2 )
  	{
    	var curitem = TR.children.item(i); 
    	
		//skip InaccessibleAction
		if(curitem.className == const_InaccessibleAction)
		{
			continue;
		}
    	
    	curitem.className = "middle";
    	if(curitem.children.length > 0)
    	{
    		curitem.children.item(0).style.color = '#ffffff';
    	}
      
  	}
	obj.className = "middleSelected";
	if(obj.children.length>0)
	{
		obj.children.item(0).style.color = '#000084';
	}
	SelectedItem = obj.expItemNum;
}


function MenuMouseOver(obj)
{

  	if(obj.isSelected != 'Y')
  	{
    	obj.className='middleOver';
    	/*if(obj.children.length>0)
    	{
	    	obj.children.item(0).style.color = '#FFFF00';
    	}*/
      
 	}
  	
  	return true;
}

function MenuMouseOut(obj)
{
	

	if(obj.isSelected != 'Y')
  	{
    	obj.className="middle";
    	/*if(obj.children.length>0)
    	{
    		obj.children.item(0).style.color = '#FFFFFF';
    	}*/
      	
  	}
  	
  	return true;
}
//click top menu item
function fnMenuClick(obj)
{
	
	var oldSelectObject = SelectedObject;
	SelectedObject = obj;
	SelectedItem = obj.expItemNum;
	SelectedObject.className='middleSelected';
	if(obj.children.length>0)
	{
	     obj.children.item(0).style.color = '#FFFFFF';
    	}
    	return false;
}



function LightingSelectedObject()
{
	
    try
    {
        if(SelectedObject)
        {
            MenuItemLighting(SelectedObject);
        }
    }
    catch(exception)
    {
    }
}


function ResetLightingSelectedObject()
{
	
	
	try
	{
		if(SelectedObject)
		{
			SelectedObject.className = "middle";
			if(SelectedObject.children.length>0)
			{
				SelectedObject.children.item(0).style.color = '#ffffff';
			}
			SelectedObject = 0;
			SelectedItem = 0;
		}
	}
	catch(exception)
	{
	}
}


function SelectObject(action, linkaddr)
{
	var TR = document.all("MenuData");
  	var count = TR.children.length;
  	for(var i = 1; i < count-2; i+=2 )
  	{
    	var curitem = TR.children.item(i); 
    	if(curitem.path.toLowerCase() == action.toLowerCase() && linkaddr == curitem.linkaddr)
  		{
  			SelectedObject = curitem;
  			SelectedItem = curitem.expItemNum;
  			break;
  		}
  		
    }
}

/*-----------------------------------------------------------------*/
/*                             Build menu                          */
/*-----------------------------------------------------------------*/
//on change select group(Domestic/Global)
function fnGDChange(geo)
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}
	top.PageDisabled = true;
	
	
	var url = const_BuildDropDownMenu;
	//geo
	url += "&geo=" + HTTPEncode(geo);	
	
	top.frames[topFrameTitle].location = url;
	goNews();

}

//on change select Action
function fnActionChange(geo, staff)
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}
	top.PageDisabled = true;

	var url = const_BuildDropDownMenu;
	//geo
	url += "&geo=" + HTTPEncode(geo);	
	//staff
	url += "&staff=" + HTTPEncode(staff);	
	top.frames[topFrameTitle].location = url;
	goNews();
}


/*-----------------------------------------------------------------*/
/*                  Go functions
/*-----------------------------------------------------------------*/
function goHome()
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}

	goNews();
}

function goNews()
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}

	var requestURL = BuildDynamicTemplateRequest(const_goNews, const_xslNews);
//	setLowerFrameURL(requestURL);
	OpenDataFrame(requestURL);
}

function goUsers()
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}

	var xml = const_goUsers + getUserSessionId();
	var requestURL = BuildDynamicTemplateRequest(xml, const_xslUsers);
//	setLowerFrameURL(requestURL);
	OpenDataFrame(requestURL);
}

function goMessages()
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}
	
	var xml = const_goMessages;
	xml += "&maxcount=" + "10";
	xml += "&username=" + getCurrentUser();
	xml += "&sortfield=" + "ID";
	xml += "&sortorder=" + "1";
	var requestURL = BuildDynamicTemplateRequest(xml, const_xslMessages);
//	setLowerFrameURL(requestURL);
	OpenDataFrame(requestURL);
}

function goRecentFiles()
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}
	
	var requestURL = BuildDynamicTemplateRequest(const_goRecentFiles, const_xslRecentFiles);
//	setLowerFrameURL(requestURL);
	OpenDataFrame(requestURL);
}

function goSearch()
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}

	var request = const_goSearch;
	request += "&loadcached=1";
	var requestURL = BuildDynamicTemplateRequest(request, const_xslSearch);
//	setLowerFrameURL(requestURL);
	OpenDataFrame(requestURL);
}

function goSignOut()
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}

	if(confirm(const_ConfirmLofOff))
	{
		DMGRCloseApplications();
		nIntervalID_SignOut = setInterval("SignOut()", 1*1000);
	}  
}


function SignOut()
{
	clearTimeout(nIntervalID_SignOut);
	top.location = const_goSignOut;
}

/*-----------------------------------------------------------------*/
/*                  Additional functions                           */
/*-----------------------------------------------------------------*/
function StartProgram(program)
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}

	eval(program);
}

function CheckKey()
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}

  if(window.event.keyCode == 120){
    if(DMGRLoaded(Manager))
      StartProgram(top.const_StartTEC);
      
    window.event.cancelBubble=true;
  }else{
	doNothing();
  }
}

function CheckRunApp()
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}

  if(top.bRunApp){
    top.bRunApp = false;
    if(DMGRLoaded(Manager))
      StartProgram(top.const_StartTEC);
  }
}

document.ondragstart = doNothing;
document.onselect = doNothing;


function Unloading()
{
}

function SyncFrames()
{
	try
	{
		var doc = frames[lowerFrameTitle];
		if(doc && doc.NeedSync)
		{
			doc.SelectAction();
		}
	}
	catch(exception)
	{
	}
}

function Loading()
{
	try
	{
		top.PageDisabled = false;
		top.LoadedCompletely = true;
		geo.disabled = false;
		staff.disabled = false;
		OnResize();
		SyncFrames();	
		//show user sessions page for specified users only
		ShowUserSessionsPage();
		//show messenger page for specified users only
		ShowMessengerPage();
		
		if(varShowEngineStatus == -1)
		{
			document.all("EngineStatusTD").style.display = "none";
		}
		else
		{
			if(varShowEngineStatus != -2)
			{
				drawMultiEngineStatus("", 1);
			}
			else
			{
				EnginesNum =1;
				drawSingleEngineStatus("", 1);
			}
			EngineStatus();
			setInterval("EngineStatus()", 30*1000);
		}	
		if(varShowMessengerPage == 1)
		{
			getMessages();
			if(DMGRAsyncRequest() == const_DMGRAsyncRequest)
			{
				setInterval("getMessages()", 30*1000);
			}
		}
	}
	catch(exception)
	{
	}
}

function ShowUserSessionsPage()
{
	try
	{
		if(varShowUserSessionsPage == 1)
		{
			var UsersPage = document.all("Users Page");						
			if(UsersPage)
			{
				UsersPage.style.display = "inline";			
			}
			var UsersPageSeparator = document.all("Users Separator");						
			if(UsersPageSeparator)
			{
				UsersPageSeparator.style.display = "inline";			
			}
		}
	}
	catch(exception)
	{
	}
}

function ShowMessengerPage()
{
	try
	{
		if(varShowMessengerPage == 1)
		{
			var UsersPage = document.all("Messenger Page");						
			if(UsersPage)
			{
				UsersPage.style.display = "inline";			
			}
			var UsersPageSeparator = document.all("Messenger Separator");						
			if(UsersPageSeparator)
			{
				UsersPageSeparator.style.display = "inline";			
			}
		}
	}
	catch(exception)
	{
	}
}


function ComboBoxMouseOut()
{
}


function getCookie(CookieName) {
   var WholeCookie = document.cookie;
   var index = WholeCookie.indexOf(CookieName + "=");
   if (index == -1) return "";
   index = WholeCookie.indexOf("=", index) + 1;
	var endstr = WholeCookie.indexOf(";", index);
	if (endstr == -1) endstr = WholeCookie.length;
	var IndividualCookie = unescape(WholeCookie.substring(index, endstr));

   if (IndividualCookie == null || IndividualCookie == "null" ||
IndividualCookie == "" || IndividualCookie.indexOf("undefined", 0) >= 0 ||
IndividualCookie.lastIndexOf("=") == IndividualCookie.length - 1) {
      IndividualCookie = ""
   }
   return IndividualCookie
}

function NormalizeNumber(str)
{
	var s = str.toString();
	if(s.length != 2)
	{
		s = "0" + s;
	}
	return s;
}

function drawInboxStatus(InboxStatus)
{
	var pos = InboxStatus.indexOf(":");	
	var msgcount = 0;
	var msgurgentcount = 0;
	if(pos != -1)
	{
		msgcount = parseInt(InboxStatus.substr(0, pos));
		msgurgentcount = parseInt(InboxStatus.substr(pos + 1));
	}
	var msgline = "";		
	var img = const_Img_MsgNewNo;
	if(msgcount)
	{
		msgline = msgcount.toString();	
		if(msgurgentcount)
		{
			msgline += "(" + msgurgentcount.toString() + ")";	
			img = const_Img_MsgNewUrgent;
		}
		else
		{
			img = const_Img_MsgNew;
		}
	}
	document.all("MsgImg").src = img;	
	document.all("MsgCount").innerText = msgline;	
}

function getMessages()
{
	if(bChecking_InboxStatus)
	{
		return false; 
	}
	try
	{
		bChecking_InboxStatus = true;
		var CurrentUser = getCurrentUser();
		var request = const_CheckMessages;
		request += "&maxcount=" + "0";
		request += "&username=" + CurrentUser;
		var url_InboxStatus = getProtocol() + location.host + request;
		
		var requestType = DMGRAsyncRequest();
		var Response = DMGRCallUrl(url_InboxStatus);
		if(requestType == const_DMGRSyncRequest)
		{
			drawInboxStatus(Response);	
			bChecking_InboxStatus = false;			
		}
		if(requestType == const_DMGRAsyncRequest)
		{
			nIntervalID_InboxStatus = setInterval("ReadInboxStatus()", 500);		
		}
	}
	catch(exception)
	{
		bChecking_InboxStatus = false;
	}
}

function ReadInboxStatus()
{
	var CurrentUser = getCurrentUser();
	var request = const_CheckMessages;
	request += "&maxcount=" + "0";
	request += "&username=" + CurrentUser;
	var url_InboxStatus = getProtocol() + location.host + request;
	var Response = DMGRReadResponse(url_InboxStatus);
	if(Response.length)
	{
		bChecking_InboxStatus = false;		
		clearInterval(nIntervalID_InboxStatus);
		drawInboxStatus(Response);
	}
}


var const_ImageArrowGroupShow = "/tpe/modules/site/img/arrow_down.gif";
var const_ImageArrowGroupHide = "/tpe/modules/site/img/arrow_right.gif";
var const_ImageArrowPathShow 	= "/tpe/modules/site/img/arrow_down1.gif";
var const_ImageArrowPathHide 	= "/tpe/modules/site/img/arrow_right1.gif";

function ExpandAll()
{
	var count = 1;
	var doc = 	frames[lowerFrameTitle].document;
	while(true)
	{
		var tabID = "tab" + count.toString();
		var imgID = "image" + count.toString();
	    var tabObj = doc.all(tabID);
	    var imgObj = doc.all(imgID);
	    if(!tabObj || !imgID)
	    {
			break;	    
	    }
		tabObj.showTab = true;
		tabObj.className = 'MenuGroupShow';
		if(imgObj.pathimage)
		{
			imgObj.src = const_ImageArrowPathShow;
		}
		else
		{
			imgObj.src = const_ImageArrowGroupShow;
		}
		count++;
	}
}

function CollapseAll()
{
	var count = 1;
	var doc = 	frames[lowerFrameTitle].document;
	while(true)
	{
		var tabID = "tab" + count.toString();
		var imgID = "image" + count.toString();
	    var tabObj = doc.all(tabID);
	    var imgObj = doc.all(imgID);
	    if(!tabObj || !imgID)
	    {
			break;	    
	    }
		tabObj.showTab = false;
		tabObj.className = 'MenuGroupHide';
		if(imgObj.pathimage)
		{
			imgObj.src = const_ImageArrowPathHide;
		}
		else
		{
			imgObj.src = const_ImageArrowGroupHide;
		}
		count++;
	}
}


function OnResize()
{
	/*
	if(document.lb)
	{
		setLbPos();
	}
	var frameSite = document.all("LowerFrameSite");
	frameSite.style.posHeight = document.body.clientHeight - frameSite.offsetTop;
       */
}


function ReloadComboBox(opt, name)
{
	if(top.PageDisabled || !top.LoadedCompletely)
	{
		return false;
	}

	if(name == 0 && selectedGeo == opt || name == 1 && selectedStaff == opt)
	{
		return false;	
	}
	geo.disabled = true;
	staff.disabled = true;
	if(name == 0)
	{
		fnGDChange(opt);
	}
	if(name == 1)
	{
		fnActionChange(geo.hidden.value, opt);
	}
};
