/*------------------------------------------------------------------- 
Author's Statement:
This script is based on ideas of the author.
You may copy, modify and use it for any purpose. The only condition is that if you publish web pages that use this script you point to its author at a suitable place and don't remove this Statement from it.
It's your responsibility to handle possible bugs even if you didn't modify anything. I cannot promise any support.
Dieter Bungers
GMD (www.gmd.de) and infovation (www.infovation.de)
--------------------------------------------------------------------*/
// Checking the client's browser and setting the text sizes of the headings in the 
// ToC depending on the global parameter textSizes set in tocParas and the browser useed:

var isIE = navigator.appName.toLowerCase().indexOf("explorer") > -1;
var mdi = (isIE) ? 1:1;
var sml = (isIE) ? 1:1;
var targetWindow = '';
//what link number has been clicked. To open right span on next page
var activeNumber = 'Top'; 
//controls the alt text for each type of button
var altText = '';
function changeColorText(whichObject){ 
  whichObject.style.color='#AB9D85';
}
function rechangeColorText(whichObject) {
  whichObject.style.color='#000000';
}
function changeColor(whichObject) {     
  whichObject.style.background = currentBackColor;  
}
function rechangeColor(whichObject) {  
  whichObject.style.background = 'none';
}
///////////////////////
function openLink(link, type, security, target){   
  if(type == "jsp"){		
    if(security != "YES")
      openJspLinkPublic(link, target);	
    else
      openJspLink(link, target);
  }
  else if(type == "html" || type == "pdf"){
    if(security != "YES")
      openHtmlLinkPublic(link, target);		
  }
  else if(type == "mailto:")
    window.open(link, TARGET="_BLANK");	
  else if(target == "MAINDISPLAY")
    location.href = link;	
  else
    window.open(link, TARGET="_BLANK");	  
}
function openJspLinkPublic(relLink, relTarget){		
  if(relTarget == "blank")
    window.open(relLink + '?userId=');
  else{
    if(relLink == "quickSearch.jsp" || relLink == "advancedSearch.jsp" ||
      relLink == "searchColonialPlants.jsp")
      location.href = relLink + '?newSearch=true&linkN=' + getActiveNumber();
    else
      location.href = relLink + '?linkN=' + getActiveNumber();				  
  }	
}
function openJspLink(relLink, relTarget){		 
  var targetLink = '';
  if(relLink != null && relLink != '')
    targetLink = relLink;						
  if(relTarget == "blank")
    window.open(relLink + '?userId=');
  else		
    location.href = relLink + '?linkN=' + getActiveNumber();							
}
function openHtmlLinkPublic(relLink, relTarget){	
  if(relTarget == "blank")
    window.open(relLink);
  else
    location.href = relLink;	
}
function openLinkPractice(relLink){
	userId = currUserId;
		
	if(userId != '')
		location.href = relLink + '&linkN=' + getActiveNumber();	
	else			
		location.href = relLink + '&linkN=' + getActiveNumber();	

}

function openStatsLink (page){ 
  var data = page;   
  if(_statsOn){
    frames['statsFrame'].document.statsForm.actionKey.value="*.*.toc_link_url.on_click";
    frames['statsFrame'].document.statsForm.data.value=data;
    frames['statsFrame'].document.statsForm.submit(); 
  }
  
 openLink('http://museum.collection.hht.net.au/', '', 'NO', 'blank');
}


