/**************************************************************************
 * JAVASCRIPT UTILITIES
 *
 * Version 1.0
 *
 * Copyright by BeyondVision, 2007
 **************************************************************************/

function objById(id) {
  if (document.all)
    return document.all[id];
  return document.getElementById(id);
}


function getPageSize() {

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = [pageWidth, pageHeight, windowWidth, windowHeight];
	return arrayPageSize;
}

function getScrollXY() {
  var scrOfX = 0;
  var scrOfY = 0;
  if (typeof( window.pageYOffset ) == 'number') {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [scrOfX, scrOfY];
}

function getTop(obj, v) {
  return parseInt(obj.style.top);
}

function setTop(obj, v) {
  obj.style.top = v+'px';
}

function getLeft(obj, v) {
  return parseInt(obj.style.left);
}

function setLeft(obj, v) {
  obj.style.left = v+'px';
}

function getWidth(obj, v) {
  return parseInt(obj.style.width);
}

function setWidth(obj, v) {
  obj.style.width = v+'px';
}

function getHeight(obj, v) {
  return parseInt(obj.style.height);
}

function setHeight(obj, v) {
  obj.style.height = v+'px';
}

function getMargin(obj) {
  return [parseInt(obj.style.marginTop), parseInt(obj.style.marginRight),
          parseInt(obj.style.marginBottom), parseInt(obj.style.marginLeft)];
}

function setMargin(obj, top, right, bottom, left) {
  obj.style.marginTop = top+'px';
  obj.style.marginRight = right+'px';
  obj.style.marginBottom = bottom+'px';
  obj.style.marginLeft = left+'px';
}

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

function setVisibility(obj, value) {
  if (value) {
    obj.style.visibility = 'visible';
  } else {
    obj.style.visibility = 'hidden';
  }
}

function getDisplayBool(obj) {
  return !(obj.style.display == 'none');
}

function setDisplayBool(obj, value) {
  if (value) {
    obj.style.display = 'block';
  } else {
    obj.style.display = 'none';
  }
}

function getOpacity(obj) {
  var opacity;
  if (typeof(eval(obj.style)['opacity']) != 'undefined')
    opacity = obj.style['opacity'];
  if (typeof(eval(obj.style)['MozOpacity']) != 'undefined')
    opacity = obj.style['MozOpacity'];
  if (typeof(eval(obj.style)['KHTMLOpacity']) != 'undefined')
    opacity = obj.style['KHTMLOpacity'];
  if (typeof(eval(obj.style)['filter']) != 'undefined') {
    opacity = obj.style['filter'].match(/alpha\(opacity=(.*)\)/);
    if (opacity[1])
      return parseFloat(opacity[1] / 100);
  }
  if (opacity == parseFloat(opacity)) {
    return opacity;
  } else return 0;
}

function setOpacity(obj, value){
  if (value == 1){
    obj.style['opacity'] = value;
    obj.style['MozOpacity'] = value;
    obj.style['KHTMLOpacity'] = value;
    if (typeof(eval(obj.style)['filter']) != 'undefined') {
      obj.style['filter'] = obj.style['filter'].replace(/alpha\([^\)]*\)/gi, '');
    }
  } else {
    if (value < 0.001)
      value = 0;
    obj.style['opacity'] = value;
    obj.style['MozOpacity'] = value;
    obj.style['KHTMLOpacity'] = value;
    if (typeof(eval(obj.style)['filter']) != 'undefined') {
      obj.style['filter'] = obj.style['filter'].replace(/alpha\([^\)]*\)/gi, '')+'alpha(opacity='+(value*100)+')';
    }
  }
}

function getText(obj) {
  return obj.innerHTML;
}

function setText(obj, value) {
  obj.innerHTML = value;
}

function preloadImage(url) {
  var Preloader = new Image();
  Preloader.src = url;
}

function addEvent(obj, eventType, fnct, useCaption) {
  if (obj.addEventListener) {
    obj.addEventListener(eventType, fnct, useCaption);
    return true;
  } else if (obj.attachEvent) {
    var retVal = obj.attachEvent("on"+eventType, fnct);
    return retVal;
  } else {
    return false;
  }
}

