/*
	Auteur : Gael CADORET
	Date de création : 17/08/2011
	Version : 0.1
*/

// Fonction de remplacement de la fonction split() :
function explode (delimiter, string, limit) 
{    
	// Splits a string on string separator and return array of components. 
	// If limit is positive only limit number of components is returned. 
	// If limit is negative all components except the last abs(limit) are returned.  
    // *     example 1: explode(' ', 'Gael Cadoret');
    // *     returns 1: {0: 'Gael', 1: 'Cadoret'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
	
     var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||        typeof arguments[1] == 'undefined' ) {
        return null;
    }
 
    if ( delimiter === '' ||        delimiter === false ||
        delimiter === null ) {
        return false;
    }
     if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' ) {
        return emptyArray;    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;    }
}

// Fonction de vérification de présence d'un élément dans un tableau
Array.prototype.in_array = function(p_val) {
    for(var i = 0, l = this.length; i < l; i++) {
        if(this[i] == p_val) {
            rowid = i;
            return true;
        }
    }
    return false;
} 

// Fonction de suppression d'un élément d'un tableau
Array.prototype.unpush = function(id){
   var bfr = [];
   for(var i = 0; i < this.length; i++) {
      if(this[i] != id) {
         bfr.push(this[i]);
      }
   }
   return bfr;
}

function getAction(key, shift) 
{ 
   if(shift) 
   { 
       // Si la touche vaut 113 (F2) + shift on appelle copyToAll 
       //if(key==113) copyToAll(selectedField, nbFields); 
   } 
   else 
   { 
       // Si la touche vaut 113 (F2) on appelle copyToNext 
       //if(key==113) copyToNext(selectedField); 
	   if((key==27) || (key==13))
	   { $("#btn_comments").remove(); }
   } 
    
   // PROVISOIRE : pour vérifier le code récupéré 
   //alert(key); 
} 

function enlever_ressource_liste_D(elem)
{
	var id_ligne = elem.id;

	var tab_info = explode("_", id_ligne);
	
	var ID = tab_info[1];
	
	var contenu = $("#" + id_ligne).html();
	
	$("#" + id_ligne).slideUp(250, function(){
		$("#" + id_ligne).remove();
	});
	
	$("#liste_G").append("<div id='userFree_" + ID + "' class='style_user' onclick='enlever_ressource_liste_G(this);'>" + contenu + "</div>");

	var str_liste_ressources = $("#liste_ressources_tache").val();
	
	var tab_liste_ressources = new Array();
	tab_liste_ressources = explode(",", str_liste_ressources);
	
	if(tab_liste_ressources.in_array(ID))
	{
		// Supprimer l'id de la liste des ressources affectées sur cette tâche !
		tab_liste_ressources = tab_liste_ressources.unpush(ID);
		
		var str_new_liste = "";
		for(i=0; i < tab_liste_ressources.length ; i++)
		{
			if(i == (tab_liste_ressources.length - 1))
			{	str_new_liste += tab_liste_ressources[i];
			}else{
					str_new_liste += tab_liste_ressources[i] + ",";
				}
		}
		
		$("#liste_ressources_tache").val(str_new_liste);
	}else{ alert("enlever_ressource_liste_D => Cette personne n'est pas encore affectée à cette tâche !"); }
}

function enlever_ressource_liste_G(elem)
{
	var id_ligne = elem.id;
	
	var tab_info = explode("_", id_ligne);
	
	var ID = tab_info[1];
	
	var contenu = $("#" + id_ligne).html();
	
	$("#" + id_ligne).slideUp(250, function(){
		$("#" + id_ligne).remove();
	});
	
	$("#liste_D").append("<div id='userBusy_" + ID + "' class='style_user' onclick='enlever_ressource_liste_D(this);'>" + contenu + "</div>");

	var str_liste_ressources = $("#liste_ressources_tache").val();
	
	var tab_liste_ressources = new Array();
	
	if(str_liste_ressources != "")
	{
		tab_liste_ressources = explode(",", str_liste_ressources);

		if(!tab_liste_ressources.in_array(ID))
		{	
			// Supprimer l'id de la liste des ressources disponibles !
			tab_liste_ressources.push(ID);
			
			var str_new_liste = "";
			for(i=0; i < tab_liste_ressources.length ; i++)
			{
				if(i == (tab_liste_ressources.length - 1))
				{	str_new_liste += tab_liste_ressources[i];
				}else{
						str_new_liste += tab_liste_ressources[i] + ",";
					}
			}
			
			$("#liste_ressources_tache").val(str_new_liste);
			
		}else{ alert("enlever_ressource_liste_G => Cette personne est déjà affectée à cette tâche !"); }
	}else{
			$("#liste_ressources_tache").val(ID);
		}
}

