var isInPageHelp = false;
var HELP_POPUP_WINDOW_NAME = "mhspopuphelpwin";
var HELP_IN_PAGE_WINDOW_NAME = "helpIFrame";

//	Define a HelpQuery object used to generate URLs using the toString method
function HelpQuery()
{
}
//	Define default values for HelpQuery instances
HelpQuery.prototype.servlet = "HndHelp";
HelpQuery.prototype.helpDisplayMode = "helpDisplayInPopup";
HelpQuery.prototype.helpFrameMode = "helpFrameNoNav";

//	Generates a help query based on the properties of the HelpQuery instance.
//	The initial portion of the query takes the following form where the
//	HelpQuery property values are indicated with square brackets[]:
//	
//		[servlet]?helpDisplayMode=[helpDisplayMode]
// 
//	The remaining portion of the query is generated using the following formula
//	that is repeated for any other defined properties:
//
//		&[propName]=[propValue]
// 
//	Any property you set will be added to the query string using the formula
//	above. The following is a list of properties and their descriptions
//	(including default values if any) that are typically used by the help
//	system:
//
//		servlet: The name of the help servlet used to process help requests.
//		(default: HndHelp).
//		
//		helpDisplayMode: The requested help display mode. The current options
//		are "helpDisplayInPopup" (default) and "helpDisplayInPage".
//		
//		helpFrameMode: The frame mode for pop-up help topic display. This is
//		ignored when the helpDisplayMode=helpDisplayInPage. It determines
//		whether help navigational aids (Search, Table of Contents, Index) are
//		displayed in the help pop-up window. The current options are
//		"helpFrameNoNav" (default) and "helpFrameNav"
//		
//		helpCurPage: The Help Topic ID to query.
//		
//		XTOL_CUR_PAGE/XSADMIN_CUR_PAGE/etc.: This is the name of the parameter
//		used to indicate the
//		current page of the application. In the absence of an id, help will
//		attempt to determine the correct help topic based on the current page
//		of the application.
//		
//		helpAnchor: The anchor name of the location within the specified help
//		topic of which to link. There are a select few topics that have anchors
//		within them. (e.g. Commercial and Ag use type topics)
//		
//		helpMode: The mode to set when opening help. This is primarily used by
//		help when in popup display mode to determine which help navigational
//		aid to display in the navigation pane (Search, Table of Contents,
//		Index).
HelpQuery.prototype.toString = function()
{
	var query = this.servlet + "?";
	query += "helpDisplayMode=" + this.helpDisplayMode;
	
	for (var propName in this)
	{
		if (	propName != "helpDisplayMode"
			&&	propName != "servlet"
			&&	propName != "toString"
			&&	this[propName] != "null"
			&&	this[propName]					)
		{
			query += "&" + propName + "=" + this[propName];
		}
	}
	//alert("HelpQuery.prototype.toString()=[" + query + "]");
	return query;
}



function MHSHelpPopup(query)
{
	//alert("type of the regular help window is " + typeof mhsHelpNoNavWin);
	if (typeof mhsHelpNoNavWin != 'undefined' && !mhsHelpNoNavWin.closed)
	{
		mhsHelpNoNavWin.close();
	}
	if (typeof mhsHelpNavWin != 'undefined' && !mhsHelpNavWin.closed)
	{
		mhsHelpNavWin.close();
	}

	var width = 400;
	var height = 400;
	var shape = "width=" + width + ",height=" + height;
	var position = centerPopup(height, width);
	var features = position + ',' + shape + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes';
	var mhsHelpWin = window.open(query, 'mhspopuphelpwin', features);
	if (mhsHelpWin.opener == null) mhsHelpWin.opener = self;
	mhsHelpWin.resizeTo(width, height);
	mhsHelpWin.focus();
}

