<?PHP
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.6
Copyright (C) 2006  Sylvain FATOME

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr
****************************************************************************************/

/*****************************************************/
/* Fonctions de MAJ des textes types pour les emails */
/*****************************************************/

// Récupération et formatage données texte email

function txt_formate($type,$mode,$orig,$dest)
{
	$codetxt = str_majuscules($type);
		
	$codetxt .= "_".str_majuscules($mode);
	$codetxt .= "_".str_majuscules($orig);
	$codetxt .= "_".str_majuscules($dest);

	$row = lit_enr_texte($codetxt);
	extract ($row);
	
	return $txt_texte;
}

// Mise à jour d'un texte en fonction de codes de champs

function parse_txtmail($type,$code,$texte)
{
 	// Balayage de 1er niveau
 	switch ($type)
	{
		// Agence
		case "age" :
					$row = lit_enr_age($code);
					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$code."'";
					break;
					
		// Centre de gestion
		case "cdg" :
					$row = lit_enr_cdg();
					$rech2 = "SELECT * FROM `" .TBCG;
					break;
					
		// Client
		case "cli" :
					$row = lit_enr_cli($code);
					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$code."'";
					break;
					
		// Contrat
		case "con" :
					$row = lit_enr_cocl($code);
					$rech2 = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_code` = '".$code."'";
					break;
					
		// Demande
		case "dde" :
					$row = lit_enr_dde($code);
					$rech2 = "SELECT * FROM `" .TBDDE. "` WHERE `dde_code` = '".$code."'";
					break;
					
		// Fichiers Internes : Répertoire
		case "dir" :
					$row = lit_enr_fic($code);
					$rech2 = "SELECT * FROM `" .TBFIC. "` WHERE `fic_nom` = '".$code."'";
					break;
					
		// Fichiers Internes : Fichier
		case "fic" :
					$row = lit_enr_fic($code);
					$rech2 = "SELECT * FROM `" .TBFIC. "` WHERE `fic_nom` = '".$code."'";
					break;
					
		// Intervention
		case "int" :
					$row = lit_enr_int($code);
					$rech2 = "SELECT * FROM `" .TBINT. "` WHERE `int_code` = '".$code."'";
					break;
					
		// Opérateur
		case "ope" :
					$row = lit_enr_ope($code);
					$rech2 = "SELECT * FROM `" .TBOPE. "` WHERE `ope_code` = '".$code."'";
					break;
					
		// Relevé (Dossier)
		case "rel" :
					$row = lit_enr_rel($code);
					$rech2 = "SELECT * FROM `" .TBREL. "` WHERE `rel_code` = '".$code."'";
					break;
					
		// Relevé (Licence)
		case "rlic" :
					$row = lit_enr_rlic($code);
					$rech2 = "SELECT * FROM `" .TBRLIC. "` WHERE `rlic_code` = '".$code."'";
					break;
					
		// Relevé (Matériel)
		case "rpdt" :
					$row = lit_enr_rpdt($code);
					$rech2 = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_code` = '".$code."'";
					break;
					
		// Déclaration de problème
		case "rpbm" :
					$row = lit_enr_rpbm($code);
					$rech2 = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_id` = '".$code."'";
					break;
					
		// Technicien
		case "tec" :
					$row = lit_enr_tec($code);
					$rech2 = "SELECT * FROM `" .TBTEC. "` WHERE `tec_code` = '".$code."'";
					break;
					
		default : break;
	}


	if (is_array($row))
		extract($row);
	
	$txt = $texte;
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$num = mysql_num_fields($rs2);
	$i = 0;
	for ($i = 0; $i < $num; $i++) 
	{
		$nom = mysql_field_name($rs2, $i);
		$nom_rech = "%".$nom."%";
		if (mysql_field_type($rs2, $i) <> "date")
			$txt = eregi_replace($nom_rech,$row[$i],$txt);
		else
			$txt = eregi_replace($nom_rech,formatedate($row[$i]),$txt);
		
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	// Balayage des champs centre de gestion
	$row = lit_enr_cdg();

	$txt = ereg_replace("%cp_cge%",Retournecp($row["cge_ville"]),$txt);
	$txt = ereg_replace("%ville_cge%",Retourneville($row["cge_ville"]),$txt);

	$rech2 = "SELECT * FROM `" .TBCG;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$num = mysql_num_fields($rs2);
	$i = 0;
	for ($i = 0; $i < $num; $i++) 
	{
		$nom = mysql_field_name($rs2, $i);
		$nom_rech = "%".$nom."%";
		$txt = eregi_replace($nom_rech,$row[$i],$txt);
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	// Balayage pour les champs complémentaires
	switch ($type)
	{
		// Client
		case "cli" :
					// Balayage des champs complémentaires clients
					$row = lit_enr_cli($code);

					$txt = ereg_replace("%cp_cli%",Retournecp($row["cli_ville"]),$txt);
					$txt = ereg_replace("%ville_cli%",Retourneville($row["cli_ville"]),$txt);

					break;
		
		// Contrat
		case "con" :
					// Balayage des champs client
					$row = lit_enr_cli($cocl_codecli);

					$txt = ereg_replace("%cp_cli%",Retournecp($row["cli_ville"]),$txt);
					$txt = ereg_replace("%ville_cli%",Retourneville($row["cli_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$cocl_codecli."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs agence
					$row = lit_enr_age($cocl_codeage);

					$txt = ereg_replace("%cp_age%",Retournecp($row["age_ville"]),$txt);
					$txt = ereg_replace("%ville_age%",Retourneville($row["age_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$cocl_codeage."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs périodicités
					$row = lit_enr_per($cocl_codecope);

					$rech2 = "SELECT * FROM `" .TBPER. "` WHERE `cope_code` = '".$cocl_codecope."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs quotas d'heures

					$txt = ereg_replace("%heures_cocl%",sec_heure($cocl_nbs),$txt);

					break;
					
		// Demande
		case "dde" :
					// Balayage des champs code postal et ville

					$txt = ereg_replace("%cp_dde%",Retournecp($dde_ville),$txt);
					$txt = ereg_replace("%ville_dde%",Retourneville($dde_ville),$txt);

					// Balayage des types de demande
					$row = lit_enr_ddet($dde_codeddet);
					$txt = ereg_replace("%ddet_desc%",$row["ddet_desc"],$txt);

					break;
					
		// Intervention
		case "int" :
					// Balayage des champs client
					$row = lit_enr_cli($int_codecli);

					$txt = ereg_replace("%cp_cli%",Retournecp($row["cli_ville"]),$txt);
					$txt = ereg_replace("%ville_cli%",Retourneville($row["cli_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$int_codecli."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs agence
					$row = lit_enr_age($int_codeage);

					$txt = ereg_replace("%cp_age%",Retournecp($row["age_ville"]),$txt);
					$txt = ereg_replace("%ville_age%",Retourneville($row["age_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$int_codeage."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					break;
					
		// Relevé
		case "rel" :
					// Balayage des champs client
					$row = lit_enr_cli($rel_codecli);

					$txt = ereg_replace("%cp_cli%",Retournecp($row["cli_ville"]),$txt);
					$txt = ereg_replace("%ville_cli%",Retourneville($row["cli_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$rel_codecli."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs agence
					$row = lit_enr_age($rel_codeage);

					$txt = ereg_replace("%cp_age%",Retournecp($row["age_ville"]),$txt);
					$txt = ereg_replace("%ville_age%",Retourneville($row["age_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$rel_codeage."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					break;

		// Relevé Licence
		case "rlic" :
					// Balayage des champs client
					$row = lit_enr_cli($rlic_codecli);

					$txt = ereg_replace("%cp_cli%",Retournecp($row["cli_ville"]),$txt);
					$txt = ereg_replace("%ville_cli%",Retourneville($row["cli_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$rlic_codecli."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs agence
					$row = lit_enr_age($rlic_codeage);

					$txt = ereg_replace("%cp_age%",Retournecp($row["age_ville"]),$txt);
					$txt = ereg_replace("%ville_age%",Retourneville($row["age_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$rlic_codeage."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					break;

		// Relevé Matériel
		case "rpdt" :
					// Balayage des champs client
					$row = lit_enr_cli($rpdt_codecli);

					$txt = ereg_replace("%cp_cli%",Retournecp($row["cli_ville"]),$txt);
					$txt = ereg_replace("%ville_cli%",Retourneville($row["cli_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$rpdt_codecli."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs agence
					$row = lit_enr_age($rpdt_codeage);

					$txt = ereg_replace("%cp_age%",Retournecp($row["age_ville"]),$txt);
					$txt = ereg_replace("%ville_age%",Retourneville($row["age_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$rpdt_codeage."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					break;

		// Déclaration de problème
		case "rpbm" :
					// Balayage des champs client
					$row = lit_enr_cli($rpbm_codecli);

					$txt = ereg_replace("%cp_cli%",Retournecp($row["cli_ville"]),$txt);
					$txt = ereg_replace("%ville_cli%",Retourneville($row["cli_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$rpbm_codecli."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs agence
					$row = lit_enr_age($row["cli_codeage"]);

					$txt = ereg_replace("%cp_age%",Retournecp($row["age_ville"]),$txt);
					$txt = ereg_replace("%ville_age%",Retourneville($row["age_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$row["cli_codeage"]."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					break;

		default : break;

	}

	return $txt;
}


// Ajout d'un pied au texte d'email

function add_pied_txtmail()
{
	// Récupération données pied email
	
	$row = lit_enr_texte("MAIL_PIED");
	extract ($row);
	
	return $txt_texte;
}

// Création du corps de l'email

function cre_corps_mail($type,$mode,$orig,$dest,$code,$pass,$total)
{
	$txtcorps = "";

	$txt = txt_formate($type,$mode,$orig,$dest);

	// Création du texte de l'email

	$txtcorps = parse_txtmail($type,$code,$txt);

	//Mot de passe
	$txtcorps = ereg_replace("%pass%",$pass,$txtcorps);

	//Montant total
	$txtcorps = ereg_replace("%total%",number_format($total,2,'.',''),$txtcorps);

	$txtcorps .= "<br>".add_pied_txtmail();

	return $txtcorps;

}


// Envoi des emails

function envoi_mail($sujet,$type,$mode,$orig,$dest,$code,$pass,$total,$copieadmin)
{
	global
		$admin_name,
		$admin_mail,
		$smtp_ip,
		$smtp_port;
				
	$mail = new SMTP;
	$mail->Delivery('client');
	$mail->Priority('high');

	// Destinataires

	if ($dest == "wm") // Le destinataire est le webmaster
		$mail->AddTo($admin_mail);

	switch ($type) // Analyse du type d'opération
	{
		// Agence
		case "age" :
					if ($dest == "age")
					{
						$row = lit_enr_age($code);
						extract ($row);
						$mail->AddTo($age_emaild);
						if (array_key_exists($age_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($age_emailt);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($cge_emailt);
					}
					break;
					
		// Centre de Gestion
		case "cdg" :
					$row = lit_enr_cdg();
					extract ($row);
					$mail->AddTo($cge_emaild);
					if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
						$mail->AddTo($cge_emailt);
					break;
					
		// Client
		case "cli" :
					if ($dest == "cli")
					{
						$row = lit_enr_cli($code);
						extract ($row);
	
						if ($cli_email <> "") // Contact Principal
							$mail->AddTo($cli_email);
							
						$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);

						// Si on crée un compte client, ou si on modifie le MDP, ou si déménagement copie à direction direction uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MDP") OR (str_majuscules($mode) == "DEM"))
						{
							$rexist = "SELECT DISTINCT * FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$cli_code' AND `ccli_maild` = '1' AND `ccli_email` <> '".$cli_email."'";
							$rs2 = mysql_query($rexist) or die(mysql_error());
							if (mysql_num_rows($rs2) > 0)
							{
								while ($resultat = mysql_fetch_array($rs2)) 
								{
									extract($resultat);
									if (array_key_exists($ccli_email, $mail->_toaddrs)==false)
										$mail->AddTo($ccli_email);
								}
							}
							mysql_free_result($rs2);
						}
						mysql_close($conn2);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($cge_emailt);
					}
					break;
					
		// Contrat
		case "con" :
					if ($dest == "cli")
					{
						$row = lit_enr_cocl($code);
						extract ($row);
	
						$row = lit_enr_cli($cocl_codecli);
						extract ($row);
	
						if ($cli_email <> "") // Contact Principal
							$mail->AddTo($cli_email);
							
						$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);

						// Si on crée un contrat, on le modifie, ou on en accuse réception, copies à direction et technique uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MOD") OR (str_majuscules($mode) == "ENV") OR (str_majuscules($mode) =="AR"))
						{
							$rexist = "SELECT DISTINCT * FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$cli_code' AND (`ccli_maild` = '1' OR `ccli_mailt` = '1') AND `ccli_email` <> '".$cli_email."'";
							$rs2 = mysql_query($rexist) or die(mysql_error());
							if (mysql_num_rows($rs2) <> 0)
							{
								while ($resultat = mysql_fetch_array($rs2)) 
								{
									extract($resultat);
									if (array_key_exists($ccli_email, $mail->_toaddrs)==false)
										$mail->AddTo($ccli_email);
								}
							}
							mysql_free_result($rs2);
						}
						mysql_close($conn2);
					}						
					if ($dest == "age")
					{
						$row = lit_enr_cocl($code);
						extract ($row);
						$row = lit_enr_age($cocl_codeage);
						extract ($row);
						$mail->AddTo($age_emaild);
						if (array_key_exists($age_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($age_emailt);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($cge_emailt);
					}
						
					break;
					
		// Si demande
		case "dde" :
					if (str_majuscules($mode) == "CRE") // On est en mode demande
					{
						$row = lit_enr_dde($code);
						extract ($row);
						if ($dde_codedded <> "VA")
						{
							$row = lit_enr_dded($dde_codedded);
							extract ($row);
							$mail->AddTo($dded_email);
						}
						else // Le destinataire est "Votre agence"
						{
							if (@$HTTP_SESSION_VARS["niv_log"] == 1) // On vérifie que c'est un client qui demande
							{
								$row = lit_enr_cli(@$HTTP_SESSION_VARS["code_log"]);
								extract ($row);
								if ($cli_prop <> NULL) // Le client est géré par une agence
								{
									$row = lit_enr_age($cli_prop);
									extract ($row);
									$mail->AddTo($age_email);
								}
								else // Le client n'est pas géré par une agence, on envoie à toutes les agences de sa zone
								{
									$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
									mysql_select_db(DBNAME);
									$rech = "SELECT * FROM " .TBAGE;
									$rs = mysql_query($rech) or die(mysql_error());
									if (mysql_num_rows($rs) >0)
									{
										while ($row = mysql_fetch_assoc($rs)) 
										{
											extract($row);
											if ($age_zone <> "") 
											{
												$zone_tab = explode("-",$age_zone);
												
												if (in_array($cli_dep,$zone_tab))
												{
													if (array_key_exists($age_email, $mail->_toaddrs)==false)
														$mail->AddTo($age_email);
												}
											}
										}
									}
									mysql_free_result($rs);
									mysql_close($conn);
								}
							}
						}
					}
					if (str_majuscules($mode) == "AR") // On est en mode accusé de réception
					{
						$row = lit_enr_dde($code);
						extract ($row);
						$mail->AddTo($dde_email);
					}
					break;
					
		// Intervention
		case "int" :
					if ($dest == "cli")
					{
						$row = lit_enr_int($code);
						extract ($row);
	
						$row = lit_enr_cli($int_codecli);
						extract ($row);
	
						if ($cli_email <> "") // Contact Principal
							$mail->AddTo($cli_email);
							
						$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);

						// Si on crée une intervention, ou on la termine, copie à technique uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "FAIT"))
						{
							$rexist = "SELECT DISTINCT * FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$cli_code' AND `ccli_mailt` = '1' AND `ccli_email` <> '".$cli_email."'";
							$rs2 = mysql_query($rexist) or die(mysql_error());
							if (mysql_num_rows($rs2) <> 0)
							{
								while ($resultat = mysql_fetch_array($rs2)) 
								{
									extract($resultat);
									if (array_key_exists($ccli_email, $mail->_toaddrs)==false)
										$mail->AddTo($ccli_email);
								}
							}
							mysql_free_result($rs2);
						}
						mysql_close($conn2);
					}						
					if ($dest == "age")
					{
						$row = lit_enr_int($code);
						extract ($row);
						$row = lit_enr_age($int_codeage);
						extract ($row);
						$mail->AddTo($age_emaild);
						if (array_key_exists($age_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($age_emailt);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($cge_emailt);
					}
						
					break;
					
		// Opérateur
		case "ope" :
					if ($dest == "ope")
					{
						$row = lit_enr_ope($code);
						extract ($row);
						$mail->AddTo($ope_email);
						$destinataires = $ope_email;
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($cge_emailt);
					}
					break;
					
		// Technicien
		case "tec" :
					if ($dest == "tec")
					{
						$row = lit_enr_tec($code);
						extract ($row);
						$mail->AddTo($tec_email);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($cge_emailt);
					}
					break;
					
		// Relevé de parc
		case "rel" :
					if ($dest == "cli")
					{
						$row = lit_enr_rel($code);
						extract ($row);
	
						$row = lit_enr_cli($rel_codecli);
						extract ($row);
	
						if ($cli_email <> "") // Contact Principal
							$mail->AddTo($cli_email);
							
						$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);

						// Si on crée un contrat, ou si on le modifie, copies à technique uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MOD"))
						{
							$rexist = "SELECT DISTINCT * FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$cli_code' AND `ccli_mailt` = '1' AND `ccli_email` <> '".$cli_email."'";
							$rs2 = mysql_query($rexist) or die(mysql_error());
							if (mysql_num_rows($rs2) <> 0)
							{
								while ($resultat = mysql_fetch_array($rs2)) 
								{
									extract($resultat);
									if (array_key_exists($ccli_email, $mail->_toaddrs)==false)
										$mail->AddTo($ccli_email);
								}
							}
							mysql_free_result($rs2);
						}
						mysql_close($conn2);
					}						
					if ($dest == "age")
					{
						$row = lit_enr_rel($code);
						extract ($row);
						$row = lit_enr_age($rel_codeage);
						extract ($row);
						$mail->AddTo($age_emaild);
						if (array_key_exists($age_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($age_emailt);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($cge_emailt);
					}
						
					break;
					
		// Relevé Licence
		case "rlic" :
					if ($dest == "cli")
					{
						$row = lit_enr_rlic($code);
						extract ($row);
	
						$row = lit_enr_cli($rlic_codecli);
						extract ($row);
	
						if ($cli_email <> "") // Contact Principal
							$mail->AddTo($cli_email);
							
						$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);

						// Si on crée un contrat, ou si on le modifie, copies à technique uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MOD"))
						{
							$rexist = "SELECT DISTINCT * FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$cli_code' AND `ccli_mailt` = '1' AND `ccli_email` <> '".$cli_email."'";
							$rs2 = mysql_query($rexist) or die(mysql_error());
							if (mysql_num_rows($rs2) <> 0)
							{
								while ($resultat = mysql_fetch_array($rs2)) 
								{
									extract($resultat);
									if (array_key_exists($ccli_email, $mail->_toaddrs)==false)
										$mail->AddTo($ccli_email);
								}
							}
							mysql_free_result($rs2);

							// On envoie une copie au contact principal du site 
							if ($rpdt_idsite>0)
							{
								$rexist = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '$rpdt_idsite'";
								$rs2 = mysql_query($rexist) or die(mysql_error());
								if (mysql_num_rows($rs2) <> 0)
								{
									if ($resultat = mysql_fetch_array($rs2)) 
									{
										extract($resultat);
										if (array_key_exists($scli_remail, $mail->_toaddrs)==false)
											$mail->AddTo($ccli_email);
									}
								}
								mysql_free_result($rs2);
							}							
						}
						mysql_close($conn2);
					}						
					if ($dest == "age")
					{
						$row = lit_enr_rlic($code);
						extract ($row);
						$row = lit_enr_age($rlic_codeage);
						extract ($row);
						$mail->AddTo($age_emaild);
						if (array_key_exists($age_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($age_emailt);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($cge_emailt);
					}
						
					break;
					
		// Relevé Produit
		case "rpdt" :
					if ($dest == "cli")
					{
						$row = lit_enr_rpdt($code);
						extract ($row);
	
						$row = lit_enr_cli($rpdt_codecli);
						extract ($row);
	
						if ($cli_email <> "") // Contact Principal
							$mail->AddTo($cli_email);
							
						$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);

						// Si on crée un contrat, ou si on le modifie, copies à technique uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MOD"))
						{
							$rexist = "SELECT DISTINCT * FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$cli_code' AND `ccli_mailt` = '1' AND `ccli_email` <> '".$cli_email."'";
							$rs2 = mysql_query($rexist) or die(mysql_error());
							if (mysql_num_rows($rs2) <> 0)
							{
								while ($resultat = mysql_fetch_array($rs2)) 
								{
									extract($resultat);
									if (array_key_exists($ccli_email, $mail->_toaddrs)==false)
										$mail->AddTo($ccli_email);
								}
							}
							mysql_free_result($rs2);

							// On envoie une copie au contact principal du site 
							if ($rpdt_idsite>0)
							{
								$rexist = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '$rpdt_idsite'";
								$rs2 = mysql_query($rexist) or die(mysql_error());
								if (mysql_num_rows($rs2) <> 0)
								{
									if ($resultat = mysql_fetch_array($rs2)) 
									{
										extract($resultat);
										if (array_key_exists($scli_remail, $mail->_toaddrs)==false)
											$mail->AddTo($ccli_email);
									}
								}
								mysql_free_result($rs2);
							}							
						}
						mysql_close($conn2);
					}						
					if ($dest == "age")
					{
						$row = lit_enr_rpdt($code);
						extract ($row);
						$row = lit_enr_age($rpdt_codeage);
						extract ($row);
						$mail->AddTo($age_emaild);
						if (array_key_exists($age_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($age_emailt);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($cge_emailt);
					}
						
					break;
					
		// Déclaration de problème
		case "rpbm" :
					if ($dest == "cli")
					{
						$row = lit_enr_rpbm($code);
						extract ($row);
						
						$row = lit_enr_cli($rpbm_codecli);
						extract ($row);
	
						if ($cli_email <> "") // Contact Principal
							$mail->AddTo($cli_email);
							
						$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);

						// Si on crée un contrat, ou si on le modifie, copies à technique et contact du site (si existe) uniquement
						if ((str_majuscules($mode) == "CRE") OR (str_majuscules($mode) == "MOD"))
						{
							// On envoie des copies aux contacts techniques du client
							$rexist = "SELECT DISTINCT * FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$cli_code' AND `ccli_mailt` = '1' AND `ccli_email` <> '".$cli_email."'";
							$rs2 = mysql_query($rexist) or die(mysql_error());
							if (mysql_num_rows($rs2) <> 0)
							{
								while ($resultat = mysql_fetch_array($rs2)) 
								{
									extract($resultat);
									if (array_key_exists($ccli_email, $mail->_toaddrs)==false)
										$mail->AddTo($ccli_email);
								}
							}
							mysql_free_result($rs2);

							// On envoie une copie au contact principal du site 
							if ($rpbm_idsite>0)
							{
								$rexist = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '$rpbm_idsite'";
								$rs2 = mysql_query($rexist) or die(mysql_error());
								if (mysql_num_rows($rs2) <> 0)
								{
									if ($resultat = mysql_fetch_array($rs2)) 
									{
										extract($resultat);
										if (array_key_exists($scli_remail, $mail->_toaddrs)==false)
											$mail->AddTo($ccli_email);
									}
								}
								mysql_free_result($rs2);
							}							
						}
						
						mysql_close($conn2);
					}						
					if ($dest == "age")
					{
						$row = lit_enr_rpbm($code);
						extract ($row);
						
						switch ($rpbm_typrel)
						{
							case 1 :
								$row = lit_enr_rlic($rpbm_coderlp);
								extract ($row);
								$row = lit_enr_age($rlic_codeage);
								extract ($row);
								break;
							case 2 :
								$row = lit_enr_rpdt($rpbm_coderlp);
								extract ($row);
								$row = lit_enr_age($rpdt_codeage);
								extract ($row);
								break;
						}
						
						$mail->AddTo($age_emaild);
						if (array_key_exists($age_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($age_emailt);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if (array_key_exists($cge_emailt, $mail->_toaddrs)==false)
							$mail->AddTo($cge_emailt);
					}
						
					break;
					
		default : break;
	}
	

	// Entête de l'email

	//Expédié par
	$mail->From($admin_mail, $admin_name);

	//Réponse automatique à...
	$mail->AddHeader('Reply-To', $admin_mail);

	//Gestion des retours d'erreurs
	$mail->AddHeader('Return-Path', $admin_mail);
	$mail->AddHeader('Errors-To', $admin_mail);

	// Corps de l'email
	
	$txtcorps = "";
	$txtcorps = cre_corps_mail($type,$mode,$orig,$dest,$code,$pass,$total);

	if(($mail->_toaddrs && count($mail->_toaddrs) > 0) && (trim($txtcorps)<>"") && (trim($sujet)<>""))
	{
		$mail->Html($txtcorps);
		$sent = $mail->Send($sujet);

		//Copie du mail à l'administrateur ?
		if (($copieadmin == TRUE) AND ($admin_mail <> ""))
		{
			$mail->DelTo();
			$mail->AddTo($admin_mail);
			$copie_sujet = $sujet." (Copie Administrateur)";
			$sent = $mail->Send($copie_sujet);
		}
	}
	else
		$sent=false;
}

?>