function redirect(url, delay) {
	var tmp = function() {
		window.location = url;
	}

	setTimeout(tmp, delay);
}
function downloader(url, delay) {
	var tmp = function() {
		window
				.open(url, 'download',
						'width=800, height=600, status=no, copyhistory=no,location=no,toolbar=no');
	}
	setTimeout(tmp, delay);
}
/**
 * @DESC Setzt einen Step in einem inputfield, dessen name step ist
 * 
 */
function setStep(str_step) {
	window.document.form.go.value = str_step;
}

/**
 * @DESC ermittelt die window-hoehe
 * @returns {Number}
 */
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof (window.innerHeight) == 'content') {
		windowHeight = window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		} else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

/**
 * @DESC ermittelt die window-breite
 * @returns {Number}
 */
function getWindowWidth() {
	var windowWidth = 0;
	if (typeof (window.innerWidth) == 'content') {
		windowWidth = window.innerWidth;
	} else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		} else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

/**
 * @DESC Setzt den Footer ganz nach unten
 */
function setFooter() {
	if (document.getElementById('footer')) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var contentMenue = document.getElementById('content_left').offsetHeight;
			var contentCont = document.getElementById('content').offsetHeight;

			var contentHeight = document.getElementById('content').offsetHeight;
			if (contentMenue > contentCont) {
				var contentHeight = document.getElementById('content_left').offsetHeight;
			}

			var footerElement = document.getElementById('footer');
			var footerHeight = footerElement.offsetHeight;
			if (windowHeight - (contentHeight + footerHeight) >= 0) {
				footerElement.style.top = (windowHeight - (contentHeight + footerHeight))
						+ 'px';
			} else {
				footerElement.style.top = '0px';
			}
		}
	}
}

/**
 * @DESC Passt eine Tabelle an
 */
function setTableSize() {
	if (document.getElementById('tableWidth')) {
		var windowWidth = document.body.clientWidth;
		var contentLeftWidth = document.getElementById('content_left').offsetWidth;
		var tableElement = document.getElementById('tableWidth');
		tableElement.width = (windowWidth - (contentLeftWidth + 50)) + 'px';
	}
}

/**
 * @DESC Gibt die absolute Höhe eines Elements zurück
 */
function absTop(el) {
	return (el.offsetParent) ? el.offsetTop + absTop(el.offsetParent)
			: el.offsetTop;
}

/**
 * @DESC Passt einen Iframe an
 */
function setIframeSize() {
	if (document.getElementById('iframeid')) {
		var windowHeight = document.body.clientHeight;
		var windowWidth = document.body.clientWidth;

		var contentLeftWidth = document.getElementById('content_left').offsetWidth;
		var iframeElement = document.getElementById('iframeid');

		iframeElement.height = (windowHeight - 50) + 'px';
		iframeElement.width = (windowHeight) + 'px';
	}

	iframe = document.getElementById('iframeService')
	if (iframe) {
		var windowHeight = getWindowHeight();
		var iframeOffset = absTop(iframe);
		// Höhe für den Footer:
		var footerFixedHeight = 50;

		var iframeHeight = windowHeight - iframeOffset - footerFixedHeight;
		iframe.height = iframeHeight + 'px';
	}
}

/**
 * @DESC Wenn Helptip - ON
 * 
 * @param text
 * @param id
 */
function helptip(text, id) {
	/*
	 * <HTML-CODE> <a onMouseOver="textmessage!','id');"
	 * onMouseOut="helptipdown('id');">
	 */
	document.getElementById(id).innerHTML = '<font>' + text + '</font>';
	document.getElementById(id).style.display = 'block';
}

/**
 * @DESC Wenn Helptip - OFF
 * 
 * @param id
 */
function helptipdown(id) {
	document.getElementById(id).style.display = 'none';
}

/**
 * @DESC Window-POPUP
 * 
 * @param url
 * @param fensterName
 * @param eigenschaften
 */
function window_popup(url, fensterName, eigenschaften) {
	var newwin;
	newwin = window.open(url, fensterName, eigenschaften);
	if (version > 1.0) {
		setTimeout(fensterName + '.focus();', 200);
	}
	newwin.focus();
}

/**
 * @DESC Fenster oeffnen
 */
function windowOpener(url, fensterName, eigenschaften) {
	winName = window.open(url, 'fensterName', 'eigenschaften');
	winName.focus();
}

/**
 * @DESC Fenster schliessen
 */
function windowClose(fensterName) {
	fensterName.close();
}

function submitform2(formname) {
	document.forms[formname].submit();
}