function MHSNavHelpPopup(query)
{
	//alert("type of the regular help window is " + typeof mhsHelpNoNavWin);
	if (typeof mhsHelpNoNavWin != 'undefined' && !mhsHelpNoNavWin.closed)
	{
		mhsHelpNoNavWin.close();
	}
	if (typeof mhsHelpNavWin != 'undefined' && !mhsHelpNavWin.closed)
	{
		mhsHelpNavWin.close();
	}

	var width = 658;
	var height = 400;
	var shape = "width=" + width + ",height=" + height;
	var position = centerPopup(height, width);
	var features = position + ',' + shape + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes';
	var mhsHelpWin = window.open(query, 'mhspopuphelpwin', features);
	if (mhsHelpWin.opener == null) mhsHelpWin.opener = self;
	mhsHelpWin.resizeTo(width, height);
	mhsHelpWin.focus();
}

function MHSNavHelpPopupFromOpener(queryObj)
{
	if (top.opener != null && !top.opener.closed)
	{
		//alert("top.opener is valid = ["+top.opener+"]");
		top.opener.MHSNavHelpPopup(queryObj.toString());
	}
	else
	{
		MHSNavHelpPopup(queryObj.toString());
	}
}

//******************************
//  Default Help Navigation

	function hNav(id, forcePopup)
	{
		if (getControlWin(self).isInPageHelp && forcePopup)
		{
			//	This needs to open help from the parent window so if there is
			//	a session timeout situation the parent window of the popup
			//	will be the main window. Otherwise, a 
			parent.openPopupHelp(id);
		}
		else if (getControlWin(self).isInPageHelp)
		{
			//alert("Setting mode to helpModeHelp in parent");
			//parent.setHelpModeInPage("helpModeHelp");
			//alert("Opening the Help Page to "+id);
			parent.openInPageHelp(id);
		}
		else
		{
			HelpNav("helpFrameTopic", id);
		}
	}
//******************************

//******************************
//  Default Help Navigation

function HelpNav(frameName, id)
{
	top.helpCurPage = id;
	var topicFrame = top[frameName];
	var loc = new HelpQuery();
	loc.helpTopicId = id; 
	topicFrame.location = loc.toString();
}
//******************************

//******************************
//  Update Help cur topic ID

function HelpSetCurTopic(id)
{
	top.helpCurPage = id;
	//alert("HelpSetCurTopic - top.helpCurPage is now = ["+top.helpCurPage+"]");
}
//******************************

//******************************
//  Open Help Window
//******************************
//  Open's help to the specified topic id in the no-nav mode.

function OpenHelp(id, anchor)
{
	//alert("in OpenHelp(["+id+"], ["+anchor+"]) in helpScripts.js (inpage=["+isInPageHelp+"])");
	if (getControlWin(self).isInPageHelp)
	{
		//parent.setHelpModeInPage("helpModeHelp");		
		openInPageHelp(id, anchor);
	}
	else
	{
		openPopupHelp(id, anchor);
	}
}
//******************************

function openPopupHelp(id, anchor)
{
	var query = new HelpQuery();
	query.helpMode = "helpSearch";
	query.helpCurPage = id;
	query.helpAnchor = anchor;
	//alert("in openPopupHelp in help module helpScripts.js query=[" + query.toString() + "]");
	MHSHelpPopup(query.toString());
}
//******************************

function openPopupHelpWithNav(id, anchor)
{
	var query = new HelpQuery();
	query.helpMode = "helpSearch";
	query.helpCurPage = id;
	query.helpAnchor = anchor;
	query.helpFrameMode = "helpFrameNav";
//	alert("in openPopupHelpWithNav in help module helpScripts.js query=[" + query.toString() + "]");
	MHSNavHelpPopup(query.toString());
}
//******************************

function openPopupHelpFromOpenerWithNav(id, anchor)
{
	var query = new HelpQuery();
	query.helpMode = "helpSearch";
	query.helpCurPage = id;
	query.helpAnchor = anchor;
	query.helpFrameMode = "helpFrameNav";
//	alert("in openPopupHelpWithNav in help module helpScripts.js query=[" + query.toString() + "]");
	MHSNavHelpPopupFromOpener(query.toString());
}
//******************************

