
// if you change SMALL_SIZE OR MINIMUM_HELP_SIZE you will need to update 
//UserInfo.IN_PAGE_HELP_MIN_WIN_WIDTH to (SMALL_SIZE + MINIMUM_HELP_SIZE)
var SMALL_SIZE = 800;
var LARGE_SIZE = 1400;
var JUMP_WIDTH = 200;
var MINIMUM_HELP_SIZE = 200;
var OPTIMUM_HELP_SIZE = 400;
var USER_WIN_WIDTH = -1;
var showHelp = false;

// getWindoWidth gets the size of the window including the width of the scrollbars...we want the width of the content
// excluding the scrollbars...clientWidth seems to give us that consistantly in both IE and FireFox
//	KMS: I put this call in a function to hopefully keep the resize and
//	updateUserWinWidth functions in sync, because they need to use the same
//	calculation to determing window width.
function getClientWindowWidth()
{
	return document.body.clientWidth;
}

function resize()
{
	var contentObj = document.getElementById('main');
	var footerObj = document.getElementById('footer');
	var helpObj = document.getElementById("help");
	

	//alert("helpObj:" + helpObj + "\n showHelp: " + showHelp + "\n scrollWidth: " + document.body.scrollWidth+"\n clientWidth: " + document.body.clientWidth+"\n offsetWidth: " + document.body.offsetWidth+"\n getWindowWidth(): " + getWindowWidth());
	if (showHelp && helpObj != null)
	{
		var windowWidth = getClientWindowWidth();//getWindowWidth();
		//alert("helpObj:" + helpObj + "\n showHelp: " + showHelp + "\n scrollWidth: " + document.body.scrollWidth+"\n clientWidth: " + document.body.clientWidth+"\n offsetWidth: " + document.body.offsetWidth+"\n getWindowWidth(): " + getWindowWidth());
		if (windowWidth > 0)
		{
			if (helpObj != null && windowWidth < (SMALL_SIZE+MINIMUM_HELP_SIZE))
			{
				/*helpObj.className = "helpstylesmall";
				if (windowWidth < SMALL_SIZE) // Help stops moving inward
				{
					helpObj.style.left = (SMALL_SIZE - OPTIMUM_HELP_SIZE) + "px";
				}
				else // Help begins to overlap the content
				{
					helpObj.style.left = (windowWidth - OPTIMUM_HELP_SIZE) + "px";
				}
				contentObj.className = "main";
				footerObj.className = "footer";
				helpObj.style.width = OPTIMUM_HELP_SIZE + "px";*/
				//helpObj.style.width = MINIMUM_HELP_SIZE;
								
				if (showHelp)
				{				
					toggleHelpServer();
					showHelp = false;
					changeToHide('help');							
				}
			}
			else
			{
				if (helpObj != null && windowWidth < LARGE_SIZE) // Help changes to medium mode
				{
					contentObj.className = "main";
					helpObj.className = "helpstylemedium";
					footerObj.className = "footer";
	
				}
				else  // Help changes to large mode
				{
					contentObj.className = "mainlarge"
					helpObj.className = "helpstylelarge";
					footerObj.className = "footerlarge";
				}
				
				var sSheet = getStyleSheetFromDoc(document, "xv2styles.css");
				var cssRule = getCSSRuleFromStyleSheet(sSheet, "div." + contentObj.className, true);
				var styleWidth = cssRule.style.width.replace(/px/gi, "");
				//alert("styleWidth=["+styleWidth+"]");
				var contentWidth = contentObj.offsetWidth;
				
				if (contentWidth > styleWidth)
				{
					contentObj.style.width = contentObj.offsetWidth + "px";
				}
				
				helpObj.style.left = contentWidth + "px";
				var newHelpWidth = ((windowWidth - contentWidth) < MINIMUM_HELP_SIZE)
									?	MINIMUM_HELP_SIZE
									:	(windowWidth - contentWidth);
				helpObj.style.width = newHelpWidth + "px";
				//alert("helpclass: " + helpObj.className + " windowWidth: " + windowWidth + " contentWidth: " + contentWidth + " windowoffset: " + window.document.body.offsetWidth);
			}
			
			// Hack to get firefox working when resizing window up and scrollbar not showing
			//if (!stopRecall)
			//{
				//alert("im here");
			//	changeToHide('help');
			//	setTimeout("resize(true)", 1);
			//	setTimeout("changeToHide('help')", 1);
			//}
			//setTimeout("changeToShow('help')", 5);
			
		}	

	}
	else
	{
		contentObj.style.width = "";
		//alert("helpObj:" + helpObj + "\n showHelp: " + showHelp + "\n scrollWidth: " + document.body.scrollWidth+"\n clientWidth: " + document.body.clientWidth+"\n offsetWidth: " + document.body.offsetWidth+"\n getWindowWidth(): " + getWindowWidth());
		contentObj.className = "mainnohelp";
		footerObj.className = "footernohelp";
	}

	setFooter();

	
}

