// -------------------------------------------------------------------------------------
// Developer Lab Tools
//  v2.31, November 2003
// -------------------------------------------------------------------------------------
//
// This version of LabTools.js should be used while developing a lab.  It will show
// the layout of the lab as it will appear when ported to WebCT quiz format.
// Category names, Question names and numbers are generated uniquely and 
// automatically.
// Question boxes appear as they will appear in the WebCT quiz.
// A "Table of contents navigation guide" containing links to each section is generated 
// automatically and appears at the end of the document.
// When development is finished, the HTML generated for this table of contents should 
// be copied and inserted in the beginning.
//
// Modifications:
// Nov 2003 -- fixed Due Date generation to account for change in Daylight Savings Time
//             fixed generation of Roman Numerals (<UL Type=I> changed to <OL Type=I>)
//
// -------------------------------------------------------------------------------------
// Global Variables:
//
//   sectNum :  keeps track of section number for lab instructions
//   stepNum :  keeps track of step number for lab instructions
//   questionNum :  keeps track of question number for lab sheet instructions
// -------------------------------------------------------------------------------------

     var sectNum=0
     var stepNum=0
     var questionNum=0
	 
     var RomanNumeral = Array("0","I","II","III","IV","V","VI","VII","VIII","IX","X",
                              "XI","XII","XIII","XIV","XV")
	 
 	// sect holds the name of each section created.
 	// anch holds the name of the anchor assigned to each section.
	// The dimension (20) is arbitrary and can be changed.
  	 var anch = new Array(20)
	 var sect = new Array(20)
    

// -------------------------------------------------------------------------------------
// LabHeader(DueDate)
// -------------------------------------------------------------------------------------
//
// Usage: LabHeader("Monday")
//	The DueDate can be changed as desired.  Hey, you can
//  even include calendar dates. e.g. "Monday, Feb. 19"
//
// Purpose: 
//	Puts the standard header at the top of every lab
//  sheet.
//
// Requirements:
//   The file config.js contains global data on the lab.
//   (Such as author, lab title, etc.)
//   config.js is well documented - please see that file
//   for more details
//
// To Do:
//    For now, (a copy of) this file should be in the same directory as
//    the lab.  Eventually, we'll have to come up with a stable
//    "universal" version to put in the scripts folder.
// 
//	Author:
//		Paul Zoski (pzoski@mcs.drexel.edu)
//	Revised:
//		JL Popyack (JPopyack@MCS.Drexel.edu) : usage of showDayDateAndTime 
// -------------------------------------------------------------------------------------