//******************************
//  Open In-page Help Div
//******************************
//  Open's help to the specified topic id in the in-page mode.

function openInPageHelp(id, anchor)
{
	//alert("in openInPageHelp(id=["+id+"], anchor=["+anchor+"])");
	var helpIFrameWin = window.helpIFrame;

	//	The help iFrame is not in the page yet.
	if (	null == helpIFrameWin
		||	typeof(helpIFrameWin) == "undefined"	)
	{
		//	Set the help form with the correct Topic ID and Toggle Help
//			alert("The iFrame is not there yet - set ID and toggle help");
		document.helpForm["helpCurPage"].value = id;
		toggleHelpDiv();
	}
	//	Help is in the page
	else
	{
		//	the correct topic is already there.
		if (	null != helpIFrameWin.helpCurPage
			&&	typeof(helpIFrameWin.helpCurPage) != "undefined"
			&&	id == helpIFrameWin.helpCurPage						)
		{
			//alert("Help id is already there - showing help if hidden");

			//	display help if necessary, otherwise do nothing.
			if (!showHelp)
			{
				toggleHelpDiv();
			}
			
			parent.setHelpModeInPage('helpModeHelp');
		}
		//	Must Update iframe with correct topic.
		else
		{
			//alert("in openInPageHelp - nested else");
			submitHelpFormToIFrame(id, anchor);

			//	display help if necessary
			if (!showHelp)
			{
				toggleHelpDiv(id, anchor);
			}
		}
	}
}
//******************************

function submitHelpFormToIFrame(id, anchor)
{
	var helpIFrameWin = window.helpIFrame;

	//	Save the original help form data.
	var formAction = document.helpForm.action;
	var formTarget = document.helpForm.target;
	var formCurrPage = document.helpForm["helpCurPage"].value;
	var formAnchor = document.helpForm["helpAnchor"].value;
	var formMode = document.helpForm["helpMode"].value;
	
	//	Modify the help form & Submit it.
	document.helpForm.action = "HndHelp";
	document.helpForm.target = window.helpIFrame.name;
	document.helpForm["helpMode"].value = "helpModeHelp";

	//	Set id if not null, otherwise clear
	if (id)
	{
		document.helpForm["helpCurPage"].value = id;
	}
	else
	{
		document.helpForm["helpCurPage"].value = "";
	}

	//	Set anchor if not null, otherwise clear
	if (null != anchor && typeof(anchor) != "undefined")
	{
		document.helpForm["helpAnchor"].value = anchor;
	}
	else
	{
		document.helpForm["helpAnchor"].value = "";
	}

	document.helpForm.submit();
	
	//	Reset the help form
	document.helpForm.action = formAction;
	document.helpForm.target = formTarget;
	document.helpForm["helpCurPage"].value = formCurrPage;
	document.helpForm["helpMode"].value = formMode;
	document.helpForm["helpAnchor"].value = formAnchor;
}
//******************************

//******************************
//  Open Help Window
//******************************
//  Open's help to the specified topic id in the nav mode. This should only
//	be called from the application window.

function OpenHelpWithNav(id, anchor)
{
	//alert("in OpenHelpWithNav in helpScripts.js (getControlWin(self).isInPageHelp=["+getControlWin(self).isInPageHelp+"])");
	if (getControlWin(self).isInPageHelp)
	{
		//parent.setHelpModeInPage("helpModeHelp");		
		openInPageHelp(id, anchor);
	}
	else
	{
		openPopupHelpWithNav(id, anchor);
	}
}
//******************************

//******************************
//  Open Help Window From the Opener Window
//******************************
//  Open's help from the opener page (if it exists) to the specified topic id
//	in the nav mode.

