/******************************************************************************/
/*** EVENT CAPTURE ***/

document.body.onload = initRB;
document.onclick = checkPrevent;
document.oncontextmenu = filterClicks;
document.onkeydown = disableBrowserKeyControl;
document.onsubmit = disableClicks;
document.body.onbeforeunload = disableClicks;
document.body.onunload = endRB;

/******************************************************************************/
/*** GLOBAL VARIABLES ***/

var ie4Browser = document.all&&navigator.userAgent.indexOf("Opera")==-1;
var ns6Browser = document.getElementById&&!document.all;
var ns4Browser = document.layers;

var menuItems = new Array();
var menuLevel2Items = new Array();
var menuItemsLen = 0;
var menuLevel2Length = 0;

var menuReadyFlag = false;
var menuTimer = 0;
var tipsTimer = 0;
var helpMenuTimer = 0;

var menuTimeout = 1000;

var lastMenuObj = "";

var DropDownMenuShadowFilter = "progid:DXImageTransform.Microsoft.DropShadow(offx=1, offy=1, positive=1, color=#777777)";
var DropDownShadowFilter = "progid:DXImageTransform.Microsoft.DropShadow(offx=3, offy=3, positive=1, color=#777777)";

var assistFlag = false;
var largeExpDate = new Date();
var alpha025 = "progid:DXImageTransform.Microsoft.Alpha(opacity='25')";
var alpha050 = "progid:DXImageTransform.Microsoft.Alpha(opacity='50')";
var alpha075 = "progid:DXImageTransform.Microsoft.Alpha(opacity='75')";
var alpha100 = "progid:DXImageTransform.Microsoft.Alpha(opacity='100')";

var imagePressed = false;
var codeRed = false;

var warningTimer;
var endTimer;

var warnTime = 1080000;
var expiryTime = 1200000;

var specificOccasionsFlag = false;
var occasionActive = false;
var occasionURL = "";
var occasionSubmitForm = "";
var occasionStatus = false;
var occasionAction = false;

var constantNonHoliday = "0";
var constantHoliday = "0";
var constantDisclaimerMsg = "0";
var constantDisableFunction = "0";
var constantEnableFunction = "0";

if((typeof tradeTimeoutWarn != "undefined") && (typeof tradeTimeoutExpiry != "undefined"))
{
	//inside trade only
	warnTime = tradeTimeoutWarn;
	expiryTime = tradeTimeoutExpiry;
}

/******************************************************************************/
/*** COMMON FUNCTIONS ***/

// Code was written by Tyler Akins and is placed in the public domain

var base64_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encodeString(input) 
{
   var output = "";   var chr1, chr2, chr3;   var enc1, enc2, enc3, enc4;   var i = 0;
   do {chr1 = input.charCodeAt(i++);chr2 = input.charCodeAt(i++);chr3 = input.charCodeAt(i++);
      enc1 = chr1 >> 2;enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63;
      if (isNaN(chr2)) {enc3 = enc4 = 64;} else if (isNaN(chr3)) {enc4 = 64; }
      output = output + base64_keyStr.charAt(enc1) + base64_keyStr.charAt(enc2) + base64_keyStr.charAt(enc3) + base64_keyStr.charAt(enc4);
   } while (i < input.length);   
   return output;
}
function encryptQueyString (_url)
{
	var index  = _url.indexOf("?");
	var qs = "";		
	var url = _url;
	if(index != -1)
	{
		qs = _url.substr(index+1);
		url = _url.substr(0,index+1);
	}
	var encodedQs = encodeString(qs);	
	var result = url + "_EP=" +encodedQs;	
	return result;
}


function initRB()
{
	if(theCurrentPageDir == "rtl")
	{
		DropDownMenuShadowFilter = "progid:DXImageTransform.Microsoft.DropShadow(offx=-1, offy=1, positive=1, color=#777777)";
		DropDownShadowFilter = "progid:DXImageTransform.Microsoft.DropShadow(offx=-3, offy=3, positive=1, color=#777777)";
	}

	if(document.getElementById("ENZAR") != null) startTimer();

	largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));

	if(document.getElementById("cacheID") != null) cacheOn();

	if(document.getElementById("FirstFocus") != null) document.getElementById("FirstFocus").focus();

	if(document.getElementById("ASSIST") != null)
		if(document.getElementById("ASSIST_TXT").innerText != "")
			document.body.onscroll = relocateAssist;

	if(typeof announcementFlag != "undefined")
	{
		var announcementWindow = window.open("/RBWeb/announcement.jsp","announcementWindow","height=580,width=796,status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=yes");
	}

	var cookieValue = GetCookie("wantAssistance");
	if(cookieValue == "ON" && document.getElementById("TIPS_IMG") != null) assistPage();

	if(document.getElementById("MAIN_MENU") != null) createMenu();

	if(document.getElementById("PRINT_PAGE") != null) self.print();

	startUp();
}

