/*******************************

 NavigationTool.js, Version 4.2.2, 18-Nov-2008
 Copyright Northgate Information Solutions UK Limited, 2002-2008

 *******************************/

/********************
 NavigationTool class

 Constructor:
	NavigationTool()
 Properties:
	parent
	searching
	timeout
	url
	useLinksFrame
 Methods:
	load()
	initialise()
	terminate()
	resize()
	textSearch(text)
	toString()

 ********************/

// *** Constructor for NavigationTool class ***
function NavigationTool() {
	this.parent = null;
	this.searching = false;
	this.url = "";
	this.useLinksFrame = false;
	this.scope = "";
	// Reference to timeout object so it can be cleared.
	this.timeout = null;
}

// *** NavigationTool instance methods ***
	// Load navigation tool.
NavigationTool.prototype.load = function ntLoad() {
    /* If it is not already loaded, open the contents or 
       index file in the navigation frame. */

	if (typeof this.parent.navFrame != "undefined") {
		// Get the URL of the current navigation tool.
		var nav = this.parent.getNavigationUrl();

		// If the required navigation tool is not loaded...    
		if (!nav.isEqualIgnoreCase(this.url)) {
			// Load it.
			this.parent.navFrame.location.replace(
				unescape(this.parent.rootFolder + this.url));
		}
		else if (this.toString() == "Contents") {
			this.synchronise();
		}
	}
}

NavigationTool.prototype.initialise = function ntInitialise() {
	// Check for invalid navigation type.
	if (this.toString() == "NavigationTool")
		return;

	var doc = this.parent.navFrame.document;
	if (this.toString() == "Index")
		this.applet = this.waitUntilDefined(doc.HHCtrl);
	else if (this.toString() == "Contents")
		this.applet = this.parent.navFrame.ctList;

    if (this.toString() == "Contents") {
		this.applet.setGraphicsFolder(this.parent.configuration.navigationFiles.siteMap);

		// Disable unavailable navigation buttons.
		var sm = this.parent.configuration.navigationFiles.siteMap;
		if (!!sm) {
			if (this.parent.topic.url.startsWith(this.parent.configuration.controlFolder, true))
				this.scope = this.parent.configuration.navigationFiles.defaultSection;
			else
				this.scope = this.parent.topic.url.split("/").slice(1, 2)[0].toLowerCase();
			var enabled = sm[this.scope][1];
			enabled = (enabled == "" ? "CISL" : enabled);
			var buttons = this.parent.optFrame.document.getElementsByTagName("INPUT");
			for (var n = 0; n < buttons.length; n++) {
				if (enabled.indexOf(buttons[n].name.slice(0, 1)) == -1)
					buttons[n].disabled = true;
				else
					buttons[n].disabled = false;
			}
		}
		
		this.synchronise();
	}

    this.resize();
}

NavigationTool.prototype.resize = function ntResize() {
	// Check for invalid navigation type.
	if (this.toString() == "NavigationTool")
		return;

	if (this.toString() == "Index" && DHTML.browserType == "IE") {
		this.parent.navFrame.index.resize();
	}
}

NavigationTool.prototype.waitUntilDefined = function ntWaitUntilDefined(obj) {
	var now;
	var start = new Date();
	do {
		now = new Date();
		if ((now.getTime() - start.getTime()) / 1000 > 5)
			break;
	} while (typeof obj == "undefined");
	return(obj);
}
NavigationTool.prototype.terminate = function ntTerminate(){};
NavigationTool.prototype.textSearch = function ntTextSearch(text){};
NavigationTool.prototype.toString = function ntToString(){return("NavigationTool");};
NavigationTool.prototype.handleEvent = function ntHandleEvent(e){return(false)};


/********************
 Contents class

 Constructor:
	Contents(parent)
 Properties:
	url				- Inherited from NavigationTool.
	data
	parent			- Inherited from NavigationTool.
	searching		- Inherited from NavigationTool.
	useLinksFrame	- Inherited from NavigationTool.
 Methods:
	load()			- Inherited from NavigationTool.
	initialise()	- Inherited from NavigationTool.
	terminate()
	resize()		- Inherited from NavigationTool.
	synchronise()
	textSearch()	- Inherited from NavigationTool.
	toString()

 ********************/

// *** Constructor for Contents class ***
function Contents(parent) {
	this.parent = ((typeof parent == "object") 
		&& (parent.constructor == HelpSystem)
		? parent : null);
	if (parent == null)
		return(null);

	this.data = "contentsdata.htm";
}

// *** Contents inherits from NavigationTool ***
Contents.prototype = new NavigationTool();
Contents.prototype.constructor = Contents;

// *** Contents instance methods ***
Contents.prototype.terminate = function ntcTerminate() {
    clearTimeout(this.timeout);
}