function OpenHelpFromOpenerWithNav(id)
{
	if (top.opener != null && !top.opener.closed)
	{
		//alert("top.opener is valid = ["+top.opener+"]");
		top.opener.OpenHelpWithNav(id);
	}
	else
	{
		OpenHelpWithNav(id);
	}
}
//******************************

//******************************
//  Open Help Window From the Opener Window
//******************************
//  Open's help from the opener page if it existsto the specified topic id in the no-nav mode.

function OpenHelpFromOpener(id)
{
	if (top.opener != null && !top.opener.closed)
	{
		//alert("top.opener is valid = ["+top.opener+"]");
		top.opener.OpenHelp(id);
	}
	else
	{
		OpenHelp(id);
	}
}
//******************************

//******************************
//  Open Help Window From the Opener Window
//******************************
//  Open's help from the opener page if it existsto the specified topic id in the no-nav mode.

function openPopupHelpFromOpener(id)
{
	if (top.opener != null && !top.opener.closed)
	{
		//alert("top.opener is valid = ["+top.opener+"]");
		top.opener.openPopupHelp(id);
	}
	else
	{
		openPopupHelp(id);
	}
}
//******************************

//******************************
//	isParentWorksheet
//******************************
//	This function will return true if the parent window is the Residential Worksheet. Otherwise, it will return false
//
//	NOTE: This should only be called from a pop-up window. It calls top.opener so it can function with popup windows
//	that use frames. However, State Farm runs our application within a browser frame, therfore, if you call this from
//	the main window, it will fail because we have no access to the window within which our app is running.

function isParentWorksheet()
{
	var retVal = false;
	if	(		top.opener != null
			&&	!top.opener.closed
			&&	top.opener.document != null
			&&	top.opener.document.xtolform != null
			&&	typeof (top.opener.document.xtolform['curpage']) != "undefined"
			&&	top.opener.document.xtolform['curpage'].value == "Worksheet"
		)
	{
		retVal = true;
	}
	return retVal;
}
//******************************


//******************************
//	OpenWorksheetInterviewHelpFromOpener
//******************************
//	This function will open help for a pop-up window where the topics are different depending on
//	whether the parent page is on the Residential Worksheet.
//
//	NOTE: This should only be called from a pop-up window. It calls top.opener so it can function with popup windows
//	that use frames. However, State Farm runs our application within a browser frame, therfore, if you call this from
//	the main window, it will fail because we have no access to the window within which our app is running.

function OpenWorksheetInterviewHelpFromOpener(wksHelpId, intHelpId)
{
	var id = wksHelpId;
	if (!isParentWorksheet())
	{
		id = intHelpId;
	}
	OpenHelpFromOpener(id);
}

//******************************
//	OpenHelpFromOpenerByType
//******************************
//	This function will open help for a pop-up window where the topics are different depending on
//	the valuation type (Residential, Agricultural, Commercial, Condo (As Built), Condo (Bare Walls In), Manufactured).
//
//	NOTE: This should only be called from a pop-up window. It calls top.opener so it can function with popup windows
//	that use frames (eg. Help Center). However, State Farm runs our application within a browser frame, therfore, if you call this from
//	the main window, it will fail because we have no access to the window within which our app is running.

function OpenHelpFromOpenerByType(valType, subType, helpTypeArray)
{
	var id = getHelpIdByType(valType, subType, helpTypeArray);
	OpenHelpFromOpener(id);
}

function openPopupHelpFromOpenerByType(valType, subType, helpTypeArray)
{
	var id = getHelpIdByType(valType, subType, helpTypeArray);
	openPopupHelpFromOpener(id);
}

function OpenHelpByType(valType, subType, helpTypeArray)
{
	var id = getHelpIdByType(valType, subType, helpTypeArray);
	OpenHelp(id);
}

function openPopupHelpByType(valType, subType, helpTypeArray)
{
	var id = getHelpIdByType(valType, subType, helpTypeArray);
	openPopupHelp(id);
}

