/******************************************************************************

  File: tcnav.js
  Author: eweb
  Copyright WBT Systems, 1999-2006
  Contents:
  Version: 7.4.0
  Build: 068

******************************************************************************/
/*
   Date:          Author:  Comments:
    5th Dec 2003  eweb     Extracted from tcmenu.js,
                           Set up via a single function doTC5StyleNavButtons().
   13th Jan 2004  eweb     #8588 Alt text for content navigation icons not localised.
    1th Mar 2005  rmcn     #8782 Redundant mail instructor link on instructor's review-submission form.
   30th Mar 2006  eweb     #10138 Mobile: No MailToInstructor.
   30th May 2006  eweb     #????? Hide Print Button
*/

// TC5Style Course Navigation (when frames are on)

// index of these arrays corresponds to the naviagtion buttons
// (there are X possible: prev folder, prev page, up folder, next page, next folder, edit, edit rights)
// we always use top.X so as to avoid confusion
var TCCourseNavType = new Array();
var TCCourseNavArg1 = new Array();
var TCCourseNavArg2 = new Array();

// default to the english strings, will be overridden with correct language.
var strPreviousFolder = "Previous folder";
var strPreviousPage   = "Previous page";
var strUpFolder       = "Up folder";
var strNextPage       = "Next page";
var strNextFolder     = "Next folder";
var strEditULM        = "Edit";
var strEditRights     = "Edit Rights";
var strPrint          = "Printer friendly";

function TCDoCourseNav( index )
{
    // course type = 1 for course, 2 for page, 3 for edit ulm, 4 for edit rights
    var myTCPathToTopClass = TCPathToTopClass;
    var tcmain = TCFindTCmain();
    if ( tcmain != null ) {
      var tccmain  = TCFindTCcmain( tcmain );
      if ( tccmain != null ) {
        myTCPathToTopClass = tccmain.TCPathToTopClass;
      }
      // cgarvey 20Jun01 - if still undefined .. try the index frame
      if( typeof( myTCPathToTopClass ) == "undefined" || myTCPathToTopClass == "" ) {
        var tccindex  = TCFindTCcindex( tcmain );
        if ( tccindex != null ) {
         myTCPathToTopClass = tccindex.TCPathToTopClass;
        }
      }
    }
    if( index == 7 ) {
        if( parent ) parent.location.replace( myTCPathToTopClass + "Create-Message-Role-Tutor" );
    }
    else if( top.TCCourseNavType[index] == 1 ) { // course
        if( top.TCCourseNavArg1[index] && top.TCCourseNavArg2[index] ) {
            TCLinkForCourse( myTCPathToTopClass + top.TCCourseNavArg1[index], myTCPathToTopClass + top.TCCourseNavArg2[index] );
        }
    }
    else if( top.TCCourseNavType[index] == 2 ) {
        if( top.TCCourseNavArg2[index] ) { // arg 1 could be 0
            TCLinkForPage( myTCPathToTopClass + top.TCCourseNavArg1[index], myTCPathToTopClass + top.TCCourseNavArg2[index] );
        }
    }
    else if( top.TCCourseNavType[index] == 3 ) {
        if( top.TCCourseNavArg1[index] ) {
            parent.location = myTCPathToTopClass + top.TCCourseNavArg1[index];
        }
    }
    else if( top.TCCourseNavType[index] == 4 ) {
        if( top.TCCourseNavArg1[index] ) {
            parent.location = myTCPathToTopClass + top.TCCourseNavArg1[index];
        }
    }
}
// TODO -- string localisation
function doTC5StlyeCourseFolderNavButtons( prev1, prev2, up1, up2, next1, next2 )
{
    if (this.name == "TCcindex"){//stops navigation being re-evaluated - bug 5664
      return;
    }
    // type will always be 1 (for course) for folder nav buttons
    if( TCFramesAreOn && top.frames && top.frames['TCtool'] ) {
        if ( top.frames['TCtool'].document.readyState == "loading" ) {
          setTimeout( "doTC5StlyeCourseFolderNavButtons('" + prev1 + "', '" + prev2 + "', '" + up1 + "', '" + up2 + "', '" + next1 + "', '" + next2 + "')", 10 );
          return;
        }
        if( top.frames['TCtool'].document.images && top.frames['TCtool'].document.images['IcnavPF'] ) {
            // prev folder
            if( prev1 && prev2 && prev2 != "" && prev2 != "" ) {
                top.frames['TCtool'].document.images['IcnavPF'].src = getTC5ImagesSrc() + "cnav_prevfolder.gif";
                top.frames['TCtool'].document.images['IcnavPF'].alt = strPreviousFolder;
                top.frames['TCtool'].document.images['IcnavPF'].title = strPreviousFolder;
                // now set up the first element of the arrays ( 0 is the prev folder button )
                top.TCCourseNavType[0] = 1; // 1 for course
                top.TCCourseNavArg1[0] = prev1;
                top.TCCourseNavArg2[0] = prev2;
            }
            else {
                top.frames['TCtool'].document.images['IcnavPF'].src = getTC5ImagesSrc() + "cnav_empty.gif";
                top.TCCourseNavType[0] = top.TCCourseNavArg1[0] = top.TCCourseNavArg2[0] = 0;
                top.frames['TCtool'].document.images['IcnavPF'].alt = "";
                top.frames['TCtool'].document.images['IcnavPF'].title = "";
            }
            // up folder
            if( up1 && up2 && up1 != "" && up2 != "" ) {
                top.frames['TCtool'].document.images['IcnavUF'].src = getTC5ImagesSrc() + "cnav_upfolder.gif";
                top.frames['TCtool'].document.images['IcnavUF'].alt = strUpFolder;
                top.frames['TCtool'].document.images['IcnavUF'].title = strUpFolder;

                                // now set up the first element of the arrays ( 2 is the up folder button )
                top.TCCourseNavType[2] = 1; // 1 for course
                top.TCCourseNavArg1[2] = up1;
                top.TCCourseNavArg2[2] = up2;
            }
            else {
                top.frames['TCtool'].document.images['IcnavUF'].src = getTC5ImagesSrc() + "cnav_empty.gif";
                top.TCCourseNavType[2] = top.TCCourseNavArg1[2] = top.TCCourseNavArg2[2] = 0;
                top.frames['TCtool'].document.images['IcnavUF'].alt = "";
                top.frames['TCtool'].document.images['IcnavUF'].title = "";
            }
            // next folder
            if( next1 && next2 && next1 != "" && next2 != "" ) {
                top.frames['TCtool'].document.images['IcnavNF'].src = getTC5ImagesSrc() + "cnav_nextfolder.gif";
                top.frames['TCtool'].document.images['IcnavNF'].alt = strNextFolder;
                top.frames['TCtool'].document.images['IcnavNF'].title = strNextFolder;
                // now set up the first element of the arrays ( 4 is the next folder button )
                top.TCCourseNavType[4] = 1; // 1 for course
                top.TCCourseNavArg1[4] = next1;
                top.TCCourseNavArg2[4] = next2;
            }
            else {
                top.frames['TCtool'].document.images['IcnavNF'].src = getTC5ImagesSrc() + "cnav_empty.gif";
                top.TCCourseNavType[4] = top.TCCourseNavArg1[4] = top.TCCourseNavArg2[4] = 0;
                top.frames['TCtool'].document.images['IcnavNF'].alt = "";
                top.frames['TCtool'].document.images['IcnavNF'].title = "";
            }
        }
    }
}

