$(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;
	});
	
	$(document).ready(function(){

	/*
	 *	Dia/General contact form: Activate form elements on dia contact page which are not usable without javascript
	 */	
	if($("#id-subject").length) {
		$("#id-subject-label").show();
		$("#id-subject").show();
	}
	if($("#id-product").length) {
		$("#id-product-label").show();
		$("#id-product").show();
		
		/*
		 *	Dia contact form: Disable dropdowns until a country is chosen
		 */
		$("#id-subject").attr("disabled","disabled");
		$("#id-product").attr("disabled","disabled");		
	}
	
	
	
	/*
	 *	Watch changes on the country list
	 */	
	$("#id-country").change(function() {
		diaContactCountryDropdown();		
	});	
	
	/*
	 *	Watch changes on the subject list
	 */
	$("#id-subject").change(function() {
		diaContactSubjectDropdown();		
	});	
	
	/*
	 *	Populate the "subject" list according to availability in the selected country
	 *
	 */
	function diaContactCountryDropdown() {
		var sProductOptions = '<option value="">(Only available when Subject is \"Products\")</option>';
		$("#id-product").html(sProductOptions);	
		$("#id-product").attr("disabled","disabled");
		
		if($("#id-country").attr("value") > 0) {
			$("#id-subject").attr("disabled","");
			
			$.getJSON("form-dropdown-generator?format=json", { countryId: $("#id-country").attr("value")}, function(data) {				
				var options = '<option value="">Please select</option>';
				for (var i = 0; i < data.length; i++) {
					options += '<option value="' + data[i].key + '">' + data[i].value + '</option>';
				}
				$("#id-subject").html(options);
			});
			
		} else {
			$("#id-subject").attr("disabled","disabled");
			$("#id-subject").html('<option value="">(Only available when Subject is "Products")</option>');
		}
	}
	
	/*
	 *	Populate the "product" list according to availability in the selected country (only if the subject "product" is chosen)
	 *
	 */
	function diaContactSubjectDropdown() {
		if($("#id-country").attr("value") > 0 && $("#id-subject").val() == 1) {
			$("#id-product").attr("disabled","");
			
			$.getJSON("form-dropdown-generator?format=json", { countryId: $("#id-country").attr("value"), subjectId: $("#id-subject").attr("value") }, function(data) {				
				var options = '<option value="">Please select</option>';
				for (var i = 0; i < data.length; i++) {
					options += '<option value="' + data[i].key + '">' + data[i].value + '</option>';
				}
				$("#id-product").html(options);				
			});
			
		} else {
			$("#id-product").attr("disabled","disabled");
			$("#id-product").html('<option value="">(Only available when Subject is "Products")</option>');
		}
	}
	

	/*
	 *	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;
	});
});

	
});


