$(document).ready(function(){
	
		LBL_CHARSLEFT = { 'en': 'characters left', 'de': 'Zeichen &uuml;brig' };
	
	/*
	 *	Limit length of textareas
	 */
	$("#contact-form textarea.limited").keyup(function() 
	{
		if (!this.area) 
		{
			this.area = $(this);
		}
	
		// set maxlength
		if (!this.maxlength)
		{
			this.maxlength = 160;
		
			// get maxlength
			var className = this.area.attr('className');
			if (className.indexOf('maxlength') > -1)
			{
				this.maxlength = className.substring(className.indexOf('maxlength') + 9);
				if (this.maxlength.indexOf(' ') > -1)
				{
					this.maxlength = this.maxlength.substring(0, this.maxlength.indexOf(' '));
				}
			}
		}

		// check length of input
		var val = this.area.val();
		if (val.length > this.maxlength)					
		{ 
			this.area.val(val.substr(0, this.maxlength));
		}

		// display remaining characters
		if (!this.counter)
		{
			this.counter = $('#rem_' + this.area.attr('id'));
		}
		
		this.counter.html((this.maxlength - this.area.val().length) + ' ' + LBL_CHARSLEFT[LANGUAGE]);
	});
	
	/*
	 *	Validate contact form
	 */
	$("#contact-form").submit(function() {
		var bAllValid = true;
		$("input.required").each(function() {
			
			switch(this.type) {
				case "radio":
					if($("input[type=radio][name='" + this.name + "']").filter(':checked').length == 0) {			
						bAllValid = false;					
						$(this).parent().children("label:first").addClass("invalid");
						$("label[for='" + this.id + "']").each(function() {
							// $(this).addClass("invalid");
						});					
					} else {
						$(this).parent().children("label:first").removeClass("invalid");
						$("label[for='" + this.id + "']").each(function() {
							$(this).removeClass("invalid");
						});
					}
					break;
				
				default:				
					if(this.value == "") {
						bAllValid = false;
						$(this).addClass("invalid");
						$("label[for='" + this.id + "']").each(function() {
							$(this).addClass("invalid");
						});
					} else {
						$(this).removeClass("invalid");				
						$("label[for='" + this.id + "']").each(function() {
							$(this).removeClass("invalid");
						});
					}
					break;
			}
			
		});
		
		$("select.required").each(function(sValue, oElement) {
			if(this.value == 0) {
				bAllValid = false;
				$(this).addClass("invalid");				
				$("label[for='" + this.id + "']").each(function() {
					$(this).addClass("invalid");
				});
			} else {
				$(this).removeClass("invalid");				
				$("label[for='" + this.id + "']").each(function() {
					$(this).removeClass("invalid");
				});
			}
		});
		
		$("textarea.required").each(function(sValue, oElement) {
			if(this.value == 0) {
				bAllValid = false;
				$(this).addClass("invalid");				
				$("label[for='" + this.id + "']").each(function() {
					$(this).addClass("invalid");
				});
			} else {
				$(this).removeClass("invalid");				
				$("label[for='" + this.id + "']").each(function() {
					$(this).removeClass("invalid");
				});
			}
		});
		
		if(bAllValid == false) {
			$("#form-validation-error").addClass("show");
		} else {
			$("#form-validation-error").removeClass("show");
		}
		
		return bAllValid;
	});
	
	/**
 * redirects users to the diagnostics contactform when they choose 'Diagnostics' as subject
 * redirects users to the publications order form when they choose 'Publications' as subject
 */
 
$('#id-subject').change(function(e){
	if ($(this).val() == 'Diagnostics') {
		location.href = 'contact_form-diag.htm';
	}
	if ($(this).val() == 'Publications') {
		location.href = 'corporate_publications.htm';
	}
});
	
	jQuery(document).ready(function()
{
	jQuery(".autotooltip").hide().parent().tooltip({bodyHandler: function()
	{
		return jQuery(this).children(".autotooltip").html()
	},
	delay: 0, 
    showURL: false,
	extraClass: "autotooltip"
	});
});
	
	jQuery(".mailsubject label a").each(function()
	{
	    var link = jQuery(this).attr("href");
	    var opened = 0;
		
		jQuery(this).click(function() { opened = new Date().valueOf(); return true; });
		
	    jQuery("#" + jQuery(this).parent().attr("for")).bind("focus click",function()
	    {
			jQuery(this).removeAttr("checked").blur();
			var id = jQuery(this).attr("id");
			setTimeout(function() {jQuery("#" + id).removeAttr("checked").blur();},100);
	        if (opened + 500 < new Date().valueOf())
			{
				window.open(link,'','');
				opened = new Date().valueOf();
			}
	    });
	});
	
	
	
});