function doTC5StlyeCoursePageNavButtons( prevType, prev1, prev2, nextType, next1, next2, editulm, editrights )
{
    if (this.name == "TCcindex"){//stops navigation being re-evaluated - bug 5664
      return;
    }
    if( TCFramesAreOn && top.frames && top.frames['TCtool'] ) {
        if ( top.frames['TCtool'].document.readyState == "loading" ) {
          setTimeout( "doTC5StlyeCoursePageNavButtons(" + prevType + ", '" + prev1 + "', '" + prev2 + "', " + nextType + ", '" + next1 + "', '" + next2 + "', '" + editulm + "', '" + editrights + "')", 10 );
          return;
        }
        if( top.frames['TCtool'].document.images && top.frames['TCtool'].document.images['IcnavPP'] ) {
            // prev page
            if( prevType && prev2 && prev2 != "" ) { // prev1 can be 0
                top.frames['TCtool'].document.images['IcnavPP'].src = getTC5ImagesSrc() + "cnav_prevpage.gif";
                top.frames['TCtool'].document.images['IcnavPP'].alt = strPreviousPage;
                top.frames['TCtool'].document.images['IcnavPP'].title = strPreviousPage;
                // now set up the first element of the arrays ( 1 is the prev page button )
                top.TCCourseNavType[1] = prevType; // page or course
                top.TCCourseNavArg1[1] = prev1;
                top.TCCourseNavArg2[1] = prev2;
            }
            else {
                top.frames['TCtool'].document.images['IcnavPP'].src = getTC5ImagesSrc() + "cnav_empty.gif";
                top.TCCourseNavType[1] = top.TCCourseNavArg1[1] = top.TCCourseNavArg2[1] = 0;
                top.frames['TCtool'].document.images['IcnavPP'].alt = "";
                top.frames['TCtool'].document.images['IcnavPP'].title = "";
            }
            // next page
            if( nextType && next2 && next2 != "" ) { // next1 can be 0
                top.frames['TCtool'].document.images['IcnavNP'].src = getTC5ImagesSrc() + "cnav_nextpage.gif";
                top.frames['TCtool'].document.images['IcnavNP'].alt = strNextPage;
                top.frames['TCtool'].document.images['IcnavNP'].title = strNextPage;
                // now set up the first element of the arrays ( 1 is the prev page button )
                top.TCCourseNavType[3] = nextType; // page or course
                top.TCCourseNavArg1[3] = next1;
                top.TCCourseNavArg2[3] = next2;
            }
            else {
                top.frames['TCtool'].document.images['IcnavNP'].src = getTC5ImagesSrc() + "cnav_empty.gif";
                top.TCCourseNavType[3] = top.TCCourseNavArg1[3] = top.TCCourseNavArg2[3] = 0;
                top.frames['TCtool'].document.images['IcnavNP'].alt = "";
                top.frames['TCtool'].document.images['IcnavNP'].title = "";
            }
            // edit folder
            if( editulm && editulm != "" ) {
                top.frames['TCtool'].document.images['IcnavEF'].src = getTC5ImagesSrc() + "cnav_editpage.gif";
                top.frames['TCtool'].document.images['IcnavEF'].alt = strEditULM;
                top.frames['TCtool'].document.images['IcnavEF'].title = strEditULM;
                // now set up the fifth element of the arrays ( 5 is the edit ulm button )
                top.TCCourseNavType[5] = 3; // edit ulm
                top.TCCourseNavArg1[5] = editulm;
            }
            else {
                top.frames['TCtool'].document.images['IcnavEF'].src = getTC5ImagesSrc() + "cnav_empty.gif";
                top.TCCourseNavType[5] = top.TCCourseNavArg1[5] = 0;
                top.frames['TCtool'].document.images['IcnavEF'].alt = "";
                top.frames['TCtool'].document.images['IcnavEF'].title = "";
            }
            // edit rights
            if( editrights && editrights != "" ) { // next1 can be 0
                top.frames['TCtool'].document.images['IcnavER'].src = getTC5ImagesSrc() + "cnav_editrights.gif";
                top.frames['TCtool'].document.images['IcnavER'].alt = strEditRights;
                top.frames['TCtool'].document.images['IcnavER'].title = strEditRights;
                // now set up the sixth element of the arrays ( 6 is the edit rights button )
                top.TCCourseNavType[6] = 4; // edit rights
                top.TCCourseNavArg1[6] = editrights;
            }
            else {
                top.frames['TCtool'].document.images['IcnavER'].src = getTC5ImagesSrc() + "cnav_empty.gif";
                top.TCCourseNavType[6] = top.TCCourseNavArg1[6] = 0;
                top.frames['TCtool'].document.images['IcnavER'].alt = "";
                top.frames['TCtool'].document.images['IcnavER'].title = "";

            }
        }
    }
}