function verif_form(type_champ, elem)
{
	var id_verif = "verif_" + elem.id;
	
	var valide_inscription = true;
	
	switch(type_champ)
	{
		case 1 : // Champ texte
			
			if( isNaN( $("#" + elem.id).val() ) )
				$("#" + id_verif).html("<img src='images/ball-cute-rendez-vous-icone-6043-16.png' class='verif_img'>");
			else
				$("#" + id_verif).html("<img src='images/balle-fermer-cute-stop-icone-4372-16.png' class='verif_img'>");
				
			break;
			
		case 2 : // Champ numérique
		
			if( !isNaN( $("#" + elem.id).val() ) )
			{	$("#" + id_verif).html("<img src='images/ball-cute-rendez-vous-icone-6043-16.png' class='verif_img'>");
			}else{
				$("#" + id_verif).html("<img src='images/balle-fermer-cute-stop-icone-4372-16.png' class='verif_img'>");
				valide_inscription = false;
				}
				
			break;
			
			
		case 3 : alert("Champ alphanumerique !");
			break;
			
			
		case 4 : // Champ email
			
			var js_email = $("#" + elem.id).val();
			
			if(VerifMail(js_email))
			{	$("#verif_" + elem.id).html("<img src='images/ball-cute-rendez-vous-icone-6043-16.png' class='verif_img'>");
			}else{	
					$("#verif_" + elem.id).html("<img src='images/balle-fermer-cute-stop-icone-4372-16.png' class='verif_img'>");
					valide_inscription = false;
				}
			break;
			
			
		case 5 : // Comparaison de champs correspondant à un mot de passe et son champ de confirmation
			
			Expression = new RegExp("confirm_");
			var champ = elem.id;
			
			if(champ.match(Expression))
			{	//alert("champ confirm !");
				var champ_defaut = champ.replace("confirm_", "");
				var js_password = $("#" + champ_defaut).val();
				var js_confirm_password = $("#" + elem.id).val();
				
				if(js_password!=js_confirm_password)
				{	//alert(champ_defaut + "\n" + elem.id);
					$("#verif_" + champ_defaut).html("<img src='images/balle-fermer-cute-stop-icone-4372-16.png' class='verif_img' title='Le mot de passe est différent de la confirmation !'>");
					$("#verif_" + elem.id).html("<img src='images/balle-fermer-cute-stop-icone-4372-16.png' class='verif_img' title='Le mot de passe est différent de la confirmation !'>");
					valide_inscription = false;
				}else{
						$("#verif_" + champ_defaut).html("<img src='images/ball-cute-rendez-vous-icone-6043-16.png' class='verif_img'>");
						$("#verif_" + elem.id).html("<img src='images/ball-cute-rendez-vous-icone-6043-16.png' class='verif_img'>");
					}
			}else{
					var js_password = $("#" + elem.id).val();
					var js_confirm_password = $("#confirm_" + elem.id).val();
					
					if(js_password!=js_confirm_password)
					{	//alert(champ_defaut + "\n" + elem.id);
						$("#verif_" + elem.id).html("<img src='images/balle-fermer-cute-stop-icone-4372-16.png' class='verif_img' title='Le mot de passe est différent de la confirmation !'>");
						$("#verif_confirm_" + elem.id).html("<img src='images/balle-fermer-cute-stop-icone-4372-16.png' class='verif_img' title='Le mot de passe est différent de la confirmation !'>");
						valide_inscription = false;
					}else{
							$("#verif_" + elem.id).html("<img src='images/ball-cute-rendez-vous-icone-6043-16.png' class='verif_img'>");
							$("#verif_confirm_" + elem.id).html("<img src='images/ball-cute-rendez-vous-icone-6043-16.png' class='verif_img'>");
						}
				}
				
			break;
		
		case 6 : // Comparaison de champs correspondant à un CAPTCHA
					var js_captcha = $("#" + elem.id).val();
					var js_confirm_captcha = $("#verif_" + elem.id).val();
					
					if(js_captcha == js_confirm_captcha)
					{	$("#verif_" + "verif_" + elem.id).html("<img src='images/ball-cute-rendez-vous-icone-6043-16.png' class='verif_img'>");
					}else{
						$("#verif_" + "verif_" + elem.id).html("<img src='images/balle-fermer-cute-stop-icone-4372-16.png' class='verif_img'>");
						valide_inscription = false;
					}
			break;
		
	}
	
	return valide_inscription;
}

