
// replace occurance of a string with anotger
//
function replace(s, t, u) 
{
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + u;
  if ( i + t.length < s.length) r += replace(s.substring(i + t.length, s.length), t, u);
  return r;
 }

 
// code to parse the xml and build the DHTML menu.
function buildMenu(url_xmlFile2,url_button,url_arrow,oM,x,y,w,h)
{
	var xmldoc2=new ActiveXObject("Microsoft.XMLDOM");
	xmldoc2.async =false;
	xmldoc2.validateOnParse = true;
	xmldoc2.load(url_xmlFile2);
	//alert(xmldoc2.parseError.reason);

	//some properties set
	oM.resizeCheck		=1;
	oM.rows				=1;
	//oM.onlineRoot		="";
	oM.pxBetween		=0;
	//oM.fillImg			="fill.gif";
	oM.fromTop			=x;
	oM.fromLeft			=y;
	oM.wait				=600;
	oM.zIndex			=900;
	oM.useBar			=0;
	oM.barWidth			="50%";
	oM.barHeight		="menu";
	oM.barX				=0;
	oM.barY				="menu";
	oM.barClass			="clBar";
	oM.barBorderX		=0;
	oM.barBorderY		=0;
	oM.menuPlacement	="right";

	// some argumens that MakeLevel methods has
	// these arguments to start with
	//cm_makeLevel <width>,<height>,<type>,<Hover>,<ver-width>,<hor-hieght>,,,<orientation>,<offset-left>,<offset-top>....


	oM.level[0]		= new cm_makeLevel(110,20,"","",1,1,"clT",0,"bottom",6,-5,0,0,0);//<-- "bottom" can be replaced by "top"
	oM.level[1]		= new cm_makeLevel(110,20,"","",0,-1,"clB",2,"left",1,1,1,10,10);	//<-- "right" can be replaced by "left"
	oM.level[2]		= new cm_makeLevel(200,20,"clS2","clS2over");
	oM.level[3]		= new cm_makeLevel(200,20);


	var dummy = '';
	var s_find = '';
	var s_replace = '';

	dummy = xmldoc2.xml +'';


	// for generic purposes
	// replace 'chanel' and 'sub-channel' tags to menuitems
	//
	s_find		= '<channel'; s_replace	= '<menuitem';
	dummy		= replace(dummy,s_find,s_replace);

	s_find		= '</channel>';s_replace	= '</menuitem>';
	dummy		= replace(dummy,s_find,s_replace);
	
	s_find		= '<sub-channel';s_replace	= '<menuitem';
	dummy		= replace(dummy,s_find,s_replace);

	s_find		= '</sub-channel>';s_replace	= '</menuitem>';
	dummy		= replace(dummy,s_find,s_replace);


	// after the replace operation the dtd wont validate so switch off the
	// dtd validation
	//
	xmldoc2.validateOnParse = false;
	xmldoc2.loadXML(dummy);

	//select all the 'menuitem' tags
	//

	var menuItems = xmldoc2.getElementsByTagName("menuitem");
	//alert(menuItems.length);

	// declare the variables that we need to use while processing the XML file.

	var nodeid;
	var parentid;
	var height;
	var labelTxt;
	var linkTxt;
	var nolink='';
	var width ='';

	nodeid		= 'm' + "0";
	parentid	= 'm' + ""
	labelTxt	= url_button;
	linkTxt		= url_button;
	height		= h;
	width		= w;
	nolink		= '';

	labelTxt="";
	width=122;
	oM.makeMenu(nodeid, parentid, labelTxt, '', '', width, height,linkTxt,'',"clT","clT",'','',nolink,0,0,0,'c:/menu/fill.gif');

	// go through the 'menuitem' tags sequentially
	//
	for (var i=0;i<menuItems.length;i++)
	{
		// the original xml doesnt have the 'node' attribute
		// we need to add this to our xml structure
		// 
		var Attr = xmldoc2.createAttribute("node") 
		// here we give it a value
		//
		Attr.appendChild(xmldoc2.createTextNode(i+1)) 

		// select the attribute so to make the focus easier
		//
		e = menuItems[i];
		e.setAttributeNode(Attr) 

		// get the attributes (there are only 2 user maintenable attributes)
		//
		nodeid		= 'm' + e.getAttribute("node");
		parentid	= 'm' + e.parentNode.getAttribute("node");

		if( e.parentNode.getAttribute("node")== null)
		{
			parentid="m0";
		}

		labelTxt = "";
		if(e.childNodes.length>0)
		{
			labelTxt	=		e.childNodes(0).text ;
		}

		linkTxt		=		e.getAttribute("link");
		//height		=		e.getAttribute("height");
		nolink		=		e.getAttribute("disabled");

		height	= h;
		width	= w
		if(nolink=="no")
		{
			nolink="";
		}

		// set the menu item
		//
		oM.makeMenu(nodeid, parentid, labelTxt, linkTxt, '', width, height,'','',"clS","clSover",'','',nolink,'','','',url_arrow);
	}



	nodeid		= 'm' + "100";
	parentid	= 'm' + "0"
	labelTxt	= url_arrow;
	linkTxt		= url_arrow;
	height		= '22';
	nolink		= '';

	//labelTxt="";
	//width=135;
	//oM.makeMenu(nodeid, parentid, labelTxt, linkTxt, '', 135, height,linkTxt,'',"clT","clT",'','',nolink,'','','','c:/menu/fill.gif');

	//var avail = "190+((cmpage.x2-235)/7)";
	//oM.menuPlacement = new Array(192,avail+"-11",avail+"*2-8",avail+"*3-12",avail+"*4-7",avail+"*5-9",avail+"*6+5");

	// if the menu items are set, then construct the menu
	// 
	oM.construct();
}
