/**************************************************
--> Deze plugin is voor formulier controles
--> @author Michel de Groot <michel@bratpack.nl>
--> @copyright 2011 Bratpack Internetdiensten B.V.
--> @filesource Scripts/jControleer.js
--> @version 1.0 (31-05-2011)
--> @license Restricted.
***************************************************/

(function($){
	// plugin definitie
	$.fn.controleer = function(options){
		// Eerst de opties opbouwen voordat de plugin wordt uitgevoerd op het element
		var opts = $.extend({}, $.fn.controleer.defaults, options);
		// Voer de plugin uit op de aangegeven elementen
		var Form = $(this);
		if(opts.SubmitTrigger != null){
			$(opts.SubmitTrigger).bind('click', function(e){ e.preventDefault(); Form.submit(); });
		}
		Form.submit(function(){
			// Eerst even alle fout elementen verwijder, voordat we opnieuw checken
			$("."+opts.FoutClass).remove();
			$("#"+opts.FoutContainerId).remove();
			// Verplichte velden controleren
			var VerplichtVeld = $(this).find(".verplicht");
			VerplichtVeld.each(function(){
				var InhoudVerplicht = $(this).val();
				if(InhoudVerplicht != ""){
					$.fn.controleer.doSuccess(this, opts);
				} else {
					$.fn.controleer.doError(Form, this, opts, opts.TekstVerplicht);
				};
			});
			//
			// E-mail velden controleren
			var EmailVeld = $(this).find(".email");
			EmailVeld.each(function(){
				var InhoudEmail = $(this).val();
				var EmailExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
				if(InhoudEmail != "" && InhoudEmail.match(EmailExp)){
					$.fn.controleer.doSuccess(this, opts);
				} else {
					$.fn.controleer.doError(Form, this, opts, opts.TekstEmail);
				};
			});
			//
			// Wachtwoord velden controleren
			var WachtwoordVeld = $(this).find(".wachtwoord");
			WachtwoordVeld.each(function(){
				var InhoudWachtwoord = $(this).val();
				var WachtwoordExp = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
				if(InhoudWachtwoord != "" && !InhoudWachtwoord.match(WachtwoordExp)){
					$.fn.controleer.doSuccess(this, opts);
				} else {
					$.fn.controleer.doError(Form, this, opts, opts.TekstWachtwoord);
				};
			});
			//
			// Postcode velden controleren
			var PostcodeVeld = $(this).find(".postcode");
			PostcodeVeld.each(function(){
				var InhoudPostcode = $(this).val();
				var PostcodeExp = /^[1-9]{1}[0-9]{3}[A-Z]{2}$/i;
				if(InhoudPostcode != "" && InhoudPostcode.match(PostcodeExp)){
					$.fn.controleer.doSuccess(this, opts);
				} else {
					$.fn.controleer.doError(Form, this, opts, opts.TekstPostcode);
				};
			});
			//
			// Telefoonnummer velden controleren
			var TelefoonVeld = $(this).find(".telefoon");
			TelefoonVeld.each(function(){
				var InhoudTelefoon = $(this).val();
				var TelefoonExp = /^0[0-9]{9}$/i;
				if(InhoudTelefoon != "" && InhoudTelefoon.match(TelefoonExp)){
					$.fn.controleer.doSuccess(this, opts);
				} else {
					$.fn.controleer.doError(Form, this, opts, opts.TekstTelefoon);				
				};
			});
			//	
			// Verplichte selects controleren
			var VerplichtSelect = $(this).find(".verplicht-select");
			VerplichtSelect.each(function(){
				var InhoudVerplichtSelect = $(this).val();
				if(InhoudVerplichtSelect != 0){
					$.fn.controleer.doSuccess(this, opts);
				} else {
					$.fn.controleer.doError(Form, this, opts, opts.TekstSelect);
				};
			});
			//
			// Verplichte checkboxes controleren
			var VerplichtCheck = $(this).find(".check");
			VerplichtCheck.each(function(){
				var InhoudCheck = $(this).prop("checked");
				if(InhoudCheck){
					$.fn.controleer.doSuccess(this, opts);
				} else {
					$.fn.controleer.doError(Form, this, opts, opts.TekstCheck);
				};
			});
			//
			// Verplichte radiobuttons controleren
			var VerplichtRadio = $(this).find(".radio");
			VerplichtRadio.each(function(){
				var InhoudRadio = $(this).prop("name");
				var CheckedRadio = $("input[name="+InhoudRadio+"]").filter(":checked").val();
				if(CheckedRadio){
					$.fn.controleer.doSuccess(this, opts);
				} else {
					$.fn.controleer.doError(Form, this, opts, opts.TekstRadio);
				};
			});
			//
			if($("."+opts.FoutClass+"").length){
				return false;
			} else{
				if(opts.onSubmit != null && typeof opts.onSubmit == 'function'){
					opts.onSubmit.call(this);
					return false;
				} else{
					return true;
				}
			}
		});
	};
	// Met deze functies worden de meldingen aangemaakt
	$.fn.controleer.doSuccess = function(veld, o){
		/*$(veld).addClass(o.GoedClass);
		$(veld).removeClass(o.FoutClass);*/
	};
	$.fn.controleer.doError = function(Formulier, veld, o, tekst){
		// Meldingen opbouwen
		if(o.FoutContainerId != false){
			if($("#"+o.FoutContainerId).length == 0){
				Formulier.prepend("<div id="+o.FoutContainerId+"></div>");
			}
			$("#"+o.FoutContainerId).append("<"+o.FoutElement+" class="+o.FoutClass+">"+tekst+"</"+o.FoutElement+">");
		} else{
			$(veld).parents("td").append("<"+o.FoutElement+" class="+o.FoutClass+">"+tekst+"</"+o.FoutElement+">");
		}
	};
	// plugin defaults
	$.fn.controleer.defaults = {
		FoutClass: 'fout',
		FoutElement: "label",
		FoutContainerId: false,
		TekstVerplicht: "Dit veld is verplicht.",
		TekstWachtwoord: "U kunt geen gebruik maken van speciale characters.",
		TekstEmail: "U dient een geldig e-mailadres in te vullen.",
		TekstPostcode: "U dient een geldige postcode in te vullen in de vorm van 1234AB.",
		TekstTelefoon: "U dient een geldig telefoonnummer in te vullen.",
		TekstSelect: "U dient u keuze te maken uit de lijst.",
		TekstCheck: "Deze checkbox is verplicht.",
		TekstRadio: "U dient een keuze te maken uit bovenstaande opties.",
		SubmitTrigger: null,
		onSubmit: null
	};
	// Kleine debug functie
	function debug($obj){
		if(window.console && window.console.log){
			window.console.log('Debug functie: ' + $obj);
		}
	};
})(jQuery);
