var URL_AJAX = "lib/ajax/rciFunction.php" ;
/** Pour l'accordéon des actualités, appelée dans salleDePresse-actualites.php **/
function slideActualites(){
	$(".contentEvent:not").hide();
	$(".actualite .headActu").click( function () {
		if ($(this).next(".contentEvent:visible").length != 0) {
			$(this).next(".contentEvent").slideUp("fast");
		} else {
			$(".contentEvent").slideUp("fast");
			$(this).next(".contentEvent").slideDown("normal");
		}
	});
}
/** Dans salleDePresse-newsletter.php, permet de modifier le lien du btn telecharger suivant la selection faite à partir du menu deroulant **/
function ajoutLienPdf(lienD, id){
	// Recupere le nom du pdf correspondant a la newsletter selectionnee
	var nomPdf = $.ajax({
	   type: "GET",
	   url: URL_AJAX,
	   data: "type=select_newsletter&valeur="+id,
	   async: false
	   //success: function(msg){/*alert( "Data Saved: " + msg );*/}
	 }).responseText;
	// on reattribut le nouveau nom au bouton
	var newValue = lienD+nomPdf ;
	$('#btnTelecharge').attr('href', newValue) ;
}
/* Vérifie la validité du courriel */
function checkCourriel() {
	if($("#courriel").val() == ""){
		$("#msg_confirmation").slideUp();	
		$("#msg_erreur").slideUp();	
	}
			
	var courrielValid = $.ajax({ 
	type: "GET", 
	data: "type=verifMail&courriel="+$("#courriel").val(), 
	url: URL_AJAX,
	async: false,
	success: function(msg){
		//alert( "Data Saved: " + msg );
	}
	}).responseText;
	
	if(courrielValid=="1"){
		$("#btnInscription").show() ;
		$("#btnInscriptionDisabled").hide() ;
	}else{
		$("#btnInscription").hide() ;
		$("#btnInscriptionDisabled").show() ;
	}
}
/* Inscription dans salle de presse - newsletter */
function inscrire(courriel){
	var validation = $.ajax({
	   type: "GET",
	   url: URL_AJAX,
	   data: "type=insert_newsletter&valeur="+courriel,
	   async: false,
	   success: function(msg){/*alert( "Data Saved: " + msg );*/}
	 }).responseText;
	if(validation=="1"){
		$("#msg_confirmation").slideDown();
	}else{
		$("#msg_erreur").slideDown();
	}
}

/** Pour l'accordéon des emplois, appelée dans carrieres-offres **/
function slideOffres(){
	$(".contentEvent").hide();
	$(".actualite").click( function () {
		if ($(this).next(".contentEvent:visible").length != 0) {
			$(this).next(".contentEvent").slideUp("fast");
		} else {
			$(".contentEvent").slideUp("fast");
			$(this).next(".contentEvent").slideDown("normal");
		}
	});
}


/** Dans carrieres-formulaire **/
function afficheProvince(paysId){
	var provinces_arr = $.ajax({
	   type: "GET",
	   url: URL_AJAX ,
	   data: "type=select_provinces&valeur="+paysId,
	   async: false,
	   success: function(msg){/*alert( "Data Saved: " + msg );*/}
	 }).responseText;

	if(provinces_arr == ""){$("#provinceListe").attr("disabled","disabled");$("#provinceListe").val("");$("#province").val("");}
	else{
		$("#provinceListe").attr("disabled","");
		var provinces = new Array;
		var arr_data = provinces_arr.split("#") ;
		for (var i = 0; i < arr_data.length; i++){
			var arr_data_liste = arr_data[i].split("|") ;
			provinces[i] = arr_data_liste[0]+"|"+arr_data_liste[1] ;
		}	
		
		$("#provinceListe").autocomplete(provinces, {
            minChars:1,
            autoFill: true,
            mustMatch: true,
            matchContains: false,
			formatItem: function(data, i, total) {
				return data[0].split("|")[1];
			}
		}).result(function(event, data, formatted){	
			$("#province_id").val(data[0].split("|")[0]) ;
			$("#provinceListe").val(data[0].split("|")[1]) ;
        });
	}
}
/* Formatte correctement un CP */
function formatPostalCode(el) {
    var val = $(el).val();
    if (val.length > 3) {
    	val = val.replace(/ /, "");
    	val = val.substring(0, 3)+" "+val.substring(3);
    }
    val = val.toUpperCase();
    $(el).val(val);
}
/* Pour se loguer à son compte */
function login(){
	var uname = jQuery.trim($("#uname").val());
	var pword = jQuery.trim($("#pword").val());
	// Check dans la bdd
	var is_log = $.ajax({
	   type: "GET",
	   url: URL_AJAX ,
	   data: "type=login&uname="+uname+"&pword="+pword,
	   async: false,
	   success: function(msg){/*alert( "Data Saved: " + msg );*/}
	}).responseText;
	// redirection vers une page index de mon compte
	if (is_log == "success") {
    	document.location = $("#userLogin").attr("action");
	} else { //sinon msg d'erreur
		$('#erreurConnexion').show();
		$('#erreurMail').hide();
		$('#mailEnvoye').hide();
		$('#blocErreur').fadeIn() ;
		setTimeout(function() {
			$("#blocErreur").fadeOut() ;
			$("#userLogin input.userName").attr("value", nomUsager);
	        $("#userLogin input.passWord").attr("value", motDePasse);
		}, 2000);
	}	
}
/* Pour se deconnecter de son compte, avec redirection */
function deconnexion(){
	$.ajax({
	   type: "GET",
	   url: URL_AJAX ,
	   data: "type=deconnexion" ,
	   async: false,
	   success: function(msg){/*alert( "Data Saved: " + msg );*/}
	});
	document.location = "";
}

/* Vérifie le courriel pour l'envoi du mot de passe */
function checkCourrielMdp() {
			
	/*
	// BUG SUR rcienvironnement.com : AJAX trop lent
	var courrielValid = $.ajax({ 
	type: "GET", 
	data: "type=verifMail&courriel="+$("#courrielRappel").val(), 
	url: URL_AJAX,
	async: false,
	success: function(msg){
		//alert( "Data Saved: " + msg );
	}
	}).responseText;
	*/
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var courriel = $("#courrielRappel").val();
	courrielValid = courriel.match(emailRegEx);
	
	if(courrielValid){
		$("#submit_rappelMdp").show() ;
		$("#submit_rappelMdp_disabled").hide() ;
	}else{
		$("#submit_rappelMdp").hide() ;
		$("#submit_rappelMdp_disabled").show() ;
	}
}
/* Envoi le mail contenant le mdp */
function envoiMdp(){
	var rep = $.ajax({ 
		type: "GET", 
		data: "type=envoiMdp&courriel="+$("#courrielRappel").val(), 
		url: URL_AJAX,
		async: false,
		success: function(msg){
			//alert( "Data Saved: " + msg );
		}
	}).responseText;
	$('#erreurConnexion').hide();
	if(rep == "succes"){
		$('#erreurMail').hide();
		$('#mailEnvoye').show();
	}else{		
		$('#erreurMail').show();
		$('#mailEnvoye').hide();
	}
	$('#blocErreur').fadeIn() ;
	setTimeout(function() {
		$("#blocErreur").fadeOut() ;
		$("#blocMdpRappel input.courrielRappel").attr("value", courrielRappel);
	}, 2000);
}