$(function() {

	$('.jj').focus(function(){
		var jour = $('#jours').val(); 
		var mois = $('#mois').val();
		var annee = $('#annee').val();
		if(isNaN(jour)){
			$('#jours').val('');
		}
		if(isNaN(mois)){
			$('#mois').val('');
		}
		if(isNaN(annee)){
			$('#annee').val('');
		}
	});

   	$('.pop-inscription').click ( function () {
		$('#inscripiton').oLightBox();
		return false;
	});

	$('#recupMdp').click ( function () {
		$('#ident-erreur')[0]._closeLightBox();
		$('#ident-recup').oLightBox();
		return false;
	});

	$('#recupMdpLeft').click ( function () {
		$('#ident-recup').oLightBox();
		return false;
	});
	$('#fermer3').click ( function () {
		$('#contactnonlogge')[0]._closeLightBox();
		return false;
	});

	$('.contactConfirmation').submit ( function () {
		$('#contact')[0]._closeLightBox();
		//$('.contactConfirm').oLightBox();
		return false;
	});
	$('.pop-credit').click ( function () {
		$('#credit').oLightBox();
		return false;
	});
	$('.pop-cgu').click ( function () {
		$('#cgu').oLightBox();
		return false;
	});
	$('.pop-contact').click ( function () {
		$('#contact').oLightBox();
		$('#sujetContact').val(0);
		$('#messageContact').text('');
		$('#sujetContact').css('border', 'none');
		$('#messageContact').css('border', 'none');
		$('.larg-5').css('visibility','visible');
		$('#categorie').removeAttr('style');
		$('#activite').removeAttr('style');
		return false;
	});
	$('.pop-contactnonlogge').click ( function () {
		$('#contactnonlogge').oLightBox();
		//$('.larg-5').css('visibility','visible');
		return false;
	});	
	$('#cliqueLogge').click ( function () {
		$('#contactnonlogge')[0]._closeLightBox();
		$('#inscripiton').oLightBox();
		$('#categorie').removeAttr('style');
		$('#activite').removeAttr('style');
		return false;
	});

	$('.fermerContact').click ( function () {
		$('.contactConfirm')[0]._closeLightBox();
		return false;
	});
	$('.fermerCgu').click ( function () {
		$('#cgu')[0]._closeLightBox();
		return false;
	});
	$('.fermerCredit').click ( function () {
		$('#credit')[0]._closeLightBox();
		return false;
	});
	$('.fermerNonLogge').click ( function () {
		//$('.confirmation')[0]._closeLightBox();
		return false;
	});

	$('.submit').click(function(){
		$('#inscriptionReghalal').trigger('submit');
		return false;
	});

	//Inscription
	$('#inscriptionReghalal').submit( function () {
		$('.errorMessage').html('').hide();
		
		if(!tmt_validateForm(this)) {
			return false;
		}
			var mr = document.getElementById('civilite-1');
			var mme = document.getElementById('civilite-2');
			var mlle = document.getElementById('civilite-3');
			var femme = document.getElementById('femme');
			var homme = document.getElementById('homme');
			
			if(mr.checked==true){
				var civilite = mr.value;
			}
			if(mme.checked==true){
				var civilite = mme.value;
			}
			if(mlle.checked==true){
				var civilite = mlle.value;
			}
			if(homme.checked==true){
				var sexe = homme.value;
			}
			if(femme.checked==true){
				var sexe = femme.value;
			}
		$.ajax	({
			url:$('#urlCreateUserFo').val(),
			type: "post",
			data: {
					civilite: civilite, 
					nom: $('[@name=nom]').val(), 
					prenom: $('[@name=prenom]').val(), 
					sexe: sexe, 
					jours: $('[@name=jours]').val(), 
					mois: $('[@name=mois]').val(), 
					annee: $('[@name=annee]').val(), 
					cp: $('[@name=cp]').val(), 
					email: $('[@name=email]').val(), 
					confEmail: $('[@name=confEmail]').val(), 
					mdpasse: $('[@name=mdpasse]').val(), 
					email: $('[@name=email]').val(), 
					nbEnfant: $('[@name=nb-enfant]').val(), 
					categorie: $('[@name=categorie]').val(), 
					activite: $('[@name=activite]').val(), 
					condition: $('[@name=condition]').val(), 						
					infos: ($('[@name=infos]').get(0).checked==true)?1:0
				  },
			success: function (_zRespData){
				if (_zRespData == "true"){
					$('#inscripiton')[0]._closeLightBox();
					$('.confirmation').oLightBox({ callClose : returnRedirect });
				}else{
					$('#inscripiton')[0]._closeLightBox();
					$('.erreur').oLightBox({ callClose : returnRegister });
					$('.errorMessage').empty();
					$('.errorMessage').html(_zRespData).show();
				}
			},
			async: false
		}) ;

		return false;
	});
	//Fin Inscription

	//Contact
	$('#contactReghalal').submit( function () {
		$('.errorMessage').html('').hide();
		
		if(!tmt_validateForm(this)) {
			return false;
		}

		$.ajax	({
			url:$('#urlContactFo').val(),
			type: "post",
			data: {
				sujet: $('[@name=sujetContact]').val(), 
				message: $('[@name=messageContact]').val()
			},
			success: function (_zRespData){
				if (_zRespData == "true"){
					$('#contact')[0]._closeLightBox();
					$('.contactConfirm').oLightBox();
				}
			},
			async: false
		}) ;

		return false;
	});
	//Fin contact

	//Recuperation mot de passe oublié
	$('#validerIdentRecup').click (	function (){
		$('#ident-recup-email').removeClass('invalid');
		var email = $('#ident-recup-email').val();
		if (email == 'Votre adresse e-mail'){
			$('#ident-recup-email').addClass('invalid');
			$('.errorMessageIdentRecup').html('Veuillez renseigner votre adresse email').show();
		}else{
			$('.errorMessageIdentRecup').empty();
			if (email.search(/^[a-z0-9._-]+@[a-z0-9._-]{2,}[.][a-z]{2,3}$/) == -1){
				$('#ident-recup-email').addClass('invalid');
				$('.errorMessageIdentRecup').html('Veuillez saisir correctement votre adresse email').show();
			}else{
				$('.errorMessageIdentRecup').empty();
				$.ajax	({
					url:$('#ident-recup-url').val(),
					type: "post",
					data: {
							email: email
						  },
					success: function (_zRespData){
						if (_zRespData == "true"){
							$('#ident-recup')[0]._closeLightBox();
							$('.confirmationMdpOublie').oLightBox();
							$('.confirmationMdpOublie').fadeOut(3000, function (){
								$('.confirmationMdpOublie')[0]._closeLightBox();								
							});
						}else{
							var zHtmlErreur = "Cette adresse ne correspond pas à un compte existant.<br /><br />Merci de vérifier votre adresse ou de vous inscrire au club Reghalal";
							$('#ident-recup')[0]._closeLightBox();
							$('.erreurMdpOublie').oLightBox({ callClose : returnRegisterMdpOublir });
							$('.errorMessage').empty();
							$('.errorMessage').html(zHtmlErreur).show();
						}
					},
					async: false
				}) ;

			}
		}
		return false;
	});
	//Fin recuperation mot de passe oublié

	$('.fermer').click ( function () {
		$('.confirmation')[0]._closeLightBox();
		return false;
	});

	$('#lien-cgu').click(function(){
		$('#inscripiton')[0]._closeLightBox();
		$('#cgu').oLightBox({ callClose : returnRegister });
	});

	//Vote recette	
	$('.pop-voter').click ( function () {
		if ($('#bNote').val() == 0){
			$('#voter').oLightBox();
		}else{
			$('#popConfirmDejaNoter').oLightBox();
		}
		return false;
	}); 


	$('.effacer').click (
		function (){
			$("input[name=noteVal]").val(0);
			$('#image_0').attr('src', j_basepath + 'design/front/images/design/star-vote-gris.gif');
			$('#image_1').attr('src', j_basepath + 'design/front/images/design/star-vote-gris.gif');
			$('#image_2').attr('src', j_basepath + 'design/front/images/design/star-vote-gris.gif');
			$('#image_3').attr('src', j_basepath + 'design/front/images/design/star-vote-gris.gif');
			$('#image_4').attr('src', j_basepath + 'design/front/images/design/star-vote-gris.gif');
			return false;
		}	
	);

	$('.fermerVote').click ( function () {
		$('#voter')[0]._closeLightBox();
		return false;
	}); 

	$(".contentConfirm img").click(function() {
			var rang = parseInt($(this).attr("id").split("_")[1]);
			var val = rang + 1;
			var imgJaune = j_basepath+"design/front/images/design/star-vote-jaune.gif";
			var imgGris = j_basepath+"design/front/images/design/star-vote-gris.gif";
			$("input[name=noteVal]").val(val);
			for(i=0; i<val; i++) {
				$("img[id=image_"+i+"]").attr({"src":imgJaune});
			}
			for(k=i; k<6; k++) {
				$("img[id=image_"+k+"]").attr({"src":imgGris});
			}

	});

	$('#retourPopConfirmErreurNoteZero').click(function(){
		$('#popConfirmErreurNoteZero')[0]._closeLightBox();
		$('#voter').oLightBox();
	});

	$('#retourPopConfirmDejaNoter').click(function(){
		$('#popConfirmDejaNoter')[0]._closeLightBox();
	});

	
	$('.votez').click(
		function (){
			var note = $("input[name=noteVal]").val();
			var noteRecetteId = $("input[name=noteRecetteId]").val();
			var zUrlTraitement = $('#urlTraitementVodeRecette').val();
			if (note == 0){
				$('#voter')[0]._closeLightBox();
				$('#popConfirmErreurNoteZero').oLightBox({ callClose : returnRegisterVote });
				return false;
			}else{
				$.getJSON(zUrlTraitement, {iNoteVal:note, iNoteRecetteId:noteRecetteId},
					function(data){
						$('#voter')[0]._closeLightBox();
						document.location.href = document.location.href;
					}
				);
				return false;
			}
		}
	);
});
function returnRegisterVote (){
	$('#voter').oLightBox();
}
function returnRegister () {
	$('#inscripiton').oLightBox();
}
function returnContact () {
	$('#contact').oLightBox();
}
function returnRedirect (){
	document.location.href= j_basepath + "index.php?module=offre&action=offreFo:offreDetail";
}
function returnRegisterMdpOublir (){
	$('#ident-recup').oLightBox();
}

