var windowManager = new WindowManager();	// Create the Window Manager Object for this window

var g_MainWindowName = "MainWindow";
var g_LoginWindowName = "AprimoLogin70";
var g_WinMgmtCookieName = "AprimoWindowMgmt";
var g_AprimoCookieName = "Aprimo";
var g_LoginCookieName = "Aprimo7Login";
var g_SessionCookieName = GetCookie(g_LoginCookieName, "SessionCookieName");
var g_WinPositionCookieName = "AprimoWindowPosition";
var g_WinNamesCrumbName = "WindowNames";
var g_WinSizeCrumbName = "WindowSize";
var g_WinPositionCrumbName = "WindowPosition";
var g_PopWinSizeCrumbName = "PopWindowSize";

var g_WinMenuItemsCrumbName = "WinMenuItems";
var g_WinMenuItemName = "pb_WindowMenuItem";

// ComponentArt constants, since they might change and jack us
var caIDArrayIndex = 0;
var caParentIndexArrayIndex = 1;
var caChildrenArrayIndex = 2;
var caDisplayTextArrayIndex = 12;
var caJSFunctionArrayIndex = 13;

var g_ChildWindow = null;

function Logoff() {
	window.open(RelativePath + "LogOff.aspx?Logoff=1", '');
	window.top.close();	
}	

function LogoffTaskPortal() {
	window.open(RelativePath + "TaskDefault.aspx?Logoff=1", '');
	window.top.close();
	//window.top.document.location = RelativePath + "Default.aspx?Logoff=1";	
}


function SetLocalWindowName(WinName)
{
	if (window.top.WindowBar)
	{
		for (var i=0; i<window.top.WindowBar.Windows.length; i++)
		{
		
			if(typeof GetContentWindow(window.top.WindowBar.Windows[i]).id != "undefined")
			{
				if (GetContentWindow(window.top.WindowBar.Windows[i]).id == window.name)
				{  
					window.top.WindowBar.Windows[i].Name = WinName;
				}
			}
			else
			{
				if(GetContentWindow(window.top.WindowBar.Windows[i]) == window)
				{
					window.top.WindowBar.Windows[i].Name = WinName;
				}			
			}
		}
	}
}


function GetParentWin()
{
	var parentWin = null;
	var thisWin = window.top.WindowBar.GetWindow(LocalWindowName);
	if (thisWin && thisWin.ParentWindowName != "") {
		parentWin = window.top.WindowBar.GetWindow(thisWin.ParentWindowName);
		if (parentWin)
			parentWin = GetContentWindow(parentWin);
	}
	if(blnLoggedIntoPortal)
	{
		if(parentWin == null && window.top)
			parentWin = window.top.opener		
	}	
	return parentWin;
}

function GetParentWinObject()
{
	var parentWin = null;
	var thisWin = window.top.WindowBar.GetWindow(LocalWindowName);
	if (thisWin && thisWin.ParentWindowName != "") {
		parentWin = window.top.WindowBar.GetWindow(thisWin.ParentWindowName);
	}
	return parentWin;
}

function CloseWin()
{
	
	window.top.WindowBar.GetWindow(LocalWindowName).CloseWindow(true);
	if(blnLoggedIntoPortal)
	{
	  if(window.top.WindowBar.WindowCount() == 0)
	  {
	    window.top.close();
	  }  
	}
}

function CloseWinNoPrompt()
{
	window.top.WindowBar.GetWindow(LocalWindowName).CloseWindow(false);
}

function RefreshParentWindowHref(targetURL)
{	
	if(IsMac())
	{
		if(targetURL.toLowerCase().indexOf('http') != 0 && targetURL.toLowerCase().indexOf('javascript:') != 0)
		{	
			//strip the filename from pathname
			var strPath = window.location.pathname;			
			strPath = strPath.substring(0, strPath.lastIndexOf('/') + 1);
			targetURL = window.location.protocol + '//' + window.location.hostname + strPath + targetURL;	
		}
	}
	
	var parentWin = GetParentWin();
	if (parentWin) 
	{		
		parentWin.location.href = targetURL;
	}	
	CloseWin();
}

