
// global variables
var             wndCount = 0;
var             curXPos = 0;
var 			curYPos = 0;
var             wnd = null;
var             ht_loc = "";
var             curr_frames = new Array (5);
var             fs_file;
var				fs_applet;

// functions


function OpenWindow (file, width, height, x_pos, y_pos, wndname) {

	var             name, cc, temp = "";

	if (width == 0)
		width = 250;
	
	if (height == 0)
		height = 100;

	// adjust x and y to fit on screen
	if (SupportsDHTML ()) {
		if (x_pos < screen.width) {
			if (x_pos + width > (screen.width - 15))
				x_pos -= (x_pos + width) - (screen.width - 15)
			}
		else {  // assume 2nd monitor
			if (x_pos + width > ((screen.width * 2) - 15))
				x_pos -= (x_pos + width) - ((screen.width * 2) - 15)
			}

		if (y_pos + height > (screen.height - 60))
			y_pos -= (y_pos + height) - (screen.height - 60)
		}

	if (wndname == "") {
		name = file.substring (0, file.indexOf ("."));
		name += wndCount;
		wndCount++;
		}
	else
		name = wndname;
	
	// take all invalid chars out of name
	for (i = 0; i < name.length; i++) {
		cc = name.substring (i, i+1);
		if ((cc >= 'a' && cc <= 'z') || (cc >= 'A' && cc <= 'Z') && (cc >= '0' && cc <= '9'))
			temp += cc;
		else {
			val = parseInt (cc);
			if (val >= 0 || val <= 9)
				temp += cc;
			else
				temp += "_";
			}
		}

	name = temp;

	if (FullDHTML () == 1)
		wnd = open (file, name, 'width=' + width + ',height=' + height + ',left=' + x_pos + ',top=' + y_pos + ',resizable=1,scrollbars=1')              
	else if (PartialDHTML () == 1)
		wnd = open (file, name, 'width=' + width + ',height=' + height + ',screenX=' + x_pos + ',screenY=' + y_pos + ',resizable=1,scrollbars=1')               
	else
		wnd = open (file, name, 'width=' + width + ',height=' + height + ',resizable=1,scrollbars=1')

	if (wndname != "") {
		if (GetBrowser () == 0) // netscape
			wnd.focus ();
		else {
			if (FullDHTML ()) {
				test = wnd.location;    // this seems to make the window available
				wnd.focus ();
//                              setTimeout ("wnd.focus ()", 1000);
				}
			}
		}
	}


function SaveMousePos (evnt) {

	if (FullDHTML ()) {
		curXPos = window.event.screenX;
		curYPos = window.event.screenY;
		}
	else if (PartialDHTML ()) {
		curXPos = evnt.screenX;
		curYPos = evnt.screenY;
		}
	else
		curXPos = curYPos = 0;
	}


function ShowPopup (file, width, height) {
	var		name;

	name = "fhpopupwnd" + wndCount;
	wndCount++;
	OpenWindow (file, width, height, curXPos, curYPos, name);
	}


function ClosePopup () {

	var		name = window.name;

	if (name.indexOf ("fhpopupwnd") != -1)
		window.close ();
	}


function ShowSecWindow (file, wndname) {

	var     width, height;
	var             x_pos, y_pos;
	var             data, caption;
	var             i, str;

	// parse window data to caption, x, y, width, height
	data = windows[wndname];

	if (! data) {	// invalid window name
		window.open (file);
		return;
		}

	// first parse off caption
	i = data.indexOf (',');
	caption = data.substring (0, i);
	data = data.substring (i + 1);

	// parse off x pos
	i = data.indexOf (',');
	str = data.substring (0, i);
	x_pos = parseInt (str);
	data = data.substring (i + 1);

	// parse off y pos
	i = data.indexOf (',');
	str = data.substring (0, i);
	y_pos = parseInt (str);
	data = data.substring (i + 1);

	// parse off width
	i = data.indexOf (',');
	str = data.substring (0, i);
	width = parseInt (str);
	data = data.substring (i + 1);

	// height is left
	height = parseInt (data);

	OpenWindow (file, width, height, x_pos, y_pos, wndname);
	}


function ShowHideObject (val) {

	var             e;

	if (FullDHTML ()) {
		e = eval (val);
		if (e.style.visibility == "hidden")
			e.style.visibility = "visible";
		else
			e.style.visibility = "hidden";
		}
	else if (PartialDHTML ()) {
		e = eval ("document." + val);
		if (e.visibility == "hide")
			e.visibility = "show";
		else
			e.visibility = "hide";
		}
	}



function SetPopupDuration (val) {

	if (val == 0)
		val = 120000;
	else
		val = val * 1000;
	
	if (SupportsDHTML () == 0 && GetBrowser () == 1)        // ie3.0
		setTimeout ("self.close ()", val);              // 2 minutes by default
	}


function SaveSubFrames () {

	for (i = 0; i < 5; i++) {
		if (parent.frames['subframe'].frames.length > i) {
			if (parent.frames['subframe'].frames[i].document) {
				curr_frames[i] = parent.frames['subframe'].frames[i].name;
				}
			else
				curr_frames[i] = "";
			}
		else
			curr_frames[i] = "";
		}
	}