//	Retruns the help topic associated with the val type using the val type as an index to the helpTypeArray.
//	If that item is undefined, it searches the array for the first defined value and returns that.
function getHelpIdByType(valType, subType, helpTypeArray)
{
	var helpId = null;
	//alert("valType = [" + valType + "]\n subType = [" + subType + "]\n helpTypeArray = [" + helpTypeArray + "]");
	
	if (valType > 0 && valType <= helpTypeArray.length)
	{
		valType--;
		if (helpTypeArray && helpTypeArray[valType] && helpTypeArray[valType][subType])
		{
			helpId = helpTypeArray[valType][subType];
		}
		else
		{
			//	Get the type array.
			var typeArray = null;
			if (	helpTypeArray.length > valType
				&&	helpTypeArray[valType]			)
			{
				typeArray = helpTypeArray[valType];
			}
			//	If it's null, grab the first one that's not null
			else
			{
				for (var i = 0; i < helpTypeArray.length; i++)
				{
					if (null != helpTypeArray[i])
					{
						typeArray = helpTypeArray[i];
						break;
					}
				}
			}
				
			//	Grab the subtype.
			if (	typeArray.length > subType
				&&	typeArray[subType]				)
			{
				helpId = typeArray[subType];
			}
			//	If it's null, grab the first one that's not null
			else
			{
				for (var i = 0; i < typeArray.length; i++)
				{
					if (typeArray[i])
					{
						helpId = typeArray[i];
						break;
					}
				}
			}
		}
	}

	return helpId;
}

//******************************



//******************************
//  Resize the Help Window to show or hide the navigation (Change help mode)
//******************************
//  Switches between the navigation and non-navigation help display modes.
function changeHelpFrameMode(mode)
{
	var query = new HelpQuery();
	query.helpMode = top.helpMode;
	query.helpFrameMode = mode;
	query.XTOL_CUR_PAGE = top.helpAppCurrPage;
	query.helpCurPage = top.helpCurPage;
	if (mode == "helpFrameNav")
	{
		top.resizeTo(658, 400);
	}
	else
	{
		top.resizeTo(400, 400);
	}
	top.location = query.toString();
}
//******************************


//******************************
//  Resize the Help Window to show or hide the navigation (Change help mode)
//******************************
function changeHelpMode(mode, indexNum)
{
	var loc = new HelpQuery();
	loc.helpMode = mode;
	loc.helpIndexNumber = indexNum;

	if (getControlWin(self).isInPageHelp)
	{
		//parent.setHelpModeInPage(mode);
		
		loc.helpCurPage = helpCurPage;
		loc.helpDisplayMode = "helpDisplayInPage";
		loc.hML = hML;
		//alert("changeHelpMode setting location to ["+loc+"]");
		self.location = loc.toString();
	}
	else
	{
		loc.servlet = "ComHelpLoadNavFrame.jsp";
		top.helpMode = mode;
		top.helpFrameNav.location = loc.toString();
		self.location = self.location;
	}
}


//******************************
//  Submit a help topic search
//******************************
function submitHelpSearch(submitForm)
{
	//alert("in submitHelpSearch submitForm = ["+submitForm+"]");
	if (getControlWin(self).isInPageHelp)
	{
		//alert("curr page = ["+helpCurPage+"]");
		parent.setHelpModeInPage("helpSearch");
		document.helpSearchForm["helpCurPage"].value = helpCurPage;
		//alert("multi = ["+hML+"]");
		if (null != hML && hML != "" && typeof(hML) != "undefined")
		{
			document.helpSearchForm["hML"].value = hML;
		}
	}
	
	if(submitForm)
	{
		document.helpSearchForm.submit();
	}
}

