window.defaultStatus = "";
var visibleDiv = ""

window.onerror = Fehlerbehandlung;

function Fehlerbehandlung (Nachricht, Datei, Zeile) {
  Fehler = "Fehlermeldung:\n" + Nachricht + "\nDatei:" + Datei + "\nZeile:" + Zeile;
  showError();
  return true;
}

function showError () { alert(Fehler) }

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

MM_reloadPage(true);

function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function openWindow(url,wName,features,returnWinObj,setFocus) { 
  newWin = window.open(url,wName,features); if(setFocus) newWin.focus(); if(returnWinObj) return newWin
}

function setFocus() {
	if(document.loginform) document.loginform.db_usr.focus()
}

function setFormFieldFocus(form, field) {
	if(document.forms[form]) {
		if(document.forms[form].elements[field]) {
			document.forms[form].elements[field].focus()
			document.forms[form].elements[field].focus()
		}
	}
}

var getPositionObjects = new Array;
var setPositionObjects = new Array;

function reloadPage(init) {
	if (init==true) {
		if (window.innerWidth) { document.pgW=window.innerWidth; document.pgH=window.innerHeight; }
		else if (document.body.offsetWidth)  document.pgW=document.body.offsetWidth; document.pgH=document.body.offsetHeight;  
		onresize=reloadPage;
	} else if (window.innerWidth!=document.pgW || window.innerHeight!=document.pgH) initPositions()
}

function makePass(obj) {
	obj = findObj(obj)
	passwort = "";
	randomNumber = Math.random();
	randomNumber = randomNumber.toString();
	randomNumber = randomNumber.substr(2,8);
	window.status = randomNumber
	for(i=0;i<8;i=i+2) {
		passwort+=randomNumber.substr(i,1)
		charCode = parseInt(randomNumber.substr(i,2))
		if(charCode>51) charCode = charCode-52;
		if(charCode<26) charCode = charCode+65
		else if(charCode>=26) charCode = charCode+71
		passwort+=String.fromCharCode(charCode)
	}
	obj.value = passwort.substr(0,8)
}

function showList(hide,show) {
	changeProp(hide,'','style.visibility','hidden','INPUT/TEXT')
	changeProp(show,'','style.visibility','visible','INPUT/SELECT')
}

function setInputValue(from,to) {
	objFrom = findObj(from); objTo = findObj(to); objTo.value = objFrom.options[objFrom.selectedIndex].text
}

function setInputValue2(value,to) { objTo = findObj(to); objTo.value =  value }

function deselectOtherCB(o) {
	obj = findObj(o);
	if(obj.checked == true) {
		f = obj.form.name;
		obj = findObj(f);
		oSliced = o.replace(/\[\d\]/,'');
		for(i=0; i<obj.elements.length; i++) {
			if(obj.elements[i].name.slice(0,oSliced.length) == oSliced && obj.elements[i].name != o) {
				obj.elements[i].checked = false;
			}
		}
	}
}

function deselectOneCB(o,d) {
	obj = findObj(o);
	if(obj.checked == true) {
		obj = findObj(d);
		obj.checked = false;
	}else return true;
}

function statIn(statMsg) { popup(statMsg); window.status = statMsg; }

function statOut() { popout(); 	/* window.defaultStatus(); */ }

function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=(navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6||this.ie7
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}

var bw=lib_bwcheck()

//Makes crossbrowser object.
function makeObj(obj){								
   	this.evnt=bw.dom? document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?document.layers[obj]:0;
	if(!this.evnt) return false
	this.css=bw.dom||bw.ie4?this.evnt.style:bw.ns4?this.evnt:0;	
   	this.wref=bw.dom||bw.ie4?this.evnt:bw.ns4?this.css.document:0;		
	this.writeIt=b_writeIt;
	return this
}

// A unit of measure that will be added when setting the position of a layer.
var px = bw.ns4||window.opera?"":"px";

function b_writeIt(text){
	if (bw.ns4){this.wref.write(text);this.wref.close()}
	else this.wref.innerHTML = text
}

