/***************************************************************************
*  This is xmltree DHTML Tree Generator.  
****************************************************************************/

/* This array contains a "hash" stating which list items that are of class
*  "collapse" are actually collapsed or not
*/
var xmltree_isCollapsed = new Array();
var xmltree_grove= new Array();
var xmltree_isFirstTime = true;
var xmltree_isBreadCrumb = false;
var xmltree_mozilla_ok = false;
var xmltree_currentURI;
var current_xmltree_depth = 1;
var MAX_XMLTREE_VISIBLE_DEPTH = 2;
var nav_index = 1;
var HM_Array = new Array();
var popupItemIsCurrentURI = false;

function debugalert(s) {
	if (debug) alert(s);
}

function init_xmltree_isCollapsed() {
	var colon_split= new Array();
	var equal_split= new Array();
	colon_split=document.cookie.split(";");
	for (i=0; i<colon_split.length; i++){
		equal_split=colon_split[i].split("=");
		if (equal_split[0].substr(0,1) == " ") {
			equal_split[0]=equal_split[0].substr(1,
				equal_split[0].length);
		}

		if (equal_split[0].substr(0,12) == "xmltreemenu_") {
			var name = equal_split[0].substr(12, 
				     equal_split[0].length);
			var value = equal_split[1];

			xmltree_isCollapsed[name]=(value=="closed");

		} 
	}
}

function url2uri(url){

	var i;
	var uri;

	uri=url;

	for (i=1; i<=3; i++) {
		uri=uri.substr(uri.indexOf("/")+1, uri.length);
	}
	if (uri.indexOf("index.html") != -1)
	{
		uri=uri.substring(0, uri.lastIndexOf("index.html"));
	}	

	return "/" + uri;

}


/***************************************************************************
*  initial function responsible for parsing the XML Tree Structure.  
****************************************************************************/
function parse(divname, attributes_js) {

        var menu_xml;
        var i;
	var s;

	xmltree_currentURI = url2uri(document.location.toString());

	if (attributes_js) {
		eval(attributes_js);
	}

	// if (!xmltree_capable_browser()) return;

	if (xmltree_isFirstTime) {
		init_xmltree_isCollapsed();
		xmltree_isFirstTime=false;
	}

	if (!divname) {
		// this is odd ... that means there is no divname. Exit.
		return;
	}
	menu_id=(!document.getElementById) ? document.all(divname) : 
		document.getElementById(divname);
        if (menu_id) {
                menu_xml=menu_id.innerHTML;
        } else {
                return;
        }

	if (is.ie) {
	menu_xml=html_to_xhtml(menu_xml);
	}

        xmltree_grove[divname]= Xparse(menu_xml);
		
        s=maketree(xmltree_grove[divname], divname);
	if (s) {
		if (is.nav5up && !xmltree_mozilla_ok) 
			wdvlObjs[divname].objHide();
		wdvlObjs[divname].replace_html(s);
		if (is.nav5up && !xmltree_mozilla_ok) 
			setTimeout('wdvlObjs.'+ divname +'.objShow();', 200);
		wdvlObjs[divname].objShow();
        } /* else {
                window.alert("The tree that you have generated is not valid");
        } */

	
}

function redisplay(divname) {
	/* Hack: Mozilla does not like redrawing the list-images unless we 
	*  resize the menu container
	*/
	wdvlSetWindowWidth(wdvlWindowWidth()-1);
        wdvlSetWindowWidth(wdvlWindowWidth()+1);

}

function xmlmenu_open(id, menudisplay) {
	xmltree_isCollapsed[id]=false;
	if (is.nav5up &&  !xmltree_mozilla_ok) 
		wdvlObjs[menudisplay].objHide();
	wdvlObjs[menudisplay].replace_html(maketree(xmltree_grove[menudisplay], 
			menudisplay));
	if (is.nav5up  &&  !xmltree_mozilla_ok)
		setTimeout('wdvlObjs.'+ menudisplay +'.objShow();', 200);

	//SetCookie("xmltreemenu_" + id, "open");
}

function xmlmenu_close(id, menudisplay) {
	xmltree_isCollapsed[id]=true;
	if (is.nav5up  &&  !xmltree_mozilla_ok) 
		wdvlObjs[menudisplay].objHide();
	wdvlObjs[menudisplay].replace_html(maketree(xmltree_grove[menudisplay],
			menudisplay));
	if (is.nav5up  &&  !xmltree_mozilla_ok) 
		setTimeout('wdvlObjs.'+ menudisplay +'.objShow();', 200);
	//SetCookie("xmltreemenu_" + id, "closed");
}

