/////////////////////////////////////////////////////////////////////////
// general and navigation library functions for CMS
/*

	author: dmw  on behalf of c2 technologies
	revision history:
		feb 2003: prototype
		jly 2003: initial version (cms risk mgmt)
		sep 2003: cms pilot version
		dec 2003: post pilot version

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

// loaded in Begin_here.htm and register.htm; referenced as opener.function() from course pages.
// NOT included in individual course pages (one time download hit)
// (contrast with cms_page.js)

if (DEBUG)	{alert("dev: loading cms_base.js");	}

// need to know whether we are running on a webserver 
//		protocool http://webserver/mod3/m3l1s010.htm
// or locally
//		file:///J:/mod3/m3l1s010.htm
// want to avoid making pseudo-URLS like
//	J:\mod3\m3l1s010.htm
// because they don't work consistently (mixed /\ problem?)

serverMode = document.location.protocol;
if (-1 != document.location.protocol.indexOf("file"))
	{
		if (DEBUG){alert("running locally!");}
	}
else
	{
		if (DEBUG){alert("running on webserver");}
	}


serverMode = document.location.protocol;

serverPath = document.location.pathname;

// any instances of backslash (windoze slash) need to be converted to fwd slash
// so we get a canonical path
if	(serverMode == "file:")
	{
	// this DOES NOT WORK! leaves \'s in string!
	//if (-1 != serverPath.indexOf("\\"))
	//	{
	//	serverPath = serverPath.replace("\\","/");
	//	alert(serverPath);
	//	}
	// time to use brute force!
	var brute = "";			// new string to build
	var unpleasant = "";	// single char extraced from old string
	for (var force=0;force<serverPath.length ;force++ )
		{
		unpleasant = serverPath.charAt(force);
		if (unpleasant == "\\")
			{
			brute = brute + "/";
			//alert("got one!");
			}
		else
			{
			brute = brute + unpleasant;
			//alert("claims not to be bkslsh:" + unpleasant);
			}
		}
	serverPath = brute;
	}
else
	{
	// keep track of the host along with the serverPath
	serverPath = document.location.host + serverPath;
	}
// now serverPath should be well-formed, but it includes a file name.
// get rid of that.
serverPath = serverPath.substring(0,serverPath.lastIndexOf("/")+1);

//	serverMode needs to have // appended to it so that
//		serverMode + serverPath
//	is a valid reference
serverMode += "//";

if (DEBUG){alert('"' + serverMode + '" "' + serverPath + '"');}

Root = serverMode + serverPath;

// figure out Root
//alert(document.location.href)




//Root = document.location.href.substring(0,document.location.href.lastIndexOf("/")+1);
//Root_path = document.location.pathname;
//Root_trunc1 = Root_path.substring(0,Root_path.lastIndexOf("/")+1);
//Root_trunc2 = Root_path.substring(0,Root_path.lastIndexOf("\\")+1);
/*
if (DEBUG)
	{
	//alert(" location href: " +
	document.location.href + 
	" location.hash " + 
	document.location.hash +
	" host: " +
	document.location.host +
	" pathname " +
	document.location.pathname +
	" port " +
	document.location.port +
	" protocol " +
	document.location.protocol +
	" search " +
	document.location.search +
	" \r\r trunc1: " + 
	Root_trunc1 +
	" \r\r trunc2: " + 
	Root_trunc2 
		);
	}
if (Root_trunc1.length > Root_trunc2.length)
	{
	Root_path = document.location.protocol + Root_trunc1;
	}
else
	{
	Root_path = document.location.protocol + Root_trunc2;
	}


if (DEBUG)
	{
	//alert("set Root_path " + Root_path);
	}
*/ 

// setup two dimensional array for risksheet table
for (var i=1;i<15 ;i++ )	// 15 rows
	{
	risksheet[i] = new Array(5);	// 5 columns each
	}
// initialize risksheet to empty
risksheetMT();