/**
 * Opens calendar window.
 *
 * @param   string      calendar.php parameters
 * @param   string      form name
 * @param   string      field name
 * @param   string      edit type - date/timestamp
 */
function openCalendar(file, params, form, field, type) {
    newWin = window.open(file + "?" + params, "calendar", "width=400,height=200,status=yes");
    // dateField = eval("document." + form + "." + field);
	dateField = field; dateType = type; newWin.focus()
}

function formatNum2(i, valtype, addLeadNull) {

	if (valtype && valtype != '') {
		switch(valtype) {
			case 'month': i = (i > 12)?12:i; break;
			case 'day':   i = (i >31)?31:i; break;
			case 'hour':  i = (i>24)?24:i; break;
			default:
			case 'second':
			case 'minute':
				i = (i>59)?59:i;
				break;
		}
	}
	i = (addLeadNull)?addLeadZero(i, 2):delLeadZero(i);
	return i;
}

function addLeadZero(i,a) { i=String(i);while(i.length<a) i="0"+i;return i; }
function delLeadZero(i) { while(i.substring(0,1)=="0") i=i.substring(1,i.length);return i; }
function formatNum4(i) { return (i<1000?i<100?i<10?'000':'00':'0':'') + i; }

function resetForm(formName) {
	obj = findObj(formName)
	i = obj.elements.length;
	for(n = 0; n < i; n++) {
		switch(obj.elements[n].type) {
			case "select-one":				
				obj.elements[n].options.selectedIndex = 0;
				break;
/*			case "radio":
				if(obj.elements[n].checked === true) returnString += objName.substr(0, objName.indexOf('[')) + "=" + obj.elements[n].value + "&";
				break;
			case "checkbox":
				if(obj.elements[n].checked === true) returnString += objName + "=" + obj.elements[n].value + "&";
				break; */				
			case "text":
			case "hidden":
			case "textarea":
			case "input":
			case "password":
				obj.elements[n].value = '';
				break;
		}
	}
}

function changeDIVContent(div) {
	obj1 = findObj(div);
	obj2 = findObj(div + "_ph");
	if(obj2.innerHTML) obj2.innerHTML = "";
	else obj2.innerHTML = obj1.innerHTML
}

function checkIfSaved() {
	// alert(document.forms.length)
	if(document.forms.length>0) {
		if(document.forms[0].toBeSaved) {
			if(document.forms[0].toBeSaved.value == 1) {
				if(document.forms[0].valuesChanged) {
					if(document.forms[0].valuesChanged.value == 1) {
							event.returnValue = "Klicken Sie auf 'OK', wenn die Daten im geöffneten Formular gesichert sind. Ansonsten klicken Sie auf 'Abbrechen' und speichern Sie die Daten im Formular." 
					}
				}
			}
		}
	}
}

function checkdate(y,m,d) {
	check = true
	if(y == 0 || m < 1 || d < 1 || m > 12 || isNaN(y) || isNaN(m) || isNaN(d)) check = false
	// alert(d)
	if(d>31) check = false
	if((m==4 || m==6 || m==9 || m==11) && d>30) check = false
	if(m==2 && d>29) check = false
	if(m==2 && d>28 && y%4 !=0) check = false
	return check
}


