//////////////////////////////////////////////////////////////////////////////////////////
// ---------------------------------- Menu Script ------------------------------------- //
//////////////////////////////////////////////////////////////////////////////////////////

//The first three variables checks for the running browser
NS = (document.getElementById&&!document.all); //for Netscape 6
IE = (document.all); //Internet Explorer
NS4 = (navigator.appName=="Netscape" && navigator.appVersion.charAt(0)=="4"); //Netscape 4


var Div = new Array(); 
var BarDiv = new Array(); 
//Stores all the Divs or Layers created by the Menu so that they can be
//refereneced to change their attributes
moving=setTimeout('null',1);//the timer
num=0; //keeps track of the current menu index being built buildMenu()



// Run Script
Go();


function Go(){
	buildMenu(Menu[0]);
}


function create(){
	document.write("<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>");
	document.write('<form name="message">');
	document.write('<p align="left">');
	//document.write('<button onClick="document.message">');
 	document.write('<textarea name="text1" rows="25" name="S1" cols="100" wrap="hard"></textarea>');
	document.write('</p>');
	document.write('</form>');
}

/*
This function is responsible for moving the menu out when the mouse pointer comes over it.
It works as follows:
	1. Increasing the zIndex of the current active menu's div by the number of menus, so
	   that the active menu has the hieghest zIndex and appears to be above all other menus.
	2. Checks if the active menu has reached its destination. If the condition is false, all
	   other menus get the default zIndex which is a predefined variable "startZindex". Then,
	   the "slideMenu()" is called to move the menu out with a predefined paramter that controls
	   the slidding speed (HorSlideSpeed).
	3. If the last condition is true, the function exits.
Also, when this function triggered, it stores all the other menus to their default positions.
*/

function moveOut(numb) {
    clearTimeout(moving);
	Div[numb].zIndex=startZindex+Menu.length;
	BarDiv[numb].zIndex=startZindex+Menu.length;
	if ((NS||NS4)&&parseInt(Div[numb].left)<0 || (IE||!(NS||NS4)) && Div[numb].pixelLeft<0) {
	
		//when a menu is moving out, all the other menus should be forced to return to their
		//original position
		for (l = 0; l<Div.length; l++){
			menu = Menu[l];	
			MenuBorderSize = menu.MenuBorderSize;
			menuTableWidth = menu.MenuWidth - (2*MenuBorderSize);
			XOffset = -menu.MenuWidth;
			if (NS4) XOffset = -(menu.MenuWidth+(2*MenuBorderSize)) ;
			
			if (l!=numb){
				if(NS4||NS) {
					Div[l].left=XOffset; 
					Div[l].zIndex = startZindex+l;
					BarDiv[l].left=0; 
					BarDiv[l].zIndex = startZindex+1;
				}
				else {
					Div[l].pixelLeft=XOffset;
					Div[l].zIndex = startZindex+l;
					BarDiv[l].pixelLeft=0;
					BarDiv[l].zIndex = startZindex+1;
				}
			}
		}
		moving = setTimeout("moveOut("+numb+")", 10);
		if (!(NS||NS4) && (HorSlideSpeed + Div[numb].pixelLeft > 0)) {
			slideMenu(- Div[numb].pixelLeft, numb)
		}
		else if ((NS||NS4) && (HorSlideSpeed + parseInt(Div[numb].left) > 0)) {
			slideMenu(- parseInt(Div[numb].left), numb)
		}
		else {
			slideMenu(HorSlideSpeed,numb)
		}
	}
}
/*
The mission of this function is to store the last active menu to its default position when
the mouse pointer leaves it.
	1. The first thing to be done is clearing the timer so that the menu stops moving out.
	2. Then, the "movBack1()" function is called, and this function does the main proccess.
The reason why the moving back motion is divided into two functions is to allow a "waitTime"
between the "mouseOut" action and the actual motion of the menu.
*/
function moveBack(numb) {
	clearTimeout(moving);
	moving = setTimeout("moveBack1("+numb+")", waitTime)
}

