var popup_window, prev_window;
var popup_number = 0;

function submitform(name) {
  if (name) {
    document.forms[name].submit();
  }
}

function setAllCheckboxes(formId, curBox) {
  if (document.getElementById) {
    newvalue = curBox.checked;
    inputElemente = document.getElementById(formId).getElementsByTagName('input');
    for (var i = 0; i < inputElemente.length; i++) {
      if (inputElemente[i].type == 'checkbox') {
        inputElemente[i].checked = newvalue;
      }
    }
  }
}

function swap_chkbox(id) {
    if (document.getElementById) {
        element = document.getElementById(id);
        if (element.type == 'checkbox') {
            element.checked = !element.checked;
        }
    }
}

function fenster_oeffnen(my_url,my_width,my_height,my_winname) {
  if (!my_winname) {
  	my_winname = "_new";
  }
  window.open(my_url,my_winname,'scrollbars,width='+my_width+',height='+my_height+',screenX=400,screenY=50, resizable')
}

function openerNav(my_url) {
//  window.alert(my_url);
  window.opener.location.href = my_url;
}

function popWin(file, Width, Height, Menubar, Id) {
  prev_window = popup_window;
  x_pos = (window.screen.availWidth - Width)/2;
  y_pos = (window.screen.availHeight - Height)/2;
  //popup_window = window.open(file,'popWin'+Id+popup_number,'WIDTH='+Width+',HEIGHT='+Height+',toolbar=no,menubar='+Menubar+',directories=no,resizable=no,status=no,scrollbars=yes,left='+x_pos+',top='+y_pos+',screenX='+x_pos+',screenY='+y_pos);
  popup_window = window.open(file,'popWin'+Id,'WIDTH='+Width+',HEIGHT='+Height+',toolbar=no,menubar='+Menubar+',directories=no,resizable=no,status=no,scrollbars=no,left='+x_pos+',top='+y_pos+',screenX='+x_pos+',screenY='+y_pos);

  if (!popup_window) {
    alert("You need to allow Javascript popups");
  } else {
    popup_window.focus();
  }

  //popup_number++;
  if (prev_window) {
    //prev_window.close();
  }
}

function storeCaret (textEl) {
	if (textEl.createTextRange)
		textEl.caretPos = document.selection.createRange().duplicate();
}

function insertAtCaret (textId, text) {
	if (document.getElementById) {
		textEl = eval('document.getElementById("' + textId + '")');
		if (textEl.createTextRange && textEl.caretPos) {
			var caretPos = textEl.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
			textEl.focus();
		} else {
			textEl.value  = text;
		}
	}
}

function setCheckboxes(the_form, do_check) {
	var elts_cnt = document.forms[the_form].elements.length;
	if (elts_cnt) {
		for (var i = 0; i < elts_cnt; i++) {
			var myObj = document.forms[the_form].elements[i];
			if (myObj.type == 'checkbox') {
				document.forms[the_form].elements[i].checked = do_check;
			}
		}
	}
	return true;
}

/**
 * Changes background color of table row
 *
 * @param   object    the table row
 * @param   string    the new color
 *
 * @return  boolean  whether pointer is set or not
 */
function setRowColor(theRow, newColor)
{
    var theCells = null;

    // 1. The browser can't get the row -> exits
    if (typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Detects DOM...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        domDetect    = false;
    } // end 3

    // 4. Sets the new color...
    if (newColor) {
        var c = null;
        // 4.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            }
        }
        // 4.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 4

    return true;
} // end of the 'setRowColor()' function
