
var APP_MIN_HEIGHT = 300;
var APP_MIN_WIDTH = 200;
var docWidth = 0;
var docHeight = 0;

var objHeaderContainer;
var objMainContainer;
var objFooterContainer;

var blnMenuVisible = false;

var blnNs = false;
var blnIe = false;

var myobj;
var objLastRowClicked = false;
var objLastProdMenuVisible = false;
var objLastButtonClicked = false;
var lastListClicked = false;
var lastRowClicked = false;

var arrBarGlobal = new Array("Main","Products","Portfolios","Reports","Admin","View","Help","Logout");

var optionSelBgColor = "#13829d";
var optionSelTxtColor = "#ffffff";

var btnSelectOff = new Image();
var btnSelectOn = new Image();
var imgButtonBgOver = new Image();
var imgButtonBg = new Image();
 
window.onload = init;

function init()
{
	// Check browser capability

	blnNs = (document.all) ? false : true;
	blnIe = (document.all) ? true : false;
	
	if (!blnNs) { document.body.onselectstart = doSelectStart; }

	document.onclick = doClick;
	document.onmouseup = doMouseUp;
	window.onresize = doResize;
	
	docWidth = getDocumentWidth();
	docHeight = getDocumentHeight();

	objHeaderContainer = document.getElementById("headerContainer");
	objMainContainer = document.getElementById("mainContainer");
	objFooterContainer = document.getElementById("footerContainer");
	objWaitWindow = document.getElementById("waitWindow");
	
	preloadImages();
	if (typeof childInit == "function") { childInit(); }
	
	if (objWaitWindow) { objWaitWindow.style.display = "none"; }
	if (objHeaderContainer) {objHeaderContainer.style.visibility = "";}

	if (objMainContainer) { objMainContainer.style.display = ""; }
	if (objFooterContainer) { objFooterContainer.style.display = ""; }
	
	doResize();
	
	if (objFooterContainer && blnNs)
	{
		window.setTimeout("doResize()",100);
	}
	
	var undef;
	
	if (typeof generatedInitJS == "function")
	{
		generatedInitJS();
	}
	
	if (typeof childPostInit == "function") { childPostInit(); }
	
	
}

function getMainWidth()
{
	return objMainContainer.offsetWidth;
}

function getMainHeight()
{
	var mainHeight = docHeight;
	if(objHeaderContainer) mainHeight -= objHeaderContainer.offsetHeight;
	if(objFooterContainer) mainHeight -= objFooterContainer.offsetHeight;

	return mainHeight;
}

function preloadImages()
{
	btnSelectOff.src = "common/images/btnselectoff.gif";
	btnSelectOn.src = "common/images/btnselecton.gif";
	imgButtonBgOver.src = "common/images/buttonbgover.gif";
	imgButtonBg.src = "common/images/buttonbg.gif";
}

function getDocumentWidth()
{
	return (document.body.clientWidth < APP_MIN_WIDTH) ? APP_MIN_WIDTH : document.body.clientWidth;
}

function getDocumentHeight()
{
	return (document.body.clientHeight < APP_MIN_HEIGHT) ? APP_MIN_HEIGHT : document.body.clientHeight;
}

function doResize()
{
	docWidth = getDocumentWidth();
	docHeight = getDocumentHeight();
	
	if (objHeaderContainer) { objHeaderContainer.style.width = docWidth; }
	if (objMainContainer) { objMainContainer.style.height = getMainHeight(); }
	if (objFooterContainer) 
	{ 
		objFooterContainer.style.top = window.document.body.scrollTop+window.document.body.offsetHeight - objFooterContainer.offsetHeight; 
		if (blnNs)
		{
			objFooterContainer.style.width=docWidth-15;
		}
		else
		{
			objFooterContainer.style.width=docWidth;
		}
		
	}
	if (objMainContainer) { objMainContainer.style.width = docWidth; }

	if (typeof doLocalResize == "function") { doLocalResize(); }
	
	if (objFooterContainer && blnNs)
	{
		window.clearTimeout();
		window.setTimeout("doResize()",150);
	}
	
}