function RefreshParentWin()
{
	var parentWin = null;
	var thisWin = window.top.WindowBar.GetWindow(LocalWindowName);
	if (thisWin && thisWin.ParentWindowName != "") {
		parentWin = window.top.WindowBar.GetWindow(thisWin.ParentWindowName);
		if (parentWin) {
			parentWin.StartAnimation();
			if(IsMac())
				GetContentDocument(parentWin).location.reload();
			else
			{
				parentWin.GetContentDocument(parentWin.ContentFrame).body.style.display = 'none';
				parentWin.GetContentDocument(parentWin.ContentFrame).URL = parentWin.GetContentDocument(parentWin.ContentFrame).URL;
			}
		}	
	}	
}

function RefreshMainWindow()
{
	var mainWin = window.top.WindowBar.GetWindow(g_MainWindowName);
	if (mainWin) {
		mainWin.StartAnimation();
		if(IsMac())
			GetContentDocument(mainWin).location.reload();
		else
		{
			mainWin.GetContentDocument(mainWin.ContentFrame).body.style.display = 'none';
			mainWin.GetContentDocument(mainWin.ContentFrame).URL = mainWin.GetContentDocument(mainWin.ContentFrame).URL;
		}
	}
}	


function WindowManager()
{	
	this.objName = "WindowManager";
	this.windowArray = new Array();
	this.numWindows = 0;
	this.winMenuItemKeyArray = new Array();
	this.winMenuItemValueArray = new Array();
	this.numWinMenuItems = 0;
	this.arg = new Array(2); 
	
	this.RaiseAPEvent = function(o, w)
	{
		//this.FindMainWindow().windowManager.UpdateAllChildren(o); 
		window.top.WindowBar.UpdateAllWindows(o);
	}
		
	this.OnInit = function()
	{
		// Read the window names from the cookie
		var windowNames = GetCookie(g_WinMgmtCookieName, g_WinNamesCrumbName);
		
		if (windowNames && windowNames != "") {
			// Split the Window Names and get a window reference to each name
			var vntWindowNames = windowNames.split("~~@~~");
			var win = null;
			
			for (var i = 0; i < vntWindowNames.length; i++) {
				
				//if( g_MainWindowName != window.name && "Page2_1" != vntWindowNames[i] ) {
				
					// Get a reference to the already open window
					win = window.open('', vntWindowNames[i]);
					//alert(window.name + ".OnInit().Open() : " + win.name + " : cookie name =" + vntWindowNames[i]); 
					if (win)
						this.windowArray[this.numWindows++] = win;
				//}
			}
		}
		
		// Read the names of the Window Menu Items from a cookie
		if (window.name == g_MainWindowName) {
			var winMenuItems = GetCookie(g_WinMgmtCookieName, g_WinMenuItemsCrumbName);
			// Split the Window Names and add them as menu items
			if (winMenuItems != "") {
				var vntMenuItems = winMenuItems.split("~~@~~");
				var vntSplit = null;
				for (var i = 0; i < vntMenuItems.length; i++) {
					vntSplit = vntMenuItems[i].split(";");
					this.RegisterWindowMenuItemMain(vntSplit[0], vntSplit[1]);
				}
			}
		}
		this.RegisterWindowMenuItem(window.name, document.title);		
	}
	
	this.FindWindowsMenuItem = function()
	{
		var winMenuItem = null;
		
		// Try to find the Window Menu Item
		if (window.AprimoMenu) {
			var items = AprimoMenu.Items();
			if (items != null) {
				for (var i = items.length-1; i >= 0; i--) {			
					if (AprimoMenu.qzah[items[i].qzi][caIDArrayIndex] == g_WinMenuItemName) {
						winMenuItem = AprimoMenu.qzah[items[i].qzi];						
						break;
					}	
				}
			}	
		}
		return winMenuItem;
	}
	
	this.OnUnLoad = function()
	{
		// Write the window names to a cookie
		var windowNames = "";
		for (var i = 0; i < this.numWindows; i++) {
			if (this.windowArray[i] && !this.windowArray[i].closed )	 {
				// TODO:  Use XML here?
				if (windowNames != "")
					windowNames += "~~@~~";
					
				windowNames += this.windowArray[i].name;				
			}	
		}
		
		// Write the Window Names to a Cookie
		SetCookie(g_WinMgmtCookieName, g_WinNamesCrumbName, windowNames);
		
		// Write off the names of the Window Menu Items into a cookie
		if (window.name == g_MainWindowName) {
			var winMenuItems = "";
			for (var i = 0; i < this.numWinMenuItems; i++) {
				if (this.winMenuItemKeyArray[i] != "" && this.winMenuItemValueArray[i] != "")	 {
					// TODO:  Use XML here?
					if (winMenuItems != "")
						winMenuItems += "~~@~~";
						
					winMenuItems += this.winMenuItemKeyArray[i] + ";" + this.winMenuItemValueArray[i];				
				}	
			}
			// Write the Values to a Cookie
			SetCookie(g_WinMgmtCookieName, g_WinMenuItemsCrumbName, winMenuItems);
		}
		
		// Close Child Windows, if we're closing this window		
		this.CloseWindows();
		
		// Remove this window from the parent's windows arrays and menu items and set focus
		// to the parent window, if we are closing the window
		//if (window.opener && !window.opener.closed && window.opener.windowManager) {
		//	window.opener.CheckChildClosed(window);
		//}	
	}
	
	
	this.OpenWindow = function(url, title,  bModal, windowfeatures)
	{
		// reposition window
		var newTop = window.screenTop - 55;
		var newLeft = window.screenLeft + 12;

		// resize pop window to it's last known size, if it exists
		var winSize = GetCookie(g_WinPositionCookieName, g_PopWinSizeCrumbName);
		if (winSize != "") {
			var vntSize = winSize.split(";");
			var newHeight = vntSize[0];
			var newWidth = vntSize[1];
		}
		else {
			var newHeight = document.body.clientHeight;
			var newWidth = document.body.clientWidth;
		}

		if(!IsMac() && bModal)
		{
			window.showModalDialog(url, title, "dialogheight:" + newHeight + "px;dialogWidth:" + newWidth + "px");		
			return;
		}	
		// See if this window already exists
		var newWin = this.FindExistingWindow(title);
		if (newWin == null) 
		{
			// Doesn't exist yet, create it
			if (!windowfeatures || windowfeatures=="") 
			{
				windowfeatures = "height=" + newHeight + ",width=" + newWidth;
			}
			var addressBarOption = GetAddressBarOpenWinOption();
			windowfeatures += ",menubar=no,hotkeys=no,directories=no," + addressBarOption + ",personalbar=no,titlebar=no,toolbar=no,resizable=yes,scrollbars=no,status=yes"
				
			if(bModal)
			{																	
				newWin = window.open(url, title, windowfeatures);	
				window.Modal = newWin; //window can only have one Modal open								
				window.onmouseover = function()
				{	
					if(window.Modal != null)
						window.Modal.focus();
				}								
			}
			else
				newWin = window.open(url, title, windowfeatures);	
			
			newWin.moveTo(newLeft,newTop);
			this.windowArray[this.numWindows++] = newWin;
		} 
		else 
		{
			// See if the URL has changed
			if (newWin.document.location.href.indexOf(url) < 0)
				newWin.document.location = url;
		}	
		// Give focus to the window
		newWin.focus();		
	}

	this.FindExistingWindow = function(windowName)
	{
		var theWin = null;
		// Loop through the array and find a window with the given name
		for (var i = 0; i < this.numWindows; i++) {
			if (!this.windowArray[i].closed && this.windowArray[i].name == windowName) {
				theWin = this.windowArray[i];
				break;
			}
		}		
		return theWin;
	}
	
	this.CloseWindows = function()
	{
		for (var i=0; i < this.windowArray.length; i++) {
			if(this.windowArray[i] && !this.windowArray[i].closed) {
				// Call a method to tell the child to close itself, if needed
				this.windowArray[i].CheckParentClosed();
			}	
		}
	}
	
	this.FindMainWindow = function()
	{
		// Travel up the hierarchy
		var theWin = window;
		while (theWin.name != g_MainWindowName)
		{
			try
			{
				if (theWin.opener == null || theWin.opener == theWin || theWin.opener.name == g_LoginWindowName)
					break;
				else
					theWin = theWin.opener;				
			} catch (e)
			{
				// Use this window
				break;
			}		
		}
		return theWin;
	}
	
	this.BringMainToFocus = function() 
	{
		// Travel up the hieararchy
		var theWin = this.FindMainWindow();
		
		// Find the Main window
		if (theWin && !theWin.closed)
			theWin.focus();
	}
	
	this.CheckForClosedChild = function(childWindow, childWindowName)
	{
		// Loop through the child windows, and if the names match, 
		// check to see if that window is closed.  If so, bring this window to focus
		// and remove that window from the window array and menu items array
		for (var i=0; i < this.windowArray.length; i++) {
			if(this.windowArray[i] && this.windowArray[i] == childWindow) {
				// See if this window is closed.
				if (this.windowArray[i].closed) {
					// Give Focus to this window
					window.focus();
					// Remove from the windows array
					this.windowArray[i] == null;
					// Remove from the Window Menu Item Arrays
					for (var j = 0; j < this.winMenuItemKeyArray.length; j++) {
						if (this.winMenuItemKeyArray[j] == childWindowName) {
							// Clear out the values
							this.winMenuItemKeyArray[j] = "";
							this.winMenuItemValueArray[j] = "";
						}
					}
					
					// Remove from the actual menu from the main window down
					// Need to push this change to all window managers, start with the top
					var theWin = this.FindMainWindow();
					if (theWin && theWin.windowManager)
						theWin.windowManager.RemoveWindowMenuItem(childWindowName);
				}
				break;
			}	
		}		
	}
	
	this.RegisterWindowMenuItemMain = function(windowName, title)
	{
		// Change the value of the first item in the Windows Menu
		// to the page title
		
		var innerTable = null;
		var innerTRs = null;
		var tc = null;
		
		// Get the Window Toolbar Item
		var winMenuItem = this.FindWindowsMenuItem();
		
		if (winMenuItem) {
			if (windowName == g_MainWindowName) {
				// Modify the first child entry
				if (winMenuItem[caChildrenArrayIndex].length > 0)
				{
					// Change the name of the first item
					AprimoMenu.qzah[winMenuItem[caChildrenArrayIndex][0]][caDisplayTextArrayIndex] = title;
					AprimoMenu.qzah[winMenuItem[caChildrenArrayIndex][0]].windowName = windowName;
					this.winMenuItemKeyArray[0] = windowName;
						this.winMenuItemValueArray[0] = title;
						if (this.numWinMenuItems == 0)
							this.numWinMenuItems = 1;
				}
			} else {
				var blnFound = false;
				// Loop through the children and see if the window name property matches
				var theArray = winMenuItem[caChildrenArrayIndex];				
				if (theArray) {
					for (var i = 0; i < theArray.length; i++) {
						if (AprimoMenu.qzah[theArray[i]].windowName == windowName) {
							// Change the title of this item
							AprimoMenu.qzah[theArray[i]][caDisplayTextArrayIndex] = title;
							blnFound = true;
							// TODO:  Make sure this index is right
							this.winMenuItemValueArray[i] = title;
							break;
						}
					}
				}
				
				if (!blnFound) {
					// Add this item to the end
					var index = AprimoMenu.qzah.length;
					
					winMenuItem[caChildrenArrayIndex][winMenuItem[caChildrenArrayIndex].length] = index;
					// Set some defaults, then we'll override them
					
					AprimoMenu.qzah[index] = ['pb_999',-1,[],1,0,null,0,0,0,2,'MenuGroup',2,'REPLACE_ME','REPLACE_ME','MenuItem','MenuItemOver',null,null,2,10,10,2,'MenuItemOver',null,null,null,16];					
					AprimoMenu.qzah[index][caIDArrayIndex] = "pb_" + (index+1);
					
					//AprimoMenu.qzah[index][caParentIndexArrayIndex] = winMenuItem[caIDArrayIndex];
					AprimoMenu.qzah[index][caParentIndexArrayIndex] = this.FindIndexOfMenuItem(AprimoMenu, winMenuItem[caIDArrayIndex]);
					
					AprimoMenu.qzah[index][caDisplayTextArrayIndex] = title;
					AprimoMenu.qzah[index][caJSFunctionArrayIndex] = 'FindWindow("' + windowName + '")';
					AprimoMenu.qzah[index].windowName = windowName;  // Set the window name too
					this.winMenuItemKeyArray[this.numWinMenuItems] = windowName;
					this.winMenuItemValueArray[this.numWinMenuItems++] = title;
				}	
			}			
		}
		
		// Need to push this change to all child window managers
		for (var i=0; i < this.windowArray.length; i++) {
			if(this.windowArray[i] && !this.windowArray[i].closed && window.name != this.windowArray[i].name) {
				this.windowArray[i].windowManager.RegisterWindowMenuItemChild(AprimoMenu, winMenuItem);
			}	
		}
	}
		
	this.RegisterWindowMenuItem = function(windowName, title)
	{
		// Need to push this change to all window managers, start with the top
		var theWin = this.FindMainWindow();
		
		// Tell the Main Win to update
		theWin.windowManager.RegisterWindowMenuItemMain(windowName, title);
	}
	
	this.RegisterWindowMenuItemChild = function(parentMenu, menuItemToCopy)
	{
		var winMenuItem = this.FindWindowsMenuItem();
			
		if (winMenuItem) {			
			// Remove the old items
			winMenuItem[caChildrenArrayIndex] = new Array();
			var index = 0;			
			var itemToCopy = null;
			var theArray = menuItemToCopy[caChildrenArrayIndex];
			for (var i = 0; i < theArray.length; i++) {
				index = AprimoMenu.qzah.length;
				itemToCopy = parentMenu.qzah[theArray[i]];
				AprimoMenu.qzah[index] = new Array(itemToCopy.length);
				for (var j = 0; j < itemToCopy.length; j++) {
					AprimoMenu.qzah[index][j] = itemToCopy[j];
				}
				// Copy the Window Name
				AprimoMenu.qzah[index].windowName = itemToCopy.windowName;  // Set the window name too
				
				AprimoMenu.qzah[index][caIDArrayIndex] = "pb_" + (index+1);
				AprimoMenu.qzah[index][caParentIndexArrayIndex] = this.FindIndexOfMenuItem(AprimoMenu, winMenuItem[caIDArrayIndex]);
				winMenuItem[caChildrenArrayIndex][winMenuItem[caChildrenArrayIndex].length] = index;
			}
		}
		
		// Need to push this change to all child window managers
		for (var i=0; i < this.windowArray.length; i++) {
			if(this.windowArray[i] && !this.windowArray[i].closed && window.name != this.windowArray[i].name) {
				this.windowArray[i].windowManager.RegisterWindowMenuItemChild(parentMenu, menuItemToCopy);
			}	
		}
	}
	
	this.FindIndexOfMenuItem = function(CAMenu, menuItemIDToFind)
	{
		var index = -1;		
		if (CAMenu) {
			for (var i = CAMenu.qzah.length-1; i >= 0; i--) {
				if (CAMenu.qzah[i][caIDArrayIndex] == menuItemIDToFind) {
					index = i;
					break;
				}
			}
		}
		return index;
	}
	
	this.RemoveWindowMenuItem = function(windowName)
	{
		var innerTable = null;
		var innerTRs = null;
		var tc = null;
		
		var winMenuItem = this.FindWindowsMenuItem();
		if (winMenuItem) {
			// Loop through the children and see if the window name property matches
			var theArray = winMenuItem[caChildrenArrayIndex];				
			if (theArray) {
				for (var i = 0; i < theArray.length; i++) {
					if (AprimoMenu.qzah[theArray[i]].windowName == windowName) {						
						// Remove this item
						
						// Move each element up one
						for (var k = i; k < theArray.length-1; k++) {
							theArray[k] = theArray[k+1];
						}
						// Remove the last item
						theArray.length = theArray.length-1;
						
						// Remove from the Window Menu Item Arrays
						for (var j = 0; j < this.winMenuItemKeyArray.length; j++) {
							if (this.winMenuItemKeyArray[j] == windowName) {
								// Clear out the values
								this.winMenuItemKeyArray[j] = "";
								this.winMenuItemValueArray[j] = "";
							}
						}
						break;
					}
				}
			}
		}
		
		// Need to push this change to all child window managers
		for (var i=0; i < this.windowArray.length; i++) {
			if(this.windowArray[i] && !this.windowArray[i].closed) {
				this.windowArray[i].windowManager.RemoveWindowMenuItem(windowName);
			}	
		}
	}
}