function setFooter()
{
	//alert("hi footer");
    if(document.getElementById('main') != null &&
	document.getElementById('footer') != null)
	{
		var windowHeight = getWindowHeight();
        if(windowHeight>0)
        {
			var helpObj = document.getElementById('help');
			var helpHeight = 0;
			if (helpObj != null)
			{
            	helpHeight = helpObj.offsetHeight;
            }
            var contentObj = document.getElementById('main');
			var contentHeight = contentObj.offsetHeight;
			var contentWidth = contentObj.offsetWidth;
            var footerElement = document.getElementById('footer');
            var footerHeight = footerElement.offsetHeight;
            var footerWidth = footerElement.offsetWidth;
            helpHeight = helpHeight - footerHeight;

//alert("windowHeight: " + windowHeight);
//alert("helpHeight: " + helpHeight);
//alert("contentHeight: " + contentHeight);
            //contentHeight = Math.max(helpHeight,contentHeight);
			
			
//alert("footerHeight: " + footerHeight);
//alert("helpHeight: " + helpHeight);
//alert("contentHeight: " + contentHeight);

            if(windowHeight < contentHeight)
            {
            	footerElement.style.top = contentHeight; 
            	if (null != helpObj)
	            {
					helpObj.style.height = (contentHeight + footerHeight) + "px";
				}           	
            	//alert("if windowHeight: " + windowHeight + " contentHeight: " + contentHeight + " footerHeight: " + footerHeight + " top: " + footerElement.style.top);
            }
            else
            {
            	footerElement.style.top = (windowHeight - footerHeight) + "px";    
            	if (null != helpObj)
	            {
					helpObj.style.height = windowHeight + "px";
				}         	
            	//alert("else windowHeight: " + windowHeight + " contentHeight: " + contentHeight + " footerHeight: " + footerHeight + " top: " + footerElement.style.top);
            }
            
            if (contentWidth > footerWidth)
            {
            	footerElement.style.width = contentWidth + "px";	
            }
            
        }
    }
    
    var helpDocIFrameObj = document.getElementById('helpIFrame');
    if (helpDocIFrameObj)
	{
		HELP_FRAME_HEIGHT = getHelpFrameSize();
		helpDocIFrameObj.style.height = HELP_FRAME_HEIGHT + "px";
		//divObj = HELP_MAIN_DIV ? HELP_MAIN_DIV : frames["helpIFrame"].document.getElementById("mainHelpPage");
		//alert("doc=["+frames['helpIFrame'].document+"]");
		//alert("doc.body.offsetHeight=["+frames['helpIFrame'].document.body.offsetHeight+"]");
		//alert("doc.div=["+frames['helpIFrame'].document.getElementById('mainHelpPage')+"]");
//		if (divObj) alert("HELP_MAIN_DIV=["+HELP_MAIN_DIV+"]\n HELP_MAIN_DIV.id=["+HELP_MAIN_DIV.id+"]\n HELP_MAIN_DIV.offsetHeight=["+HELP_MAIN_DIV.offsetHeight+"]\n divObj.offsetParent=["+divObj.offsetParent+"]\n divObj.offsetHeight=["+divObj.offsetHeight+"]\n helpObj.offsetHeight=["+helpObj.offsetHeight+"]\n helpDocIFrameObj.offsetHeight=["+helpDocIFrameObj.offsetHeight+"]\n HELP_FRAME_HEIGHT=["+HELP_FRAME_HEIGHT+"]\n frames['helpIFrame'].document.HELP_FRAME_HEIGHT=["+frames['helpIFrame'].document.HELP_FRAME_HEIGHT+"]\n getHelpFrameSize()=["+getHelpFrameSize()+"]");
//		else alert("helpObj.offsetHeight=["+helpObj.offsetHeight+"]\n helpDocIFrameObj.offsetHeight=["+helpDocIFrameObj.offsetHeight+"]\n HELP_FRAME_HEIGHT=["+HELP_FRAME_HEIGHT+"]\n frames['helpIFrame'].document.HELP_FRAME_HEIGHT=["+frames['helpIFrame'].document.HELP_FRAME_HEIGHT+"]\n getHelpFrameSize()=["+getHelpFrameSize()+"]");
	}
}