function cancelBubble()
{
	(blnNs) ? e.stopPropagation() : window.event.cancelBubble = true;
}

function barGlobalMouseOver(obj,e)
{
	if (blnMenuVisible) { showBarGlobalMenu(obj,e); }

	if (obj.className != "barGlobalItemOn")
	{
		obj.className = (obj.className == "barGlobalItemOver") ? "barGlobalItemOff" : "barGlobalItemOver";
	}
}

function clearLastListClicked()
{
	evt = getWindowEvent();

	objSelect = document.getElementById(lastListClicked + "List");

	if (objSelect && evt.id.indexOf(lastListClicked) == -1)
	{
		objSelect.style.display = 'none';
	}
}

function doClick(e)
{
	clearMenus();
	clearLastListClicked();	
	if (typeof doChildClick == "function") { doChildClick(); }
}

function doMouseUp(e)
{
	clearButtons();
}

function getWindowEvent(e)
{
	return (blnNs) ? e : window.event.srcElement;
}

function clearButtons()
{
	objLastButtonClicked.className = 'windowButton';
}

function showBarGlobalMenu(barItem,e)
{
	var targMenu = document.getElementById(barItem.id + "Menu");
	var buttonCell = document.getElementById(barItem.id);
	

	(blnNs) ? e.stopPropagation() : window.event.cancelBubble = true;
	clearMenus(); // Clear existing menus

	if (targMenu) // Check for menu
	{
		
		
		barItem.className = "barGlobalItemOn";
		targMenu.style.display = "";
		targMenu.style.left=(buttonCell.offsetLeft+4)+"px";
		blnMenuVisible = true;
		
		hideUnderDiv(targMenu);
	}
}

function doHighlightRowClick(obj)
{
	obj.style.backgroundColor = 'rgb(216,232,215)';

	if (objLastRowClicked)
	{
		objLastRowClicked.style.backgroundColor = '';
		objLastRowClicked.style.color = '';
	}

	objLastRowClicked = obj;
}

function doAbstractButtonClicked(obj,classOn,classOff)
{
	if (objLastButtonClicked) { objLastButtonClicked.style.className = classOff; }
	
	obj.style.className = classOn;
	objLastButtonClicked = obj;
}

function clearMenus()
{	
	// Clear all menus and bar buttons

	var menuNameArray;
	
	if ( typeof custMenuBar != typeof undefinedVari ) {
	   menuNameArray = custMenuBar;
	}
	else
	{
	   menuNameArray = arrBarGlobal;
	}

	for (i = 0; i < arrBarGlobal.length; i++)
	{
		barItem = document.getElementById("bar" + menuNameArray[i]);
		barMenu = document.getElementById("bar" + menuNameArray[i] + "Menu");

		if (barItem)
		{
			if (barItem.className.indexOf("barGlobalItem") != -1)
			{
				barItem.className = "barGlobalItemOff";
			}
		}

		if (barMenu)
		{
			if (barMenu.className == "barGlobalMenuHolder")
			{
				barMenu.style.display = "none";
			}
		}
	}

	blnMenuVisible = false;
	setAllVisible();
}


function openWindow(url,name,width,height,center,resize,scroll,posleft,postop,modal)
{
	if (posleft != 0) { X = posleft; }
	if (postop  != 0) { Y = postop; }

	if (center)
	{
		X = (screen.width - width) / 2;
		Y = (screen.height - height) / 2;
	}

	if (scroll != 0) scroll = 1;
	if (resize != 0) resize = 1;

	var features = 'width='+width+',height='+height+',top='+Y+',left='+X+',resizable='+resize+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no';

	if (modal)
	{
		window.showModalDialog(url,name,features);
	} 
	else 
	{
		win2 = window.open(url,name,features);
		win2.focus();
	}
}