function WriteWinCoord()
{
	if (window.name == g_MainWindowName) {
		// Get main window coordinates and size and write them to a cookie
		var winPosition = window.screenTop + ";" + window.screenLeft;
		SetCookie(g_WinPositionCookieName, g_WinPositionCrumbName, winPosition, "D:30");

		var winSize = document.body.clientHeight + ";" + document.body.clientWidth;
		SetCookie(g_WinPositionCookieName, g_WinSizeCrumbName, winSize, "D:30");
	}
	else
	{
		if(window.name != g_LoginWindowName)
		{
			// Get pop window size and write it to a cookie
			var winSize = document.body.clientHeight + ";" + document.body.clientWidth;
			SetCookie(g_WinPositionCookieName, g_PopWinSizeCrumbName, winSize, "D:30");
		}
	}
}


function CheckContextMenu() {
	if (event.ctrlKey && event.altKey)
		return true;
		
	return false;	
}


var contextMenuTableRow = null;

function HideContextMenu() {
	// See if the context menu is present
	// TODO:  Get the correct ID
	var contextMenu = document.getElementById("ListContextMenug2");
	if (contextMenu && contextMenu.style.visibility == "visible")
		contextMenu.style.visibility = "hidden";
		
	// Clear out the old selected row	
	ResetContextMenuTRClass();	
	contextMenuTableRow = null;
}

