// global variables for timeout and for current menu
var t=false,current;
var inputcount=0, IsDirty=false;

function SetDirty()
{
      IsDirty = true;
}

function CheckIsDirty(e)
{
      if (IsDirty)
      {
            IsDirty = false;
            alert('Save your work?');


            var frm = window.event? window.event.srcElement: e ? e.target: null;
            if (!frm) return;
            
            frm.submitAllowed = false;
            if (e && e.stopPropagation && e.preventDefault)
            {
                  e.stopPropagation();
                  e.preventDefault();
            }
            if (window.event)
            {
                  window.event.cancelBubble = true;
                  window.event.returnValue = false;
                  return false;
            }
      }
      IsDirty = false;

}

function RemoveDirty()
{
      IsDirty = false;
}

function addEvent(elem, evType, fn, useCapture)
{
      // cross browser event handling 
      // by Scott Andrew
      if (elem.addEventListener)
      {
            elem.addEventListener(evType,fn,useCapture);
            usingIE = false;
            return true;
      } 
      else if (elem.attachEvent)
      {
            var r = elem.attachEvent('on' + evType, fn);
            return r;
      }
      else
      {
            elem('on' + evType) = fn;
      }
}


function SetupMenu() {
   items=document.getElementsByTagName("li");
   for (i=0; i<items.length; i++) {
      if (items[i].className != "menu") continue;
      //set up event handlers
//      thelink=findChild(items[i],"A");
      tlink = findChild(items[i],"A");
      thelink = findChild(items[i],"IMG");
      if (thelink == false) thelink = tlink;
      thelink.onmouseover=ShowMenu;
      thelink.onmouseout=StartTimer;
      //is there a submenu?
      if (ul=findChild(items[i],"UL")) {
         ul.style.display="none";
         for (j=0; j<ul.childNodes.length; j++) {
            ul.childNodes[j].onmouseover=ResetTimer;
            ul.childNodes[j].onmouseout=StartTimer;
         }
      }
   } 

   inputcount = document.getElementById('inputcount').value;
   for (i = 0; i < inputcount; i++)
   {
      cfield = document.getElementById('inputfield'+i);
      addEvent(cfield,'change',SetDirty,false);
   }
   
   buttoncount = document.getElementById('buttoncount').value;
   for (i = 0; i < buttoncount; i++)
   {
      cfield = document.getElementById('button'+i);
      addEvent(cfield,'click',CheckIsDirty,false);
   }
   if (document.getElementById('savebutton'))
   {
      cfield = document.getElementById('savebutton');
      addEvent(cfield,'click',RemoveDirty,false);
   }
}

// find the first child object of a particular type
function findChild(obj,tag) {
   cn = obj.childNodes;
   for (k=0; k<cn.length; k++) {
     if (cn[k].nodeName==tag) return cn[k];
   }
   return false;
}

function ShowMenu(e) {
   CheckIsDirty();
   if (!e) var e = window.event;
   // which link was the mouse over?
   thislink = (e.target) ? e.target: e.srcElement;
   ResetTimer();
   // hide the previous menu, if any
   if (current) HideMenu(current);
   // we want the LI, not the link
   thislink = thislink.parentNode;
   current=thislink;
   // find the submenu, if any
   ul = findChild(thislink,"UL");
   if (!ul) return;
   ul.style.display="block";
}

function HideMenu(thelink) {
   // find the submenu, if any
   ul = findChild(thelink,"UL");
   if (!ul) return;
   ul.style.display="none";
}

function ResetTimer() {
   if (t) window.clearTimeout(t);
}

function StartTimer() {
   t = window.setTimeout("HideMenu(current)",500);
}

// Set up the menu when the page loads
window.onload=SetupMenu;
