// 20031125 - Added support for repositioning the menu when scrolling with the arrow keys in Mozilla and Netscape
//var iens6=document.all||document.getElementById;  // Browser Check
var x=0;             // the x position of the mouse when the event occurred
var y=0;             // the y position of the mouse when the event occurred
var menuY=0;         // the current y or top position of the menu
var moveMenu=null;   // null - not defined yet, 0 - no menu relocation, 1 - menu is relocated
var closeTimer=750; // menus automatically close after 3/4 of a second if another menu is not entered within that 1 second

var theobj=null;     // a pointer to the pop up menu
var timerID;         // used for closing the pop up menu
var isOpen='';       // the name of the last opened pop up menu
var activeMenu;      // the name of the currently active menu

var theobj2=null;    // a pointer to the pop up sub-menu
var timerID2;        // used for closing the pop up sub-menu
var isOpen2='';      // the name of the last opened pop up sub-menu

// This takes an item and it's related href (URL) and returns a consistent and nicely formatted HTML string for the menu
function formatI(item, href)
{
   return '<A class="toolbar" HREF="' + href + '">   ' + item + '</A></BR>'
}

// This takes an two items and their related hrefs (URLs) and returns a consistent and nicely formatted HTML string putting each item in a column of a two column table for the menu.
function formatT(item1, href1, item2, href2)    // the T represents a Table format
{
   return '<tr><td align=center>' + formatI(item1, href1) + '</td>' + '<td align=center>' + formatI(item2, href2) + '</td></tr>'
}

// This is exactly like formatI but adds an additional event that opens a sub-menu with the name of item
function formatS(item, href)  // the S indicate that this item will open a sub-menu
{
   return '<div class="toolbar" onMouseOut=close_menu2() onMouseOver=open_menu2("' + item + '")>' + formatI(item, href) + '</div>'
}

// These are the items that appear in the left menu of the every Seva Software web site.
// If the item has a pop up menu, then the name must also appear in the sub_menu array with content of the pop-up menu.
menu_items = ["Home",       "News",          "Products",      "Services",      "Clients",      "Information"]
// These are web pages or references for each items in the left menu of the every Seva Software web site.
menu_hrefs = ["http://www.sevasoftware.com/index.html", "http://www.sevasoftware.com/whatsnew.html", "http://www.sevasoftware.com/products.html", "http://www.sevasoftware.com/services.html", "http://www.sevasoftware.com/clients.html", "http://www.sevasoftware.com/information.html"]

// This array contains the content of each pop up menu.  There is an entry in this array for every pop up menu.
sub_menu = new Array()
size_menu = new Array()

size_menu['Home'] = 7
sub_menu['Home'] = '' +
   formatI('Who is Seva Software?', 'http://www.sevasoftware.com/index.html#WhoIsSevaSoftware') +
   formatI('What I do?', 'http://www.sevasoftware.com/index.html#WhatWeDo') +
   formatI('Freeware Policy', 'http://www.sevasoftware.com/index.html#FreewarePolicy') +
   formatI('Contact Information', 'http://www.sevasoftware.com/contact_info.html') +
   formatI('Menu Relocation', 'http://www.sevasoftware.com/index.html#MenuRelocation')