function FindFrame (name) {

	for (i = 0; i < curr_frames.length; i++)  {
		if (curr_frames[i] == name)
			return i;
		}

	return -1;
	}


function ShowNavPane () {

	SaveSubFrames ();

	if (IsHidden ()) {
		if (IsOldBrowser ())
			return 0;

		ToggleNavPane ();
		return 1;
		}

	return 1;
	}


function IsHidden () {

	if (FindFrame ("hlptopics") == -1)
		return 1;
	return 0;
	}


function Back () {
	
	var                     num;

	SaveSubFrames ();

	num = FindFrame ("content");
	if (num != -1)                  
		parent.frames['subframe'].frames[num].history.back ();
	}


function Forward () {
	
	var                     num;

	SaveSubFrames ();

	num = FindFrame ("content");
	if (num != -1)
		parent.frames['subframe'].frames[num].history.forward ();
	}


function SetFile (def_file, in_sub_fs, parent_set) {

	var             index;
	var             loc;
	var             temp;

	loc = document.location.href;
	index = loc.indexOf ("?");

	if (index == -1) {
		if (IsIE ()) {
			index = loc.indexOf ("%3F");
			if (index != -1)
				index = index + 3;
			}
		}
	else
		index = index + 1;

	if (index == -1)
		fs_file = "'" + def_file + "'";
	else {
		if (in_sub_fs) {
			fs_file = loc.substring (index, loc.length);
			fs_file = "'" + fs_file + "'";
	
			temp = fs_file;
			temp = temp.toLowerCase ();
			parent_set = parent_set.toLowerCase ();

			if (temp.indexOf (parent_set) != -1)
				fs_file = "'" + def_file + "'";
			}
		else {
			fs_file = loc.substring (index, loc.length);

			if (fs_file.indexOf ("ctx=") != -1) {
				index = fs_file.indexOf ("=");
				fs_file = fs_file.substring (index + 1);

				index = fs_file.indexOf (".");
				if (index != -1)
					fs_file = fs_file.substring (0, index);
				
				if (CtxNumbers["ctx_" + fs_file] == null)
					fs_file = def_file;
				else
					fs_file = CtxNumbers["ctx_" + fs_file];
				}

			if (def_file.indexOf ("subfrm.htm") != -1) {
				if (IsIE4 ())
					def_file = MakeFullPath (def_file);
				fs_file = "'" + def_file + "?" + fs_file + "'";
				}
			else
				fs_file = "'" + fs_file + "'";
			}
		}
	}


function ShowFileInFrames (frameset, file, frame_name, window_name) {
	
	var			loc;
	var			top_wnd;

	frameset = MakeFullPath (frameset);	// handle relative paths

	if (file != "") {
		loc = frameset + "?" + file;
		}
	else
		loc = frameset;

	if (window_name != "") {
		ShowSecWindow (loc, window_name);
		return;
		}

	if (frame_name == "")
		frame_name = "_top";	// default of blank here means replace all frames from top

	IHShowTopic (loc, frame_name);
	}


function MakeFullPath (file) {

	var		path;
	var 		i, j;
	var		str1;
	var		str2;
	var		cc;

	i = file.indexOf (":");
	if (i != -1) {	// already looks like a full path
		if (i == 1) {	// not a moniker, looks like a file path
			if (IsNS4 ())
				path = "file:///" + file;	
			else
				path = "file://" + file;	
			}
		else
			path = file;

		return path;
		}

	if (FullDHTML () == 1 || IsNS4 ())
		str1 = document.location.href;
	else
		str1 = document.location;

	// look for ? - strip
	i = str1.indexOf ("?");
	if (i != -1)
		str1 = str1.substring (0, i);

	i = str1.lastIndexOf ("/");
	if (i == -1)	// try with \
		i = str1.lastIndexOf ("\\");

	if (i != -1)
		path = str1.substring (0, i+1);

	// strip file:///
	i = -1;
	i = path.indexOf ("file:///");
	if (i >= 0)
		path = path.substring (i+8);
	else {	// look for file://
		i = path.indexOf ("file://");
		if (i >= 0)
			path = path.substring (i+7);
		else { // look for file:/
			i = path.indexOf ("file:/");
			if (i >= 0)
				path = path.substring (i+6);
			}
		}

	// strip off file name
	i = -1;
	i = path.lastIndexOf ("/");
	
	if (i == -1)
		i = path.lastIndexOf ("\\");

	if (i >= 0) 
		path = path.substring (0, i);

	// look for .. in file, for each set remove a directory
	i = file.indexOf ("..");
	while (i != -1) {
		file = file.substring (2);	// ../

		j = path.lastIndexOf ("/");		
		if (j == -1)
			j = path.lastIndexOf ("\\");

		if (j >= 0)
			path = path.substring (0, j);

		i = file.indexOf ("..");
		}

	// make sure has trailing / or \\
	cc = path.substring (path.length - 1, path.length);

	if (cc != '\\' && cc != '/')
		path += "/";

	path += file;

	i = path.indexOf (":");
	if (i <= 1 && path.indexOf ("file:") == -1) {
		if (IsNS4 ())
			path = "file:///" + path;	
		else
			path = "file://" + path;	
		}

	return path;
	}

