
 
isValidDate = function (d,type) {
	var sd = d.split('/');
	if (sd.length!=3||isNaN(sd[0])||isNaN(sd[1])||isNaN(sd[2])) { return false;}
	if(sd[2]<100) { return false; }
	var cd = new Date(sd[2],(sd[1]-1),sd[0]);
	if ((sd[2]!=cd.getFullYear())||((sd[1]-1)!=cd.getMonth())||(cd.getDate()!=sd[0])) { return false; }
	if (type!=null )  {
		var nd = new Date();
		if (type==false&&nd<cd){ return false; }
	}                                              
	return true;
}
	
hasMinAge = function (d,age) {
	var d = d.split("/");
	var myDate = new Date(Number(d[2])+age,d[1]-1,d[0]);
	var today = new Date();
	return ((today.getTime()-myDate.getTime())<0) ?false :true;
}

isValidEmail = function (email) {
	var reg = /^[A-Za-z0-9._-]+@[A-Za-z0-9.-]{2,}[.][A-Za-z]{2,3}$/;
 		return (reg.exec(email) != null);
}

validateCreateProfile = function(isIdentified) {
	
	var civilityErrorMsg = "Veuillez saisir votre civilit&eacute;";
	var firstnameErrorMsg = "Veuillez saisir votre pr&eacute;nom";
	var nameErrorMsg = "Veuillez saisir votre nom";
	var emptyBirthDateErrorMsg = "Merci de saisir votre date de naissance";
	var invalidBirthDateErrorMsg = "Date de naissance incorrecte";
	var tooLessBirthDateErrorMsg = "Pour vous inscrire sur Monoprix.com vous devez avoir plus de 18 ans.";
	var emptyPhoneErrorMsg = "Veuillez saisir votre num&eacute;ro de t&eacute;l&eacute;phone";
	var invalidPhoneErrorMsg = "Num&eacute;ro de t&eacute;l&eacute;phone incorrect";
	var emptyEmailSponsorMsg = "Veuillez saisir l'adresse e-mail de votre parrain";
	var invalidEmailSponsorMsg = "Adresse e-mail de votre parrain incorrecte";
	var emptyEmailMsg = "Veuillez saisir votre adresse e-mail";
	var invalidEmailMsg = "Adresse e-mail incorrecte";
	var emptyConfirmEmailMsg = "Veuillez resaisir votre adresse e-mail";
	var notEqualsEmailsMsg = "L'adresse email et la confirmation d'adresse email doivent &ecirc;tre identiques";
	var emptyPasswordMsg = "Veuillez saisir votre mot de passe";
	var invalidPasswordMsg = "Mot de passe incorrect\nVotre mot de passe doit comporter 6 caract&egrave;res minimum";
	var emptyConfirmPasswordMsg = "Veuillez resaisir votre mot de passe";
	var notEqualsPasswordsMsg = "Le mot de passe et la confirmation du mot de passe doivent &ecirc;tre identiques";
	
	
	
	
	
	var formulaireValide = true;
	
	mme = document.getElementById("selectedCivilityMme").checked;
	melle = document.getElementById("selectedCivilityMelle").checked;
	mr = document.getElementById("selectedCivilityMr").checked;
	
	var firstname = document.getElementById("firstnameField").value;
	
	var name = document.getElementById("nameField").value;
	
	var dayBorn = document.getElementById("dayBornField").value;
	var monthBorn = document.getElementById("monthBornField").value;
	var yearBorn = document.getElementById("yearBornField").value;
	
	var birth = dayBorn + "/" + monthBorn + "/" + yearBorn;
	
	var phoneNumber = document.getElementById("phoneNumberField").value;
	var phoneNumber2 = document.getElementById("phoneNumber2Field").value;
	
	var sponsorIsSelected = null;
	if (document.getElementById("selectedIsSponsoredYes") != null)
		sponsorIsSelected = document.getElementById("selectedIsSponsoredYes").checked;
	
	var emailSponsor = null;
	if (document.getElementById("emailSponsorField")!= null)
		emailSponsor = document.getElementById("emailSponsorField").value;
	
	var email = document.getElementById("emailField").value;
	
	var confirmEmail = document.getElementById("confirmEmailField").value;
	
	var password = document.getElementById("passwordField").value;
	
	var confirmPassword = document.getElementById("confirmPasswordField").value;
	
	
	/*
	telephone=document.getElementById("telephone1").value;
	objet=document.getElementById("objet").value;
	*/
	
	
	if (!mme && !melle && !mr)
	{
		formulaireValide = false;
		document.getElementById("civilityError").innerHTML = civilityErrorMsg;
		$j("#civilityError").show();
	}
	else
	{
		document.getElementById("civilityError").innerHTML = "";
		$j("#civilityError").hide();
	}
	
	if (firstname == "")
	{
		formulaireValide = false;
		document.getElementById("firstnameError").innerHTML = firstnameErrorMsg;
		$j("#firstnameError").show();
	}
	else
	{
		document.getElementById("firstnameError").innerHTML = "";
		$j("#firstnameError").hide();
	}
	
	if (name == "")
	{
		formulaireValide = false;
		document.getElementById("nameError").innerHTML = nameErrorMsg;
		$j("#nameError").show();
	}
	else
	{
		document.getElementById("nameError").innerHTML = "";
		$j("#nameError").hide();
	}
	
	if (dayBorn == "" || monthBorn == "" || yearBorn == "") {
		formulaireValide = false;
		document.getElementById("bornError").innerHTML = emptyBirthDateErrorMsg;
		$j("#bornError").show();
	}
	else if (!this.isValidDate(birth)) {
		formulaireValide = false;
		document.getElementById("bornError").innerHTML = invalidBirthDateErrorMsg;
		$j("#bornError").show();
	}
	else if (!this.hasMinAge(birth, 18)) {
		formulaireValide = false;
		document.getElementById("bornError").innerHTML = tooLessBirthDateErrorMsg;
		$j("#bornError").show();
	}
	else
	{
		document.getElementById("bornError").innerHTML = "";
		$j("#bornError").hide();
	}
	
	
	if (phoneNumber == "")
	{
		formulaireValide = false;
		document.getElementById("phoneError").innerHTML = emptyPhoneErrorMsg;
		$j("#phoneError").show();
	}
	else
	{
		var reg = /^0[0-9]{9}$/;
   		if (reg.exec(phoneNumber) != null)
   		{
			// le téléphone n°1 est OK, on vérifie maintenant la validité du téléphone n°2 s'il est saisi
			if ((phoneNumber2 != "") && (reg.exec(phoneNumber2) == null)) {
		   		formulaireValide = false;
				document.getElementById("phoneError").innerHTML = invalidPhoneErrorMsg;
				$j("#phoneError").show();
			}
	   		else
	   		{
				document.getElementById("phoneError").innerHTML = "";
				$j("#phoneError").hide();
	   		}
   		}
   		else
   		{
   			formulaireValide = false;
			document.getElementById("phoneError").innerHTML = invalidPhoneErrorMsg;
			$j("#phoneError").show();
   		}
	}
	
	if (sponsorIsSelected != null) {	
		if (sponsorIsSelected && emailSponsor == "") {
			formulaireValide = false;
			document.getElementById("emailSponsorError").innerHTML = emptyEmailSponsorMsg;
			$j("#emailSponsorError").show();
		}
		else if (sponsorIsSelected && !this.isValidEmail(emailSponsor)) {
			formulaireValide = false;
			document.getElementById("emailSponsorError").innerHTML = invalidEmailSponsorMsg;
			$j("#emailSponsorError").show();
		}
		else
		{
			document.getElementById("emailSponsorError").innerHTML = "";
			$j("#emailSponsorError").hide();
		}
	}
	
	if (emailSponsor != null) {
		if (email == "") {
			formulaireValide = false;
			document.getElementById("emailError").innerHTML = emptyEmailMsg;
			$j("#emailError").show();
		}
		else if (!this.isValidEmail(email)) {
			formulaireValide = false;
			document.getElementById("emailError").innerHTML = invalidEmailMsg;
			$j("#emailError").show();
		}
		else
		{
			document.getElementById("emailError").innerHTML = "";
			$j("#emailError").hide();
		}
	}
	
	if (confirmEmail == "") {
		formulaireValide = false;
		document.getElementById("confirmEmailError").innerHTML = emptyConfirmEmailMsg;
		$j("#confirmEmailError").show();
	}
	else if (!this.isValidEmail(confirmEmail)) {
		formulaireValide = false;
		document.getElementById("confirmEmailError").innerHTML = invalidEmailMsg;
		$j("#confirmEmailError").show();
	}
	else if (email != confirmEmail) {
		formulaireValide = false;
		document.getElementById("confirmEmailError").innerHTML = notEqualsEmailsMsg;
		$j("#confirmEmailError").show();
	}
	else
	{
		document.getElementById("confirmEmailError").innerHTML = "";
		$j("#confirmEmailError").hide();
	}
	
	

	if (password == "") {
		if (isIdentified == "false") {
			formulaireValide = false;
			document.getElementById("passwordError").innerHTML = emptyPasswordMsg;
			$j("#passwordError").show();
		}
	}
	else if (password.length < 6) {
		if (isIdentified == "false") {
			formulaireValide = false;
			document.getElementById("passwordError").innerHTML = invalidPasswordMsg;
			$j("#passwordError").show();
		}
	}
	else
	{
		document.getElementById("passwordError").innerHTML = "";
		$j("#passwordError").hide();
	}
	
	if (confirmPassword == "") {
		if (isIdentified == "false") {
			formulaireValide = false;
			document.getElementById("confirmPasswordError").innerHTML = emptyConfirmPasswordMsg;
			$j("#confirmPasswordError").show();
		}
	}
	else if (confirmPassword.length < 6) {
		if (isIdentified == "false") {
			formulaireValide = false;
			document.getElementById("confirmPasswordError").innerHTML = invalidPasswordMsg;
			$j("#confirmPasswordError").show();
		}
	}
	else if (password != confirmPassword) {
		formulaireValide = false;
		document.getElementById("confirmPasswordError").innerHTML = notEqualsPasswordsMsg;
		$j("#confirmPasswordError").show();
	}
	else
	{
		document.getElementById("confirmPasswordError").innerHTML = "";
		$j("#confirmPasswordError").hide();
	}
	
		
	if (formulaireValide)
	{
		document.getElementById("validateProfileForm").submit();
	}
}