///////////////////////////////////////////////////////////////////////////////
function underconstruction(which)
{
var str;
str =  "Under Construction!\n" + which + " is not available for this demonstration.";
alert(str); //"Under Construction");
}
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
function pageInit()
// call this function onLoad for each course page.
// sets up menu behavior, Next/Back behavior, Screen X of Y text, etc.
// pass extra argument if called from seperate coursemap window
// 
{
	var mapFlag = false;
	var nextFunc = "";	// either navNext() or checkNext(), depending
	if (arguments.length>0)
		{
		mapFlag = true;
		if (DEBUG)			{			alert("pageinit called from coursemap");			}
		}

	//alert("courseWin " + courseWin );
	//if (DEBUG)
	//	{
	//	alert("in pageInit for NavPosition " + NavPosition + "," + courseWin.location.href);
	//	}
	//if (DEBUG)
	//	{
	//	alert("autodlink mode = " + AutoDLink);
	//	}
	
	// menu setup -- there are four of these, with several possible values.
	// easiest is _notavail_.
	
	var prepend  = "";
	// are we in a module? one deeper.
	if (-1 != courseWin.location.href.indexOf("mod"))
		{
		prepend = prepend + "../"
		}
	// are we in a dlink? one deeper.
	//if (-1 != courseWin.location.href.indexOf("dlink"))
	//	{
	//	prepend = prepend + "../"
	//	}

	if (typeof courseWin.question != "undefined" && AutoDLink)
		{
		if (-1 == courseWin.location.href.indexOf("dlink"))
			{
			// automatically present dlink if question, not IN dlink, and if autoDLink
			//alert("oh, i WISH i knew what to do! " + courseWin.dlinktarg);
			courseWin.location.href = courseWin.dlinktarg;
			}
		}

	var ref,myAlt,myTab,txt;
	for (var i=1;i<=4 ;i++ )
		{
		// set document object reference
		//alert("courseWin.document.all.mod" + i);
		//ref = eval("courseWin.document.all.mod" + i);
		if (mapFlag)
			{
			ref = eval("coursemapWin.document.all.mod" + i);
			}
		else	
			{
			ref = eval("courseWin.document.all.mod" + i);
			}
		// set alt tag + tab order for each
		switch (i)
			{
			case 1:
				myAlt = "Module 1: Introduction to Risk Management";
				myTab = "60";
				break;
			case 2:
				myAlt = "Module 2: Risk Management Benefits";
				myTab = "70";
				break;
			case 3:
				myAlt = "Module 3: Risk Management Process";
				myTab = "80";
				break;
			case 4:
				myAlt = "Module 4: Course Review and Knowledge Check";
				myTab = "90";
				break;
			}

		// cases are
		// we are in the module (i == opener.ModIn)
		// it's already viewed (and available to review)
		// it's available to view
		// it's not available
		
		// modified post-pilot dmw: only two states, we are in the module, or we are not in the module.

		if	(i == ModIn)
			{
				// it's the module we're IN, show static graphic in the hit state
			txt = '<img src="' + prepend + 'Images/Interface/bullet_hit_'+ i +'.jpg" alt="' + myAlt + '" height="20" width="61" border="0">';
			}
		else if (MenuState[i] == _notavail_)
			{
				// set innerHTML of this menu item to static (not available) graphic.
			txt = '<img src="' + prepend + 'Images/Interface/bullet_avail_'+ i +'.jpg" alt="' + myAlt + '" height="20" width="61" border="0">';
			}
		else //if (MenuState[i] == _avail_)
			{
				// set innerHTML of this menu item to rollover between available state and highlight state
					txt = '<a href="javascript:opener.navMod(' + i + ');" tabindex="' + myTab + '"  onMouseOut="MM_swapImgRestore()" ';
					txt += "onMouseOver=\"MM_swapImage('ImgMod" + i +"','','" + prepend + "Images/Interface/bullet_over_"+ i +".jpg',1)\">";
					txt += '<img name="ImgMod' + i + '" src="' + prepend + 'Images/Interface/bullet_avail_'+ i +'.jpg" alt="' + myAlt + '" height="20" width="61" border="0"></a>';
			}
		//else if (MenuState[i] == _viewed_)
		//	{
		//		// set innerHTML of this menu item to rollover between viewed state and highlight state
		//		txt = '<a href="javascript:opener.navMod(' + i + ');" tabindex="' + myTab + '"  onMouseOut="MM_swapImgRestore()" ';
		//		txt += "onMouseOver=\"MM_swapImage('ImgMod"+ i +"','','" + prepend + "Images/Interface/bullet_over_" + i + ".jpg',1)\">";
		//		txt += '<img name="ImgMod' + i + '" src="' + prepend + 'Images/Interface/bullet_avail_'+ i +'.jpg" alt="' + myAlt + '" height="20" width="61" border="0"></a>';
		//	}
		//if (DEBUG)
		//	{
		//	alert(txt + ref);
		//	}
		ref.innerHTML = txt;
		} // end for

	// now set up next and back -- if the pointer value is -1, the button is disabled.  otherwise build a rollover for it.
	// check to make sure that the element is defined in the page -- because for coursemap, etc. it's not!

	//next and back are the LAST items in the tab index so that students can use Ctrl-End to skip repetetive menus

	if (BackPointer[NavPosition] != -1)
		{
//	    txt = "<a href=\"#\" onClick=\"opener.navBack();\" tabindex=20  onMouseOut=\"MM_swapImgRestore()\"" + 
//			"onMouseOver=\"MM_swapImage('ImgBack','','" + prepend + "Images/Interface/back_hit.jpg',1)\"><img name=\"ImgBack\"" +
//			"src=\"" + prepend + "Images/Interface/back_off.jpg\" alt=\"Back\" border=\"0\" width=\"70\" height=\"40\"></a>";

		txt = "<a href=\"javascript:opener.navBack();\" tabindex=\"199\"  onMouseOut=\"MM_swapImgRestore()\"" + 
			"onMouseOver=\"MM_swapImage('ImgBack','','" + prepend + "Images/Interface/back_hit.jpg',1)\"><img name=\"ImgBack\"" +
			"src=\"" + prepend + "Images/Interface/back_off.jpg\" alt=\"Back\" border=\"0\" width=\"70\" height=\"29\"></a>";
		if (courseWin.document.all.backbutton)
			{
			//if (DEBUG)
			//	{
			//	alert("setting BACK to " + txt);
			//	}
			courseWin.document.all.backbutton.innerHTML = txt;
			}
		}
	if (NextPointer[NavPosition] != -1)
		{
//	    txt = "<a href=\"#\" onClick=\"opener.navNext();\" tabindex=105  onMouseOut=\"MM_swapImgRestore()\"" +
//			"onMouseOver=\"MM_swapImage('ImgNext','','" + prepend + "Images/Interface/next_hit.jpg',1)\"><img name=\"ImgNext\"" +
//			"src=\"" + prepend + "Images/Interface/next_off.jpg\" alt=\"Next\" border=\"0\" width=\"70\" height=\"40\"></a>";
		if (typeof courseWin.question != "undefined")
			{
			nextFunc = "checkNext();";
			}
		else
			{
			nextFunc = "opener.navNext();";
			}

	    txt = "<a href=\"javascript:" + nextFunc + "\" tabindex=\"200\"  onMouseOut=\"MM_swapImgRestore()\"" +
			"onMouseOver=\"MM_swapImage('ImgNext','','" + prepend + "Images/Interface/next_hit.jpg',1)\"><img name=\"ImgNext\"" +
			"src=\"" + prepend + "Images/Interface/next_off.jpg\" alt=\"Next\" border=\"0\" width=\"70\" height=\"29\"></a>";
		if (courseWin.document.all.nextbutton)
			{	
			if (DEBUG)
				{
				//alert("setting NEXT to " + txt);
				}
			courseWin.document.all.nextbutton.innerHTML = txt;
			}
		}
	// set page number (if element is defined in page, i.e., not for course map)
		//alert("numberofpages and navpos:" + NavPosition);
		if (NumberOfPages[NavPosition].indexOf("of")!=-1)
			{
			if (courseWin.document.all.pgNumber)
				{
				courseWin.document.all.pgNumber.innerText = "Screen " + NumberOfPages[NavPosition];
				}
			}

	// set show text button iff "audio" property is defined in courseWin
	if (typeof courseWin.audio != "undefined")
		{
		//alert("audio has type: " + typeof courseWin.audio);
		txt = "<a href=\"javascript:opener.navShowTxt();\" tabindex=\"185\"  onMouseOut=\"MM_swapImgRestore()\"" +
			"onMouseOver=\"MM_swapImage('ImgShowTxt','','" + prepend + "Images/Interface/showtext_on.jpg',1)\"><img name=\"ImgShowTxt\"" +
			" src=\"" + prepend + "Images/Interface/showtext_off.jpg\" alt=\"Show Text\" border=\"0\" width=\"90\" height=\"29\"></a>";
			if (courseWin.document.all.showtextbutton)
				{
				courseWin.document.all.showtextbutton.innerHTML = txt;
				}
		}

	// set dlink button iff "dlink" property is defined in courseWin
	if (typeof courseWin.dlink != "undefined")
		{
		//alert("dlink has type: " + typeof courseWin.dlink);
		txt = "<a href=\"javascript:opener.navDLink();\" tabindex=\"190\"  onMouseOut=\"MM_swapImgRestore()\"" +
			"onMouseOver=\"MM_swapImage('ImgDLink','','" + prepend + "Images/Interface/dlink_on.jpg',1)\"><img name=\"ImgDLink\"" +
			" src=\"" + prepend + "Images/Interface/dlink_off.jpg\" alt=\"Accessible version of this interaction\" border=\"0\" width=\"68\" height=\"29\"></a>";
			if (courseWin.document.all.dlinkbutton)
				{
				//alert(txt);
				courseWin.document.all.dlinkbutton.innerHTML = txt;
				}
			else
			{
			//	alert("no placeholder dlinkbutton in document.");
			}
		}
	
	//else
	//	{
	//	alert("audio has type: " + typeof courseWin.audio);
	//	}

}


