new function() {
    $.fn.validate = {
        init: function(o) {
          if($(o).attr("rel") == 'email') { this.email(o) };
          if($(o).attr("rel") == 'required') { this.required(o) };
		  if($(o).attr("rel") == 'radio') { this.radio(o) };
        },
        email: function(o) {
          var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
          if (o.value.match(email)) {
              doSuccess(o);
          } else {
              doError(o);
          };
        },
        required: function(o) {
			if (o.value == $(o).attr("title")) {
				doError(o);
            } else {
				doSuccess(o);
            };
        },
		radio: function(o) {
			var o_name = "input[name="+$(o).attr('name')+"]";
			if (!$(o_name).is(':checked')) {
				doError($(o).parents().find('.radioWrapper'));
				doError(o);
            } else {
				doSuccess($(o).parents().find('.radioWrapper'));
				doSuccess(o);
            };
        }
     };
     function doSuccess(o) {
			$(o).removeClass("error");
     }
     function doError(o) {
			$(o).addClass("error");
     }
};
$(document).ready(function() {
	$("input#inputPassword").replaceWith('<input id="inputPassword" class="textInput half replace validate password" type="text" name="UserPsw" value="Sicherheits-Code*" title="Sicherheits-Code*" rel="required" />');
	
	$(".noJS").hide();
	$(".replace").live('focus', function(){
		if($(this).val() == $(this).attr("title")) {
			$(this).removeClass('given'); 
			if($(this).hasClass("password") && !isReplaces) {
				$('#inputPassword').replaceWith('<input id="inputPassword" class="textInput half replace validate password" type="password" name="UserPsw" value="" title="Sicherheits-Code*" rel="required" />');
				var isReplaces = true;
				setTimeout(function(){
					$('#inputPassword').focus();
				}, 10);
			} else {
				$(this).val("");
			}
		}
	});
	$(".replace").live('blur', function(){
		if($(this).val() == '') {
			$(this).val($(this).attr("title"));
			if($(this).hasClass("password")) {
				$('#inputPassword').replaceWith('<input id="inputPassword" class="textInput half replace validate password given" type="text" name="UserPsw" value="Sicherheits-Code*" title="Sicherheits-Code*" rel="required" />');
			} else {
				$(this).addClass('given'); 
			}
			var isReplaces = false;		
		}
	});
	
	$('#JAzahlungsweise1').click(function(){
		$('#bankeinzug_felder input').each(function(){
			$(this).addClass('validate').attr('disabled', '').attr('rel', 'required').val($(this).attr("title"));
		});
	});
	
	$('#JAzahlungsweise2').click(function(){
		$('#bankeinzug_felder input').each(function(){
			$(this).removeClass('validate').attr('disabled', 'disabled').attr('rel', '').val("");
		});
	});
	
	$("form").live('submit',function() {
		var error = false;
		$(this).find( " .validate").each(function(){
			$(this).validate.init(this);
			if($(this).hasClass("error")) {
				error = true;
			}
		});
		if(error) {
			alert("Bitte prüfen und ggf. vervollständigen Sie die notwendigen Angaben.");
			return false;
		}else {
			// Jungjaeger
			if($(this).attr('id') == "novices-login") {
				var context = $(this);
				$.post($(this).attr('action')+'?ajax=1', $(this).serialize(), function(data, status) {
					if(data.length > 0) {
						$(context).parents('#form-wrapper').empty().append(data);
					}
				});
				
				
				return false;
			} else {
			
			}
		}
	});
	
	$("input[name=RegHersteller]").click(function(){
		productDataFields();
	});
	
});

function productDataFields () {
	if($('#hersteller5').attr('checked') == true) {
		$('#notRWS').hide();
		$('#notRWS input').each(function(){
			$(this).removeClass('validate').attr('rel', '').val("");
		});
		$('#isRWS').show();
		$('#isRWS input').each(function(){
			$(this).addClass('validate').attr('rel', 'required').val($(this).attr("title"));
		});
	} else {
		$('#isRWS').hide();
		$('#isRWS input').each(function(){
			$(this).removeClass('validate').attr('rel', '').val("");
		});
		$('#notRWS').show();
		$('#notRWS input').each(function(){
			$(this).addClass('validate').attr('rel', 'required').val($(this).attr("title"));
		});	
	}
}