function SetContextMenuTableRow(tableRow) {	
	if (tableRow) {
		var selectedText = "Selected";
		// Clear out the old row	
		ResetContextMenuTRClass();
		
		// Now change the selected row
		contextMenuTableRow = tableRow;
		contextMenuTableRow.className = selectedText + contextMenuTableRow.className;
		
	}
}

function ResetContextMenuTRClass() {
	var selectedText = "Selected";
	// Clear out the old row	
	if (contextMenuTableRow && contextMenuTableRow.className.indexOf(selectedText) == 0) {
		contextMenuTableRow.className = contextMenuTableRow.className.substring(selectedText.length);
	}
}

function ListRowContextMenu(tableRow) {	
	
	// See if the context menu is present
	var contextMenu = document.getElementById("ListContextMenug2");
	if (contextMenu) {
		// Get the click coordinates
		var clickX = event.clientX;
		var clickY = event.clientY;
		
		// Get other coords needed to position menu
		var menuHeight = contextMenu.offsetHeight;
		var menuWidth = contextMenu.offsetWidth;    
		var scrollTop = document.body.scrollTop;
		var scrollLeft = document.body.scrollLeft;
		
		// Move the Context Menu to the right location
		
		// Set the Left Edge of the Context Menu
		if (clickX + menuWidth > document.body.clientWidth + scrollLeft) 						
			contextMenu.style.left = clickX - menuWidth;
		else 
			contextMenu.style.left = clickX;		
		
		// Set the Top Edge of the Context Menu
		if (clickY + menuHeight > document.body.clientHeight + scrollTop) 
			contextMenu.style.top = clickY - menuHeight + 10;							
		else
			contextMenu.style.top = clickY - 10;
		
		// Show the Context Menu Table		
		contextMenu.style.visibility = "visible";
		
		// Change the Style Sheet of the Selected Row
		SetContextMenuTableRow(tableRow);
		
		// Don't bubble up to the document's context menu event handler	
		event.cancelBubble = true;
	}
	
	return false;
}

