$.metadata.setType("attr", "validation");

$.validator.setDefaults({
	errorPlacement: function(error, element) {
		var errorElement = jQuery("<label/>").addClass("door_bg");
		var heightOfErrorElement = 30;
		var referenceElement;
		var offset; 
			
		if( jQuery( element ).attr("data-is-webeditor") == 1 ) {
		
			/*  Input is webeditor and is invisible so the error message is positioned based on the parent  */
			referenceElement = element.parent();
		
		} else if( jQuery( element ).attr("type") == "checkbox" ) {
			/*  The label must be attached to the current element  */
			var selectorLabelForCurrent = "*[for='" + jQuery( element ).attr("id")  + "']";

			if( jQuery( element ).nextAll(selectorLabelForCurrent).length == 1 ) {

				/*  We found a label after the element  */
				referenceElement = jQuery( element ).nextAll(selectorLabelForCurrent);
			
			} else if( jQuery( element ).prevAll(selectorLabelForCurrent).length == 1 ){
			
				/*  Found a label before the element  */
				referenceElement = jQuery( element ).prevAll(selectorLabelForCurrent);
			
			} else {
			
				/*  No label found around the checkbox, this shouldn't happen  */
				throw("No label found around the checkbox element with id " + jQuery( element ).attr("id"));
			
			}
		} else {
			
			/*  Normal element  */
			referenceElement = element;
			
		}
		offset = jQuery( referenceElement ).offset();


		/*  Position the error message 5 pixels to the right of the element and vertically in the middle  */
		errorElement[0].style.left = offset.left + jQuery( referenceElement ).width() + 5 + "px";
		errorElement[0].style.top  = offset.top  + (jQuery( referenceElement ).height() / 2) - (heightOfErrorElement / 2)  + "px";
		
		
		jQuery(error[0]).addClass("door_fg");
		errorElement.append(error);
		errorElement.appendTo( element.parent("td") );
	},
	wrapper: 'label'
});


/*  Sort field  */
$.validator.addMethod("custom_sort_field", function(value, element) {
	return this.optional(element) || /^\d+$/i.test(value);
} , "Bitte geben Sie bei der Reihung ein Zahl ein.");

$.validator.addMethod("custom_required_webeditor", function(value, element) {
	/*  Get the content directly from the FCKEditor  */
	/*  This function is modeled after the function UpdateLinkedField in _source/internals/fck.js  */
	var FCK = window.FCKeditorAPI.GetInstance(element.tagName);
	var v = FCK.GetXHTML( FCK.Config.FormatOutput ) ;

	if ( FCK.Config.HtmlEncodeOutput )
		v = FCKTools.HTMLEncode( v ) ;

	return (v != "" && v != "<p>&#160;</p>");
} , "Dieses Feld ist ein Pflichtfeld.");

/*  Selectbox with category  */
$.validator.addMethod("custom_category", function(value, element) {
	/*  0 means no category is selected  */
	return this.optional(element) || (value != 0);
} , "Bitte wählen Sie eine Kategorie.");

/*  Selectbox with salutation  */
$.validator.addMethod("custom_salutation", function(value, element) {
	/*  0 means no category is selected  */
	return this.optional(element) || (value != 0);
} , "Bitte geben Sie Ihre Anrede ein.");

/*  Selectbox with country  */
$.validator.addMethod("custom_country", function(value, element) {
	/*  0 means no category is selected  */
	return this.optional(element) || (value != 0);
} , "Bitte geben Sie das Land ein.");


jQuery(document).ready(function($){
	var validator = jQuery("#inputForm").validate();
});

jQuery.noConflict();
