//	www/dhtml.js
//	upd 13.07.2005 mh

var jslib;if(!jslib){jslib=[];}
var libDhtml;if(!libDhtml){libDhtml=[];}

function dom_obj(id){
	//	abbreviated reference to element in document heirarchy by ID
	return document.getElementById(id)
}

function dom_form(id){
	//	generate pointer to DOM form object
	//	all forms must have ID
	//	all fields must have ID and name
	return dom_obj(id)
}

libDhtml.eventlistenerAdd=function(evType,fn){
	if(this.addEventListener) {		//	W3C
		this.addEventListener(evType,fn,false);
	}else if(this.attachEvent){		//	IE
		this.attachEvent("on"+evType,fn);
	}else{
		var originalHandler=this["on"+evType];
		if (originalHandler){
			this["on"+evType]=function(e){originalHandler(e);fn(e);};
		}else{
			this["on"+evType]=fn;
		}
	}
}
libDhtml.eventlistenerRemove=function(evType,fn){
	if(this.removeEventListener){	//	W3C
		this.removeEventListener(evType,fn,false);
	}else if(this.detachEvent){		//	IE
		var r = this.detachEvent("on"+evType, fn);
	}else{
		alert("Handler could not be removed");
	}
}

libDhtml.domElementPlus=function(childType,oParent){
	//	create new DOM object
	//	append to object oParent if oParent exists
	//	buggy with textareas in gecko browsers ???

	if((typeof childType).toLowerCase()!="string"){alert("function domElementPlus() :invalid element type");return false}
	this.obj=document.createElement(childType?childType:"DIV")
	if(oParent){oParent.appendChild(this.obj)}
	this.obj.eventlistenerAdd=libDhtml.eventlistenerAdd;
	this.obj.eventlistenerRemove=libDhtml.eventlistenerRemove;
	if(document.getElementById){	//	IE5+ und Gecko
		this.obj.css=this.obj.style;
		this.obj.showMe=function(){this.css.visibility="visible"}
		this.obj.hideMe=function(){this.css.visibility="hidden"}
	}
 	else if(document.layers){		//	NN4
		if(!document.layers[eID]){return false}
 		this.obj.css=document.layers[eID]
		this.obj.showMe=function(){this.css.visibility="show"}
		this.obj.hideMe=function(){this.css.visibility="hide"}
 	}
 	else if(document.all){			//	IE4
		if(!document.all[eID]){return false}
	 	this.obj.css=document.all[eID].style
		this.obj.showMe=function(){this.css.visibility="visible"}
		this.obj.hideMe=function(){this.css.visibility="hidden"}
	}
	return this.obj;
}

function dom_makefield(formobj,sourcefield,fieldvalue){
	//	append a new field to a form object
	newField=libDhtml.domElementPlus((!sourcefield||(sourcefield.type.toLowerCase()!="textarea"))?"INPUT":"TEXTAREA",formobj)
	newField.setAttribute("name",sourcefield.name)
	newField.setAttribute("title",sourcefield.title)
	newField.value=fieldvalue

	//newField.setAttribute("id",sourcefield.id)	//	can't have fields with duplicate IDs!!!!
	//newField.setAttribute("value",fieldvalue)		//	value not applied to textareas in moz/nn7/saf
	//newField.onclick=function(){alert(this.name)}	//	for debugging only

	return newField
}

function dom_removeChildren(oElement){
	//	remove all child elements from a DOM object
	//	BUG: does not clear form.elements array in safari
	while(oElement.hasChildNodes()){
		oElement.removeChild(oElement.firstChild)
	}
}

function eventTarget(e){
	e=getEvent(e);
	var targ=(e.target)?e.target:e.srcElement;
	if(!targ){targ=e;}
	if(targ.nodeType===3){targ=targ.parentNode;} // defeat Safari bug
	return targ;
}

function getEvent(e){
	if(!e){var e=window.event}
	return e
}

function dyn_form(formID,formMethod,formEncType,domParent){
	//	generate dynamic form
	if(!formID){formID="form"+Math.floor(Math.random()*999999999)}
	if(!formMethod){formMethod="post"}
	if(!formEncType){formEncType="application/x-www-form-urlencoded"}
	if(!domParent){domParent=document.getElementsByTagName("body")[0]}
	newForm=libDhtml.domElementPlus("FORM",domParent)
	newForm.id=formID
	newForm.className="ghost";	//	14.7.2005 mh
	newForm.method=formMethod
	newForm.encoding=formEncType
	return newForm
}

setRemoteImages=function(layerName,hostname){
	if(!layerName||!hostname||!document.getElementById||!document.getElementsByTagName){return false}
	var cImages=document.getElementById(layerName).getElementsByTagName("img");
	i=0;
	while(cImages[i]){
		cImages[i].src=replaceAll(cImages[i].src,top.location.hostname,hostname)
		i++;
	}
}

