function msgLoc(msg, loc)
{
	if (confirm(msg)) self.location.href = loc;
}
function focusInput(inp)
{
	setCls(inp, "style2");
}
function blurInput(inp)
{
	setCls(inp, "style1");
}
function focusInput2(objEvt)
{
	if (navigator.appName == "Microsoft Internet Explorer")
		objEvt = window.event.srcElement;
	else
		objEvt = objEvt.target;
	setCls(objEvt, "style2");
}
function blurInput2(objEvt)
{
	if (navigator.appName == "Microsoft Internet Explorer")
		objEvt = window.event.srcElement;
	else
		objEvt = objEvt.target;
	setCls(objEvt, "style1");
}
function saveGb(bsgid)
{
	var msg = "";
	var nFrm = document.getElementById("frmGb");
	var nName = document.getElementById("txtName");
	var nEintrag = document.getElementById("txtEintrag");
	var nSpam = document.getElementById("txtSpam");
	var strName = nName.value;
	var strEintrag = nEintrag.value;
	var strSpam = nSpam.value;
	
	nFrm.setAttribute("action", "index.php?page=gb&cmd=save&bsgid=" + bsgid);
	if (strName == "") msg += "Bitte einen Namen angeben!\n";
	if (strEintrag == "") msg += "Bitte das Eintragsfeld füllen!\n";
	if (strSpam == "") msg += "Bitte zwecks Spamschutz den Inhalt der kleinen Grafik angeben!\n";
	
	if (msg != "")
		alert(msg);
	else
		nFrm.submit();
}

function TextField(name, size, max, value)
{
	var txt = document.createElement("input");
	txt.setAttribute("type", "text");
	setCls(txt, "style1");
	txt.setAttribute("size", size);
	txt.setAttribute("maxlength", max);
	txt.setAttribute("value", value);
	txt.id = name;
	txt.onfocus = focusInput2;
	txt.onblur = blurInput2;
	
	this.getObject = getObject;
	
	function getObject()
	{
		return txt;
	}
}

function CheckBox(name, checked)
{
	var chk = document.createElement("input");
	chk.setAttribute("type", "checkbox");
	setCls(chk, "style1");
	if (checked) chk.setAttribute("checked", "1");
	chk.id = name;
	chk.onfocus = focusInput2;
	chk.onblur = blurInput2;
	
	this.getObject = getObject;
	
	function getObject()
	{
		return chk;
	}
}

function PwField(name, size, max, value)
{
	var txt = new TextField(name, size, max, value).getObject();
	txt.setAttribute("type", "password");

	this.getObject = getObject;
	
	function getObject()
	{
		return txt;
	}
}

function Button(caption, path)
{
	var cmd = document.createElement("button");
	var nTable = document.createElement("table");
	var nTBody = document.createElement("tbody");
	var nTr = document.createElement("tr");
	var nTd1 = document.createElement("td");
	var nTd2 = document.createElement("td");
	var nTd3 = document.createElement("td");
	var nImgL = document.createElement("img");
	var nImgR = document.createElement("img");
	nTr.style.height = "20px";
	nImgL.setAttribute("src", path + "/button_l.gif");
	nImgR.setAttribute("src", path + "/button_r.gif");
	nTd1.style.padding = "0px";
	nTd1.appendChild(nImgL);
	nTd2.style.fontSize = "10pt";
	nTd2.style.backgroundImage = "url(" + path + "/button_m.gif)";
	nTd2.style.color = "#5050ff";
	nTd2.innerHTML = caption;
	nTd3.style.padding = "0px";
	nTd3.appendChild(nImgR);
	nTr.appendChild(nTd1);
	nTr.appendChild(nTd2);
	nTr.appendChild(nTd3);
	nTBody.appendChild(nTr);
	nTable.style.borderCollapse = "collapse";
	nTable.setAttribute("cellpadding", "0");
	nTable.appendChild(nTBody);
	cmd.setAttribute("type", "button");
	setCls(cmd, "button");
	cmd.appendChild(nTable);
	
	this.getObject = getObject;
	
	function getObject()
	{
		return cmd;
	}
}