//Controle formulaire d'inscription
function catchError(frm, invalidFields){ 
	regex = new RegExp("^[\\w\\.=-]+@[\\w\\.-]+\\.[\\w\\.-]{2,4}$");
	var initErrorMsg = "";
	var initCiv = "";
	var initErrorCivilite = ", la civilité";
	var initErrorNom = ", le nom";
	var initErrorPrenom = ", le prénom";
	var initErrorSexe = ", le sexe";
	var initErrorAnniv = ", l'âge";		
	var initErrorCodePostal = ", le code postal";
	var initErrorEmail = ", l'e-mail";
	var initErrorMdp = ", le mot de passe";
	var initErrorNbEnfant = ", le nombre d'enfants au foyer";		
	var initErrorConfEmail = "<br>Veuillez confirmer votre adresse email.";
	var initErrorReConfEmail = "<br>Confirmation d'e-mail incorrecte.";
	var initErrorEmailSyntaxe = "<br>Veuillez saisir correctement votre adresse email.";
	var initErrorLongueurMdp = "<br>Le champ mot de passe doit être à 6 caractères minimum.";
	var initErrorCondition = "<br>Vous devez accepter les CGU.";
	var initErrorDateNaiss = "<br>Veuillez saisir une date de naissance valide.";
	var initErrorCodePostalLongueur = "<br>Le champ code postal doit être à 5 chiffres.";

	var bCheckForm = false;
	var iRadioCivilite = 0;
	var iRadioSexe = 0;
	var iCivilite = 0;
	var iSexe = 0;
	var iCheckBoxeCondition = 0;
	var iCondition = 0;

	$('[@name=civilite]').each(function(){
		if($(this).attr('checked') == true){
			iRadioCivilite++ ;
		}
	});
	if (iRadioCivilite == 0){
		$('[@name=civilite]').each(function(){
			$(this).attr('tmt:required','true');
			$(this).attr('tmt:errorclass','error');
			if (iCivilite == 0){
				initCiv += initErrorCivilite;
				iCivilite = 1;
			}
		});
	}

	if($('[@name=nom]').val() == ''){
		initCiv += initErrorNom;
	}

	if($('[@name=prenom]').val() == ''){
		initCiv += initErrorPrenom;
	}

	$('[@name=sexe]').each(function(){
		if($(this).attr('checked') == true){
			iRadioSexe++;
		}
	});
	if (iRadioSexe == 0){
		$('[@name=sexe]').each(function(){
			if (iSexe == 0){
				initCiv += initErrorSexe;
				iSexe = 1;
			}
		});
	}	

	if( $('[@name=jours]').val() == '' 	|| 
		$('[@name=jours]').val() == 'JJ'||
		$('[@name=mois]').val() == '' 	||
		$('[@name=mois]').val() == 'MM'	||
		$('[@name=annee]').val() == '' ||
		$('[@name=annee]').val() == 'AAAA'){
		initCiv += initErrorAnniv;
	}

	if($('[@name=cp]').val() == ''){
		initCiv += initErrorCodePostal;
	}

	if($('[@name=email]').val() == ''){
		initCiv += initErrorEmail;
	}

	if($('[@name=mdpasse]').val() == ''){
		initCiv += initErrorMdp;
	}

	if($('[@name=nb-enfant]').val() == ''){
		initCiv += initErrorNbEnfant;
	}

	if(initCiv != ''){
		erreurCiv = trim(initCiv, ',');
		initErrorMsg += "Veuillez renseigner " + erreurCiv + ".";
	}
	
	if(( 
		$('[@name=jours]').val() != '' 	&& 
		$('[@name=jours]').val() != 'JJ'&&
		$('[@name=mois]').val() != '' 	&&
		$('[@name=mois]').val() != 'MM'	&&
		$('[@name=annee]').val() != '' &&
		$('[@name=annee]').val() != 'AAAA'		
		) && (
		parseFloat($('[@name=jours]').val()) < $('[@name=jours]').attr("tmt:minnumber") || (!parseFloat($('[@name=jours]').val())) || 
		parseFloat($('[@name=jours]').val()) > $('[@name=jours]').attr("tmt:maxnumber") || (!parseFloat($('[@name=jours]').val())) || 
		parseFloat($('[@name=mois]').val()) < $('[@name=mois]').attr("tmt:mois") || (!parseFloat($('[@name=mois]').val())) || 
		parseFloat($('[@name=mois]').val()) > $('[@name=mois]').attr("tmt:maxnumber") || (!parseFloat($('[@name=mois]').val())) || 
		parseFloat($('[@name=annee]').val()) < $('[@name=annee]').attr("tmt:minnumber") || (!parseFloat($('[@name=annee]').val())) || 
		parseFloat($('[@name=annee]').val()) > $('[@name=annee]').attr("tmt:maxnumber") || (!parseFloat($('[@name=annee]').val()))
		)
	){
		initErrorMsg += initErrorDateNaiss;
	}
	
	if ($('[@name=cp]').val() != '' && $('[@name=cp]').val().length < 5) {
		initErrorMsg += initErrorCodePostalLongueur;
	}
	
	if ($('[@name=email]').val() != '' && !regex.test($('[@name=email]').val())) {
		initErrorMsg += initErrorEmailSyntaxe
	} else if ($('[@name=confEmail]').val() != '' && !regex.test($('[@name=confEmail]').val())) {
		initErrorMsg += initErrorEmailSyntaxe
	}  else if($('[@name=email]').val() != '' && $('[@name=confEmail]').val() == ''){
		initErrorMsg += initErrorConfEmail;
	} else if($('[@name=confEmail]').val() != $('[@name=email]').val()){
		initErrorMsg += initErrorReConfEmail;
	}

	if($('[@name=mdpasse]').val() != '' && $('[@name=mdpasse]').val().length < 6){
		initErrorMsg += initErrorLongueurMdp;
	}
	
	if($('[@name=condition]').attr('checked') == true){
		if($('[@name=condition]').attr('checked') == true){
			iCheckBoxeCondition++;
		}
	}

	if (iCheckBoxeCondition == 0){
		$('[@name=condition]').each(function(){
			if (iCondition == 0){
				initErrorMsg += initErrorCondition;
				iCondition = 1;
			}
		});
	}	

	//fermeture popup
	if($('#inscripiton')[0]){
		$('#inscripiton')[0]._closeLightBox();
	}else{
		$('#inscripiton')._closeLightBox();
	}

	$('.erreur').oLightBox({ callClose : returnRegister });
	$('.errorMessage').empty();
	$('.errorMessage').html(initErrorMsg).show();
}

