/*
 * updated 2004-12-07 13:02:16
 */


//return boolean
function isUserAgent() {
   return document.getElementById && (window.attachEvent || window.addEventListener);
}
//global function
function getObj(x) {
    if (document.getElementById)
    	return document.getElementById(x);
    else if (document.all && document.all(x) )
    	return document.all(x);
    else if (document.layers && document.layers[x])
    	return document.layers[x];
    else
    	return false;
}


function getNextSiblingByNodeName(element, nodeName) {
	do element = element.nextSibling;
	while (element && (element.nodeName.toLowerCase() != nodeName.toLowerCase()));
	return element;
}

function getPreviousSiblingByNodeName(element, nodeName) {
	do element = element.previousSibling;
	while (element && (element.nodeName.toLowerCase() != nodeName.toLowerCase()));
	return element;
}

function getFirstChildByNodeName(element, nodeName) {
	element = element.firstChild;
	if (element && (element.nodeName.toLowerCase() != nodeName.toLowerCase()))
		return getNextSiblingByNodeName(element, nodeName);
	else
		return element;
}

function getParentNodeByName(element, nodeName) {
  if(!isUserAgent) return; //cut off
	do element = element.parentNode;
	while (element && (element.nodeName.toLowerCase() != nodeName.toLowerCase()));
	return element;
}


//swap all checkboxes by the element ID
function invertAllCheckbox(id) {
    var root = getObj(id);
  var boxes = root.getElementsByTagName("input");
  for(i=0; i<boxes.length; i++) {
    if(boxes[i].type.toLowerCase() == "checkbox") {
      if(boxes[i].checked == true){
        boxes[i].checked = false;
      }//end: if - checked boxes
      else if(boxes[i].checked == false){
        boxes[i].checked = true;
      }//end: if - NONchecked boxes
    }
  }//end: for
  return false;
}

function resetAllCheckbox(id) {
    swapAllCheckbox(getCheckboxes(getObj(id)));
    return false;
}

function getCheckboxes(element)
{
    var resultBoxes = [];
    var inputs = element.getElementsByTagName("input");
    for(i=0; i<inputs.length; i++) {
        if(inputs[i].type.toLowerCase() == "checkbox") {
            resultBoxes.push(inputs[i])
        }
    }//end: for
    return resultBoxes;
}


//boxes: array
function swapAllCheckbox(boxes)
{
    var swapmode = true;
    for(i=0; i<boxes.length; i++) {
        if(boxes[i].checked == true){
            swapmode = false;
        }
    }//end: for
    for(i=0; i<boxes.length; i++) {
        boxes[i].checked = swapmode;
    }//end: for
    return false;
}



function chnChk(element) {
 var row = this.getParentNodeByName(element, "tr")
 var chkBox = row.getElementsByTagName("input")[0];
 row.style.backgroundColor = "#DADBEA";
 chkBox.checked = "checked";
 return false;
}

function setChangeStatusActions(id) {
  var root = getObj(id);
  var elements = root.getElementsByTagName("input");
  for(i=0; i<elements.length; i++) {
   if(elements[i].type.toLowerCase() != "checkbox")  continue;
   if(elements[i].type.toLowerCase() != "submit")  continue;
    elements[i].onblur = chnChk(this);
  }
}

function initTableRows(id, hoverColor) {
    var root = getObj(id);
    var rows = root.getElementsByTagName("tr");
    for (j=0; j<rows.length; j++){
    var row = rows[j];
        //row.origBackgroundColor = row.style.backgroundColor;
        //row.origBorder = row.style.border;
        row.onmouseover = function(){
            setCellsBackground(this, hoverColor)
        }
    }
}

function setCellsBackground(row, color){
    row.origCellBackg = [];
    row.origCellBorderColor = [];
    var cells = row.getElementsByTagName("td");
    for (var j=0; j<cells.length; j++){
        row.origCellBackg.push(cells[j].style.backgroundColor)
        row.origCellBorderColor.push(cells[j].style.borderColor)
        //cells[j].style.backgroundColor = color;
        cells[j].style.borderColor = color;
    }
    row.onmouseout = function() {
        for (var j=0; j<cells.length; j++){
            cells[j].style.backgroundColor = this.origCellBackg[j];
            cells[j].style.borderColor = this.origCellBorderColor[j];
        }
    }
}

//returns true || false
function isInArray(item, arr) {
    for(var x in arr) {
       if(item == arr[x])
       return true;
    }
    return false;
}

function confirmAction(mode){
    var msg = [];
    msg["process"] = "Really mark this user as PROCESSED ?";
    msg["unprocess"] = "Really mark this user as UNPROCESSED ?";
    msg["del"] = "Wirklich löschen ?";
    if(!mode) mode = "del";
    return window.confirm(msg[mode]);
}

function confirmMsg(msg){
    return window.confirm(msg);
}

//prototypes
Array.prototype.in_array = function (needle) {
    for (var x in this) {
        if (x == 0 && needle == 0) return false;
        if(this[x] == needle) {
            return true;
        }
    }
    return false;
}
//removes an item which equals the value
Array.prototype.deleteValue = function deleteFromArray(val) {
    for (var x in this) {
        if(this[x] == val) {
            this.splice(x, 1)
        }
    }
}

Array.prototype.push = function (item) {
    this[this.length] = item;
}