/*
	1. movBack1() first checks if the menu has returned to its default position.
	2. If the condition is false, it moves the menu backwords by calling "slideMenu"
	   with the paramter "HorSlideSpeed" as explained in moveOut().
	3. If the menu has reached its default position, each menu's div is assigned a 
	   zIndex value starting from the default startZindex and then increasing by one 
	   for each menu.
*/
function moveBack1(numb) {
	clearTimeout(moving);
	menu = Menu[numb];	
	MenuBorderSize = menu.MenuBorderSize;
	menuTableWidth = menu.MenuWidth - (2*MenuBorderSize);
	XOffset = -menu.MenuWidth;
	if (NS4) XOffset = -(menu.MenuWidth+(2*MenuBorderSize)) ;
	
	if (((NS4||NS) && parseInt(Div[numb].left)>XOffset) || (IE && Div[numb].pixelLeft>XOffset)) {

		moving = setTimeout("moveBack1("+numb+")", 10);
		if (!(NS||NS4) && (Div[numb].pixelLeft - XOffset< HorSlideSpeed)) {slideMenu(XOffset - Div[numb].pixelLeft, numb)}
		else if ((NS||NS4) && (parseInt(Div[numb].left) - XOffset< HorSlideSpeed)) {slideMenu(XOffset - parseInt(Div[numb].left), numb)}
		else slideMenu(-HorSlideSpeed,numb)
	}
	else {
		for (i=0; i<Menu.length; i++){
			Div[i].zIndex = startZindex+i;
			BarDiv[i].zIndex = startZindex+i;
		}
	}
}

/*
This is the function that actually moves the menu out or backword by changin the div's left pixel
for the active menu either by increasing it (moving out) or dereasing it (moving backword).
It receives the active menu index and the value by which the div's left pixel will be changed, and
as it's noticed this value is positive when moving out and negative when moving backword.
*/
function slideMenu(sp,DivNum){
	if (IE) {
		Div[DivNum].pixelLeft += sp;
		BarDiv[DivNum].pixelLeft += sp;
	}
	if (NS4||NS) {
		Div[DivNum].left = parseInt(Div[DivNum].left)+sp;
		BarDiv[DivNum].left = parseInt(BarDiv[DivNum].left)+sp;
	}
}


/*
This function scrolls the menu up or down as the page scrolls. This vertical slidding is seen in a 
deaccelerating motion. Moreover, the vertical slidding speed is controlled by a predefined variable
"VerSlideSpeed" which must be smaller than 1; otherwise, it's assigned a value of 0.2. 
*/
function makeStatic() {
	YOffset = Menu[0].MenuOffset;
	if (VerSlideSpeed>.9) VerSlideSpeed=.2;
	if (NS4||NS) {winY = window.pageYOffset;}
	if (IE) {winY = document.body.scrollTop;}
	
	if (lastY != winY){
		smooth=VerSlideSpeed * (winY-lastY);
		if(smooth > 0) smooth = Math.ceil(smooth);
		else smooth = Math.floor(smooth);
	}
	else smooth = 0;

	for (j=0;j<Menu.length;j++){
		if (IE) {
			Div[j].pixelTop += smooth;
			BarDiv[j].pixelTop += smooth;
		}
			
		if (NS||NS4) {
			Div[j].top  = parseInt(Div[j].top)+smooth;
			BarDiv[j].top  = parseInt(Div[j].top)+smooth;
		}
	}
	lastY = lastY+smooth;
	setTimeout('makeStatic()', 1)
}

/*
While building a menu, the div inside which the menu is created is set as hidden. After the menu
is completed, the div is set to be visible so that the user can't see the menu while being built.
*/
function set_Div(DivID){
	BarDivID = DivID+"b";
	if (NS){
		Div[num]=document.getElementById(DivID).style;
		Div[num].visibility="visible";
		BarDiv[num]=document.getElementById(BarDivID).style;
		BarDiv[num].visibility="visible";
		}
	else if (IE) {
		Div[num]=document.all(DivID).style;
		Div[num].visibility = "visible";
		BarDiv[num]=document.all(BarDivID).style;
		BarDiv[num].visibility = "visible";
		}
	
	else if (NS4) {
		Div[num]=document.layers[DivID];
		Div[num].visibility = "show";
		BarDiv[num]=document.layers[BarDivID];
		BarDiv[num].visibility = "show";		}
}