function getWindowHeight()
{
	var windowHeight=0;
	if(typeof(window.innerHeight)=='number')
	{
		windowHeight=window.innerHeight;
	}
	else
	{
		if(document.documentElement &&	document.documentElement.clientHeight)
		{
			windowHeight = document.documentElement.clientHeight;
		}
		else
		{
			if(document.body && document.body.clientHeight)
			{
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getWindowWidth()
{
	var windowWidth=0;
	if(typeof(window.innerWidth)=='number')
	{
		windowWidth=window.innerWidth;
		//alert("if innerWidth: " + window.innerWidth);
	}
	else
	{
		if(document.documentElement &&	document.documentElement.clientWidth)
		{
			windowWidth = document.documentElement.clientWidth;
			//alert("else element: " + windowWidth);
		}
		else
		{
			if(document.body && document.body.clientWidth)
			{
				windowWidth = document.body.clientWidth;
				//alert("else doc: " + windowWidth);
			}
		}
	}
	//alert("scrollwidth: " + document.body.scrollWidth);
	return windowWidth;
}


function createValMouseOver()
{
	changeToHide('createvalimage');
	changeToShow('createvaltext');
}

function createValMouseOut()
{
	// IE calls a mouseout event when scrolling over an inner div. This
	// causes the ui to flicker for the create valuation list.
	var doIEFix = false;
	if (typeof event != 'undefined')
	{
		doIEFix = true;
	}
	
	if (!doIEFix || !event.fromElement.contains(event.toElement) &&!document.getElementById('createvalsection').contains(event.toElement)) 
	{	
		changeToHide('createvaltext');
		changeToShow('createvalimage');	
	}
	
}

function searchMouseOver()
{
	var obj = document.getElementById('searchvalimage');
	obj.src = "/xtol/images/xv2_search_on.jpg";
	
}

function searchMouseOut()
{
	// IE calls a mouseout event when scrolling over an inner div. This
	// causes the ui to flicker for the create valuation list.
	var doIEFix = false;
	if (typeof event != 'undefined')
	{
		doIEFix = true;
	}
	
	if (!doIEFix || !event.fromElement.contains(event.toElement) &&!document.getElementById('searchsection').contains(event.toElement)) 
	{ 
		var obj = document.getElementById('searchvalimage');
		obj.src = "/xtol/images/xv2_search_off.jpg";
		
	}
}

function collabMouseOver()
{
	var obj = document.getElementById('collabimage');
	obj.src = "/xtol/images/xv2_collaborate_on.jpg";
}

function collabMouseOut()
{
	// IE calls a mouseout event when scrolling over an inner div. This
	// causes the ui to flicker for the create valuation list.
	var doIEFix = false;
	if (typeof event != 'undefined')
	{
		doIEFix = true;
	}
	
	if (!doIEFix || !event.fromElement.contains(event.toElement) &&!document.getElementById('collabsection').contains(event.toElement)) 
	{ 
		obj = document.getElementById('collabimage');
		obj.src = "/xtol/images/xv2_collaborate_off.jpg";
	}
}


function adjustXV2DropDown(dropDownId, headerItemId, showDropDown)
{
	var newValObj = document.getElementById(headerItemId);
	if (showDropDown)
	{
		
		document.getElementById(dropDownId).style.display = "block";
		newValObj.style.backgroundColor = '#CCCCCC';
		newValObj.style.color = '#555555';
	}
	else
	{
		document.getElementById(dropDownId).style.display = "none";
		newValObj.style.backgroundColor = '#FFFFFF';
		newValObj.style.color = '#888888';
	}
}

function highlightMenuItem(menuItem)
{
	menuItem.style.backgroundColor = '#6e6e6e';
	menuItem.style.color = '#FFFFFF';
}

function unhighlightMenuItem(menuItem)
{
	menuItem.style.backgroundColor = '#FFFFFF';
	menuItem.style.color = '#6e6e6e';
}

function changeStyle(element, style)
{
	try
	{
		element.className=style;
		//alert(element.className)
	}
	catch (e)
	{
		try { element.setAttribute("className",style);}
		catch (e) { alert(e);}
	}
}

//	setting must be a valid number including or between 0 and 1.0.
function setTransparent(id, setting)
{
	var percent = 100;
	try
	{
		percent = Math.round(parseFloat(setting) * 100);
	}
	catch (e) { alert(e);}

	var obj = document.getElementById(id);
	
	if (obj)
	{
		//opacity:0; filter:alpha(opacity=0); -moz-opacity:0;
		obj.style.filter = "alpha(opacity=" + percent + ")";
		obj.style.opacity = "" + setting;
		obj.style.mozOpacity = "" + setting;
	}	
}

function toggleHelpServer()
{
	ajaxPost("HndHelp", "InPageHelpToggleAJAX=true", null);
}

function toggleHelpDiv(id, anchor)
{
	if (showHelp) hideHelpDiv();
	else showHelpDiv(true, id, anchor);
}

function hideHelpDiv()
{
	toggleHelpServer();
	showHelp = false;
	changeToHide('help');
	resize();
}

function showHelpDiv(toggleServer, id, anchor)
{
	showHelp = true;
	var helpIFrameWin = window.helpIFrame;
	//alert("help iframe src=[" + helpIFrameWin.location + "]");

	//	The help iFrame is already in the page	
	if (null != helpIFrameWin && typeof(helpIFrameWin) != "undefined")
	{
		//	The help iFrame is there but empty - submit the help form to the iframe.
		if (helpIFrameWin.location.href.indexOf("blank.htm") != -1)
		{
			submitHelpFormToIFrame(id, anchor);
		}
					
		if (toggleServer)
		{
			toggleHelpServer();
		}
		changeToShow('help', 'block');
		resize();
	}
	//	The help iFrame is not there - reload the page with help in it.
	//	This case should not happen anymore.
	else
	{
		document.helpForm.submit();
	}
}

function bover (buttonId) { changeStyleImg(buttonId, 'over'); }
function bout  (buttonId) { changeStyleImg(buttonId, '');  }

function changeStyleImg(buttonId, over)
{
	var element = document.getElementById(buttonId+"img");
	if (element != null) changeStyle(element, buttonId+'img'+over);

	element = document.getElementById(buttonId+"box");
	if (element != null) changeStyle(element, buttonId+'box'+over);

	element = document.getElementById(buttonId);
	if (element != null) changeStyle(element, buttonId+'txt'+over);

}

/*
	IE fires the resize event everytime a window is reloaded. This works
	with doAfterOnload to only happen when a user-initiated resize occurs. 
*/
function updateUserWinWidth()
{
	if (USER_WIN_WIDTH != -1)
	{
		var windowWidth = getClientWindowWidth();
		var qString="userWinWidth=" + windowWidth;
		var boundary = SMALL_SIZE + MINIMUM_HELP_SIZE;
		var crossedBoundary =	(USER_WIN_WIDTH > boundary && windowWidth <= boundary)
							||	(USER_WIN_WIDTH <= boundary && windowWidth > boundary);
		//alert("updateUserWinWidth\n USER_WIN_WIDTH=["+USER_WIN_WIDTH+"]\n boundary=["+boundary+"]\n windowWidth=["+windowWidth+"]");
								
		if (crossedBoundary)
		{
			ajaxPost("HndValuation", qString, null);
		}
		if (windowWidth > boundary)
		{
			isInPageHelp = true;	
		}
		else
		{
			isInPageHelp = false;
		}
	
		USER_WIN_WIDTH = windowWidth;
	}
//	else { alert("called updateUserWinWidth when USER_WIN_WIDTH = -1"); }
}

function getStyleSheetFromDoc(docObj, filename)
{
	var sheet = null;
	//alert("getStyleSheetFromDoc("+docObj+", "+filename+") docObj.styleSheets.length=["+docObj.styleSheets.length+"]");
	for (var i = 0; i < docObj.styleSheets.length; i++)
	{
		if (docObj.styleSheets[i].href.indexOf(filename) != -1)
		{
			//alert("docObj.styleSheets["+i+"].href=["+docObj.styleSheets[i].href+"]");
			sheet = docObj.styleSheets[i];
			break;
		}
	}
	return sheet;
}

function getCSSRuleFromStyleSheet(sheet, ruleName, equals)
{
	var rule = null;
	var rules = (sheet.cssRules) ? sheet.cssRules : sheet.rules;
	//alert("rules.length=["+rules.length+"]"); 
	for (var i = 0; i < rules.length; i++)
	{
		var currRule = (rules.item(i)) ? rules.item(i) : rules(i);
		//alert("currRule.selectorText=["+currRule.selectorText+"]"); 
		//if (currRule.selectorText.toLowerCase().indexOf(ruleName) != -1) alert("ruleName=["+ruleName+"]\n rules["+i+"].selectorText=["+currRule.selectorText+"]\n cssText=["+currRule.cssText+"]\n parentRule=["+currRule.parentRule+"]\n type=["+currRule.type+"]");
		var test =	equals
					?	(currRule.selectorText.toLowerCase() == ruleName)
					:	(currRule.selectorText.toLowerCase().indexOf(ruleName) != -1);
		if (test)
		{
			//alert("score");
			rule = currRule;
			break;
		}
	}
	return rule;
}

function xv2ButtonForceMouseOver(id)
{
	var obj = document.getElementById(id);
	obj.style.color = "#6D97C1";	//	Keep in sync with style xv2button
}

function xv2ButtonForceMouseOut(id)
{
	var obj = document.getElementById(id);	//	Keep in sync with style xv2button:hover	
	obj.style.color = "#23437F";
}