function doBarGlobalOver(srcObj)
{
	trgObj = document.getElementById(srcObj.id + "Menu");
	srcObj.className = "barGlobalItemOver";
	trgObj.style.display = "";
}

function doBarGlobalOut(srcObj)
{
	trgObj = document.getElementById(srcObj.id + "Menu");
	srcObj.className = "barGlobalItemOff";

	if (window.event.toElement.className != "barGlobalMenu")
	{
		trgObj.style.display = "none";
	}
}

function doHighlightRow(srcObj)
{
	srcObj.style.backgroundColor = 'rgb(247,253,247)';
}

function undoHighlightRow(srcObj)
{
	srcObj.style.backgroundColor = '';
	srcObj.style.color = '';

	if (srcObj == objLastRowClicked)
	{
		srcObj.style.backgroundColor = 'rgb(216,232,215)';
	}
}

function doHighlightMenuItem(srcObj)
{
	srcObj.style.backgroundColor = 'rgb(14,80,106)';
}

function undoHighlightMenuItem(srcObj)
{
	srcObj.style.backgroundColor='';
}

function showWindow(obj)
{
	if (obj.style.display == 'none')
	{
		obj.style.display = '';
	}
	else
	{
		obj.style.display = 'none';
	}

	doResize();
}

function closeFlatWindow(name)
{
	var objWin = document.getElementById(name);
	if (objWin) { objWin.style.visibility = "hidden"; }
}

function openFlatWindow(name)
{
	var objWin = document.getElementById(name);

	objWin.style.top = parseInt((docHeight - parseInt(objWin.offsetHeight)) / 2);
	objWin.style.left = parseInt((docWidth - parseInt(objWin.offsetWidth)) / 2);
	
	if (objWin) { objWin.style.visibility = "visible"; }
}

function doSelectStart()
{
	obj = window.event.srcElement;
	
	if (obj.tagName == "INPUT" || obj.tagName == "TEXTAREA")
	{
		window.cancelBubble = true;
	}
	else
	{
		return false;
	}
}

function highlightSelectOption(obj)
{	
	if (obj.style.backgroundColor.toLowerCase() == optionSelBgColor)
	{
		obj.style.backgroundColor = '';
		obj.style.color = '';
	}
	else
	{
		obj.style.backgroundColor = optionSelBgColor;
		obj.style.color = optionSelTxtColor;
	}
}

function toggleDisplay(obj)
{
	obj.style.display = obj.style.display.toLowerCase() == "none" ? "" : "none";
}

function hideUnderDiv(div)
{
	
	hideElement("applet",div);
	hideElement("select",div);
	
}


function setAllVisible()
{
	showElement("applet");
	showElement("select");
}


function showActionWindow(msg)
{
	var objActionWindow = document.getElementById("actionWindow");
	var objActionWindowText = document.getElementById("actionWindowText");
	
	objActionWindow.style.top = parseInt((docHeight - parseInt(objActionWindow.style.height)) / 2);
	objActionWindow.style.left = parseInt((docWidth - parseInt(objActionWindow.style.width)) / 2);
	
	objActionWindowText.innerHTML = msg;
	objActionWindow.style.display = '';
	objActionWindow.style.visibility = 'visible';
}

function hideActionWindow()
{
	var objActionWindow = document.getElementById("actionWindow");	
	objActionWindow.style.display = 'none';
	objActionWindow.style.visibility = 'hidden';
	
}