validateCreateAddress = function() {

	var addressErrorMsg = "Veuillez saisir votre adresse de livraison";
	var zipCodeEmptyErrorMsg = "Veuillez saisir votre code postal";
	var zipCodeInvalidErrorMsg = "Votre code postal est invalide";
	var townErrorMsg = "Veuillez saisir votre ville de livraison";
	var homeTypeErrorMsg = "Veuillez choisir Maison ou Appartement";
	
	var formulaireValide = true;
	
	var address = document.getElementById("addressField").value;
	var zipCode = null;
	if (document.getElementById("zipCodeField") != null)
		zipCode = document.getElementById("zipCodeField").value;
	var town = document.getElementById("townField").value;
	var home = document.getElementById("selectedHomeTypeMaison").checked;
	var flat = document.getElementById("selectedHomeTypeAppartement").checked;
	
	
	if (address == "")
	{
		formulaireValide = false;
		document.getElementById("addressError").innerHTML = addressErrorMsg;
		$j("#addressError").show();
	}
	else
	{
		document.getElementById("addressError").innerHTML = "";
		$j("#addressError").hide();
	}
	
	if (zipCode != null) {
		if (zipCode == "")
		{
			formulaireValide = false;
			document.getElementById("zipCodeError").innerHTML = zipCodeEmptyErrorMsg;
			$j("#zipCodeError").show();
		}
		else
		{	var reg = /^[0-9]{5}$/;
	   		if (reg.exec(zipCode) == null) {
	   			formulaireValide = false;
				document.getElementById("zipCodeError").innerHTML = zipCodeInvalidErrorMsg;
				$j("#zipCodeError").show();
	   		}
	   		else
	   		{
				document.getElementById("zipCodeError").innerHTML = "";
				$j("#zipCodeError").hide();
			}
		}
	}
	
	if (town == "")
	{
		formulaireValide = false;
		document.getElementById("townError").innerHTML = townErrorMsg;
		$j("#townError").show();
	}
	else
	{
		document.getElementById("townError").innerHTML = "";
		$j("#townError").hide();
	}
	
	if (!home && !flat)
	{
		formulaireValide = false;
		document.getElementById("homeTypeError").innerHTML = homeTypeErrorMsg;
		$j("#homeTypeError").show();
	}
	else
	{
		document.getElementById("homeTypeError").innerHTML = "";
		$j("#homeTypeError").hide();
	}
	
	if (formulaireValide)
	{
		document.getElementById("validateAddressForm").submit();
	}
}

