var currentButton;
var contentDiv;

/**************************************
 ** Execute the following when the page is ready.
 *************************************/
window.addEvent('domready', function(){
  initMenu();
});

/**
 * Get a random number to add to the parameter of AJAX
 * to get a unique URL. This solves the problem of
 * caching in Internet Explorer.
 */
function getUniqueParam() {
  return "&" + Math.round(Math.random() * 1000000);
}

/**
 * Update an element.
 */
function updateElement(elementId, url, params) {
  new Ajax(url + '?' + params + getUniqueParam(), {
    method: 'get',
    update: $(elementId),
    evalScripts: true
  }).request();
}

function updateContent(page) {
  updateElement('contentDiv', 'page.php', 'page=' + page);
}

function initMenu() {
  currentButton = document.getElementById('onLoadButton');
  contentDiv = document.getElementById('contentDiv');
}

function buttonAction(button, page) {
  if (button != currentButton) {
    currentButton.className = 'inactive';
    button.className = 'active';
    updateContent(page);
    currentButton = button;
  }
}

function registerForm(formName, updateElementName) {
  $(formName).addEvent('submit', function(e) {
    new Event(e).stop();

    var updateElement = $(updateElementName);

    this.send({
      update: updateElement,
      evalScripts: true
    });
  });
}