//Filename:	view.js
//Description:	Functions related to displaying content pages in different view modes.
//		Include functions to dynamically write stylesheet links and popup menu.
//		Also includes functions to link to workbook

// Version: 1.24

//Revisions:
//	1.0 - 27-Sept-2005
//	1.1 - 05-Oct-2005 DLP.
//	1.2 - 11-Oct-2005 RJE - WriteStyleSheet2 has to have extra ..\/ to avoid duplicate theme folders
//		  					Instructor Notes changed to Solutions Book
//	1.3 - 17-Oct-2005 DLP - Modified showing instructor notes for use with ClassAct.
//	1.4 - 3-Nov-2005 RJE  - show and hide functions check object exists
//							added initialiseInstructor to show appropriate divs
//							added writeStudentActivity and writeTeacherActivity
//							added functions to write style sheets for Glossary, Reference and Help
//							added function writeInsertFaultMessage - will require activeX link adding
//	1.5 - 4-Nov-2005 DLP  - Now includes stylesheet linking for the student workbook.
//	1.6 - 7-Nov-2005 RJE  - Reference sheet style made the same as Instruction Sheet
//	1.7 - 9-Nov-2005 RJE  - CSS_UtilPath changed to allow chapter specific inventory
//							writeInsertFaultMessage updated to call SendLJFault
//	1.8 - 10-Nov-2005 RJE - Added fix for querystring handling for IE5
//	1.9 - 17-Nov-2005 RJE - ChangeDisplay() uses WriteCustomCookie and add '/' as path
//	1.10- 18-Nov-2005 DLP - Checks for the "Notes" setting in the content preferences to
//							determine when the popup is a ClassAct popup or not.
//	1.12- 21-Nov-2005 DLP - Make sure the override setting is checked in all placed where it is needed.
//	1.13- 11-Jan-2006 RJE - Added function UpdatePrintViewQs and helper getElementsByClass to handle display of questions in print view
//	1.14-  7-Feb-2006 RJE - Added function addLangSwitch()
//	1.15- 16-Feb-2006 DLP - Modified addLangSwitch to allow for no "language" variable.
//							Changed text on fault insertion.
//	1.16- 27-Feb-2006 DLP - Modified WriteLink to allow for a "review" setting.
//							Removed path setting from the journal link.
//	1.17- 28-Feb-2006 DLP - Changed the wording of the fault insertion message.
//	1.18- 30-Mar-2006 DLP - Modified to use different refresh mode for ClasAct operation.
//							Plus use ASP path finding if an ASP is launched.
//							Language settings reference variables. 
//							Added showtablewithID from Roger's custom.js
//							Removed options setting for launching workbooks.
//	1.19- 11-Apr-2006 DLP - Added looking inside a "LJInsResPath" cookie for the instructor resource directory.
//	1.20- 11-May-2006 DLP - Modified to use different screen mode change for ClasAct operation.
//      1.21- 24-May-2006 DLP - Uses new version of "CustomRecord" that takes a lines parameter that will draw
//							a specified number of lines for the print substitute box.
//      1.22- 27-Jul-2006 RJE - Change to WriteFooter() to add Copyright to page.
//      		  DLP - Stop instructor links from appearing on a preview page.
//      1.23- 12-Sep-2006 RJE - Added writeTeacherStudentActivity
//      1.24- 14-Sep-2006 RJE - Force a reload in Online Manual mode after a mode change to fix problem
//				with anchors and popup windows.
// 	1.25- 14-Nov-2006 DLP - Modified GetMediaDir function to work around a bug in IE7.  
// 				Modified bIsOnlineManual function to declare the operational mode as online manual 
//				if the instructor mode setting is made on the query string.
// 	1.26- 20-Nov-2006 DLP - Made sure local variables used in for loops are local variables.
//      1.27- 03-Jan-2007 RJE - Modified initialiseInstructor() to call processInstructorQuestions to allow Instructor Questions to be interspersed in TCW
//                            - Added writeGuidanceNote(QId)
//		 



//Get the current page.
var firstpage = false;
var sQueryStr ='';
var sLocation = unescape(window.location);
var pos = sLocation.indexOf('?');
if(pos!=-1)
	sQueryStr=sLocation.substring(pos, sLocation.length);
else
	sQueryStr='';
var PageOptions = sQueryStr;
var CurrentPagePath = location.pathname;
var PageURL = '';
var ljInsNotes = '';  //Instructor Notes value - Added 17/11/2005 DLP.