function download(url) {
	open(url);
}

/**
 * @DESC Immer beim Laden
 */
window.onload = function() {
	setIframeSize();
}

/**
 * @DESC Immer beim Resize des Windows
 */
window.onresize = function() {
	setIframeSize();
}

/**
 * @desc Aufruf eines asyncronen HTTP-Requests. Der ausgegebene HTML-Code wird
 *       an die Stelle von id gesetzt.
 * 
 * @param url
 * @param id
 * @return
 */
function asyncRequest(url, id) {
	var xmlhttp = null;

	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		// IE 5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById(id).innerHTML = xmlhttp.responseText;
			xmlhttp = null;
		}
	}

	xmlhttp.open('GET', url, true);
	xmlhttp.send();
}

/**
 * @desc Context-Menues:
 */
var activeContextMenu = null;

function hideActiveContextMenu() {
	/* Aktives Kontextmenu ausblenden: */
	if (activeContextMenu) {
		activeContextMenu.style.display = "none";
		activeContextMenu = null;
	}
}

function handleOnMouseUp(e) {
	if (!e) {
		e = window.event;
	}

	if (e && e.button != 2) {
		hideActiveContextMenu();
	}
}

if (document.all) {
	/* IE */
	document.onmouseup = handleOnMouseUp;
	window.onmouseup = handleOnMouseUp;
} else if (document.getElementById) {
	/* Firefox */
	window.captureEvents(Event.MOUSEUP);
	window.onmouseup = handleOnMouseUp;
}

function showContextMenu(e, menuid) {
	if (!e) {
		e = window.event;
	}

	hideActiveContextMenu();

	/* Zeige Kontextmenu */
	if (e.pageX && e.pageY) {
		/* Firefox */
		var posX = e.pageX;
		var posY = e.pageY;
	} else if (e.clientX && e.clientY) {
		/* IE 7 */
		var posX = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
		var posY = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
	}

	activeContextMenu = document.getElementById(menuid);
	if (activeContextMenu) {
		activeContextMenu.style.display = "block";

		var windowMiddleHeight = getWindowHeight() / 2;
		if (windowMiddleHeight > posY) {
			activeContextMenu.style.top = posY + "px";
		} else {
			posY -= activeContextMenu.offsetHeight;
			if (posY < 0) {
				posY = 0;
			}
			activeContextMenu.style.top = posY + "px";
		}

		var windowMiddleWidth = getWindowWidth() / 2;
		if (windowMiddleWidth > posX) {
			activeContextMenu.style.left = posX + 1 + "px";
		} else {
			posX -= activeContextMenu.offsetWidth;
			activeContextMenu.style.left = posX + "px";
		}
	}
}

/**
 * Loescht einen DB Eintrag nach Bestaetigung der Message mit OK. Wird ein
 * referer uebergeben, wird im Anschluss diese Seite aufgerufen, sonst wird die
 * Variable http-referer verwendet. Alles ausser der Message-Abfrage geschieht
 * im Service #28.
 * 
 * @param string
 *            message
 * @param string
 *            inifile
 * @param string
 *            table
 * @param string
 *            field
 * @param string
 *            value
 * @param string
 *            referer
 * 
 * deleteFromDB('', 'infrastruktur', 'PORT_PSP_SERVER_PHY', '.PSP_ID', '7', '')
 */
function deleteFromDB(message, inifile, table, field, value) {
	if (message != '') {
		var response = window.confirm(message);

		if (!response) {
			return

		}
	}

	var deleteService = "index.php?pageid=10&i=" + inifile + "&t=" + table
			+ "&f=" + field + "&v=" + value;

	window.location = deleteService;
}

function deleteFromDBQuery(message, inifile, querystring) {
	if (message != '') {
		var response = window.confirm(message);

		if (!response) {
			return

		}
	}

	var deleteService = "index.php?pageid=10&i=" + inifile + "&q="
			+ querystring;

	window.location = deleteService;
}

function redirect(url, delay) {
	var tmp = function() {
		window.location = url;
	}

	setTimeout(tmp, delay);
}

/**
 * @DESC Schiebt ein OPTION-Element um eine Stelle nach oben. Wenn Start auf
 *       TRUE gesetzt ist, wird Element an die erste Stelle geschoben.
 * 
 */
function moveOptionsUp(selectId, start) {
	var selectList = document.getElementById(selectId);
	var selectOptions = selectList.getElementsByTagName('option');
	for ( var i = 1; i < selectOptions.length; i++) {
		var opt = selectOptions[i];
		if (opt.selected) {
			selectList.removeChild(opt);
			if (start == true) {
				selectList.insertBefore(opt, selectOptions[0]);
			} else {
				selectList.insertBefore(opt, selectOptions[i - 1]);
			}
		}
	}
}