validateCreateLoyalty = function() {

	var emptyLoyaltyCardNumberErrorMsg = "Veuillez saisir votre num&eacute;ro de carte";
	var invalidLoyaltyCardNumberErrorMsg = "Num&eacute;ro de carte incorrect";
	var emptyBirthDateLoyaltyErrorMsg = "Veuillez saisir la date de naissance du porteur de la carte";
	var invalidBirthDateLoyaltyErrorMsg = "Date de naissance du porteur de la carte incorrecte";
	
	var formulaireValide = true;

	var loyaltyIsSelected = document.getElementById("choiceLoyaltyYes").checked;
	var cardNumber = document.getElementById("cardNumberField").value;
	var dayBornLoyalty = document.getElementById("dayBornLoyaltyField").value;
	var monthBornLoyalty = document.getElementById("monthBornLoyaltyField").value;
	var yearBornLoyalty = document.getElementById("yearBornLoyaltyField").value;
	var birthLoyalty = dayBornLoyalty + "/" + monthBornLoyalty + "/" + yearBornLoyalty;
	
	if (loyaltyIsSelected && cardNumber == "") {
		formulaireValide = false;
		document.getElementById("cardNumberError").innerHTML = emptyLoyaltyCardNumberErrorMsg;
		$j("#cardNumberError").show();
	}
	else if (loyaltyIsSelected && cardNumber.length < 17) {
		formulaireValide = false;
		document.getElementById("cardNumberError").innerHTML = invalidLoyaltyCardNumberErrorMsg;
		$j("#cardNumberError").show();
	}
	else
	{
		document.getElementById("cardNumberError").innerHTML = "";
		$j("#cardNumberError").hide();
	}
	
	if (loyaltyIsSelected && (dayBornLoyalty == "" || monthBornLoyalty == "" || yearBornLoyalty == "")) {
		formulaireValide = false;
		document.getElementById("bornLoyaltyError").innerHTML = emptyBirthDateLoyaltyErrorMsg;
		$j("#bornLoyaltyError").show();
	}
	else if (loyaltyIsSelected && !this.isValidDate(birthLoyalty)) {
		formulaireValide = false;
		document.getElementById("bornLoyaltyError").innerHTML = invalidBirthDateLoyaltyErrorMsg;
		$j("#bornLoyaltyError").show();
	}
	else
	{
		document.getElementById("bornLoyaltyError").innerHTML = "";
		$j("#bornLoyaltyError").hide();
	}
	
	if (formulaireValide)
	{
		document.getElementById("validateLoyaltyForm").submit();
	}
}