function extract_link(tag) {

	var i;
	for (i=0; i<tag.contents.length; i++){
	
		if (tag.contents[i].type == "element" &&
		    tag.contents[i].name == "a") {
				return tag.contents[i].attributes["href"];
		}
	}
	return "";
}

function makePopupMenuArray (tag, id) {
	popupItemIsCurrentURI = false;
	if (!tag) {
		debugalert(tag + " is not a tag");
		return false;
	} else if (tag.name != "ul" ) {
		debugalert("Warning: makePopupMenuArray() needs a "+
                        "<ul> tag as a parameter");
		return false;
	}
	var popupMenuArray = new Array();
	popupMenuArray[0] = [200,											// menu width
						 155,											// left_position
						"HM_f_PlaceAtElement('" + id + "','top')",      // top_position
						"#565546",										// font_color
						"#DE8805",										// mouseover_font_color
						"#E5E5DC",										// background_color
						"#F5F5F1",										// mouseover_background_color
						"#FFFFFF",										// border_color
						"#FFFFFF",										// separator_color
						0,												// top_is_permanent
						0,												// top_is_horizontal
						0,												// tree_is_horizontal
						1,												// position_under
						1,												// top_more_images_visible
						1,												// tree_more_images_visible
						"null",											// evaluate_upon_tree_show
						"null",											// evaluate_upon_tree_hide
						,												// right-to-left
						]
    var nextIndex = 1;
	for (var tagIndex=0; tagIndex < tag.contents.length; tagIndex++) {
		if (tag.contents[tagIndex].type=="element" && tag.contents[tagIndex].name == "li") {
			var li_tag = tag.contents[tagIndex];
			var extracted_link=extract_link(li_tag);
			var text_in_link=strip_html(return_html( li_tag.contents[0]));
			popupMenuArray[nextIndex++] = [text_in_link,extracted_link,1,0,0]
			if (extracted_link == xmltree_currentURI &&
				current_xmltree_depth == MAX_XMLTREE_VISIBLE_DEPTH)
				popupItemIsCurrentURI = true;				
		}
	}
	//alert(popupMenuArray.toString().replace(/,/g, ", "));
	return popupMenuArray;
}