function VerifMail(email)
{
	a = email;
	valide1 = false;

	for(var j=1;j<(a.length);j++)
	{
		if(a.charAt(j)=='@')
		{
			if(j<(a.length-4))
			{
				for(var k=j;k<(a.length-2);k++)
				{
					if(a.charAt(k)=='.')
					{	valide1 = true;	}
				}
			}
		}
	}
	
	//if(valide1 == false) 
		//alert("Veuillez saisir une adresse email valide.");
		
	return valide1;
}

function inscription()
{
	alert("Fonctionnalité bientot disponible...");
}

function verif_inscription()
{
	var js_civ = $("#civilite").val();
	var js_login = $("#login").val();
	var js_nom = $("#nom").val();
	var js_prenom = $("#prenom").val();
	var js_email = $("#new_identifiant").val();
	
	var js_structure = $("#id_societe").val();
	
	var js_password = $("#password_identifiant").val();
	var js_confirm_password = $("#confirm_password_identifiant").val();
	
	var js_captcha = $("#confirm_captcha").val();
	var js_confirm_captcha = $("#verif_confirm_captcha").val();
	
	//if((nom != "") && (prenom!=""))
	//alert("Merci de renseigner les champs obligatoires.\nMerci de votre compréhension.");
	
	if( (js_structure != "0") && (js_civ != "0") && (js_login != "") && (js_nom != "") && (js_prenom != "") && (js_email != "") && (js_password == js_confirm_password) && (js_captcha == js_confirm_captcha))
	{ return true; 
	}else{ alert("Merci de renseigner les champs obligatoires.\nMerci de votre compréhension.\n\n* = Champs obligatoires !"); return false; }
}

function verif_connexion()
{
	var login = $("#identifiant").val();
	var mot_de_passe = $("#password_identifiant").val();
	
	if((login != "") && (mot_de_passe!=""))
	{ return true; 
	}else{ return false; }
}

function verif_reinit()
{
	var email = $("#user_email").val();
	
	var js_captcha = $("#confirm_captcha").val();
	var js_confirm_captcha = $("#verif_confirm_captcha").val();
	
	if( VerifMail(email) && (js_captcha == js_confirm_captcha))
	{ 
		return true; 
		
	}else{ $("#retour_fonction").html("<img src='images/exclamation-icone-6126-16.png' class='centre'> Merci de saisir une adresse email valide et de recopier le texte de l'image !"); 
			return false; 
		}
	
}

function verif_init()
{
	var email = $("#email").val();
	
	var js_captcha = $("#confirm_captcha").val();
	var js_confirm_captcha = $("#verif_confirm_captcha").val();
	
	var js_password = $("#new_password").val();
	var js_confirm_password = $("#confirm_new_password").val();
	
	if(VerifMail(email) && (js_captcha == js_confirm_captcha) && (js_password == js_confirm_password))
	{ return true; 
	}else{ $("#retour_fonction").html("<img src='images/exclamation-icone-6126-16.png' class='centre'> Merci de saisir une adresse email valide et de recopier le texte de l'image !"); return false; }
	
}