Contents.prototype.synchronise = function ntcSynchronise() {
	var sct;

	// Find out if a sync contents URL is specified in the topic.
	var d = this.parent.topicFrame.document;
	if (d.getElementById)
		sct = d.getElementById("syncContentsTo");
	else
		sct = d.all("syncContentsTo");
	
	// Get location of current topic, relative to the root.
	var topicUrl = this.parent.getTopicUrl(false);
	var syncTopic = null;
	
	// If a URL is specified...
	if (sct != null) {
		// Get the sync contents URL.
		var syncHref = sct.href;
		// If it starts with a protocol...
		if (syncHref.search(/^(?:file:|http:)/i) != -1) {
			// Make it relative to the root of the help system.
			syncTopic = syncHref.substring(this.parent.rootFolder.length);
		}
		else {
			/* Otherwise, get the path to the folder containing the
			   current topic. */
			syncTopic = topicUrl.split("/").slice(0, -1).join("/");
			syncTopic += (syncTopic != "" ? "/" : "");
			syncTopic += syncHref;
		}
		syncTopic = this.parent.absoluteUrl(syncTopic);
		syncTopic = "../../" + syncTopic;
	}
    /* Change the location of the topic, so that it's relative to
       the common contents folder. */
	topicUrl = "../../" + topicUrl;
    
    // Sync it in the table of contents
    if (this.applet != null) {
		this.applet.syncTopic = syncTopic;
		if (syncTopic)
		    this.applet.synchronise(true);
        this.applet.syncURL(topicUrl);
    }
}

Contents.prototype.toString = function ntcToString(){return("Contents")};


/****************
 Index class

 Constructor:
	Index(parent)
 Properties:
	parent			- Inherited from NavigationTool.
	searching		- Inherited from NavigationTool.
	url				- Inherited from NavigationTool.
	useLinksFrame	- Inherited from NavigationTool.
 Methods:
	load()			- Inherited from NavigationTool.
	initialise()	- Inherited from NavigationTool.
	terminate()		- Inherited from NavigationTool.
	resize()		- Inherited from NavigationTool.
	textSearch()	- Inherited from NavigationTool.
	setReference(reference)
	toString()

 ****************/
 
// *** Constructor for Index class ***
function Index(parent) {
	this.parent = ((typeof parent == "object") 
		&& (parent.constructor == HelpSystem)
		? parent : null);
	if (parent == null)
		return(null);
}

// *** Index inherits from NavigationTool ***
Index.prototype = new NavigationTool();
Index.prototype.constructor = Index;

// *** Index instance methods ***
Index.prototype.toString = function ntiToString(){return("Index")};


/****************
 Search class

 Constructor:
	Search(parent)
 Properties:
	fileList
	globalFileList
	globalIndex
	global
	index
	searching
	url				- Inherited from NavigationTool.
	useLinksFrame	- Inherited from NavigationTool.
 Methods:
	initialise()
	load()
	resize()
	terminate()
	textSearch()
	toString()

 ****************/
 
// *** Constructor for Search class ***
function Search(parent) {
	this.parent = ((typeof parent == "object") 
		&& (parent.constructor == HelpSystem)
		? parent : null);
	if (parent == null)
		return(null);

	this.searchEngine = null;     // Reference to the applet.
	this.searchData = "";	  // Path to the search data file.
	this.text = "";               // Text to search for in the displayed topic.
	this.matchWords = 1;  		  // The matchWords status from the search applet.
	this.matchCase = false;       // The matchCase status from the search applet.
	this.matchWord = false;		  // The matchWord status from the search applet.
	this.searching = false;       // Searching flag.
	this.previousTopic = "";      // Previous topic.
	this.global = true;
}

// *** Search inherits from NavigationTool ***
Search.prototype = new NavigationTool();
Search.prototype.constructor = Search;

// *** Search instance methods ***
	// Load the search page.
Search.prototype.load = function ntsLoad() {
    /* If it is not already loaded, open the search
       file in the navigation frame. */

    if (typeof this.parent.navFrame != "undefined") {
		// Get the URL of the current navigation tool.
		var nav = this.parent.getNavigationUrl();
    
		if (!nav.isEqualIgnoreCase(this.url))
			this.parent.navFrame.location.replace(
				unescape(this.parent.rootFolder + this.url));
		// Otherwise, reinitialise it.
		else
			this.initialise();
	}
}

	// Initialise the search applet.