//Constants used in the page.
var CSS_LinkStart				= "<link rel='stylesheet' type='text\/css' href='"
var CSS_CustomPath				= CSS_LinkStart+"..\/theme\/custom\/styles\/";
var CSS_SysPath					= CSS_LinkStart+"..\/theme\/system\/styles\/";
var CSS_UtilPath				= CSS_LinkStart+"..\/..\/help\/css\/";
var CSS_MenuColours				= CSS_CustomPath+"entColour.css' />";
var CSS_Print					= CSS_SysPath+"print.css' \/>";
var CSS_Presentation			= CSS_SysPath+"presentation.css' \/>";
var CSS_Enterprise				= CSS_SysPath+"enterprise.css' />";
var CSS_ViewMenu				= CSS_SysPath+"viewmenu.css' \/>";
var CSS_LocalStyle				= CSS_SysPath+"localstyle.css' \/>";
var CSS_Preview					= CSS_SysPath+"preview.css' \/>";
var CSS_Overview				= CSS_SysPath+"overview.css' \/>";
var CSS_Workbook				= CSS_SysPath+"workbook.css' \/>";
var CSS_PreviewOverview			= CSS_SysPath+"pre_overview.css' \/>";
var CSS_Help_Print				= CSS_UtilPath+"instructionSheetPrint.css' \/>";
var CSS_Help_Presentation		= CSS_UtilPath+"instructionSheetPresentation.css' \/>";
var CSS_Help_Default			= CSS_UtilPath+"instructionSheet.css' \/>";
var CSS_Glossary_Print			= CSS_UtilPath+"glossaryPrint.css' \/>";
var CSS_Glossary_Presentation	= CSS_UtilPath+"glossaryPresentation.css' \/>";
var CSS_Glossary_Default		= CSS_UtilPath+"glossary.css' \/>";
var CSS_Reference_Print			= CSS_UtilPath+"instructionSheetPrint.css' \/>";
var CSS_Reference_Presentation	= CSS_UtilPath+"instructionSheetPresentation.css' \/>";
var CSS_Reference_Default		= CSS_UtilPath+"instructionSheet.css' \/>";
var CSS_Util_ViewMenu     		= CSS_LinkStart+"..\/..\/theme\/system\/styles\/viewmenu.css' \/>";


var sPRINT_OPTION = '?opt=print';
var sFIRST_PAGE = 'page001.htm';

var sPOPUP_OPTIONS = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width=775,height=530,left=5,top=5,screenX=5,screenY=5';

var sTEACHER_GRAPHIC = "..\/theme\/images\/teacher.gif";
var sSTUDENT_GRAPHIC = "..\/theme\/images\/student.gif";
var sTEACHERSTUDENT_GRAPHIC = "..\/theme\/images\/tchstd.gif";

//Browser snif
var ns4 = (document.layers);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);

if (PageOptions.length != 0)
{
	//There is a search parameter specified, need to built what the search parameter page number is.
	PageURL = PageName(PageOptions);
}
else 
{
	endPosIE = CurrentPagePath.lastIndexOf('\\');
	endPosNN = CurrentPagePath.lastIndexOf('/');
	if (endPosIE > endPosNN) 
		endPos = endPosIE;
	else
		endPos = endPosNN;          
          
	PageURL = CurrentPagePath.substring(endPos+1, CurrentPagePath.length);
  	      
	if (PageURL.search('.htm') == -1)
	{
		PageURL = sFIRST_PAGE;
		firstpage = true;
	}
}

if (PageURL.length <= 0)
{
	PageURL = sFIRST_PAGE;
}
if (PageURL.toLowerCase() == sFIRST_PAGE)
{ 
        firstpage = true;
}  
PageURL = PageURL + sPRINT_OPTION;



function bIsClassAct()
{
  var sOverride = unescape(GetCookieStr('MS='));
  var sModSet = unescape(GetCookieStr('LJModID='));
  
  var bShowNotes = ShowNotes();
  var bReturnValue = (sModSet!='') && (ljInsNotes=='') && !((bIsNewWindow()) || (bUsesFrames()) || (sOverride == '1'));
  
  return bReturnValue;

}

function IsPrintMode()
{
  return ((PageOptions == sPRINT_OPTION) || (GetMode()==0));
}

// Function to determine whether the window 
// is a popup window.
function bIsNewWindow()
{
  if ((window.opener) && (window.opener != window))
  {
    return (!window.opener.closed);
  }
  else
  {
    return false;
  }
}

// Function to determine whether the html 
// document sits in a frame or now.
function bUsesFrames()
{
  return (window.parent.frames.length  != 0);
}

function bIsOnlineManual()
// Modified to declare as online manual if the instructor mode
// setting is made.
// Change made 14/11/2006 DLP.
{
  if (bHasInsQuery())
  {
    var bReturnValue = true;
  }
  else
  {
    var bReturnValue = false;
    var sOverride = unescape(GetCookieStr('MS='));
  
    if (document.all)
    {
      var sModSet = unescape(GetCookieStr('LJModID='));
      if (bIsNewWindow())
      {
        bReturnValue = true;

        if ((!window.opener.closed) && (window.opener != window))
        {
          var parentWin = window.opener;
          bReturnValue = (parentWin.parent.frames.length==0);
        }
      }
      else
      {
        bReturnValue = !bUsesFrames();
      }
    
      // Only return that the Online manual is being used
      // if frames are not being used and a cookie has been set.
      bReturnValue = (bReturnValue && (sModSet!='')) && ((sOverride != '1'))
    }
  }
  
  return bReturnValue; 
}