function startUp()
{
}

function endRB()
{
	endUp();

	if(document.getElementById("ENZAR") != null) killTimers();
}

function endUp()
{
}

function tip()
{
}

function isIE5()
{
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		return true;
	}
	else
	{
		return false;
	}
}

/******************************************************************************/
/*** EVENT HANDLERS ***/

function filterClicks()
{
	var srcTagName = event.srcElement.tagName;
	srcTagName.toLowerCase();
	if(!event.ctrlKey)
	{
		if(srcTagName == "input" || srcTagName == "textarea")
			return true;
		else
			return false;
	}
}

function checkPrevent()
{
	if(event.srcElement.prevent == "no")
		document.body.onbeforeunload = "";
	else
		document.body.onbeforeunload = disableClicks;
}

function preventNavigation()
{
	imagePressed = true;
	return false;
}

function disableClicks()
{
	try
	{
		imagePressed = true;
		codeRed = true;

		if(document.getElementById("HDR_LINKZ") != null) document.getElementById("HDR_LINKZ").style.display = "none";
		if(document.getElementById("HDR_LINKS1") != null) document.getElementById("HDR_LINKS1").style.display = "none";
		if(document.getElementById("HDR_LINKS2") != null) document.getElementById("HDR_LINKS2").style.display = "none";
		if(document.getElementById("HDR_LINKS3") != null) document.getElementById("HDR_LINKS3").style.display = "none";
		if(document.getElementById("HDR_LINKS4") != null) document.getElementById("HDR_LINKS4").style.display = "none";
		if(document.getElementById("HDR_USER_INFO") != null) document.getElementById("HDR_USER_INFO").style.display = "none";
		if(document.getElementById("HDR_CLOCK") != null) document.getElementById("HDR_CLOCK").style.display = "none";

		shadowHideYou(alpha025, true);

		if(document.getElementById("RB_HEADER") != null)
		{
			var scrolledAmount = document.body.scrollTop;
			document.getElementById("RB_HEADER").focus();
			window.scrollBy(0, scrolledAmount);
		}

		var bodyObj = document.getElementById("RB_BODY");
		var shadowObj = document.getElementById("SHADOW");
		var darknessObj = document.getElementById("DARKNESS");
		var dropDownLists = document.getElementsByTagName("SELECT");

		var bodyHeight = bodyObj.offsetHeight;

		shadowObj.style.display = "block";

		shadowObj.style.top = 0;
		shadowObj.style.height = bodyHeight - 2;
		darknessObj.style.top = 0;
		darknessObj.style.height = bodyHeight - 2;

		for(var i = 0; i < menuLevel2Length; i++)
		{
			if(document.getElementById("L2_"+i) != null) document.getElementById("L2_"+i).style.visibility = "hidden";
		}

		if(document.getElementById("HELP_MENU_1") != null) document.getElementById("HELP_MENU_1").style.visibility = "hidden";
		if(document.getElementById("HELP_MENU_2") != null) document.getElementById("HELP_MENU_2").style.visibility = "hidden";
		if(document.getElementById("MENU_TIPS") != null) document.getElementById("MENU_TIPS").style.visibility = "hidden";
		if(document.getElementById("ASSIST") != null) document.getElementById("ASSIST").style.visibility = "hidden";
		if(document.getElementById("ENZAR") != null) document.getElementById("ENZAR").style.visibility = "hidden";

		for(var i = 0; i < dropDownLists.length; i++)
		{
			dropDownLists[i].style.visibility = "hidden";
		}

		var iFramesCollection = document.getElementsByTagName("IFRAME");
		var iFramesCollectionLen = iFramesCollection.length;

		if(iFramesCollectionLen != 0)
		{
			for(var j = 0; j < iFramesCollectionLen; j++)
			{
				var anID = iFramesCollection[j].id;
				var collective = document.frames(anID);

				spanCollection = collective.document.getElementsByTagName("SELECT");

				for(var i = 0; i < spanCollection.length; i++)
				{
					spanCollection[i].style.visibility = "hidden";
				}
			}
		}
	}
	catch(e)
	{
		// ignore
	}
}

