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

Version 0.9.9

Copyright (C) 2006-2009  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

Web : http://smitst.homelinux.com
****************************************************************************************/


// Vérification des tentatives de hack
$pos=strpos($_SERVER['PHP_SELF'],"inc.php");
if ($pos===FALSE)
	$ok_inc=TRUE;
else
	$ok_inc=FALSE;
if ($ok_inc==FALSE) header("Location: ../index.php");

/****************************************/
/* Fonctions pour les accès aux retours */
/* et fichiers rattachés                */
/****************************************/

/** Produits à retourner **/

// Nombre d'enregistrements produits à retourner
function nb_retp()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRETP;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Lecture d'un enregistrement produit à retourner
function lit_enr_retp($code,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETP. "` WHERE `retp_code` = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}

// Vérifie existence NS dans une fiche produit à retourner
function exist_ns_retp($ns,$open)
{
	$exist=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETP. "` WHERE `retp_ns` = '$ns'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$exist=TRUE;
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $exist;
}


// Vérifie existence intervention dans une fiche produit à retourner
function exist_int_retp($nint,$open)
{
	$exist=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETP. "` WHERE `retp_codeint` = '$nint'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$exist=TRUE;
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $exist;
}


/** Demandes de retour **/

// Nombre d'enregistrements demandes de retour
function nb_retd()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRETD;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Lecture d'un enregistrement demande de retour pour un produit donné
function lit_enr_retd($codepdt,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETD. "` WHERE `retd_coderetp` = '$codepdt'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}

// Vérifie existence demande de retour pour un produit donné
function exist_enr_retd($codepdt,$open)
{
	$exist=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETD. "` WHERE `retd_coderetp` = '$codepdt'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$exist=TRUE;
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $exist;
}


/** Envois Retours **/

// Nombre d'enregistrements envois de retours
// Pour un code envoi donné ou pour tous
function nb_rete($codeenv)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRETE;
	if (trim($codeenv)<>"")
		$rech2.= " WHERE `rete_codeenv` = '$codeenv'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Lecture d'un enregistrement envoi de retour
function lit_enr_rete($code,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETE. "` WHERE `rete_code` = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}

// Vérifie existence envoi de retour pour un produit donné
function exist_enr_rete($codepdt,$open)
{
	$exist=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETEP. "` WHERE `retep_coderetp` = '$codepdt'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$exist=TRUE;
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $exist;
}

// Vérifie existence envoi de retour clos pour un produit donné
function exist_enr_rete_clos($codepdt,$open)
{
	$exist=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETEP. "` WHERE `retep_coderetp` = '$codepdt'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
	{
		if ($row2 = mysql_fetch_assoc($rs2))
		{
			extract($row2);
			$rech2 = "SELECT rete_clos FROM `" .TBRETE. "` WHERE `rete_code` = '$retep_codeenv' AND `rete_clos` = '1'";
			$rs2 = mysql_query($rech2)or die(mysql_error());
			if (mysql_num_rows($rs2)>0)
				$exist=TRUE;
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $exist;
}

// Vérifie Nombre de produit dans un envoi
function nb_art_in_envoi($coderete,$open)
{
	$nb=0;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETEP. "` WHERE `retep_codeenv` = '$coderete'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nb=mysql_num_rows($rs2);
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	return $nb;
}

// Vérifie Nombre de produit traités dans un envoi
function nb_art_traites_in_envoi($coderete,$open)
{
	$nb=0;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETEP. "` WHERE `retep_codeenv` = '$coderete'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
	{
		while ($row2 = mysql_fetch_assoc($rs2)) 
		{         
			extract($row2);
			$rech3 = "SELECT * FROM `" .TBRETR. "` WHERE `retr_coderetp` = '$retep_coderetp'";
			$rs3 = mysql_query($rech3)or die(mysql_error());
			if (mysql_num_rows($rs3)>0)
				$nb++;
			mysql_free_result($rs3);
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $nb;
}

// Vérifie si un envoi est clos
function envoi_clos($coderete,$open)
{
	$clos=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT rete_clos FROM `" .TBRETE. "` WHERE `rete_code` = '$coderete'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
	{
		if ($row2 = mysql_fetch_assoc($rs2)) 
			extract($row2);
		mysql_free_result($rs2);
		return $rete_clos;
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $clos;
}

// Donne le nombre de renvois non clos pour un distribueur donné
function nb_envoi_nonclos($codemque,$open)
{
	$nb=0;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETE. "` WHERE `rete_codemque` = '$codemque'";
	$rech2.= " AND `rete_clos` = '0'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nb=mysql_num_rows($rs2);
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $nb;
}

// Donne la date d'envoi
function date_envoi($coderete,$open)
{
	$dateenv="0000-00-00";
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT rete_dateenv FROM `" .TBRETE. "` WHERE `rete_code` = '$coderete'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
	{
		if ($row2 = mysql_fetch_assoc($rs2)) 
			extract($row2);
		mysql_free_result($rs2);
		return $rete_dateenv;
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $clos;
}

// Vérifie si un envoi est entièrement soldé
function envoi_solde($coderete,$open)
{
	$solde=TRUE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETEP. "` WHERE `retep_codeenv` = '$coderete'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
	{
		while ($row2 = mysql_fetch_assoc($rs2)) 
		{         
			extract($row2);
			$rech3 = "SELECT * FROM `" .TBRETR. "` WHERE `retr_coderetp` = '$retep_coderetp' AND `retr_clos` = '1'";
			$rs3 = mysql_query($rech3)or die(mysql_error());
			if (mysql_num_rows($rs3)==0)
			{
				mysql_free_result($rs3);
				return FALSE;
			}
		}
		mysql_free_result($rs3);
	}
	else
		$solde=FALSE;
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $solde;
}

/** Retours fournisseurs **/

// Nombre d'enregistrements retours fournisseur
function nb_retr()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRETR;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Lecture d'un enregistrement retour fournisseur pour un produit donné
function lit_enr_retr($codepdt,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETR. "` WHERE `retr_coderetp` = '$codepdt'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}

// Vérifie existence retour pour un produit donné
function exist_enr_retr($codepdt,$open)
{
	$exist=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETR. "` WHERE `retr_coderetp` = '$codepdt'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$exist=TRUE;
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $exist;
}

// Dit si traitement de retour est clot
function clos_retr($codepdt,$open)
{
	$clos=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBRETR. "` WHERE `retr_coderetp` = '$codepdt' AND `retr_clos` = '1' ";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$clos=TRUE;
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $clos;
}


/** Moyens d'envoi **/

// Lecture d'un enregistrement moyen d'envoi
function lit_enr_me($code,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM " .TBME. " WHERE me_code = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}

// Donne le nom d'un moyen d'envoi
function nom_me($code,$open)
{
	$me_nom="";
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT me_nom FROM " .TBME. " WHERE me_code = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		if ($row2 = mysql_fetch_array($rs2))
			extract($row2);
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $me_nom;
}

// Nombre d'enregistrements moyens d'envoi
function nb_me()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBME;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

/** Types de demandes **/

// Lecture d'un enregistrement type de demande
function lit_enr_md($code,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM " .TBMD. " WHERE md_code = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}

// Donne le nom d'un type de demande
function nom_md($code,$open)
{
	$md_nom="";
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT md_nom FROM " .TBMD. " WHERE md_code = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		if ($row2 = mysql_fetch_array($rs2))
			extract($row2);
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $md_nom;
}

// Nombre d'enregistrements type de demande
function nb_md()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBMD;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

/** Création liste des articles sur fiche d'envoi **/
// Création du code Javascript pour affichage des articles de l'intervention

function cre_js_pdte($td,$code,$from)
{
	global
		$txt_ent_lst_clic;
		
	echo "<script language=\"javascript\">\n";
	
	echo "function updatenv() {\n";
	echo "var myid = ".$from."document.getElementById(\"detpdte\");\n";
	echo "myid.innerHTML = '';\n";

	if (nb_art_in_envoi($code,TRUE)>0)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);

		// *** Affichage de la table
		$str_html = "<table class=\"Ltable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";     

		// Création entête
		$str_html.="<th>";
		$str_html.="Code";
		$str_html.="</th>";

		$str_html.="<th>";
		$str_html.="Code Fabr.";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Désignation";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="RMA";
		$str_html.="</th>";
		
		// Récupération des données de l'envoi
		$rech = "SELECT * FROM ".TBRETE." WHERE rete_code='".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());
			
		if ($row = mysql_fetch_assoc($rs))
		{
			extract($row);
			   
			if (($td=="edit") OR (($td=="view") AND ($rete_clos==1)))
			{
				$str_html.="<th>";
				$str_html.=$txt_ent_lst_clic;
				$str_html.="</th>";
			}
	
			// Lignes des articles
			$rech = "SELECT * FROM ".TBRETEP." WHERE retep_codeenv='".$code."' ORDER BY retep_id";
			$rs = mysql_query($rech) or die(mysql_error());
				
			$i = 0;
			
			while ($row = mysql_fetch_assoc($rs))
			{
				extract($row);
				   
				$rech2 = "SELECT * FROM ".TBRETP." WHERE retp_code='".$retep_coderetp."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
					
				if ($row2 = mysql_fetch_assoc($rs2))
				{
					extract($row2);
					   
					$rech2 = "SELECT * FROM ".TBRETD." WHERE retd_coderetp='".$retep_coderetp."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
						
					if ($row2 = mysql_fetch_assoc($rs2))
					{
						extract($row2);
						   
						// Couleurs alternées
						if($i%2 == 0)  
						   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter1'\" class=\"TRalter1\">";
						else
						   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter2'\" class=\"TRalter2\">";
			
						// Code produit à retourner
						$str_html.="<td class=\"tdlst\" valign=\"top\">";
						$str_html.=$retp_code;
						if ($rete_clos<>0)
						{
							$str_html.=" ";
							$str_html.="<img src=\"../img/ok.png\" border=\"0\" align=\"absmiddle\" title=\"Produit traité\"></a>";
						}
						$str_html.="</td>";
			
						// Code fabricant
						$str_html.="<td class=\"tdlst\" valign=\"top\">";
						$str_html.=$retd_codepdtm;
						$str_html.="</td>";
		
						// Désignation
						$str_html.="<td class=\"tdlst\" valign=\"top\">";
						$str_html.=$retp_des;

						// Affichage ligne(s) du commentaire
						if (trim($retp_ns) <> "")
						{
							$str_html.="<div class=\"annot_moy_it\">";
							$txt=" Sur ".$desart;
							$str_html.="N/S : ".$retp_ns;
							$str_html.="</div>";
						}
						$str_html.="</td>";
			
						// RMA
						$str_html.="<td class=\"tdlst\" valign=\"top\">";
						$str_html.=$retd_rma;
						$str_html.="</td>";

						if ($td == "edit")
						{
							$urldelpdt = "retep_enr.php?td=del&tdo=".$td."&ce=".$code."&cp=".$retep_coderetp;
							$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
							$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urldelpdt."',1,1,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no')\")>";
							$str_html.="<img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\" title=\"Supprimer la ligne\"></a>";
							$str_html.="</td>";
						}
						else
						{
							if ($rete_clos==1)
							{
								$rech2 = "SELECT * FROM ".TBRETR." WHERE retr_coderetp='".$retep_coderetp."'";
								$rs2 = mysql_query($rech2) or die(mysql_error());

								$urlretpdt="";
								$txt="";
								$nimg="";
								$tailleh=1;
								$taillev=1;
								$clos_delok=FALSE;
								if (mysql_num_rows($rs2)>0)
								{
									if ($row2 = mysql_fetch_assoc($rs2))
									{
										extract($row2);
										
										if ($retr_clos==0)
										{
											$urlretpdt = "retr_maj.php?td=edit&tdo=".$td."&cpr=".$retep_coderetp."&ce=".$code."&tx=e";;
											$txt="Editer retour";
											$nimg="edit.png";
											$tailleh=1024;
											$taillev=650;
											$clos_delok=TRUE;
										}
										else
										{
											$urlretpdt = "retr_maj.php?td=view&tdo=".$td."&cpr=".$retep_coderetp."&ce=".$code."&tx=e";;
											$txt="Voir retour";
											$nimg="eye.png";
											$tailleh=1024;
											$taillev=650;
										}
									}
								}
								else
								{
									$urlretpdt = "retr_maj.php?td=add&tdo=".$td."&cpr=".$retep_coderetp."&ce=".$code."&tx=e";;
									$txt="Traiter retour";
									$nimg="retr.png";
									$tailleh=1024;
									$taillev=650;
								}
									
								if ($urlretpdt<>"")
								{
									$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
									$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urlretpdt."',".$tailleh.",".$taillev.",'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no')\")>";
									$str_html.="<img src=\"../img/".$nimg."\" border=\"0\" align=\"absmiddle\" title=\"".$txt."\"></a>";
									if ($clos_delok==TRUE)
									{
										$str_html.=" ";
										$urldelpdt = "retr_maj.php?td=del&tdo=".$td."&cpr=".$retep_coderetp."&ce=".$code."&tx=e";
										$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urldelpdt."',".$tailleh.",".$taillev.",'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no')\")>";
										$str_html.="<img src=\"../img/delp.pg\" border=\"0\" align=\"absmiddle\" title=\"Supprimer le traitement\"></a>";
									}
									$str_html.="</td>";
								}
								else
								{
									$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
									$str_html.=" ";
									$str_html.="</td>";
								}
							}
						}

						$str_html.="</tr>";		
						$i++;
					}
				}
			}
		}
		mysql_close($conn);

		// Fermeture de la table
		$str_html.="</table>";

		// MAJ de l'id
		echo "var str_html = '".addslashes($str_html)."';\n";
		echo "myid.innerHTML=str_html;\n";

	}
	echo "}\n";
	
	echo "</script>\n";
}

// Création du code Javascript pour affichage des moyens d'envoi
function cre_js_me($from)
{
	echo "<script language=\"javascript\">\n";
	
	echo "function updatme() {\n";
	echo "var myid = ".$from."document.getElementById(\"codeme\");\n";
	echo 'if (myid) {';
	echo "myid.innerHTML = '';\n";

	echo 'option = '.$from.'document.createElement("option");';
	echo 'option.setAttribute("value","");';
	echo 'option.innerHTML ="-- Choisissez un  mode --";';
	echo 'myid.appendChild(option);';

	echo 'option = '.$from.'document.createElement("option");';
	echo 'option.setAttribute("value","-1");';
	echo 'option.innerHTML ="Sans objet";';
	echo 'myid.appendChild(option);';

	// On se connecte a la base
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);

	$rech = "SELECT * FROM ".TBME." ORDER BY me_nom";
	$rs = mysql_query($rech) or die(mysql_error());
	if (mysql_num_rows($rs) <> 0)
	{
		while ($resultat = mysql_fetch_array($rs)) 
		{
			extract($resultat);
			echo 'option = '.$from.'document.createElement("option");';
			echo 'option.setAttribute("value","'.$me_code.'");';
			echo 'option.innerHTML ="'.$me_nom.'";';
			echo 'myid.appendChild(option);';
		}
	}
	mysql_free_result($rs);
	mysql_close();
	echo "}\n";
	echo "}\n";
	
	echo "</script>\n";
}

?>