//////////////////////////////////////////////////////////////////////////////
//                                                                          //
// Script name  : IE4TreeMenu (javascript version 1.2)                      //
// Version      : 1.0                                                       //
// Description  : Script for generating Explorer-like tree menu, which can  //
//                be interactively expanded and collapsed. It also can be   //
//                used for drawing multiple tree menus in a single page.    //
//                Runs only on MSIE version 4 or above.                     //
// License      : Freeware. This script can be used and redistributed for   //
//                free as long as this header remains the same and not used //
//                for commercial use.                                       //
//                                                                          //
// Written by   : Elfan Nofiari <el@fan.net>                                //
//                (http://elfan.tim.informatika.org)                        //
//                                                                          //
// Date         : Nov 5, 1999                                               //
//                                                                          //
// Required file: 1. ie4treemenu.js  (main file)                            //
//                2. structure.js    (example structure file)               //
//                3. some image files                                       //
//                                                                          //
// Send comments and bug reports to email address above.                    //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////


// customizable variables
var supportNonIE4Browser = true;	// attention : not all non IE4 browsers supported

var reserveLastSetting = false;		// If 'false' then default setting will be loaded each time
					// after drawing a tree menu. This is very useful when you
					// skip some settings and want them to be default for
					// multiple treemenus.
					// If 'true' then last settings will be used when you skip
						// some settings.


// default values for parameter settings
function LoadDefaultSetting() {
	wrap = false;
	targetFrame = "";

	iconOpen  = "../img/minus1.gif";				// image file for opened folders
	iconClose = "../img/plus2.gif";				// image file for closed folders
	iconLink  = "../img/arrow1.gif";			// image file for links

	htmlString = "<!--MENU_TREE-->";				// initial HTML code

	styleRootIE4 = "font: none : 8pt Verdana; color:black; lign-height:20px";	// CSS for root menus in IE4
	styleRootNon = "font: bold : 8pt Verdana; color:black;  lign-height:20px";	// CSS for root menus in non-IE4

	styleMenuIE4 = "font: none : 8pt Verdana; lign-height:20px";			// CSS for other menus in IE
	styleMenuNon = "font: bold : 8pt Verdana; lign-height:20px";			// CSS for other menus in non-IE
}


//////////////////////////////////////////////////////////////////////////////


// browser checking
var isIE4 = (navigator.userAgent.indexOf("Windows") != -1 &&
             navigator.userAgent.indexOf("MSIE") != -1 &&
             navigator.appVersion.substring(0,1) > 3); 

// global variables
var wrap,			Wrap;
var targetFrame,	FrameTarget;
var iconOpen,		icOpen    = Array();
var iconClose,		icClose   = Array();
var iconLink,		icLink;
var htmlString,		htmlStr;
var styleRootIE4,	styleRootNon,	styleRoot;
var styleMenuIE4,	styleMenuNon,	styleMenu;

var quote = String.fromCharCode(34);
var newline = String.fromCharCode(13) + String.fromCharCode(10);
var level = 0;
var prevloc = 0;
var prevparent = "";
var id = 0;
var localFrameTarget, frameStr, styleFrameStr;

LoadDefaultSetting();

//alert('Outside htmlStr : ' + htmlStr);
//alert('Outside htmlString: ' + htmlString);

///////////////////////
//// main function ////
///////////////////////

function DrawTreeMenu() {
	id++;

//	alert(id + ' ->  htmlStr : ' + htmlStr);
//	alert(id + ' ->  htmlString' + htmlString);

	Wrap        = wrap;
	FrameTarget = targetFrame;
	icOpen[id]  = iconOpen;
	icClose[id] = iconClose;
	icLink      = iconLink;
	htmlStr     = htmlString;
	styleRoot   = (isIE4? styleRootIE4 : styleRootNon);
	styleMenu   = (isIE4? styleMenuIE4 : styleMenuNon);
	
	eval("BuildMenu" + id + "()");

	if (isIE4) {
		document.write(htmlStr);
	}
	else if (supportNonIE4Browser) {
//		document.write("<font face='Arial' size='1'>(optimized for IE4 and above)</font><br>"+htmlStr);
		document.write(htmlStr);
	}
	else {
		document.write("This page must be viewed with IE4 or above.");
	};

	if (! reserveLastSetting) {
		LoadDefaultSetting();
	}
}


