

$(document).ready(function() {	
 valid.init()
});

valid = {
	
	init : function()  {
		
		
		$("form").submit(function () { 
			var formToValid = this
			if($(formToValid).hasClass("fr")){
				lng = "f"
			}else(
				lng = "e"	
			)
			
			
			divErrorContainer = document.createElement('div')
			$(divErrorContainer).attr('id','formError');
			$(formToValid).prepend(divErrorContainer)
			
			var vA=valid.emptyInput(lng, formToValid);
			var vB=valid.emptyEmail(lng, formToValid);
			
				
			if(vA && vB){
				return true;
			}
			return valid.finall();

		});
		
		
	},
	
	
	
	// Below - Input Error
	
	emptyInput : function(lng, formToValid)  {

		var isError=false;

		emptyCountMax = $(formToValid).find(".validEmpty").size();

		$(formToValid).find(".validEmpty").parent().find(".labelName").css("color",""); // put back the normal color
		$(".emptyInputError").css({ display:"none"}); // Each time you start the script it disable error display, so when you push the button a seoncd time , you have only the new error of your form
		$(".validForm").css({ display:"none"});

		for (i=0; i < emptyCountMax; i++) {
			if($(formToValid).find(".validEmpty").eq(i).attr('value') == ""){
				$(formToValid).find(".validEmpty").eq(i).parent().find(".emptyInputError").css({ display:"block"});
				$(".validForm").css({ display:"block"});
				var errMess=$(formToValid).find(".validEmpty").eq(i).parent().find(".emptyInputError");
			
				if(errMess.length == 0){
					var divError = document.createElement('div')
					
					//namee = $('.validEmpty').eq(i).attr('name');

					var iLabel=$(formToValid).find(".validEmpty").eq(i).parent().find(".labelName").html();//le label du champ
					//enlève le : du label
					iLabel=iLabel.replace(':','');
					//alert(iLabel);

					$(divError).addClass('emptyInputError');
					$(divError).addClass('error');
					// ** BELOW - Take this out to remove error message
					if(lng=='f'){
						$(divError).html("Le champ <i>"+iLabel+"</i> est requis"); // French version (default)
					} else {
						$(divError).html("&rsaquo; The field <strong>"+iLabel+"</strong> is mandatory"); // Use <strong> for bold <i> for italics or both
					}
					// ** ABOVE - Take this out to remove error message
					$(formToValid).find(".validEmpty").eq(i).parent().find(".labelName").css("color","#076EB0"); // Set colour for label text
					$('#formError').append(divError);
					// $('.validEmpty').eq(i).parent().append(divError) Append dans le parent du input
					//$('.validEmpty').eq(i).parent().css("color","red");
					$(divError).css({ display:"block",color:"#076EB0", clear:"both"}); // Set colour for error message text
				}
				isError=true;//valid.finall();
			
			}
		}
		
		return !isError;
		
	},
	
	
	
	// Below - Email Error
	
	emptyEmail : function(lng, formToValid)  {
		var isError=false;
		emptyCountMax =$(formToValid).find(".validEmail").size();
		$(".validEmailError").css({ display:"none"});
		$(".validForm").css({ display:"none"});
		$(formToValid).find(".validEmail").parent().find(".labelName").css("color",""); // put back the normal color

		
		var filter  = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/;
		for (i=0; i < emptyCountMax; i++) {
	

			var thisMail=$(formToValid).find(".validEmail").eq(i).attr('value');
			//alert(thisMail);
			if(!filter.test(thisMail)){
				
				$(formToValid).find(".validEmail").eq(i).parent().find(".validEmailError").css({ display:"block"})
				$(".validForm").css({ display:"block"})
				var errMess=$(formToValid).find(".validEmail").eq(i).parent().find(".validEmailError");
				
				if(errMess.length == 0){
						
					divError = document.createElement('div');
					
					$(divError).addClass('validEmailError');
					$(divError).addClass('error');
                    if(lng=='f'){
						$(divError).text("* Adresse courriel non valide"); // French version (default) with .text rather than .html
					}else {
						$(divError).html("&rsaquo; Please enter a valid <strong>Email Address</strong>"); // Use <strong> for bold <i> for italics or both
					}
					$(formToValid).find(".validEmail").eq(i).parent().find(".labelName").css("color","#076EB0"); // Set colour for label text
					$('#formError').append(divError);//$('.validEmail').eq(i).parent().append(divError);
					//$('.validEmail').eq(i).parent().css("color","red");
					$(divError).css({ display:"block",color:"#076EB0", clear:"both"}); // Set colour for error message text	
				}
				 isError=true;// valid.finall()
			}
		}
		return !isError;
	},
	

	
	// Below - Unsure
	
	finall : function()  {
		topOffset = $(".error").eq(0).offset().top;
		$('html,body').animate({scrollTop:topOffset},'slow');
		return false;
	}
}