//Controle formulaire de contact
function catchErrorContact (frm, invalidFields){ 
	var initErrorMsg = "";
	var initErrorsujetContact = "<br>Veuillez choisir un sujet.";
	var initErrormessageContact = "<br>Veuillez saisir votre message.";

	var bCheckForm = false;

	if ($('[@name=sujetContact]').val() == 0){
		initErrorMsg += initErrorsujetContact;
		$('#sujetContact').css('border', '1px solid red');
	}

	if ($('[@name=messageContact]').val() == ''){
		initErrorMsg += initErrormessageContact;
		$('#messageContact').css('border', '1px solid red');
	}


	//fermeture popup
	if($('#contact')[0]){
		$('#contact')[0]._closeLightBox();
	}else{
		$('#contact')._closeLightBox();
	}

	$('.erreurContact').oLightBox({ callClose : returnRegisterContact });
	$('.errorMessageContact').empty();
	$('.errorMessageContact').html(initErrorMsg).show();
}

function returnRegisterContact (){
	$('#contact').oLightBox();
}

function trim(chaine){
	if(trim.arguments.length > 1)
	{
		var str = trim.arguments[1];
		expreg = new RegExp('(^'+ str +'*)|('+ str +'*$)', 'g');
	}
	else
	{
		expreg = /(^\s*)|(\s*$)/g;
	}
	return chaine.replace(expreg,'');
}