/*function AddMenu(MenuStr, LinkUrl, MenuIDStr, MenuParentStr, defaultShow) {
	GenStr = "";
	SubStr = MenuParentStr==""? "<!--MENU_TREE-->" : "<!--" + MenuParentStr + "-->";

	if (htmlStr.indexOf(SubStr) >= 0) {
		localFrameTarget = "frmMain";
		if (((i = LinkUrl.indexOf("@")) > -1) && ((LinkUrl.toLowerCase()).indexOf("mailto:") < 0)) {
			localFrameTarget = LinkUrl.substring(i+1);
			LinkUrl = LinkUrl.substring(0, i);
		}
		frameStr = (localFrameTarget != ""? " target='" + localFrameTarget + "' " : (FrameTarget != ""? " target='" + FrameTarget + "' " : ""));

		styleRoot = (MenuParentStr == "" && !isIE4) ? (styleRoot.indexOf("font:") >= 0 ? styleRoot.replace("font:", "font: bold ") : "font-weight: bold; " + styleRoot) : styleRoot;
		if (MenuIDStr != "") {		// folder
			MenuStr = "" + MenuStr + "";
			styleRootStr = "STYLE=" + quote + 
						   (LinkUrl == ""? "text-decoration:none; " : "") + 
						   styleRoot + quote;
		}

		if (!isIE4 && supportNonIE4Browser) {
			AddMenuNonIEPlus(MenuParentStr, MenuIDStr, MenuStr, LinkUrl);
		}
		else {
			if (MenuIDStr != "") {		// folder
				sImg   = defaultShow? icOpen[id] : icClose[id];
				sShow  = defaultShow? "block" : "none";
				GenStr = "<DIV STYLE='margin-left:16; text-indent:-16'" + (Wrap?"":"NOWRAP") + ">" + newline +
						 "<IMG SRC='" + sImg + "' ID=I_" + MenuIDStr + id + " ALIGN=middle" + (isIE4 ? " STYLE='cursor: hand'" : "") +
						 "     onClick=" + quote + "ClickMenu('" + MenuIDStr + id + "')" + quote + ">&nbsp;" +
						 "<A " +
							   (LinkUrl != ""? "HREF='" + LinkUrl + "' " + frameStr + (MenuParentStr == ""? styleRootStr : "") :
							   (MenuParentStr == ""? styleRootStr : " STYLE='text-decoration:none; font-weight: bold' ") + "HREF='#expand'" +
						 "     onFocus=" + quote + "window.status=''" + quote +
						 "     onMouseOver=" + quote + "window.status=''" + quote +
						 "     onClick=" + quote + "ClickMenu('" + MenuIDStr + id + "')" + quote) +
						 "     >" + MenuStr + "</A>" + newline +
						 "<DIV STYLE=" + quote + "display:" + sShow + "; " + (MenuParentStr == ""? styleMenu : "") + quote + " ID=D_" + MenuIDStr + id + 
						 "     onClick=" + quote + "window.event.cancelBubble=true" + quote + ">" + newline +
						 "<!--" + MenuIDStr + "-->" + 
						 "</DIV></DIV>" + newline;
				}
			else {		// link
				GenStr = CreateLink (MenuParentStr, MenuStr, LinkUrl);
				GenStr = "<DIV STYLE='margin-left:16; text-indent:-16'>" + newline + GenStr + newline + "</DIV>" + newline;
			}
		}

		htmlStr = htmlStr.replace(SubStr, GenStr + SubStr);	
		
	}
}
*/
// added kk
function AddMenu(MenuStr, LinkUrl, MenuIDStr, MenuParentStr, defaultShow,tgt,showpro) {
	GenStr = "";
	SubStr = MenuParentStr==""? "<!--MENU_TREE-->" : "<!--" + MenuParentStr + "-->";

	if (htmlStr.indexOf(SubStr) >= 0) {
		if (tgt == null ) tgt="frmMain";
		localFrameTarget = tgt;
		if (((i = LinkUrl.indexOf("@")) > -1) && ((LinkUrl.toLowerCase()).indexOf("mailto:") < 0)) {
			localFrameTarget = LinkUrl.substring(i+1);
			LinkUrl = LinkUrl.substring(0, i);
		}
		frameStr = (localFrameTarget != ""? " target='" + localFrameTarget + "' " : (FrameTarget != ""? " target='" + FrameTarget + "' " : ""));

		styleRoot = (MenuParentStr == "" && !isIE4) ? (styleRoot.indexOf("font:") >= 0 ? styleRoot.replace("font:", "font: bold ") : "font-weight: bold; " + styleRoot) : styleRoot;
		if (MenuIDStr != "") {		// folder
			MenuStr = "" + MenuStr + "";
			styleRootStr = "STYLE=" + quote + 
						   (LinkUrl == ""? "text-decoration:none; cursor:hand; " : "") + 
						   styleRoot + quote;
		}

		if (!isIE4 && supportNonIE4Browser) {
			AddMenuNonIEPlus(MenuParentStr, MenuIDStr, MenuStr, LinkUrl);
		}
		else {
			//alert('MenuIDStr=' + MenuIDStr + ' LinkUrl='+ LinkUrl);
			if (MenuIDStr != "") {		// folder
				sImg   = defaultShow? icOpen[id] : icClose[id];
				sShow  = defaultShow? "block" : "none";
				// HERE CONDITION --------------------
				GenStr = "<DIV STYLE='margin-left:16; text-indent:-16'" + (Wrap?"":"NOWRAP") + ">" + newline +
						 "<IMG SRC='" + sImg + "' ID=I_" + MenuIDStr + id + " ALIGN=middle" + (isIE4 ? " STYLE='cursor: hand'" : "") +
						 "     onClick=" + quote + "ClickMenu('" + MenuIDStr + id + "')" + quote + ">&nbsp;" +
						 "&nbsp;<A " +
							   (LinkUrl != ""? "HREF='javascript:" + LinkUrl + "' " + frameStr + (MenuParentStr == ""? styleRootStr : "") :
							   (MenuParentStr == ""? styleRootStr : " STYLE='text-decoration:none; font-weight: bold;cursor:hand' ") + " target='_self' " + 
						 "     onFocus=" + quote + "window.status=''" + quote +
						 "     onMouseOver=" + quote + "window.status=''" + quote +
						 "     onClick=" + quote + "ClickMenu('" + MenuIDStr + id + "')" + quote) +
						 "     >" + MenuStr + "</A>" + newline +
						 "<DIV STYLE=" + quote + "display:" + sShow + "; " + (MenuParentStr == ""? styleMenu : "") + quote + " ID=D_" + MenuIDStr + id + 
						 "     onClick=" + quote + "window.event.cancelBubble=true" + quote + ">" + newline +
						 "<!--" + MenuIDStr + "-->" + 
						 "</DIV></DIV>" + newline;
				//alert(GenStr);	

				}
			else {		// link
				//alert('IF LINK ADDED THEN EXECUTE THIS ..');
				GenStr = CreateLink (MenuParentStr, MenuStr, LinkUrl, showpro);
				GenStr = "<DIV STYLE='margin-left:16; text-indent:-16'>" + newline + GenStr + newline + "</DIV>" + newline;
			}
		}

		//alert('SubStr : ' + SubStr);
		//alert('GenStr : ' + GenStr);
		//alert('SubStr : ' + SubStr);
		
		htmlStr = htmlStr.replace(SubStr, GenStr + SubStr);	
		//alert('New HTML_Str : ' + htmlStr);
	}
}