size_menu['News'] = 5
sub_menu['News'] = '' +
   formatI('12/2003 - Moved all web sites to a much faster and more robust web server', 'http://www.sevasoftware.com/whatsnew.html') +
   formatI('11/2003 - Released a new version of the JavaScript pop-up menus', 'http://www.sevasoftware.com/products.html#ThisWebSite') +
   formatI('10/2003 - Locana 0.83 Released', 'http://www.sevasoftware.com/locana.html') +
   formatI('10/2003 - Released the HStore class version 0.91', 'http://www.sevasoftware.com/products.html#HStore') +
   formatI('09/2003 - Released the HStore class version 0.90', 'http://www.sevasoftware.com/products.html#HStore') +
   formatI('05/2003 - Locana 0.82 Released', 'http://www.sevasoftware.com/locana.html') +
   formatI('01/2003 - Released Coin Wizard for Access2000', 'http://www.sevasoftware.com/products.html#CoinWizard') +
   formatI('09/2002 - The JavaScript used to create the menus for this web site is now available', 'http://www.sevasoftware.com/products.html#ThisWebSite') +
   formatI('09/2002 - New web site installed', 'http://www.sevasoftware.com/index.html') +
   formatI('03/2002 - Locana 0.81 Released', 'http://www.sevasoftware.com/locana.html') +
   formatI('11/2001 - ArunaDB 0.80 Released', 'http://www.sevasoftware.com/arunadb.html') +
   formatI('02/2001 - Released Access to PostgreSQL Conversion Tool', 'http://www.sevasoftware.com/products.html#Upsizing') +
   formatI('06/2000 - Released Coin Wizard for Access97', 'http://www.sevasoftware.com/products.html#CoinWizard') +
   formatI('To receive email notification of changes to this web site', 'http://www.sevasoftware.com/whatsnew.html#EmailNotification')

size_menu['Products'] = 8
sub_menu['Products'] = '' +
   formatI('Freeware', 'http://www.sevasoftware.com/products.html#Freeware') +
   formatI('Locana', 'http://www.locana.org') +
   formatI('Locana Applets', 'http://www.sevasoftware.com/products.html#LocanaApplets') +
   formatI('ArunaDB', 'http://www.arunadb.org') +
   formatI('HStore', 'http://www.sevasoftware.com/products.html#HStore') +
   formatI('Upsizing Tools', 'http://www.sevasoftware.com/products.html#Upsizing') +
   formatI('This Web Site', 'http://www.sevasoftware.com/products.html#ThisWebSite') +
   formatI('Coin Wizard', 'http://www.sevasoftware.com/products.html#CoinWizard')

size_menu['Services'] = 9
sub_menu['Services'] = '' +
   formatI('Outsourcing', 'http://www.sevasoftware.com/services.html#Outsourcing') +
   formatI('Consulting', 'http://www.sevasoftware.com/services.html#Consulting') +
   formatI('Contracting', 'http://www.sevasoftware.com/services.html#Contracting') +
   formatI('Upgrading', 'http://www.sevasoftware.com/services.html#Upgrading') +
   formatI('Integrating', 'http://www.sevasoftware.com/services.html#Integrating') +
   formatI('Problem Solving', 'http://www.sevasoftware.com/services.html#ProblemSolving') +
   formatI('Software Devel.', 'http://www.sevasoftware.com/services.html#SoftwareDevelopment') +
   formatI('Web Development', 'http://www.sevasoftware.com/services.html#WebDevelopment') +
   formatI('Database Design', 'http://www.sevasoftware.com/services.html#DatabaseDesign')

size_menu['Clients'] = 6
sub_menu['Clients'] = '' +
   formatI('TV Guide', 'http://www.sevasoftware.com/clients.html#TVGuide') +
   formatI('MasterPath', 'http://www.sevasoftware.com/clients.html#MasterPath') +
   formatI('Fort Lupton Market', 'http://www.sevasoftware.com/clients.html#FortLupton') +
   formatI('DISC', 'http://www.sevasoftware.com/clients.html#DISC') +
   formatI('DOD', 'http://www.sevasoftware.com/clients.html#DOD')

size_menu['Contributing'] = 6
sub_menu['Contributing'] = '' +
   formatI('How to Contribute', 'http://www.sevasoftware.com/contributing.html#HowToContribute') +
   formatI('Why Contribute', 'http://www.sevasoftware.com/contributing.html#WhyContribute') +
   formatI('Contributors', 'http://www.sevasoftware.com/contributing.html#Contrubutors') +
   formatI('How to Invest', 'http://www.sevasoftware.com/contributing.html#HowToInvest') +
   formatI('Why Invest', 'http://www.sevasoftware.com/contributing.html#WhyInvest') +
   formatI('Investors', 'http://www.sevasoftware.com/contributing.html#Investors')