function preLabHeader(DueDate) {

	var numColumns=0
	numColumns++   // The leftmost (yellow) column is #1
	numColumns++   // The author column is #2
	if( program.length>0 ) numColumns++
	if( reading.length>0 ) numColumns++
	if( lecture.length>0 ) numColumns++
	if( related.length>0 ) numColumns++

	with(document) {

		// Course and Lab info
		writeln("<TABLE WIDTH=100% BORDER=0 CELLPADDING=1 BGCOLOR=#000000>\n<TR>\n<TD BGCOLOR=#000000>")
		writeln("<TABLE WIDTH=100% CELLPADDING=10 CELLSPACING=0>")
		writeln("<TR BGCOLOR=#FFCC99>\n<TD VALIGN=TOP COLSPAN=" + numColumns + ">") //Note: COLSPAN varies since #columns varies
		writeln("<H1>" + course + " " + Terms[term] + " " + year + "<BR>")
		writeln("pre-Lab " + labNumber + " Exercise: " + labName + "</H1>")
		writeln("</TD>\n</TR>\n<TR BGCOLOR=#FFFFFF>\n<TD VALIGN=TOP BGCOLOR=#FFFF99>\n<H1>&nbsp;</H1>\n</TD>")
		writeln("<TD VALIGN=TOP>")
		writeln("<H4>Written: " + author + ", " + date)
	}

	for(i=0; i<revision.length; i++) {
		if(i==0)
		   document.write("<BR>Revised: ")
		else
		   document.write(", ")
		document.write(revision[i])
	}



	with(document) {

		//Due Date 

//		write("<br>Due: at end of Lab period")
		write("<BR><FONT COLOR=#990000>Due: ")
		var d1 = new Date(SundayOfWeek1 + ", " + year)
		d1 = midnightSameDay(d1)
		var d2 = advanceDate(d1,(labNumber-1)*WEEKS+4*DAYS)
	// ------------------------------------------------------------------
	// Ensure that d2 is midnight (it may be 1 AM if Daylight Savings Time
	// begins between d1 and d2 - or 11 PM if it stops between d1 and d2 ) 
	// ------------------------------------------------------------------
		d2 = advanceDate(d2, 6*HOURS)
		d2 = midnightSameDay(d2)
		d2 = advanceDate(d2,-5*MINUTES)
		if(DueDate=="")
			showDayDateAndTime(d2)
		else
			document.write(DueDate)
		writeln("</FONT></H4>")
	

// ------------------------------------------------------------------
//	Links to programs

	var index,num
 	for(i=0; i<program.length; i++) {
 		if(i==0) {
 			writeln("</TD><TD VALIGN=TOP>")
			document.writeln("<P><FONT FACE=Arial COLOR=#990000>" +
 		                     "<B>Programs:</B></FONT>")
		}					   
		index = program[i].lastIndexOf("/")
		if( index < 0 || index > program[i].length )
			index = -1
		
		num = program[i].length - index - 1
		
		document.writeln("<BR><A HREF=" + program[i]
 		             + " TARGET=_code><FONT SIZE=-2 FACE=Arial>"
 		             + program[i].substr(index+1,num) + "</FONT></A>")
 	}


// ------------------------------------------------------------------
//	Reading Assignments

 	for(i=0; i<reading.length; i++) {
 		if(i==0) {
 			writeln("</TD><TD VALIGN=TOP>")
			document.writeln("<P><FONT FACE=Arial COLOR=#990000>" +
 		                     "<B>Reading:</B></FONT>")
		}
 		document.writeln("<BR><FONT SIZE=-2 FACE=Arial>"
 		             + reading[i] + "</FONT></A>")
 	}


// ------------------------------------------------------------------
//	Links to Lecture Notes

 	for(i=0; i<lecture.length; i+=2) {
 		if(i==0) {
 			writeln("</TD><TD VALIGN=TOP>")
			document.writeln("<P><FONT FACE=Arial COLOR=#990000>" +
 		                     "<B>Lecture Notes:</B></FONT>")
		}
 		document.writeln("<BR><A HREF=" + lectPrefix + lecture[i]
 		             + " TARGET=_lecture><FONT SIZE=-2 FACE=Arial>"
 		             + lecture[i+1] + "</FONT></A>")
	}
 
// ------------------------------------------------------------------
//	Related Links

 	for(i=0; i<related.length; i+=2) {
 		if(i==0) {
 			writeln("</TD><TD VALIGN=TOP>")
			document.writeln("<P><FONT FACE=Arial COLOR=#990000>" +
 		                     "<B>Related Links:</B></FONT>")
		}
 		document.writeln("<BR><A HREF=" + related[i]
 		             + " TARGET=_links><FONT SIZE=-2 FACE=Arial>"
 		             + related[i+1] + "</FONT></A>")
 	}
 	

	writeln("</TD></TR></TABLE>")
	writeln("</TD></TR></TABLE>")
	}
}