function doTC5StlyeExtraNavButtons( type, showMessage, showPrint  )
{
    if (this.name == "TCcindex"){//stops navigation being re-evaluated - bug 5664
      return;
    }
    if( TCFramesAreOn && top.frames && top.frames['TCtool'] ) {
        if ( top.frames['TCtool'].document.readyState == "loading" ) {
          setTimeout( "doTC5StlyeExtraNavButtons(" + type + "," + showMessage + "," + showPrint + ")", 10 );
          return;
        }

        //alert( "doTC5StlyeExtraNavButtons(" + type + "," + showMessage + "," + showPrint + ")" );
        if( top.frames['TCtool'].document.images && top.frames['TCtool'].document.images['IcnavSM'] ) {
            // print 
            if( showPrint ) { // next1 can be 0
                //alert( "Showing print button" );
                top.frames['TCtool'].document.images['IcnavPPR'].src = getTC5ImagesSrc() + "cnav_print.gif";
                top.frames['TCtool'].document.images['IcnavPPR'].alt = strPrinterFriendly;
                top.frames['TCtool'].document.images['IcnavPPR'].title = strPrinterFriendly;
                top.TCCourseNavType[8] = top.TCCourseNavArg1[8] = top.TCCourseNavArg2[8] = 0;
            }
            else {
                //alert( "Hiding print button" );
                top.frames['TCtool'].document.images['IcnavPPR'].src = getTC5ImagesSrc() + "cnav_empty.gif";
                top.frames['TCtool'].document.images['IcnavPPR'].alt = "";
                top.frames['TCtool'].document.images['IcnavPPR'].title = "";
                top.TCCourseNavType[8] = top.TCCourseNavArg1[8] = top.TCCourseNavArg2[8] = 0;
            }
            if ( type != 28 ){
              // Send Message
              if( showMessage ) { // showMessage can be 0 or 1
                  top.frames['TCtool'].document.images['IcnavSM'].src = getTC5ImagesSrc() + "cnav_sendmessage.gif";
                  top.frames['TCtool'].document.images['IcnavSM'].alt = strMailInstructor;
                  top.frames['TCtool'].document.images['IcnavSM'].title = strMailInstructor;
                  top.TCCourseNavType[7] = top.TCCourseNavArg1[7] = top.TCCourseNavArg2[7] = 0;
              }
              else {
                  top.frames['TCtool'].document.images['IcnavSM'].src = getTC5ImagesSrc() + "cnav_empty.gif";
                  top.frames['TCtool'].document.images['IcnavSM'].alt = "";
                  top.frames['TCtool'].document.images['IcnavSM'].title = "";
                  top.TCCourseNavType[7] = top.TCCourseNavArg1[7] = top.TCCourseNavArg2[7] = 0;
              }
            }
        }
        else { //if( !top.frames['TCtool'].document.images || !top.frames['TCtool'].document.images['IcnavSM'] ) {
            //alert( "No image called IcnavSM" );
        }
    }
}