size_menu['Information'] = 3
sub_menu['Information'] = '' +
   formatS('Languages', 'http://www.sevasoftware.com/languages.html') +   // these will open a sub-menu
   formatS('Databases', 'http://www.sevasoftware.com/databases.html') +
   formatS('Systems', 'http://www.sevasoftware.com/systems.html')

size_menu['Languages'] = 8
sub_menu['Languages'] = '<table cellpadding=5 border=groove>' +
   formatT('Ruby', 'languages.html#Ruby', 'HTML', 'http://www.sevasoftware.com/languages.html#HTML') +
   formatT('Java', 'languages.html#Java', 'JavaScript', 'http://www.sevasoftware.com/languages.html#JavaScript') +
   formatT('C#', 'languages.html#CS', 'CGI', 'http://www.sevasoftware.com/languages.html#CGI') +
   formatT('Perl', 'languages.html#Perl', 'Tk', 'http://www.sevasoftware.com/languages.html#Tk') +
   formatT('C++', 'languages.html#CP', 'SQL', 'http://www.sevasoftware.com/languages.html#SQL') +
   formatT('C', 'languages.html#C', 'PL/SQL', 'http://www.sevasoftware.com/languages.html#PLSQL') +
   formatT('Modula2', 'languages.html#Modula2', 'ODBC', 'http://www.sevasoftware.com/languages.html#ODBC') +
   formatT('Fortran', 'languages.html#Fortran', 'COBOL', 'http://www.sevasoftware.com/languages.html#COBOL') +
   '</table>'

size_menu['Databases'] = 6
sub_menu['Databases'] = '' +
   formatI('Database Terms', 'http://www.sevasoftware.com/databases.html#Terms') +
   formatI('Choosing A Database', 'http://www.sevasoftware.com/databases.html#ChoosingADatabase') +
   formatI('ArunaDB', 'http://www.sevasoftware.com/databases.html#ArunaDB') +
   formatI('PostgreSQL', 'http://www.sevasoftware.com/databases.html#PostgreSQL') +
   formatI('Oracle', 'http://www.sevasoftware.com/databases.html#Oracle') +
   formatI('SQL Server', 'http://www.sevasoftware.com/databases.html#SQLServer') +
   formatI('Access', 'http://www.sevasoftware.com/databases.html#Access') +
   formatI('MySQL', 'http://www.sevasoftware.com/databases.html#MySQL')

size_menu['Systems'] = 7
sub_menu['Systems'] = '' +
   formatI('Operating System Terms', 'http://www.sevasoftware.com/systems.html#Terms') +
   formatI('Linux', 'http://www.sevasoftware.com/systems.html#Linux') +
   formatI('FreeBSD', 'http://www.sevasoftware.com/systems.html#FreeBSD') +
   formatI('Windows', 'http://www.sevasoftware.com/systems.html#Windows') +
   formatI('MAC OS', 'http://www.sevasoftware.com/systems.html#MACOS') +
   formatI('HPUX', 'http://www.sevasoftware.com/systems.html#HPUX') +
   formatI('DEC OSF', 'http://www.sevasoftware.com/systems.html#OSF') +
   formatI('MSDOS', 'http://www.sevasoftware.com/systems.html#MSDOS')

// This store the position of the mouse on every mouse move event in the window so Mouse Over events can track where the mouse is
function getXY(e) {
	if (document.all) {
		x=event.x;
		y=event.y;
	}
	else {
		x=e.pageX;
		y=e.pageY;
	}
}

// This relocates the menu on the left to the new_y position
function relocate_menu(new_y) {
   //alert(new_y + ' == ' + menuY)
   if (new_y == menuY) return nil;     // we don't need to relocate
   obj = (document.getElementById ? document.getElementById('left_menu') : document.all.left_menu)
   obj.style.top = new_y + 10
   menuY = new_y + 10
}