function vider_champ(elem)
{
	$("#" + elem.id).val("");
}

function close_fiche_client()
{
	// Fermeture de la liste des tache en priorité, puis fermeture de la fiche descriptive du projet !
		$("#tableau_update_fiche_projet").animate({
			height:'toggle'
		}, 300, function(){ // Animation complete

				$("#tableau_liste_projet").animate({
						height:'toggle'
					}, 300, function(){ // Animation complete

						});
			});
}

function close_fiche()
{
	// Fermeture de la liste des tache en priorité, puis fermeture de la fiche descriptive du projet !
		$("#tableau_liste_taches").animate({
			height:'toggle'
		}, 300, function(){ // Animation complete
		
				$("#tableau_update_fiche_projet").animate({
					height:'toggle'
				}, 300, function(){ // Animation complete
					
					// Cache le bouton de suppression de projet
					$("#btn_control_suppression_projet").hide();
					
					$("#tableau_liste_projet").animate({
						height:'toggle'
					}, 300, function(){ // Animation complete
							
						});
					
					});
			});

	
}

function update_fiche_client(client_id, nom_client, prenom_client)
{

	//alert(nom_client + " (" + client_id + ")");
	
	$("#tableau_liste_projet").animate({
		height: 'toggle'
	}, 300, function(){ // Animation complete
	
		$("#tableau_update_fiche_projet").animate({
			height:'toggle'
		}, 300, function(){ // Animation complete
			
			$("#titre_fiche_projet > *").remove();
			$("#titre_fiche_projet").append("<div id='titre_fiche_projet_contenu'>" + prenom_client + " " + nom_client + "</div>");
			
			$('#corp_fiche_projet > *').remove();
			$('#corp_fiche_projet').append("<div id='contenu_corps'><div id='contenu_gauche'></div><div id='contenu_droite'></div></div>");
			
			get_infos_client(client_id);
		});
	});
}

function get_infos_client(client_id)
{
	var js_user_id = $("#user_id").val();
	
	$.ajax({
		type: "POST",
		url: "ajax/ajax_get_infos_client.php",
		data: "client_id=" + client_id + "&user_id=" + js_user_id,
		success: function(chaine_retour){	// Code si succès !

			var tab_infos_client = explode(";", chaine_retour);
			
			$('#update_id_client').val($.URLDecode(tab_infos_client[11]));
			$('#update_nom_client').val($.URLDecode(tab_infos_client[5]));
			$('#update_prenom_client').val($.URLDecode(tab_infos_client[6]));
			$('#update_nom_societe_client').val($.URLDecode(tab_infos_client[0]));
			$('#update_adresse_societe_client').val($.URLDecode(tab_infos_client[2]));
			$('#update_CP_societe_client').val($.URLDecode(tab_infos_client[3]));
			$('#update_ville_societe_client').val($.URLDecode(tab_infos_client[4]));
			$('#update_nom_categorie_societe').val($.URLDecode(tab_infos_client[1]));
			$('#update_email_client').val($.URLDecode(tab_infos_client[7]));
			$('#update_tel_client_fixe').val($.URLDecode(tab_infos_client[8]));
			$('#update_tel_client_port').val($.URLDecode(tab_infos_client[9]));
			$('#update_siret_client').val($.URLDecode(tab_infos_client[10]));
			
// $societe.";".urlencode($categorie).";".$adresse_client.";".$cp_client.";".$ville_client.";".$nom_client.";".$prenom_client.";".$email_client.";".$tel_fixe_client.";".$tel_port_client.";".$num_siret
			$('#contenu_gauche').append(
				"<div id='lbl_infos'>Société : </div><div id='var_infos'>" + $.URLDecode(tab_infos_client[0]) + "</div>" +
				"<div id='lbl_infos'>Catégorie : </div><div id='var_infos'>" + $.URLDecode(tab_infos_client[1]) + "</div>" +
				"<div id='lbl_infos'>Adresse : </div><div id='var_infos'>" + $.URLDecode(tab_infos_client[2]) + "</div>" +
				"<div id='lbl_infos'>Code postal : </div><div id='var_infos'>" + tab_infos_client[3] + "</div>" +
				"<div id='lbl_infos'>Ville : </div><div id='var_infos'>" + $.URLDecode(tab_infos_client[4]) + "</div>"
				);
			
			$('#contenu_droite').append(
				"<div id='lbl_infos'>Email : </div><div id='var_infos_D'>" + tab_infos_client[7] + "</div>" +
				"<div id='lbl_infos'>Tél fixe : </div><div id='var_infos_D'>" + $.URLDecode(tab_infos_client[8]) + "</div>" +
				"<div id='lbl_infos'>Tél port : </div><div id='var_infos_D'>" + $.URLDecode(tab_infos_client[9]) + "</div>" +
				"<div id='lbl_infos'>N° Siret : </div><div id='var_infos_D'>" + $.URLDecode(tab_infos_client[10]) + "</div>"
				);
		}
	});
}