function List(arCap)
{
	this.getCaptionLine = getCaptionLine;
	this.getContentLine = getContentLine;
	this.getSingleContentLine = getSingleContentLine;
	
	function getCaptionLine()
	{
		var nTr = document.createElement("tr");
		for (var i=0; i<arCap.length; i++)
		{
			var nTh = document.createElement("th");
			setCls(nTh, "list");
			nTh.appendChild(document.createTextNode(arCap[i]));
			nTr.appendChild(nTh);
		}
		return nTr;
	}
	
	function getContentLine(nlCont, arId, strId)
	{
		var nTr = document.createElement("tr");
		for (var l=0; l<nlCont.length; l++)
		{
			var nTd = document.createElement("td");
			setCls(nTd, "list");
			if (l<arId.length) nTd.id = arId[l] + strId;
			if (nlCont[l].hasChildNodes()) nTd.innerHTML = nlCont[l].firstChild.nodeValue;
			nTr.appendChild(nTd);
		}
		nTr.onclick = markLine;
		return nTr;
	}

	function getSingleContentLine(nCont, strId)
	{
		var nTr = document.createElement("tr");
		var nTd = document.createElement("td");
		setCls(nTd, "list");
		nTd.id = strId;
		if (nCont.hasChildNodes()) nTd.innerHTML = nCont.firstChild.nodeValue;
		nTr.appendChild(nTd);
		nTr.onclick = markLine;
		return nTr;
	}
	// markLine war vorher hier drin
}
function markLine(objEvt)
{
	if (navigator.appName == "Microsoft Internet Explorer")
		objEvt = window.event.srcElement;
	//else
	//	objEvt = objEvt.target;
	if (objEvt.nodeName != "TR" && objEvt.nodeName != "TD")
	{
		if (navigator.appName == "Microsoft Internet Explorer")
			objEvt = window.event.srcElement;
		else
			objEvt = objEvt.target;
	}
	while (objEvt.nodeName != "TR")
		objEvt = objEvt.parentNode;
	var nTr = document.getElementById("selListItem");
	if (nTr != null)
	{
		for (var i=0; i<nTr.childNodes.length; i++)
		{
			nTr.childNodes[i].style.backgroundColor = "white";
		}
		nTr.id = null;
	}
	objEvt.id = "selListItem";
	for (var i=0; i<objEvt.childNodes.length; i++)
	{
		objEvt.childNodes[i].style.backgroundColor = "#d6d7ff";
	}
}
function refCleanXmlStr(str)
{
	str = str.replace(/:AND:/g, "&");
	str = str.replace(/:KL:/g, "<");
	str = str.replace(/:GR:/g, ">");
	//str = str.replace(":GR:", "&lt;");
	//str = str.replace(":KL:", "&gt;");
	//alert(str);
	return str;
}
function selTrByTd(selTd, selClass, unselClass)
{
	var nTable = selTd.parentNode.parentNode.parentNode;
	var nTr = selTd.parentNode;
	var nTds = nTable.getElementsByTagName("td");
	var nTds2 = nTr.getElementsByTagName("td");
	for (var i=0; i<nTds.length; i++)
	{
		setCls(nTds[i], unselClass);
	}
	for (var i=0; i<nTds2.length; i++)
	{
		setCls(nTds2[i], selClass);
	}	
}
function clipStr(str, len)
{
	var ret = str;
	if (str.length > len) ret = str.substring(0, len) + "...";
	return ret;
}
function setCls(obj, cls)
{
	obj.setAttribute("class", cls);
	obj.setAttribute("className", cls);
}
function transferFrm(lst, strTxtId)
{
	var txt = document.getElementById(strTxtId);
	txt.value = lst.options[lst.selectedIndex].text;
	txt.focus();
}
function getImg(pic, str)
{
	var img = document.createElement("img");
	img.setAttribute("src", pic);
	if (str != "") setToolTip(img, str);
	return img;
}
function setToolTip(n, str)
{
	n.setAttribute("alt", str);
	n.setAttribute("title", str);
	n.style.cursor = "pointer";
}
function Visibility(arIds)
{
	var allVisible = true;
	this.setVisible = setVisible;
	this.isVisible = isVisible;
	
	function isVisible()
	{ return allVisible; }
	
	function setVisible(blnVisible)
	{
		allVisible = blnVisible;
		
		for (var i=0; i<arIds.length; i++)
		{
			var n = document.getElementById(arIds[i]);
			if (n != null)
			{
				if (navigator.appName == "Microsoft Internet Explorer")
				{
					if (blnVisible)
						n.style.display = "block";
					else
						n.style.display = "none";
				}
				else
				{
					if (!blnVisible)
						n.style.display = "none";
					else
					{
						var strType = n.nodeName;
						switch (strType)
						{
							case "TR":
								n.style.display = "table-row";
								break;
							case "TD":
								n.style.display = "table-cell";
								break;
							case "TABLE":
								n.style.display = "table";
								break;
							default:
								n.style.display = "block";
						}
					}
				}
			}
		}
	}
}
function valiDate( fld, blnAktuell )
{
    var strDate = parseDate(fld.value);
	var strOK = "";
	var dv = new dateValidator();
	
	if (strDate != "")
	{
        dv.setRegexp( /^(\d{1,2})\.(\d{1,2})\.(\d{2,4})$/ );
		if (!dv.isPatternValide( strDate )) strOK += "falsches Datumsformat!\n";
		if (!dv.isValideDMY( strDate )) strOK += "falsches Datum!\n";

		if(dv._date)
		{
			if (!dv.isInRange( dv._date )) strOK += "falscher Datumsbereich!\n";
			if (!dv.isInFuture( dv._date ) && blnAktuell) strOK += "Das Datum darf nicht in der Vergangenheit liegen!\n";
		}
	}
	if (strOK != "")
	{
		alert("Bei der Datumseingabe wurden Fehler festgestellt:\n\n" + strOK);
		fld.value = "";
		fld.focus();
	}
	else
		fld.value = strDate;
	return strOK == "";
}
function dateValidator()
{
    this.startYear = 1900;
    this.endYear = 2035;
    // das aktuelle Datum
    this.cDate = new Date();
    this.beginGregorian = new Date(1582, 9, 15);
    this.endJulian = new Date(1582, 9, 4);

    this.regExp = /^(\d{1,2})\.(\d{1,2})\.(\d{2,4})$/;

    // zugehoerige Methoden
    this.setRegexp = setRegexp;
    this.setYearRange = setYearRange;
    this.isPatternValide = isPatternValide;
    this.isValideDMY = isValideDMY;
    this.isInRange = isInRange;
    this.isLeapYear = isLeapYear;
    this.isInFuture = isInFuture;
    function isPatternValide ( strDate )
    {
      var match = this.regExp.exec(strDate);
      return (match)?true:false;
    }
    function isValideDMY( strDate )
    {
        var regExp = /^(\d{1,2})\.(\d{1,2})\.(\d{2,4})$/;
        var match  = regExp.exec(strDate);
        var ret    = false;

        if ( match )
        {
            year = parseInt(match[3], 10);
            if(year < 100){ year += 2000; }
            month = parseInt(match[2], 10);
            day = parseInt(match[1], 10);
            this._date = new Date(year, month-1, day);
            tmpYear = this._date.getFullYear ?  this._date.getFullYear() : this._date.getYear();
            tmpYear = (tmpYear < 100)?(tmpYear+1900):(tmpYear);
            if(year == tmpYear && month == this._date.getMonth() + 1 && day == this._date.getDate())
            {
                /* gibt es im Gregorianischen Kalender nicht */
                if(this._date.getTime() >= this.endJulian.getTime() && this._date.getTime() < this.beginGregorian.getTime())
                {
                    this._date = null;
                }
                else
                {
                    ret = true;
                }
            }
        }
        this._date = null;
        return ret;
    }
    function isInRange (date)
    {
        if(!date) return false;
        dBegin = new Date(this.startYear, 0, 1);
        dEnd = new Date(this.endYear+1, 0, 1);
        if(dBegin.getTime() <= date.getTime() && date.getTime() < dEnd.getTime())
        {
            return true;
        }
        return false;
    }
    function isInFuture(date)
    {
      return ( date.getTime() > this.cDate.getTime() );
    }
    function isLeapYear(year)
    {
      return (year%4 == 0) && ((year%100 != 0) || (year%400 == 0));
    }
    function setYearRange( start, end )
    {
      this.startYear = parseInt(start);
      this.endYear = parseInt(end);
    }
    function setRegexp( re )
    {
      this.regExp = re;
    }
}
function parseDate(strDate)
{
	var ret = "";
	for (i=0; i<strDate.length; i++)
	{
		var subs = strDate.substring(i,i+1);
		if (subs == "0" || subs == "1" || subs == "2" || subs == "3" || subs == "4" || subs == "5" ||
		    subs == "6" || subs == "7" || subs == "8" || subs == "9") ret += subs;
	}
	strDate = ret;
	if (strDate.indexOf(".") < 0 && strDate.length == 8)
	{
		var t,m,j;
		t = strDate.substring(0, 2);
		m = strDate.substring(2, 4);
		j = strDate.substring(4, 8);
		ret = t + "." + m + "." + j;
	}
    if (strDate.indexOf(".") < 0 && strDate.length == 6)
    {
        var t,m,j,j2;
        j2 = "20";
        t  = strDate.substring(0, 2);
        m  = strDate.substring(2, 4);
        j  = strDate.substring(4, 6);
        if (parseInt(j,10) > 30) j2 = "19";
        ret = t + "." + m + "." + j2 + j;
    }
	return ret;
}

