// Toggles the elements' visibility by switching between the 'switchable-hidden' and 'switchable-visible' classes. Currently does not support multiple classes on the element
function toggleVisibility() {
  for (var i = 0; i < arguments.length; i++) {
    var el = document.getElementById(arguments[i]);
    var newValue = el.className == 'switchable-hidden' ? 'switchable-visible' : 'switchable-hidden';
    el.className = newValue;
  }
}

// this method limit the length of a field
function limitLength(element, maxLength) {
    if (element.value.length >= maxLength) {
        element.value = element.value.substring(0, maxLength);
    }
}

// this method checks all boxes accordingly when masterbox is checked/unchecked
// this method checks all boxes accordingly when masterbox is checked/unchecked
function checkAll(masterBox, chkBoxes) {
    var count = chkBoxes.length;
    for (var i = 0; i < count; i++) {
        chkBoxes[i].checked = masterBox.checked;
    }
}

// change the pagetitle
function changePageTitle(text) {
	var titleElement = document.getElementById('pagetitle');
	titleElement.innerHTML = text;
}
	

// highlights the given tab
function tab(tabName) {
  changeClass('menu-' + tabName, 'selected');
}

// gives focus to an element with given name
function defaultFocus(elementId) {
  addToStart("document.getElementById('" + elementId + "').focus()");
}

// set given class to an element with given id
function changeClass(elementId, className) {
  addToStart("document.getElementById('" + elementId + "').className='" + className + "'");
}

// add a function to execute in the onLoad page
function addToStart(fnc) {
  if (!window.listStart) window.listStart = new Array();
  window.listStart.push(fnc)
}

// function that will call all the registered function on page loading
function start() {
  var ls = window.listStart;
  if (ls) {
    for (var i = 0; i < ls.length; i++) {
      fnc = ls[i];
      if (typeof(fnc) == 'function') {
        fnc();
      } else {
        eval(fnc);
      }
    }
  }
}

// method start is called on the page loading
window.onload = start;


