function log_me_in() {
	document.forms[0].submit();
}
function log_me_out(titre, url, msg) {
	/*if (confirm(msg) == true)
		document.location.href = url;*/
		
	jConfirm(msg, titre, function(r) {
	     if( r ) document.location.href = url;
	});
}

// J'ai pas pu voir quelque part o? on a appel? cette fonction ci' 
function confirm_clear_communautaire_fan_de(url_clear, msg) {
	if (confirm(msg) == true)
		document.location.href = url_clear;
}

function confirm_delete_friend(titre,msg,url) {
	jConfirm(msg, titre, function(r) {
	     if( r ) document.location.href = url;
	});
}

function load_invite(id, url){
	
	var xhr = getXhr();
	// On d?fini ce qu'on va faire quand on aura la r?ponse
	
	xhr.onreadystatechange = function() {
		// On ne fait quelque chose que si on a tout re?u et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			resp = xhr.responseText;
			// On se sert de innerHTML pour rajouter les options a la liste
			document.getElementById('import_tiers').innerHTML =	resp;							
		}
	}

	
	// Ici on va voir comment faire du get
	xhr.open("POST",url,true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id="+id);	
}

function emailcheck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		
		

		if (str.indexOf(at)==-1){		   
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){		    
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){		    
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){		    
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){		    
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){		    
		    return false;
		 }

 		 return true;				

	}

function undo_red(field) {
	var f = document.getElementById(field);
	if (f != null) {
		f.style = null;
	}
}


function send_invitation_to_join(lg) {
	var msg_err_list_inv = '';
	var msg_err_login = '';
	var msg_err_passw = '';
	var msg_err_format_mail = '';
	var err = 0;
	switch (lg) {
		default:
			msg_err_format_mail = 'Vous devez fournir une adresse email correcte';
			msg_err_list_inv = "Vous devez fournir une liste d'adresses";
			msg_err_login = 'Vous devez votre login ou adresse email  avant de continuer';
			msg_err_passw = 'Vous devez votre mot de passe avant de continuer';
		break;
	}
	
	//1. checkup contenu des champs
	var list_inv = document.frmInscriptionInviteFriends.invite_emails.value;
	var mess_inv = document.frmInscriptionInviteFriends.invite_message.value;		
	var log = '';
	if (document.getElementById('login') != null) {
		log = document.frmInscriptionInviteFriends.login.value;		
		document.getElementById('login').color = '#ffffff';
		err++;
	}	
	var pwd = '';
	if (document.getElementById('passw') != null) {
		pwd = document.frmInscriptionInviteFriends.passw.value;			
		document.getElementById('passw').color = '#ffffff';	
		err++;
	}	
	if (list_inv == null || list_inv == '') {
		//Si liste d'adresses saisie vide, on doit fournir un autre moyen
		if (log == '' && document.getElementById('login') != null) {
			alert(msg_err_login);
			document.getElementById('login').style.border = '1px solid #ff0000';								
			document.getElementById('login').focus();
			document.getElementById('invite_emails').style = null;
			err++;	
		}
		else {
			if (pwd == '' && document.getElementById('passw') != null) {
				alert(msg_err_passw);
				document.getElementById('passw').style.border = '1px solid #ff0000';								
				document.getElementById('passw').focus();
				document.getElementById('invite_emails').style = null;
				err++;	
			}
			else {
				if (err == 0) {
					alert(msg_err_list_inv);
					document.getElementById('invite_emails').style.border = '1px solid #ff0000';								
					document.getElementById('invite_emails').focus();
					err++;
				}
			}
		}
		
	}
	else {
		if (log != '' && document.getElementById('login') != null) {
			if (pwd == '' && document.getElementById('passw') != null) {
				alert(msg_err_passw);
				document.getElementById('passw').style.border = '1px solid #ff0000';								
				document.getElementById('passw').focus();							
				err++;
			}							
		}
		else {
			if (document.getElementById('login') != null) {
				alert(msg_err_login);
				document.getElementById('login').style.border = '1px solid #ff0000';								
				document.getElementById('login').focus();							
				err++;		
			}
		}
	}
	
	if (err > 0)
		return false;
		
	//2. checkup syntaxe emails
	if (list_inv != '') {
		list_inv = list_inv.split(" ").join("");
		var liste_adresses = list_inv.split(',');
		
		var lg = liste_adresses.length;
		var j=0;
		var erreur_f = false;
		while (j < lg && erreur_f == false) {
			var val = liste_adresses[j];						
			var ret = emailcheck(liste_adresses[j]);
			if (ret == false) {
				alert(msg_err_format_mail);
				document.getElementById('invite_emails').style.border = '1px solid #ff0000';								
				document.getElementById('invite_emails').focus();				
				erreur_f = true;
				err++;
			}
			j++;
		}		
	}
	else {
	
	}	
}

