(function($){

    $.fn.valid = function(){        
        
        var inputs = new Array();
			
		$(this).find("fieldset > ul > li.neededInfo").each(function(index, domElement) {
		    $(this).append("<span class='missingInfo'>Pflichtfeld, bitte ausfüllen!</span>");
		    
		    $(this).children(".inputString").blur(validateString);
		    $(this).children(".inputNumber").blur(validateNumber);
		    $(this).children(".email").blur(validateEmail);
		    $(this).children(".datepicker").change(validateString);
		    
		    inputs[$(this).children(":input").attr("name")] = false;				
		});
		
		function validateString(){
		    
		    if($(this).val().length < 3) {
		    	
		    	$(this).next(".missingInfo").text("Angabe fehlt!").css({"color" : "#cf2525", "font-weight" : "bold"});
		    	
		    	inputs[$(this).attr("name")] = false;
		    }
		    else {
		    	$(this).next(".missingInfo").text("Angabe korrekt!").css({"color" : "green", "font-weight" : "bold"});
		    	
		    	inputs[$(this).attr("name")] = true;					
		    }
		}
		
		function validateNumber(){
		    var regex = /\d/;
		    if(
		    	!($(this).val().length > 0) &&
		    	!($(this).val().match(regex))
		    ) {
		    	
		    	$(this).next(".missingInfo").text("Angabe fehlt!").css({"color" : "#cf2525", "font-weight" : "bold"});
		    	
		    	inputs[$(this).attr("name")] = false;
		    }
		    else {
		    	$(this).next(".missingInfo").text("Angabe korrekt!").css({"color" : "green", "font-weight" : "bold"});
		    	
		    	inputs[$(this).attr("name")] = true;
		    }				
		}
		
		function validateEmail(){
		var email = $(this).val();
		    if(
		    	!(email.lastIndexOf(".") > 2) && 
		    	!(email.indexOf("@") > 0) && 
		    	!(email.lastIndexOf(".") > (email.indexOf("@")+1))
		    ) {
		    	$(this).next(".missingInfo").text("eMail inkorrekt!").css({"color" : "#cf2525", "font-weight" : "bold"});
		    	
		    	inputs[$(this).attr("name")] = false;
		    }
		    else {
		    	$(this).next(".missingInfo").text("eMail korrekt!").css({"color" : "green", "font-weight" : "bold"});
		    	
		    	inputs[$(this).attr("name")] = true;
		    }
		}
		
		function validateCompleteForm() {
		    for (key in inputs) {
		    	if(!inputs[key]) return false;
		    }
		    return true;
		}
	
		$(this).submit(function(event){
		    event.preventDefault();
		    if(validateCompleteForm()) {
		    	$.post("kontaktForm/sendkontakt.php", {
		    		ts: $(":input[name=ts]").val(),
		    		vorname: $(":input[name=vorname]").val(), 
		    		nachname: $(":input[name=nachname]").val(),
		    		email: $(":input[name=email]").val(),
		    		strnr: $(":input[name=strnr]").val(),
		    		plzort: $(":input[name=plzort]").val(),
		    		tel: $(":input[name=tel]").val(),
		    		betreff: $(":input[name=betreff]").val(),
		    		nachricht: $(":input[name=nachricht]").val().replace(/(\r\n)|(\n)/g,"<br />")
		    		},
		    		function() {
		    			$("form").slideUp("slow", function() {
		    			$("div.userNotification#success").fadeIn("slow");
		    		});
		    	});
		    }
		    else {
		    	$(this).slideUp("slow", function() {
		    		$("#fail").fadeIn("slow");
		    		$("a").click(function() {
		    			$("div.userNotification#fail").fadeOut("slow", function() {
		    				$("form").slideDown("slow");
		    			});
		    		});
		    	});
		    }
		});
	};
})(jQuery); 
