//#######################################################################
// Variable that is used to store the current scroll position of a window
//#######################################################################
var yoffset = 0;

//###########################################################
// The Tree object constructor takes the name of a variable
// to which the tree object is assigned (for use in event 
// handling) and a unique id value (to differentiate one tree
// from another)
//###########################################################
function Tree(variable, id, appname) {
 this.appname = appname;
 this.variable = variable	;		// The name of a variable by which the tree can be referenced
 this.id = id					;	// A unique ID for the tree
 this.nodeID = 0				;	// Used to give each node of the tree a unique ID
 this.nodes = new Array()		;	// An array consisting of the root nodes of the tree
 this.openedMarker = "-  "		;	// String used to indicate that a node has been opened
 this.closedMarker = "+  "		;	// String used to indicate that a node has been closed
 this.leafMarker = "*  "		;	// String used to indicate a leaf of the tree
 this.addNode = Tree_addNode	;	// Add a root node to the tree
 this.display = Tree_display	;	// Display the tree
 this.loadState = Tree_loadState;	// Load the state of the tree from cookies
 this.saveState = Tree_saveState;	// Save the state of the tree using cookies
 this.getState = Tree_getState	;	// Get a string that contains the current state of each tree node
 this.setState = Tree_setState	;	// Set the state of each tree node from a string
 this.getNodeID = Tree_getNodeID	;// Returns a new node ID
 this.getNode = Tree_getNode		;// Returns the node corresponding to a specific ID
 this.findNodeUseName = Tree_findNodeUseName;
 this.setMarkers = Tree_setMarkers	;// Sets the opened, closed, and leaf markers of the tree
 this.forceOpenParent = Tree_forceOpenParent;
}

//#############################
// Add a root node to the tree
//#############################
function Tree_addNode(node) {
 this.nodes[this.nodes.length] = node;
}

//#############################
// Display the tree object
//#############################
function Tree_display(tree_state) {
   //alert("Tree_display" + tree_state);
 for(var i=0; i<this.nodes.length; ++i) 
 {
    //alert(tree_state);
   this.nodes[i].display(tree_state);
 }
}


//############################################################################
// PARSE THE WINDOW LOCATION TO SEE IF THE HI_LIGHT: STRING IS FOUND
// IF IT IS, YOU WILL NEED TO RETURN THE STRING THAT FOLLOWS IT.  THAT STRING
// IS USED TO IDENTIFY THE LEFT NAV ITEMS IDENTIFIER.
//############################################################################
 function onLoad_hilight(a) {
    
	var args = "" + a;
	var args_length = args.length;	
	var find_string = "HI_LIGHT:";
	var find_string_length = find_string.length;
	var stop_searching_at = args_length - find_string_length;
	var i=0;
	var j=0;
	
	//alert("J is " + j + "args_length" + args_length + "find_string_length" + find_string_length);
	for(j=0; j< stop_searching_at ;j++) {
	    if(args.substring(j,j+find_string_length)==find_string)
	    {
	        break;
	    }
	}
	for(i=j; i<args_length  ;i++) {
	    if(args.substring(i,1) == ",")
	    {
	        break;
	    }
	}
	
	var hi_light_length = i - j - find_string_length ; // i - starts HI_LIGHT:, j - finds comma, -1 for the comma!
	var start_at = j + find_string_length;
	var end_at = start_at + hi_light_length - 1;
	var id = args.substring(start_at, end_at);

	return id;
} 