function MaintainTableWidths() {
	// Get the two tables, set their widths to be equivalent
	var iconTable = document.getElementById("IconTable");
	var viewTable = document.getElementById("ViewTable");
	var tabsTable = document.getElementById("TabsTable");
	var objContainer = document.getElementById('TableContainer');
	var width = 0;
	
	if (iconTable && viewTable && iconTable.offsetWidth != viewTable.offsetWidth) {
		if (objContainer && objContainer.offsetWidth < viewTable.offsetWidth)
			width = objContainer.offsetWidth;
		else	
			width = viewTable.offsetWidth;
		
		iconTable.style.width = width;
		
		if (tabsTable) {
			tabsTable.style.width = width;
		}	
	}			
}

function OpenFilterWin(URL) {
	var newWin = window.open(URL, "FilterWin", "menubar=no,hotkeys=no,directories=no,location=no,personalbar=no,titlebar=no,toolbar=no,resizable=yes,scrollbars=no,status=yes,height=" + (screen.availHeight-200) + ",width=" + (screen.availWidth-200));
	newWin.moveTo(20, 20);
}

function OpenNewWin(URL) {
	var newWin = window.open(URL, "NewWin", "menubar=no,hotkeys=no,directories=no,location=no,personalbar=no,titlebar=no,toolbar=no,resizable=yes,scrollbars=no,status=yes,height=" + (screen.availHeight-200) + ",width=" + (screen.availWidth-200));
	newWin.moveTo(20, 20);				
}