function checkAllDates(obj, msg) {

	dateIsValid = true

	with(obj) {
			
		from_date = formatNum4(from_y) + formatNum2(from_m, "month", true) + formatNum2(from_d, "day", true) 
		to_date = formatNum4(to_y)  +  formatNum2(to_m, "month", true) + formatNum2(to_d, "day", true)
		
		from_date_time = from_date
		to_date_time = to_date
		
		try {
			if (from_h && from_i && to_h && to_i) throw true;
		} catch (e) {
			if (e === true) {
				from_date_time += from_date + formatNum2(from_h, "minute", true) + formatNum2(from_i, "second", true)
				to_date_time += to_date + formatNum2(to_h, "minute", true) + formatNum2(to_i, "second", true)
			}
		}

		// alert('from_date_time: ' + from_date_time)				
		// alert('to_date_time: ' + to_date_time)				


		if(from_date > 0) {
			if(!checkdate(formatNum4(from_y), formatNum2(from_m, "month", true), formatNum2(from_d, "day", true))) {
				alert("Beginn am: Ungültiges Datum");
				dateIsValid = false
			}
		}

		if(to_date > 0) {
			if(!checkdate(formatNum4(to_y), formatNum2(to_m, "month", true), formatNum2(to_d, "day", true))) {
				alert("Ende am: Ungültiges Datum")
				dateIsValid = false
			}
		}
		
		if(to_date > 0 && from_date > 0) {
			if(to_date_time < from_date_time) {
				alert("Enddatum liegt vor Anfangsdatum")
				dateIsValid = false
			}
		}
	}

	// alert("from_date / pr_from / to_date / pr_to: " + from_date + " / " + pr_from + " / " + to_date + " / " + pr_to)

	if(dateIsValid != false) {
		
		if(pr_from && pr_to && from_date && to_date) {
			if(from_date < pr_from || to_date > pr_to) {
				msg = msg.toString()
				if(msg.length == 0) msg = "Eingegebenes Datum liegt außerhalb des Veranstaltungszeitraumes. Trotzdem anlegen?"
				dateIsValid = confirm(msg)
			}
		}
	}

	return dateIsValid
}

// ***************** this is for the fold-ou menu *****************

function showAbsDiv(divName) { // setting absolute div to 'visible' respectively 'hidden'
	document.onclick = doNothing; 
	if(visibleDiv && divName != visibleDiv) hideVisibleDiv(); dNeu = findObj(divName)
	if(dNeu.style.visibility == 'hidden') {
		dNeu.style.visibility = 'visible'
		showHideSelect('hidden')
	} else {
		dNeu.style.visibility = 'hidden'
		showHideSelect('visible')
	}
	visibleDiv = divName; setTimeout("document.onclick = hideVisibleDiv", 500)
}

function showHideSelect(action) { // hide/show all 'select' elements, as they would overlap divs
	if(bw.ie) { for(fNam in document.forms) { formObj = document.forms[fNam]
	if(document.forms[fNam].length) { if(formObj.name != "menu") {
	i = formObj.elements.length; for(n = 0; n < i; n++) {
	if(formObj.elements[n].type == "select-one" || formObj.elements[n].type == "multiple") formObj.elements[n].style.visibility = action 	} } } } }
}

function hideVisibleDiv() { // hide visible menu div, empty the'visibleDiv' var 
	showHideSelect('visible'); div = findObj(visibleDiv); if(visibleDiv) { div.style.visibility = 'hidden'
	visibleDiv = ""; } document.onclick = doNothing
}

// ***************** this was for the fold-out menu *******************

function toggleVisDiv(divName) {
	div = findObj(divName)
	if(div.style.position == "absolute") showDiv(divName)
	else hideDiv(divName)
}

function showDiv(divName) {
	div = findObj(divName); div.style.position = ''; div.style.visibility = 'visible'
}

function hideDiv(divName) {
	div = findObj(divName); if(div.style) { div.style.position = 'absolute'; div.style.visibility = 'hidden'; }
}

function setSelectGroupDiv(groupName, groupID) {
	divObjName=findObj("selectGroupDiv"); divObjID=findObj("selectedGroupID"); divObjName.innerHTML = groupName; divObjID.value = groupID; 
}

function setRadioChecked(obj, nam, val) {
	obj = findObj(obj);
	for(i=0; i<obj.elements.length; i++) {
		el = obj.elements[i]
		if(el.type == "radio" && el.name == nam && el.value == val) el.checked = true
	}
}

function setRpToNull() { setObjToVal('rp_id', ''); setObjToVal('p_name', '&nbsp;'); }

function setAccompToNull() { setObjToVal('accomp_id', ''); setObjToVal('p_name', ''); }

function setGroupToNull() { setObjToVal('gr_id', ''); setObjToVal('gr_name', ''); }

function setObjToVal(n,v) { 
	o = findObj(n); i=false; for(a in o) if(a=="value") { i=true;  break }
	if(i) o.value=v; else o.innerHTML=v;
}