// This relocates the menu when the user scrolls around in the web page only if menu relocation is enabled
function on_scroll() {
   if (moveMenu == null) moveMenu = get_menu_relocation();    // retrieve the cookie so we can determine the value of the menu relocation
   if (moveMenu == 0)  return null;   // moving the menu has been disabled
	if (window.pageYOffset >= 0 && window.pageYOffset != menuY)
	   relocate_menu(document.pageYOffset);   // Netscape
	if (document.body.scrollTop >= 0 && document.body.scrollTop != menuY)
	   relocate_menu(document.body.scrollTop);         // ie
}

// This saves the turns menu relocation value in a cookie
function save_menu_relocation(value)
{
   moveMenu = value;
   obj = (document.getElementById ? document.getElementById('toggle_menu_relocation_button') : document.all.toggle_menu_relocation_button)
   //alert(obj.value + ' - ' + value)
   if (value == 0) {
      exp = new Date()
      expn = exp.getTime() + (90*24*60*60*1000);   // the cookie is good for 90 days from the last time the web site was hit
      exp.setTime(expn);
      document.cookie = 'ToggleMenuRelocation=0; expires=' + exp.toGMTString();  // store a cookie so the new setting is remembered
      obj.value = "Turn Menu Relocation On"
      relocate_menu(0); // let's send the menu to the top of the display
   }
   else {
      if (document.cookie) // delete the cookie if it exists
         document.cookie = 'ToggleMenuRelocation=1; expires=Thu, 01-Jan-70 00:00:01 GMT'     // the default is that menu_relocation is on, so lets delete the cookie
      obj.value = "Turn Menu Relocation Off"
      on_scroll();   // let's put the menu in the right spot
   }
}

// This retrieves and returns the state of the menu relocation value from a cookie
function get_menu_relocation()
{
   if (document.cookie && document.cookie.indexOf('ToggleMenuRelocation=0') >= 0)
      return 0  // menu relocation has been turned off
   return 1  // menu relocation is on by default
}

// This toggles menu relocation on and off
function toggle_menu_relocation()
{
   if (moveMenu == null) moveMenu = get_menu_relocation();    // retrieve the cookie so we can determine the value of the menu relocation
   save_menu_relocation((moveMenu == 0 ? 1 : 0));
}

document.onmousemove=getXY    // make sure we remember the x,y position of the mouse for mouse over events
document.onscroll=on_scroll   // This is needed for Netscape/Mozilla to reposition the menu after clicking on the scroll bar, <body onScroll=on_scroll()> in the actual web page is needed for IE to reposition the menu
document.onkeyup=on_scroll    // This is needed for Netscape/Mozilla to reposition the menu after scrolling with the keyboard
window.onload=on_scroll       // This is needed for Netscape/Mozilla to reposition the menu after reloading a web page

var popupLeft = 140;                   // the location of the pop up menu
var popupSubLeft = popupLeft + 105;    // the location of the pop up sub-menu
var lineHeight = 20;                   // the pixel height of a single line in a menu, this used to estimate the height of the menu in ns

// Returns the pixel position to use for the top of the menu
function get_menu_top(myobj, menu_name)
{
   var winHeight;
   var maxHeight;
   if (document.body.clientHeight)
      winHeight = document.body.clientHeight;  //ie
   if (myobj.clientHeight > 0) {
      maxHeight = myobj.clientHeight;
      //alert(myobj.clientHeight)
   }
   else {    // estimate the height of each menu
      maxHeight = 20 + (size_menu[menu_name] + 1) * (lineHeight + (menu_name=='Languages' ? 5 : 0));   // make sure we take the padding and the header into account
      //alert(maxHeight + ' =  20 + (' + size_menu[menu_name] + ' + 1) * (' + lineHeight + ' + ' + (menu_name=='Languages' ? + 5 : 0) + ') - menu_name:' + menu_name)
   }

   // my_y is the position of the mouse relative to the top of the window, i.e. no ScrollTop
	if (document.all)    // ie
      my_y = y;         // y does not include scrollTop for ie
   else                 // ns
      my_y = y - document.body.scrollTop;  // y includes scrollTop for ns

   //alert('menu: ' + my_y + ' + ' + maxHeight + ' > ' + winHeight)
   if (my_y + maxHeight > winHeight)   // This would clip the bottom menu so lets put the menu at the bottom of the page
      h = winHeight - maxHeight - 20;  // make sure the whole menu is visible
   else
      h = my_y - (maxHeight/2) + 20;   // center the pop up menu
   if (h < 0) h = 0;
   if (moveMenu == 1)
      return h + document.body.scrollTop; // only if menu relocation is on
   return h;
}