function LabHeader(DueDate) {

	var numColumns=0
	numColumns++   // The leftmost (yellow) column is #1
	numColumns++   // The author column is #2
	if( program.length>0 ) numColumns++
	if( reading.length>0 ) numColumns++
	if( lecture.length>0 ) numColumns++
	if( related.length>0 ) numColumns++

	with(document) {

		// Course and Lab info
		writeln("<TABLE WIDTH=100% BORDER=0 CELLPADDING=1 BGCOLOR=#000000>\n<TR>\n<TD BGCOLOR=#000000>")
		writeln("<TABLE WIDTH=100% CELLPADDING=10 CELLSPACING=0>")
		writeln("<TR BGCOLOR=#99CCCC>\n<TD VALIGN=TOP COLSPAN=" + numColumns + ">") //Note: COLSPAN varies since #columns varies
		writeln("<H1>" + course + " " + Terms[term] + " " + year + "<BR>")
		writeln("Lab " + labNumber + ": " + labName + "</H1>")
		writeln("</TD>\n</TR>\n<TR BGCOLOR=#FFFFFF>\n<TD VALIGN=TOP BGCOLOR=#FFFF99>\n<H1>&nbsp;</H1>\n</TD>")
		writeln("<TD VALIGN=TOP>")
		writeln("<H4>Written: " + author + ", " + date)
	}

	for(i=0; i<revision.length; i++) {
		if(i==0)
		   document.write("<BR>Revised: ")
		else
		   document.write(", ")
		document.write(revision[i])
	}



	with(document) {

		//Due Date 

//		write("<br>Due: at end of Lab period")
		write("<BR><FONT COLOR=#990000>Due: ")
		var d1 = new Date(SundayOfWeek1 + ", " + year)
		d1 = midnightSameDay(d1)
		var d2 = advanceDate(d1,(labNumber-1)*WEEKS+4*DAYS)
	// ------------------------------------------------------------------
	// Ensure that d2 is midnight (it may be 1 AM if Daylight Savings Time
	// begins between d1 and d2 - or 11 PM if it stops between d1 and d2 ) 
	// ------------------------------------------------------------------
		d2 = advanceDate(d2, 6*HOURS)
		d2 = midnightSameDay(d2)
		d2 = advanceDate(d2,-5*MINUTES)
		if(DueDate=="")
			showDayDateAndTime( d2 )
		else
			document.write(DueDate)
		writeln("</FONT></H4>")
	

// ------------------------------------------------------------------
//	Links to programs

	var index,num
 	for(i=0; i<program.length; i++) {
 		if(i==0) {
 			writeln("</TD><TD VALIGN=TOP>")
			document.writeln("<P><FONT FACE=Arial COLOR=#990000>" +
 		                     "<B>Programs:</B></FONT>")
		}					   
		index = program[i].lastIndexOf("/")
		if( index < 0 || index > program[i].length )
			index = -1
		
		num = program[i].length - index - 1
		
		document.writeln("<BR><A HREF=" + program[i]
 		             + " TARGET=_code><FONT SIZE=-2 FACE=Arial>"
 		             + program[i].substr(index+1,num) + "</FONT></A>")
 	}


// ------------------------------------------------------------------
//	Reading Assignments

 	for(i=0; i<reading.length; i++) {
 		if(i==0) {
 			writeln("</TD><TD VALIGN=TOP>")
			document.writeln("<P><FONT FACE=Arial COLOR=#990000>" +
 		                     "<B>Reading:</B></FONT>")
		}
 		document.writeln("<BR><FONT SIZE=-2 FACE=Arial>"
 		             + reading[i] + "</FONT></A>")
 	}


// ------------------------------------------------------------------
//	Links to Lecture Notes

 	for(i=0; i<lecture.length; i+=2) {
 		if(i==0) {
 			writeln("</TD><TD VALIGN=TOP>")
			document.writeln("<P><FONT FACE=Arial COLOR=#990000>" +
 		                     "<B>Lecture Notes:</B></FONT>")
		}
 		document.writeln("<BR><A HREF=" + lectPrefix + lecture[i]
 		             + " TARGET=_lecture><FONT SIZE=-2 FACE=Arial>"
 		             + lecture[i+1] + "</FONT></A>")
	}
 
// ------------------------------------------------------------------
//	Related Links

 	for(i=0; i<related.length; i+=2) {
 		if(i==0) {
 			writeln("</TD><TD VALIGN=TOP>")
			document.writeln("<P><FONT FACE=Arial COLOR=#990000>" +
 		                     "<B>Related Links:</B></FONT>")
		}
 		document.writeln("<BR><A HREF=" + related[i]
 		             + " TARGET=_links><FONT SIZE=-2 FACE=Arial>"
 		             + related[i+1] + "</FONT></A>")
 	}
 	

	writeln("</TD></TR></TABLE>")
	writeln("</TD></TR></TABLE>")
	}
}

