
function toggleAllDivisions(){
    showAll = !showAll;
    var divs = document.all.tags( "div" ).length;
    for( iDiv = 1; iDiv < divs; iDiv++ ){
      divObj = document.all( 'div' + iDiv );
      arrowObj = document.all( 'arrow' + iDiv );
      if( showAll ) {
	arrowObj.src="gif/arrowdn.gif"
	divObj.style.display="";
      } else {
	arrowObj.src="gif/arrowrt.gif"
	divObj.style.display="none";
      }
    }
    // toggle which menu item is shown
    if( showAll ){
       document.all( 'menutd2' ).style.display='';
       document.all( 'menutd3' ).style.display='none';
    } else {
       document.all( 'menutd2' ).style.display='none';
       document.all( 'menutd3' ).style.display='';
    }
}

function setAllDivisions( showDivisions ){
  // set showDivisions to true to show all,
  //                   to false to hide all
  // Each page has an additional division that is used for the menubar
    var divID, arrowID;
    var divs = document.all.tags( "div" ).length;
    for( iDiv = 1; iDiv < divs ; iDiv++ ){
	 divID = document.all( 'div' + iDiv );
	 arrowID = document.all( 'arrow' + iDiv );
         if( showDivisions ){
	     arrowID.src="gif/arrowdn.gif";
	     divID.style.display="";
	 } else {
	     arrowID.src="gif/arrowrt.gif";
	     divID.style.display="none";
	 }
    }
    showAll = showDivisions;
}

function toggleDivision( divID, arrowID ) {
	//toggle the tag with ID secID on and off
	if( divID.style.display == "none") {
		arrowID.src="gif/arrowdn.gif"
		divID.style.display="";
	} else {
		arrowID.src="gif/arrowrt.gif"
		divID.style.display="none";
	}
}

function lightOn( elementID ) {
	elementID.style.background="#FFEEBB";
	}

function lightOff( elementID ){
	elementID.style.background="";
	}
	
// Load external HTML pages, relative to directory of CHM file
// This function is for use from CHM files
function jumpToFile(fn) {
  var X, Y, sl, a, ra, link;
  ra = /:/;
  a = location.href.search(ra);
  if (a == 2)
  X = 14;
  else
  X = 7;
  sl = "\\";
  Y = location.href.lastIndexOf(sl) + 1;
  link = 'file:///' + location.href.substring(X, Y) + fn;
  location.href = link;
}

// Load external file, with path relative to directory of CHM file
// This function is for use from CHM files
function jumpToFileNewWindow( fn ) {
  var X, Y, sl, a, ra, link;
  ra = /:/;
  a = location.href.search(ra);
  if (a == 2)
  X = 14;
  else
  X = 7;
  sl = "\\";
  Y = location.href.lastIndexOf(sl) + 1;
  link = 'file:///' + location.href.substring(X, Y) + fn;
  window.open( link, 'htmlwin' );
}

// Carry out a jump to another topic, for use in menubar
function jumpTo( URL ){
	location.href=URL;
}

// secondary-window functions
function displayWindow( url, name ){
	openWindow( url, name );
}

function openWindow( url, name) { 
  	var _style = 'scrollbars,status=no,resizable,';
	if( name == 'tasks' ) {
	    _style += 'toolbar=no,';
  	    _style += 'width=400,height=600,left=10,top=10' ;
	} else if( name == 'htmlwin' ) {
	    _style += 'toolbar=yes,';
  	    _style += 'width=600,height=750,left=0,top=0' ;
	} else if( name == 'genlist' ) {
  	    _style += 'width=450,height=750,left=0,top=0' ;
        } else { // includes textwin
	    _style += 'toolbar=no,';
  	    _style += 'width=600,height=400,left=10,top=10' ;
	}
	return window.open( url, name, _style);
}

function popupTextFile ( directory, filenamestring ){
    var sampleWindow = openWindow( 'fileframeset.htm', 'textwin' );
    pause( 100 );
    showSample( sampleWindow, directory, filenamestring );
}

function popupHTMLFile( url ){
    openWindow( getPath( url ), 'htmlwin' );
}

function popupWebLink( url ){
    openWindow( url, 'htmlwin' );
}

// This function works only from HTML files, not CHM
function openPDFInNewWindow( pdf_file, pdf_named_destination ) {
    if( navigator.appName == "Microsoft Internet Explorer" ){
        openWindow( pdf_file );
    } else {
        openWindow( pdf_file + pdf_named_destination );
    }
}

function pause( length ){
	var _datestart = new Date();
	var start = _datestart.getTime();
	var _datefinish = new Date();
	var finish;
	do {
	    _datefinish = new Date();
	    finish = _datefinish.getTime();
	} while ( finish - start < length );
}