/*
This function is called to create the menu bar which is the only visible part of each menu when it's
at its default position. It may be an image or a vertically written text as specifed by the menu 
creator. It can also have a background or be transparent. Moreover, the menu creator has the option
to make a border for the bar or to make it borderless.
*/
function BuildBar(menu) {
	BarDivID  = menu.DivID;
	BarDivID  +="b"
	menuWidth = menu.MenuWidth;
	YOffset = menu.BarOffset+staticYOffset;
	var bar = '';	
	titleImg = menu.TitleImg.toUpperCase();
	var BarIsImg = (titleImg.indexOf('<IMG')>-1);
	titleImg = menu.TitleImg;
	//note that the value of 'titleImg' is in upper-case letters, thus
	//the value is copied again to keep it as the user defined it
	BarBgC = menu.BarBgColor;
	BarTxC = menu.BarTextColor;
	
	if (!NS4) {		
		document.write('<DIV ID="'+BarDivID+'" style="visibility:hidden; Position:absolute ; Left:0 ; Top:'+YOffset
				+' ; Z-Index:'+startZindex+'; width:'+barWidth
				+'; background-color:'+BarBorderColor+';" onmouseover="moveOut('
				+num+')" onmouseout="moveBack('+num+')">');
	}
	if (NS4) {
		document.write('<Layer name="'+BarDivID+'" width='+barWidth+' Z-Index = '+startZindex+' top="'
					+YOffset+'" LEFT=0 visibility="hide" bgcolor="'+BarBorderColor
					+'" onmouseout="moveBack('+num+')" onmouseover="moveOut('+num+')">');
	}	
	document.write('<table width="100%" cellpadding="0" cellspacing="'+BarBorderSize+'" align="center"');
	document.write(' valign=top border="0" bordercolor=".">');
  	
    document.write('<tr>');
    document.write('  <td width="100%" valign="top"');
	if (!BarBgC=="") document.write('bgcolor="'+BarBgC+'"');
	document.write('>');
	InnerBarWidth = barWidth-(2*BarBorderSize);
	if(BarIsImg) {
		bar = titleImg;
		bar+= ' width="'+InnerBarWidth+'">';
		document.write(bar);
	}
  	else {
    	for (b=0;b<titleImg.length;b++) {bar+=titleImg.charAt(b)+"<BR>"}
		if (!NS4) {
			document.write('<div style="cursor:default; width:'
						   +InnerBarWidth+'; color:'+BarTxC+'; background : '+BarBgC+';">');
		}
		else if (NS4){
			document.write('<ILayer><layer width="'+barWidth+'" bgcolor="'+BarBgC+'">');
		}
		document.write('<p align="center"><b>'+bar+'</b>');
		if (NS4) document.write('</layer></Ilayer>');
		else document.write('</div>');
  	}
		
    document.write(' </td></tr></table>');
	if(!NS4) document.write('</div>');
	else document.write('</layer>');
}