//** Function to return current page name **//
function PageName(Page){

	//Break at each ampersand
	var PageInfo = unescape(Page);
	var NextPageURL = '';
	
	
	//Search for a static page url
	var StaticPageQueryPos = PageInfo.search(/url=/i);

	if(StaticPageQueryPos != - 1)
	{
		//Read out the static URL information
		var PageURL = PageInfo.substring(StaticPageQueryPos + 4, PageInfo.length);
		var PageEnd = PageURL.indexOf('&');
		
		if(PageEnd != - 1)
			PageURL = PageURL.substring(0, PageEnd);
		NextPageURL = PageURL;		
	}

	//Look for a defined page parameter
	else
	{
		//Search for the page parameter
		var PageQueryPos = PageInfo.search(/page=/i);

		//If a page parameter is found
		if(PageQueryPos != -1)
		{	
			//Read out the page number
			var PageNum = PageInfo.substring(PageQueryPos + 5, PageInfo.length);
			var PageEnd = PageNum.indexOf('&');
		
			if(PageEnd != - 1)
				PageNum = PageNum.substring(0, PageEnd);								

			//make sure that the page is not the exit page
			if(PageNum.search(/<submit>/i) == -1)
			{	
				NextPageURL = 'page';
		
				//Make sure that the number is formatted to 3 digits
				for(var i=PageNum.length; i < 3; i++)
					NextPageURL = NextPageURL + '0';
					
				//Add in the next page number
				NextPageURL = NextPageURL + PageNum + '.htm';
			}
		}
	}
	return(NextPageURL);
}

function writeCustomCookie(name, value, expires, path, domain, secure)
{
  document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((secure) ? "; secure" : "");
}

//** Updates cookie with new display mode **//
function changedisplay(mode){

  var sCookie = 'LJContPref';
  var sNewCookieStr = '';
  var bModeChanged = false;

  sContentPrefs = unescape(GetCookieStr(sCookie + '='));

  key_value_pairs = sContentPrefs.split('|');

  //For each pair, split into keys and values and process.
  for (var i=0;i<key_value_pairs.length;i++)
  {

    key_value_pair = key_value_pairs[i];
    key_value = key_value_pair.split(',');

    switch(key_value[0].toLowerCase())
    {
      case "mode":
        sNewCookieStr += 'mode,' + mode + '|';
        bModeChanged = true;
        break;
      default:
        if (key_value[0] != '')
        {  
          sNewCookieStr += key_value[0] + ',' + key_value[1] + '|'; 
        }
    }
  }

  if(!bModeChanged){
  sNewCookieStr += 'mode,'+mode;
  }

  writeCustomCookie(sCookie, sNewCookieStr, '', "/", '', false);
  if (bIsOnlineManual())
  {

    if (bIsNewWindow())
    {
      //Force a reload as well for when anchors are used.
      location.reload();
    }
    else
    {
      location=location;
    }
  }
  else
  {
    location.reload();
  }
	
}

function WriteStyleSheet(){

	var mode = '';

	if (IsPrintMode())
	{
		//Use print stylesheet
		document.write(CSS_Print);
	}
	else
	{

		mode = GetMode();
		if (mode == 0)
		{
			//Use presentation stylesheet
			document.write(CSS_Print);
		}
		else if (mode == 1)
		{
			//Use presentation stylesheet
			document.write(CSS_Presentation);
   			//Not printing therefore need stylesheet for menu
   			document.write(CSS_Enterprise);
    		document.write(CSS_MenuColours);
			if (firstpage){
				document.write(CSS_Overview);
			}
		}
		else
		{
			//Use screen stylesheet
			document.write(CSS_LocalStyle);
    		//Not printing therefore need stylesheet for menu
    		document.write(CSS_Enterprise);
   			document.write(CSS_MenuColours);
			if (firstpage){
				document.write(CSS_Overview);
			}
		}

	}
	document.write(CSS_ViewMenu);
	document.write(CSS_Workbook);
	
	if (!IsPrintMode())
	{
		if (bIsNewWindow()){
			document.write(CSS_Preview);
			if (firstpage){
				document.write(CSS_PreviewOverview);
			}
		}
	}
  	addLangSwitch();
}

function WriteStyleSheet2(){

	var mode = '';

	if (IsPrintMode())
	{
		//Use print stylesheet
		document.write(CSS_LinkStart+"..\/..\/theme\/system\/styles\/print.css' \/>");
	}
	else
	{

		mode = GetMode();
		if (mode == 0)
		{
			//Use print stylesheet
			document.write(CSS_LinkStart+"..\/..\/theme\/system\/styles\/print.css' \/>");
		}
		else if (mode == 1)
		{
			//Use presentation stylesheet
			document.write(CSS_LinkStart+"..\/..\/theme\/system\/styles\/presentation.css' \/>");
   			//Not printing therefore need stylesheet for menu
   			document.write(CSS_LinkStart+"..\/..\/theme\/system\/styles\/enterprise.css' />");
   			document.write(CSS_LinkStart+"..\/..\/theme\/custom\/styles\/entColour.css' />");
		}
		else
		{
			//Use screen stylesheet
			document.write(CSS_LinkStart+"..\/..\/theme\/system\/styles\/localstyle.css' \/>");
   			//Not printing therefore need stylesheet for menu
   			document.write(CSS_LinkStart+"..\/..\/theme\/system\/styles\/enterprise.css' />");
   			document.write(CSS_LinkStart+"..\/..\/theme\/custom\/styles\/entColour.css' />");
		}

	}
	document.write(CSS_LinkStart+"..\/..\/theme\/system\/styles\/viewmenu.css' \/>");
	if (!IsPrintMode())
    {
    	if (bIsNewWindow()) 
    	{
			document.write(CSS_LinkStart+"..\/..\/theme\/system\/styles\/preview.css' \/>");
        }
	}
  addLangSwitch();
}