var UseTextNavLinks = true;
var NeedNavLinkSep = false;

function doNavButtonAux( cmd, graphic, title, target )
  {
    var stuff = "";
    if ( UseTextNavLinks )
      {
        if ( cmd == 0 || cmd == '' )
          {
            stuff = "";
          }
        else
          {
            stuff = "<A HREF=\"" + cmd + "\"" + target + ">" + title + "</A>";
            if ( NeedNavLinkSep )
              {
                stuff = " | " + stuff;
              }
            NeedNavLinkSep = true;
          }
      }
    else
      {
        if ( cmd == 0 || cmd == '' )
          {
            stuff = "<IMG ALT=\"\" SRC=\"icons/tc5style/common/cnav_empty.gif\" BORDER=\"0\">"; //WIDTH=\"20\" HEIGHT=\"20\" BORDER=\"0\">";
          }
        else
          {
            stuff = "<A HREF=\"" + cmd + "\"" + target + ">"
                   + "<IMG ALT=\"" + title + "\" SRC=\"icons/tc5style/common/" + graphic + "\" BORDER=\"0\">" //WIDTH=\"20\" HEIGHT=\"20\" BORDER=\"0\">"
                   + "</A>";
          }
      }
    return stuff;
  }

function doNavButtonTarget( cmd, graphic, title, target )
  {
    if ( cmd != "" )
      {
        cmd = TCPathToTopClass + cmd;
      }
    return doNavButtonAux( cmd, graphic, title, target )
  }


function doNavButton( cmd, graphic, title)
  {
    return doNavButtonTarget( cmd, graphic, title, "" );
  }

/*

    // cgarvey 05Jun01 - now a hidden option
    if ( gTC5ShowSendMsg )
      {
        // cgarvey 26Apr01 - add mail to tutor link for when frames are off
        AHREF a = AHREF(_TEXT(""));
        //Nicholas Murphy 19-May-2003  bug 6573 use tcmailtoinstructor instead
        a.onclick=_TEXT("TCMailToInstructor(true);return false;");
        FormButton( ri.result, strSendMessage, a, needSep );
      }

*/

/*
   Types
   0 - nothing
   1 - Course
   2 - Page or Exercise
   3 - Edit ulm
   4 - Edit rights
*/