//#########################################
// Load the state of the tree from cookies
// THIS IS THE MAIN ROUTINE!!!!!!!!!!!!!!!
//#########################################
function Tree_loadState(args) {
    
 //alert(this.getState() + " LOAD NEW STATE" );
 
 
 var s = getCookie("yoffset");
 if(s != null) yoffset = s;
 

//alert("args is " + args + "cookie is " + s);
//if ((args.length > 0) && ((s == null) || (s=="")))
//{
//alert("USING ARGS");

s = args;  // JT NEW 

//}
//else
//{
//  alert("this.id in loadstate" + this.id);
// s = getCookie(this.id)
//}
//alert(s + "Tree_loadState");
 
// THIS IS WHERE WE'RE GETTING THE STATE OF THE HI-LIGHTED ITEM, AND MAKING SURE THE PARENT NODE OF THE HILIGHT IS SET
//#####################################################################################################################

var forceopenname = onLoad_hilight(args);
 sl = getCookie("LeftNavBeanCookie");
 if (sl != null) {
   //alert("A:SL LENGTH" + sl.length + "   COOKIE IS " + sl);
 
//this.forceOpenParent(this.findNodeUseName(forceopenname));

var a;
var b;
a = this.findNodeUseName(forceopenname).forceOpenName;
//alert("FON" + forceopenname);
if ((a != null) && (a != ""))
{
    
  b = this.findNodeUseName(a).id;
  if ((b != null) || (b != ""))
  { //alert("FORCING PARENT" + b);
    this.forceOpenParent( b);
  }
}







 }
 var i;
 var j;
 
 
 
 // SET THE STATE OF THE MENU
 //##########################
 
 sl = getCookie("LeftNavBeanCookie");
 if(sl != null && sl!="") {
 //alert("B:SL LENGTH" + sl.length + "   COOKIE IS " + sl);
    //alert (sl);
    this.setState(sl);
 }  
 else if(s != null && s!="") {
    this.setState(s);
 } 
  //alert (args);
 // setCookie("LeftNavBeanCookie", this.getState(), this.appname);
 
 
//alert("# of nodes is " + i);
//for (j=0; j<i; j++)
//  alert("# " + j + " of nodes is " + this.nodes[j].length);
}  // END OF THE LOAD TREE FUNCTION


//##########################################
// Save the state of the tree using cookies
//##########################################
function Tree_saveState() {
 setCookie(this.id, this.getState(), this.appname);
 setCookie("yoffset", yoffset, this.appname);
 //alert(this.getState());
 //alert(this.getState() + " NEW STATE1 - " + this.getState().length);
 setCookie("LeftNavBeanCookie", this.getState(), this.appname  );
 //alert(this.id + "Tree_saveState" + this.getState());
}

//###############################################################
// Get a string that contains the current state of each tree node
//###############################################################
function Tree_getState() {
 var s = "";
 for(var i=0; i<this.nodes.length; ++i) 
  s += this.nodes[i].getState();
 // alert("Tree_getState" + s);
 return s;
}

//##############################################
// Set the state of each tree node from a string
//###############################################
function Tree_setState(s) {
 //alert("REAL tree setState " + s);
 for(var i=0; i<this.nodes.length; ++i) {
  s = this.nodes[i].setState(s);
  }
 //alert("New Tree State is " + s);
}

//#######################
// Returns a new node ID
//#######################
function Tree_getNodeID() {
 return this.nodeID++;
}

//################################################
// Returns the node corresponding to a specific ID
//################################################
function Tree_getNode(id) {
 for(var i=0; i<this.nodes.length; ++i) {
  var node = this.nodes[i].findNode(id);
  if(node != null) return node;
 }
 return null;
}



function Tree_findNodeUseName(fon) {
    //alert("JT" + fon);
 for(var i=0; i<this.nodes.length; ++i) {
  var node = this.nodes[i].findNodeUseName(fon);
  if(node != null) return node;
 }
 return -1;
}

//################################################
// Handles the clicking of a tree's marker
//################################################
function Tree_stateChange(tree, nodeID) {
 // Get scroll position
 if(navigator.appName == "Netscape") 
   yoffset = window.pageYOffset;
 else
   yoffset = window.document.body.scrollTop;
   
 //alert("Tree_stateChange yoffset" + yoffset);
 

 node = tree.getNode(nodeID);
 
 //alert("Tree_stateChange node" + node + " length " + node.nodes.length);
 
 // Change the opened/closed state of the node
 if(node != null && node.nodes.length > 0) {
    node.changeState();
  }
}

