// JavaScript Document

function showmessage(id){
	msgGood = "<strong>Vous &ecirc;tes inscrit &agrave; notre infolettre.</strong>"
	//retourne le msg demander 
	if(id == 1){
		msgAlert = msgGood
	}
	if(id == 2){
		msgAlert = "Vous &ecirc;tes d&eacute;j&agrave;ginscrit &agrave; l'infolettre.<br>changer d'adresse &eacute;lectronique et recommencer."
	}
	return msgAlert
}

	//global variable
	var isFirstKeyS
	
function redout(){
	if(isFirstKeyS){
		$("#Email").css("borderColor","#1d204f")
		$("#Email").css("backgroundColor","#FFF")
		isFirstKeyS = false;
	}
}


function validForm(){
	//alert("rentre dans js");
	var Prenom, Nom, Email
	var EmailFilter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	
	Prenom = $("#Prenom").val()
	Nom = $("#Nom").val()
	Email = $("#Email").val()
	//alert(Prenom+" ; "+Nom+" ; "+Email+"; ")
	//Prenom = Prenom.replace(/\;/g,"");
	//Nom =  Nom.replace(/\;/g,"");
	
	//vlaider si le Email est présent
		if(Email ==""){
			//alert("Entrer le Email !")
			$("#Email").focus()
			$("#Email").css("borderColor","Red")
			$("#Email").css("backgroundColor","#FFBFBF")
			//Email.style.borderColor = "Red"
			/*$("#FormWow").html("")
			$("#FormWow").css("display","none")
			$("#badFormWow").html(showmessage(2)).fadeIn("slow")*/
			
			//isFirstKeyS remetre le keypress a false pour changer
			//la couleur du textebox de rouge a gris
			isFirstKeyS = true;
			
		}
		
		if (Email != "") {
			// doit etre du bon forma 
			if(!(EmailFilter.test(Email))){
				//alert("Veuillez indiquer une adresse électronique valide pour votre entreprise \n(ex.: smith@msn.com).");
				$("#Email").focus()
				$("#Email").css("borderColor","Red")
				$("#Email").css("backgroundColor","#FFBFBF")
				//alert("*Veuillez indiquer une adresse électronique valide \n(ex.: smith@msn.com).")
				/*$("#FormWow").html("")
				$("#FormWow").css("display","none")
				$("#badFormWow").html(showmessage(3)).fadeIn("slow")*/
				//isFirstKeyS remetre le keypress a false pour changer
				//la couleur du textebox de rouge a gris
				isFirstKeyS = true;
				
			}else{
				//tout a belle et vbien passer on vas faire une call ajax 
				//les variable ajax
				fName = $("#Prenom").val()
				lName = $("#Nom").val()
				mail = $("#Email").val()
				fName =  fName.replace(/\;/g,"");
				lName =  lName.replace(/\;/g,"");
				mail =  mail.replace(/\;/g,"");
				//alert("hey you")
				// do some ajax :)
				$.ajax({
					type: "post",
					//a quelle page je submit le formulaire 
					url: "traitInfoLettre.asp",
					dataType: "html",
					data: "Prenom="+$.URLEncode(fName)+"&Nom="+$.URLEncode(lName)+"&Email="+mail+"",
					
					success: function(isGood){
					
						isGoodTab = isGood.split(";")
						
						isGood = parseInt(isGoodTab[0])
					//si isGood = true nous avions bien inscrit la personne dans la BD
						if(isGood){
							//affiche message comme quoi tout est correct
							$("#Prenom").val("")
							$("#Nom").val("")
							$("#Email").val("")
							//alert("i am good !")
							$("#FormAS").css("display", "none").fadeOut("slow")
								$("#FormASGood").css("display", "block").fadeIn("slow")
							if(isGoodTab[1] == "null" || isGoodTab[2] == "null"){
								
								$("#FormASmsg").html(showmessage(1))
							}else{
								$("#FormASmsg").html("<strong>"+isGoodTab[1]+" "+isGoodTab[2]+"</strong><br>"+showmessage(1))
							}
							
							
							
						}else{
							
							$("#Email").focus()
							$("#Email").css("borderColor","Red")
							$("#Email").css("backgroundColor","#FFBFBF")
							//isFirstKeyS remetre le keypress a false pour changer
							//la couleur du textebox de rouge a gris
							isFirstKeyS = true;
						}
					//
					
					
					}//callbck
				});//AJAX
				//alert("good to go!")
				
				
			}
		}
	
	
	
}