function MouseOver(Element, mouseX, mouseY) {
	var x1 = Position(Element, "LEFT");
	var x2 = Position(Element, "RIGHT");
	var y1 = Position(Element, "TOP");
	var y2 = Position(Element, "BOTTOM");
	
	if (mouseX >= x1 && mouseX <= x2 && mouseY >= y1 && mouseY <= y2)
		return true;
	else					
		return false;
}

function GoToMainWindow() {
	// get the window manager, and tell it to find the main window
	if (windowManager)
		windowManager.BringMainToFocus();
}

function Navigate(direction) {
	window.history.go(direction);
}

function RefreshPage() {
	window.reloading = true;
	if(IsMac())
		window.location.reload();
	else
	{	
		window.document.body.style.display = 'none';		
		window.document.URL = window.document.URL;	
	}
}

function GoToHomePage() {
	if (window.top.WindowBar)
	{
		for (var i=0; i<window.top.WindowBar.Windows.length; i++)
		{
		    if (window.top.WindowBar.Windows[i].Name == g_MainWindowName)
		    {		      		
			    GetContentWindow(window.top.WindowBar.Windows[i]).document.location = "Aprimo.aspx?PageID=1&NavItem=Nav_97";
			    window.top.WindowBar.Windows[i].Activate();
			    break;
			}						
		}
	}
}