function disableBrowserKeyControl(event)
{
	if(window.event.keyCode == 8)
	{
		var srcEle = window.event.srcElement;
		var srcTagName = srcEle.tagName.toLowerCase();

		try
		{
			var srcType = srcEle.type.toLowerCase();
		}
		catch(e)
		{
			var srcType = "N/A";
		}

		if((srcTagName == "input" && srcType == "text") || (srcTagName == "input" && srcType == "password") || srcTagName == "textarea")
		{
			return true;
		}
		return false;
	}
	else if(window.event.keyCode == 116 || window.event.keyCode == 122)
	{
		window.event.keyCode = 505;
		return false;
	}
}

function shadowHideYou(alphaFilter, allFlag)
{
	var DIVs = document.getElementsByTagName("DIV");

	for(var i = 0; i < DIVs.length; i++)
	{
		if((DIVs[i].id == "RB_HEADER" || DIVs[i].id == "LOGOUT_BTN" || DIVs[i].id == "ENZAR" 
		|| DIVs[i].id == "SHADOW" || DIVs[i].id == "DARKNESS" || DIVs[i].id == "HDR_CLOCK" 
		|| DIVs[i].id == "HDR_LINKZ" || DIVs[i].id == "HDR_MSG" || DIVs[i].id == "HDR_USER_INFO" 
		|| DIVs[i].id == "HDR_USER_NAME" || DIVs[i].id == "HELP_MENU_1" || DIVs[i].id == "HELP_MENU_2") && !allFlag)
		{
			continue;
		}
		else
		{
			DIVs[i].style.filter = alphaFilter;
		}
	}
}

function captureEnter()
{
	var pressedKeyCode = window.event.keyCode;

	if(pressedKeyCode == 13)
	{
		check();
		return false;
	}
}

function check()
{
}

/******************************************************************************/
/*** VISUAL EFFECTS ***/

var shadowX = "";
var alphaX = "";

function customDDShadow(offsetX, offsetY, pos, colorHex)
{
	if(isNaN(offsetX)) offsetX = 2;
	if(isNaN(offsetY)) offsetY = 2;
	if(isNaN(positive)) positive = 1;
	if(isNaN(colorHex)) colorHex = "#777777";

	if(theCurrentPageDir == "rtl") offsetX = eval(offsetX * -1);

	shadowX = "progid:DXImageTransform.Microsoft.DropShadow(offx="+offsetX+", offy="+offsetY+", positive="+positive+", color="+colorHex+")";

	return shadowX;
}

function customAlpha(opacityX)
{
	if(isNaN(opacityX)) offsetX = 50;

	alphaX = "progid:DXImageTransform.Microsoft.Alpha(opacity='"+offsetX+"')";

	return alphaX;
}

function shadowHideYou(alphaFilter, allFlag, objID)
{
	var DIVs = document.getElementsByTagName("DIV");

	for(var i = 0; i < DIVs.length; i++)
	{
		if((DIVs[i].id == "RB_HEADER" || DIVs[i].id == "LOGOUT_BTN" || DIVs[i].id == "ENZAR" 
		|| DIVs[i].id == "SHADOW" || DIVs[i].id == "DARKNESS" || DIVs[i].id == objID) && !allFlag)
		{
			continue;
		}
		else
		{
			DIVs[i].style.filter = alphaFilter;
		}
	}
}

function fog(alphaValue, flag, objID, mystFlag)
{
	var shadowDisplay = "none";
	var shadowHeight = 1;
	var darknessHeight = 1;
	var dropDownListsVisibility = "visible";

	var dropDownLists = document.getElementsByTagName("SELECT");
	var dropDownListsDisplay = "hidden";

	var bodyObj = document.getElementById("RB_BODY");
	var shadowObj = document.getElementById("SHADOW");
	var darknessObj = document.getElementById("DARKNESS");

	var bodyHeight = bodyObj.offsetHeight;

	shadowHideYou(alphaValue, flag, objID);

	if(mystFlag)
	{
		shadowDisplay = "block";
		shadowHeight = bodyHeight - 2;
		darknessHeight = bodyHeight - 2;
		dropDownListsVisibility = "hidden";
	}

	shadowObj.style.display = shadowDisplay;
	shadowObj.style.height = shadowHeight;
	darknessObj.style.height = darknessHeight;

	for(var i = 0; i < dropDownLists.length; i++)
	{
		dropDownLists[i].style.visibility = dropDownListsVisibility;
	}
}