// -------------------------------------------------------------------------------------
// ContentsBar()
// -------------------------------------------------------------------------------------
//
// Usage: ContentsBar()
//
// Purpose: 
//	Displays a "return to contents" bar as a section break.
//
// Cautions:
//   This also displays a "return to (course) home page" message.
//   The external link that goes with this message can get
//   misdirected if anyone starts messing with the directory
//   structure.
// 
// Author:
//		Paul Zoski (pzoski@mcs.drexel.edu)
// Modified:
//      Jeff Popyack (JPopyack@MCS.Drexel.edu), Sept. 2001
//      -- commented out the "return to home page" table entry and link.

function ContentsBar() {
	with(document) {
		writeln("<P><TABLE BORDER=0 CELLPADDING=0 WIDTH=100%>")
		writeln("<TR BGCOLOR=#FFFF99>")
		writeln("<TD><P><FONT SIZE=-2>Click </FONT><A HREF=#Contents><FONT SIZE=-2>here</FONT></A><FONT SIZE=-2>")
		writeln("to return to table of Contents. </FONT></TD>")
//		writeln("<TD ALIGN=right><P><FONT SIZE=-2>Click </FONT><A HREF=\"../../index.html\" TARGET=_top><FONT SIZE=-2>here</FONT></A><FONT SIZE=-2>")
//		writeln("to return to " + coursenum + " home page. </FONT></TD>")
		writeln("</TR></TABLE>")
		
		writeln("<P>&nbsp;")
	}
}


// -------------------------------------------------------------------------------------
// NewSection(anchor,name)
// -------------------------------------------------------------------------------------
//
// Usage: NewSection("Anchor Name","Section Name")
//
// Purpose: 
//  Initializes new section in lab instructions.
//  Calls ContentsBar(), prints section name, 
//    increments sectNum and sets stepNum back to 0.
//
// Side Effects:
//  Alters global variables sectNum, stepNum
// 
// Author:
//		JL Popyack (JPopyack@MCS.Drexel.edu)
// Modified:
//      Sept 2001, JL Popyack - added Roman Numerals to headers     
//      Jan 2002, JL Popyack - added sect[] usage     
// -------------------------------------------------------------------------------------

function NewSection(anchor,name) {
	ContentsBar()
	anch[sectNum] = anchor
	sect[sectNum] = name
	sectNum++
	stepNum=0
	document.writeln("<A NAME=" + anchor + "><H4>" + RomanNumeral[sectNum] + ". " 
	                 + name + ":</H4></A>")
}


// -------------------------------------------------------------------------------------
// NewStep()
// -------------------------------------------------------------------------------------
//
// Usage: NewStep()
//
// Purpose: 
//  Increments stepNum and displays step number 
//
// Side Effects:
//  Alters global variable stepNum
// 
// Author:
//		JL Popyack (JPopyack@MCS.Drexel.edu)
// -------------------------------------------------------------------------------------

function NewStep() {
	stepNum++
	document.writeln("<P><I>Step " + stepNum + "</I>: ")
}


// -------------------------------------------------------------------------------------
// EndLab()
// -------------------------------------------------------------------------------------
//
// Usage: EndLab()
//
// Purpose: 
//  compatibility with "WebCT lab questions" generator sheet
//
// Side Effects:
//  Alters global variable W
// 
// Author:
//		JL Popyack (JPopyack@MCS.Drexel.edu)

function EndLab() {
//  For the "designer" version of LabTools.js,
//  this function creates a "Table of Contents"
//  from the anchors created for each "New Section"
//  JL Popyack, Jan 2002

	document.writeln("<A NAME=Contents></A>")
	document.writeln("<P><B>Contents:</B>\n")
	document.writeln("<OL TYPE=I>")
	
	var i
	for(i=0; i<sectNum; i++)
		document.writeln("  <LI><A HREF=#" + anch[i] + ">" + sect[i] + "</A></LI>")	

	document.writeln("</UL>\n")
}