var modification_en_cours = false;

function test_var()
{
	alert("Modif : " + modification_en_cours);
}

function actualiser_liste()
{
	alert("Fonction non disponible pour le moment !");
}



function cherche_tache(elem, js_user_id, js_user_id_societe)
{
	js_cherche_mot = elem.value;
	js_len_cherche_mot = js_cherche_mot.length;
	
	if(js_len_cherche_mot >= 2){	
		$.ajax({
			type: "POST",
			url: "www/mon_compte/ajax/ajax_cherche_mot.php",
			data: "GO=true&cherche_mot=" + js_cherche_mot + "&user_id=" + js_user_id + "&user_id_societe=" + js_user_id_societe,
			success: function(chaine_retour){ // Code si succès !
				
				if(chaine_retour!="")
				{	
					$('#table_gauche > *').remove();
					//alert(chaine_retour);
					$('#table_gauche').append(chaine_retour);
				}else{
						$('#table_gauche > *').remove();
					}
			}
		});
	}
}

function rafraichir_liste_taches(elem, js_user_id, js_user_id_societe)
{
	$.ajax({
		type: "POST",
		url: "www/mon_compte/ajax/ajax_lister_project_task.php",
		data: "user_id=" + js_user_id + "&user_id_societe=" + js_user_id_societe,
		success: function(chaine_retour){ // Code si succès !
			
			$("#table_gauche > *").remove();
			
			$("#table_gauche").append(chaine_retour);
		}
	});
}

function get_project_tasks(id_projet)
{

	$.ajax({
		type: "POST",
		url: "ajax/ajax_lister_project_task.php",
		data: "id_projet=" + id_projet,
		success: function(chaine_retour){
			// Code si succès !
			
			tab_chaine_retour = explode("##", chaine_retour);
			
			//for(i=0 ; i < tab_chaine_retour.length ; i++){
				data_taches = explode(";",tab_chaine_retour[0]);
				//alert(data_taches);
				var project_name = data_taches[4];
			//}
			
			var liste_taches = "<table><tr height='30px'><td colspan='4' style='text-align:left;margin-left:10px;'>Liste des tâches : <b>" + project_name + "</b></td></tr><tr>" +
							   "<td style='width:300px;text-align:center;background-color:lightblue;color:white;'>Tache</td>" +
							   "<td style='width:60px;text-align:center;background-color:lightblue;color:white;'>Nb jours</td>" +
							   "<td style='width:130px;text-align:center;background-color:lightblue;color:white;'>Date début</td>" +
							   "<td style='width:130px;text-align:center;background-color:lightblue;color:white;'>Date fin</td>" +
							   "</tr>";

			for(i=0 ; i < tab_chaine_retour.length ; i++)
			{
				data_taches = explode(";",tab_chaine_retour[i]);
			
				//for(j=0 ; j < data_taches.length ; j++){
				if(data_taches[0]!="")
					liste_taches += "<tr><td style='font-size:10px;'>" + data_taches[0] + "</td><td style='font-size:10px;'>" + data_taches[1] + "</td><td style='font-size:10px;'>" + data_taches[2] + "</td><td style='font-size:10px;'>" + data_taches[3] + "</td></tr>";
				//}
			}
			
			liste_taches += "</table>";
			
			$('#liste_taches').html(liste_taches);
			// rediriger("index16.php?page=4");
		}
	});
}