function maketree (tag, menudisplay) {
	var i;
	var s="";

	if (!tag) {
                debugalert(tag + " is not a tag");
                return false;
        } else if (tag.name == "ROOT" ) {
                return maketree(first_subtag_element(tag), menudisplay);
        } else if (tag.name != "ul" ) {
                debugalert("Warning: maketree() needs a "+
                        "<ul> tag as a parameter");
                return false;
        } 
	
	s+="<ul>";

	/* Iterate through all li tags */
	for (i=0; i<tag.contents.length; i++) {
                if (tag.contents[i].type=="element" 
			&& tag.contents[i].name == "li") {
			var li_tag = tag.contents[i];
		 	/* if the li tag has a class of "collapse" and 
			*  xmltree_isCollapsed(id) != true then don't show it's
			*  contents
			*/
			var extracted_link=extract_link(li_tag);

			
			var text_in_link="";

			var a_index;
			for(a_index=0; li_tag.contents[a_index].type=='chardata'; a_index++);



			  text_in_link =strip_html(
				return_html( li_tag.contents[a_index])); //.contents[0]));

			var onMouseOverOutString = "";
			var navLinkClass = "";

			// In this case, the navigation item will have a popup menu 
			// containing its children
			if ((li_tag.attributes["class"] == "collapse" &&
				xmltree_currentURI.indexOf(extracted_link) != 0) ||
				(current_xmltree_depth == MAX_XMLTREE_VISIBLE_DEPTH &&
				 (li_tag.attributes["class"] == "collapse" ||
				  li_tag.attributes["class"] == "collapse_force_open"))) {
				// Create a popup menu with the children as items.
				// Won't someone please think of the children!
				var id = li_tag.attributes["id"];
				onMouseOverOutString = ' name="' + id + '" id="' + id + '" onMouseOver=\"HM_f_PopUp(\'elMenu' + nav_index + '\', event); return false;\" onMouseOut=\"HM_f_PopDown(\'elMenu' + nav_index + '\'); return false;\"';
				HM_Array[nav_index] = makePopupMenuArray(return_subtag("ul", li_tag), id);
				if (popupItemIsCurrentURI == true) {
					navLinkClass = ' class=\"navSelectedLink\" ';
				}
				nav_index++;
			}

			if (li_tag.attributes["class"] == "collapse" ||
				extracted_link!="") {
				
				if (current_xmltree_depth == 1)
					s+='<div class=\"navdiv\">';
			
				var id = li_tag.attributes["id"];

				/* If this part of the tree is visible */
				if (xmltree_isCollapsed[li_tag.attributes.id]
					==false
				  || (xmltree_isBreadCrumb && 
				      xmltree_currentURI.indexOf(
					extracted_link) == 0 )
				  || (li_tag.attributes["class"] == 
					"collapse_forced_open")) {

				   //
				   // if it is a breadcrumb, just display the
				   // contents of the <li> tag.  
				   //
				   // If not, then make it a link with 
				   // javascript in an onclick event
				   // to collapse the branch of the tree if
				   // the link is clicked.  
				   //

				   if ( xmltree_isBreadCrumb &&
					(extracted_link!="" ||
					 li_tag.attributes["class"] ==
                                        "collapse_forced_open")) {

				      if (xmltree_currentURI == 
						extracted_link ) {
					  if (li_tag.attributes["class"]
						!= "collapse"  &&
					      li_tag.attributes["class"] != 
						"collapse_forced_open")  {

				              s+='<li class="collapse">';
					  } else {
				              s+='<li class="collapse">';
					  }

				      } else if (!li_tag.attributes["class"]) {
				          s+='<li class="collapse">';
				      } else {
				          s+='<li class="collapse">';
				      }



					if (extracted_link != 
						xmltree_currentURI ) {

					   s+='<a class=\"menu\" href=\"' +
					      extracted_link + 
					      '\"' + onMouseOverOutString + '>' + '<span' + navLinkClass + '>' +
					      text_in_link +
					      '</span></a></li>';

					} else { 

					   s+='<b class="chosenpage"' + onMouseOverOutString + '><span class=\"chosenpage\">' +
					      text_in_link +
					      '</span></b></li>';

					}
				   } else if (li_tag.attribute && 
					(li_tag.attribute["class"] == 
					"collapse" || li_tag.attribute["class"]
					== "collapse_forced_open")) {
				      s+='<li class="collapse">' + 
					'<a class=\"menu\" href="#" '+
					'onclick="xmlmenu_close(\''+
					id +'\', \'' + menudisplay +
                                        '\'); return false;' +
					 '">' +  li_tag.contents[0].value + 
					'</a></li>';
				   } else {
				      s+='<li class="collapse">' + 
					'<a class=\"menu\" href="#" '+
					'onclick="xmlmenu_close(\''+
					id +'\', \'' + menudisplay +
                                        '\'); return false;' +
					 '">' +  li_tag.contents[0].value + 
					'</a></li>';
				   }


				   if ((li_tag.attributes["class"] == "collapse"
				   ||  li_tag.attributes["class"]
                                        == "collapse_forced_open")
					   && current_xmltree_depth < MAX_XMLTREE_VISIBLE_DEPTH ) {
					current_xmltree_depth++;	
					s+=maketree(return_subtag("ul", li_tag),
					/* first_subtag_element(li_tag), */
						menudisplay); 
					current_xmltree_depth--;
				   }
				} 
				/* If this part of the tree is not known to 
				*  be hidden or not hidden, assume it is visible
				*/
				else if (
				   !xmltree_isCollapsed[li_tag.attributes.id] 
				   || extracted_link!=""
				   || li_tag.attributes["class"] == 
					"collapse_forced_open" ) {
				
				   // 
				   // If this is a breadcrumb, then make this
				   // a link to the URI inside the "href" 
				   // attribute of the <li> tag (if there is
				   // one.  Otherwise, put in HTML to open
				   // this branch of the tree.
				   //
				   if (xmltree_isBreadCrumb && 
					text_in_link!="" &&
					extracted_link!="") 

				      if (li_tag.attributes["class"]) {
				      s+='<li class="collapse">' + 
					'<a class=\"menu\" href="' +
					extracted_link +
					 '"' + onMouseOverOutString + '><span' +  navLinkClass + '>' + 
						text_in_link + 
						'</span></a></li>';
				      } else {
				        s+='<li class="collapse">' + 
					'<a class=\"menu\" href="' +
					extracted_link +
					 '"' + onMouseOverOutString + '><span' + navLinkClass + '>' + 
						text_in_link + 
						'</span></a></li>';
				      } 
			
			

				   } else {

				      s+='<li class="collapse">' + 
					'<a class=\"menu\" href="#" onclick="xmlmenu_open(\''+
					 id +'\', \'' + menudisplay + 
					'\'); return false;' +
					 '">' +  li_tag.contents[0].value + 
					'</a></li>';

				   }

				
				   if (xmltree_isCollapsed[li_tag.attributes.id]
					!= true) {
				     /* This means that the xmltree_isCollapsed entry
				     *  has not been indexed ... so index it
				     *  now.
				     */
				     xmltree_isCollapsed[li_tag.attributes.id]=true;
				   } 

				/* If this branch is not visible */
			 	} else {
				   s+='<li class="collapse">' +
                                      '<a class=\"menu\" href="#" onclick="xmlmenu_open(\''+
                                       id +'\', \'' + menudisplay +
                                      '\'); return false;' +
                                      '">' +  li_tag.contents[0].value + 
				      '</a></li>';
				}
				if (current_xmltree_depth == 1)
					s+='</div>';
			} else if (tag.contents[i].type != "chardata"){
				if (return_html(li_tag) != false)
					s+=return_html(li_tag);
			}
		
        }
		if (current_xmltree_depth == 1)
			s+="<div class=\"navdiv\"><li style=\"list-style-type: none;\" /></div>";

		s+="</ul>";

	/* alert(show_props(xmltree_isCollapsed, 'i')); */
        return s.replace(/false|true/g, '');
}