function returnGroupToOpener(id, name, form_name, element_name) {
	elements = new Object(); elements[element_name] = id;
	elements["gr_name"] = name; returnValueToOpener(form_name, elements, 'opener')
}

function returnValueToOpener(form_name, els, type) {
	obj = (type == 'opener')?window.opener:window.parent;
	for(eName in els) obj.document.forms[form_name].elements[eName].value = els[eName]
	window.close()
}

function qvSubmitForm(loc, submitSwitch) {
	formObj = findObj('quickview_form'); formObj.action = loc;
	if(submitSwitch) formObj.submit(); else location.href = formObj.action;
}


function openAdaptWin(custID, loc, winName)  {
	if(custID) openWindow(loc, winName, "width=380px,height=220px,status=no,scrollbars=no", false, true); return true;
}

function snglValToOpnrAndClose(openerForm, openerField, val) {
	window.opener.document.forms[openerForm].elements[openerField].value = val; window.opener.focus(); window.close();
}

function getOpenerVal(openerForm, openerField, currentForm, currentField) {
	setToValue = window.opener.document.forms[openerForm].elements[openerField].selectedIndex;
	document.forms[currentForm].elements[currentField].options[setToValue].selected = true
}

function switchBackgroundColor(colIn, colOut, switchVal, obj) {
	var tr = document.getElementById(obj);
	var td = tr.getElementsByTagName("td");
	for (var i = 0; i < td.length; i++) {
		if(switchVal == "in") td[i].style.backgroundColor = colIn
		if(switchVal == "out") td[i].style.backgroundColor = colOut
	}
}

function swBGC(colIn, colOut, switchVal, obj) {
	switchBackgroundColor(colIn, colOut, switchVal, obj)	
}

function completeLink(obj, objFormName) {
	objForm = findObj(objFormName); obj.href += objForm.options[objForm.options.selectedIndex].value
}

function setSelValToHid(objFromName, objToName) {
	objFrom = findObj(objFromName); objTo = findObj(objToName); objTo.value = objFrom.options[objFrom.options.selectedIndex].value
}

function confirmDelAndGoto(msg, loc) {
	testForDel = confirm(msg);
	if(testForDel == true) {
		location.href= loc
		return true;
	} else return false;
}

function setFocusField(name) {
	field = findObj("setFocusOn"); field.value = name
}

function setFocusBack(element) {
	if(element) {
		obj = findObj(element)
		setTimeout("obj.focus()", 75)	
		if(obj.value.length > 0) {
			setTimeout("obj.focus()", 25)	
		}
	}
}


function getIFrameSrc(objName) {
	obj = findObj(objName)
	if(obj.location) return obj.location.href 
	else return obj.src 
}


function setIFrameSrc(newLocation, objName) {
	obj = findObj(objName)
	if(obj.location) obj.location.href = newLocation
	// if(bw.ie) { obj.location.href = newLocation }
	else obj.src = newLocation
	// obj.src = newLocation
}

function submitForm(formName) {
	obj = findObj(formName);
	obj.submit()
}

function doNothing() { null }

function showMsg(m,c) {
	if(c) return confirm(m);
	else return true;
}


offsetX = 20;
offsetY = 20;
var toolTipSTYLE="";


function initToolTips()
{

    obj = findObj("toolTipLayer")	    
    if(bw.ns4) toolTipSTYLE = obj;
	else toolTipSTYLE = obj.style
    if(bw.ns4) document.captureEvents(Event.MOUSEMOVE);
    else
    {
      toolTipSTYLE.visibility = "visible";
      toolTipSTYLE.display = "none";
    }
    document.onmousemove = moveToMouseLoc;

}


function toolTip(msg, fg, bg)
{

  if(toolTip.arguments.length < 1) // hide
  {
    if(bw.ns4) toolTipSTYLE.visibility = "hidden";
    else toolTipSTYLE.display = "none";
  }
  else // show
  {
	if(!fg) fg = "#000000";
    if(!bg) bg = "#FFFF99";
    var content =   '&nbsp;' + msg +  '&nbsp';
    if(bw.ns4)
    {
      toolTipSTYLE.document.write(content);
      toolTipSTYLE.document.close();
      toolTipSTYLE.visibility = "visible";
    }
	
	obj = findObj("toolTipLayer")
	obj.innerHTML = content
    toolTipSTYLE.display='block'	
  }
}