validateRegister = function() {
	document.getElementById("validateRegisterForm").submit();
}

addPhone = function() {
	document.getElementById("Telephone2").style.display = "block";
	document.getElementById("addPhoneLink").style.display = "none";
}

addAddress = function() {
	document.getElementById("Address2").style.display = "block";
	document.getElementById("addAddressLink").style.display = "none";
}

addDigicode = function() {
	if (document.getElementById("Digicode2").style.display != "block")
		document.getElementById("Digicode2").style.display = "block";
	else {
		document.getElementById("Digicode3").style.display = "block";
		document.getElementById("addDigicodeLink").style.display = "none";
	}
}



/* Page Sponsorship */
validateSponsoring = function() {
	var invalidEmailErrorMsg = "Adresse e-mail de votre ami incorrecte";
	
	var formulaireValide = true;

	var email1 = document.getElementById("email1Field").value;
	var email2 = document.getElementById("email2Field").value;
	var email3 = document.getElementById("email3Field").value;
	var email4 = document.getElementById("email4Field").value;
	var email5 = document.getElementById("email5Field").value;
	var email6 = document.getElementById("email6Field").value;
	var email7 = document.getElementById("email7Field").value;
	var email8 = document.getElementById("email8Field").value;
	var email9 = document.getElementById("email9Field").value;
	var email10 = document.getElementById("email10Field").value;
	
	if (email1 != "" && !this.isValidEmail(email1)) {
		formulaireValide = false;
		document.getElementById("email1Error").innerHTML = invalidEmailErrorMsg;
		$j("#email1Error").show();
	}
	else
	{
		document.getElementById("email1Error").innerHTML = "";
		$j("#email1Error").hide();
	}
	
	if (email2 != "" && !this.isValidEmail(email2)) {
		formulaireValide = false;
		document.getElementById("email2Error").innerHTML = invalidEmailErrorMsg;
		$j("#email2Error").show();
	}
	else
	{
		document.getElementById("email2Error").innerHTML = "";
		$j("#email2Error").hide();
	}
	
	if (email3 != "" && !this.isValidEmail(email3)) {
		formulaireValide = false;
		document.getElementById("email3Error").innerHTML = invalidEmailErrorMsg;
		$j("#email3Error").show();
	}
	else
	{
		document.getElementById("email3Error").innerHTML = "";
		$j("#email3Error").hide();
	}
	
	if (email4 != "" && !this.isValidEmail(email4)) {
		formulaireValide = false;
		document.getElementById("email4Error").innerHTML = invalidEmailErrorMsg;
		$j("#email4Error").show();
	}
	else
	{
		document.getElementById("email4Error").innerHTML = "";
		$j("#email4Error").hide();
	}
	
	if (email5 != "" && !this.isValidEmail(email5)) {
		formulaireValide = false;
		document.getElementById("email5Error").innerHTML = invalidEmailErrorMsg;
		$j("#email5Error").show();
	}
	else
	{
		document.getElementById("email5Error").innerHTML = "";
		$j("#email5Error").hide();
	}
	
	if (email6 != "" && !this.isValidEmail(email6)) {
		formulaireValide = false;
		document.getElementById("email6Error").innerHTML = invalidEmailErrorMsg;
		$j("#email6Error").show();
	}
	else
	{
		document.getElementById("email6Error").innerHTML = "";
		$j("#email6Error").hide();
	}
	
	if (email7 != "" && !this.isValidEmail(email7)) {
		formulaireValide = false;
		document.getElementById("email7Error").innerHTML = invalidEmailErrorMsg;
		$j("#email7Error").show();
	}
	else
	{
		document.getElementById("email7Error").innerHTML = "";
		$j("#email7Error").hide();
	}
	
	if (email8 != "" && !this.isValidEmail(email8)) {
		formulaireValide = false;
		document.getElementById("email8Error").innerHTML = invalidEmailErrorMsg;
		$j("#email8Error").show();
	}
	else
	{
		document.getElementById("email8Error").innerHTML = "";
		$j("#email8Error").hide();
	}
	
	if (email9 != "" && !this.isValidEmail(email9)) {
		formulaireValide = false;
		document.getElementById("email9Error").innerHTML = invalidEmailErrorMsg;
		$j("#email9Error").show();
	}
	else
	{
		document.getElementById("email9Error").innerHTML = "";
		$j("#email9Error").hide();
	}
	
	if (email10 != "" && !this.isValidEmail(email10)) {
		formulaireValide = false;
		document.getElementById("email10Error").innerHTML = invalidEmailErrorMsg;
		$j("#email10Error").show();
	}
	else
	{
		document.getElementById("email10Error").innerHTML = "";
		$j("#email10Error").hide();
	}
	
	if (formulaireValide)
	{
		document.getElementById("sponsorshipForm").submit();
	}
	
}