function GetFilename (path) {

	var 	i;
	var		file;

	// first strip any quotes
	if (path.charAt (0) == "'")
		path = path.substring (1);
	if (path.charAt (path.length - 1) == "'")
		path = path.substring (0, path.length - 1);

	i = path.lastIndexOf ("/");
	if (i == -1)	// try with \
		i = path.lastIndexOf ("\\");

	if (i != -1)
		file = path.substring (i+1);
	else
		file = path;

	return file;
	}


/****************************************************************************************
	 The following functions are used by custom built templates
****************************************************************************************/

function SetFile2 (def_file, def_applet) {

	var             index, index2, save;
	var             loc;
	var             temp;
	var		do_resize = true;

	loc = document.location.href;
	
	index = loc.indexOf ("~noresize~");
	if (index != -1) {
		do_resize = false;
		loc = loc.substring (0, index);

		// strip ? or %3F
		index = loc.lastIndexOf ("?");
		if (index != -1)
			loc = loc.substring (0, index);
		else if (IsIE ()) {
			index = loc.lastIndexOf ("%3F");
			if (index != -1)
				loc = loc.substring (0, index);
			}
		}

	index = loc.indexOf ("?");

	if (index == -1) {
		if (IsIE ()) {
			index = loc.indexOf ("%3F");
			if (index != -1)
				index = index + 3;
			}
		}
	else
		index = index + 1;

	if (index == -1) {
		fs_file = "'" + def_file + "'";
		fs_applet = "'" + def_applet + "'";
		}
	else {
		fs_file = loc.substring (index, loc.length);
		
		// look for applet string
		index2 = fs_file.indexOf ("?");
		save = index2;
		if (index2 == -1) {
			if (IsIE ()) {
				index2 = fs_file.indexOf ("%3F");
				save = index2;
				if (index2 != -1)
					index2 = index2 + 3;
				}
			}
		else
			index2 = index2 + 1;

		if (index2 == -1)
			fs_applet = def_applet;
		else {
			fs_applet = fs_file.substring (index2, fs_file.length);
			fs_file = fs_file.substring (0, save);
			}

		// evaluate fs_file
		if (fs_file.indexOf ("ctx=") != -1) {
			index = fs_file.indexOf ("=");
			fs_file = fs_file.substring (index + 1);

			index = fs_file.indexOf (".");
			if (index != -1)
				fs_file = fs_file.substring (0, index);
			
			if (CtxNumbers["ctx_" + fs_file] == null)
				fs_file = def_file;
			else
				fs_file = CtxNumbers["ctx_" + fs_file];
			}

		fs_file = "'" + fs_file + "'";
		fs_applet = "'" + fs_applet + "'";
		}

	return do_resize;
	}


function ShowFileAndApplet (frameset, file, applet) {
	
	var			loc;

	if (IsIE4 () || IsNS4 ())
		frameset = MakeFullPath (frameset);


	file = GetFilename (file);	// strip full path
	applet = GetFilename (applet);	// strip full path

	loc = frameset + "?" + file + "?" + applet + "?~noresize~";
	top.location.href = loc;
	}


function FindFrame2 (name, frames) {

	var 			frame = null;
	var				collection;

	if (frames == null)
		collection = top.frames;
	else
		collection = frames;

	// go through frame collection, recursing until frame_name is found
	for (i = 0; i < collection.length; i++) {
		if (collection[i].frames.length) {	// recurse
			frame = FindFrame2 (name, collection[i].frames);
			if (frame !=null)
				break;
			}
		else if (collection[i].name == name) {
			frame = collection[i];
			break;
			}
		}

	return frame;
	}


function GetContentLocation (frame_name) {

	var			loc = "";
	var			frame;

	frame = FindFrame2 (frame_name, top.frames);	
	if (frame != null)
		loc = frame.document.location.href;

	return loc;
	}


function IsHidden2 (frame_name) {

	if (FindFrame2 (frame_name, null) == null)
		return 1;
	return 0;
	}


function Back2 (frame_name) {
	
	var			frame;


	frame = FindFrame2 (frame_name);
	if (frame != null)                  
		frame.history.back ();
	}


function Forward2 (frame_name) {
	
	var			frame;


	frame = FindFrame2 (frame_name);
	if (frame != null)                  
		frame.history.forward ();
	}


function GetSA () {
	return fs_applet;
	}

function GetSavedApplet () {
	return top.GetSA ();
	}

function IHShowTopic (file, frame_name) {

	var		frame;

	if (frame_name == "" || frame_name == "_self")
		location.href = file;
	else if (frame_name == "_parent")
		parent.location.href = file;
	else if (frame_name == "_top")
		top.location.href = file;
	else if (frame_name == "_blank")
		window.open (file);
	else {
		frame = FindFrame2 (frame_name, null);
		if (frame)
			frame.location.href = file;
		else
			window.open (file);
		}
	}

function RepositionWindow (x, y, width, height) {

	window.moveTo (x, y);
	window.resizeTo (width, height);
	}