Search.prototype.initialise = function ntsInitialise() {
	var s;
	
	this.terminated = false;
	
	// Get a reference to the search page.
    var d = this.parent.navFrame.document;

	this.setScopeDescription(d, "Search");
	
	if (this.terminated)
		return;
		
    // Set a reference to the search engine.
	this.searchEngine = this.parent.navFrame.searcher;

    // Set the properties of the search engine.
    var c = this.parent.configuration;
	this.searchEngine.rootFolder = this.parent.rootFolder;
    this.searchEngine.searchTool = this;
    this.searchEngine.setGlobal(this.global);
	this.searchEngine.setSearchData(this.searchData);
	this.searchEngine.resultsFrame = this.parent.topicFrame;
	this.searchEngine.stylesheet = c.searchResultsStylesheet;
	this.searchEngine.localSiteMaps = !!c.navigationFiles.siteMap;
	
	this.searchEngine.setSearchText(this.text + "");
	this.searchEngine.setMatchWords(this.matchWords);
	this.searchEngine.setMatchCase(this.matchCase);
	this.searchEngine.setMatchWord(this.matchWord);

	// Set up click events for this navigation tool.	
	d.onclick = self.navigationEvent;
}

	// Cancel search polling.
Search.prototype.terminate = function ntsTerminate() {
    // Store the current status of the search engine.
    this.matchWords = this.searchEngine.getMatchWords();
    this.matchCase = this.searchEngine.getMatchCase();
    this.matchWord = this.searchEngine.getMatchWord();
    this.text = this.searchEngine.getSearchText();
    this.global = this.searchEngine.getGlobal();

    this.searching = false;
}

	// Search the text of a topic.
Search.prototype.textSearch = function ntsTextSearch(text) {
	if (typeof text != "string")
		text = this.text;
		
	var p = this.parent;
	if (DHTML.browserType == "IE") {
	    var range = p.topicFrame.document.body.createTextRange();
		if (text != "") {
			var flags = (this.searchEngine.getMatchWord() ? 2 : 0) 
				+ (this.searchEngine.getMatchCase() ? 4 : 0);
			if (range.findText(this.text, 0, flags)) {
				range.scrollIntoView();
				try {
					range.select();
				}
				catch (e) {
					;
				}
			}
		}
	}
	else {
	    p.topicFrame.find(this.text, this.matchCase);
	}

    this.searching = false;
}

Search.prototype.handleEvent = function ntsHandleEvent(e) {
	// Get a reference to the clicked element.
	var obj = (document.all ? e.srcElement : e.target.parentNode);
	// Get a reference to the parent element.
	var p = (document.getElementById ? obj.parentNode : obj.parentElement);
	
	switch (e.type.toLowerCase()) {
	case "click":
/*		if (p.id.toLowerCase() == "searchScope"
				&& obj.type.toLowerCase() == "radio") {
			this.applet.setUseAlt = (obj != p.global);
			return(true);
		}
		else */
			return(false);
	default:
		return(false);
	}
}


Search.prototype.setScopeDescription = function ntSetScopeDescription(doc, opText) {
	// If there is any scope text, display it.
	var scopePara;
	var scopeText;

	//	Get a reference to the scope paragraph.
	if (doc.getElementById)
		scopePara = doc.getElementById("scope");
	else
		scopePara = doc.all["scope"];
		
	// Get the folder name from the URL.
	if (this.parent.topic.url.startsWith(this.parent.configuration.controlFolder, true))
		this.scope = this.parent.configuration.navigationFiles.defaultSection;
	else
		this.scope = this.parent.topic.url.split("/").slice(1, 2)[0].toLowerCase();
	this.parent.configuration.navigationFiles.defaultSection = this.scope;
	// Get the scope text.
	var nf = this.parent.configuration.navigationFiles;
	if (!!nf.siteMap)
			scopeText = nf.siteMap[this.scope][0];
	
	//	The scope text must be a string.
	if (typeof scopeText != "string")
		scopeText = "";
	if (scopeText != "")
		scopeText = opText.pathConcat(scopeText, " ");

	//	Set the paragraph's text content.
	if (scopePara != null) {
		//	If there is text to display, display the paragraph.
		if (scopeText != "") {
			scopePara.style.position = "static";
			scopePara.style.visibility = "visible";
			this.setupScopeList(doc,
								nf.siteMap,
								scopePara,
								this.scope,
								scopeText);
		}
		
		//	Otherwise, hide the paragraph.
		else {
			scopePara.style.position = "absolute";
			scopePara.style.visibility = "hidden";
		}
	}
}