function pop_tiers(pop_page, pop_name)
{
	window.open (pop_page, pop_name, config='height=400, width=700, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
}

function checkAllInput(form, action)
{
   var i = document.forms[form].getElementsByTagName("input");
    for ( var cpt = 0; cpt < i.length; cpt++)
                              i[cpt].checked = (action)? true : false ;
}

function confirm_clear_commentaire(url_clear, lg) {
	var msg;
	switch (lg) {
		default:
			msg = 'Etes-vous certain de vouloir effacer ce commentaire ?';
		break;
	}
	if (confirm(msg) == true)
		document.location.href = url_clear;
}

function confirm_join_group(titre, url_join) {
	msg = document.getElementById('msg_txt_alert').value;
	jConfirm(msg, titre, function(r) {
	     if( r ) document.location.href = url_join;
	});
}

function showtext_priv() {
 	sel = document.getElementById('privategroup');
	privategroup = sel.options[sel.selectedIndex].value;	
	
	if (privategroup == 0){
		msg = "";
	}
	else{
        msg_priv = document.getElementById('msg_add_priv');
        msg = msg_priv.value;
	}
	document.getElementById('txtpriv').innerHTML =	msg;
}


function comm_params(a) {	
	alert(a);
	switch (a) {
		case 1:
			document.forms[0].elements[0].focus();			
		break;
		default:
		
		break;		
	}
	
}

/***************   Code Lamine    **********************************************/
function confirm_quit_group(titre,url_quit, msg) {
	jConfirm(msg, titre, function(r) {
	     if( r ) document.location.href = url_quit;
	});
}
/***************   Code Lamine    **********************************************/

$(document).ready(function(){
	$('#redirect_select').change(function(){
		if($(this).val() != 'undefined' && $(this).val() != '')
			$(location).attr('href',$(this).val());
	})
	
	$('#menu_onglet').each(function(){
		$(this).children("li").children("a").each(function(){
			$(this).click(function(){highlightCurrentTab($(this));})
		});
	});
});

function highlightCurrentTab(oTab)
{
	oTab.parent().parent().children("li").children("a").each(function(){$(this).removeClass('current');});
	oTab.addClass('current');
}

function confirm_delete_follow_friend(titre,msg,url) {
	jConfirm(msg, titre, function(r) {
	     if( r ) document.location.href = url;
	});
}

	function effacerValeurDefaut(obj, val_defaut){
		var nom_obj = obj.name;
		var id_obj = obj.id;
		var val_obj = obj.value;
		
		if(nom_obj=='password' || nom_obj=='password_copy'){
			document.getElementById(id_obj).type = 'password';
		}
		
		if(val_obj==val_defaut){
			obj.value= "";
		}
	}
	
	function mettreValeurDefaut(obj, val_defaut){
		var nom_obj = obj.name;
		var id_obj = obj.id;
		var val_obj = obj.value;
		
		if(val_obj=="" || val_obj==" "){
			obj.value= val_defaut;
		}
		
		if((nom_obj=='password' || nom_obj=='password_copy') && (val_obj=="" || val_obj==val_defaut) ){
			document.getElementById(id_obj).type = 'text';
		}
		if(nom_obj=='password'){
			document.getElementById("span_error_taille_pwd").className = "cachediv";
			if(val_obj.length < 6){
				document.getElementById("span_error_taille_pwd").className = "affdiv";
			}
		}
		if(nom_obj=='password_copy'&& val_obj!=val_defaut){
			checkPasswords();
		}
	}
	
	function verifierChamps(){
		form = document.frmRegister;
		var reg_sel = new RegExp("^sel_obligatoire.*$","i");
		var reg_txt = new RegExp("^txt_obligatoire.*$","i");
		
		for(var j=1;j<(form.length);j++){
			var id_champ = form[j].id;
			var classe_champ = form[j].className;
			var valeur = form[j].value;
			var span_error = "span_error_"+j;
			
			reponse = true;
			if(classe_champ.match(reg_sel)){
				document.getElementById(span_error).className = "cachediv";
				if(valeur=='0'){
					document.getElementById(span_error).className = "affdiv";
					document.getElementById(id_champ).className="sel_obligatoire, cls_erreur_champ";
					
					reponse = false;
					break;
				}
				else{
					document.getElementById(id_champ).className="sel_obligatoire";
				}
			}
			if(classe_champ.match(reg_txt)){
				document.getElementById(span_error).className = "cachediv";
				if(valeur ==''){
					document.getElementById(span_error).className = "affdiv";
					document.getElementById(id_champ).className="txt_obligatoire, cls_erreur_champ";
					reponse = false;
					break;
				}
				else{
					document.getElementById(id_champ).className="txt_obligatoire";
				}
			}
		}
		
		if(reponse == false){
			return false;
		}
		else{
			reponse = checkPasswords();
		}
		
		return reponse;
	}
	
	function checkChamp(champ, id_champ, val_defaut){
		var span_error = "span_err_"+champ;
		var valeur = document.getElementById(id_champ).value;
		document.getElementById(span_error).className = "cachediv";
		document.getElementById("span_error_mail").className = "cachediv";
		if(valeur==""){
			document.getElementById(id_champ).value= val_defaut;
			return false;
		}
			
		var xhr = getXhr();
		xhr.onreadystatechange = function()
		{
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
			if(xhr.readyState == 4)
			{
				var reponse = xhr.responseText;
			//	alert(reponse);
				if(reponse == 'invalide'){
					document.getElementById(span_error).className = "affdiv";
					return false;
				}
				else if(reponse == 'mail_invalide'){
					document.getElementById("span_error_mail").className = "affdiv";
					return false;
				}
				else{
					return true;
				}
			}
		}
		
		xhr.open("POST", "_view/traitements_ajax.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
		xhr.send("check_champ=&champ="+champ+"&valeur="+valeur);
	}
	
	function checkPasswords(){
		var password1 = document.getElementById('password').value;
		var password2 = document.getElementById('password_copy').value;
		document.getElementById("span_error_pass_diff").className = "cachediv";
		
		if(password1 !== password2){
			document.getElementById("span_error_pass_diff").className = "affdiv";
			return false;
		}
	}

