(function($) {
	$(init);
	var form_register = '#tx-srfeuserregister-pi1-fe_users_form.register';
	var form_edit = '#tx-srfeuserregister-pi1-fe_users_form.edit';
	var form_all = '#tx-srfeuserregister-pi1-fe_users_form';
	function check_fields($form) {
		var ok = true;
		var password = '';
		$('input[type="text"], select', $form).each(function(index){
			$t = $(this);
			//get the name-attribute to identify the element
			name = $t.attr('name');
			var value = '';

			if(name.match(/\[email\]/i) != null) {
				value = prepareValue($t, this);
				if(value.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i) != null) {
					clear_hint($t);
				} else {
					hint_message($t, 'Bitte geben Sie uns eine korrekte E-Mail. Sie ist wichtig, damit wir Ihren gedankenPlatz-Gast-Konto zuordnen können.');
					ok = false;
				}
			}
			//check Password only on form hasClass register
			if(true) { //$form.hasClass('register')) {
				if(name.match(/password/i) != null) {
					value = prepareValue($t, this);
					if(name.match(/again/i) == null) {
						password = value;
						if(value.match(/^.{5,}$/i) == null) {
							hint_message($t, 'Das Passwort sollte aus mindestens 5 Zeichen bestehen.');
							ok = false;
						}
						else if(value.match(/^\d*$/i) != null) {
							hint_message($t, 'Das Passwort sollte nicht nur aus Zahlen bestehen.');
							ok = false;
						}
						else {
							clear_hint($t);
						}
					} 
					else {
						if(value != password) {
							hint_message($t, 'Die beiden Passwörter müssen gleich sein.');
							ok = false;
						}
						else {
							clear_hint($t);
						}
					}
				}
			}
			if(name.match(/first_name/i) != null) {
				value = prepareValue($t, this);
				if(value.match(/\w{2,}/i) != null) {
					clear_hint($t);
				} else {
					hint_message($t, 'Der Vorname muss aus mindestens zwei Buchstaben bestehen.');
					ok = false;
				}
			}
			if(name.match(/last_name/i) != null) {
				value = prepareValue($t, this);
				if(value.match(/\w{3,}/i) != null) {
					clear_hint($t);
				} else {
					hint_message($t, 'Der Nachname muss aus mindestens drei Buchstaben bestehen.');
					ok = false;
				}
			}
			if(name.match(/line_of_business/i) != null) {
				value = prepareValue($t, this);
				if(value.match(/^$/i) == null) {
					clear_hint($t);
				} else {
					hint_message($t, 'Bitte geben Sie uns Ihre Branche an. Wenn Sie keine angeben möchten, dann belassen Sie das Feld auf "Keine Angabe".');
					$t.val('Keine Angabe');
					ok = false;
				}
			}
			if(name.match(/company/i) != null) {
				value = prepareValue($t, this);
				if(value.match(/^$/i) == null) {
					clear_hint($t);
				} else {
					hint_message($t, 'Bitte geben Sie uns Ihre Firma. Wenn Sie keine angeben möchten, dann belassen Sie das Feld auf "Keine Angabe".');
					$t.val('Keine Angabe');
					ok = false;
				}
			}
			if(name.match(/status/i) != null) {
				value = prepareValue($t, this);
				if(value.match(/^0$/i) != null) {
					hint_message($t, 'Bitte geben Sie uns Ihre Status. Wenn Sie keinen angeben möchten, belassen Sie das Feld auf "Sonstiges".');
					ok = false;
					$t.val('5');
				} else {
					clear_hint($t);
				}
			}
			if(name.match(/date_of_birth/i) != null) {
				value = prepareValue($t, this);
				if(value.match(/^\d{1,2}\.\d{1,2}\.\d{4}$/i) != null) {
					clear_hint($t);
				} else {
					hint_message($t, 'Bitte geben Sie uns Ihr Geburtstag. Wenn Sie dies nicht möchten, belassen Sie das Feld so und schicken Sie das Formular noch einmal ab.');
					ok = false;
					$t.val('1.1.1900');
				}
			}
		});
		if(ok){
			$('input[type="text"], select', $(form_all)).each(function(index){
				$t = $(this);
				if($t.val() == this.defaultValue){
					$t.val('');
					//$t.remove();
				}
			});			
		}
		return ok;
	}
	function prepareValue($t, t) {
		// get the value of the element
		var value = $t.val();
		// empty/false the value if it is the defaultValue
		if(t.defaultValue == value) {
			value = '';
		}
		// trim the value and rewrite it for better displaying
		else {
			value = $.trim(value);
			$t.val(value);
		}
		return value;
	}
	function hint_message($t, mes) { 
		//check if mes is a string. then output the string
		if(typeof(mes) == 'string'){
			var hint = mes;
		}
		else {
			var hint = $t.metadata().hint;			
		}
		$t.closest('.fieldrow').addClass('hint').find('.hint').text(hint);
	}
	function clear_hint($t){
		$t.closest('.fieldrow').removeClass('hint').find('.hint').html('&nbsp;');
	}
	function setDefaultValue($t, t) {
		//if there is a empty value get the defaultValue and set the class defaultstate
		if(!$t.val() || $t.val() == t.defaultValue){
			$t.val(t.defaultValue);
			$t.addClass('defaultstate');
		}
		//get the birthday
		if($t.attr('name') == 'day') {
			var hits = $t.closest('.fieldrow').find('input').val().match(/^\d+/i);
			if( hits != null) {
				$t.val(parseInt(hits[0]));
			}
		}
		if($t.attr('name') == 'month') {
			var hits = $t.closest('.fieldrow').find('input').val().match(/^\d+\.(\d+)/i);
			if( hits != null) {
				$t.val(parseInt(hits[1]));
			}
		}
		if($t.attr('name') == 'year') {
			var hits = $t.closest('.fieldrow').find('input').val().match(/\d+$/i);
			if( hits != null) {
				$t.val(parseInt(hits[0]));
			}
		}
	}
	function setHintOnError($t) {
		//if there is in the span.hint text mark the div with .hint
		var text = $.trim($t.closest('.fieldrow').find('.hint').text());
		if(text.match(/\w/i) != null){
			$t.closest('.fieldrow').addClass('hint');
		}
	}
	function init(){
		//check the fields
		 $(form_all).on('submit', function(){
		 	var $t = $(this);
			var ok = check_fields($t);
			if(!ok){
				//scroll to the top off the form
				$('html').animate({scrollTop: $t.offset().top}, 'slow');
			}
			return ok;
		});
		$('input[type="text"], select', $(form_all)).each(function(index){
			setHintOnError($(this));
			setDefaultValue($(this), this);
		});
		//to set the birth-day. selct2input
		$(form_all).on('change', 'div.date_of_birth select', function(){
			$t = $(this);
			var value = '';
			$t.parent().find('select').each(function(){
				value+=$(this).val()+'.';
			});
			$('#tx-srfeuserregister-pi1-date_of_birth').val(value.replace(/\.$/, ''));
		});
		$(document).keypress(function(e){
			// press shift and q for fillOut the fields
			if(e.which == 81 ){
				debugSetField(form_all);
			}
		});
	}
	function debugSetField($form) {
		var identify = Math.round(Math.random()*100000000);
		$('input[type="text"], select', $form).each(function(index){
			$t = $(this);
			name = $t.attr('name');
			if(name.match(/\[email\]/i) != null) {
				$t.val(identify+'@zerored.de');
			}
			if(name.match(/password/i) != null) {
				$t.val('passwordf'+identify);
			}
			if(name.match(/first_name/i) != null) {
				$t.val('Max'+identify);
			}
			if(name.match(/last_name/i) != null) {
				$t.val('Mustermann'+identify);
			}
			if(name.match(/line_of_business/i) != null) {
				$t.val('Irgendwas');
			}
			if(name.match(/company/i) != null) {
				$t.val('Blender AG');
			}
			if(name.match(/status/i) != null) {
				$t.val(4);
			}
			if(name.match(/date_of_birth/i) != null) {
				$t.val('1.2.1984');
			}
		});
		return true;
	}	
})(jQuery);