addGodsonEmail = function() {
	var nbSponsoringAllowed = document.getElementById("nbSponsoringAllowedField").value;
	
	// on rend visible la 1ère zone texte actuellement invisible de saisie d'email filleul
	if (document.getElementById("godsonEmail4").style.display != "block")
		document.getElementById("godsonEmail4").style.display = "block";
	else if (document.getElementById("godsonEmail5").style.display != "block")
		document.getElementById("godsonEmail5").style.display = "block";
	else if (document.getElementById("godsonEmail6").style.display != "block")
		document.getElementById("godsonEmail6").style.display = "block";
	else if (document.getElementById("godsonEmail7").style.display != "block")
		document.getElementById("godsonEmail7").style.display = "block";
	else if (document.getElementById("godsonEmail8").style.display != "block")
		document.getElementById("godsonEmail8").style.display = "block";
	else if (document.getElementById("godsonEmail9").style.display != "block")
		document.getElementById("godsonEmail9").style.display = "block";
	else
		document.getElementById("godsonEmail10").style.display = "block";
	
	// on compte le nombre de zones texte visibles pour la saisie d'emails filleuls supplémentaires
	var cptInputSuppl = 0;
	if (document.getElementById("godsonEmail4").style.display == "block")
		cptInputSuppl++;
	if (document.getElementById("godsonEmail5").style.display == "block")
		cptInputSuppl++;
	if (document.getElementById("godsonEmail6").style.display == "block")
		cptInputSuppl++;
	if (document.getElementById("godsonEmail7").style.display == "block")
		cptInputSuppl++;
	if (document.getElementById("godsonEmail8").style.display == "block")
		cptInputSuppl++;
	if (document.getElementById("godsonEmail9").style.display == "block")
		cptInputSuppl++;
	if (document.getElementById("godsonEmail10").style.display == "block")
		cptInputSuppl++;
	
	// si on a atteint la limite de parrainages autorisés, on rend invisible le lien d'ajout supplémentaire
	if (nbSponsoringAllowed == (3 + cptInputSuppl))
			document.getElementById("addGodsonEmailLink").style.display = "none";
}