function get_all_totaux_load_page()
{	
	var colonne = new Array("lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche");
	
	// Récupération du nombre de projet pour le calcul des sous-totaux !
	var nombre_projet = $("#liste_projets > *").size();
	
	for(cpt_projet=1 ; cpt_projet <= nombre_projet ; cpt_projet++)
	{
		// Total de toutes les colonnes par projet
		get_sous_totaux(colonne, cpt_projet);
		
		var nb_tache_projet = $("#projet_" + cpt_projet + " > #detail_tache").size();

		//alert(cpt_projet + ":" + nb_tache_projet);
		
		for(cpt_tache_projet=1 ; cpt_tache_projet <= nb_tache_projet ; cpt_tache_projet++)
		{
			// Total d'une taches d'un projet
			get_total_tache_projet(cpt_projet, cpt_tache_projet);
		}
		
		// Total de toutes les colonnes sur toutes les taches par projet
		get_sous_totaux_projet(cpt_projet);
	}
	
	// Total de tous les projets par jour
	get_totaux(colonne);
	
	get_total_time_sheet();
}

function get_all_totaux()
{	
	modification_en_cours = true;
	var colonne = new Array("lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche");
	
	// Récupération du nombre de projet pour le calcul des sous-totaux !
	var nombre_projet = $("#liste_projets > *").size();
	
	for(cpt_projet=1 ; cpt_projet <= nombre_projet ; cpt_projet++)
	{
		// Total de toutes les colonnes par projet
		get_sous_totaux(colonne, cpt_projet);
		
		var nb_tache_projet = $("#projet_" + cpt_projet + " > #detail_tache").size();

		//alert(cpt_projet + ":" + nb_tache_projet);
		
		for(cpt_tache_projet=1 ; cpt_tache_projet <= nb_tache_projet ; cpt_tache_projet++)
		{
			// Total d'une taches d'un projet
			get_total_tache_projet(cpt_projet, cpt_tache_projet);
		}
		
		// Total de toutes les colonnes sur toutes les taches par projet
		get_sous_totaux_projet(cpt_projet);
	}
	
	// Total de tous les projets par jour
	get_totaux(colonne);
	
	get_total_time_sheet();
}

function get_total_time_sheet()
{
	var total = 0.0;
	
	$("div[name^='total_timesheet'] > div > span[name='total_jour']").each(function(){
		var valeur = $(this).html();
		var id_span = $(this).attr('id');
		total = parseFloat(total) + parseFloat(valeur);
	});
	
	$("#total_total").html(total);
}

function get_sous_totaux_projet(cpt_projet)
{
	// var nb_spans = $("div[name^='sous_total_projet_" + cpt_projet + "'] > div > span[name='sous_total_jour']").size();
	//alert(nb_spans); // => 7
	
	var total = 0.0;
	
	$("div[name^='sous_total_projet_" + cpt_projet + "'] > div > span[name='sous_total_jour']").each(function(){
		var valeur = $(this).html();
		var id_span = $(this).attr('id');
		total = parseFloat(total) + parseFloat(valeur);
	});
	
	$("#sous_total_total_projet_" + cpt_projet).html(total);
	
}

function get_total_tache_projet(cpt_projet, cpt_tache_projet)
{
	var total = 0.0;
	
	if((cpt_projet!="") && (cpt_projet!=0) && (cpt_tache_projet!="") && (cpt_tache_projet!=0))
	{
		$("#projet_" + cpt_projet + "_tache_" + cpt_tache_projet + " input").each(function(){
			var valeur = $(this).val();
			total = parseFloat(total) + parseFloat(valeur);
		});

		$("#sous_total_tache_" + cpt_tache_projet + "_projet_" + cpt_projet).html(total);
	}
	//return total;
}

function get_totaux(colonne)
{
	for(i=0 ; i < colonne.length ; i++)
	{
		var total_colonne = get_total_colonne(colonne[i]);
		$("#total_" + colonne[i]).html(total_colonne);
	}
}

