// JavaScript Document
$(document).ready(function(){
	
	$("#project_form_submit").click(function(){
		var valide = validate("project_form");
		if (valide){
		
			var project_infolettre = ($("#project_infolettre").is(':checked')) ? '1' : '0';
			
			$.ajax({
				type: "POST",
				data:{flag:'submit', entreprise:$("#project_entreprise").val(), contact:$("#project_contact").val(), phone:$("#project_phone").val(), email:$("#project_email").val(), description:$("#project_description").val(), infolettre:project_infolettre},
				url: $("#project_form").attr("action"),
				success: function(html) {
					$("#message_wrapper").fadeIn(400);
					$("#project_form").fadeOut(100);
					//alert(html);
				}
			});
		}
	});
	
	function isEmail(email) {
		return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/.test(email));
	}
	
	function isEmpty(s){
		return ((s == null) || (s.length == 0));
	}
	
	function validate(form_name){
	
		var errors = new Array();
		$("#errors_list").html("");
		$("#errors_wrapper").fadeOut(100);
		$("#message_wrapper").fadeOut(100);
		$("#"+form_name+" label").css("color","#000");
	
		$("#"+form_name+" input[req='true'], #"+form_name+" textarea").each(function(){
			var $valtype = $(this).attr("valtype");
			var $label = $(this).prev();
			var $error_msg = $(this).attr('error_msg');
	
			if($valtype == "email"){
				if(!isEmail($(this).val())){
					errors.push($error_msg);
					$label.css("color","#990000");
				}
			}else if($valtype == "notempty"){
				if(isEmpty($(this).val())){
					errors.push($error_msg);
					$label.css("color","#990000");
				}
			}
		})
		
		if(errors.length > 0){
			for (key in errors) $("#errors_list").append("<li>"+errors[key]+"</li>");
			$("#errors_wrapper").fadeIn(400);			
			return false;
		}
		return true;
		
	}
	
	
	
	
});