////////////////////////////////////////////////////////////////////////////////////
function addtoQuesTable()
////////////////////////////////////////////////////////////////////////////////////
// mark current question answered for this session
////////////////////////////////////////////////////////////////////////////////////
{
	//alert("pushing " + CoursePage[NavPosition] + " into answered array length " + quesTable.length);
	
	// this fails sometimes!?
	//quesTable.push(CoursePage[NavPosition]);
	
	var i = quesTable.length;
	quesTable[i] = CoursePage[NavPosition];

	//alert("table has length " + quesTable.length);

}


////////////////////////////////////////////////////////////////////////////////////
function risksheetMT()
////////////////////////////////////////////////////////////////////////////////////
// remove any values from the risk sheet and ensure all vars are initialized
////////////////////////////////////////////////////////////////////////////////////
{
risksheet[1][1] = "";
risksheet[1][2] = "";
risksheet[2][1] = "";
risksheet[2][2] = "";
risksheet[3][1] = "";
risksheet[4][1] = "";
risksheet[5][1] = "";
risksheet[6][1] = "";
risksheet[7][1] = "";
risksheet[7][2] = "";
risksheet[8][1] = "";
risksheet[8][2] = "";
risksheet[8][3] = "";
//
//placeholder - to be used for circlular .gif images
//
risksheet[10][1] = "";
risksheet[11][1] = "";
risksheet[12][1] = "";
}
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function risksheetSET(maxRisk)
////////////////////////////////////////////////////////////////////////////////////
// remove any values from the risk sheet and ensure all vars are initialized
// then loop through values up to maxRisk and set them to the standard values.
// this is so the risk sheet displays correctly when returning from a stored
// bookmark, or when backing up through the course
////////////////////////////////////////////////////////////////////////////////////
{
// clear values
risksheetMT();
// now set standard values
if (arguments.length>1) // circles arrows and other stuff of questionable value
	{
	if (arguments[1]=="circles")
		{
		risksheet[10][1] = arguments[1];
		// extra x and y offsets
		risksheet[11][1] = arguments[2];
		risksheet[12][1] = arguments[3];
		}
	}
else // none. good!
	{
	risksheet[10][1] = "";
	}
for (var i=1; i<=maxRisk; i++)
	{
	if (i == 1) 
		{	risksheet[1][1] = "John Taylor"; }
	if (i == 2) 
		{	risksheet[2][1] = "1-A"; }
	if (i == 3) 
		{	risksheet[3][1] = "If goals and objectives are not communicated to project team members, then the components will not be able to complete their assigned duties and the project will be behind schedule."; }
	if (i == 4) 
		{	risksheet[4][1] = "Staff members are confused about their roles and responsibilities. "; }
	if (i == 5) 
		{	risksheet[1][2] = "1"; }
	if (i == 6) 
		{	risksheet[2][2] = "Internal, Non-Technical Management"; }
	if (i == 7)
		// there are two different values for element [5][1] -- just overwrite the first value with the second.
		{	risksheet[5][1] = "Hold Monthly Meetings with project team"; }
	if (i == 8)
		// there are two different values for element [5][1] -- just overwrite the first value with the second.
		{
		risksheet[5][1] = "1. Hold monthly meetings with project team.\r" +
		   "2. Write a \"Roles and Responsibilities Plan\" for team members.\r" +
		    "3. Track monthly progress on project schedule.\r"; 
		}
	if (i == 9)
		{	risksheet[6][1] = "If the schedule slips by six days, then the project lead will bring in contractors to help get the project back on schedule."; }
//Monthly meetings occurring as scheduled. Project members are aware of duties. Project is ahead of schedule.
	if (i == 10)

		{	risksheet[7][1] = "Monthly meetings occurring as scheduled. Project members are aware of duties. Project is ahead of schedule."; }
//If the schedule slips by six days, then the project lead will bring in contractors to help get the project back on schedule. 
	if (i == 11)
		{	risksheet[7][2] = "5/5/2003"; }
	if (i == 12)
		{	risksheet[8][1] = "Bob Richards"; }
	if (i == 13)
		{	risksheet[8][2] = "6/10/2003"; }
	if (i == 14)
		{	risksheet[8][3] = "Project near completion and on schedule"; }
	}

}
////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////
function navNext()
// go next
{

	//if (DEBUG)
	//	{
		//alert("hullo navNext()");
		//alert("NextPointer at " + NavPosition + " is " + NextPointer[NavPosition]);
	//	}
	// check bounds!
	if (NavPosition < MaxPage)
		{
		if (NextPointer[NavPosition]!=0)
			{
			NavPosition = NextPointer[NavPosition];
			}
		else
			{
			NavPosition++;
			}
		//if (DEBUG)
		//	{
		//	alert(CoursePage[NavPosition]);
		//	}
		navTo(CoursePage[NavPosition]);
		}
}
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function navBack()
// go back
{
	//if (DEBUG)
	//	{
		//alert("hullo navBack()");
		//alert("BackPointer at " + NavPosition + " is " + BackPointer[NavPosition]);
	//	}
	// check bounds!
	if (NavPosition > 0)
	{
		if (BackPointer[NavPosition]!=0)
			{
			//alert("adjusting NavPosition to " + BackPointer[NavPosition]);
			NavPosition = BackPointer[NavPosition];
			}
		else
			{
			NavPosition--;
			}
		//if (DEBUG)
		//	{
		//	alert(CoursePage[NavPosition]);
		//	}
		navTo(CoursePage[NavPosition]);
	}
}
////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////
function navCourseMap()
{
	//alert("in coursemap...");
	if (ModIn<1)
		{
		courseWin.location.href =  "CourseMap.htm";
		}
	else
		{
		courseWin.location.href =  "../CourseMap.htm";
		}
	
	//var result = winCheck(coursemapWin);
	//alert(result);
	//if (result != "open")
	//	{
	//	coursemapWin = makeNewWindow("../CourseMap.htm",800,600,"coursemapWin");
	//	}
	//coursemapWin.focus();

	
	//navTo('CourseMap');
}
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function navResume()
{
	destination = bookmarkLocation;
    if (0 == destination.indexOf("mod"))
		{
		var gomod = parseInt(destination.charAt(3));
		ModIn = gomod;
		}

	if (-1 == destination.indexOf(".htm")) 	{destination += ".htm";}
	courseWin.location.href = destination;
}
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function navGlossary()
// if there is an argument, display term.
// else, just pop glossary up.
{
	var prepend  = "";
	var tag=""
	if (arguments.length>0)
		{
		tag = "#" + arguments[0];
		}
	// are we in a module? one deeper.
	// NO! using fully qualified path!
	//if (-1 != courseWin.location.href.indexOf("mod"))
	//	{
	//	prepend = prepend + "../";
	//	}
	// are we in a dlink? one deeper.
	//if (-1 != courseWin.location.href.indexOf("dlink"))
	//	{
	//	prepend = prepend + "../"
	//	}

	
	// makeNewWindow(prepend+'glossary.htm'+tag,600,400);

	// check window status
	var result = winCheck(glossWin);
	//alert(result);
	if (result != "open")
		{
		if (ModIn<1)
			{
			glossWin = makeNewWindow( prepend + 'glossary.htm'+tag,600,400,"glossWin","noroot");
			}
		else
			{
			glossWin = makeNewWindow( prepend + 'glossary.htm'+tag,600,400,"glossWin");
			}

		//glossWin = makeNewWindow( prepend + 'glossary.htm'+tag,600,400,"glossWin");
		}
	glossWin.focus();
}