// -------------------------------------------------------------------------------------
// NewQuestion()
// -------------------------------------------------------------------------------------
//
// Usage: NewQuestion()
//
// Purpose: 
//  Designates the beginning of a new segment culminating
//  in a question whose answer is to be recorded.
//
// Side Effects:
//  Alters global variable questionNum
// 
// Author:
//		JL Popyack (JPopyack@MCS.Drexel.edu)
// -------------------------------------------------------------------------------------

function NewQuestion() {
	if(questionNum>0)
//		document.writeln("<FORM><INPUT TYPE=text SIZE=80></FORM>")
		document.writeln("<FORM><TEXTAREA ROWS=5 COLS=60> </TEXTAREA></FORM>")
	questionNum++
//	document.writeln("<P>----------------------------- Question " + questionNum + " -----------------------------<BR>")
	document.writeln("<HR><H3>Question " + questionNum + ":</H3><BR>")
}

// -------------------------------------------------------------------------------------
// LastQuestion()
// -------------------------------------------------------------------------------------
//
// Usage: LastQuestion()
//
// Purpose: 
//  Designates the end of the "questions" segment of the lab.
//
// Author:
//		JL Popyack (JPopyack@MCS.Drexel.edu)
// -------------------------------------------------------------------------------------

function LastQuestion() {
//	document.writeln("<FORM><INPUT TYPE=text SIZE=80></FORM>")
	document.writeln("<FORM><TEXTAREA ROWS=5 COLS=60> </TEXTAREA></FORM>")
//	document.writeln("<P>----------------------------- End of Questions ----------------------------<BR>")
	document.writeln("<HR><H3>End of Questions</H3><HR>")
}

// -------------------------------------------------------------------------------------
// ShowURL(text,ref)
// -------------------------------------------------------------------------------------
// This function allows a URL 'ref' to be given either as an
// absolute or relative reference, and will display it as an
// absolute reference.
// This is useful when hardcopy is desired that shows explicitly
// an absolute URL.  The URL will automatically be updated even
// when the location of the source file changes.

// Example:
// Suppose the base URL of the document is "http://www.isp.com/bin/index.html"
// Inserting this reference
//   ShowURL("click here", "../info.html")
// produces the HTML
//   <A HREF="../info.html">click here</A> (<TT>http://www.isp.com/info.html</TT>)

// JL Popyack, Oct. 1998
//    Modified Mar. 2000 - added ShowURLinTarget.  Allows link to open in target
//                         window or frame.
//    Modified Jan. 2001 - changed ShowURL to use JavaScript text functions rather
//                         than HTML.  Allows display of link in document's link color
//                         instead of blue.
// -------------------------------------------------------------------------------------

var NumRefs=0

function ShowURL(text,ref) {
  document.write(text.link(ref))
  if(document.links.length-1 > NumRefs) 
    NumRefs = document.links.length - 1
  var theRef = document.links[NumRefs++].href
  var linkColor = document.linkColor
  document.write(" (" + theRef.fontsize(-1).fontcolor(linkColor).fixed() + ")")
}

function ShowURLinTarget(text,ref,target) {
  document.write("<A HREF=" + ref + " TARGET=" + target + ">" + text + "</A>") 
   if(document.links.length-1 > NumRefs) 
    NumRefs = document.links.length - 1
  var theRef = document.links[NumRefs++].href
  var linkColor = document.linkColor
  document.write(" (" + theRef.fontsize(-1).fontcolor(linkColor).fixed() + ")")
}

function oldShowURL(text,ref) {
  document.write("<A HREF=" + ref + ">" + text + "</A>") 
  NumRefs = document.links.length - 1
  document.write(" (<FONT SIZE=-1 COLOR=0000FF><TT>" + document.links[NumRefs].href + "</TT></FONT>)")
}