function WriteStyleSheetResource(){

	var mode = '';

	if (IsPrintMode())
	{
		//Use print stylesheet
		document.write(CSS_Print);
	}
	else
	{
		mode = GetMode();
		if (mode == 0)
		{
			//Use presentation stylesheet
			document.write(CSS_Print);
		}
		else if (mode == 1)
		{
			//Use presentation stylesheet
			document.write(CSS_Presentation);
   		//Not printing therefore need stylesheet for menu
   		document.write(CSS_Enterprise);
    		document.write(CSS_MenuColours);
		}
		else
		{
			//Use screen stylesheet
			document.write(CSS_LocalStyle);
    		//Not printing therefore need stylesheet for menu
    		document.write(CSS_Enterprise);
   		document.write(CSS_MenuColours);
		}

	}
	document.write(CSS_ViewMenu);
}



function WriteHeader(){

  var headerStr = '<td class=\"pg_header_icon\"><script language=\"javascript\">addViewMenu();<\/script>';
  var sOverride = unescape(GetCookieStr('MS' + '='));
  var bShowNotes = ShowNotes();
  
  if (!IsPrintMode() && !firstpage)
  {
    headerStr += '<br \/><a href=\"javascript:LoadPage(\'Page=1\')\" class=\"syslink\">'+sVieTxt_Overview+'<\/a>';     
  }

  //Modified to check for no cookie being set.
  //Does not show resources in a popup preview window.
  if (!bIsNewWindow())
  {
    if ((ljInsNotes=='') || (bUsesFrames()) || (sOverride == '1'))
    {
      if (InstructorMode())
      {
        headerStr += "<br \/><a href='";
        headerStr += getInstructorPath();
        headerStr += "' target='_blank' class=\"syslink\">"+sVieTxt_InstructorRes+"<\/a>";
      }
    }
    else
    {
      if (bShowNotes)
      {
        headerStr += "<br \/><a href='";
        headerStr += getInstructorPath();
        headerStr += "' target='_blank' class=\"syslink\">"+sVieTxt_InstructorRes+"<\/a>";
      }
    }
  }

  headerStr += '<\/td>';
  document.write(headerStr);
  addLangSwitch();
}

function WriteHeaderResource(){

  var headerStr = '<td class="pg_header_icon"><script language="javascript">addViewMenu();<\/script><\/td>';
  document.write(headerStr);
}


function WriteFooter(){
	
	var bPreview = false;
        var sFooterText = '<table class=\"pg_footer\"><tr><td class=\"pg_footer_text\">';

	if (!IsPrintMode())
	{
		if (firstpage) 
		{  
			bPreview = bIsNewWindow();
                	if (!bPreview){
				sFooterText += '<a href=\"javascript:LoadPage(\'Page=<SUBMIT>\')\" class=\"syslink\">'+sVieTxt_CloseWork+'<\/a>';
               		}
		}
		else{
		sFooterText += '<a href=\"javascript:LoadPage(\'Page=1\')\" class=\"syslink\">'+sVieTxt_ReturnToOverview+'<\/a>';
		}
        	sFooterText += '<\/td>';

		if (firstpage) {
			sFooterText += '<td class=\"pg_header_icon\"><img src=\"..\/theme\/system\/images\/content_browser.gif\" width=\"79px\" height=\"28px\" alt=\"'+sVieTxt_CACCCompatibleHTML+'\" \/><\/td>';
	        }

	}
	else{
		if(window.sCopyright!=null)
			sFooterText += '<p class="pg_footer_text">' + sCopyright + '<\/p><\/td>';
		
	}

	sFooterText += '<\/tr><\/table>';
    	document.write(sFooterText);

}

function WriteMenu(){
	if (!IsPrintMode())
	{
	  addMenu();
	}
}
/* Function gets display mode from cookie */
function GetMode(){

  var sContentPrefs = '';
  var mode = '';
  
  if (PageOptions == sPRINT_OPTION)
  {
    mode=0;
  }
  else	
  {
    //Read in cookie
    sContentPrefs = unescape(GetCookieStr('LJContPref='));

    //Split into pairs
    key_value_pairs = sContentPrefs.split('|');

    //For each pair, split into keys and values and process.
    for (var i=0;i<key_value_pairs.length;i++)
    {

	key_value_pair = key_value_pairs[i];
	key_value = key_value_pair.split(',');

	switch(key_value[0].toLowerCase())
	{
          case "mode":
            mode = key_value[1];
            break;
          default: 
	}
    }
  }

  //If mode not set, set to screen mode 
  if ((!mode)||(mode == ''))
  {
    mode=2;
  }

  return(mode);

}

function ShowNotes()
{
  
  var bNotes = false;
  var sSetPath = unescape(GetCookieStr('LJInsResPath='));

  ljInsNotes = '';

  //Read in cookie
  sContentPrefs = unescape(GetCookieStr('LJContPref='));
  
  //Split into pairs
  key_value_pairs = sContentPrefs.split('|');

  //For each pair, split into keys and values and process.
  for (var i=0;i<key_value_pairs.length;i++)
  {

    key_value_pair = key_value_pairs[i];
    key_value = key_value_pair.split(',');

    switch(key_value[0].toLowerCase())
    {
      case "notes":
            ljInsNotes = key_value[1];
            break;
          default: 
    }
  }

  //Show the notes if a path to the notes has been set.
  if ((ljInsNotes == 1) || (sSetPath !='')) 
  {
    bNotes = true;
  }

  return bNotes;
}

