/* Page Controls */
var PageControls = new Array();
function TPageControl(id, active, submitPage) {
  this.id = id;
  this.activePage = (parseInt(active)=="NaN") ? 0 : parseInt(active);
  /* Add to global list */
  this.inList = PageControls.length;
  PageControls.push(this);
  /* Get links */
  this.tabs = new Array();
  div = eval(document.getElementById(this.id + "_tabs"));
  var itab = 0;
  for (var i=0; i<div.childNodes.length; i++)
    if (div.childNodes[i].nodeName.toLowerCase() == "a") {
      tab = div.childNodes[i];
      tab.href = "javascript:PageControls[" + this.inList + "].SetPage(" + itab + ")";
      tab.className = (this.activePage == itab) ? "current" : "";
      this.tabs.push(tab);
      itab++;
    }
  /* Get Pages */
  this.pages = new Array();
  div = eval(document.getElementById(this.id + "_pages"));
  var ipage = 0;
  for (var i=0; i<div.childNodes.length; i++)
    if (div.childNodes[i].nodeName.toLowerCase() == "div") {
      page = div.childNodes[i];
      page.style.display = (this.activePage == ipage) ? "" : "none";
      this.pages.push(page);
      ipage++;
    }
  /* Submitpage */
  if (submitPage) {
    input = document.createElement("input");
    input.type = "hidden";
    input.name = this.id + "_activePage";
    input.value = this.activePage;
    div.appendChild(input);
    this.submitActivePage = input;
  }
}
/* SetPage */
TPageControl.prototype.SetPage = function(page) {
  if (this.activePage==page)
    return;
  if (parseInt(page)=="NaN") {
    alert("PageControl Error: " + page + " is not a number");
    return;
  }
  /* Clamp */
  page = parseInt(page);
  if (page<0)
    page = 0;
  if (page>=this.tabs.length)
    page = this.tabs.length-1;
  /* Hide old page, show new page */
  this.tabs[this.activePage].className = "";
  this.pages[this.activePage].style.display = "none";
  this.activePage = page;
  this.tabs[this.activePage].className = "current";
  this.pages[this.activePage].style.display = "";
  /* submit */
  if (this.submitActivePage)
    this.submitActivePage.value = this.activePage;
}

/* Textareas */
function TTextArea(id) {
  this.id = id;
  this.node = eval(document.getElementById(this.id));
}
TTextArea.prototype.frameSelection = function(sBefore, sAfter) {
  if (typeof sBefore == "undefined")
    sBefore = "";
  if (typeof sAfter  == "undefined")
    sAfter  = "";
  this.node.focus();
  if (typeof document.selection != "undefined") { /* Internet Explorer */
    /* Replace selection */
    var Range = document.selection.createRange();
    var RangeBefore = document.body.createTextRange();
    RangeBefore.moveToElementText(this.node);
    var RangeAfter = RangeBefore.duplicate();
    RangeBefore.setEndPoint("EndToStart", Range);
    RangeAfter.setEndPoint("StartToEnd", Range);
    Range.text = sBefore + Range.text + sAfter;
    /* Update selection */
    Range.setEndPoint("StartToEnd", RangeBefore);
    Range.setEndPoint("EndToStart", RangeAfter);
    Range.moveStart("character", sBefore.length);
    if (Range.text != "")
      Range.moveEnd("character", -sAfter.length);
    Range.select(); 
  }
  else if (typeof this.node.selectionStart != "undefined") { /* Gecko */
    /* Save scroll-position */
    var saveScroll = (typeof this.node.scrollTop != "undefined"), scrollTop, scrollLeft;
    if (saveScroll) {
      scrollTop  = this.node.scrollTop;
      scrollLeft = this.node.scrollLeft;
    }
    /* Replace selection */
    var Start = this.node.selectionStart;
    var End = this.node.selectionEnd;
    var Sel = this.node.value.substring(Start, End);
    this.node.value = this.node.value.substr(0, Start) + sBefore + Sel + sAfter + this.node.value.substr(End);
    /* Update selection */
    this.node.setSelectionRange(Start + sBefore.length, End + sBefore.length);
    /* Update scroll-position */
    if (saveScroll) {
      this.node.scrollTop  = scrollTop;
      this.node.scrollLeft = scrollLeft;
    }
  }
  else
    this.node.value += sBefore + sAfter;
  this.node.focus();
}
/* Focus control at window.onload that is contained in control with id 'focus_onload' */
Array.prototype.contains = function (needle) {
	for (var i=0; i<this.length; i++) {
		if (this[i]==needle) {
			return true;
		}
	}
	return false;
};

function FocusControl() {
	var focusables = ['input', 'textarea', 'select', 'a'];
	var node = document.getElementById('focus_onload');
	if (!node)
		return;
	if (focusables.contains(node.nodeName.toLowerCase())) {
		node.focus();
		return;
	}
	for (var i=0; i<focusables.length; i++) {
		var childs = node.getElementsByTagName(focusables[i]);
		if (childs.length>0) {
			childs[0].focus();
			return;
		}
	} 
}
/* Event Registrar, Simon Willison */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
    window.onload = func;
  else {
    window.onload = function() { oldonload(); func(); }
  }
}
addLoadEvent(FocusControl);