/******************************************************************************/
/*** COOKIE MONSTER ***/

function getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if(endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function SetCookie(name, value)
{
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;

	document.cookie = name + "=" + escape(value) + "; path=/";
}

function getLangCookie(name)
{
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;

	if((!start) && (name != document.cookie.substring(0, name.length))) return null;

	if(start == -1) return null;

	var end = document.cookie.indexOf(";", len);

	if(end == -1) end = document.cookie.length;

	return unescape(document.cookie.substring(len, end));
}

/******************************************************************************/
/*** WINDOW HANDLERS ***/

function downloadWindow(aURL,windowName)
{
	windowName = window.open(aURL,windowName,"height=480,width=640,status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=yes");
	windowName.blur();
}

function redirect_to_url(aURL)
{
	if(aURL != null)
	{
		if(!codeRed && aURL != "none")
		{
			disableClicks();
			codeRed = true;
			window.location.assign(aURL);
		}
	}
}

function balancePage(aURL)
{
	redirect_to_url(aURL);
}

function welcomeBack()
{
	redirect_to_url(welcomeURL);
}

function openXwindow(aURL, winName, winSpecs)
{
	if(aURL != null)
	{
		if(winName == null) winName = "";
		if(winSpecs == null) winSpecs = "";
		window.open(aURL, winName, winSpecs);
	}
}

function openURL(aURL)
{
	if(aURL != null) window.open(aURL, "");
}

function open_a_url(aURL)
{
	if(aURL != null) window.location.assign(aURL);
}

function submitThisForm(someForm, someFormTarget, someFormAction)
{
	if(someForm != null)
	{
		if(someFormTarget != null) someForm.target = someFormTarget;
		if(someFormAction != null) someForm.action = someFormAction;
		someForm.submit();
	}
}

function endRBSession(aURL)
{
	if(confirm(confirmLogoutMsg)) redirect_to_url(aURL);
}

function printHandle(aURL, submitFlag, theForm)
{
	if(submitFlag != null || submitFlag == true)
	{
		var printWindow = window.open("/RBWeb/common/images/dot.gif","PrintWindow","top=0,left=0,height=580,width=800,status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=yes");

		theForm.action = aURL;
		theForm.target = "PrintWindow";
		theForm.submit();
		theForm.target = "_self";
	}
	else
	{
		var printWindow = window.open(aURL,"PrintWindow","height=540,width=779,status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=yes");
	}
}

function helpHandle(aURL)
{
	var helpWindow = window.open(aURL,"HelpWindow","height=540,width=779,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=no");
}

function helpFunction(aURL)
{
	var helpWindow = window.open(aURL,"helpWindow","height=540,width=779,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=no");
}

function glossaryFunction(aURL)
{
	var helpWindow = window.open(aURL,"GlossaryWindow","height=540,width=796,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
}

function helpIndex(aURL)
{
	var glossaryWindow = window.open(aURL,"TMMWindow","height=540,width=779,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
}

/******************************************************************************/
/*** SPECIFIC TO MODULES ***/

function resizeMainWin(ignoreFlag)
{
	var bodyHeight = document.getElementById("RB_MAIN").offsetHeight + 62;
	var bodyWidth = document.getElementById("RB_MAIN").offsetWidth + 29;
	var clientScreenHeight = screen.height;
	var maxHeight = 400;

	if(clientScreenHeight >= 1024) maxHeight = 994;
	else if(clientScreenHeight >= 768) maxHeight = 738;
	else if(clientScreenHeight >= 600) maxHeight = 570;
	else if(clientScreenHeight >= 480) maxHeight = 450;

	if(ignoreFlag == null) bodyHeight += 17;

	if(bodyHeight > maxHeight) bodyHeight = maxHeight;

	try
	{
		window.resizeTo(bodyWidth, bodyHeight);
	}
	catch(e)
	{
		setTimeout("resizeMainWin()", 1250);
	}
}

/******************************************************************************/