/**
 * @DESC Schiebt ein OPTION-Element um eine Stelle nach unten. Wenn End auf TRUE
 *       gesetzt ist, wird Element an die letzte Stelle geschoben.
 * 
 */
function moveOptionsDown(selectId, end) {
	var selectList = document.getElementById(selectId);
	var selectOptions = selectList.getElementsByTagName('option');
	for ( var i = selectOptions.length - 2; i >= 0; i--) {
		var opt = selectOptions[i];
		if (opt.selected) {
			if (end == true) {
				var lastElement = selectOptions.length - 1;
				var nextOpt = selectOptions[lastElement];
			} else {
				var nextOpt = selectOptions[i + 1];
			}
			opt = selectList.removeChild(opt);
			nextOpt = selectList.replaceChild(opt, nextOpt);
			selectList.insertBefore(nextOpt, opt);
		}
	}
}

/**
 * @DESC Speichert alle Werte aus "sel_list" in pkg_apids
 * 
 */
function saveList() {
	var i = 0;
	var hval = "";
	var sel = window.document.form.sel_list;
	do {
		hval += "," + sel.options[i].value;
	} while (++i < sel.options.length);
	window.document.form.pkg_apids.value = hval;
}

function submitit() {
	this.form.submit();
}

function doGenkeyKonfig() {
	var errormsg = new Array();
	var i = 0;
	var hval = "";
	var sel = window.document.form.sel_list;
	var plan = window.document.form.pl;
	var hour = window.document.form.hour;
	var min = window.document.form.min;
	var datum = window.document.form.to;
	var error = false;

	if (sel.length == 0) {
		error = true;
		errormsg.push('Es muss mindestens ein Server selektiert werden');
	}

	if (datum.length == 0) {
		error = true;
		errormsg.push('Das Datum muss angegeben werden');
	}

	if (min.length == 0) {
		error = true;
		errormsg.push('Die Angabe der Minuten in der Uhrzeit fehlen');
	}

	if (hour.length == 0) {
		error = true;
		errormsg.push('Die Angabe der Stunde in der Uhrzeit fehlt');
	}

	if (plan.length == 0) {
		error = true;
		errormsg.push('Es muss mindestens ein Plan selektiert werden');
	}

	if (error == true) {
		errormsg.unshift('Bitte korrigieren Sie Ihre Eingabe(n):\n');
		alert(errormsg.join('\n'));
	} else {

		do {
			hval += "," + sel.options[i].value;
		} while (++i < sel.options.length);
		window.document.form.pkg_apids.value = hval;
		window.document.form.ac.value = 'genkeykonfig';
	}

}

/**
 * @deprecated NEU - setInputField (str_name, str_value)
 * 
 * @DESC Setzt einen Step in einem inputfield, dessen name step ist
 * 
 */
function setStep(str_step) {
	window.document.form.step.value = str_step;
}

/**
 * @deprecated NEU - setInputField (str_name, str_value)
 * 
 * @DESC Veraendert die Pageid in einem inputfield dessen name pageid ist
 * 
 */
function setPageid(str_value) {
	window.document.form.pageid.value = str_value;
}

/**
 * @DESC Veraendert den Wert eines inputfeldes
 * 
 */
function setInputField(str_name, str_value) {
	window.document.form.str_name.value = str_value;
}

function submitAndSet(state) {
	window.document.form.ishidden.value = state;
	window.document.form.submit();
}

function submitAndSetSubStep(state) {
	window.document.form.substep.value = state;
	window.document.form.submit();
}
function submitAndSetStep(state) {
	window.document.form.step.value = state;
	window.document.form.submit();
}

function submitAndRenew() {
	window.document.form.step.value = 20;
	window.document.form.submit();
}

/*
 * SHOW HIDE von IDs
 */
var state = 'none';
function showhide(layer_ref) {

	if (state == 'block') {
		state = 'none';
	} 
	else {
		state = 'block';
	}
	
	if (document.all) { // IS IE 4 or 5 (or 6 beta)
		eval("document.all." + layer_ref + ".style.display = state");
	}
	if (document.layers) { // IS NETSCAPE 4 or below
		document.layers[layer_ref].display = state;
	}
	if (document.getElementById && !document.all) {
		hza = document.getElementById(layer_ref);
		hza.style.display = state;
	}
}

