/**
 *  $Id: vzamenu.js,v 1.2 2007/01/09 13:26:34 cvs Exp $
 *
 *  $Log: vzamenu.js,v $
 *  Revision 1.2  2007/01/09 13:26:34  cvs
 *  Revised menus, moving menu construction entirely to the server, so that
 *  links are static for search engine robots.
 *  Javascript now only swaps the menu displays.
 *
 *  Revision 1.1  2007/01/08 17:49:22  cvs
 *  Initial Entry
 *
 *
 * The menu is activated using an instrument panel.
 * The main menu is chosen with a switch and the submenus are displayed on the
 * instrument panel screen.
 */
var theMenu;

function Menu()
{
  this.menuItems = new Array();
  this.currentMenu = null;
  this.glancedMenu = null;
  this.btnOut = null;
  this.btnOver = null;
  this.panelUp = true;
  this.visibleForm = getRawObject('login');
  this.hiddenForm = getRawObject('register');
  this.visibleButton = getRawObject('btn_register');
  this.hiddenButton = getRawObject('btn_login');
}

Menu.prototype.buttonDown = function(img,pic)
{
  var image = getRawObject(img);
  image.src = pic;
}

Menu.prototype.login = function()
{
  var form = this.visibleForm;
  if(arguments.length)
  {
    if(typeof arguments[0] == 'boolean' && arguments[0] == true)
    {
      form.style.display = 'none';

      this.visibleForm = this.hiddenForm;
      this.visibleForm.style.display = 'block';
      this.hiddenForm = form;

      var button = this.visibleButton;
      button.style.display = 'none';
      this.visibleButton = this.hiddenButton;
      this.visibleButton.style.display = 'block';
      this.hiddenButton = button;
      return;
    }
    if(validateForm(form))
      form.submit();
    return;
  }
  form.reset();
}

Menu.prototype.back = function()
{
  history.go(-1);
}

Menu.prototype.appendSubMenu = function(sm)
{
  var length = this.menuItems.length;
  if(3<length)
    throw "Maximum 3 switch menu items allowed!"
  this.menuItems[length] = sm;
  this.menuItems[sm] = this.menuItems[length];
}

Menu.prototype.onMenuItem = function(page,target)
{
  window.open(page,target);
  window.open('vzatouch.php','params1');
}

Menu.prototype.writeSubMenu = function(smName)
{
  var m, td;
  this.currentMenu = this.menuItems[smName];
  if(!this.currentMenu)
    return;

  for(m=0;m<this.menuItems.length;m++)
  {
    var btn = getRawObject("btn"+this.menuItems[m]);
    var tbl = getRawObject("tbl"+this.menuItems[m]);
    if(btn && tbl)
    {
      if(this.menuItems[m] == smName)
      {
        // Show the menu
        tbl.className = "menuselected";

        // Disable events from the current button
        btn.src = "images/button_"+this.menuItems[m]+"_down.gif";
        btn.onmouseout = null;
        btn.onmousedown = null;
        btn.onmouseover = null;
      }
      else
      {
        // Hide other menus
        tbl.className = "menudefault";

        // Pop up the other radio buttons and enable their events
        btn.src = "images/button_"+this.menuItems[m]+"_up.gif";
        btn.onmouseout = menuButtonEvent;
        btn.onmousedown = menuButtonEvent;
        btn.onmouseover = menuButtonEvent;
      }
    }
  }
}

Menu.prototype.openMagazine = function(content,target)
{
  window.open('magazine/index.php','vzamain');
}

function resetMenuButtonEvent()
{
  var current = getRawObject("tbl"+theMenu.currentMenu);
  var glanced = getRawObject("tbl"+theMenu.glancedMenu);
  glanced.className = "menudefault";
  current.className = "menuselected";
}

function showMenuButtonEvent()
{
  var current = getRawObject("tbl"+theMenu.currentMenu);
  var glanced = getRawObject("tbl"+theMenu.glancedMenu);
  glanced.className = "menuselected";
  current.className = "menudefault";
}

function menuButtonEvent(evt)
{
  var ev = evt?evt:window.event;
  if(ev)
  {
    clearTimeout(theMenu.btnOver);
    clearTimeout(theMenu.btnOut);
    var elem = ev.target?ev.target:ev.srcElement;
    var name = new String(elem.id);
    name = name.substring(3);
    var tbl = getRawObject("tbl"+name);
    var menu = getRawObject("tbl"+theMenu.currentMenu);
    var glanced = getRawObject("tbl"+theMenu.glancedMenu);
    switch(ev.type)
    {
    case "mousedown":
      elem.src = "images/button_"+name+"_down.gif";
      break;
    case "mouseout":
      elem.src = "images/button_"+name+"_up.gif";
      resetMenuButtonEvent();
      break;
    case "mouseover":
      elem.src = "images/button_"+name+"_over.gif";
      theMenu.glancedMenu = name;
      theMenu.btnOver = setTimeout(showMenuButtonEvent,250);
      break;
    }
  }
}