// This returns a string of HTML to use for the content of a menu item in the left menu bar.  It includes the sub-menu if there is one.
function format_menu_item(menu_name)
{
   return '<div align="CENTER" style="background-color:#00F0F0"><font size=+1>' + menu_name + '</font></div>' + sub_menu[menu_name];
}

// Opens the pop up menu
function open_the_menu(myObj, menu_name, myLeft)
{
	myObj.style.borderStyle = 'groove'
	myObj.style.backgroundColor = '#E0FFFF'  // light blue
	if (document.all) {
   	myObj.innerHTML = ""
		myObj.insertAdjacentHTML("BeforeEnd", format_menu_item(menu_name));
		}
	else
	   myObj.innerHTML = format_menu_item(menu_name);
   myObj.style.top = get_menu_top(myObj, menu_name);
   myObj.style.left = myLeft;
	myObj.style.visibility = "visible";
}

// Opens the pop up menu
function open_menu(menu_name)
{
   activeMenu = menu_name;
	clearTimeout(timerID)                  // make sure we don't close the open menu
	if (isOpen == menu_name) return null;  // already open
	if (theobj2) close_the_menu2();        // changing menus closes the sub-menu
	if (y <= 20) return null;              // make sure we have a valid before continuing
   //theobj = document.getElementById('left_popup_menu');
   //if (!thobj)
	//   theobj = document.all.left_popup_menu;
   theobj = (document.getElementById ? document.getElementById('left_popup_menu') : document.all.left_popup_menu);
   //java.lang.System.out.println("open_menu(" + menu_name + ") - " + theobj);
	isOpen = menu_name;
   open_the_menu(theobj, menu_name, popupLeft);
}

// Closes the pop up menu
function close_the_menu()
{
	isOpen = '';
	if (theobj) {
		theobj.innerHTML = "";
		theobj.style.visibility = "hidden";
	}
}

// Closes the pop up menu after 1 second.  This allows the user to navigate to another menu and stop the closing menu.
function close_menu()
{
   activeMenu = null;
   timerID = setTimeout("close_the_menu()", closeTimer);
}

// Opens the pop up sub-menu
function open_menu2(menu_name)
{
	clearTimeout(timerID);                  // make sure we don't close the open menu
	clearTimeout(timerID2);                 // make sure we don't close the open sub-menu
	if (isOpen2 == menu_name) return null; // already open
	if (y <= 20) return null;              // make sure we have a valid before continuing
   theobj2 = (document.getElementById ? document.getElementById('left_popup_menu2') : document.all.left_popup_menu2);
	isOpen2 = menu_name;                   // remember the open menu so we don't reopen it
   open_the_menu(theobj2, menu_name, (x > popupLeft ? popupSubLeft : popupLeft));
}

// Closes the pop up sub-menu
function close_the_menu2()
{
	if (activeMenu == null)
      timerID = setTimeout("close_the_menu()", 250);
	isOpen2 = '';
	theobj2.innerHTML = ""
	theobj2.style.visibility="hidden"
}

// Closes the pop up sub-menu after 1 second.  This allows the user to navigate to another menu and stop the closing menu.
function close_menu2()
{
   timerID2 = setTimeout("close_the_menu2()", closeTimer)
}

// This should be called in the body of every web page and puts invisible objects in the web page to use for the pop up menu and pop up sub-menu
function add_popup_menu()
{
   document.write(
      '<div class="toolbar" id="left_popup_menu" style="padding:10;left:0;top=0;visibility:hidden;position:absolute;z-index:1;overflow:hidden;" onMouseOver="clearTimeout(timerID)" onMouseOut="close_menu()"></div>'+
      '<div class="toolbar" id="left_popup_menu2" style="padding:10;left:0;top=0;visibility:hidden;position:absolute;z-index:1;overflow:hidden;" onMouseOver="clearTimeout(timerID2);clearTimeout(timerID)" onMouseOut="close_menu2()"></div>'
      )
}