function showSample( sampleWindow, directory, filenamestring ){
    var _span;
    _span = sampleWindow.nonscrollingregion.document.all('spanDir');
    var filename;
    if( sampleWindow != null ) {
	var filearray = filenamestring.split( "|" );
	if( filearray.length == 1 ){
	    filename = getPath( directory + "\\" + filearray[0] );
	    var _form = sampleWindow.frames[0].document.forms[0];
	    _form.style.display = "none";
	    _span.innerHTML = filearray[0];
	} else {
	    // display directory name
	    _span.innerHTML = "Directory: " + directory;
	    // display file list
	    var formString = "" ;
	    formString += '<SELECT SIZE="1" NAME="fileSelect" ONCHANGE="displaySampleFile()">';
	    for ( var i = 0 ; i < filearray.length; i++ ){
	        formString += '<OPTION VALUE="../samples/' + directory + '/' + filearray[i] + '">' + filearray[i];
	    }
	    formString += "</SELECT>";
	    var _div = sampleWindow.frames[0].document.all('divForm');
	    _div.innerHTML = formString;
	    var _select = sampleWindow.frames[0].document.all('fileSelect');
	    var index = _select.selectedIndex;
	    filename = getPath( _select.options[index].value );
	}
	sampleWindow.displayFrame.location.href = filename;
    } else { 
	alert ( 'svwindow is null' );
	return;
    }
}

function tearoffCode( tearoffcounter, url ) {
	// identify the primary window
	if ( window.parent.opener != null ){
		var thiswin = window.parent.opener;
	} else if ( window.opener != null ){
		var thiswin = window.opener ;
	} else {
		var thiswin = window;
	}

	var spanID = 'span' + tearoffcounter + 'tear';
	thiswin.document.all( spanID ).style.display = "none" ;
	spanID = 'span' + tearoffcounter + 'paste';
	thiswin.document.all( spanID ).style.display = "" ;
	var divID = 'div' + tearoffcounter ;
	thiswin.document.all( divID ).style.display="none";
        var codeWindow = openWindow( url ,'codewin');
}

function inlineCode( tearoffcounter, url ){
	// identify the primary window and close the secondary window 
	if ( window.opener != null ){
		var thiswin = window.opener ;
		window.close();
	} else if ( window.parent.opener != null ){ // called from secondary window
		var thiswin = window.parent.opener;
		window.parent.close();
	} else {
		var thiswin = window;
        	var codeWindow = openWindow( url ,'codewin');
		codeWindow.close();
	}

	// add the code into the primary window
	var spanID = 'span' + tearoffcounter + 'tear';
	thiswin.document.all( spanID ).style.display = "" ;
	spanID = 'span' + tearoffcounter + 'paste';
	thiswin.document.all( spanID ).style.display = "none" ;
	var divID = 'div' + tearoffcounter ;
	thiswin.document.all( divID ).style.display="";
}

function toggleTearoffCode( secID, arrowID, url ) {
	if( secID.style.display == "none") {
		arrowID.src="gif/arrowdn.gif"
		inlineCode( secID, url );
	} else {
		arrowID.src="gif/arrowrt.gif"
                tearoffCode( secID, url );
	}
}

// functions for file window
function displaySampleFile(){
	// requires <SELECT ID=fileSelect>
	var _select = window.document.all('fileSelect');
	var i = _select.selectedIndex;
	var filename = getPath( _select.options[i].value );
	window.parent.displayFrame.location.href = filename;
}

function displaySampleDirectory( thisForm ){
	var select = thisForm.dirSelect;
	var i = select.selectedIndex;
	var filename = select.options[i].value;
	var fullname = getPath( filename );
	window.filelistFrame.location.href = fullname + "/files.txt";
	//window.displayFrame.document.open( 'text/plain', replace );
}

function getPath( filename ) {
// gets the proper path for an external file fn, but
// does not open it.
// -- typically used with openWindow to show a file 
// in an external window.

// location.href contains the location of the current file
// 
  // alert( " filename : " +  filename  );
  // alert ( location.href);
  var colonpos, link;
  var ra = /:/;
  var colonpos = location.href.search(ra);
  // alert ( "colonpos: " + a );
  var slash = "\\";
  var slashpos = location.href.lastIndexOf( slash ) + 1;
  // alert ( "Slashpos: " + slashpos );
  if (colonpos == 2){
    colonpos = 14;
  } else {
    colonpos = 7;
  }
  link = 'file:///' + location.href.substring(colonpos, slashpos ) +  filename ;
  // alert ("Link: " + link) ;
  return link;
}