////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function navShowTxt()
	//makeNewWindow('m3l1s010.text.htm',600,400);
{
	var which = "";
	// put "_text" before the "."
	//alert(courseWin.document.location.href);
	which = courseWin.document.location.href.substring(0,courseWin.document.location.href.lastIndexOf("."))
		+ ".text.htm";
	//alert(which);
	// check window status
	var result = winCheck(textWin);
	//alert(result);
	if (result != "open")
		{
		textWin = makeNewWindow(which,600,432,"textWin","noroot");
		}
	else
		{
		textWin.location.href = which;
		}
	textWin.focus();
}

////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function navHelp()
{
	// makeNewWindow('help.htm',600,400,);
	// check window status
	var result = winCheck(helpWin);
	var prepend = "";
	// NO! using fully qualified path!
	//if (-1 != courseWin.location.href.indexOf("mod"))
	//	{
	//	prepend = prepend + "../";
	//	}
	if (result != "open")
		{
		if (ModIn<1)
			{
			helpWin = makeNewWindow(prepend +  'help.htm',600,400,"helpWin","noroot");
			}
		else
			{
			helpWin = makeNewWindow(prepend +  'help.htm',600,400,"helpWin");
			}

		//helpWin = makeNewWindow(prepend +  'help.htm',600,400,"helpWin");
		}
	helpWin.focus();
}
///////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function navCertificate()
{
	// makeNewWindow('help.htm',600,400,);
	// check window status
	var result = winCheck(certificateWin);
	var prepend = "";
	// NO! using fully qualified path!
	//if (-1 != courseWin.location.href.indexOf("mod"))
	//	{
	//	prepend = prepend + "../";
	//	}
	if (result != "open")
		{
		if (ModIn<1) {
			certificateWin = makeNewWindow(prepend +  'certificate.htm',600,400,"certificateWin","noroot",",titlebar,menubar,toolbar,resizable,scrollbars");
			}
		else {
			certificateWin = makeNewWindow(prepend +  'certificate.htm',600,400,"certificateWin","rootit",",titlebar,menubar,toolbar,resizable,scrollbars");
			}

		//helpWin = makeNewWindow(prepend +  'help.htm',600,400,"helpWin");
		}
	certificateWin.focus();
}
///////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////
function navRiskSheet()
{
	// show the entire risk communication sheet
	// check window status
	var result = winCheck(risksheetWin);
	//alert(result);
	if (result != "open")
		{
		risksheetWin = makeNewWindow(Root + 'risksheet.htm',800,600,"risksheetWin","noroot");
		}
	else
		{
		// it's already open! make sure to refresh it!
		risksheetWin.setTableValues();
		}
	risksheetWin.focus();
}
///////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////
function navDLink()
	//makeNewWindow('m3l1s010.text.htm',600,400);
{

	var which = "";
	//alert("hullo! navDLink");
	// put "_text" before the "."
	//alert(courseWin.document.location.href);
	which = courseWin.location.href.substring(0,courseWin.location.href.lastIndexOf("."))
		+ ".dlink.htm";
	//alert(which);
	courseWin.location.href = which;
}