//******************************
//  Submit a help topic search
//******************************
function submitHelpSearchShowTOC()
{
	if (getControlWin(self).isInPageHelp)
	{
		//alert("curr page = ["+helpCurPage+"]");
		parent.setHelpModeInPage("helpSearch");
		document.helpSearchForm["helpCurPage"].value = helpCurPage;
		document.helpSearchForm["helpInPageShowTOC"].value = "true";
		//alert("in submitHelpSearchShowTOC document.helpSearchForm['helpInPageShowTOC'].value = " + document.helpSearchForm['helpInPageShowTOC'].value);
		//alert("multi = ["+hML+"]");
		if (null != hML && hML != "" && typeof(hML) != "undefined")
		{
			document.helpSearchForm["hML"].value = hML;
		}
	}
	
	document.helpSearchForm.submit();
}

//******************************
//  Link to the multi-link index page
//******************************
function loadMultiLink(terms)
{
	var loc = 	new HelpQuery();
	loc.hML = terms;
	
	//alert("terms = {"+terms+"}");
	if (getControlWin(self).isInPageHelp)
	{
		parent.setHelpModeInPage("helpModeHelp");
		loc.helpDisplayMode = "helpDisplayInPage";
	}
	

	self.location = loc.toString();
}

//******************************
//  isMSIE
//******************************
//  Returns true if the browser is MSIE. Otherwise, returns false.

	function isMSIE()
	{
		return (getMSIEVersionNumber() != -1);
	}
//******************************

//******************************
//  getMSIEVersionNumber
//******************************
//  Get the version number of MSIE Browser.
//	If the browser is not MSIE, returns -1
//	Otherwise returns the version as a float.

function getMSIEVersionNumber()
{
	var previousString	= "MSIE ";
	var trailingString	= ";";
	var appString		= "Microsoft";

	if (navigator.appName.indexOf(appString) == -1)
	{
		return -1;
	}

	var versionString = navigator.appVersion;
	var begin = previousString.length + versionString.indexOf(previousString);
	var end = versionString.indexOf(trailingString, begin);
	versionString = versionString.substring(begin, end);
	return parseFloat(versionString);
}
//******************************

//******************************
//  makeOnScreen
//******************************
//  Makes sure the element specified by the given id is visible. Works in
//	standards-compatible browsers and in IE
function makeOnScreen(id)
{
	//alert("makeOnScreen(" + id + ")");
	var obj = document.getElementById(id);
	//alert("obj=["+obj+"]");
	if (obj)
	{
		if (isMSIE())
		{
			if(getMSIEVersionNumber() >= 6.0)
			{
				obj.scrollIntoView(true);
			}
			else
			{
				document.location = document.location;
			}
		}
		else
		{
			obj.focus();
		}
	}
}
//******************************

//******************************
//  submitDummyTOCId
//******************************
//  Submits a request from the Dummy TOC to display the real TOC and
//	expand the specified ID
function dummyOnClickHandler(evt)
{
	var target = evt ? evt.target : event.srcElement;
	if (is_list_node(target))
	{
		if (getControlWin(self).isInPageHelp)
		{
			document.helpSearchForm["helpTOCNodeExpand"].value = target.id;
			submitHelpSearchShowTOC();	
		}
	}
}
//******************************

//	Returns the window instance that contains the variables used to control
//	help behavior. This should only be called when you know you are in a
//	help topic. Otherwise it may propigate up the parent window chain until
//	it reaches a window that we have no permissions to access and throw an
//	error.
function getControlWin(thisWindow)
{
	//alert("in getControlWin");
	var retVal = thisWindow;
	var noSkip = false;

	do
	{
		if (noSkip)
		{
			thisWindow = thisWindow.parent;
		}

		if (thisWindow.helpControlWin)
		{
			retVal = thisWindow;
			break;
		}

		noSkip = true;

	} while (thisWindow.parent != thisWindow.self)

	return retVal;
}
		
function doHelpFramesOnLoad(event)
{
	if (!helpAppCurrPage)
	{
		if (opener.helpAppCurrPage)
		{
			helpAppCurrPage = opener.helpAppCurrPage;
		}
	}
}