function EditLine(params, url)
{
	var arBackup;
	var nImgNow;
	
	this.setLine = setLine;
	this.removeLine = removeLine;
	
	function setLine(strTrId, nImg)
	{
		nImgNow = nImg;
		var nImgNext = nImgNow.nextSibling;
		nImgNext.style.display = "inline";
		nImgNow.style.display = "none";
		var nTr = document.getElementById(strTrId);
		var strTrId = strTrId.split("_")[1];
		var nTds = nTr.childNodes;
		arBackup = new Array(nTds.length);
		for (var i=0; i<params.length; i++)
		{
			var nTd = nTds[i];
			var param = params[i];
			var strInput = param[0];
			var strName = param[1];
			var strType = param[2];
			var strMax = param[3];
			var strSize = param[4];
			var strFld = param[5];
			var nInput;
			if (strType == "text") nInput = new TextField(strName, strSize, strMax, getValue(strFld, strTrId)).getObject();
			if (strType == "checkbox")
			{
				var strVal = getValue(strFld, strTrId);
				nInput = new CheckBox(strName, strVal == 1).getObject();
			}
			if (nTd.hasChildNodes())
			{
				arBackup[i] = nTd.firstChild.NodeValue;
				nTd.removeChild(nTd.firstChild);
			}
			else
				arBackup[i] = "";
			nTd.appendChild(nInput);
		}
	}
	
	function getValue(fld, val)
	{
		var ret = "";
		var cmd = "bsgid=" + bsgid;
		cmd += "&fld=" + fld;
		cmd += "&val=" + val;
		cmd += "&cmd=get"
		var ajax = new AjaxRequest(url, cmd, false).getRequest();
		var xml = ajax.responseXML;
		if (!xml.hasChildNodes())
			alert(xml.responseText);
		else
		{
			var newVal = xml.getElementsByTagName("val")[0];
			if (newVal.hasChildNodes())	ret = newVal.firstChild.nodeValue;
		}
		return ret;
	}
	
	function removeLine(withValues)
	{
		for (var i=0; i<params.length; i++)
		{
			var param = params[i];
			var strInput = param[0];
			var strName = param[1];
			var strType = param[2];
			var nInput = document.getElementById(strName);
			var nParent = nInput.parentNode;
			var strVal;
			if (strType == "text") strVal = nInput.value;
			if (strType == "checkbox")
			{
				if (nInput.checked) strVal = "ja"; else strVal = "nein";
			}
			nParent.removeChild(nInput);
			nInput = null;
			if (withValues)
			{
				strVal = clipStr(strVal, 16);
				nParent.appendChild(document.createTextNode(strVal));
			}
		}
		nImgNow.style.display = "inline"		;
		nImgNow.nextSibling.style.display = "none";
	}
}