(function($){
	$(document).ready( function() {
		// empty and non selected fields validation
		$('form').submit( function() {

			$(this).children('.valueerror').removeClass('valueerror');
			var errors = false;

			$(this).children('input[type="text"].mandatory, textarea.mandatory, select.mandatory').each( function() {
				if ($(this).val() == "0" || $(this).val().length < 1) {
					errors = true;
					$(this).addClass('valueerror');
					$(this + ' label[for="' + $(this).attr('id') + '"]').addClass('valueerror');
					if ($(this).hasClass('previous_label')) $(this).prevAll('label.mandatory:first').addClass('valueerror');
					if ($(this).hasClass('next_label')) $(this).prevAll('label.mandatory:last').addClass('valueerror');
					$(this + ' #' + $(this).attr('id') + '_select').addClass('valueerror');
				}
			});
					
			$(this + ' input[type="checkbox"].mandatory').each( function() {
				if (!$(this).is(':checked')) {
					errors = true;
					$(this).addClass('valueerror');
					$(this + ' label[for="' + $(this).attr('id') + '"]').addClass('valueerror');
					if ($(this).hasClass('previous_label')) $(this).prevAll('label.mandatory:first').addClass('valueerror');
					if ($(this).hasClass('next_label')) $(this).prevAll('label.mandatory:last').addClass('valueerror');
				}
			});
		
			$(this + ' input[type="radio"].mandatory').each( function() {
				group_name = $(this).attr('name');
				
				group_errors = false;
				$(this + ' input[name="' + group_name + '"]').each( function() {			
					if (!$(this).is(':checked')) {
						group_errors = true;
					} else {
						group_errors = false;
						return false;
					}
				});
				if (group_errors == true) {
					$(this + ' input[name="' + group_name + '"]').each( function() {
						$(this).addClass('valueerror');
						$(this + ' label[for="' + $(this).attr('id') + '"]').addClass('valueerror');
					if ($(this).hasClass('previous_label')) $(this).prevAll('label.mandatory:first').addClass('valueerror');
					if ($(this).hasClass('next_label')) $(this).prevAll('label.mandatory:last').addClass('valueerror');
					});
				}			
			});

			if (errors == true) {

				$(this + ' .errormessage').remove();
				$(this + ' .link_myopellogin').before('<p class="errormessage">Bitte f&uuml;llen Sie alle mit einem Stern gekennzeichneten Felder aus.</p>');
				var errors = false;

				return false;
			} else {
				if(document.getElementById('field_optinemail') !== null)	{
					if(!$('input#field_optinemail').is(':checked') && !$('.crm_form input#field_optinphonesms').is(':checked'))	{
						$('label.optinEmail, label.optinEmailNewsletter, label.optinPhoneSMS').addClass('valueerror');
						Check = confirm('Sind Sie sicher, dass Sie keine Informationen erhalten wollen??\n("OK" zum Absenden, "Abbrechen" zum Ändern)');
						
						if(Check == true)	{
							return true;
						} else	{
							return false;
						}
						
					} else	{
						return true;
					}
				} else	{
					return true;
				}
			}
		});
		
		// error displaying on selected fields
		$('input:checkbox.mandatory + label').click( function() {
			if (!$(this).prev('input').is(':checked')) $(this).removeClass('valueerror');
		});
		$('select').change( function() {
			 if ($(this).val() != "0") $('#'+$(this).attr('id')+'_select').removeClass('valueerror');
		});
		
		$('label.optinEmail, label.optinEmailNewsletter, label.optinPhoneSMS').click( function() {
			if ($(this).hasClass('checked')) $(this).removeClass('valueerror');
		});
		
		// checking the main checkbox within the "datenschutzhinweis" block
		$('label.optinEmail, label.optinEmailNewsletter, label.optinPhoneSMS, input#field_optinemail, input#field_optinphonesms').click( function() {
			inputsid = $(this).attr('for');
			if ($(this).hasClass('checked') || $('input#' + inputsid).is(':checked') || $(this).is(':checked')) {
				$('label.optinGeneral').addClass('checked');
				$('input#field_optingeneral').check();
			}
		});
		$('label.optinGeneral, input#field_optingeneral').click( function() {
			inputsid = $(this).attr('for');
			if (!$(this).hasClass('checked') || !$('input#' + inputsid).is(':checked') || !$(this).is(':checked')) {
				$('label.optinEmail, label.optinEmailNewsletter, label.optinPhoneSMS').removeClass('checked');
				$('input#field_optinemail, input#field_optinphonesms').uncheck();
			}
		});
	});
})(jQuery);