//----------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	GoToURL
//  Description:	    Redirects the browser to a given URL
//  Arguments:			URL - the URL to direct the browser to
//						bPopWindow - open URL in a new popup window
//						New window mode (full browser window or not)
//  Return Value:	    (none)
//  Comments:			If the URL parameter is empty, no action will be taken.
//----------------------------------------------------------------
function GoToURL(targetURL, windowName, bPopWindow, bModal, bShowNavAlert, bAllowSubmit, bForceSubmit, bSaveButton, bCenterWindow, iWidth, iHeight)
{  	
	SetCookie(g_AprimoCookieName, "FromURL", document.location, "M:30");
		
	if (bCenterWindow == undefined) bCenterWindow = false;
	if (iWidth == undefined) iWidth = 0;
	if (iHeight == undefined) iHeight = 0;
	
	//Redirect the browser to the given URL, if it exists
	if (!targetURL || targetURL == "")
		return;			
	if(typeof(window.top.WindowBar)=="undefined")
	{
		if(bPopWindow)
			OpenExternalLink(targetURL, windowName);
		else if (bSaveButton || bForceSubmit) 
		{
			ObjectManager.Destination = targetURL;
			ObjectManager.EvaluateUserInput('yes');
		} 
		else
			document.location = targetURL;
		return;
	}
	
	if (bShowNavAlert == undefined)
		bShowNavAlert = true;
		
	if(bAllowSubmit == undefined)
		bAllowSubmit = true; 

	//Mac document.location does not prepend the rootpath correctly
	if(IsMac())
	{
		if(targetURL.toLowerCase().indexOf('http') != 0 && targetURL.toLowerCase().indexOf('javascript:') != 0)
		{	
			//strip the filename from pathname
			var strPath = window.location.pathname;
			
			strPath = strPath.substring(0, strPath.lastIndexOf('/') + 1);
			targetURL = window.location.protocol + '//' + window.location.hostname + strPath + targetURL;		
		}
	}
	
	if(bPopWindow)
	{
		// TODO:  bModal, probably pass it into the window manager		
		if (!windowName || windowName == "")
			windowName = "NewWin";					
			
		//windowManager.OpenWindow(targetURL, windowName, bModal);
		var w = window.top.WindowBar.GetWindow(windowName);
		if (w == null)
		{
			w = window.top.WindowBar.CreateWindow(targetURL, windowName);
							
			var wpix, hpix;
			
			if (iWidth == 0) wpix = w.Width; else wpix = iWidth;
			if (iHeight == 0) hpix = w.Height; else hpix = iHeight;			
			
			if (iWidth > 0 || iHeight > 0)
			{				
				w.SizeWindowNoDefault(wpix, hpix);
				w.RememberSize = false;
			}
			else
				w.SetInitialSize();			
								
			if (bCenterWindow)
				w.InitialPositionDelegate = w.CenterWindow;
			else
				w.InitialPositionDelegate = w.SetInitialPosition;				
				
			if(!blnLoggedIntoPortal)
				w.SetModal(false);
			else
				w.SetModal(true);



			//w.HideWindow();
		} 
		else 
		{
			w.ParentWindowName = window.top.WindowBar.GetActiveWindowName();
			w.Activate(true);
		}
		if(blnLoggedIntoPortal)
		{
		    w.HideMinimizeButton();
		}
		else
		{
		    w.ShowMinimizeButton()
		}
		w.ShowMaximizeButton();			
	}
	else
	{	    
		ObjectManager.VerifyNavigation(targetURL,bShowNavAlert,bAllowSubmit,bForceSubmit, bSaveButton);
	}
	return;
}