function printGlobalReport(reportName, datatype){  
  var userId = currUserId;
  if(datatype == "NUMBER")
    parent.location.href = 'printDialogueGlobal.jsp?linkN=' + getActiveNumber() + '&reportType=globalNumber&report='+reportName;
  else if(datatype == "DateLocation")
    parent.location.href = 'printDialogueGlobal.jsp?linkN=' + getActiveNumber() + '&userId=' + userId + '&reportType=globalDateLocation&report='+reportName;		
  else
    parent.location.href = 'printDialogueGlobal.jsp?linkN=' + getActiveNumber() + '&reportType=global&report='+reportName;	
}
function printStatsReport(reportName){ 
  //window.open('print.jsp?report='+reportName,'printStats','scrollbars=no,status=no,resizable=yes,width=600,height=600,top=' + 50 + ',left=' + 190);		
  parent.location.href = 'printDialogueStats.jsp?linkN=' + getActiveNumber() + '&report='+reportName;	
}
// ***************************************
// The function redisplays the ToC and the content 
// ***************************************
function getChangedMenu(currentNumber) {  
  expandToc(currentNumber);
  flipToc(currentNumber);
}
function expandToc(el){  
  //close all prev opened   
  for (i=1; i<tocTab.length; i++){
    
    if(tocTab[i][0].split(".").length == 1 && tocTab[i][0] != el){              
      if(el.split("_").length == 1){//only main top level (not submenus. E.g. not Print in Request)          
        theEIToc=eval("document.getElementById('" + tocTab[i][0] + "')");
        
        if(theEIToc){
          theEIToc.style.display="none";		            
          theEIToc.expanded=false;        
          //plus image
          theEIToc=eval("document.images.img_" + tocTab[i][0]);  
          theEIToc.src = "css/images/tocImages/plus.gif";	
          theEIToc.alt = "More Options";
        }
      }      
    }
  }
  
  //close/open current
  theEIToc=eval("document.getElementById('" + el + "')");  
  
  if(theEIToc.style.display=="none"){   			
    theEIToc.style.display="block";		
    theEIToc.expanded=true;
  }
  else{   			
    theEIToc.style.display="none";		
    theEIToc.expanded=false;	
  }
  
  theEIToc.style.position="relative";
}    
function flipToc(el) {   
  theEIToc=eval("document.images.img_" + el);  
  var imageFile = "";  
  if(theEIToc.src.indexOf("tocImages/plus") > 0)  
    imageFile = theEIToc.src.substring(theEIToc.src.indexOf("plus") + 
    "plus".length);
  else
    imageFile = theEIToc.src.substring(theEIToc.src.indexOf("minus") + 
    "minus".length);
  
  if(theEIToc.src.indexOf("tocImages/plus") > 0){    
    theEIToc.src = "css/images/tocImages/minus" + imageFile;
    theEIToc.alt = "More Options - Active";   
  }else{
    theEIToc.src = "css/images/tocImages/plus" + imageFile;	
    theEIToc.alt = "More Options";   
  }
}
function setActiveNumber(thisNumber){	
  activeNumber = thisNumber;	
}
function getActiveNumber(){	
  activeNumber = (activeNumber.indexOf('.') > -1) ? activeNumber : activeNumber;
  return activeNumber;
}
function reDisplay(currentNumber) {
  var htmlOutStr = "";		
  var prevLevel = -1;
  htmlOutStr = htmlOutStr + "<table border=0 cellspacing=0 cellpadding=1 style=\""+tableLocation+"\">\n";  
  // ***************************************
  // loop over the tocTab entries: Determining which heading to display:
  // and other
  // ***************************************	
  for (i=1; i<tocTab.length; i++){	    
    thisNumber = tocTab[i][0];
    thisNumArray = thisNumber.split(".");
    thisLevel = thisNumArray.length-1;
    thisLink = tocTab[i][2];
    
    if(prevLevel == -1)
      prevLevel = thisLevel;	 		  	  
    if (i < tocTab.length-1){	
      if(thisLink == ''){				
        //img = imageMinus; //all open        
        if(activeNumber != '') {
          if(thisNumber == activeNumber.substring(0, activeNumber.indexOf("."))){
            img = imageMinus;          
            altText = "More Options - Active";
          }          
          //2nd level and so on
          if(thisNumber == activeNumber.substring(0, activeNumber.length - 2)){
            img = imageMinus;      
            altText = "More Options - Active";
          }
          else{
            img = imagePlus;
            altText = "More Options";
          }                      
        }
        else{
          img = imagePlus;
          altText = "More Options";
        }
      }
      else{
        img = imageLeaf;
        altText =  "-";
      }
    } 
    else{ 
      img = imageLeaf; //The last heading is always a leaf.
      altText =  alt="-";
    }
    thisTextColor = (thisNumber==currentNumber) ? currentColor : normalColor;       
    thisBackColor = (thisNumber==currentNumber) ? currentBackColor : backColor;
    thisTextColor = (activeNumber==tocTab[i][0]) ? lastVisitColor : thisTextColor;
    thisBackColor = (activeNumber==tocTab[i][0]) ? lastBackColor : thisBackColor;
    currentMouseAction = (activeNumber==tocTab[i][0]) ? lastLinkMouseAction : normalMouseAction;
    currentBackAction = (activeNumber==tocTab[i][0]) ? lastLinkBackAction : normalBackAction;    
    
    htmlOutStr = htmlOutStr + "<tr>\n";										
    
    //is it subsection?			
    //display subsection in as sepp. divition	
    var questionNum = '';  
    if(thisLevel > prevLevel){		      
      localNumArray = thisNumber.split(".");
      if(localNumArray.length > 2)
        questionNum = localNumArray[localNumArray.length - 3] + '_' + localNumArray[localNumArray.length - 2];
      else
        questionNum = localNumArray[0];        
      htmlOutStr = htmlOutStr + "<td style=\"padding-left: 0px\"; colspan=" + (nCols + 1) + ">\n";
      htmlOutStr = htmlOutStr + "<SPAN id=" + questionNum + " class=\"tocAnswer\">";
      htmlOutStr = htmlOutStr + "<table border=0 cellspacing=0 cellpadding=0><tr>";      
      prevLevel = thisLevel;
    }			
    //close prev. sub section/s and continue...		
    else if(thisLevel < prevLevel){
      //case when one or more levels have been skipped
      //Eg. reports are 2nd level and Email after is 0      
      if(parseInt(prevLevel) - parseInt(thisLevel) > 1){
        for(j=0; j<(parseInt(prevLevel) - parseInt(thisLevel)); j++){
          htmlOutStr = htmlOutStr + "</td></tr></table>";
          htmlOutStr = htmlOutStr + "</SPAN>";	      
          htmlOutStr = htmlOutStr + "</td><tr>";            
        }        
        prevLevel = thisLevel;
      }else{
        htmlOutStr = htmlOutStr + "</td></tr></table>";
        htmlOutStr = htmlOutStr + "</SPAN>";	      
        htmlOutStr = htmlOutStr + "</td><tr>";
        prevLevel = thisLevel;
      }
    }									    
    //spaces for subsection rows
    //default
    var leafpadd = 0;
    for (k=1; k<=thisLevel; k++) 
      leafpadd += 14;     
    
    //htmlOutStr = htmlOutStr + "<td style=\"padding-left: 10px\"></td>";								
    // ...then the hading symbol and the heading text each with a javaScript link calling 
    // just this function reDisplay again: 	      
    htmlOutStr = htmlOutStr + "<td valign=top style=\" " + tablecell + "\" align=\"left\" valign=middle>\n";
    
    if(tocTab[i][2] == ''){//submenu opener
      while(thisNumber.indexOf('.') > -1)
        thisNumber = thisNumber.replace('.','_');      
      
      htmlOutStr = htmlOutStr + "<a href=\"javascript:getChangedMenu('" + thisNumber + "')\">\n";
      htmlOutStr = htmlOutStr + "<img style=\"margin-left: " + leafpadd + "px; \" src=\"" + imageLocation + img + "\" border=0 alt=\"" + altText + "\" name=\"img_" + thisNumber + "\">\n";
      htmlOutStr = htmlOutStr + "</a>\n";
    }
    else{    
      //patch to support extra padding for stats reports 
      
      
        /*for (k=0; k<tocTabPadding.length; k++)
          if(tocTab[i][0] == tocTabPadding[k][0]){
            leafpadd = 0
            leafpadd = tocTabPadding[k][1];     
          }*/
      
      htmlOutStr = htmlOutStr + "<a href=\"" + tocTab[i][2] +  "\">";			
      htmlOutStr = htmlOutStr + "<img style=\"margin-left: " + leafpadd + "px;\"" + 
      "src=\"" + imageLocation + img + "\" border=0 alt=\"" + altText + "\" name=\"img_" + thisNumber + "\"></a>\n";			
    }
    
    htmlOutStr = htmlOutStr + "</td>\n";    
    if(tocTab[i][2] != ''){         
      htmlOutStr = htmlOutStr + "<td style=\"" + tablecell +  "\" title=\"" + tocTab[i][3] + "\" width=100% onClick=\"setActiveNumber('" + thisNumber + "');" + tocTab[i][2] + "\" ";
      htmlOutStr = htmlOutStr + "colspan=" + (nCols-thisLevel) + " " + currentBackAction  + " ";    
      htmlOutStr = htmlOutStr + "style=\"background-color: " + thisBackColor + "; vertical-align: top; cursor: hand;\"><font style=" + thisTextColor + ">" + currentMouseAction + tocTab[i][1] + "</span></font>\n";			
    }
    else{			      
      htmlOutStr = htmlOutStr + "<td width=100% onClick=\"getChangedMenu('" + thisNumber + "');\" ";
      htmlOutStr = htmlOutStr + "colspan=" + (nCols-thisLevel) + " style=\"" + tablecell +  "\" " + currentBackAction + " ";   
      htmlOutStr = htmlOutStr + "style=\"background-color: " + thisBackColor + "; vertical-align: top; cursor: hand;\"><font style=" + thisTextColor + ">" + currentMouseAction + tocTab[i][1] + "</span></font>\n";			
    }
    
    htmlOutStr = htmlOutStr + "</td></tr>\n";					
  } // End of loop over the tocTab
  
  htmlOutStr = htmlOutStr + "</TABLE>\n";	
  return htmlOutStr;	
}//End of reDisplay()

