/*-- spam protection --*/

	function getAdr(prefix, postfix, text) {
		document.write('<a href="mailto:' + prefix + '@' + postfix + '">' + (text ? text.replace(/&quot;/g, '"').replace(/%EMAIL%/, prefix + '@' + postfix) : prefix + '@' + postfix) + '</a>');
	}

/*-- swap image --*/

	function swapImage(element, newimage) {
		var oldsrc = element.src;
		element.src = newimage;
		if (!element.onmouseout) {
			element.onmouseout = function () {
				swapImage(this, oldsrc);
			}
		}
	}

/*-- popup --*/

	function popup(url, width, height) {
		window.open(url, 'Popup', 'width=' + width + ', height=' + height + ',scrollbars=1');
	    return false;
	}

/*-- getElementsByClassName --*/

	function getElementsByClassName(strClass, strTag, objContElm) {
		strTag = strTag || "*";
		objContElm = objContElm || document;
		var objColl = objContElm.getElementsByTagName(strTag);
		if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
		var arr = new Array();
		var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
		var arrClass = strClass.split(delim);
		for (var i = 0, j = objColl.length; i < j; i++) {
			var arrObjClass = objColl[i].className.split(' ');
			if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
			var c = 0;
			comparisonLoop:
			for (var k = 0, l = arrObjClass.length; k < l; k++) {
				for (var m = 0, n = arrClass.length; m < n; m++) {
					if (arrClass[m] == arrObjClass[k]) c++;
					if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
						arr.push(objColl[i]);
						break comparisonLoop;
					}
				}
			}
		}
		return arr;
	}

/*-- validform --*/

	function init_forms() {
		var content = document.getElementById('content');
		if(content) {
			var forms = content.getElementsByTagName('form');
			for(f=0; f<forms.length; f++) {
				init_form(forms[f]);
			}
		}
		var email_empfaenger;
		var inputs = document.getElementsByTagName('*');
		for(i=0; i < inputs.length; i++) {
			if(inputs[i].name) {
				if(inputs[i].name == 'email_empfaenger') {
					email_empfaenger = inputs[i];
				}
			}
		}
		if(email_empfaenger) {
			if(email_empfaenger.tagName.toLowerCase() == 'select') {
				var options = email_empfaenger.getElementsByTagName('option');
				for(o=0; o<options.length; o++) {
					options[o].value = options[o].value.replace(/\+at\+/g, '@');
				}
				email_empfaenger.onchange = function() {
					var options = this.getElementsByTagName('option');
					for(o=1; o<options.length; o++) {
						if(options[o].selected) {
							var location_data = $($('.form_locations>div')[o-1]).find('span');
							var form = this.parentNode;
							while(form.tagName.toLowerCase() != 'form') {
								form = form.parentNode;
							}
							for(i=0; i<location_data.length; i++) {
								if(location_data[i].title != 'location_id') {
									form['to_' + location_data[i].title].value = location_data[i].innerHTML;
								}
							}
						}
					}
				}
				email_empfaenger.onchange();
			}
			else {
				email_empfaenger.value = email_empfaenger.value.replace(/\+at\+/g, '@');
			}
		}

		// check for form_application
		var url = document.location.href;
		if(url.indexOf('job_title') > -1 && document.getElementById('form_application')) {
			var spliturl = url.split('?');
			var splitparams = spliturl[1].split('&');
			var params = new Object();
			for(i=0; i<splitparams.length; i++) {
				var splitvalues = splitparams[i].split('=');
				params[splitvalues[0]] = splitvalues[1];
			}

			if(params['job_title'] && params['location_id'] && $("select[name='email_empfaenger']").length > 0) {
				$('.job input')[0].value = params['job_title'];
				$('.form_locations>div').each(function(i) {
					$(this).find('span').each(function(s) {
						if(this.title == 'location_id' && this.innerHTML == params['location_id']) {
							$("select[name='email_empfaenger'] option")[i+1].selected = true;
							$("select[name='email_empfaenger']")[0].disabled = true;
						}
					});
				});
				$("select[name='email_empfaenger']")[0].onchange();
			}
		}
	}

	function init_form(form) {
		var required = getElementsByClassName('required', '*', form);
		for(i=0; i<required.length; i++) {
			required[i].oldformonblur = required[i].onblur;
			required[i].onblur = function() {
				validate(this);
				if(this.oldformonblur)
					this.oldformonblur();
			}
		}
		form.onsubmit = function() {
			var doSubmit = true;
			var list = new Array();
			var required = getElementsByClassName('required', '*', this);
			for(v=0; v<required.length; v++) {
				if(! validate(required[v])) {
					doSubmit = false;
					list.push(required[v]);
				}
			}
			var checkboxeles = getElementsByClassName('required_checkbox', '*', this);
			for(i=0; i<checkboxeles.length; i++) {
				valid = false;
				checkboxes = checkboxeles[i].getElementsByTagName('input');
				for(c=0; c<checkboxes.length; c++) {
					if(checkboxes[c].type == 'checkbox') {
						checkboxeles[i].name = checkboxes[c].name; // we need the name to check for custom_required_texts
						if(checkboxes[c].checked)
							valid = true;
					}
				}
	
				if(valid == false) {
					doSubmit = false;
					set_msg(checkboxeles[i], FORM_valid_checkbox);
					list.push(checkboxeles[i]);
				} 
				else {
					clear_msg(checkboxeles[i]);
				}
			}
			var radioeles = getElementsByClassName('required_radio', '*', this);
			for(i=0; i<radioeles.length; i++) {
				valid = false;
				radios = radioeles[i].getElementsByTagName('input');
				for(c=0; c<radios.length; c++) {
					if(radios[c].type == 'radio') {
						radioeles[i].name = radios[c].name; // we need the name to check for custom_required_texts
						if(radios[c].checked)
							valid = true;
					}
				}
	
				if(valid == false) {
					doSubmit = false;
					set_msg(radioeles[i], FORM_valid_radio);
					list.push(radioeles[i]);
				} 
				else {
					clear_msg(radioeles[i]);
				}
			}

			if(doSubmit == false) {
				show_error(list);
				return doSubmit;
			}
		}
	}

	function validate(el) {
		var valid = true;
		clear_msg(el);
		switch(el.type) {
			case 'text':
			case 'textarea':
			case 'select-one':
				if(el.value != '') {
					if(el.className.indexOf('email') > -1) {
						var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
						if(el.value.toUpperCase().match(regEmail)){
							valid = true;
						}
						else {
							valid = false;
							set_msg(el, FORM_valid_email);
						}
					}
					if(el.className.indexOf('number') > -1) {
						if(el.value == Math.round(el.value)) {
							valid = true;
						}
						else {
							valid = false;
							set_msg(el, FORM_valid_postal_code);
						}
					}
				}
				else {
					valid = false;
					set_msg(el);
				}
				break;
		}
	
		return valid;
	}

	function set_msg(el, msg) {
		if(msg == undefined) {
			msg = el.parentNode.getElementsByTagName('span')[0].innerHTML.replace(' *', '') + ' ' + FORM_valid_required;
		}

		if(custom_required_text[el.name])
			msg = custom_required_text[el.name];

		el.errorMessage = msg;
		if(el.parentNode.className.indexOf('error') == -1)
			el.parentNode.className += ' error';
	}

	function clear_msg(el) {
		var classes = el.parentNode.className.split(' ');
		for(e=0; e<classes.length; e++) {
			if(classes[e].indexOf('error') > -1)
				classes[e] = "";
		}
		el.parentNode.className = classes.join(' ');
		el.errorMessage = "";
	}

	function show_error(list) {
		errorlist = '<ul>';
		for(i=0; i<list.length; i++) {
			el = list[i];
			if(el.errorMessage != undefined)
				errorMessage = el.errorMessage;
			else
				var errorMessage = el.parentNode.getElementsByTagName('span')[0].innerHTML.replace(' *', '') + ' ' + FORM_valid_required;
	
			errorlist += '<li>' + errorMessage + '</li>';
		};
		errorlist += '</ul>';
	
		var validationMessage = document.getElementById('validationMessage');
		validationMessage.innerHTML = '<h2 title="' + FORM_valid_error + '">' + FORM_valid_error + '</h2>' + errorlist;
		document.location.href = '#validationMessage';
	}

	$(function() {
		init_forms();
	});

