$(document).ready(function() {
	//regex email
	//var reg = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	//var reg = /^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/;
	var regPhone = new RegExp(/^0[1-59]([\.|\-|\s]*[0-9]{2}){4}$/);
	var regEmail = new RegExp ( "^\\w[\\w+\.\-]*@[\\w\-]+\.\\w[\\w+\.\-]*\\w$", "gi" ) ;
	var send = new Array();
	var ext = new Array(".docx",".doc",".odt",".pdf");
	var classElement; var actualColor;
	
	//couleur de base de la balise <p> dans form
	/*$("form").ready(function() {
		actualColor = $("form > div > p > label").css("color");
	});*/
	
	function getColor(nameForm) {
		if (nameForm != "rappel") {
			actualColor = $("form > div > p > label").css("color");
		}
		else {
			actualColor = $("form > label").css("color");
		}
	}
	/*
	$("div[class^=cadre] > form[name!=rappel]").ready(function() {
		actualColor = $("form > div > p > label").css("color");
	});
	
	$("div[class^=cadre] > form[name=rappel]").ready(function() {
		if (!$("#formRappel:hidden"))
			actualColor = $("form > label").css("color");
	});
	*/
	
	//focus input
/*	$("div[class^=cadre] :input[type!=submit]").focus(function() {
		var nameForm = $("div[class^=cadre] > form").attr("name");
		i = $(":input[type!=submit]").index(this);
		$("form label:eq("+i+"):has(sup)").css("color", actualColor);
	});
	
	//blur input
	$(":input[type!=submit]").blur( function() {
		var i = $(":input[type!=submit]").index(this);
		if ($(this).val() == '')
		{
			$("form label:eq("+i+"):has(sup)").css("color", "red");
		}
		else
		{
			if ($(this).attr("name") == 'email')
			{
				if (!$(this).val().match(regEmail))
					$("label:eq("+i+")").css("color", "red");
			}
		}
	});
*/	
	function empty(nameForm, i) {
		//$("form[name="+nameForm+"] label:eq("+i+"):has(sup)").css("color", "red");
	}
	
	//submit du formulaire
	$("div[class^=cadre] form[name!=newsletter]").submit(function() {
		var nameForm = $(this).attr("name");
		$("form[name="+nameForm+"] :input[type!=submit]").each(function() {
			var i = $("form[name="+nameForm+"] :input[type!=submit]").index(this);
			if ($(this).val() == '') {
				empty(nameForm, i);
				send[$(this).attr("name")] = 0;
			} else send[$(this).attr("name")] = 1;
			
			if ($(this).attr("name") == 'email') {
				if($(this).val() != '') {
					if(!$(this).val().match(regEmail)) {
						//empty(i);
						send[$(this).attr("name")] = 2;
					} else send[$(this).attr("name")] = 1;
				}
			}
			
			if ($(this).attr("name") == "cv") {
				if($(this).val() != '') {
					var extension = $(this).val().substring($(this).val().lastIndexOf("."));
					for(key in ext)
					{
						if(ext[key] != extension) {
							//empty(i);
							send[$(this).attr("name")] = 2;
						}
						else { 
							send[$(this).attr("name")] = 1;
							break;
						}
					}
				}
			}
		})
		
		
		for(element in send)
		{
			if(element != 'tel2' && element != 'fax')
			{
				if(send[element] == 0)
				{	
					$("div[class^=cadre] > div:eq(0) > p").remove();
					$("div[class^=cadre] > div:eq(0)").append("<p class='form-text-wrong'>Merci de remplir les champs !</p>");
					return false;
				}
			}
		}
		
		if(send["email"] == 2)
		{
			$("div[class^=cadre] > div:eq(0) > p").remove();
			$("div[class^=cadre] > div:eq(0)").append("<p class='form-text-wrong'>Adresse email invalide !</p>");
			return false;
		}
		else if(send["cv"] == 2)
		{
			$("div[class^=cadre] > div:eq(0) > p").remove();
			$("div[class^=cadre] > div:eq(0)").append("<p class='form-text-wrong'>L\'extension du CV est invalide !</p>");
			return false;
		}
	});
	
	//formulaire de rappel
	$("#formRappel > form").submit(function() {
		var dataString = new Array();
		var nameForm = $(this).attr("name");
		$("form[name="+nameForm+"] :input[type!=submit]").each(function() {
			var i = $("#formRappel > form > :input[type!=submit]").index(this);
			if ($(this).val() == '') {
				empty(i);
				send[$(this).attr("name")] = 0;
			} else {
				send[$(this).attr("name")] = 1;
				if ($(this).attr("name") != 'numero')
				{
					dataString.push($(this).val());
				}
			}
			
			if ($(this).attr("name") == 'numero') {
				if(!$(this).val().match(regPhone)) {
					empty(i);
					send[$(this).attr("name")] = 2;
				} else {
					send[$(this).attr("name")] = 1;
					dataString.push($(this).val());
				}
			}
		})
		
		for (element in send)
		{
			if(send[element] == 0)
			{
				$("#formRappel > div:first-child > p").remove();
				$("#formRappel > div:first-child").append("<p class='form-text-wrong'>Merci de remplir les champs !</p>");
				return false;
			}
		}
		
		if (send["numero"] == 2)
		{
			$("#formRappel > div:first-child > p").remove();
			$("#formRappel > div:first-child").append("<p class='form-text-wrong'>Numéro invalide !</p>");
			return false;
		}
		
		$.ajax({
			type:"POST",
			url:'rappel.php',
			data: "value="+dataString[0]+"&value1="+dataString[1]+"&value2="+dataString[2],
			success: function(html) {	$("#formRappel > div:first-child > p").remove();
										$("#formRappel > div:first-child").append(html); 
			}
		});
		return false;
	});

	//effet menu du haut
/*	$(".container-top").find("li").hover(function() {
		$(this).fadeOut("fast");
		$(this).fadeIn("fast");
	});
*/	
	//effet sous-menu
	$("div[class^=menu] > ul:eq(0)").find("a").hover(function() {
		var i = $(this).attr('class').match(/\d$/) - 1;
		var b = $("div[class^=menu] > ul:eq(1)").find("a");
		$(b).eq(i).fadeOut("fast");
		$(b).eq(i).fadeIn("fast");
	}, function() {
		$(this).stop(); 
	});

	
	//partenaire	
	$(".btn-suite").click(function() {
		if ($("#div1").css('display') == 'block')
		{
			$("#div1").hide();
			$("#div2").show();
			
		}
		else if ($("#div1").css('display') == 'none')
		{
			$("#div2").hide();
			$("#div1").show();
		}	
	});
	
	//rappel
	$("#formRappelLink").click(function() {
		$("#formRappel").dialog({
			bgiframe: true,
			autoOpen: false,
			modal: false,
			width: 420,
			height: 320,
			title: "Service disponible du Lundi au Vendredi de 9H à 17H"
		});
		$("#formRappel > form > :input[type!=submit]").val('');
		$("#formRappel > div:first-child > p").remove();
		$("#formRappel").dialog("open");
	});
	
});