function CheckParentClosed() {
	// Asynchronous, to give the parent time to close, if indeed it is closing
	setTimeout("CheckParentClosedTimeout()", 1);
}

function CheckParentClosedTimeout() {
	if (opener && opener.closed) {
		window.close();
	}	
}


function CheckChildClosed(childWindow) {
	g_ChildWindow = childWindow;
	// Asynchronous, to give the parent time to close, if indeed it is closing
	setTimeout("CheckChildClosedTimeout('" + childWindow.name + "')", 1);
}

function CheckChildClosedTimeout(childWindowName) {
	var childWin = g_ChildWindow;
	// Have to have the window and the name, because once the window is closed
	// you can't access the name
	windowManager.CheckForClosedChild(childWin, childWindowName);
	g_ChildWindow = null;
}

function FindWindow(windowName) {
	var win = window.open('', windowName);
	win.focus();
	return win;
}

function GetAddressBarOpenWinOption() {	
	var addressBarOption = "location=";
	var cookieVal = GetCookie(g_LoginCookieName, "ShowAddressBar");
	
	if (cookieVal == "on")
		addressBarOption += "yes";
	else	
		addressBarOption += "no";
	
	return addressBarOption;
}

function OpenExternalLink(URL, windowName) 
{
    SetCookie(g_AprimoCookieName, "FromURL", document.location, "M:30");
    // Defect: 183410
    if(IsMac())
    {
        var temp;
        temp = URL.replace("%be","_");
        var win = window.open(temp, windowName);
    }
    else
    {
	var win = window.open(URL, windowName);
    }
}

function IsObjectOnPage(objectName) {
	if (eval("window." + objectName) != null)
		return true;
	else if (window.top.WindowBar && eval("GetContentWindow(window.top.WindowBar.GetWindow(LocalWindowName))." + objectName) != null)
		return true;
	return false;	
}	

function GetContentWindow(winObject) {
	
	if(winObject.ContentFrame.contentWindow)
		return winObject.ContentFrame.contentWindow;
	else
		return winObject.ContentFrame;
}

function GetContentDocument(winObject) {
	if (IsMac())
		return winObject.ContentFrame.contentDocument;
	else
		return winObject.ContentFrame.contentWindow.document;
}
