/**
 * @author mh
 */

function object2String(o, emptyAttr) {
	var s = '' + o + "\n";
	//s += o.getAttribute('src');
	for (a in o.attributes) if (a.nodeValue || emptyAttr || 1) s += "\t" + a + '=' + a.nodeValue + "\n";
	return s;
}

function getUserTime(millisec) {
	var d = millisec?new Date(millisec):new Date();
	var str;
	str = (d.getDay()+1)+'.'+(d.getMonth()+1)+'.'+d.getFullYear()+', '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds()+'.'+d.getMilliseconds();
	return str;
}

var enableDebug = 0;
function printDebug(text) {
	var d = new Date();
	text = getUserTime() + ': ' + text;
	if (!enableDebug) return;
	var e = $('#debug_javascript');
	if (!e) return;
	e.html(e.html() + text + "\n");
}

function changeFavicon(url) {
	var icon=$('#favicon');
	icon.replaceWith(icon.clone(true).attr('href', url));
}

function decodeHTML(code) {
	code = code.replace(/{"}/g, '"');
	code = code.replace(/{<}/g, '<');
	code = code.replace(/{>}/g, '>');
	return code;
}

function gebi(id) {return document.getElementById(id);}

function setCookie(name, value, path, domain, expires) {
   c= name+"="+value+";";
   if (path) c += "path="+path+";";
   if (domain) c += "domain="+domain+";";
   if (expires) c += "expires="+expires+";";
   document.cookie = c;
}

function getCookie(name) {
   c = document.cookie;
   c = c.split('; ');
   c2 = new Array();
   for (c1 in c) {
      t = c[c1].split('=');
      c2[t[0]] = t[1];
   }
   return c2[name];
}

function getAbsPos(elem) {
	var tagname="",x=0,y=0;
	while ((typeof(elem)=="object")&&(typeof(elem.tagName)!="undefined")){
		y+=elem.offsetTop;
		x+=elem.offsetLeft;
		tagname=elem.tagName.toUpperCase();
		if (tagname=="BODY") elem=0;
		if (typeof(elem)=="object") if (typeof(elem.offsetParent)=="object") elem=elem.offsetParent;
	}
	position=new Object();
	position.x=x;
	position.y=y;
	return position;
}

function limitString(string, maxWords, maxChars, appendChars) {
	if (!appendChars) appendChars = '...';
	pos = 0;
	wordCount = 0;
	string = string.replace(/<\/?(?!\!)[^>]*>/gi, '');
	string = string.replace("\n", ' ');
	string = string.replace('"', '&quot;');
	strLen = string.length;
	if (isNaN(maxChars)) maxChars = strLen + 1;
	while (wordCount < maxWords) {
		while (string[pos] == ' ') ++pos;
		while (string[pos] != ' ' && pos<maxChars) ++pos;
		if (pos>=maxChars) break;
		++pos;
		if (pos) lastPos = pos-1;
		++wordCount;
	}
	if (pos>=strLen && strLen < maxChars) return string;
	if (!wordCount && strLen) string = string.substr(0, maxChars);
	else string = string.substr(0, lastPos);
	if (appendChars && string.length<strLen) string += appendChars;
	return string;
}

function limitInnerHTML(elem, maxWords, maxChars, appendChars) {
	iHTML = elem.innerHTML;
   limitedContent = limitString(iHTML, maxWords, maxChars, appendChars);
	title = iHTML.replace(/<\/?(?!\!)[^>]*>/gi, '').replace(/"/g, '&quot;');
	elem.innerHTML = '<span title="' + title + '">' + limitedContent + '</span>';
}

function showHideBox(id, idLink, hide, openSpeed, closeSpeed) {
	if (typeof openSpeed == 'undefined') openSpeed = 300;
	if (typeof closeSpeed == 'undefined') closeSpeed = 500;
	elem = document.getElementById(id);
	if (hide!=1 && hide!=0) {
		if (elem.style.display == "none") hide = 0;
		else hide = 1;
	}
	else if (hide && elem.style.display == "none") return;
	else if (!hide && elem.style.display != "none") return;
	if (!hide) {
		$('#' + id).slideDown(openSpeed);
		//elem.style.display = "block";
		if (idLink) {
			var classStr = document.getElementById(idLink).className;
			document.getElementById(idLink).className = classStr.replace("plus", "minus");
		}
	}
	else {
		$('#' + id).slideUp(closeSpeed);
		//$('#' + id).hide("scale", { percent:0, direction:'vertical' }, 500);
//		elem.style.display = "none";
		if (idLink) {
			var classStr = document.getElementById(idLink).className;
			document.getElementById(idLink).className = classStr.replace("minus", "plus");
		}
	}
}

function toggleView(viewID, linkID) {
	id = "shortView_" + viewID;
	showHideBox(id);
	id = "fullView_" + viewID;
	showHideBox(id);
	more = (document.getElementById(id).style.display == "none") ? 0 : 1;
	if (linkID) {
		linkElem = document.getElementById(linkID);
		var classStr = linkElem.className;
		if (more) {
			linkElem.className = classStr.replace("more", "less");
			linkElem.innerHTML = 'weniger';
		}
		else {
			linkElem.className = classStr.replace("less", "more");
			linkElem.innerHTML = 'mehr';
		}
	}
}

function toggleView2(view1ID, view2ID, linkID) {
	if (view1ID) {
		more = (document.getElementById(view1ID).style.display != "none") ? 1 : 0;
		showHideBox(view1ID, '', -1, 100, 100);
	}
	if (view2ID) {
		more = (document.getElementById(view2ID).style.display != "none") ? 0 : 1;
		showHideBox(view2ID, '', -1, 100, 100);
	}
	if (linkID) {
		linkElem = document.getElementById(linkID);
		var classStr = linkElem.className;
		if (more) {
			linkElem.className = classStr.replace("more", "less");
			linkElem.innerHTML = 'weniger';
		}
		else {
			linkElem.className = classStr.replace("less", "more");
			linkElem.innerHTML = 'mehr';
		}
	}
}

var previewHeights = new Array();
function togglePreview(viewID, linkID) {
	elem = document.getElementById(viewID);
	//currentHeight = parseInt($(viewID).style.height);
	currentHeight = parseInt(elem.style.height);
	if (currentHeight) {
		previewHeights[viewID] = currentHeight;
		elem.style.height = '';
		more = 1;
	}
	else {
		elem.style.height = previewHeights[viewID] + 'px';
		more = 0;
	}
	if (linkID) {
		linkElem = document.getElementById(linkID);
		var classStr = linkElem.className;
		if (more) {
			linkElem.className = classStr.replace("more", "less");
			linkElem.innerHTML = 'weniger';
		}
		else {
			linkElem.className = classStr.replace("less", "more");
			linkElem.innerHTML = 'mehr';
		}
	}
   return more;
}

function hideSubmitButton(divID) {
   $('#'+divID).css('height', $('#'+divID).height()+'px').html('<span class="loadingIcon1"></span>');
}

var menueTimeOuts = new Array();

function showMenue(id) {
	clearTimeout(menueTimeOuts[id]);
   $('#'+id+':hidden').fadeIn('fast');
	//document.getElementById(id).style.display='block';
}

function hideMenue(id) {
	menueTimeOuts[id] = setTimeout(function() {$('#' + id).fadeOut('fast');}, 200);
	//document.getElementById(id).style.display='none';
}

var preDefVals = new Object();
function predef(obj, focus) {
	if(!preDefVals[obj.name]) {
		preDefVals[obj.name] = new Object();
		preDefVals[obj.name]['value'] = obj.value;
		obj.value = '';
		obj.onfocus = function() {
			predef(obj, true);
		}
		obj.onblur = function() {
			predefOut(obj, true);
		}
		obj.onmouseout = function() {
			predefOut(obj, false);
		}
	}
	else if(preDefVals[obj.name]['value'] == obj.value) obj.value = '';

	if(focus) preDefVals[obj.name]['focus'] = focus;
}

function predefOut(obj, focus) {
	if(obj.value == '' && (focus || !preDefVals[obj.name]['focus'])) {
		obj.value = preDefVals[obj.name]['value'];
		preDefVals[obj.name]['focus'] = false;
	}
}

var browser = '';
function getBrowser() {
	if (navigator.userAgent.indexOf("Opera") != -1) {browser = 'opera';}
	else if (navigator.userAgent.indexOf("Netscape") != -1) {browser = 'netscape';}
	else if (navigator.appName.indexOf("Internet Explorer") != -1) {browser = 'ie';}
	else if (navigator.userAgent.indexOf("Firefox") != -1) {browser = 'firefox';}
	else if (navigator.userAgent.indexOf("Chrome") != -1) {browser = 'chrome';}
	else if (navigator.userAgent.indexOf("Safari") != -1) {browser = 'safari';}
	else {browser = 'unknown';}
	return browser;
}


var ttShowTimer = 0;
function showTooltip(event, content, hoverObj, width, templateID, templateCCID) {
	if (!content) return;
	if (!event) alert('Event nicht definiert!');
	if ($(hoverObj).is(':animated')) return;
	content = decodeHTML(content);
	if (!templateID) templateID = 'tooltipTemplate';
	ttElem = document.getElementById(templateID);
	//if (!ttElem) {alert('Tooltipp: Vorlage nicht gefunden!');return;}
	if (!templateCCID) templateCCID = 'tooltipTemplateCC';
	ttCCElem = document.getElementById(templateCCID);
	//if (!ttCCElem) {alert('Tooltipp: Container nicht gefunden!');return;}
	ttCCElem.innerHTML = content;

	if (width) ttElem.style.width = width + 'px';
	if (!width) {
		width = parseInt(ttElem.style.width);
		if (!width) width = 400;
	}
	clearTimeout(ttShowTimer);
	$("#tooltipTemplate").stop(true, true).fadeIn(100);
	if (!$(hoverObj).attr('tt_init')) {
		$(hoverObj).mousemove(function(){moveTooltip()});
		$(hoverObj).mouseleave(function(){$("#tooltipTemplate").stop(true, true).fadeOut(500);});
		$(hoverObj).attr('tt_init', 1);
	}
}

function moveTooltip() {
	var mPosition = getMousePos();
	$("#tooltipTemplate").css('left', (mPosition.x+10)+"px");
	$("#tooltipTemplate").css('top', (mPosition.y+10)+"px");
}

function getCaretPos(elem) {
	if(typeof elem.selectionStart != 'undefined') return elem.selectionStart;
	var oSel = document.selection.createRange();
	oSel.moveStart('character', -elem.value.length);
   if (oSel.text.length) return oSel.text.length;
	return -1;
}

function setCaretPos(elem, pos) {
	if(elem.setSelectionRange) {
		elem.setSelectionRange(pos, pos);
	}
	else if (elem.createTextRange) {
		var range = elem.createTextRange();
		range.move("character", pos);
		range.select();
	} else if(window.getSelection){
		s = window.getSelection();
		var r1 = document.createRange();
		var walker=document.createTreeWalker(elem, NodeFilter.SHOW_ELEMENT, null, false);
		var p = pos;
		var n = elem;
		while(walker.nextNode()) {
			n = walker.currentNode;
			if(p > n.value.length) {
				p -= n.value.length;
			}
			else break;
		}
		n = n.firstChild;
		r1.setStart(n, p);
		r1.setEnd(n, p);
		s.removeAllRanges();
		s.addRange(r1);
	}
	else if (document.selection) {
		var oSel = document.selection.createRange();
		oSel.moveStart('character', -elem.value.length);
		oSel.moveStart('character', pos);
		oSel.moveEnd('character', pos-elem.value.length);
		oSel.select();
	}
}

function replaceFloatInputContent(content) {
	content = content+' ';
	var pref = content.substr(0,1)=='-'?'-':'';
	content = content.replace(/[^0-9,]+/g, '');
	var pointPos = content.indexOf(',');
	if (pointPos == -1) pointPos = content.length;
	var digitCounter = -1;
	var pointCounter = 0;
	for (i = pointPos; i>=0 ; i--) {
		digitCounter++;
		if (digitCounter > 2 && i) {
			content = content.substr(0,i) + '.' + content.substr(i);
			digitCounter = 0;
			pointCounter++;
		}
	}
	return pref+content;
}

var keys_ctrl = 0;
var keys_shift = 0;
function replaceDateInputContent(content) {
	content = content.replace(/[^0-9]+/g, '');
	content = content.substr(0, 8);
	if (content.length > 1) content = content.substr(0, 2) + '.' + content.substr(2);
	if (content.length > 4) content = content.substr(0, 5) + '.' + content.substr(5);
	if (parseInt(content.substr(0, 2)) > 31) content = '31' + content.substr(2);
	if (parseInt(content.substr(3, 2)) > 12) content = content.substr(0, 3) + '12' + content.substr(5);
	if (parseInt(content.substr(6, 4)) > 2005) content = content.substr(0, 6) + '2005' + content.substr(10);
	return content;
}

function handleFloatInput(event, elem) {
	if (event.which==16 || event.which==17) return;
	if (event.which==9 || (keys_ctrl&&event.which==65)) {	/* Tab */elem.blur();elem.focus();return;}
	elem.focus();
	var cursorPos = getCaretPos(elem);
	if (cursorPos != -1) {
		var content = elem.value;
		var oldContentLength = content.length;
		content = replaceFloatInputContent(content);
		if (event.which == 8) {	// Backspace?
			if (content[cursorPos] == '.') {
				content = content.substr(0,cursorPos-1) + content.substr(cursorPos);
				content = replaceFloatInputContent(content);
			}
		}
		var newContentLength = content.length;
		elem.value = content;
		setCaretPos(elem, cursorPos - (oldContentLength - newContentLength));
	}
}

function handleDateInput(event, elem) {
	if (event.which==16 || event.which==17) return;
	if (event.which==9 || (keys_ctrl&&event.which==65)) {	/* Tab */elem.blur();elem.focus();return;}
	elem.focus();
	var cursorPos = getCaretPos(elem);
	var tempCursorPos = cursorPos;
	if (cursorPos>5) tempCursorPos-=2;else if (cursorPos>2) tempCursorPos--;

	var content = elem.value;
	var oldContentLength = content.length;
	content = content.replace(/[^0-9]+/g, '');
	if (event.which == 8) {	// Backspace?
		if (cursorPos == oldContentLength) {content = content.substr(0,cursorPos-(cursorPos>3?2:1));}
		else if (cursorPos == 2 || cursorPos == 5) {content = content.substr(0,tempCursorPos-1) + '0' + content.substr(tempCursorPos);cursorPos-=2;}
		else if (tempCursorPos < 4 || content.length > 8) {content = content.substr(0,tempCursorPos) + '0' + content.substr(tempCursorPos);content = content.replace(/[^0-9]+/g, '');cursorPos--;}
	}
	else if (event.which > 47 && event.which < 58) {	// Zahlen
		if (cursorPos == oldContentLength) {}
		else if (cursorPos == 3 || cursorPos == 6) {content = content.substr(0,tempCursorPos+1) + content.substr(tempCursorPos+2);cursorPos+=2;}
		else if (tempCursorPos < 5 || content.length > 8) {content = content.substr(0,tempCursorPos) + content.substr(tempCursorPos+1);cursorPos++;}
	}
	else if (event.which == 190) {	// Punkt
		var v;
		if (cursorPos < 2) {content = '01'+content.substr(3);cursorPos = 5;}
		else if (cursorPos == 2) {v = parseInt(content.substr(0,1));if (v < 1) {v++;}content = '0'+v+content.substr(2);cursorPos = 4;}
		else if (cursorPos==3 || cursorPos==6) {cursorPos++;}
		else if (cursorPos == 5) {v = parseInt(content.substr(2,1));if (v < 1) {v++;}content = content.substr(0,2)+'0'+v+content.substr(4);cursorPos = 7;}
	}
	content = content.substr(0, 8);
	if (content.length > 1) content = content.substr(0, 2) + '.' + content.substr(2);
	if (content.length > 4) content = content.substr(0, 5) + '.' + content.substr(5);
	if (parseInt(content.substr(0, 2)) > 31) content = '31' + content.substr(2);
	if (parseInt(content.substr(3, 2)) > 12) content = content.substr(0, 3) + '12' + content.substr(5);
	if (parseInt(content.substr(6, 4)) > 2100) content = content.substr(0, 6) + '2100' + content.substr(10);
	var newContentLength = content.length;
	elem.value = content;
	setCaretPos(elem, cursorPos - (oldContentLength - newContentLength));
}

function handleTimeInput(event, elem) {
	if (event.which==16 || event.which==17) return;
	if (event.which==9 || (keys_ctrl&&event.which==65)) {	/* Tab */elem.blur();elem.focus();return;}
	elem.focus();
	var cursorPos = getCaretPos(elem);
	var tempCursorPos = cursorPos;
	if (cursorPos>5) tempCursorPos-=2;else if (cursorPos>2) tempCursorPos--;
	var content = elem.value;
	var oldContentLength = content.length;
	content = content.replace(/[^0-9]+/g, '');
	if (event.which == 8) {	// Backspace?
		if (cursorPos == oldContentLength) {content = content.substr(0,cursorPos-(cursorPos>4?2:1));}
		else if (cursorPos == 2 || cursorPos == 5) {content = content.substr(0,tempCursorPos-1) + '0' + content.substr(tempCursorPos);cursorPos-=2;}
		else if (tempCursorPos < 4 || content.length > 8) {content = content.substr(0,tempCursorPos) + '0' + content.substr(tempCursorPos);content = content.replace(/[^0-9]+/g, '');cursorPos--;}
	}
	else if (event.which > 47 && event.which < 58) {	// Zahlen
		if (cursorPos == oldContentLength) {}
		else if (cursorPos == 3 || cursorPos == 6) {content = content.substr(0,tempCursorPos+1) + content.substr(tempCursorPos+2);cursorPos+=2;}
		else if (tempCursorPos < 5 || content.length > 8) {content = content.substr(0,tempCursorPos) + content.substr(tempCursorPos+1);cursorPos++;}
	}
	else if (event.which == 190) {	// Punkt
		var v;
		if (cursorPos < 2) {content = '01'+content.substr(3);cursorPos = 5;}
		else if (cursorPos == 2) {v = parseInt(content.substr(0,1));if (v < 1) {v++;}content = '0'+v+content.substr(2);cursorPos = 4;}
		else if (cursorPos==3 || cursorPos==6) {cursorPos++;}
		else if (cursorPos == 5) {v = parseInt(content.substr(2,1));if (v < 1) {v++;}content = content.substr(0,2)+'0'+v+content.substr(4);cursorPos = 7;}
	}
	content = content.substr(0, 6);
	if (content.length > 1) content = content.substr(0, 2) + ':' + content.substr(2);
	if (content.length > 5) content = content.substr(0, 5) + ':' + content.substr(5);
	if (parseInt(content.substr(0, 2)) > 23) content = '23' + content.substr(2);
	if (parseInt(content.substr(3, 2)) > 59) content = content.substr(0, 3) + '59' + content.substr(5);
	if (parseInt(content.substr(6, 2)) > 59) content = content.substr(0, 6) + '59';
	var newContentLength = content.length;
	elem.value = content;
	setCaretPos(elem, cursorPos - (oldContentLength - newContentLength));
}

function handleIntInput(event, elem, maxInt, minInt) {
	if (event.which==16 || event.which==17) return;
	if (event.which==9 || (keys_ctrl&&event.which==65)) {	/* Tab */elem.blur();elem.focus();return;}
	elem.focus();
	var content = elem.value;
	content = content+' ';
	var pref = content.substr(0,1)=='-'?-1:1;
	content = parseInt(content.replace(/[^0-9]+/g, ''));
	if (isNaN(content)) content = pref==-1?'-':'';
	else {
		content = pref*content;
		if (maxInt && parseInt(content)>maxInt) content = '' + maxInt;
		if (minInt && parseInt(content)<minInt) content = '' + minInt;
	}
	elem.value = content;
	if (event.which == 9) {	/* Tab */elem.blur();elem.focus();}
}

function initInputs(class_int, class_float, class_date, class_time) {
	if (!class_int) class_int = 'input_int';
	if (!class_float) class_float = 'input_float';
	if (!class_date) class_date = 'input_date';
	if (!class_time) class_time = 'input_time';
	$('body').keyup(function(event) {if (event.which==17) keys_ctrl=0;else if (event.which==16) keys_shift=0;});
	$('body').keydown(function(event) {if (event.which==17) keys_ctrl=1;else if (event.which==16) keys_shift=1;});
   $('.'+class_int).each(function(){$(this).keyup(function(event){handleIntInput(event, this, $(this).attr('max'), $(this).attr('min'))})}) 
   $('.'+class_float).each(function(){$(this).keyup(function(event){handleFloatInput(event, this)})}) 
   $('.'+class_date).each(function(){$(this).keyup(function(event){handleDateInput(event, this)});if (!$(this).attr('maxlength'))$(this).attr('maxlength', '10')}) 
   $('.'+class_time).each(function(){$(this).keyup(function(event){handleTimeInput(event, this)});if (!$(this).attr('maxlength'))$(this).attr('maxlength', '8')}) 
}

function getLinkAnchor() {
	var anchorPos = window.location.href.lastIndexOf('#');
	if (anchorPos) {return window.location.href.substr(anchorPos+1);}
	return '';
}

/**
 * Tabs
 */
var tabSettings = new Array();

function initTabs(name, idTabs, idTabContents, idTabLinks) {
	tabSettings[name] = new Array();
	tabSettings[name]['idTabs'] = idTabs;
	tabSettings[name]['idTabContents'] = idTabContents;
	tabSettings[name]['idTabLinks'] = idTabLinks;
	tabSettings[name]['activeTab'] = 0;

	if (anchor = getLinkAnchor()) {
		var i=0;
		while (elem = document.getElementById(''+idTabs+i)) {
			link = elem.getElementsByTagName('a')[0];
			if (link && link.name == anchor) {
				change2Tab(i, name);
				//link.click();
				break;
			}
			++i;
		}
	}
}

function change2Tab(number, name) {
	if (tabSettings[name]['activeTab'] == number) return;
	elemTab = document.getElementById(tabSettings[name]['idTabs']+number);
	elemTabContent = document.getElementById(tabSettings[name]['idTabContents']+number);
	elemOldTab = document.getElementById(tabSettings[name]['idTabs']+tabSettings[name]['activeTab']);
	elemOldTabContent = document.getElementById(tabSettings[name]['idTabContents']+tabSettings[name]['activeTab']);
	elemTab.className = elemTab.className.replace('inactive', 'active');
	elemOldTab.className = elemTab.className.replace('active', 'inactive');
	elemOldTabContent.style.display = 'none';
	elemTabContent.style.display = 'block';
	tabSettings[name]['activeTab'] = number;
}

// ContentAssign
var ca_oldReferenceType = '';
var ca_oldReferenceID = 0;
var ca_idRefType = 'referenceType';
var ca_idRefID = 'referenceID';
var ca_idRefTxt = 'referenceText';
var ca_idSelRefType = 'sReferenceType';
function ca_setReferenceType(referenceType) {
	//alert(referenceType);
   elem = document.getElementById(ca_idRefType);
	if (!elem) alert('FEHLER! CA: Input (Type) nicht gefunden!');
	if (!referenceType) {elem.value = ca_oldReferenceType;ca_setReferenceID(0);return;}
	if (!ca_oldReferenceType) {ca_oldReferenceType = elem.value;}
	elem.value = referenceType;
}
function ca_setReferenceID(referenceID) {
    elem = document.getElementById(ca_idRefID);
	 if (!elem) alert('FEHLER! CA: Input (ID) nicht gefunden!');
	 if (!referenceID) {elem.value = ca_oldReferenceID;return;}
	 if (!ca_oldReferenceID) {ca_oldReferenceID = elem.value;}
	 elem.value = referenceID;
}
function ca_deleteReference() {
   document.getElementById(ca_idRefType).value = '';
   document.getElementById(ca_idRefID).value = 0;
	document.getElementById(ca_idRefTxt).innerHTML = '[keine]';
   ca_oldReferenceType = '';
   ca_oldReferenceID = 0;
	document.getElementsByName(ca_idSelRefType)[0].selectedIndex = '';
	xajax_ca_changeRefType('');
}
// ---

// Module reloaden
function onReloadModuleFinished(assignID, transition, forward2) {
	if (forward2) window.location.href = forward2;
   if (typeof myOnReloadModuleFinished == 'function'){myOnReloadModuleFinished(assignID, transition);return;}
	if (transition) $('#module'+assignID).hide().fadeIn(200, function(){$('#module'+assignID).unwrap();showMessages();});
	else showMessages();
}

function reloadModule(assignID, request, transition) {
   if (typeof myReloadModule == 'function'){myReloadModule(assignID, request, transition);return;}
   if (transition === undefined) transition = 1;
	modCon = document.getElementById('module' + assignID);
	if (modCon) {
		formElems = modCon.getElementsByTagName('input');
		for (i = 0; i < formElems.length; i++) {formElems[i].disabled = 1;}
		formElems = modCon.getElementsByTagName('a');
		for (i = 0;i < formElems.length;i++) {formElems[i].removeAttribute('href');formElems[i].className += ' disabled';}
		module = $(modCon);
		$('script', module).removeAttr('src').empty();
		if (transition) {
			module.wrap('<div id="tmodule'+assignID+'" style="height:'+module.height()+'px"/>');
			module.fadeOut(300);
		}
	}
	$('#cMessages').slideUp(200);
	xajax_reloadModule(assignID, request, transition);
}
// ---

function formConfirm(question, idForm, idTemplate, width) {
	if (!idTemplate) idTemplate = 'confirmationTemplateForm';
	container = $('#'+idTemplate);
	if (!container) alert('Template für Bestätigung nicht gefunden!');
	if (question) $('.question', container).html(question);
	link = 'javascript:gebi(\''+idForm+'\').submit()';
	$('a.linkYes', container).attr('href', link);
	showDialog(idTemplate, width);
}

function linkConfirm(question, link, idTemplate, width) {
	if (!idTemplate) idTemplate = 'confirmationTemplate';
	container = $('#'+idTemplate);
	if (!container) alert('Template für Bestätigung nicht gefunden!');
	if (question) $('.question', container).html(question);
	if (link.substr(0, 11) == 'javascript:') {
		$('form', container).unbind('submit').submit(function(){eval(link.substr(11));hideDialog(idTemplate);return false});
	}
	else {
		$('form', container).attr('action', link);
		$('a.linkYes', container).attr('href', link);
	}
	//$('input.linkYes', container).click('href', link);
	showDialog(idTemplate, width);
}

function showMessageError(message) {
   if (typeof myShowMessageError == 'function') {myShowMessageError(message);return;}
	if ($('#tMessageError').attr('viewed')) return;
   if (message) {
		$('#tMessageError').attr('viewed', '1');
		alert(message);
	}
}

function showMessageWarning(message) {
   if (typeof myShowMessageWarning == 'function') {myShowMessageWarning(message);return;}
	if ($('#tMessageWarning').attr('viewed')) return;
   if (message) {
		$('#tMessageWarning').attr('viewed', '1');
		alert(message);
	}
}

function showMessageStatus(message) {
   if (typeof myShowMessageStatus == 'function') {myShowMessageStatus(message);return;}
	if ($('#tMessageStatus').attr('viewed')) return;
   if (message) {
		$('#tMessageStatus').attr('viewed', '1');
		alert(message);
	}
}

function showMessageHint(message) {
   if (typeof myShowMessageHint == 'function') {myShowMessageHint(message);return;}
	if ($('#tMessageHint').attr('viewed')) return;
   if (message) {
		$('#tMessageHint').attr('viewed', '1');
		alert(message);
	}
}

function showMessages() {
	alert(1);
   if (typeof myShowMessages == 'function') {myShowMessages();return;}
   showMessageError($('#cMessageError').html());
   showMessageStatus($('#cMessageStatus').html());
   showMessageWarning($('#cMessageWarning').html());
   showMessageHint($('#cMessageHint').html());
}

function centerOnScreen(idTemplate, animate) {
	var container = $('#'+idTemplate);
	if (!container) alert('Template nicht gefunden!');
	if (animate) {
		container.animate({left: (($(window).width()-container.outerWidth())/2)+"px", top: (($(window).height()-container.outerHeight())/2)+"px"}, 300 );
	}
	else {
		container.css('left', (($(window).width()-container.outerWidth())/2)+'px');
		container.css('top', (($(window).height()-container.outerHeight())/2)+'px');
	}
}

function showLayer(idTemplate, width) {
	var container = $('#'+idTemplate);
	if (!container) alert('Template für Layer nicht gefunden!');
	$('body').append('<div id="layerBackgroundMask" class="layerBackgroundMask" style="display:none"/>');
	$('.linkCancel, .linkNo', container).click(function(){hideLayer(idTemplate);});
	centerOnScreen(idTemplate, 0);
	$('#layerBackgroundMask').css('height', $('body').height()+'px').fadeIn(100);
	container.fadeIn(100);
	$('body').click(function(){hideLayer(idTemplate);});
	container.click(function(e){e.stopPropagation();});
}

function hideLayer(idTemplate) {
	var container = $('#'+idTemplate);
	if (!container) alert('Template für Dialog nicht gefunden!');
	container.fadeOut(200);
	$('#layerBackgroundMask').fadeOut(200, function(){$('#layerBackgroundMask').remove();});
	$('body').click(function(){});
}

function showDialog(idTemplate, width, x, y, refElem, onHide, onShow) {
	//if (event != undefined) event.stopPropagation();
	var container = $('#'+idTemplate);
	if (!container) alert('Template für Dialog nicht gefunden!');
	$('.linkCancel, .linkNo', container).click(function(){hideDialog(idTemplate, onHide);});
	if (refElem != 'fixedPos') {
		if (refElem) {
			x += $(refElem).offset().left;
			y += $(refElem).offset().top;
		}
		if (!x) x = mPosition.x-40;
		if (!y) y = mPosition.y-20;
		if (width) container.animate({left: x+"px", top: y+"px", width: width+"px"}, 100 );
		else container.animate({left: x+"px", top: y+"px"}, 100 );
	}
	if (onShow) onShow();
	container.fadeIn(100);
	$('body').click(function(){hideDialog(idTemplate, onHide);});
	container.click(function(e){e.stopPropagation();});
}

function hideDialog(idTemplate, onHide) {
	var container = $('#'+idTemplate);
	if (!container) alert('Template für Dialog nicht gefunden!');
	container.fadeOut(200, onHide);
	$('body').click(function(){});
}

var mPosition = new Object();
mPosition.x = 0;
mPosition.y = 0;
function getMousePos(event) {
	if (!event) return mPosition;
	var mouseX = 0;
	var mouseY = 0;
	if (window.event) {
		event = window.event;
		mouseX = event.clientX;
		mouseY = event.clientY
		//alert('scrollTop: '+document.documentElement.scrollTop + ' clientY:'+event.clientY);
	}
	else {
		mouseX = event.clientX;
		mouseY = event.clientY;
	}
	mouseX += document.documentElement.scrollLeft;
	mouseY += document.documentElement.scrollTop;
	if (!document.documentElement.scrollLeft) mouseX += document.body.scrollLeft;
	if (!document.documentElement.scrollTop) mouseY += document.body.scrollTop;
	position = new Object();
	position.x = mouseX;
	position.y = mouseY;
	return position;
}

function disableSubmit() {
	$(this).submit(function(){return false;});
	$(':input', this).attr('readonly', '1');
	$(':button, input:submit', this).hide();
}

function setOnSubmit(form, newSubmit) {
	oldOnSubmit = form.onsubmit;
	if (oldOnSubmit) {$(form).submit(function(){var f=oldOnSubmit;f();$(form).submit(newSubmit);});}
	else {$(form).submit(newSubmit);}
}

function setOnClick(elem, newOnClick) {
	oldOnClick = elem.onclick;
	if (oldOnClick) {$(elem).click(function(){oldOnClick();$(elem).click(newOnClick);});}
	else {$(elem).click(newOnClick);}
}

function scrollDown(selector) {
   var elem = $(selector);
   elem.scrollTop(elem.children().height());
}

/**
 * Pinnwand
 */
var pb_cse = 0;

function pb_onLoadAnswersFinished(entryID) {
	$('#answers'+entryID).hide().fadeIn(200, function(){$('#answers'+entryID).unwrap();});
	initSite();
}

function pb_loadAnswers(entryID, maid) {
	con = document.getElementById('answers' + entryID);
	formElems = con.getElementsByTagName('input');
	for (i = 0; i < formElems.length; i++) {formElems[i].disabled = 1;}
	formElems = con.getElementsByTagName('a');
	for (i = 0;i < formElems.length;i++) {formElems[i].removeAttribute('href');formElems[i].className += ' disabled';}

   con = $(con);
   con.wrap('<div id="tanswers'+entryID+'" style="height:'+con.height()+'px"/>');
   con.fadeOut(300);
   xajax_pb_loadAnswers(entryID, maid);
}

function pb_onLoadMoreFinished() {
	$('#pbMore').slideDown(500, function(){$('#pbMore').children().unwrap()});
	initSite();
}

function pb_loadMore(firstID, maid) {
   if (!firstID) return;
   $('#pbMore').css('display', 'none');
   xajax_pb_loadMore(firstID, maid);
}

/**
 * Kommentare
 */
var com_cse = 0;
function com_onLoadAnswersFinished(entryID) {
	$('#answers'+entryID).hide().fadeIn(200, function(){$('#answers'+entryID).unwrap();});
	initSite();
}

function com_loadAnswers(entryID, maid) {
	con = document.getElementById('answers' + entryID);
	formElems = con.getElementsByTagName('input');
	for (i = 0; i < formElems.length; i++) {formElems[i].disabled = 1;}
	formElems = con.getElementsByTagName('a');
	for (i = 0;i < formElems.length;i++) {formElems[i].removeAttribute('href');formElems[i].className += ' disabled';}

   con = $(con);
   con.wrap('<div id="tanswers'+entryID+'" style="height:'+con.height()+'px"/>');
   con.fadeOut(300);
   xajax_com_loadAnswers(entryID, maid);
}

function com_onLoadMoreFinished() {
	$('#comMore').slideDown(500, function(){$('#comMore').children().unwrap()});
	initSite();
}

function com_loadMore(firstID, maid) {
   if (!firstID) return;
   $('#comMore').css('display', 'none');
   xajax_com_loadMore(firstID, maid);
}

/**
 * Forum
 */
function f_onGetEditContentFinished(entryID, inputID, error) {
   $('#f_editContent'+entryID).removeClass('loadingBgrnd2');
	if (!error) $('#f_editContent'+entryID).attr('disabled', '0').removeAttr('disabled');
	else {$('#'+inputID).closest('form').find('input[type=submit]').attr('disabled', '1');}
	initSite();
}

function f_getEditContent(entryID) {
   $('#f_editContent'+entryID).addClass('loadingBgrnd2').attr('disabled', '1');
   showHideBox('f_entry'+entryID);
   showHideBox('f_entryEdit'+entryID);
   xajax_getEditContent('forumEntry', entryID, 'f_editContent'+entryID);
}

function onGetEditContentFinished(type, contentID) {
   $('#contentEdit_'+type+'_'+contentID).removeClass('loadingBgrnd2').attr('disabled', '0').removeAttr('disabled');
	initSite();
}

function getEditContent(type, contentID) {
   $('#contentEdit_'+type+'_'+contentID).addClass('loadingBgrnd2').attr('disabled', '1');
   showHideBox('content_'+type+'_'+contentID);
   showHideBox('cContentEdit_'+type+'_'+contentID);
   xajax_getEditContent(type, contentID);
}

function oh_changeHide(elem, turnOn) {
	if (turnOn) $(elem).parents('.cOptionsHoverArea').mouseleave(function() {$('.cOptionsHover', this).stop(true,true).fadeOut(500);});
	else {$(elem).parents('.cOptionsHoverArea').unbind('mouseleave');$(elem).parents('.cOptionsHover').stop(true,true).fadeIn(100);}
}

function initOptionsHover() {
   $('.cOptionsHover').each(function() {if (!$(this).parent().hasClass('cOptionsHoverWrap')) $(this).wrap('<div class="cOptionsHoverWrap"/>').hide();});
   $('.cOptionsHoverArea').each(function(){
		if (!$(this).attr('oh_init')) {
			$(this).hover(function(){$('.cOptionsHover', this).stop(true,true).fadeIn(100);}, function(){$('.cOptionsHover', this).stop(true,true).fadeOut(500);});
			$(this).attr('oh_init',1);
		}
	});
}

var ls_config = new Array();
function initLiveSearch(idInput, mode, idRefID, submitForm, idRefType) {
	submitForm = submitForm?1:0;
	var idContainer;
	var elem = $('#'+idInput);
	if (elem.attr('ls_initialized')) return;
	idContainer = 'ls_con_'+idInput;
	ls_config[idInput] = {mode:mode, idRefID:idRefID, idRefType:idRefType, submitForm:submitForm, idContainer:idContainer};
	elem.after('<div style="position:relative;display:inline"><div id="'+idContainer+'" class="ls_container" style="position:absolute;width:'+elem.outerWidth()+'px"></div></div>')
   elem.keyup(function(event){ls_handleKeyup(event, idInput);});
   elem.focus(function(event){ls_handleKeyup(event, idInput);if (ls_config[idInput].idRefType){$('#'+ls_config[idInput].idRefType).val(ls_config[idInput].mode)}});
   elem.keydown(function(event){ls_handleKeydown(event,idInput);});
   elem.blur(function(event){$('#'+idContainer).fadeOut(200);});
	elem.attr('autocomplete', 'off');
	elem.attr('ls_initialized', 'true');
//	elem.parents('form').get(0).onsubmit = function(){if (elem.val().length<2) return false;};
}

function ls_onUpdateFinished(idInput, error) {
	var eI;var eC;
	eI = $('#'+idInput);eC = $('#'+ls_config[idInput].idContainer);
	eI.attr('orig_value', eI.val());
	ls_moveCursor(0, idInput);
	$('.suggestion', eC).click(function(){eI.val($(this).text());if (ls_config[idInput].idRefID) $('#'+ls_config[idInput].idRefID).val($(this).attr('refid'));if (ls_config[idInput].submitForm) eI.closest('form').find('input[type=submit]').click();});
	if (eC.is(':hidden')) eC.fadeIn(200);
}

function ls_moveCursor(dir, idInput) {
	var eI;var eC;
	eI = $('#'+idInput);eC = $('#'+ls_config[idInput].idContainer);
	$('.suggestion', eC).removeClass('current');
	var cp;
	cp = parseInt(eC.attr('cursorPos'));
	if (!cp) eI.attr('orig_value', eI.val());
	if (dir==0) cp=0;
	else cp+=dir;
	if (cp<0) return;
	if (!cp) {eI.val(eI.attr('orig_value'));if (ls_config[idInput].idRefID)$('#'+ls_config[idInput].idRefID).val('0');}
	if (cp>$('.suggestion', eC).size()) cp=$('.suggestion', eC).size();
	eC.attr('cursorPos', cp);
	if (cp<1) return;
	$($('.suggestion', eC).get(cp-1)).addClass('current');
	eI.val($('.suggestion.current', eC).text());
	if (ls_config[idInput].idRefID) $('#'+ls_config[idInput].idRefID).val($('.suggestion.current', eC).attr('refid'));
}

function ls_handleKeydown(event, idInput) {
	var eI;var eC;
	eI = $('#'+idInput);eC = $('#'+ls_config[idInput].idContainer);
	if (event.keyCode==27) {ls_moveCursor(0, idInput);eI.blur();}
	else if (event.keyCode==13) {if (eI.val().length<2) return;else if (!ls_config[idInput].submitForm) {eI.blur();}};
}

function ls_handleKeyup(event, idInput) {
	var eI;var eC;
	eI = $('#'+idInput);eC = $('#'+ls_config[idInput].idContainer);
	if (eI.val().length<3) return;
	//alert(event.keyCode);
	if (event.keyCode == 38) ls_moveCursor(-1, idInput);
	else if (event.keyCode == 40) ls_moveCursor(1, idInput);
	//else if (event.keyCode==13) {	if (submitForm) eI.parents('form').submit(); else eI.blur();}
	//else if (event.keyCode == 37);
	//else if (event.keyCode == 39);
	//else if (event.keyCode == 8);
	else xajax_ls_search(eI.val(), ls_config[idInput].mode, idInput, ls_config[idInput].idContainer, ls_config[idInput].idRefID);
}

var ln_maxReloads = 600;
var ln_reloadInterval = 6;
var ln_reloadCounter = 0;
var ln_disableReload = 0;
var ln_moduleAssignID = 0;
function ln_reload() {
	//$('#module'+ln_moduleAssignID).prepend(ln_disableReload+' ');
	//alert(ln_disableReload);
	if (ln_reloadCounter > ln_maxReloads) return;
	if (!ln_disableReload) reloadModule(ln_moduleAssignID, '', 0);
	setTimeout(ln_reload, ln_reloadInterval*1000);
	ln_reloadCounter++;
}


function ref_changeType(type, destID) {
	xajax_ref_changeType(type, destID)
}

function ref_setReferenceID(referenceID) {
	var elem = document.getElementById(ca_idRefID);
	if (!elem) alert('FEHLER! CA: Input (ID) nicht gefunden!');
	if (!referenceID) {elem.value = ca_oldReferenceID;return;}
	if (!ca_oldReferenceID) {ca_oldReferenceID = elem.value;}
	elem.value = referenceID;
}

function ref_setReferenceType(referenceType) {
	//alert(referenceType);
   elem = document.getElementById(ca_idRefType);
	if (!elem) alert('FEHLER! CA: Input (Type) nicht gefunden!');
	if (!referenceType) {elem.value = ca_oldReferenceType;ref_setReferenceID(0);return;}
	if (!ca_oldReferenceType) {ca_oldReferenceType = elem.value;}
	elem.value = referenceType;
}

function ref_deleteReference() {
   document.getElementById(ca_idRefType).value = '';
   document.getElementById(ca_idRefID).value = 0;
	document.getElementById(ca_idRefTxt).innerHTML = '[keine]';
   ca_oldReferenceType = '';
   ca_oldReferenceID = 0;
	document.getElementsByName(ca_idSelRefType)[0].selectedIndex = '';
	xajax_ca_changeRefType('');
}

function ref_addReference(refType1, refID1, refType2, refID2, listType) {
	if (!refType1) {alert('Bitte Typ wählen!');return;}
	if (refID1==0 || refID1=='') {alert('Bitte Inhalt wählen!');return;}
	xajax_ref_addReference(refType1, refID1, refType2, refID2, listType);
}



function onGetEntryPreviewFinished(mode, templateID, formContainerID, contentID) {
   $('#'+contentID).removeClass('loadingBgrnd2');
	initSite();
}

function getEntryPreview(mode, templateID, formContainerID, contentID, request) {
   $('#'+contentID).empty().addClass('loadingBgrnd2');
   showHideBox(templateID);
   showHideBox(formContainerID);
	xajax_getEntryPreview(mode, templateID, formContainerID, contentID, request);
}