function get_sous_totaux(colonne, cpt_projet)
{
	for(i=0 ; i < colonne.length ; i++)
	{
		var total_colonne = get_sous_total_colonne(colonne[i], cpt_projet);
		$("#sous_total_" + colonne[i] + "_projet_" + cpt_projet).html(total_colonne);
	}
}

function get_sous_total_colonne(colonne, cpt_projet)
{
	var total = 0.0;
	
	$("#projet_"+ cpt_projet + " input[name='" + colonne + "']").each(function(){
		var valeur = $(this).val();
		total = parseFloat(total) + parseFloat(valeur);
	});
	
	return total;
}

function get_total_colonne(colonne)
{
	var total = 0.0;
	
	$("input[name='" + colonne + "']").each(function(){
		var valeur = $(this).val();
		total = parseFloat(total) + parseFloat(valeur);
	});
	
	return total;
}

function JQ_AJX_delete_all_table_data()
{
	$.ajax({
		type: "POST",
		url: "modules/gestion_archives/ajax/ajax_truncate_all_table.php",
		data: "GO=true",
		success: function(msg){
			// Code si succès !
			rediriger("index16.php?page=4");
		}
	});
}

function Ajax_verif_password_societe(email, id_societe, password)
{
	$.ajax({
		type: "POST",
		url: "ajax/ajax_verif_password_societe.php",
		data: "email=" + email + "&id_societe=" + id_societe + "&password=" + password,
		success: function(chaine_retour){ // Code si succès !
		
			if(chaine_retour == 1)
			{	
				affiche_bienvenue_societe();
				
			}else{
					if(compteur_identification < 3)
						affiche_erreur_societe();
					else
						fin_erreur_identification();
				}
		}
	});
}

function affiche_bienvenue()
{
	var js_message_bienvenue = $("#message_bienvenue").val();
	var js_libelle_bouton = $("#libelle_bouton").val();
	
	$.msgbox(
		js_message_bienvenue,
		{
		  buttons : [
			{
				type: 'submit', 
				value: js_libelle_bouton
			}
		  ]
		}, function(buttonPressed) { // your code here
		  
		  affiche_formulaire_creation_societe("");
		  
		});
}

function affiche_formulaire_creation_societe(erreur)
{
	var titre_libelle = $("#titre_libelle").val();
	var js_nom_societe = $("#nom_societe").val();
	var js_mot_de_passe = $("#mot_de_passe").val();

	var js_message = erreur + titre_libelle;
	
	$.msgbox(
		js_message, 
		{
			type    : "prompt",
			inputs  : [
			  {type: "text",     label: js_nom_societe, required: true},
			  {type: "password", label: js_mot_de_passe, required: true},
			  {type: "password", label: "Confirmation du mot de passe :", required: true}
			],
			buttons : [
			  {type: "submit", value: "OK"}
			]
		}, function(nom_societe, password, confirm_password) {
				if( (nom_societe) && (password == confirm_password) ) 
				{
				  Ajax_cree_societe(nom_societe, password);
				}else{  var new_erreur = "<font style='color:#CC0000; font-style:italic;'>Désolé, les mots de passes saisis ne sont pas identiques !</font><br><br>";
						affiche_formulaire_creation_societe(new_erreur); }
	  });
}

function Ajax_cree_societe(js_nom_societe, js_password)
{

	$.ajax({
		type: "POST",
		url: "ajax/ajax_cree_new_societe.php",
		data: "nom_societe=" + js_nom_societe + "&password=" + js_password,
		success: function(chaine_retour){ // Code si succès !
		
			if(chaine_retour == 1)
			{	
				affiche_societe_cree(js_nom_societe, js_password);
				
			}else{
				var new_erreur = chaine_retour;
				affiche_formulaire_creation_societe(new_erreur);
			}
		}
	});
}

