var vWinCal = null;

function showHelp(helpTitle,helpMsg) {
	showHelp("components/common/common/img/help_big.gif",helpTitle,helpMsg);
}

function showInfo(helpTitle,helpMsg) {
	showHelp("components/common/common/img/infomsg_big.gif",helpTitle,helpMsg);
}

function showHelp(helpImg,helpTitle,helpMsg) {
	str_buffer = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
		+ "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
		+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
		+ "<head>"
		+ "<link rel=\"stylesheet\" href=\"components/common/common/css/component.css\" type=\"text/css\" />"
		+ "<title>Helpcomponents</title>"
		+ "</head>"
		+ "<body class=\"nf\">"
		+ "<img class=\"helpwimg\" src=\"" + helpImg + "\" alt=\"Help\" />"
		+ "<div class=\"helpwtitle\">" + helpTitle + "</div>"
		+ "<div class=\"helpwmsg\">" + helpMsg + "</div>"
		+ "</body>"
		+ "</html>";
	var x = window.screenX;
	var y = window.screenY;
	if (!x) {
		x = window.screenLeft +50;
		y = window.screenTop +50;
	} else {
		x += 50;
		y += 200;
	}
	vWinCal = window.open("","help","null,width=300,height=300,top="+y+",left="+x);
	vWinCal.opener = self;
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
	vWinCal.focus();
}

function doClick(tr){
	var type = "odd";
	if (tr.className.substr(0,3) != "odd") {
		type = "even";
	}
	if (tr.className == type + "marked") {
		tr.className = type + "hover";
		return;
	} else {
		tr.className = type + "marked";
		return;
	}
}

function doMark(tr){
	if (tr.className != "oddmarked" && tr.className != "evenmarked") {
		var type = "odd";
		if (tr.className.substr(0,3) != "odd") {
			type = "even";
		}
		if (tr.className == type + "hover") {
			tr.className = type;
			return;
		} else {
			tr.className = type + "hover";
			return;
		}
	}
}

function setCheckboxes(form,beginwith,do_check){
	var obj = document.forms[form];
	var l = obj.elements.length;
	var lb = beginwith.length;
	var o = null;
	for (var i=0;i<l;i++) {
		o = obj.elements[i];
		if (o.type == 'checkbox' && o.name.substr(0,lb) == beginwith) {
			o.checked = do_check;
		}
	}
}

function settrgt(twindowname,pformname){
	if(pformname==""){
		document.forms[pformname].target="_self";
	}else{
		document.forms[pformname].target=twindowname;
	}
	return true;
}

function btnsbmt(objsrcformfield, pvalue, ptargetwindowname, poutputtype, pconfirmtext){
	dosubmit=true;
	if (pconfirmtext==""){
		dosubmit=true;
	}else{
		dosubmit= confirm(pconfirmtext);
	}
	if(dosubmit){
		if(ptargetwindowname==""){
			if (poutputtype=="")
				nfsend();
			objsrcformfield.form.target="_self";
		}else{
			objsrcformfield.form.target=ptargetwindowname;
		}
		objsrcformfield.form.elements["cmds"].value=pvalue;
		objsrcformfield.form.submit();
	}
}

function btnsbmt2(pformname, pvalue, ptargetwindowname, poutputtype, pconfirmtext){
	dosubmit=true;
	myform=document.forms[pformname];
	if (pconfirmtext==""){
		dosubmit=true;
	}else{
		dosubmit= confirm(pconfirmtext);
	}
	if(dosubmit){
		if(ptargetwindowname==""){
			if (poutputtype=="")
				nfsend();
			myform.target="_self";
		}else{
			myform.target=ptargetwindowname;
		}
		myform.elements["cmds"].value=pvalue;
		myform.submit();
	}
}

function pageto(ppagetovalue, pformname, pvalue,  ptargetwindowname, poutputtype, pconfirmtext){
	dosubmit=true;
	myform=document.forms[pformname];
	if (pconfirmtext==""){
		dosubmit=true;
	}else{
		dosubmit= confirm(pconfirmtext);
	}
	if(dosubmit){
		if(ptargetwindowname==""){
			if (poutputtype=="")
				nfsend();
			myform.target="_self";
		}else{
			myform.target=ptargetwindowname;
		}
		myform.elements["cmds"].value=pvalue+"&pageto="+ppagetovalue;
		myform.submit();
	}
}