function doTC5StyleNavButtons(ID,type,prev,up,next,prevType,prevID,nextType,nextID,editp,adminp,showPrintULM)
  {
    var p0 = 0;  // previous: type
    var p1 = ''; // previous: command for index frame
    var p2 = ''; // previous: command for cmain frame
    var n0 = 0;
    var n1 = '';
    var n2 = '';
    var e0 = '';
    var a0 = '';
    var pr0 = '';
    var pf0 = '';
    var pf1 = '';
    var uf0 = '';
    var uf1 = '';
    var nf0 = '';
    var nf1 = '';
    var showMail = false;
    if ( TCIsStudent() && TopClassMobile == 0 )
      {
        showMail = true;
      }
    if ( type == 42 )
      {
        showPrintULM = false;
      }

    if ( prevType == 34 ) // a course
      {
        p0 = 1;
        p1 = 'Retrieve-Page-' + prevID;
        p2 = 'Info-Course-' + prevID;
      }
    else if ( prevType == 33 || prevType == 42 ) // a page or a test
      {
        p0 = 2;
        p1 = '';
        p2 = 'Retrieve-Page-' + prevID;
      }
    if ( nextType == 34 ) // a course
      {
        n0 = 1;
        n1 = 'Retrieve-Page-' + nextID;
        n2 = 'Info-Course-' + nextID;
      }
    else if ( nextType == 33 || nextType == 42 ) // a page or a test
      {
        n0 = 2;
        n1 = '';
        n2 = 'Retrieve-Page-' + nextID;
      }
    if ( editp && ID != '' && ID != '0D0' )
      {
        e0 = 'Edit-ULM-' + ID;
      }
    if ( adminp && ID != '' && ID != '0D0' )
      {
        a0 = 'Edit-Rights-ULM-' + ID;
      }
    if ( ID != '' && ID != '0D0' )
      {
        pr0 = 'Print-ULM-' + ID;
      }
    if ( prev != '' && prev != '0D0' )
      {
        pf0 = 'Retrieve-Page-' + prev;
        pf1 = 'Info-Course-' + prev;
      }
    if ( up != '' && up != '0D0' )
      {
        uf0 = 'Retrieve-Page-' + up;
        uf1 = 'Info-Course-' + up;
      }
    if ( next != '' && next != '0D0' )
      {
        nf0 = 'Retrieve-Page-' + next;
        nf1 = 'Info-Course-' + next;
      }

    if ( TCFramesAreOn )
      {
        //alert( 'doTC5StlyeCoursePageNavButtons('+p0+','+p1+','+p2+','+n0+','+n1+','+n2+','+e0+','+a0+');' );
        doTC5StlyeCoursePageNavButtons(p0,p1,p2,n0,n1,n2,e0,a0);
        //alert( 'doTC5StlyeCourseFolderNavButtons('+pf0+','+pf1+','+uf0+','+uf1+','+nf0+','+nf1+')' );
        doTC5StlyeCourseFolderNavButtons(pf0,pf1,uf0,uf1,nf0,nf1);
        //DumpNavButtons();
        doTC5StlyeExtraNavButtons( type, showMail, showPrintULM );
      }
    else
      {
        if ( n1 == 0 || n1 == '' )
          {
            n1 = n2;
          }
        if ( p1 == 0 || p1 == '' )
          {
            p1 = p2;
          }
        if ( UseTextNavLinks )
          {
            document.write( "<DIV class=\"TCSSTextTabs\">" );
          }
        else
          {
            document.write( "<P>" );
          }
        NeedNavLinkSep = false;
        document.write( doNavButton( pf0, "cnav_prevfolder.gif", strPreviousFolder ) ); // previous folder
        document.write( doNavButton( p1,  "cnav_prevpage.gif",   strPreviousPage ) );   // previous page
        document.write( doNavButton( uf0, "cnav_upfolder.gif",   strUpFolder ) );       // up folder
        document.write( doNavButton( n1,  "cnav_nextpage.gif",   strNextPage ) );       // next page
        document.write( doNavButton( nf0, "cnav_nextfolder.gif", strNextFolder ) );     // next folder
        document.write( doNavButton( e0,  "cnav_editpage.gif",   strEditULM ) );        // edit page
        document.write( doNavButton( a0,  "cnav_editrights.gif", strEditRights ) );     // admin page
        if ( showPrintULM )
          {
            document.write( doNavButtonTarget( pr0, "cnav_print.gif",  strPrint, " target=\"_blank\"" ) );          // print page
          }
        if ( showMail )
          {
            document.write( doNavButtonAux( "javascript:TCMailToInstructor(1);", "cnav_sendmessage.gif", strMailInstructor, "" ) );
          }

        if ( UseTextNavLinks )
          {
            document.write( "</DIV>" );
          }
        else
          {
            document.write( "</P>" );
          }
      }
  }

// for debugging
function DumpNavButtons()
  {
    if ( TCFramesAreOn && top.frames && top.frames['TCtool'] )
      {
        if ( top.frames['TCtool'].document.readyState == "loading" )
          {
            document.write( "Not ready yet" );
            return;
          }
        for ( i = 0; i < top.TCCourseNavType.length; i++ )
          {
            document.write( i + ": " + top.TCCourseNavType[i] + " " + top.TCCourseNavArg1[i] + " " + top.TCCourseNavArg2[i] );
          }
      }
  }