/*-- filter/sort selection --*/

	// function for the special objects: Veranstaltungen, Presse, ...

	function init_selection() {
		var form_selections_obj = document.getElementById('form_selections');
		if (form_selections_obj) {
			form_selections_obj.getElementsByTagName('button')[0].style.display = 'none';
			selects = form_selections_obj.getElementsByTagName('select');
			for(i=0; i<selects.length; i++) {
				selects[i].onchange = form_selections_change;
			}
		}
	}
	
	function form_selections_change() {
		urlparm = '?';
		selects = document.getElementById('form_selections').getElementsByTagName('select');
		for(i=0; i<selects.length; i++) {
			urlparm += selects[i].name + '=' + selects[i].value;
			if (i!=selects.length-1) {
				urlparm += '&';
			}
		};
		location.href=urlparm;
	}

	$(function() {
		init_selection();
	});

/*-- display elements onmouseover --*/

	var hover_links = new Array();
	hover_links.push(new Array('fontchange', 'fontsize'));
	// To add more links, follow the same pattern
	// hoverLinks.push(new Array('linkid', 'elementid'));

	function init_show_elements() {
		for (i = 0; i < hover_links.length; i++) {
			var link = document.getElementById(hover_links[i][0]);
			if (link) {
				link.el = document.getElementById(hover_links[i][1]);
				if (link.el) {
					link.onmouseover = function() {
						this.el.style.display = 'block';
					};
					link.onmouseout = function() {
						this.el.style.display = 'none';
					}
					link.onclick = function() {
						return false;
					}
				}
			}
		}
	}

	$(function() {
		init_show_elements();
	});


	
	/*-- Adjust Iframe size --*/
	function adjustContentFrameSize() {
	var width = $('#content').width();
    $('iframe').css({'height':$('#content').height()-10+'px', 'width':width+40+'px'});
    //$("#content").css({'overflow':'hidden'});
  }
  
  $(function() {
      adjustContentFrameSize();
      window.onresize = function() {adjustContentFrameSize();}
  });
  
  /*-- Add Class to CSS3 Selectors --*/
  
  $(document).ready(function() { 
    
    $("#startpagenav li:nth-child(1)").addClass("first_children");
    $("#startpagenav li:nth-child(5)").addClass("first_children");
    $("#startpagenav li:nth-child(2)").addClass("second_children");
    $("#startpagenav li:nth-child(6)").addClass("second_children");
    $("#startpagenav li:nth-child(3)").addClass("third_children");
    $("#startpagenav li:nth-child(7)").addClass("third_children");
    $("#startpagenav li:nth-child(4)").addClass("last_children");
    $("#startpagenav li:nth-child(8)").addClass("last_children");
    $("#startpagenav li:nth-child(-n+4)").addClass("first_four_children");
    $("#startpagenav li:nth-child(5)").addClass("last_four_children");
    $("#startpagenav li:nth-child(6)").addClass("last_four_children");
    $("#startpagenav li:nth-child(7)").addClass("last_four_children");
    $("#startpagenav li:nth-child(8)").addClass("last_four_children");
  });