////////////////////////////////////////////////////////////////////////////////////
function dlink(which)
{
	// prepend path...
	which = courseWin.document.location.href.substring(0,courseWin.document.location.href.lastIndexOf("/")+1)
		+ which;
	if (DEBUG)		{alert(which);		}

	// check window status
	var result = winCheck(dlinkWin);
	//alert(result);
	if (result != "open")
		{
		dlinkWin = makeNewWindow(which,795,575,"dlinkWin","noroot");
		}
	dlinkWin.focus();
}


/*
dmw: keep old version for reference
//alert("dlinkWin:" + dLinkWin);

//dLinkWin = window.open(which,'dlink','width=796,height=433,resizable,status,scrollbars,menubar,location,titlebar,toolbar');

if ((screen.width == 800) && (screen.height==600))
  {
  dLinkWin = window.open(which,'dlink','width=' + screen.availWidth + ',height=' + screen.availHeight + ',resizable,status,scrollbars,menubar,location,titlebar,toolbar');
  //popWin.moveTo(0,0); 
  dLinkWin.moveTo(0,-4);   
  dLinkWin.resizeTo(800,screen.availHeight+4);
  }
else
  {
  dLinkWin = window.open(which,'dlink','width=' + 796 + ',height=' + 433 + ',resizable,status,scrollbars,menubar,location,titlebar,toolbar');
  dLinkWin.moveTo(0,0); 
  }
dLinkWin.focus();
return;
}
*/
///////////////////////////////////////////////////////////////////////////////


