/* JavaScript Utilities.
   (c) Kaspar Fischer, <kfischer@iiic.ethz.ch>
   (See individual functions below for additional copyright notices.)
 
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   Important Note:
   If you include this file using some <script language=...> tag, be
   sure to always use the same language specifier within your pages!
   If you, for instance, include this file as JavaScript 1.3 and later
   perform some JavaScript 1.2 statements, you will get undesired
   behaviour since the former file will find that some new methods (in
   Netscape) are available and will *not* patch them appropriately.
   Later, when you use JavaScript 1.2 statements, you will thus get
   undesired behaviour since the new methods are not visible any more.

   History of changes:
   28/07/01   Added functions prepare, roll, unroll which should
              make rollovers a little easier to maintain.
   01/09/01   Added a script by Peter Belesis to get consistent
              behaviour for array methods shift, unshift, push,
              pop and slice (see below for more information).
   14/09/01   Added function openWindow.
   
*/

function prepare(name) {
  /* This is known to work on Netscape 4.7 and Explorer 5.
     I hope older browsers don't bail out... */
  if (eval("typeof(RO"  +name+")")=="undefined" ||
      eval("typeof(ROr_"+name+")")=="undefined") {
    eval("RO"  +name+" = new Image();" +
         "RO"  +name+".src = 'images/"+name+".gif';" +
         "ROr_"+name+" = new Image();" +
         "ROr_"+name+".src = 'images/rollover/"+name+".gif'");
  }
}

function roll(name) {
  prepare(name);
  document.images[name].src = eval('ROr_'+name+'.src')
}

function unroll(name) {
  prepare(name);
  document.images[name].src = eval('RO'+name+'.src')
}

function nop() {
}

/* The following is a hack to get consistent behaviour of the shift,
   unshift, push, pop and splice array properties on older versions of
   Netscape and Explorer.  The following is adopted from a script by
   Peter Belesis (see <http://www.webreference.com/dhtml/column33/>). */

if (Array.prototype.push && ([0].push(true)==true)) /* In Netscape's
                            JS 1.2 push doesn't return the new array
                            length but the pushed element instead -
                            which is undesired. */
  Array.prototype.push = null;

if (Array.prototype.splice && typeof([0].splice(0))=="number") /* In
                            Netscape's JS 1.2 splice doesn't always
                            return an array, which is undesired. */
  Array.prototype.splice = null;

if (!Array.prototype.shift) {
  function array_shift() {
    firstElement = this[0];
    this.reverse();
    this.length = Math.max(this.length-1,0);
    this.reverse();
    return firstElement;
  }
  Array.prototype.shift = array_shift;
}

if (!Array.prototype.unshift) {
  function array_unshift() {
    this.reverse();
    for(var i=arguments.length-1;i>=0;i--){
      this[this.length]=arguments[i]
    }
    this.reverse();
    return this.length
  }
  Array.prototype.unshift = array_unshift;
}

if (!Array.prototype.push) {
  function array_push() {
    for(var i=0;i<arguments.length;i++){
      this[this.length]=arguments[i]
    };
    return this.length;
  }
  Array.prototype.push = array_push;
}

if (!Array.prototype.pop) {
  function array_pop(){
    lastElement = this[this.length-1];
    this.length = Math.max(this.length-1,0);
    return lastElement;
  }
  Array.prototype.pop = array_pop;
}

if (!Array.prototype.splice) {
  function array_splice(ind,cnt){
    if (arguments.length == 0) return ind; /* (ie. return undefined) */
    if (typeof ind != "number") ind = 0;
    if (ind < 0) ind = Math.max(0,this.length + ind);
    if (ind > this.length) {
      if  (arguments.length > 2) ind = this.length;
      else return [];
    }
    if  (arguments.length < 2) cnt = this.length-ind;
    cnt = (typeof cnt == "number") ? Math.max(0,cnt) : 0;
    removeArray = this.slice(ind,ind+cnt);
    endArray = this.slice(ind+cnt);
    this.length = ind;
    for(var i=2;i<arguments.length;i++){
      this[this.length] = arguments[i];
    }
    for(var i=0;i<endArray.length;i++){
      this[this.length] = endArray[i];
    }
    return removeArray;
  }
  Array.prototype.splice = array_splice;
}

function openWindow(url,innerWidth,innerHeight) {
  window.open(url,'','height='+innerHeight+',width='+innerWidth+',resizable=1,scrollbars=1');
}