function InstructorMode_old()
//Return whether an Instructor is already logged on to the system
{
	var bInsQuery = false;

	querystr = PageOptions.substring(1,PageOptions.length);

	pairs = querystr.split('&');
	for (i=0;i<pairs.length;i++){
		pair = pairs[i];

		key_value = pair.split('=');
	
		switch (key_value[0].toLowerCase())
		{
			case "ins": 
				bInsQuery = true;
				break;
			default : //alert('Query string option not handled by this function');
		}

	}

	//find if the instructor cookie has been set
	var InstructorCookie = GetCookieStr('LJInstID=');

	//return true if an instructor is logged on
	if ((InstructorCookie.length > 0)  || (bInsQuery))
		return true;
	else
		return false;
}

function InstructorMode()
//Return whether an Instructor is already logged on to the system
{
	var bInsQuery = bHasInsQuery();

	//find if the instructor cookie has been set
	var InstructorCookie = GetCookieStr('LJInstID=');

	//return true if an instructor is logged on
	if ((InstructorCookie.length > 0)  || (bInsQuery))
		return true;
	else
		return false;
}

function bHasInsQuery()
//Return True if the instructor mode is set on the query string.
//Added 14/11/2006 DLP.
{
	var bInsQuery = false;

	querystr = PageOptions.substring(1,PageOptions.length);

	pairs = querystr.split('&');
	for (var i=0;i<pairs.length;i++){
		pair = pairs[i];

		key_value = pair.split('=');
	
		switch (key_value[0].toLowerCase())
		{
			case "ins": 
				bInsQuery = true;
				break;
			default : //alert('Query string option not handled by this function');
		}

	}
	return bInsQuery;
}

function GetCookieStr(FindCookie)
//Return the string found in the cookie for the passed information
{
var ReturnCookie = "";

//Make sure that a cookie exists
if (window.document.cookie.length != 0)
{
	//Find the position of the cookie item in the cookie
	var Index = window.document.cookie.indexOf(FindCookie);	
	
	//If an index entry exists
	if(Index != -1)
	{
		//Read all of the data
		Index += FindCookie.length;
		var CookieData = window.document.cookie.substring(Index, window.document.cookie.length);

		//Find the end of the cookie data
		var DataIndex =  (CookieData == "") ? -1 : CookieData.indexOf(";");

		//Set the length to the full length if last item
		if(DataIndex == -1)
			DataIndex = CookieData.length;

		//Set the return cookie data
		ReturnCookie = CookieData.substring(0, DataIndex);
	}
}

//Return the cookie data
return ReturnCookie;
}

function WriteLink(link_type, page_no)
{

  var str = '';
  var popuplink = false;
  var override = false;

  switch (link_type)
  {
    case "assessment": 
      str = sVieTxt_ClickToViewAssess;
      break;
    case "evaluación": 
      str = sVieTxt_ClickToViewAssess;
      break;
    case "activity": 
      str = sVieTxt_ClickToViewActivity;
      break;
    case "actividad": 
      str = sVieTxt_ClickToViewActivity;
      break;
    case "presentation": 
      str = sVieTxt_ClickToViewPres;
      Write4Screen("<a href='" + page_no + "' target='_blank'>"+sVieTxt_ClickToViewPres+"<\/a><br \/>");
      override = true;
      break;
    case "presentación": 
      str = sVieTxt_ClickToViewPres;
      Write4Screen("<a href='" + page_no + "' target='_blank'>"+sVieTxt_ClickToViewPres+"<\/a><br \/>");
      override = true;
      break;
    case "workbook": 
      str = sVieTxt_ClickToOpenWorkbook;
      popuplink = true;
      break;
    case "trabajo": 
      str = sVieTxt_ClickToOpenWorkbook;
      popuplink = true;
      break;
    case "review": 
      str = sVieTxt_ClickToOpenReview;
      break;
    case "revisión": 
      str = sVieTxt_ClickToOpenReview;
      break;
    case "observation":
      str = sVieTxt_Observation;
      popuplink = true;
      break;
    case "observación":
      str = sVieTxt_Observation;
      popuplink = true;
      break;
    default : str = sVieTxt_ErrorInLink;
  }

  if (!override)
  {
 	if (popuplink){
		Write4Screen("<a href=\"javascript:var x=popup(\'j_main.asp?LJNoHeaders=true\', \'\', true);\" border=\"0\">" + str + "<\/a>");
	}
	else 
	{
		Write4Screen("<a href=\'javascript:LoadPage(\"Page=" + page_no + "\")\'>" + str + "<\/a><br \/>");
	}
  }
} 

function Write4Screen(output_str){
	if (!IsPrintMode())
     document.write(output_str);
}

function RecordObservation(str, sAnchor, iLines){
  CustomRecord(str, sAnchor, sVieTxt_ClickRecObservation, false, iLines);
}

function AttachSolution(str, sAnchor, iLines){
  CustomRecord(str, sAnchor, sVieTxt_ClickAttachSolution, false, iLines);
}

function ResultsTable(str, sAnchor, iLines){
  CustomRecord(str, sAnchor, sVieTxt_ClickRecResults, false, iLines);
}