// add end


function AddMenuNonIEPlus(MenuParentStr, MenuIDStr, MenuStr, LinkUrl) {
// This function handles browsers (optimized for Netscape 4) besides IE4 or above.
// All menu items will be expanded, and can't be toggled.
// This function is not used if 'supportNonIE4Browser' variable is set to 'false'.

	level += (MenuParentStr == prevparent? 0 : 1);
	i = 0; loc = 0;
	while (htmlStr.indexOf(SubStr) != (i = 1 + htmlStr.indexOf("<!--", i)) - 1) {
		loc++;
	}
	level -= (loc-prevloc > 0? loc-prevloc+1 : 0);
	level = (level < 0? 0 : level);
	prevloc = loc;
	prevparent = MenuParentStr;

	if (MenuIDStr != "") {		// folder
		GenStr = "<IMG SRC='" + icOpen[id] + "' " + ">&nbsp;" +
				 (LinkUrl != ""? "<A HREF='" + LinkUrl + "' " + frameStr + (MenuParentStr == ""? styleRootStr : "STYLE='font-weight: bold'") + ">" + MenuStr + "</A>" :
				 (MenuParentStr == ""? "<SPAN " + styleRootStr + ">" : "") + MenuStr + (MenuParentStr == ""? "</SPAN>" : "")) +
				 "<BR>" + newline +
				 (MenuParentStr == ""? "<SPAN STYLE=" + quote + styleMenu + quote + "><!--" + MenuIDStr + "--></SPAN>" : "<!--" + MenuIDStr + "-->") + newline;
		}
	else {		// link
		GenStr = CreateLink (MenuParentStr, MenuStr, LinkUrl);
	}
	GenStr = duplicate("&nbsp; &nbsp; &nbsp;", level) + GenStr;

	function duplicate(Str, n) {
		d = "";
		for (i=1; i<=n; i++) { d += Str }
		return d;
	}
}
//onMouseOver='Javascript:windowopen();' onMouseOut='Javascript:windowclose();'