//######################################################
// Sets the opened, closed, and leaf markers of the tree
//######################################################
function Tree_setMarkers(opened, closed, leaf) {
 this.openedMarker = opened;
 this.closedMarker = closed;
 this.leafMarker = leaf;
}









//###############################
// Removes any blanks in a string
//###############################
function removeBlanks(s) {
 var temp = "";
 for(var i=0; i<s.length; ++i) {
  var c = s.charAt(i);
  if(c != " ") temp += c;
 }
 return temp;
}

//#####################################################################
// The Node object represents a node of a tree
// It is defined using the forceopenname, selectedname,
// name (text) of the node, a hypertext link,
// a target window or frame, and a reference to the tree in which the 
// node is contained
//#####################################################################
function Node(appname, forceopenname, selectedname, name, link, target, tree) {
 this.appname = appname;
 this.forceOpenName = forceopenname;  // The force open name is the name of the parent node which must be forced open
                                      // should this node be chosen
 this.selectedName = selectedname;    // The selected name is the name of the node that is currently hilighted.
                                      // This is used as the base name of the node to toggle the gif files
 this.name = name					;// Text of the node
 this.hi_light_string = ",HI_LIGHT:" + this.selectedName + ",";  //The selection on the command line
 this.link = link		;			// Hypertext link
 this.target = target		;		// Window or frame name
 this.tree = tree				;	// Reference to containing tree
 this.nodes = new Array()		;	// Array of sub-nodes
 this.opened = false			;	// Is the node expanded
 this.id = tree.getNodeID()		;	// A unique ID within the tree
 this.addNode = Node_addNode	;	// Add a sub-node
 this.display = Node_display	;	// Display the node
 this.open = Node_open			;	// Expand the node
 this.close = Node_close		;	// Close the node
 this.changeState = Node_changeState;	// Change the expanded state of the node
 this.findNode = Node_findNode		;// Returns a node with a specified ID
 this.getState = Node_getState		;// Returns a string that describes the expanded state of the node & its sub-nodes
 this.setState = Node_setState		;// Sets the expanded state of a node and its sub-nodes
 this.displayNode = Node_displayNode;
 this.findNodeUseName = Node_findNodeUseName;
}

//################
// Add a sub-node
//################
function Node_addNode(node) {
 this.nodes[this.nodes.length] = node;
}

//#############################################
// Write the anchor tags associated with a node
//#############################################
function writeAnchor(treeref, id, marker, link, target, name) {
var dw;
dw ='<table border=0 columns=1 cellspacing=0 cellpadding=0><tr><td  align="right" nowrap><a href="javascript:Tree_stateChange('+treeref+","+id+')" class="treemarker">'+marker+'</a>'+'<a href="'+link+'"';
//document.write(dw);
if(target != "") {
  //document.write(' target="'+this.target+'"')
}
dw+=' class="treenode">'+name+'</a></td></tr></table>';
document.write(dw);
//alert(dw);
}