var oGhostForm;
function write2ghostform(cForms){
	if(oGhostForm){oGhostForm=false}
	oGhostForm=dyn_form()
	oGhostForm.className="ghost"
	if(oGhostForm){
		cFormFields=new Array()
		dom_removeChildren(oGhostForm)
		if(!cForms){cForms=document.getElementsByTagName("form")}
		for(fL=0;fL<cForms.length;fL++){
			if(cForms[fL]!=oGhostForm){
				if((cForms[fL].elements["_write2ghostform"])&&(cForms[fL].elements["_write2ghostform"].value=="true")){
					for(fEL=0;fEL<cForms[fL].elements.length;fEL++){
						srcFld=cForms[fL].elements[fEL]
						if(srcFld.name.indexOf("_")!=0){

							//	radio button or checkbox (value if selected)
							if((srcFld.type=="radio")||(srcFld.type=="checkbox")){
								if(srcFld.checked){dom_makefield(oGhostForm,srcFld,srcFld.value)}

							//	standard form field (value)
							}else if((srcFld.value)||(srcFld.value=="")){
								dom_makefield(oGhostForm,srcFld,srcFld.value)

							//	set of checkboxes (array)
							}else if(srcFld.type=="checkbox"){
								if(!cFormFields[srcFld.name]){cFormFields[srcFld.name]=new collection()}
								cFormFields[srcFld.name]["counter"]++
								srcFld=srcFld[cFormFields[srcFld.name]["counter"]]
								if(srcFld.checked){dom_makefield(oGhostForm,srcFld,srcFld.value)}

							//	multiple non-checkbox fields with the same name
							}else if(srcFld.length){
								if(!oGhostForm.elements[srcFld.name]){ // only add the set of fields to the ghost form once
									for(mfc=0;mfc<srcFld.length;mfc++){
										dom_makefield(oGhostForm,srcFld,srcFld[mfc].value)
									}
								}
							}else{
								alert("Fehler: Feld "+srcFld.name+" ("+srcFld.type+") könnte nicht gespeichert werden.\nKontaktieren Sie bitte SOFORT den System Administrator.")
							}
						}
					}
				}
			}
		}
		cFormFields=new Array()	//	empty the array
	}else{
		alert("oGhostForm nicht definiert")
	}
	return false
}


gatherNamedElements=function(){
	//	document.getElementsByName doesn't work in ie/win
	//	from function initNamedObj() (publisher)
	//	create grouped arrays of named elements
	//	cr 06.2005 ob
	if(!document.getElementsByName||document.all){
		aAll=document.all;
		for(n=0;n<aAll.length;n++){
			if(aAll[n].name){
				objName=aAll[n].name
				if(!aNamedObj[objName]){
					aNamedObj[objName]=new Array;
				}
				//aNamedObj[objName].push(aAll[n]);	//	not supported by ie mac
				aNamedObj[objName][aNamedObj[objName].length]=aAll[n];
			}
		}
	}
}
libDhtml.gatherNamedElements=gatherNamedElements;

aNamedElements=function(objName){
	//aObj=new Array;
	if(!document.getElementsByName||document.all){
		return(aNamedObj[objName]?aNamedObj[objName]:false);
	}else{
		return(document.getElementsByName(objName));
	}
}

function fnGetElementsByClassName(oElm, strTagName, oClassNames){
	//	http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
	//	getElementsByClassName(document, "a", "info-links");
	//	getElementsByClassName(document.getElementById("container"), "div", ["col", "left"]);
	var arrElements = (strTagName == "*" && document.all)? document.all : 
    oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object"){
        for(var i=0; i<oClassNames.length; i++){
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + 
            oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else{
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + 
        oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++){
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++){
            if(!arrRegExpClassNames[k].test(oElement.className)){
                bMatchesAll = false;
                break;                      
            }
        }
        if(bMatchesAll){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

setOpacity=function(obj,opacity){
	//	mh 10.11.2005
	if(obj&&(opacity||opacity===0)){
		opacity = (opacity == 100)?99.999:opacity;
		//obj.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+opacity+")";
		obj.style.filter = "alpha(opacity:"+opacity+")";	// IE/Win
		obj.style.KHTMLOpacity = opacity/100;				// Safari<1.2, Konqueror
		obj.style.MozOpacity = opacity/100;					// Older Mozilla and Firefox
		obj.style.opacity = opacity/100;					// Safari 1.2, newer Firefox and Mozilla, CSS3
	}
}
libDhtml.setOpacity=setOpacity;