function CustomRecord(str, sAnchor, sText, bInLine, iLines){ 
  var sParams = ''; 
  var sChapter = ''; 

  if (!IsPrintMode()) 
  { 

    sChapter = getChapterNumber(); 

    sParams = 'LJNoHeaders=true&LJChap=' + sChapter; 

    if (sAnchor!=null) 
    { 
      sParams += '#'; 
      sParams += sAnchor; 
    } 
    
    
    if ((bInLine!=null) && (bInLine)) 
    { 
        WriteJournalLink(sText, sParams, bInLine); 
    } 
    else 
    { 
       WriteJournalLink(sText, sParams); 
    } 
  } 
  else 
  { 
    //Check to see if iLines specified 
    if ((iLines!=null) && (iLines>0)) { 
      document.write("<table class=\"obsTable\">"); 
      for(var i=0;i<iLines;i++){ 
        document.write("<tr><td class=\"dottedLine\">&nbsp;<\/td><\/tr>"); 
      } 
      document.write("</table>"); 
    } 
    else 
    { 
      document.write(str); 
    } 
  } 
} 


function CustomRecord_old(str, sAnchor, sText, bInLine){
  var sParams = '';
  var sChapter = '';

  if (!IsPrintMode())
  {

    sChapter = getChapterNumber();
    sParams = 'LJNoHeaders=true&LJChap=' + sChapter; 

    if (sAnchor!=null)
    {
      sParams += '#';
      sParams += sAnchor;
    } 
   
    if (bInLine)
    {
        WriteJournalLink(sText, sParams, bInLine);
    } 
    else
    {
       WriteJournalLink(sText, sParams);
    }
  }
  else
  {
    document.write(str);
  }
}

function WriteJournalLink(sText, sParams, bInLine)
{
  if (bInLine) 
  {
    document.write("<a href=\"javascript:var x=LoadJournal(\'" + sParams + "\')\">" + sText + "<\/a>");
  }
  else
  {
    document.write("<p class=\"work_link\"><a href=\"javascript:var x=LoadJournal(\'" + sParams + "\')\">" + sText + "<\/a><\/p>");
  }
}

function CustomRecordInLine(str, sAnchor, sText)
{
  CustomRecord(str, sAnchor, sText, true);
}


function LoadJournal(parameters){
  sdir = sASPGetLocation();
  var journalwindow = window.open(sdir + 'j_main.asp' + '?' + parameters, 'ejournal', 'status=no,toolbar=no,location=no,menu=no,resizable=yes,scrollbars=yes,');
  journalwindow.focus();
}

function showHelp(url)
{
  sdir = GetMediaDir();
  helpWin = window.open(sdir+url, 'helpWin', sPOPUP_OPTIONS);
  helpWin.focus();
}
function showGlossary(url)
{
  sdir = GetMediaDir();
  glossaryWin = window.open(sdir+url, 'glossaryWin', sPOPUP_OPTIONS);
  glossaryWin.focus();
}

function showReference(url)
{
  sdir = GetMediaDir();
  referenceWin = open(sdir+url, 'referenceWin', sPOPUP_OPTIONS);
  referenceWin.focus();
}

function GetMediaDir()
{
  rslt = new Image();
  rslt.src="";
  var sPath = rslt.src;
  if (sPath.lastIndexOf("?")>=0)
  {
    sPath = sPath.substr(0, sPath.lastIndexOf("?"));
  }
  if (sPath.lastIndexOf(".")>=0)
  {
    sPath = sPath.substr(0, sPath.lastIndexOf("/")+1);
  }
  return sPath;
}

