// Typo3 Stuff
function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}
return String.fromCharCode(n);}
function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}
return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,-1);}

function showFormError(text) {
	if($("p.error").size() == 0) {
		$("div.tx_powermail_pi1_fieldwrap_html_submit").before('<p class="error">' + text + '</p>');
		$("p.error").slideDown();
	}
}

function messages() {
	this.lang = null;
	var meta = document.getElementsByTagName("meta");
	for (i = 0; i < meta.length; i++) {
		if (meta[i].getAttribute("name") == "language") {
			this.lang = meta[i].getAttribute("content");
		}
	}

	this.error = new Object();

	switch (this.lang) {
		case "de":
			this.error["required"] = "Bitte füllen Sie alle Pflichtfelder aus.";
			break;
		default:
			this.error["required"] = "Please fill in all required fields.";
			break;
	}
}
var messages = new messages();

function cycleDivs(id, speed) {
	var currentObject = $(id + ' div:visible:first');
	var nextObject = currentObject.next().length == 1 ? currentObject.next() : $(id + ' div:first-child');
	if (currentObject != nextObject) {
		nextObject.css({
			zIndex: 110,
			opacity: 0,
			display: 'block'
		}).fadeTo(speed, '1', function(){
			currentObject.css({
				zIndex: 10,
				display: 'none'
			})
			$(this).css({
				zIndex: 100
			})
		});
	}
	return false;
};

// jQuery Init
$(document).ready(function() {
	/* INIT */
	$('body').removeClass('nojs');
	$('span.mail').remove(); /* remove spam-protection */

	if($('#headerImages div').length > 1) {
		window.setInterval("cycleDivs('#headerImages', 2000)", 8000);
	}
	
	$("#page form").submit(function() {
		var errors = 0;
		$("input[required][type!=radio], textarea[required]", this).each(function() {
			if(!$(this).val() || $(this).val() == $(this).data('default')) {
				showFormError(messages.error["required"]);
				$(this).addClass("requiredEmpty").one('focus', function() {
					$(this).removeClass("requiredEmpty");
				});
				errors++;
			}
		})
		$(".powermail_radio_wrapper:has(.powermail_mandatory_helper input:checked)", this).each(function() {
			var wrapper = this;
			showFormError(messages.error["required"]);
			$(this).addClass("requiredEmpty");
			$('input', this).focus(function() {
				$(wrapper).removeClass("requiredEmpty");
			});
			errors++;
		})
		$(".powermail_check_inner:has(input[required]:not(:checked))", this).each(function() {
			var wrapper = this;
			showFormError(messages.error["required"]);
			$(this).addClass("requiredEmpty");
			$('input', this).focus(function() {
				$(wrapper).removeClass("requiredEmpty");
			});
			errors++;
		})
		if(errors > 0) return false;
	});

});