function SubMenu(name)
{
  this.name = name;
  this.menuItems = new Array();
}

SubMenu.prototype.appendMenuItem = function(mi)
{
  var length = this.menuItems.length;
  this.menuItems[length] = mi;
  this.menuItems[mi.text] = this.menuItems[length];
}

function SubMenuItem(text, href, target)
{
  this.text = text;
  this.href = href;
  this.target = target;
}


var req;

function loadXMLDoc(url)
{
  // branch for native XMLHttpRequest object
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send(null);
  // branch for IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
      req.onreadystatechange = processReqChange;
      req.open("GET", url, true);
      req.send();
    }
  }
}

function processReqChange()
{
  // only if req shows "complete"
  if (req.readyState == 4) {
    // only if "OK"
    if (req.status == 200) {
      // ...processing statements go here...
    } else {
      alert("There was a problem retrieving the XML data:\n" + req.statusText);
    }
  }
}


/*
* This function needs a rewrite to allow for events that change the source
* The source (src) only needs to be changed once when the time interval is
* passed. The src is changed afterwards when a user moves the mouse over the
* image. Changing the image at the next second defeats the onmouseup event.
*/
function monitor()
{
  var s = "";

  if(top.sessionTime)
  {
    top.sessionTime--;

    var btnlogout = getRawObject("btnlogout");
    if(btnlogout)
    {
      var src = new String(btnlogout.src);
      if(top.sessionTime<300)
      {
        if(top.sessionTime<180)
        {
          if(!top.sessionTime)
            logout();

          if(-1==src.indexOf("imminent"))
          {
            // The imminent image is NOT being displayed, so show it
            btnlogout.src = "images/button_logout_timeout_imminent.gif";
            btnlogout.onmouseup = "this.src=images/button_logout_timeout_imminent.gif";
            btnlogout.onmouseout = "this.src=images/button_logout_timeout_imminent.gif";
          }
        }
        else
        {
          if(-1==src.indexOf("warning"))
          {
            // The warning image is NOT being displayed, so show it
            btnlogout.src = "images/button_logout_timeout_warning.gif";
            btnlogout.onmouseup = "this.src=images/button_logout_timeout_warning.gif";
            btnlogout.onmouseout = "this.src=images/button_logout_timeout_warning.gif";
          }
        }
      }
      else
      {
        if(-1==src.indexOf("up"))
        {
          // The normal image is NOT being displayed, so show it
          btnlogout.src = "images/button_logout_up.gif";
          logout.onmouseup = "this.src=images/button_logout_up.gif";
          logout.onmouseout = "this.src=images/button_logout_up.gif";
        }
      }

      // Display a counter to show the timeout
      var st = getRawObject("sessiontimeout");
      if(st)
      {
        var mins = Number(Math.floor(top.sessionTime/60)).toString();
        var secs = Number(Math.floor(top.sessionTime%60)).toString();
        if(2>mins.length) mins = "0"+mins;
        if(2>secs.length) secs = "0"+secs;
        st.innerHTML = "Sessie op in "+mins+":"+secs;
      }
    }
  }
  setTimeout(monitor,1000)
/*
    var form = document.createElement("form");
    form.action="pilot/vzaauthorise.php";
    form.method="post";

    var input = document.createElement("input");
    input.type = "hidden";
    input.name = "_logout";
    input.value = "logout";

    form.appendChild(input);
    document.body.appendChild(form);
    form.submit();
*/
}

function logout()
{
  var f = document.createElement("form");
  f.action = "pilot/vzaauthorise.php";
  f.method = "post";
  var i = document.createElement("input");
  i.name = "_logout";
  f.appendChild(i);
  document.body.appendChild(f);
  f.submit();
}


function isEmail(address)
{
  var s = new String(address);
  var re = /^[\w-_]+(\.[\w-_]+)*@([\w-_]+\.)+[a-zA-Z]{2,7}$/;
  return s.match(re);
}

// Validate the login
function validateForm(form,action)
{
  var errors = new String();
  if(form.register && form.register.value.length)
  {
    // Users name must have a valid e-mail address
    if(!isEmail(form.register.value))
      errors += 'Ongeldig e-mail adres';
  }
  else
  {
    if(form.user.value.length < 3)
      errors += 'Ongeldig gebruikers naam\n';

    if(form.passwd.value.length < 3)
      errors += 'Ongeldig wachwoord';
  }
  if(!errors.length)
    return true;

  msg  = "______________________________________________________\n\n"
  msg += "Het informatie kan niet gestuurd worden omdat er zijn\n";
  msg += "er fouten.\n";
  msg += ".\n";
  msg += "______________________________________________________\n\n"

  alert(errors);
  return false;
}


/**
 *  Lowers or raises the instrument panel
 */
function showPanel()
{
  if(theMenu.panelUp)
  {
    parent.resizeFrame(20);
    theMenu.panelUp = false;
  }
  else
  {
    parent.resizeFrame(parent.navWinHeight);
    theMenu.panelUp = true;
  }
}