/*
This is the main function the creates the menu and it is the first function to be triggered.
It creates the menu one by one and inserts inside each menu the items assinged to it.
*/
function buildMenu(menu) {
	padding = menu.cellpadding;	
	if (NS4) padding = 0;
	DivID  = menu.DivID;
	MenuBorderSize = menu.MenuBorderSize;
	MenuBorderColor = menu.MenuBorderColor;
	menuWidth = menu.MenuWidth;
		
	XOffset = -menu.MenuWidth ;
	if (NS4) XOffset = -(menu.MenuWidth+(2*MenuBorderSize)) ;

	
	YOffset = menu.MenuOffset+staticYOffset;
	
	if (!NS4) {		
		document.write('<DIV ID="'+DivID+'" style="visibility:hidden; Position: absolute ; Left: '+XOffset
					+'; Top:'+YOffset+' ; Z-Index: '+startZindex+'; width:'+menuWidth
					+'; background-color:'+MenuBorderColor
					+';" onmouseover="moveOut('+num+')" onmouseout="moveBack('+num+')">');
	}
	
	if (NS4) {
		document.write('<Layer name="'+DivID+'" width=0 Z-Index = '+startZindex+' top="'
					+YOffset+'" LEFT="'+XOffset+'" visibility="hide" bgcolor="'+MenuBorderColor
					+'" onmouseover="moveOut('+num+')" onmouseout="moveBack('+num+')">');		
	}
	
	// The MenuBorderColor is defined as the table background color	
	// The MenuBorderSize is defined as the cellspacing

	menuTableWidth = "100%"
	
	document.write('<table width="'+menuWidth+'" cellpadding="'+padding+'" cellspacing="'+MenuBorderSize+'" align="center"');
	document.write(' border="0">');
	
	for(i=0;i<menu.Item.length;i++) {
		kind = menu.Item[i].kind.toUpperCase();
		if (kind=="I"){	//"I" for "normal Item" while "F" is for "Form Item"
			URL = menu.Item[i].URL;
			Title = menu.Item[i].Title;
			Target  = menu.Item[i].Target;
			
			LowBg   = menu.Item[i].Color.LowBg;
			HighBg  = menu.Item[i].Color.HighBg;
			LowFont = menu.Item[i].Color.LowFont;
			HighFont = menu.Item[i].Color.HighFont;
		
			FontSize = menu.Item[i].Style.FontSize;
			FontFamily = menu.Item[i].Style.FontFamily;
			Align = menu.Item[i].Style.Align;
			Bold = menu.Item[i].Style.Bold;
			Italic = menu.Item[i].Style.Italic;
			
			document.write('<tr>');
       		if (!NS4){//paramters for font face and size				
		    	document.write('<td width="100%" align="'+Align+'"');
				document.write('style="text-align: left; position: inherit; width:'
						+menuWidth+'; cursor:default ; background-color:'+LowBg
						+'; color:'+LowFont+'; font-family: '
						+FontFamily+'; z-index: 0; font-size: '+FontSize+'pt;"');
				
				if (URL.substring(0, 7) != "onclick") 
				{
					if (Target.toUpperCase() == "_BLANK") URL = 'onclick="window.open(\''+URL+'\')"';
					else URL = 'onclick="parent.location.href=\''+URL+'\'"';
				}
						
				document.write(URL+' onmouseover="this.style.color=\''
						+HighFont+'\'; this.style.backgroundColor=\''+HighBg
						+'\';" onmouseout="this.style.color=\''
						+LowFont+'\'; this.style.backgroundColor=\''+LowBg+'\';">');
  			}
			else if (NS4)
			{
				if (URL.substring(0,  7) != "onclick") 
				{
					if (Target.toUpperCase() == "_BLANK") URL = 'onfocus="window.open(\''+URL+'\')"';
					else URL = 'onfocus="window.location.href=\''+URL+'\'"';
				}
				else
				{
					URL = "onfocus"+URL.substring(7);
				}
				
				document.write('<td width="100%" height=10 id=".">');
				//Layer
				document.write('<ILayer><LAYER width="'+menuWidth+'" BGCOLOR="'+LowBg+'" ');
				//Layer Mouse Actions
				document.write('onmouseover="bgColor=\''+HighBg+'\'" onmouseout="bgColor=\''+LowBg+'\'"');
				document.write(' '+URL+'>');
				document.write('<Font color="'+LowFont+'" face="'+FontFamily+'" point-size="'+FontSize);
				document.write('">');
				document.write('<p align='+Align+'>');
			}
			if (Bold=="Yes"){document.write('<b>');}
			if (Italic=="Yes"){document.write('<i>');}
			document.write(Title);
			if (Italic=="Yes"){document.write('</i>');}
			if (Bold=="Yes"){document.write('</b>');}
	


			if(NS4) document.write('</font></LAYER></ILayer>');
			document.write('</td></tr>');

		}//End of Normal Item

		
		else if (kind=="F"){ 	//Form Item
			Name = menu.Item[i].Name;
			Action = menu.Item[i].Action;
			Target = menu.Item[i].Target;
			Encoding = menu.Item[i].Encoding;
			BgColor = menu.Item[i].BgColor;
			cellpadding = menu.Item[i].cellpadding;
			
			document.write('<tr>');
			document.write('<td width="100%" bgcolor="'+BgColor+'">');
			document.write('<table bgcolor="'+BgColor+'" border="0" cellpadding="'+cellpadding+'" cellspacing="0" width="100%">');
            document.write('<form name='+Name+' method="POST" action="'+Action+'" target="'+Target+'" encoding="'+Encoding+'">');

				
			for (j=0; j<menu.Item[i].FormItem.length; j++){
				formItem = menu.Item[i].FormItem[j];
				
				Type = formItem.Category.toUpperCase();
				if (Type=="L") Type = "label";
				if (Type=="B") Type = "button";
				if (Type=="C") Type = "checkbox";
				if (Type=="P") Type = "password";
				if (Type=="S") Type = "submit";
				if (Type=="T") Type = "text";
				if (Type=="H") Type = "hidden";
				if (Type=="M") Type = "select";
				if (Type=="R") Type = "radio";
				iAction = formItem.Action;
				iWidth = formItem.Width;
				iName  = formItem.Name;
				iValues = formItem.Values;
				
				if ((iWidth - menuWidth-(2*padding)-(2*MenuBorderSize)) > -10) { iWidth = menuWidth-(2*padding)-(2*MenuBorderSize)-10;}
				iFontColor = formItem.Color.LowFont;
				TextFontColor = formItem.Color.HighFont;
				iBgColor = formItem.Color.LowBg;

				iFontSize = formItem.Style.FontSize;
				iFontFamily = formItem.Style.FontFamily;
				iAlign = formItem.Style.Align;
				iBold = formItem.Style.Bold;
				iItalic = formItem.Style.Italic;
				
				document.write('<tr>');
				document.write('<td width="100%" align='+iAlign);
				if (!NS4) {
					document.write(' style="color:'+TextFontColor+'; font-size:'
									+iFontSize+'pt; font-family:'+iFontFamily+';');
					if (iItalic == "Yes"){document.write('font-style : italic;');}
					if (iBold == "Yes")	 {document.write('font-weight : bolder;');}
					document.write('"');
				}
				document.write('>');
				if (Type=="password" || Type=="text" || Type=="hidden") {
                	document.write('<input type="'+Type+'" value="'+iValues[0]+'" name="'+iName+'" ');
					if (!NS4) {
						document.write('style="color:'+iFontColor+'; background-color: '+iBgColor+'; width: '+iWidth+'px;');
						document.write('font-size:'+iFontSize+'pt; font-family:'+iFontFamily+';"');
					}
					
					else{
						var isize=Math.ceil(iWidth/9)
						document.write('size='+isize+' ');
					}
					document.write(iAction+'>');
				}
				else if (Type=="submit" || Type=="button"){
					document.write('<input type="'+Type+'" value="'+iValues[0]+'" name="'+iName+'" ');
					if (!NS4) {
						document.write('style="color:'+iFontColor+'; background-color: '
							+iBgColor+'; width:' +iWidth+'px; '
							+'font-size:'+iFontSize
							+'pt; font-family:'+iFontFamily+';"');
					}
					else document.write('size=14 ');
					if (Type=="button") document.write(iAction+'>');
					else document.write('>');
				}
				else if (Type=="select"){
					document.write('<select name="'+iName+'" ');
					if (!NS4){
						document.write('style="color:'+iFontColor+'; background-color:'
							+iBgColor+'; width:' +iWidth+'px; '
							+'font-size:'+iFontSize
							+'pt; font-family:'+iFontFamily+';"');
					}
					document.write('>');
					for (indx=0; indx<iValues.length; indx++){
						document.write('<option ');
						if (indx==0) document.write('');
						document.write('value="'+iValues[indx]+'" ');
						document.write('>'+iValues[indx]+'</option>');
					}
					document.write('</select>');
				}// end of select
				
				else if (Type=="radio" || Type=="checkbox"){
					for (indx=0; indx<iValues.length; indx+=2)
					{
						document.write('<input type="'+Type+'" name="'+iName+'" value="'+iValues[indx]+'" '+iValues[indx+1]+'>'+iValues[indx]);
						if (indx != iValues.length+2) document.write('<br>');
					}
				}
				
				else if (Type=="label"){
					if (NS4){
						document.write('<font color="'+TextFontColor+'" face="'+iFontFamily+'" point-size="'+iFontSize+'">');
						document.write(iName);
						document.write('</font>');
					}
					else document.write(iName);
				}

				
				document.write('</td></tr>');
			}//for loop that checks for Field type on each loop			
			document.write('</form></table></td></tr>');			
		}//End of Form Item
	
	  }//End of For	
      document.write('</table>');
	  if (!NS4) document.write('</div>');
	  if (NS4) document.write('</layer>');
	  BuildBar(menu);
	  set_Div(DivID);
	  num++;
	  
	  theleft=-menuWidth;
	  lastY=0;
	  if (menuIsStatic.toLowerCase()=="yes" && num==Menu.length) {makeStatic();}
	  if (num!=Menu.length) {
	  	buildMenu(Menu[num]);
	  }
	
}
