//------ GO BACK TO PREVIOUS PAGE --------//

function goBack(){
	// if variable has been set
	if(lastpage && lastpage != "") {
		self.location.replace(lastpage);
	} else {
		// otherwise go back to previous page
		history.go(-1);
	}
}

//------ DISPLAY PAGE TITLE IN STATUS BAR --------//

function doStatus(){
	window.status = document.title;
	return true;
}

//---------------- IMAGE PRELOAD ----------------//

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

//---------------- PRINT FUNCTION ----------------//

function printPage(frameName) {
	if ((navigator.userAgent.indexOf("Mac") != -1) && (navigator.appName.indexOf("Microsoft") != -1)) {
		//This is the Mac IE window
		alert("Sorry, your browser doesn't support this feature. Please use the Print option from the browser's File menu");
	} else {
		if(!frameName){
			//These are the other windows (pc ie and ns, mac ns)
			self.print();
		} else {
			var frameToPrint = eval("parent." + frameName);
			frameToPrint.focus();
			
			// check if it's the bookcase
			// content frame will have frames within if it is
			if(frameToPrint.frames.length > 0){
				frameToPrint = eval("parent." + frameName + ".bkcontent");
				//alert("bookcase") 
			}
			
			frameToPrint.focus();
			frameToPrint.print();		
		}
	}
}

//---------------- GLOSSARY FUNCTIONS ----------------//

var glossary = new Array();
var g = "";
var path = "";

function GlossaryItem(key,d) {
	this.key = key
	this.definition = d
}

function addGlossaryItem(k,d) {	
	glossary[glossary.length] = new GlossaryItem(k,d)
}

function doGlossary(k, levels) {
	
	var path = "";
	for(i=0; i < levels; i++){
		path += "../"
	}

	var url = path + "glossary/gloss.htm?" + k;
	var w = checkWidth(400);
	var h = checkHeight(220);
	var name = "glossary";
	
	var features = "width=" + w + ",height=" + h + ",scrollbars,resizable,menubar,status" + getCenterStage(w,h);
	openCenteredWin(url,name,features);
}

// this function is called by the glossary frameset when pops up (toolbox/resources/glossary/definefs.htm)
function showDefinition(){

var urlString = document.location.toString();
var term = urlString.split("?")[1];
var k = unescape(term);

var found = false;

	for(var i = 0 ; i < glossary.length ; i++) {
		if(glossary[i].key == k){
			found = true
			g += '<h2>' + glossary[i].key + '</h2>\n' 
			+ glossary[i].definition + '\n'
			break
		}
	}
	
	if(!found){
		g += '<p>Sorry, couldn\'t locate a definition for <b>' + k + '</b>.</p>'
	}
	
	document.write(g);
	document.close();
}

// Displays all definitions for specified letters
// Eg. showAllDefinitions("A,B,C,D"); will display all definitions for these letters only
function showAllDefinitions(letters){

	var displayLetters = new Array();
	displayLetters = letters.split(",");
	numLetters = displayLetters.length;
	
	var headLinks = "<div class='center'>|"
	for(var i = 0 ; i < numLetters; i++) {
		headLinks += '&nbsp;&nbsp;&nbsp;<a href="#' + displayLetters[i] + '" title="Go to items beginning with ' + displayLetters[i] + '">' + displayLetters[i] + '</a>';
	}
	headLinks += '&nbsp;&nbsp;&nbsp;|</div>'
	document.write(headLinks);
	
	x = 0;
	while(x < numLetters){
		var found = false;
		
		for(var i = 0 ; i < glossary.length ; i++) {
			if(glossary[i].key == "$section$" && glossary[i].definition == displayLetters[x]){
				found = true
				g += '<a name="' + glossary[i].definition + '" id="' + glossary[i].definition + '"></a>\n'
				g += '<h1 class="glosshead">' + glossary[i].definition + '</h1>\n'
				g += '<div class="defs">\n'
				pos = i+1;
				while(glossary[pos].key != "$section$"){
					if(glossary[pos].key != ""){
						g += '<h3 class="glossWord">' + glossary[pos].key + '</h3>' + glossary[pos].definition + '\n\n'
					} else {
						g += '<p>There are no definitions for the letter '  + glossary[i].definition +  '</p>\n'
					}
					pos++;
				}
				//g += '<p class="top"><a href="javascript:scroll(0,0)" title="Go to top">top</a></p>\n';
				g += '</div>';
				g += '<p align="right">\n'
				g += '<a href="javascript:void(0);" onclick="javascript:scroll(0,0);" onmouseout="changeImages(\'sm_top' + i + '\', \'../images/sm_top.gif\'); return true;" onmouseover="changeImages(\'sm_top' + i + '\', \'../images/sm_top_on.gif\'); return true;"><img src="../images/sm_top.gif" border="0" width="41" height="13" alt="Go to top" name="sm_top' + i + '" id="sm_top' + i + '" /></a>\n'
				g += '</p>\n'
			}
		}
		x++;
	}

	//if(!found){
	//	g += '<p>Sorry, couldn\'t locate a definition for <b>' + k + '</b>.</p>'
	//}

	return g;
}