Search.prototype.setupScopeList
		= function ntSetupScopeList(
			doc, siteMap, scopePara, section, scopeText) {
	var selectElement;
	var optionElement;
	
	// Create a select element.
	selectElement = doc.createElement("select");
	selectElement.disabled = true;
	selectElement.id = "helpSections";
	selectElement.name = "helpSections";
	selectElement.size = "1";
	if (doc.defaultView)
	{
		selectElement.onblur = doc.defaultView.selectScope;
		selectElement.onchange = doc.defaultView.selectLocal;
	}
	else
	{
		selectElement.onblur = doc.parentWindow.selectScope;
		selectElement.onchange = doc.parentWindow.selectLocal;
	}
	
	// Insert the select element.
	while (scopePara.hasChildNodes())
		scopePara.removeChild(scopePara.firstChild);
	scopePara.appendChild(selectElement);
	
	// Populate it with option elements.
	for (var sect in siteMap) {
		optionElement = doc.createElement("option");
		optionElement.text = siteMap[sect][0];
		optionElement.value = sect;
		try {
			selectElement.add(optionElement, null);
		}
		catch (e) {
			selectElement.add(optionElement);
		}
		optionElement.defaultSelected = (sect == section);
		optionElement.selected = (sect == section);
		
	}
	
	// Enable the scope option buttons.
	doc.getElementById("local").disabled = false;
	doc.getElementById("global").disabled = false;
	selectElement.disabled = false;
}

Search.prototype.resize = function ntsResize(){};			// Does nothing.
Search.prototype.toString = function ntsToString(){return("Search")};


/****************
 Links class

 Constructor:
	Links(parent)
 Properties:
	frameUrl
	linksPage
	parent			- Inherited from NavigationTool.
	searching		- Inherited from NavigationTool.
	url				- Inherited from NavigationTool.
	useLinksFrame	- Inherited from NavigationTool.
 Methods:
	load()
	initialise()
	terminate()
	resize()
	textSearch()	- Inherited from NavigationTool.
	toString()

 ****************/

// *** Constructor for Links class ***
function Links(parent) {
	this.parent = ((typeof parent == "object") 
		&& (parent.constructor == HelpSystem)
		? parent : null);
	if (parent == null)
		return(null);
	
	this.frameUrl = "";
	this.linksPage = "";
	this.useLinksFrame = true;
}

// *** Links inherits from NavigationTool ***
Links.prototype = new NavigationTool();
Links.prototype.constructor = Links;

// *** Links instance methods ***
	// Loads the links frame.
Links.prototype.load = function ntlLoad() {
    if (typeof this.parent.navFrame != "undefined") {
		// Get the URL of the current navigation tool.
		var nav = this.parent.getNavigationUrl();
    
		if (!nav.isEqualIgnoreCase(this.frameUrl))
			this.parent.navFrame.location.replace(
				unescape(this.parent.rootFolder + this.frameUrl));
		else
		    this.initialise();
	}
}

Links.prototype.initialise = function ntlInitialise() {
	// Get the reference to the links frame.
	var linksFrame = this.parent.getLinksFrame();
    // Get the URL of the current contents of the links frame.
    var lnk = this.parent.getLinksUrl();

    // If the specified links file is not already loaded, load it.
    if (this.linksPage != "") {
		if (!lnk.isEqualIgnoreCase(this.linksPage))
		    linksFrame.location.replace(unescape(this.parent.rootFolder
				+ this.linksPage));
	}
}

Links.prototype.resize = function ntlResize(){};
Links.prototype.toString = function ntlToString(){return("Links")};


/****************
 Glossary class

 Constructor:
	Glossary(parent)
 Properties:
	parent			- Inherited from NavigationTool.
	searching		- Inherited from NavigationTool.
	url				- Inherited from NavigationTool.
	useLinksFrame	- Inherited from NavigationTool.
 Methods:
	load()
	initialise()
	terminate()
	resize()
	textSearch()	- Inherited from NavigationTool.
	toString()

 ****************/

// *** Constructor for Glossary class ***
function Glossary(parent) {
	this.parent = ((typeof parent == "object") 
		&& (parent.constructor == HelpSystem)
		? parent : null);
	if (parent == null)
		return(null);
}

// *** Glossary inherits from NavigationTool ***
Glossary.prototype = new NavigationTool();
Glossary.prototype.constructor = Glossary;

// *** Glossary instance methods ***
	// Loads the glossary frameset.
Glossary.prototype.load = function glLoad() {
    /* If it is not already loaded, open the glossary
       frameset in the navigation frame. */

    if (typeof this.parent.navFrame != "undefined") {
		// Get the URL of the current navigation tool.
		var nav = this.parent.getNavigationUrl();

		// If different to that of this tool...
		if (!nav.isEqualIgnoreCase(this.url)) {
			// Load this tool.
			this.parent.navFrame.location.replace(
				unescape(this.parent.rootFolder + this.url));
		}
	}
}

Glossary.prototype.initialise = function glInitialise(obj) {
	if (!this.parent.navFrame.glossary)
		this.parent.navFrame.setup(obj);
}

Glossary.prototype.resize = function glResize(){};
Glossary.prototype.toString = function glToString(){return("Glossary")};