//###################
// Display the node
//###################
function Node_display(tree_state) {

hi_light_string = ",HI_LIGHT:" + this.name + ",";

if(this.nodes.length == 0) {
    //#######
    // LINKS
    //#######
    if (this.name == this.selectedName)
    {
        this.name = "<img src=/legacy/LeftNav/" +this.name+ "_on.gif border=0>";       
        setCookie("LeftNavHiLightBeanCookie",",HI_LIGHT:" + this.selectedName + ",", this.appname);
        // THIS IS WHERE WE FORCE THE PARENT OF THIS NODE TO BE ON!0
        //forceOpenParent(this.findNodeUseName(this.name));

          
        
        
    }
    else
    {
        this.name = "<img src=/legacy/LeftNav/" +this.name+ "_off.gif border=0>";
    }


    
    //PREVIOUS CODE
    //if (this.name == "organizer/organizer") {
    //    this.name = "<img src=/legacy/LeftNav/"+this.name+"_off.gif border=0>"
    //}
    //else
    //{
    //    this.name = "<img src=/legacy/LeftNav/"+this.name+"_off.gif border=0>"
    // }


    if (this.name == "organizer/organizer")
      writeAnchor(this.tree.variable, this.id, "", this.link, this.target, this.name);
    else 
      writeAnchor(this.tree.variable, this.id, "", this.link+tree_state+hi_light_string, this.target, this.name);
    
    //writeAnchor(this.tree.variable, this.id, this.tree.leafMarker, this.link, this.target, this.name)
    //writeAnchor(this.tree.variable, this.id, this.tree.leafMarker, this.link, this.target, this.name)
} else if(this.opened) {
    //############
    // MENU NODES
    //############
    this.name = '<img src=/legacy/LeftNav/'+this.name+'_open.gif border=0>';
    writeAnchor(this.tree.variable, this.id, this.name, this.link+tree_state+hi_light_string, this.target, "");
    //writeAnchor(this.tree.variable, this.id, this.tree.openedMarker, this.link, this.target, this.name)
    for(var i = 0; i < this.nodes.length; ++i) {
        this.nodes[i].display(tree_state);
  }
} else{
    //############
    // MENU NODES
    //############
    this.name = '<img src=/legacy/LeftNav/'+this.name+'_closed.gif border=0>';
    writeAnchor(this.tree.variable, this.id, this.name, this.link+tree_state+hi_light_string, this.target, "");
    //writeAnchor(this.tree.variable, this.id, this.tree.closedMarker, this.link, this.target, this.name)
}

}

//#########################################################
// getBaseLocation returns the base location of the window
//#########################################################
function getBaseLocation() {
    var str = "";
    var l = "" + window.location;
    //alert("L" + l.length);
    for (i=0; i<l.length; i++)
    {
        str = str + l.substring(i,i+1);
        //alert(str);
        if (l.substring(i,i+1) == "?")
          break;
    }

    if (i >= l.length) {
        // check to see if there is a ? at the end of the string.  If there is not, then add it!
        if (l.substring(l.length-1,l.length) != "?")
          str = str + "?";
    }
    // alert (l.substring(l.length-1,l.length) + " STR " + str);
	return str;
}

//#################
// Expand the node
//#################
function Node_open() {
    //alert("Node_open");
 this.opened = true;
 this.tree.saveState();
 var urls = window.location.href.split("#",1);
//alert("TREE STATE Node_open" + this.tree.getState());
 //alert(this.getState() + " NEW STATE2 - " + this.getState().length);
  setCookie("LeftNavBeanCookie", this.tree.getState(), this.appname);
 
 var new_location = getBaseLocation() + this.tree.getState() + this.hi_light_string;
 
//alert(new_location);
 
//window.location=new_location; // JT NEW 
 // this.tree.setState("");
 // JT NEW this.tree.saveState();
window.location.reload()
}

//#################
// Close the node
//#################
function Node_close() {
 //   alert("Node_close");
 this.opened = false;
 this.tree.saveState();
 //alert("TREE STATE Node_close" + this.tree.getState());
 //this.tree.setState("");
 
 //alert(this.getState() + " NEW STATE3 - " + this.getState().length);
  setCookie("LeftNavBeanCookie", this.tree.getState(), this.appname);
 var new_location = getBaseLocation() + this.tree.getState() + this.hi_light_string;
 
 //alert(new_location);
 
//window.location=new_location; //JT NEW 
 
 // JT NEW this.tree.saveState();
window.location.reload()  // JOHN - I want to figure out how to get back to doing this!
}

//#######################################
// Change the expanded state of the node
//#######################################
function Node_changeState() {
 if(this.opened) this.close();
 else this.open();
}

//#######################################
// Returns a node with a specified ID
//#######################################
function Node_findNode(id) {
 if(this.id == id) return this;
 for(var i=0; i<this.nodes.length; ++i) {
  var node = this.nodes[i].findNode(id);
  if(node != null) return node;
 }
 return null;
}