// Creates the clickable items (left menu) on the left side of all Seva Software web pages, this eliminates the need for frames
function add_menu(parent)
{
   document.write('<div class="toolbar" id="left_menu" style="position:absolute;left:10;top:10">');
   document.write('<A HREF="http://sevasoftware.com"><IMG SRC="http://www.sevainc.com/logos/seva.jpg" ALT="Seva Software"></A><BR>'); // add the logo
   for(i=0; i<menu_items.length; i++) {  // for each item in the menu_items array
      document.write(
         '<div class="toolbar" onMouseOver=open_menu("' + menu_items[i] + '") onMouseOut=close_menu()>' +
         '<P><FONT SIZE=+2><A class="toolbar" HREF="' + menu_hrefs[i] + '">' + menu_items[i] + '</A></FONT></P>' +
         '</div>'
      )
      if (parent == menu_items[i].toLowerCase())
         document.write('<pre onMouseOver=close_the_menu()>' + sub_menu[menu_items[i]] + '</pre>');
   }
   document.write('</div>');
};

// Adds the thank you line to the bottom of all Seva Software web pages
function add_thank_you(page_name)
{
   document.write(
      '<P align=center><IMG SRC="http://www.sevainc.com/bar.jpg" ALT="----------------"></P>' +
      '<P align=center>' +
      '<BR>Thank you for taking the time to visit this web page.  I trust you found the information contained in this page useful.' +
      "<BR>Please email any questions, concerns, or issues with this web site to <A HREF='mailto:webmaster-" + page_name + "@sevasoftware.com'>webmaster@sevasoftware.com</A>." +
      "<BR>Please email any additional information you would like to see added to this web site to <A HREF='mailto:webmaster-" + page_name + "@sevasoftware.com'>webmaster@sevasoftware.com</A>." +
      '<BR>Please remember Seva Software when your company would benefit from an experienced database architect and software engineer.' +
      '</P>'
   )
}

// Adds the Seva Software logos to the bottom of all Seva Software web pages, this includes the last updated date below the logos
function add_logos()
{
   document.write(
      '<p align=CENTER> ' +
      '<a href="http://www.arunadb.org"><img src="http://www.sevainc.com/logos/arunadb.jpg" alt="http://www.arunadb.org" title="A database server written in Ruby"</a> ' +
      '<a href="http://www.locana.org"><img src="http://www.sevainc.com/logos/locana.jpg" alt="http://www.locana.org" title="A GUI and GUI builder written in Ruby"</a> ' +
      '<a href="http://www.ruby-lang.org"><img src="http://www.sevainc.com/logos/ruby.jpg" alt="http://www.ruby-lang.org" title="The Ruby programming language"</a> ' +
      '<a href="http://www.coolwell.org"><img src="http://www.sevainc.com/logos/coolwell.jpg" alt="http://www.coolwell.org" title="A great web site for aspiring writers and authors"</a> ' +
      '<a href="http://www.sevasoftware.com"><img src="http://www.sevainc.com/logos/seva.jpg" alt="http://www.sevasoftware.com" title="A great web site for free Ruby software"</a> ' +
      '</p>'
   )
}

// Adds the Seva Software logos to the top of all Seva Software web pages, this includes the last updated date below the logos
function add_logos_top()
{
   add_logos();   // same logos on top and bottom for now
}

// Adds the modified date of the web page to the web page, location can be 'left', 'center', or 'right'
function add_last_updated(location)
{
   var m = "Last Updated: " + document.lastModified;
   //var p = m.length-8;
   //document.write('<BR><font size=2><P align=' + location + '>' + m.substring(p, 0) + '</P></font>');
   document.write('<font size=2><P align=' + location + '>' + m + '</P></font>');
}
