<?PHP

/*****************************************************/
/* 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 `" .TBCDG;
	$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('local');
	$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 ($age_emailt <> $age_emaild)
							$mail->AddTo($age_emailt);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if ($cge_emaild <> $cge_emailt)
							$mail->AddTo($cge_emailt);
					}
					break;
					
		// Centre de Gestion
		case "cdg" :
					$row = lit_enr_cdg();
					extract ($row);
					$mail->AddTo($cge_emaild);
						if ($cge_emaild <> $cge_emailt)
							$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 ($ccli_email <> $cli_email)
										$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 ($cge_emaild <> $cge_emailt)
							$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 ($ccli_email <> $cli_email)
										$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 ($age_emailt <> $age_emaild)
							$mail->AddTo($age_emailt);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if ($cge_emaild <> $cge_emailt)
							$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))
												{
													$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 ($cli_email <> $ccli_email)
										$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 ($age_emailt <> $age_emaild)
							$mail->AddTo($age_emailt);
					}
					if ($dest == "cdg")
					{
						$row = lit_enr_cdg();
						extract ($row);
						$mail->AddTo($cge_emaild);
						if ($cge_emaild <> $cge_emailt)
							$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 ($cge_emaild <> $cge_emailt)
							$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 ($cge_emaild <> $cge_emailt)
							$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);

	$mail->Html($txtcorps);
	$sent = $mail->Send($sujet);

	//Copie du mail à l'administrateur ?
	if (($copieadmin == TRUE) AND ($admin_mail <> ""))
	{
		/*
		$mail = new SMTP;
		$mail->Delivery('local');
		$mail->Priority('high');
		*/
		$mail->DelTo();
		$mail->AddTo($admin_mail);
		/*
		$mail->From($admin_mail, $admin_name);
		$mail->AddHeader('Reply-To', $admin_mail);
		$mail->AddHeader('Return-Path', $admin_mail);
		$mail->AddHeader('Errors-To', $admin_mail);
		$mail->Html($txtcorps);
		*/
		$copie_sujet = $sujet." (Copie Administrateur)";
		$sent = $mail->Send($copie_sujet);
	}
}

?>