function focusFirst() {
  var FirstFocusableFound = false;

  for (f=0; f < document.forms.length; f++) {
    // for each element in each form
    for(i=0; i < document.forms[f].length; i++) {
      // if it's not a hidden element
      if ((document.forms[f][i].type != "hidden") && (document.forms[f][i].type != null)) {
        // and it's not disabled
        if (document.forms[f][i].disabled != true) {
            // set the focus to it
            document.forms[f][i].focus();
//            document.forms[f][i].select();
            var FirstFocusableFound = true;
        }
      }
      // if found in this element, stop looking
      if (FirstFocusableFound == true)
        break;
    }
    // if found in this form, stop looking
    if (FirstFocusableFound == true)
      break;
  }
}

function mail_yupe(account) {
  var host = 'yupe.de';
  location.href = 'mail'+'to:'+account+'@'+host;
}

function mail_to(host, account) {
  location.href = 'mail'+'to:'+account+'@'+host;
}

function mail_to_print(link, host, account) {
  setText(link, account+'@'+host);
}

function toggle_visibility(id) {
  obj = getObjectById(id);
  if (obj.style.visibility == 'visible') {
    obj.style.visibility = 'hidden';
  } else {
    obj.style.visibility = 'visible';
  }
}

function toggle_display(id) {
  obj = getObjectById(id);
  if (obj.style.display == 'none') {
    obj.style.display = 'block';
  } else {
    obj.style.display = 'none';
  }
}

function toggle_height(id, size) {
  obj = objById(id);
  if (obj.style.height == 'auto') {
    obj.style.height = size+'px';
  } else {
    obj.style.height = 'auto';
  }
}

function chk_select_all(form) {
  for (var i=0; i<form.elements.length; i++) {
    var chk = form.elements[i];
    chk.checked = true;
  }
}

function chk_invert(form) {
  for (var i=0; i<form.elements.length; i++) {
    var chk = form.elements[i];
    chk.checked = !chk.checked;
  }
}

/*
  Insert a new node to a select element.
*/
function insertNode(node, dest){
  var p = 0;
  var i = 0;

  // append node
  dest.options[dest.length] = node;

  // determine correct position
  for (p = 0; p < dest.length; p++) {
    if (dest.options[p].text >= node.text)
      break;
  }

  // move nodes between the position and the end to the end
  if (p < dest.length-1) {
    var l = dest.length-1;
    for (i = p; i < dest.length-1; i++) {
      var x = dest.options[p];
      dest.options[p] = null;
      dest.options[l] = x;
    }
  }
}

/*
  Move a node from one select element to another
*/
function moveNode(src, dest) {
  var i = 0;

  if ((typeof src != "undefined") && (typeof dest != "undefined")) {
    for (i = src.length-1; i >= 0; i--) {
      node = src.options[i];
      if (node.selected) {
        insertNode(new Option(node.text, node.value), dest);
        src.options[i] = null;
      }
    }
  }

}

/*
  Select all nodes of the given select element. Thus all values are passed.
*/
function selectAllNodes(obj) {
  var i = 0;

  for (i = 0; i < obj.options.length; i++) {
    obj.options[i].selected = true;
  }
}



function installTips() {
  addEvent(document, 'mousemove', showTip, false);
}

function showTip(event) {
  var event = (event) ? event : ((window.event) ? window.event : "");
  var obj = (event.target) ? event.target : event.srcElement;
  while (!obj.attributes['tip']) {
    if (obj.tagName == 'HTML') {
      obj = null;
      break;
    }
    obj = (obj.parentNode)? obj.parentNode : obj.parentElement;
    if (!obj)
      break;
  }
  if (obj) {
    var scroll = getScrollXY();
    var x = event.clientX;
    var y = event.clientY;
    setLeft(objById('tipDIV'), x + scroll[0] + 10);
    setTop(objById('tipDIV'), y + scroll[1] + 10);
    setText(objById('tipDIV'), obj.attributes['tip'].value);
    setDisplayBool(objById('tipDIV'), true);
  } else {
    objById('tipDIV').style.display = 'none';
  }
}