function buildCustomSelect(name,list,target,selected)
{	
	var targetObj = document.getElementById(target);
	var el = "";
	var selectEl = "";
	var selRowBgColor = "";
	
	el = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
	el+= "<tr onclick=\"toggleSelect('" + name + "');\">";
	el+= "<td width=\"100%\"><div id=\"" + name + "Selection\" class=\"select\">" + selected + "</div></td>";
	el+= "<td align=\"right\"><img class=\"selectButton\" id=\"" + name + "\" src=\"common/images/btnselectoff.gif\" onmousedown=\"this.src=btnSelectOn.src;\" onmouseup=\"this.src=btnSelectOff.src;\"></td>";
	el+= "</tr>";
	el+= "</table>";
	el+= "<div id=\"" + name + "List\" class=\"selectList\" style=\"display: none;\">";
	el+= "<table class=\"selectData\" id=\"" + name + "ListData\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";

	selectEl = "<input type=\"hidden\" id=\"" + name + "HiddenSelect\" name=\"" + name + "HiddenSelect\" class=\"hidden\">";

	for (var i = 0; i < list[0].length; i++)
	{		
		el+= "<tr id=\"" + list[0][i] + "\">";
		el+= "<td onclick=\"selectOption(this.innerHTML,this.parentElement.id,'" + name + "');\" onmouseover=\"highlightSelectOption(this);\" onmouseout=\"highlightSelectOption(this);\" width=\"100%\">" + list[1][i] + "</td>";
		el+= "</tr>";
		
		if (list[1][i] == selected)
		{
			var selName = list[1][i];
			var selValue = list[0][i];
			var selObj = name;
		}
	}

	el+= "</table>";
	el+= "</div>";
		
	targetObj.innerHTML = el + selectEl;
	selectOption(selName,selValue,selObj);
	
}

function showAboutWindow()
{
	openFlatWindow("aboutWindow");
}

function toggleSelect(obj)
{
	var undef;
	
	targetObj = document.getElementById(obj + "List");
	var listPos = targetObj.offsetTop;
	
	if (targetObj)
	{
		if (targetObj.style.display == "")
		{
			targetObj.style.display = "none";
		}
		else
		{
			targetObj.style.zIndex = 1000;
			targetObj.style.display = "";
			listPos = targetObj.offsetTop;
			targetObj.style.display = "none";
			
			var tempSize;
			eval("tempSize = " + obj + "ListSize;");
						
			if (tempSize==undef)
			{
				tempSize = 8;
			}
			
			var listHeight = tempSize * 15 + 1;
			var tempHeight = objFooterContainer.offsetTop - listPos - 10;
			
			if (listHeight > tempHeight && tempHeight > 15)
			{
				targetObj.style.overflow = "auto";
				targetObj.style.borderRight = "0px";
				targetObj.style.height = tempHeight;
			}
			else
			{
				targetObj.style.overflow = "hidden";
				targetObj.style.borderRight = "1px solid #98bbc8";
				targetObj.style.height  = listHeight;
			}
			
			targetObj.style.display = "";
		}
	
		lastListClicked = obj.toString();
	}
}

function selectOption(name,value,obj)
{
	var rowObj = document.getElementById(value);
	var targetObj = document.getElementById(obj + "Selection");
	var targetSelect = document.getElementById(obj + "HiddenSelect");
	
	if (rowObj)
	{
		rowObj.style.backgroundColor = optionSelBgColor;
		rowObj.style.color = optionSelTxtColor;
	}
	
	if (targetSelect) { targetSelect.value = value; }
	if (targetObj)
	{
		targetObj.innerHTML = name;
		
		// Try and call a JavaScript function for this object (if it exists)				
		eval(obj + "_onclick('"+ name + "','" + value + "')");		
	}
	
	if (lastRowClicked)
	{
		lastRowClicked.style.backgroundColor = "";
		lastRowClicked.style.color = "";
	}
	
	lastRowClicked = rowObj;
}

function windowButtonDown(obj)
{
	obj.className = 'windowButtonDown';
	objLastButtonClicked = obj;
}

function windowButtonOver(obj)
{
	obj.className = 'windowButtonOver';
}

function windowButtonOut(obj)
{
	obj.className = 'windowButton';
}




function getFooterHeight()
{
	return 15;
}

function displayError(errorMessage)
{
	alert(errorMessage);
}


function setXYPos(s,x,y)
{
	var d;
	d=document.getElementById(s);
	
	d.style.left=x;
	
	d.style.top=y;  
	
}
