///////////////////////////////////////////////////////////////////////////
//
// General javascript library
//
///////////////////////////////////////////////////////////////////////////
function getById(id) {
 if (document.getElementById) {
  // The standard way
  return document.getElementById(id);
 }
 if (document.all) {
  // IE4+ specific code
  return eval('document.all.' + id);
 }
 if (document.layers) {
  // Netscape 4 specific code
  return eval('document.' + id);
 }
}

function getStyleById(id) {
 if (document.getElementById) {
  // The standard way
  return document.getElementById(id).style;
 }
 if (document.all) {
  // IE4+ specific code
  return eval('document.all.' + id + '.style');
 }
 if (document.layers) {
  // Netscape 4 specific code
  return eval('document.' + id + '.style');
 }
}

function show(obj, set_on)
{
 elm = obj.style.display = (set_on ? "" : "none");
}

// DEPRECATED
function showById(id, set_on) {	 	
 elm = getById(id).style.display = (set_on ? "" : "none");
}

function hideElement(obj)
{
 obj.style.visibility = 'hidden';
}

function showElement(obj)
{
 obj.style.visibility = 'visible';
}

function isVisible(obj)
{
 return obj.style.visibility != 'hidden'
}

function findPosX(obj)
{
 var curleft = 0;
 if (obj.x) {
  // obj.x works on FireFox and Konqueror, but not on IE6
  curleft += obj.x;
 } else if (obj.offsetParent) {
  // obj.offsetLeft however works on IE.
  while (obj.offsetParent) {
   curleft += obj.offsetLeft
   obj = obj.offsetParent;
  }
 }
 return curleft;
}

function findPosY(obj)
{
 var curtop = 0;
 if (obj.y) {
  // obj.y works on FireFox and Konqueror, but not on IE6
  curtop += obj.y;
 } else if (obj.offsetParent) {
  // obj.offsetTop however works on IE, but not in all cases in
  // FireFox.
  while (obj.offsetParent) {
   curtop += obj.offsetTop
   obj = obj.offsetParent;
  }
 } 
 return curtop;
}

// prepend class cname and remove class rcname from element with id.
function modToOrigClass(id, cname, rcname) {
 var tmp = getById(id);
 objModToOrigClass(tmp, cname, rcname);
}

// As modToOrigClass but takes an object instead of an id.
function objModToOrigClass(obj, cname, rcname)
{
 var tmp = obj.className;
 if (!tmp) {
  if (cname != "") {
   obj.className = cname;
    return;
  } 
  return;
 }
 var tmparr = tmp.split(" ");
 var part_num=0;
 var tmparrlen = tmparr.length;
 var result = cname;
 var delim = cname != "" ? " " : "";
 while (part_num < tmparrlen) {
  if (tmparr[part_num] == cname || tmparr[part_num] == rcname) {
   // remove from list of class names
  } else {
   result += delim + tmparr[part_num];
  }
  part_num+=1;
 }
 obj.className = result;
}

function getFirstElement(e)
{
 var c = e.firstChild;
 while (c && !c.tagName) {
  c = c.nextSibling;
 }
 return c;
}

function getNextElement(e)
{
 var c = e.nextSibling;
 while (c && !c.tagName) {
  c = c.nextSibling;
 }
 return c;
}

///////////////////////////////////////////////////////////////////////////
// First some nifty general Array enhancement so that we can push and pop
// and serialize
//
function push() {
 var sub = this.length;
 for (var i = 0; i < push.arguments.length; ++i) {
  this[sub] = push.arguments[i];
  sub++;
 }
}

function pushArray() {
 var sub = this.length;
 for (var i = 0; i < pushArray.arguments.length; ++i) {
  var arg = pushArray.arguments[i];
  for (var j = 0; j < arg.length; ++j) {
   this[sub] = arg[j];
   sub++;	
  }
 }
}

function pop() {
 var lastElement = this[this.length - 1];
 this[this.length - 1] = null;
 this.length--;
 return lastElement;
}