function xmltree_capable_browser() {
	return !!(W3Cdom && getBody().innerHTML) ;
}

/* Method added to check for numbers, sidenav thinks anything after equal sign
   without quotes is a class.  Even escaping quotes doesn't work so this will
   allow for numeric parameters - if other non-numeric parameters are necessary, 
   another fix will need to be put added.
   May 19, 2004 - Sandy Law
*/
function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

function html_to_xhtml(s) {
	var r="";
	var subs;
	var i;
	var in_li=new Array();
	var li_index = 0;

	in_li[0] = false;

	for (i=0; i<s.length; i++) {
		subs=s.substring(i, i+1);
		/* If a number is right after the equal sign, do not treat as a class - see comment
		above for function IsNumeric */
		if (subs == "=" && s.substring(i+1, i+2) != '"' && !IsNumeric(s.substring(i+1, i+2))){
			r+='="';
			i++;
			subs=s.substring(i, i+1);
			while(subs!=" " && subs!=">" && i<s.length) {
				r+=subs
				i++;
				subs=s.substring(i, i+1);
			}
			r+='"'+subs;
		} else if (subs == "<") {

			var space_index=s.substring(i).indexOf(' ');
			var end_angle_index=s.substring(i).indexOf('>');
			if (space_index < 0) space_index=2000;
			if (end_angle_index < 0) end_angle_index=2000;
			
			/* alert(space_index);
			alert(end_angle_index); */

			var token_index=Math.min(space_index, end_angle_index);

			var tag_name=s.substring(i+1, i + token_index).toLowerCase();
			if (tag_name=="ul" || tag_name=="ol") {
				li_index ++;
				in_li[li_index]=false;
			} else if (tag_name == "/ul" || tag_name == "/ol") {
				li_index --;
			} else if (tag_name=="li") {
				/* this tag is not closed ... so close it 
				*  dammit! */
				if (in_li[li_index]) {
					r += "</li>";
				}
				in_li[li_index] = true;
			} else if (tag_name=="/li") {
				in_li[li_index] = false;
			}

			subs=s.substring(i, i+1);
			for (; subs!=' ' && subs!='>'; 
			     i++, subs=s.substring(i, i+1)) {
				r+=subs.toLowerCase();
			}
			r+=subs;
			
		} else {
			r+=subs;
		}
	}
	return r;
}

function escape_html (s) {

	var r="<pre>\n";

	for (var i = 0 ; i<s.length; i++) {

		if (s.substring(i, i+1) == "<") {
			r+="&lt;";
		} else if (s.substring(i,i+1) == ">") {
			r+="&gt;";
		} else {
			r+=s.substring(i,i+1);
		}
	} 
	r += "</pre>\n";
	return r;
}

function hide_div(divname) {
document.write('<style type="text/css">\n');
document.write('#' + divname + ' { visibility: hidden; }\n');
document.write('</style>\n');
}

hide_div('menu');
