//----------------------------
// basisfunktionen
//----------------------------

function hidejsfallback(classname){
	var hidearray = getElementsByClass(classname);
	for (var i=0;i<hidearray.length;i++){
		hidearray[i].className = 'hideme';
	}	
}

//maxlength for textareas
function setMaxLength(max, id, textprev, textnext){		
	var counter = document.createElement('div');
	var maxLength = max;
	counter.className = 'counter';
	
	if(!maxLength){return;}
	
	if(!textprev){
		var htmltextprev='Anzahl der Zeichen, die Sie noch eingeben k&ouml;nnen:';
	} else {
		var htmltextprev = textprev;
	}
	
	if(!textnext){
		var htmltextnext='';
	} else {
		var htmltextnext = textnext;
	}
	
	if (!id) {
		var x = document.getElementsByTagName('textarea');
		for (var i=0;i<x.length;i++){
			setMaxLength_Loop(counter, maxLength, htmltextprev, htmltextnext, x[i]);
		}
	} else {
		var x = document.getElementById(id);	
		setMaxLength_Loop(counter, maxLength, htmltextprev, htmltextnext, x);
	}

}
//part of setMaxLength2
function setMaxLength_Loop(counter, maxLength, htmltextprev, htmltextnext, x ){
	var counterClone = counter.cloneNode(true);
	counterClone.relatedElement = x;
	counterClone.innerHTML = htmltextprev + ' <span aria-live="polite">0</span> ' + htmltextnext;
	x.parentNode.insertBefore(counterClone,x.nextSibling);
	x.relatedElement = counterClone.getElementsByTagName('span')[0];
	x.onkeyup = x.onchange = function(){
		var currentLength = this.value.length;
		if (currentLength > maxLength){	
			this.value=this.value.slice(0, maxLength);		
		}
		if (currentLength < maxLength){
			this.relatedElement.firstChild.nodeValue = maxLength-currentLength;
		} else { 
			this.relatedElement.firstChild.nodeValue = 0;
		}
	};
	x.onkeyup();	
}





// begrenzung von zeichen in einer textarea auf maximal-laenge
function checkMax(field, max) {
	var fieldLength = field.value.length;
	if (fieldLength>max) {
		field.value = field.value.substr(0,max);
		alert('Es sind max. '+max+' Zeichen erlaubt.');
	}
}

// begrenzung von zeichen in einer textarea auf maximal-laenge unter einbeziehung eines textes aus einem pulldown
function checkMaxMulti(f, fieldTXT, fieldPD, max) {
	var FieldTXT = f.elements[fieldTXT];
	var FieldPD = f.elements[fieldPD];
	var txtFieldLength = FieldTXT.value.length;
	var pdFieldLength = FieldPD.options[FieldPD.options.selectedIndex].value.length+1;
	if (pdFieldLength+txtFieldLength>max) {
		FieldTXT.value = FieldTXT.value.substr(0,max-pdFieldLength);
		alert('Es sind max. '+max+' Zeichen erlaubt.');
	}
}

// url parsen
function parseHost(u) {
	uw = u.replace(/:\/\//, "/");
	parts = uw.split("/");
	return(parts[1]); 
}

// neues fenster
function newWin(h,t) {
	 var newwin=window.open(h, t); 
	 newwin.focus();
}

// dsi-tracking
function trackIntern(l) {
	self.location.href=rel2rootweb+'div/linktrack.php'+insertsid+'linkurl='+escape(l.href);
}

// dom-object
function oid(n) {
    return (document.getElementById?(document.getElementById(n)?document.getElementById(n):false):(document.all?(document.all(n)?document.all(n):false):false));
}
function otn(n) {
    return (document.getElementsByTagName?(document.getElementsByTagName(n)?document.getElementsByTagName(n):false):(document.all?(document.all(n)?document.all(n):false):false));
}

// timeout-cleaner
function cto(to){
	if (to!=false) window.clearTimeout(to);
}


function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function addEvent(obj, typ, fun) {
	if (obj.addEventListener) {
		obj.addEventListener( typ, fun, false); 
		return true; 
	} else if (obj.attachEvent) {
		return obj.attachEvent("on"+typ, fun);
	}
	return false;
}



//----------------------------
// ARIA-STUFF
//----------------------------

function getAttributeValue(o,a) {
	if (o.getAttribute(a)) {
		return o.getAttribute(a);
	} else if (o.attributes[a]) {
		return o.attributes[a].value;
	}
}
function setAttributeValue(o,a,v) {
	if (o.setAttribute(a,v,0)) {
		o.setAttribute(a,v,0);
	} else {
		o.attributes[a].value=v;
	}
}

function ariaSetRequired() {
	var allLabels = otn("label");
	for (l=0; l<allLabels.length; l++) {
		var labelText = allLabels[l].innerHTML;
		if (labelText.indexOf('Pflichtfeld')>0) {
			var fieldId = getAttributeValue(allLabels[l], "for");
			//alert(fieldId);
			if (fieldId) setAttributeValue(oid(fieldId), "aria-required", "true");
		}
	}
}


//----------------------------
// main init/deinit
//----------------------------

init = function() {
	ariaSetRequired();
}
deinit = function() {
}
window.onunload=deinit;
window.onload=init;



//----------------------------
// teilnahme-bereich-supporters
//----------------------------

function contestJump(targ, anch) {
	/*if (contestchanged=='yes') {
		var abfrage = confirm('Zum Speichern Ihrer Änderungen klicken Sie bitte auf "OK", um ohne Speichern weiterzugehen drücken Sie "Abbrechen"');
	}*/
	if (contestpage=='') return true; //  || abfrage==false
	document.contestform.pagejump.value=targ;
	document.contestform.pageanchor.value=anch;
	document.contestform.submit();
	return false;
}
function contestHelpInit(hid) {
	oid(hid).style.display='none';
	oid(hid+'-toggle').style.display='inline';
	if (oid(hid+'-close')) oid(hid+'-close').style.display='block';
}
var contestHelpAct = '';
function contestHelpShow(hid) {
	var contestHelpActBak = contestHelpAct;
	if (contestHelpAct) {
		oid(contestHelpAct).style.display='none';
		contestHelpAct = '';
	}
	if (contestHelpActBak!=hid) {
		oid(hid).className='contesthelp';
		oid(hid).style.display='block';
		contestHelpAct = hid;
	}
}
function contestHelpUnShow(hid) {
	oid((hid)).style.display='none';
	contestHelpAct = '';
}


//----------------------------
// fullimgtpl show-hide fct 
//----------------------------

var tplHelpAct = '';
function startElement(id){
	document.getElementById('fulltext-'+id).style.display='none';
	document.getElementById('teaser-'+id).style.display='block';
}
function toggleElement(id){
	var tplHelpActBak = tplHelpAct;
	if (tplHelpAct) {
		document.getElementById('fulltext-'+tplHelpAct).style.display='none';
		document.getElementById('teaser-'+tplHelpAct).style.display='block';
		tplHelpAct = '';
	}
	if (tplHelpActBak!=id) {
		document.getElementById('fulltext-'+id).style.display='block';
		document.getElementById('teaser-'+id).style.display='none';
		tplHelpAct = id;
	}
}