/* Page LoyaltyCard */
updateLoyalty = function() {
	document.getElementById("updateLoyaltyInfosTitle").style.display = "block";
	document.getElementById("updateLoyaltyInfos").style.display = "block";
}

cancelUpdateLoyalty = function() {
	document.getElementById("updateLoyaltyInfosTitle").style.display = "none";
	document.getElementById("updateLoyaltyInfos").style.display = "none";
}

confirmUpdateLoyalty = function() {
	validateLoyalty("updateCardNumberField", "updateDayBornField", "updateMonthBornField", "updateYearBornField", "updateCardNumberError", "updateBornError", "validateUpdateLoyaltyForm");
}

createLoyalty = function() {
	validateLoyalty("cardNumberField", "dayBornField", "monthBornField", "yearBornField", "cardNumberError", "bornError", "validateLoyaltyForm");
}

validateLoyalty = function(cardNumberFieldId, dayBornFieldId, monthBornFieldId, yearBornFieldId, cardNumberErrorDivId, bornErrorDivId, formId) {
	var emptyCardNumberErrorMsg = "Merci de saisir votre num&eacute;ro de carte";
	var invalidCardNumberErrorMsg = "Num&eacute;ro de carte incorrect";
	var emptyBirthDateErrorMsg = "Merci de saisir votre date de naissance";
	var invalidBirthDateErrorMsg = "Date de naissance incorrecte";
	
	var formulaireValide = true;
	
	var cardNumber = document.getElementById(cardNumberFieldId).value;
	
	var dayBorn = document.getElementById(dayBornFieldId).value;
	var monthBorn = document.getElementById(monthBornFieldId).value;
	var yearBorn = document.getElementById(yearBornFieldId).value;
	
	var birth = dayBorn + "/" + monthBorn + "/" + yearBorn;
	
	if (cardNumber == "") {
		formulaireValide = false;
		document.getElementById(cardNumberErrorDivId).innerHTML = emptyCardNumberErrorMsg;
		$j("#" + cardNumberErrorDivId).show();
	}
	else if (cardNumber.length < 17) {
		formulaireValide = false;
		document.getElementById(cardNumberErrorDivId).innerHTML = invalidCardNumberErrorMsg;
		$j("#" + cardNumberErrorDivId).show();
	}
	else
	{
		document.getElementById(cardNumberErrorDivId).innerHTML = "";
		$j("#" + cardNumberErrorDivId).hide();
	}
	
	if (dayBorn == "" || monthBorn == "" || yearBorn == "") {
		formulaireValide = false;
		document.getElementById(bornErrorDivId).innerHTML = emptyBirthDateErrorMsg;
		$j("#" + bornErrorDivId).show();
	}
	else if (!this.isValidDate(birth)) {
		formulaireValide = false;
		document.getElementById(bornErrorDivId).innerHTML = invalidBirthDateErrorMsg;
		$j("#" + bornErrorDivId).show();
	}
	else
	{
		document.getElementById(bornErrorDivId).innerHTML = "";
		$j("#" + bornErrorDivId).hide();
	}
	
	if (formulaireValide)
	{
		document.getElementById(formId).submit();
	}
}