/* ///////////////////////////////////////////////////////////////////////////////////
function navExit() -- MOVED to cms_page.js
{

	var result = confirm("Are you sure you wish to exit?");

	if (result == true)
		{
		storeBookmark();
		courseWin.close()
		window.close();
		}
}
*/ //////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function navGoto(destination)
{
	// lookup opener.NavPosition for this destination
	// set NavPosition. (compare with navTo)
	NavPosition = LookUpNavPos(destination);
	navTo(destination);
}
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function navMod(which)
// if the student is allowed to navigate to the given module, do so.
// call the global routine which updates the bookmark
{
	var destination;
	//alert("navigating to module " + which);

	// debug, temporary -- set destination to current frame just incase nav doesn't work
	destination = CoursePage[NavPosition];
	ModIn = which;
	switch (which) 
		{
		case 1:	
			destination = "mod1/M1L1S005";
			break;
		case 2:	
			destination = "mod2/M2L1S005";
			break;
		case 3:	
			destination = "mod3/M3L1S005";
			break;
		case 4:	
			destination = "mod4/M4L1S010";
			break;
			
		default:
			alert ("navMod got unexpected value " + which);
		}

	// lookup opener.NavPosition for this destination
	NavPosition = LookUpNavPos(destination);
	//alert("looked it up and got " + NavPosition);
	navTo(destination);
}
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function navTo(destination)
// convert destination to path (add ".htm")
// go to the specified destination, track bookmark location in "opener"
// this function does NOT set NavPosition (compare with navGoto)
{
	//if (DEBUG)
	//	{
	//	alert("hullo navTo()");
	//	}


	bookmarkLocation = destination;
	if (0 == destination.indexOf("mod"))
		{
		var gomod = parseInt(destination.charAt(3));
		ModIn = gomod;
		}

	if (-1 == destination.indexOf(".htm"))
		{
		destination += ".htm";
		}
	//if (-1 != courseWin.location.href.indexOf("mod"))
	//		{
	//		destination = "../" + destination;
	//		}
	destination = Root + destination;
	//if (DEBUG) 	{alert("destination: " + destination + " vs mylocation " + courseWin.location.href);}
		//alert("Root_path" + Root_path + destination + " vs " + courseWin.location.href);
	//alert("destination: " +destination + " vs " + document.location.href);
	courseWin.location.href = destination;
	// courseWin.location.href = Root + destination;
	//courseWin.location.replace(destination);
	//courseWin.location.href = Root_path + destination;
	//if (DEBUG)
	//	{
	//	alert("new coursewin location:" + courseWin.location.href);
	//	}

	//courseWin.focus();
}
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function LookUpNavPos(myLocation)
// return correct NavPosition value for specified screen.
// pass course location as stored in CoursePage, e.g., 'm3l1s020'
{
	var retval,i;
	for (i=1;i <= MaxPage;i++)
	{
		if (myLocation.toLowerCase() == CoursePage[i].toLowerCase())
		{
			retval = i;
			//alert (i + ":" + myLocation + ":" + CoursePage[i]);
			break; // exit loop once correct NavPos is found
		}
		else
		{
			//alert (i + ":" + myLocation + ":" + CoursePage[i]);
		}
	}
return retval;
}
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
function modFinish(which)
// call this function at end of lesson -- mark current module as viewed, make next one available.
{
MenuState[which] = _viewed_;
if (which < 4) // is there another lesson?
	{
	MenuState[which+1] = _avail_;
	}
}
////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
function preloadStandard()
// call in onLoad for page, standard graphic preloads
// very much like MM_preloadgraphics, except we use loops to build our filenames:
// back_{hit,off,on}
// bullet_{over,unavail,viewed}_[1-4]
// next_{hit,off,on}
// top_bullet_{hit,off}_[1-4]
// submit_button.jpg and submit_button_over.gif
{
var a = new Array(),str1 = new Array(), str2= new Array(), str3 = new Array();
var i,j,k=0;
var iRoot = "./Images/Interface/";
var bug="";


// next and back, hit off and on
str1 = Array("back_","next_");
str2 = Array("hit","off","on");

for (i=0;i<str1.length ;i++ )
	{
	for (j=0;j<str2.length ;j++ )
		{
		a[k++]=iRoot + str1[i]  + str2[j];
		}
	}

// bullets 1-4
str1 = Array("over_","hit_","avail_");

for (i=1;i<=4 ;i++ )
	{
	for (j=0;j<str1.length ;j++ )
		{
		a[k++] = iRoot + "bullet_" + str1[j] + i;
		}
	}

// top bullets 1-4
str1 = Array("hit_","off_");

for (i=3;i<=3 ;i++ )
	{
	for (j=0;j<str1.length ;j++ )
		{
		a[k++] = iRoot + "top_bullet_" + str1[j] + i;
		}
	}

a[k++] = iRoot + "submit_button";
a[k++] = iRoot + "submit_button_over.gif";

// truncate them
for (i=0;i<a.length ;i++ )
	{
	if (-1 == a[i].indexOf(".gif"))
		{
		a[i] =a[i] + ".jpg";
		}
// report it?
	if (DEBUG)	{bug = bug + a[i] + ";";}
	}
if (DEBUG)	{alert(bug);}

/*
like this:
function MM_preloadgraphics() { //v3.0
  var d=document; if(d.graphics){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadgraphics.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
*/

var d = courseWin;
if (!d.MM_p) d.MM_p = new Array();
//var i,j -- already defined
j = d.MM_p.length;
//a = MM_Preloadgraphics.arguments -- a is already our array
for (i=0;i<a.length ;i++ )
	{
	if (a[i].indexOf("#")!=0)
		{
		d.MM_p[j]=new Image; 
		d.MM_p[j++].src=a[i];
		}
	}
}
/*
'Images/Interface/top_bullet_hit_1.jpg', 
"Images/Interface/top_bullet_off_1.jpg",
'Images/Interface/top_bullet_hit_2.jpg'
"Images/Interface/top_bullet_off_2.jpg"
'Images/Interface/top_bullet_hit_3.jpg'
"Images/Interface/top_bullet_off_3.jpg"
'Images/Interface/top_bullet_hit_4.jpg'
"Images/Interface/top_bullet_off_4.jpg"
"Images/Interface/back_off.jpg"
"Images/Interface/next_off.jpg"
*/