function CreateLink (MenuParentStr, MenuStr, LinkUrl, showpro) {
	
	if(showpro)
	{
		//alert('1');
		return	"<IMG SRC=images/'" + icLink + "' " + (isIE4? "ALIGN=middle" : "") + ">&nbsp;" +
			(LinkUrl != ""? "<A onMouseOver='Javascript:windowopen();' onMouseOut='Javascript:windowclose();' HREF='" + LinkUrl + "' " + frameStr + (MenuParentStr == ""? " STYLE=" + quote + styleRoot + quote + ">" + MenuStr + "" : ">" + MenuStr) + "</A>" :
			(MenuParentStr == ""? "<SPAN STYLE=" + quote + styleRoot + quote + ">" + MenuStr + "</SPAN>" : MenuStr)) +
			"<BR>";
	}
	else
	{
		return	"<IMG SRC='" + icLink + "' " + (isIE4? "ALIGN=middle" : "") + ">&nbsp;" +
			(LinkUrl != ""? "<A  HREF='javascript:" + LinkUrl + "' " + frameStr + (MenuParentStr == ""? " STYLE=" + quote + styleRoot + quote + ">" + MenuStr + "" : ">" + MenuStr) + "</A>" :
			(MenuParentStr == ""? "<SPAN STYLE=" + quote + styleRoot + quote + ">" + MenuStr + "</SPAN>" : MenuStr)) +
			"<BR>";
	}
}



function ClickMenu(MenuIDStr) {
	var divObj = document.all("D_" + MenuIDStr);
	var imgObj = document.all("I_" + MenuIDStr);
	id = MenuIDStr.substring(MenuIDStr.length-1);
	if (divObj.style.display == 'none') {
		divObj.style.display = 'block';
		imgObj.src = icOpen[id];
	}
	else {
		divObj.style.display = 'none';
		imgObj.src = icClose[id];
	}
	window.event.cancelBubble=true;
}


ie4treemenu_loaded = true;		// to make sure that this file has been loaded