function ReplaceWildCard(sSource, sValue)
{
  return sSource.replace(/#/gi, sValue);
}

function getChapterNumber(){

  var sChapter = '';
  var sChapterNo = '';

  //Get chapter folder name
  sPath = GetMediaDir();
  folders = sPath.split('/');
  sChapter = folders[folders.length - 2];  //Chapter is last folder.
  sChapterNo = sChapter.substring(1,sChapter.length);  

  return(sChapterNo);
}

function getInstructorPath(){
  var sModule = 'M';
  var sPath = '';
  var sChapter = '';
  var sInsPath = '';
  var sOverride = unescape(GetCookieStr('MS='));
  var sSetPath = unescape(GetCookieStr('LJInsResPath='));

  //Get module folder name
  sModule += GetCookieStr('LJModID=');

  //Get chapter folder name
  sPath = GetMediaDir();
  folders = sPath.split('/');
  sChapter = folders[folders.length - 2];  //Chapter is last folder.

  sChapter = sChapter.substring(1,sChapter.length);
  sChapter = 'Sup' + sChapter;

  // If a path has been set as a cookie, 
  // then use that path as a link to the Instructor Resources
   
  if (sSetPath!='')
  {
  	sInsPath = sSetPath;
  }
  else
  {
    //Build link to instructor page
    //if (bUsesFrames()) - Old check replaced with the following.
    //Change made 18/11/2005 DLP.
  
    if (((ljInsNotes=='') && (bIsNewWindow())) || (bUsesFrames()) || (sOverride == '1'))
    {
      sInsPath = '..\/Support\/';
    }
    else
    {
      sInsPath = '..\/..\/Support\/' + sModule + '\/html\/';
    }
    sInsPath += sChapter + '\/Instruct.htm';
  }
  
  return(sInsPath);
}


function hide(id){
  var bSuccess = false;
  if(ns4){
    document.layers[id].visibility = "hide";
  }
  // Explorer 4
  else if(ie4){
    document.all[id].style.visibility = "hidden";
  }
  // W3C - Explorer 5+ and Netscape 6+
  else if(ie5 || ns6){
    sObj = document.getElementById(id);
    if (sObj == null)
    {
    }
    else
    {
      document.getElementById(id).style.visibility = "hidden";
      eval(id).style.display = "none";
      bSuccess = true;
    }
  }
}

function show(id){
  var bSuccess = false;
  if(ns4){
   document.layers[id].visibility = "show";
  }
  // Explorer 4
  else if(ie4){
    document.all[id].style.visibility = "visible";	    
  }
  // W3C - Explorer 5+ and Netscape 6+
  else if(ie5 || ns6){
    sObj = document.getElementById(id);

    if (sObj == null)
    {
    }
    else
    {    
      document.getElementById(id).style.visibility = "visible";
      eval(id).style.display = "";
      bSuccess = true;
    }
  }
  return bSuccess;
}

function showKey(){
  hide('ins_notes');
  hidetables();
  hide('tcwb');
  hide('ins_ass_work');
  show('ans_key');
}
function showTCWB(){
  hide('ins_notes');
  hide('ans_key');
  hide('ins_ass_work');
  show('tcwb');
  showtables();
}
function showIns(){
  hide('ins_notes');
  hide('ans_key');
  hide('tcwb');
  hidetables();
  show('ins_ass_work');
}
function showNotesSection(){
  hide('ans_key');
  hide('tcwb');
  hidetables();
  hide('ins_ass_work');
  show('ins_notes');
}
function hidetables(){

  var d = document.getElementsByTagName('table');

  for(var i=0;i<d.length;i++)
  {
    if(d[i].className == 'data_table')
    {
      d[i].className = 'data_table_no_collapse';
    }
    if(d[i].className == 'workbookTable')
    {
      d[i].className = 'workbookTable_no_collapse';
    }
  }
}

function showtables(){
  var d = document.getElementsByTagName('table');

  for(var i=0;i<d.length;i++)
  {
    if(d[i].className == 'data_table_no_collapse')
    {
      d[i].className = 'data_table';
    }
    if(d[i].className == 'workbookTable_no_collapse')
    {
      d[i].className = 'workbookTable';
    }
  }
}

function showtablewithID(id){
  var d = document.getElementById(id);

  if(d.className == 'data_table_no_collapse'){
    d.className = 'data_table';
  }
}

function initialiseInstructor(){

  var bNotesAvailable = false;
  var bInsMarkWorkAvailable = false;

  hidetables();
  var sOverride = unescape(GetCookieStr('MS='));

  if (IsPrintMode())
  {
    hide('message');
    show('instructor_content');
    show('ins_notes');
    show('ins_ass_work');

    // Added by RJE 03/01/07 to allow for Instructor Questions to be interspersed with Typical Completed Work
    processInstructorQuestions(sOverride);

    show('ans_key');
    show('tcwb');
    showtables();
  }
  else if (InstructorMode())
  {
    var d = document.getElementById('ins_ques');  

    if (d != null)
    {
      if (sOverride == '1')
      {
        d.style.display = "";
      }
      else if (window.parent.frames.length  != 0)
      {
        d.style.display = "";
      }
      else 
      {
        d.style.display = "none";
      }
    }

    // Added by RJE 03/01/07 to allow for Instructor Questions to be interspersed with Typical Completed Work
    processInstructorQuestions(sOverride);

    hide('message');  
    show('instructor_content');

    //For module chapter there is NO notes page!!  Therefore show ins_ass_work.
    bNotesAvailable = show('ins_notes');
    bInsMarkWorkAvailable = hide('ins_ass_work');

    if (!bNotesAvailable)
    {
      if (bInsMarkWorkAvailable)
      {
        show('ins_ass_work') 
      }
    }
    hide('ans_key');
    hide('tcwb');
  }
}

function writeTeacherActivity(time){

  if (IsPrintMode()){
    document.write('<p class=\"smlfont\">' + ReplaceWildCard(sVieTxt_MinTeacherActivity, time) + '</p>');
  }
  else {
    document.write('<img src=\"' + sTEACHER_GRAPHIC + '\" width=\"23\" height=\"22\" alt=\"'+ReplaceWildCard(sVieTxt_RecomTeacherDeliver, time)+'\" \/>');
  }
}

function writeStudentActivity(time){
  if (IsPrintMode()){
    document.write('<p class=\"smlfont\">' + ReplaceWildCard(sVieTxt_MinStudentActivity, time) + '</p>');
  }
  else {
    document.write('<img src=\"' + sSTUDENT_GRAPHIC + '\" width=\"23\" height=\"22\" alt=\"'+ReplaceWildCard(sVieTxt_RecomStudentDeliver, time)+'\" \/>');
  }
}

function writeTeacherStudentActivity(time){

  if (IsPrintMode()){
    document.write('<p class=\"smlfont\">' + ReplaceWildCard(sVieTxt_MinTeacherStudentActivity, time) + '</p>');
  }
  else {
    document.write('<img src=\"' + sTEACHERSTUDENT_GRAPHIC + '\" width=\"46\" height=\"22\" alt=\"'+ReplaceWildCard(sVieTxt_RecomTeacherStudentDeliver, time)+'\" \/>');
  }
}

function WriteHelpStyleSheet(){
	var mode = '';

	if (IsPrintMode())
	{
		//Use print stylesheet
		document.write(CSS_Help_Print);
	}
	else
	{

		mode = GetMode();
		if (mode == 0)
		{
			//Use presentation stylesheet
			document.write(CSS_Help_Print);
		}
		else if (mode == 1)
		{
			//Use presentation stylesheet
			document.write(CSS_Help_Presentation);
		}
		else
		{
			//Use screen stylesheet
			document.write(CSS_Help_Default);
		}

	}
	document.write(CSS_Util_ViewMenu);
  addLangSwitch();
}

function WriteGlossaryStyleSheet(){
	var mode = '';

	if (IsPrintMode())
	{
		//Use print stylesheet
		document.write(CSS_Glossary_Print);
	}
	else
	{

		mode = GetMode();
		if (mode == 0)
		{
			//Use presentation stylesheet
			document.write(CSS_Glossary_Print);
		}
		else if (mode == 1)
		{
			//Use presentation stylesheet
			document.write(CSS_Glossary_Presentation);
		}
		else
		{
			//Use screen stylesheet
			document.write(CSS_Glossary_Default);
		}

	}
	document.write(CSS_Util_ViewMenu);
  addLangSwitch();
}

function WriteReferenceStyleSheet(){
	var mode = '';

	if (IsPrintMode())
	{
		//Use print stylesheet
		document.write(CSS_Reference_Print);
	}
	else
	{

		mode = GetMode();
		if (mode == 0)
		{
			//Use presentation stylesheet
			document.write(CSS_Reference_Print);
		}
		else if (mode == 1)
		{
			//Use presentation stylesheet
			document.write(CSS_Reference_Presentation);
		}
		else
		{
			//Use screen stylesheet
			document.write(CSS_Reference_Default);
		}

	}
	document.write(CSS_Util_ViewMenu);
  addLangSwitch();
}

function writeInsertFaultMessage(fault){
  var sHTML = '<table class=\"specification\"><tr><th>'+sVieTxt_FaultInsertion+'</th></tr><tr><td><p>';

  if (IsPrintMode()){
    sHTML += ReplaceWildCard(sVieTxt_BeforeFault, '<b>'+fault+'</b>');
  }
  else
  {
    SendLJFault(fault);
    sHTML += sVieTxt_FaultInserted;
  }

  sHTML += '</p></td></tr></table>';
  document.write(sHTML);
}

function testmsg(){
  alert(sVieTxt_TestMessage);
}
function getElementsByClass(classname){
  var inc=0;
  var alltags=document.all? document.all : document.getElementsByTagName("*");
  var customcollection = new Array();

  for(var i=0; i<alltags.length; i++){
    if (alltags[i].className==classname)
    {
    customcollection[inc++]=alltags[i];
    }
  }
return customcollection;
}

function UpdatePrintViewQs(){
var d = getElementsByClass('inputBoxPrintOnly');
var maxlength = 0;

for (var i=0;i<d.length;i++){
  sPrintObjName = d[i].name.toString();
  sScreenObjName = sPrintObjName.substring(5, sPrintObjName.length);
  Inputobj = eval("document.Page."+sPrintObjName);
  Selectobj = eval("document.Page."+sScreenObjName);
  Inputobj.value = Selectobj.options[Selectobj.selectedIndex].text;

  for(var j=0;j<Selectobj.length;j++)
  {
    optText = Selectobj.options[j].text;
    maxlength = optText.length > maxlength ? optText.length : maxlength;
  }
  Inputobj.size = maxlength;
  }
}

function addLangSwitch()
{
  document.write('<style type="text/css">');
  if (window.language==null)
  {
    document.write('.gb{display:none;}');  //If no language setting, then assume US.
  }
  else
  {
    switch (language.toLowerCase())
    {
      case "gb":
        document.write('.us{display:none;}');
        break;
      case "us":
        document.write('.gb{display:none;}');
        break;
      default:
        document.write('.gb{display:none;}');
    }
  }
  document.write('</style>');  
}

//Added by RJE 03/01/07 to allow for Instructor Questions to be interspersed with Typically Completed Work
function processInstructorQuestions(sOverride){

    //Select all questions by searching for the all tables with the LJQTable style applied
    var e = getElementsByClass('LJQTable');

    if (e != null)
    {

      //For each question if ClassCampus mode (or developer mode) modify the style of the table to show etc...
      for (var i=0;i<e.length;i++){
        if (sOverride == '1')
        {
          e[i].style.display = "";
          e[i].style.marginLeft = 10;
          e[i].style.width = 588;
        }
        else if (window.parent.frames.length  != 0)
        {
          e[i].style.display = "";
          e[i].style.marginLeft = 10;
          e[i].style.width = 588;
        }
        else 
        {
          e[i].style.display = "none";
        }       
      }
    }
}

//Added by RJE 03/01/07 to write the generic message for the question/
function writeGuidanceNote(QId){
  document.write('<strong>' + sVieTxt_InsGuidanceNote + QId + '<\/strong><br \/>');
}