///////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
function makeNewWindow()
////////////////////////////////////////////////////////////////////////////////
// filename to open
// width in pixels
// height in pixels
// optional: window name (open new window vs. reusing)
// optional: special flag to not prepend root.
// optional: override default paramlist
{
var newWin;
var mode = "short";
var params;
var newWinName = "courseWin";
var a=makeNewWindow.arguments;
var what = a[0], wid=a[1], hyt=a[2];
var prependFlag = true;

if (DEBUG)
	{
	params = ",titlebar,resizable,scrollbars,location,status";
	}
else
	{
	params = ",titlebar,resizable,scrollbars";
	//params = ",fullscreen=yes";
	}

if (a.length>3) {
	newWinName = a[3];
	}
if (a.length>4) {
	prependFlag = (a[4] != "noroot");
	}
if	(a.length>5) {
	params = a[5];
	}

if (DEBUG) {alert("Make new window: " + what);}
if (prependFlag)
	{
	what = Root + what;
	}
if (DEBUG) {alert("Make new window: " + what);}

//alert("going to try to open a window " + newWinName + " now");
//popWin = window.open(what,"popwin","height=" + height + ",width=" + width + ",left=" +  xpos + ",top=" + ypos + "scrollbars,menubar=\"yes\",titlebar=\"yes\",toolbar=\"yes\"");
// don't write to a window that isn't there yet



if ((screen.width == 800) && (screen.height==600) && (newWinName=="courseWin"))
  {
  newWin = window.open(what,newWinName,'width=' + screen.availWidth + ',height=' + screen.availHeight + params);
  // need to do this RIGHT away before onLoad in spawned window can do anything
	courseWin = newWin;
  //popWin.moveTo(0,0); 
  newWin.moveTo(0,-4);   
  newWin.resizeTo(800,screen.availHeight+4);
  }
else
  {
  newWin = window.open(what,newWinName,'width=' + wid + ',height=' + hyt + params);
  // need to do this RIGHT away before onLoad in spawned window can do anything
  if (newWinName == "courseWin")	{
	  courseWin = newWin;
	  // center on screen
	  newWin.moveTo( (((screen.availWidth-wid)/2)-18),(screen.availHeight-hyt)/2);
	  }
  else {
	  // move to topleft
	  newWin.moveTo(0,0);
	}
  }
newWin.focus();


return(newWin);
}
////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
function storeBookmark()
// store the bookmark
// also store the mod completion status
{
	if (DEBUG)		{	alert("hullo storeBookmark() " + "Usr: " + usrIDX + " @ " + bookmarkLocation)		}

	bookTable[usrIDX] = bookmarkLocation + "," + NavPosition;
	// join back into strings
	var booklist = rot13(bookTable.join("&"));
	// booklist = escape(booklist);
	// escape doesn't change slashes!
	Set_Cookie("cms_book",booklist,expiry);

	// now module status from menu state array
	modstatTable[usrIDX] = MenuState.join(",");
	// join back into string
	var modstatList = modstatTable.join("&");
	Set_Cookie("cms_modstat",modstatList,expiry);

	// now autodlink status into dlink state array
	dlinkTable[usrIDX] = AutoDLink;
	var dlinklist = dlinkTable.join("&");
	Set_Cookie("cms_dlink",dlinklist,expiry); //01/19/2004

	// 
}
///////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
function bookCheck(bookdest)
// goto bookmark, or goto welcome
{
	var zz,msg = "";
	var file = "M0L0S005.htm";
	var dbug="";
	var tArr = bookdest.split(",");
	dbug = tArr[0] + "@" + tArr[1];
	msg = "Choose OK to resume where you left off in this course, " +
		"or choose Cancel to go to the welcome screen.";// + dbug;
	result = confirm(msg);

	// y'know what? even if they are choosing cancel, we'd still like
	// to parse their old MenuState info so modules that were open to them
	// are still open. So do this unconditionally.
	MenuState = modstatTable[usrIDX].split(",");

	// and now that there's a dlink status, we want to get that, too

	AutoDLink = dlinkTable[usrIDX];
	
	// dammit, i wanna know why it gets garbled!
	//var msg,ii,jj;
	//jj = dlinkTable.length;
	//for (ii=0; ii < jj; ii++ )
	//	{
	//	msg = msg + ii + "," + dlinkTable[ii] + ":" + typeof dlinkTable[ii] + ";";
	//	}
	//alert(msg);

	if	(AutoDLink)
		{
		msg = "When you last used this course, you had indicated that you " +
			"preferred question presentations that were optimized for " +
			"students with visual impairments. Choose OK if this is still " +
			"true. Choose Cancel if you would prefer question presentations " +
			"optimized for sighted students."
		AutoDLink = confirm(msg);
		}


	if (result)
		{
		// also need to parse MenuState from modstatTable
		MenuState = modstatTable[usrIDX].split(",");
		//
		bookdest = tArr[0];
		NavPosition = tArr[1];
		bookmarkLocation = bookdest;
		if (0 == bookdest.indexOf("mod"))
			{
			var gomod = parseInt(bookdest.charAt(3));
			ModIn = gomod;
			}

		if (-1 == bookdest.indexOf(".htm"))
			{
			bookdest += ".htm";
			}
		file = bookdest;
		}
	makeNewWindow(file,795,575);
}
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
function winCheck(win)
// check the status of the specified window
{
var result = ""
if (!win)
	{
	//alert("looks like window is not open");
	result = "new";
	}
else if (win.closed)
	{
	//alert("looks like it was closed, reopen it");
	result = "closed";
	}
else
	{
	//alert("window is open");
	result = "open"
	}
return result;
}
///////////////////////////////////////////////////////////////////////////////