function moveToMouseLoc(e) {
	if(bw.ns4||bw.ns6) {
		x = e.pageX;
		y = e.pageY;
	} else {
		x = event.x + document.body.scrollLeft;
		y = event.y + document.body.scrollTop;
	}
	// alert(x + offsetX);
	toolTipSTYLE.left = x + offsetX;
	toolTipSTYLE.top = y + offsetY;
	return true;
}


function showProperties(objName) {
	obj = findObj(objName); newWin=openWindow('','test','',true,true); newWin.document.open; newWin.document.write("<html><body>");
	for (a in obj) 	newWin.document.write("obj[" + a + "] = " + obj[a] + "<br>\n"); newWin.document.write("</html></body>");
}

function setSrc(divName, src) {
	div = findObj(divName)
	div.src = src
}

function trim(s) {
	while(s.substring(0, 1) == " ") s = s.substr(1)
	while(s.substring(s.length-1, 1) == " ") s = s.substring(0,s.length-1)
	return s
}

function selectAll(name, obj, selSwitch) {
	for(i=0; i<obj.form.elements[name].options.length; i++) {
		obj.form.elements[name].options[i].selected = selSwitch;
	}
}

function writeAssToHidden(fName) {
	obj1 = document.forms[fName].elements["not_assigned[]"]
	obj2 = document.forms[fName].elements["is_assigned[]"]
	notyetSelected = "#" + notyetSelected.join("#") + "#"
	compLeng = obj1.options.length
	for(i=0; i<compLeng; i++) {
		if(notyetSelected.indexOf("#" + obj1.options[i].value + "#")<0) {
			document.forms[fName].elements["nAssH[" + i + "]"].value = obj1.options[i].value;
		}
	}
	alreadySelected = "#" + alreadySelected.join("#") + "#"
	compLeng = obj2.options.length
	for(i=0; i<compLeng; i++) {
		 if(alreadySelected.indexOf("#" + obj2.options[i].value + "#")<0) {
		 	document.forms[fName].elements["iAssH[" + i + "]"].value = obj2.options[i].value;
		}
	}
}

function getAssignValue(fName) {
	obj0 = document.forms[fName].elements["assign_to"]
	if(obj0.selectedIndex < 0 || obj0.options[obj0.selectedIndex].value == "null" || obj0.options[obj0.selectedIndex].value == null) {
		alert("Keine gültige Auswahl");
		return false;
	} else return true;
}

function selComp(action, fName, assignValue) {
	if(assignValue) {
		if(action == "select") {
			obj1 = document.forms[fName].elements["not_assigned[]"]
			obj2 = document.forms[fName].elements["is_assigned[]"]
		} else {
			obj2 = document.forms[fName].elements["not_assigned[]"]
			obj1 = document.forms[fName].elements["is_assigned[]"]
		}
		compLeng = obj1.options.length
		for(i=0; i<compLeng; i++) {
			if(obj1.options[i].selected) {
				newEntry = new Option(obj1.options[i].text, obj1.options[i].value, false, true);
				obj2.options[obj2.length] = newEntry;
			}
		}
		for(n=0; n<compLeng; n++) { 
			for(i=0; i<compLeng; i++) {
				if(obj1.options[i]) {
					if(obj1.options[i].selected) {
						obj1.options[i] = null;
						break;
					}
				}
			}
		}
	}
}

function renameObj(id) {	
	toggleVisDiv('obj_name[' + id + ']'); 
	toggleVisDiv('obj_rename[' + id + ']');
	obj1 = findObj('obj_name[' + id + ']');
	obj2 = findObj('obj_rename_field[' + id + ']');
	obj1.innerHTML = obj2.value
}

function selectLang(l) {
	h=location.href; if(h.search("lang=(de|en)")>0) hNew=h.replace(/lang=(de|en)/g,"lang="+l);
	else hNew=h+((h.indexOf("?")>0)?"&":"?")+"lang="+l; location.href=hNew;
}
