// JavaScript Document

var form_validate_status = true;

$(document).ready(function(){
	
	$('form:has(.validate)').submit(function() {
		form_validate_status = true;
		
		$('#'+$(this).attr("id")+' .validate').each(function(i) {
			
			if($(this).is('textarea')){
				
				alert("Validator doesn't support textarea yet.");
				return false;
				
			}else
			if($(this).is('input')){
				
				switch($(this).attr("type")){
					
					case "checkbox":
						alert("Validator doesn't support checkbox yet.");
						return false;
						break;
					case "radio":
						
						var is_checked = $("input[name='"+$(this).attr("name")+"']:checked").length;
						
						if(!is_checked){
							
							var warning_msg = "Vous devez sélectionner le cours désiré.";
							$(this).parent().parent().append('<span style="position:absolute; color:#F00; padding:3px; margin-left:8px;">'+warning_msg+'</span>');
							
							$("input[name='"+$(this).attr("name")+"']").click(function(){
								$(this).parent().siblings('span').remove();
							});
						}
						
						
						break;
						
					case "select":
						alert("Validator doesn't support select yet.");
						return false;
						break;
						
					case "text":
						
						if(($(this).hasClass("validate_email") && !$(this).attr("value").match(/([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4})/gi)) || $(this).attr("value") == ""){
						
							var warning_msg = "Le champ "+$("label[for='"+$(this).attr("id")+"']").text();
							
							if($(this).attr("value") == ""){
								warning_msg = warning_msg+" est vide.";
							}else{
								warning_msg = warning_msg+" n'est pas valide.";
							}
							$(this).siblings().remove();
							
							$(this).css("outline","2px solid #F00");
							
							$(this).parent().append('<span style="position:absolute; color:#F00; padding:3px;">'+warning_msg+'</span>');
							
							
							$(this).blur(function() {
								
								if(($(this).hasClass("validate_email") && !$(this).attr("value").match(/([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4})/gi)) || $(this).attr("value") == ""){
									
									
									
								}else{
									$(this).css("outline","0px");
									
									$(this).siblings().remove();
								}
								
							});
							
							form_validate_status = false;
							
						}
						break;
					
				}
				
			}
			
		});
		return form_validate_status;
		
	});
	
});

function handleElementFocus(e){
	e.data.element.focus();
}