function invisibleSet(o,setid,form){
	var h = '';
	var x = '';
	if (o.parentNode.style.height=='20px') {
		h='';
		x='block';
	} else {
		h='20px';
		x='none';
	}
	// handling for saving data for user-setting
	if(null!=setid){
		//send request for the formsettings
		if (window.XMLHttpRequest) {
			framesettingsReq=new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			framesettingsReq=new ActiveXObject("Microsoft.XMLHTTP");
		}
		framesettingsReq.open("GET", "/nice/admin?pagetype=saveUserSettings&output=xml&prefix=formset&settingItem="+form+"&dataItem="+setid+"_"+x, true);
		framesettingsReq.send(null);
	}
	o.parentNode.style.height=h;
	var c = o.parentNode.childNodes;
	if (navigator.product == "Gecko") {
		for (var i=3;i<c.length;i+=2) {
			c[i].style.display=x;
		}
	} else {
		for (var i=1;i<c.length;i++) {
			c[i].style.display=x;
		}
	}
}

function formatTime(obj){
	var hours = '';
	var minutes = '';
	var time = '';
	
	if(obj.value.length == 4 && obj.value.indexOf(":") == -1){
		hours = obj.value.substring(0,2);
		minutes = obj.value.substring(2,4);
		obj.value = hours + ":" + minutes;
				
		return obj;
	}
}

function formatDate(obj){
	var days = '';
	var month = '';
	var year = '';
	
	if(obj.value.length == 6 && obj.value.indexOf(".") == -1){
		days = obj.value.substring(0,2);
		month = obj.value.substring(2,4);
		year = obj.value.substring(4,6);
		if(year > 30){
			year = "19" + year;
		}
		else
			year = "20" + year;
		obj.value = days + "." + month + "." + year;
		
		return obj;
	}
	else if(obj.value.length == 8 && obj.value.indexOf(".") == -1){
		days = obj.value.substring(0,2);
		month = obj.value.substring(2,4);
		year = obj.value.substring(4,8);
		obj.value = days + "." + month + "." + year;
		
		return obj;
	}
}

function nfsend() {
	nfHasChanges = false;
	nfIsSubmit = true;
	nfFormSent();
}

function nfFormSent() {
	// funktion wird im admin.jsp ueberschrieben
	if (document.getElementById("nfmenu"))
		document.getElementById("nfmenu").style.visibility = 'hidden';
}

window.onbeforeunload = function() {
	if (document.getElementById("nfcontent")) {
		if (!nfIsSubmit) {
			if (nfRecordLocking) {
				try {
					lockingFunction('release');
				} catch (e) {}
			}
		}
		if (nfEditForm && nfHasChanges) {
			return 'Ihre Eingaben wurden noch nicht gespeichert.\nDie noch nicht gespeicherten Daten verwerfen?';
		}
	}
}

function c() {
	nfHasChanges = true;
}

function lockingFunction (command) {
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//alert(req.responseText);
				var doc = req.responseXML.documentElement;
				var node = doc.firstChild;
				var state = "";
				while (node != null) {
					if (node.nodeName == "state") {
						state = node.firstChild.nodeValue;
						if (state == "locked" || state == "modified") {
							//nothing
						} else if (state == "released") {
							toStateReleased();
						} else {
							toStateReleased();
						}
					} else if (state == "locked" && node.nodeName == "remaining-time") {
						rtime = node.firstChild.nodeValue;
						loadtime = new Date().getTime();
						toStateLocked();
					} else if (state == "modified" && node.nodeName == "user") {
						var node0 = node.firstChild;
						var id0 = "";
						var display0 = "";
						while (node0 != null) {
							if (node0.nodeName == "id") {
								id0 = node0.firstChild.nodeValue;
							} else if (node0.nodeName == "display") {
								display0 = node0.firstChild.nodeValue;
							}
							node0 = node0.nextSibling;
						}
						toStateModified(id0,display0);
					} else if (node.nodeName == "error") {
						alert(node.firstChild.nodeValue);
					}
					node = node.nextSibling;
				}
	        } else {
	            alert("There was a problem retrieving the XML data:\n" + req.statusText);
	        }
		}
	}
	req.open("GET","/nice/admin?pagetype=locking&action="+command+"&nws="+nws+"&r="+Math.random(),true);
	req.send(null);
}