function Node_findNodeUseName(fon) {
 if(this.name == fon) return this;
 for(var i=0; i<this.nodes.length; ++i) {
  var node = this.nodes[i].findNodeUseName(fon);
  if(node != null) return node;
 }
 return null;
}
//###############################################################################
// Returns a string that describes the expanded state of the node & its sub-nodes
//###############################################################################
function Node_getState() {
 var s = "";
 if(this.opened) s += "1";
 else s += "0";
 
 for(var i=0; i<this.nodes.length; ++i)
  s += this.nodes[i].getState() ;
 //alert("node state s" + s);
 return s;
}

//###############################################################################
// Sets the expanded state of a node and its sub-nodes
//###############################################################################
function Node_setState(s) {
 if(s == null || s == "") return;
 var bit = s.substring(0,1);
 if(bit == "1") this.opened = true;
 else this.opened = false;
 s = s.substring(1);
 for(var i=0; i<this.nodes.length; ++i)
 {
   s = this.nodes[i].setState(s);
   
   
 }
 //alert("Node setState 2" + s + "Current Tree State is " + this.tree.getState());
// this.tree.saveState(); JOHN - save the state to the cookie
 return s;
}

//###############################################################################
// FOR TESTING PURPOSES!
//###############################################################################
function Node_displayNode() {
 alert ( this.forceOpenName + " =  forceOpenName; \n" + 
     this.selectedName + " =  selectedname; \n" + 
	 this.name + " =  name					\n" + 
	this.hi_light_string + " =  hi_light_string\n" + 
	this.link + " =  link					\n" + 
	this.target + " =  target				\n" + 
	this.tree + " =  tree					\n" + 
	this.nodes + " =  nodes			\n" + 
	this.opened + " =  opened				\n" + 
	this.id + " =  id			\n"   );

//	this.addNode + " =  Node_addNode		\n" + 
//	this.display + " =  Node_display		\n" + 
//	this.open + " =  Node_open				\n" + 
//	this.close + " =  Node_close			\n" + 
//	this.changeState + " =  Node_changeState	\n" + 
//	this.findNode + " =  Node_findNode		\n" + 
//	this.getState + " =  Node_getState		\n" + 
//	this.setState + " Node_setState		\n"
}




var caution = false

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments

function setCookie(name, value, path) {
    var secure;
    var expires = new Date ();
    var expiredays = 7;
var curCookie = name + "=" + escape(value) +
((path) ? "; path=" + path : "") + ";";
document.cookie = curCookie;

//alert (curCookie + " JOHN " + document.cookie);
}


 


function getCookie(name) {
var prefix = name + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
  return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
if (cookieEndIndex == -1)
  cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path) {
if (getCookie(name)) {
  document.cookie = name + "=" +   ((path) ? "; path=" + path : "") +  "; expires=Thu, 01-Jan-70 00:00:01 GMT"
}
}


function Tree_forceOpenParent(numberToOpen) {

               var oldcookie;
        //alert ("in fop");
        if (numberToOpen == -1) {
            // This means that there was nothing to force open!  alert("NTO = -1");
            return;
        }
        
        oldcookie = getCookie("LeftNavBeanCookie");
        if ((oldcookie != null) && (oldcookie != ""))
        {
            //The position of the parent tree is this.forceOpenName
            var g = 0;
            g = parseInt(numberToOpen) - 1;
            
            var h;
            h = parseInt(numberToOpen) + 1;
            
            
            
            var newcookie;
            if (numberToOpen == 0) {
              newcookie = "1" + oldcookie.substring(1,oldcookie.length);
            }
            else
            { 
              newcookie = oldcookie.substring(0,numberToOpen) + "1" + oldcookie.substring(h,oldcookie.length);
            }  
 //alert(newcookie + " NEW STATE4 - " + newcookie.length);
            setCookie("LeftNavBeanCookie", newcookie, "/legacy");
            //alert(newcookie);
        }
        
        //alert("# to OPEN" + numberToOpen + "\n" + "INSIDE FORCE OLD:" + oldcookie + "\n" + "INSIDE FORCE NEW:" + newcookie);
        return ;
}