function toEvalString()
{
 if (this == null) {
  return "null";
 }
 var l_len = this.length;
 var i = 0;
 var res = "[";
 for ( ; i < l_len ; i++) {
  if (i != 0) {
   res += ",";
  }
  res += "\"" + this[i] + "\"";
 }
 return res + "]";
}
Array.prototype.push = push;
Array.prototype.pushArray = pushArray;
Array.prototype.pop = pop;
Array.prototype.toEvalString = toEvalString;

function tokenize(str, delim)
{
 var last_index = 0;
 var index = str.indexOf(delim);
 var i = 0;
 var result =  new Array();
 while (index != -1) {
  result[i] = str.substring(last_index, index);
  i = i + 1;
  // skip the ':'
  last_index = index + 1;
  index = str.indexOf(delim, last_index); // start search after last match found
 }
 result[i] = str.substring(last_index);
 return result;
}

function id2obj_array(idarr)
{
 var res = new Array();
 for (i=0; id = idarr[i]; i++) {
  res.push(getById(id));
 }
 return res;
}

function obj2id_array(idarr)
{
 var res = new Array();
 for (var i=0; id = idarr[i]; i++) {
  // alert("bla " + id.id);
  res.push(id.id);
 }
 return res;
}

///////////////////////////////////////////////////////////////////////////
// Special window creation that allowes connection between 
// parent and child window.
//
function newWindow(file, window, args) {
 var msgWindow=open(file,window, args);
 // Let the children windows know about the parent
 if (msgWindow.opener == null) msgWindow.opener = self;
 return msgWindow;
}

///////////////////////////////////////////////////////////////////////////
// DO NOT USE. delay function.
//
function delay(gap) { 
 /* gap is in millisecs */
 var then,now; then=new Date().getTime();
 now=then;
 while((now-then)<gap) {
  now=new Date().getTime();
 }
}

// getWindowInfo will return the with of the HTML-window, and how much
// the page is scrolled. Both in pixels.
function getWindowInfo() {
 var obj = new Object;
 if (document.all) {
  // IE doesn't play the same way as other children :-)
  obj.sizeX = document.body.clientWidth;
  obj.sizeY = document.body.clientHeight;
  obj.offsetX = document.body.scrollLeft;
  obj.offsetY = document.body.scrollTop;
 } else {
  obj.sizeX = window.innerWidth;
  obj.sizeY = window.innerHeight;
  obj.offsetX = window.pageXOffset;
  obj.offsetY = window.pageYOffset;
 }
 return obj;
}

///////////////////////////////////////////////////////////////////////////
// Function to send HTTP requests from JavaScript.
//
// You simply call it like this:
//
// sendRequest("http://yada", callback_function);
//
// The callback_function should take one argument, which is the
// resulting XMLHttpRequest object.
//
function sendRequest(url, callback, postData) {
 var req = createXMLHTTPObject();
 if (!req) return;
 var method = (postData) ? "POST" : "GET";
 req.open(method,url,true);
 req.setRequestHeader('User-Agent','XMLHTTP/1.0');
 if (postData)
  req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  req.onreadystatechange = function () {
  if (req.readyState != 4) return;
  if (req.status != 200 && req.status != 304) {
   // alert('HTTP error ' + req.status);
   return;
  }
  callback(req);
 }
 if (req.readyState == 4) return;
 req.send(postData);
}
var XMLHttpFactories = [
 function () {return new XMLHttpRequest()},
 function () {return new ActiveXObject("Msxml2.XMLHTTP")},
 function () {return new ActiveXObject("Msxml3.XMLHTTP")},
 function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
 var xmlhttp = false;
 for (var i=0;i<XMLHttpFactories.length;i++) {
  try {
   xmlhttp = XMLHttpFactories[i]();
  } catch (e) {
   continue;
  }
  break;
 }
 return xmlhttp;
}