function affiche_societe_cree(js_nom_societe, js_password)
{
	var js_message_societe_cree = "Votre mot de passe : <font style='color:#CC0000;font-weight:bold;'>" + js_password + "</font><br>" +
								  "<font style='color:green;font-style:italic;'>Mot de passe valides !</font><br><br>" +
								  "La structure \"<font color='darkblue'><b>" + js_nom_societe + "</font></b>\" à été créée avec succès !<br>" +
								  "Veuillez transmettre ce mot de passe à tous les utilisateurs faisant partie de votre structure afin qu'ils puissent accéder à votre environnement (liste des projets, tâches, etc.).<br><br>" +
								  "<b>Vous allez être redirigé vers la page de login afin de charger la nouvelle configuration de votre profil !</b><br><br>" +
								  "Merci pour votre confiance et bonne utilisation.<br><br>" +
								  "<font style='text-align:right;'><a href='mailto:gael.cadoret@yahoo.fr'>Gael CADORET</a></font>";
	
	$.msgbox(
		js_message_societe_cree,
		{
		  type    : "valid",
		  buttons : [
			{
				type: 'submit', 
				value:'Continuer'
			}
		  ]
		}, function(buttonPressed) {
		 
			rediriger("index.php?logout");
		});
}

var compteur_identification = 0;

function affiche_identification()
{
	var js_email = $("#email").val();
	var js_message_accueil = $("#message_accueil").val();
	
	$.msgbox("<p>" + js_message_accueil + "</p>", {
			type    : "prompt",
			inputs  : [
			  {type: "text",     label: "Votre email :", value: js_email, required: true},
			  {type: "password", label: "Mot de passe de la société :", required: true}
			],
			buttons : [
			  {type: "submit", value: "OK"}
			]
	  }, function(email, password) {
			if( (email) && (password) ) {
			  compteur_identification++;
			  var id_societe = $("#id_societe").val();
			  Ajax_verif_password_societe(email, id_societe, password);
			}
	  });
}

function fin_erreur_identification()
{
	var js_message_fin_erreur = $("#message_fin_erreur").val();
	
	$.msgbox(
		js_message_fin_erreur,
		{
		  type    : "error",
		  buttons : [
			{
				type: 'submit', 
				value:'Annuler'
			}
		  ]
		}, function(buttonPressed) {
			rediriger("index.php?logout");
		});
}

function affiche_erreur_societe()
{
	var js_message_erreur = $("#message_erreur").val();
	
	$.msgbox(
		js_message_erreur,
		{
		  type    : "error",
		  buttons : [
			{
				type: 'submit', 
				value:'Réessayer'
			}
		  ]
		}, function(buttonPressed) {
		  
			if(compteur_identification < 3)
				affiche_identification();
			else
				fin_erreur_identification();

		  
		});
}

function affiche_bienvenue_societe()
{
	var js_message_valid_password = $("#message_valid_password").val();
	
	$.msgbox(
		js_message_valid_password,
		{
		  type    : "valid",
		  buttons : [
			{
				type: 'submit', 
				value:'Continuer'
			}
		  ]
		}, function(buttonPressed) {
		  // your code here
		  //alert("Bienvenue dans la société !");
			rediriger("index.php");
		});
}

function date_heure(id)
{
	date = new Date;
	
	annee = date.getFullYear();
	
	moi = date.getMonth();
	mois = new Array('Janvier', 'F&eacute;vrier', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Ao&ucirc;t', 'Septembre', 'Octobre', 'Novembre', 'D&eacute;cembre');
	
	j = date.getDate();
	jour = date.getDay();
	jours = new Array('Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi');
	
	h = date.getHours();
	
	if(h<10)
	{	h = "0"+h; }
	
	m = date.getMinutes();
	
	if(m<10)
	{	m = "0"+m; }
	
	s = date.getSeconds();
	
	if(s<10)
	{	s = "0"+s;	}
	
	resultat = annee + '-' + (moi+1) + '-' + j + ' ' +h+':'+m+':'+s;
	
	//document.getElementById(id).innerHTML = resultat;
	
	return resultat;
}

function mapause(time)
{
   d = new Date();
   diff = 0;
    while(diff < time)
    {
      n = new Date();
      diff = n-d;
     } 
}

function rediriger(url)
{
	window.location = url;
}

function rediriger_NW(url)
{
	window.open(url);
}
