(function (){
		$.validateform = {
		
			defaults: {
				onComplete: function (){},
				btn: '#sendForm',
				normMessage: 'Veld is verplicht/ Field is required',
				emailMessage: 'E-mail is verplicht/ E-mail is required',
				radioMessage: 'Kies een optie/ Choose an option',
				passMessage:'Wachtwoord moet hetzelfde zijn/ Password has to be equal',
				selectMessage: 'Kies een veld/ Choose a field'
				
			}
		}
		$.fn.extend({
			validateform: function (config) {
				
				var config = $.extend({}, $.validateform.defaults, config);
				
				var onComplete = config.onComplete;
				var normMessage = config.normMessage;
				var emailMessage = config.emailMessage;
				var selectMessage = config.selectMessage;
				var radioMessage = config.radioMessage;
				var passMessage = config.passMessage;
				var rPass  = new Array();
				var btn = config.btn;
				var clickedForm = false;
			
				return this.each(function (){
					var form = $(this);
					form.find('.valn').each(function (){
						$(this).attr('id', $(this).attr('name'));
						$(this).parent().append('<div class="errorMess">'+normMessage+'</div>');
						$(this).parent().find('.errorMess').hide();
						$(this).blur(function (){if(clickedForm)checkFields()});
					});
					form.find('.vale').each(function (){
						$(this).attr('id', $(this).attr('name'));
						$(this).parent().append('<div class="errorMess">'+emailMessage+'</div>');
						$(this).parent().find('.errorMess').hide();
						$(this).blur(function (){if(clickedForm)checkFields()});
					});
					form.find('.vals').each(function (){
						
						$(this).parent().find('select').each(function (){
							$(this).attr('id', $(this).attr('name'));
						})
						$(this).parent().append('<div class="errorMess">'+selectMessage+'</div>');
						$(this).parent().find('.errorMess').hide();
						$(this).blur(function (){if(clickedForm)checkFields()});
					});
					var iPass = 0;
					form.find('.valp').each(function (){
						$(this).attr('id', $(this).attr('name'));
						rPass[iPass] = $(this).attr('id');
						iPass++;
						$(this).parent().append('<div class="errorMess">'+passMessage+'</div>');
						$(this).parent().find('.errorMess').hide();
						$(this).blur(function (){if(clickedForm)checkFields()});
					});
					
					var n=0;
					form.find('.valr').each(function (){
						n++;
						if ($(this).is(":checked")){
							$(this).attr('id', $(this).attr('name'));
						}
						
						if (n < 2)
						$(this).parent().parent().append('<div class="errorMess">'+radioMessage+'</div>');
						$(this).parent().parent().find('.errorMess').hide();
						$(this).blur(function (){if(clickedForm)checkFields()});
					});
					
					jQuery(btn).click(function () {
						clickedForm = true;
						if (checkFields ())
						onComplete();
					});
					//===================Validation
					
					function checkFields () {
						var f = 0;
						var g = 0;
						var h = 0;
						var j = 0;
						var l = 0;

						
						iP = 0;
						form.find('.valp').each(function (){
							if($(this).val() != ""){
									f=0;
									$(this).parent().parent().find('.errorMess').hide();
									
									for (var i = 0; i < rPass.length; i++) {
										if (form.find('#'+rPass[i]).val() == form.find('#'+rPass[i + 1]).val()) {
											f=0;
											$(this).parent().parent().find('.errorMess').hide();
										}else{
											f++;
											$(this).parent().parent().find('.errorMess').show();
										}
										if (form.find('#'+rPass[i]).val() == form.find('#'+rPass[i - 1]).val()) {
											f=0;
											$(this).parent().parent().find('.errorMess').hide();
										}else{
											f++;
											$(this).parent().parent().find('.errorMess').show();
										}
									}

								}else{
									f++;
									$(this).parent().parent().find('.errorMess').show();
								}
								iP++;
						});
						
						
						form.find('.valn').each(function (){
							if($(this).val() == ""){
								g++;
								$(this).parent().find('.errorMess').show();
							}else{
								g=0;
								
								$(this).parent().find('.errorMess').hide();
							}
						});
						form.find('.vale').each(function (){
							if(echeck($(this).val())){
								h=0;
								
								$(this).parent().find('.errorMess').hide();
							}else{
								h++;
								$(this).parent().find('.errorMess').show();
							}
						});
						form.find('.vals').each(function (){
							if($(this).val() != ""){
									j=0;
									
									$(this).parent().parent().find('.errorMess').hide();
								}else{
									j++;
									$(this).parent().parent().find('.errorMess').show();
								}

						});
						
						
						
						
						if (f == 0 && j ==0 && g ==0 ) {
							return true;
						}else{
							return false;
						}
					}
					
					function echeck(str) {

						var at="@"
				    	var dot="."
				    	var lat=str.indexOf(at)
				    	var lstr=str.length
				    	var ldot=str.indexOf(dot)
				    	if (str.indexOf(at)==-1){
				    	   return false
				    	}
				
				    	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
				    	   return false
				    	}
				
				    	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
				    	    return false
				    	}
				
				    	 if (str.indexOf(at,(lat+1))!=-1){
				    	    return false
				    	 }
				
				    	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
				    	    return false
				    	 }
				
				    	 if (str.indexOf(dot,(lat+2))==-1){
				    	    return false
				    	 }
				    	
				    	 if (str.indexOf(" ")!=-1){
				    	    return false
				    	 }
				
				 		 return true	;			
				    }
					
					//===================END
				})
			}
		});
	
})(jQuery);