/********
 //other HTML staff and onLoad functions
 **********/
function getBackground(){		
  document.getElementById('menu').style.backgroundColor = backColor;			
}
function getInitMenu(activeNumber){  	  
  document.getElementById('menuSection').innerHTML = reDisplay(activeNumber);	  
  var tempColl = document.getElementsByTagName("SPAN");
  //by default close all
  for (i=1; i<tempColl.length; i++) {    
    if(tempColl[i].id != ''){
      if(tempColl[i].id == activeNumber.substring(7,9)){
        document.getElementById(tempColl[i].id).style.display = ""; //open
      }
      else
        document.getElementById(tempColl[i].id).style.display = "none"; //closed     
    }       
  }
  //...then, this part opens up the appropriate link depending on what's been clicked
  if(activeNumber != ''){    
    thisNumArray = activeNumber.split(".");
    thisLevel = thisNumArray.length-1;   
    if(thisLevel > 0){      
      //1st level      
      theEM = eval("document.getElementById(" + thisNumArray[0] + ")");	
      if(theEM != null){        
        theEM.style.display="block";		
        theEM.expanded=true;      
      }      
      //2nd level and so on
      if(thisNumArray.length > 2){ 
        activeSubNumber = activeNumber.substring(0, activeNumber.length - 2);
        while(activeSubNumber.indexOf('.') > -1)
          activeSubNumber = activeSubNumber.replace('.','_');        
        theEM = eval("document.getElementById('" + activeSubNumber + "')");								
        theEM.style.display="block";		
        theEM.expanded=true;	
      }
    }					    
  }  
}//getInitMenu
