<?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");

include_once("../inc/fic_ret.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire
$td = strip_tags(trim($_GET['td']));
$nvil = intval(strip_tags(trim($_GET['nvil'])));
$tx = strip_tags(trim($_GET['tx'])); // Source de l'appel (tx="f" -> depuis une fiche)

$pbm_ville=FALSE;
$msg1_err_type=$ajx_nodeppays;

if ($nvil>0)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rvil="SELECT * FROM ".TBVILLES." WHERE `villes_id`='".$nvil."'";
	$rsvil = mysql_query($rvil) or die(mysql_error());
	if (mysql_num_rows($rsvil) == 1) // Uniquement UN SEUL enregistrement !
	{
		if ($rowvil = mysql_fetch_array($rsvil)) 
			extract ($rowvil);
	}
	else
		$pbm_ville=TRUE;
	mysql_close($conn2); 
} 
else
{
	if ($td<>"add") $pbm_ville=TRUE;
}

switch ($td)
{
	case "add" :
		$titre = "CREATION VILLE";
		$villes_lng=str_majuscules($_SESSION["lng"]);
		break;
	case "edit" :
		$titre = "MODIFICATION VILLE";
		break;
	case "del" :
		$titre = "SUPPRESSION VILLE";
		break;
	default :
		$pbm_ville=TRUE;
		break;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - <?PHP echo $title_esp_prive; ?> : Fiche Ville</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->

<script language="JavaScript" src="../jscript/coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour fenêtres -->
<script language="JavaScript" src="../jscript/fen.js"></script>

<!-- Scripts Javascript pour contrôle de formulaire -->
<script	type="text/javascript" src="../jscript/vform/vform.js"></script>

<script language="javascript">
<!--
var tab_champ=new Array(
	"nom:cp ; format:txt ; req:Y ; txt:Code Postal ; lmx:10; stok:frmok ; stbad:frmbad",
	"nom:nom ; format:txt ; req:Y ; txt:<?PHP echo html_entity_decode($txt_chp_nom); ?> ; lmx:40; stok:frmok ; stbad:frmbad",
	"nom:codepays; format:liste ; req:Y ; txt:<?PHP echo html_entity_decode($txt_chp_pays); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
	"nom:dep; format:liste ; req:Y ; txt:Département ; vmn:1; stok:frmok ; stbad:frmbad");
//-->
</script>

<!-- Scripts Javascript pour alertes -->
<script language="JavaScript" src="../jscript/alertes.js"></script>

<!-- Scripts Javascript divers -->
<script language="JavaScript" src="../jscript/divers.js"></script>

<!-- Scripts Ajax -->
<script type="text/javascript" src="../ajax/util.js"></script>
<script type="text/javascript" src="../ajax/SelectUpdater.js"></script>

<script type="text/javascript">
<!--        
function updateDep()
{
	depUpdater = new SelectUpdater("dep", "<?PHP echo $rep_appli.'/ajax/'; ?>recupdep.php?pays=", "attn1", "msg1", "<?PHP echo $msg1_err_type; ?>");
	anciencodepays = "";
	anciendep = "";
	document.getElementById("codepays").onchange = function() {
		var listepays = document.getElementById("codepays");
		var codepays = listepays.options[listepays.selectedIndex].value;
		if (codepays.length > 0)
		{
			if (codepays != anciencodepays)
			{
				// Faire l'appel Ajax
				depUpdater.run(codepays);
				anciencodepays = codepays;
			}
		}
		else
		{
			// Remettre la liste a vide et annuler l'appel eventuel
			depUpdater.reset();
			anciencodepays = "";
		}
	}
}
//-->
</script>
</head>

<?PHP if ($td == "del"){ ?>
<body onload="warndel();metReadonly();">
<?PHP } else { ?>
<body onLoad="metFocus();updateDep();">
<?PHP } ?>

<?php 
if ($tx<>"f")
	include_once("../inc/entete.inc.php"); 
if ($pbm_ville==FALSE)
{
?>
<p align="center"> <span class="titrepage"><?PHP echo $titre ?></span></p>
<table width="100%"  border="0">
<tr>
	<td width="33%"><strong><em><a href="villes_lst.php"><?PHP echo $txt_lnk_ret_liste; ?></a></em></strong></td>
	<td width="33%">
	<div id="oblig" align="center" class="annot_petit_it">(<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>"> : <?PHP echo $txt_div_saisoblg; ?>)
	</div>
	</td>
	<td width="33%">&nbsp;</td>
</tr>
</table>
<form name="villes_maj" method="post" action="villes_enr.php" onSubmit="javascript:; return (VForm(this,tab_champ,'','<?PHP echo $_SESSION["lng"]; ?>'));">
<input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
<input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
<input name="tx" type="hidden" id="tx" value="<?PHP echo $tx ?>">
<?PHP if (($td=="edit") OR ($td=="del")) { ?>
<input name="nville" type="hidden" id="nville" value="<?PHP echo $villes_id; ?>">
<?PHP } ?>
<table width="100%"  border="0">
<tr>
	<td width="160" class="rubfrm" id="f_codepays" align="right"><?PHP echo $txt_chp_pays; ?></td>
	<td>
		<?PHP
		if (($td =="add") OR ($td=="edit"))  // Ajout/Edition = zone modifiable (on peut choisir un mode)
		{ 
			// Création de la liste des mode d'envoi
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "SELECT * FROM ".TBPAYS;
			if ($td=="edit")
				$rech.= " WHERE pays_lng='".$villes_lng."'";
			$rech.= " ORDER BY pays_nom";
			$rs = mysql_query($rech) or die(mysql_error());

			if (mysql_num_rows($rs) <> 0)
			{
			?>
				<select name="codepays" id="codepays">
				<?PHP
					echo "<option value=\"\">".$sel_lst_pays."</option>";

					while ($resultat = mysql_fetch_array($rs)) 
					{
						extract($resultat);
		
						echo '<option value="'.$pays_code.'"';
						if (($villes_codepays == $pays_code) && ($villes_lng == $pays_lng))
							echo ' selected';
						echo '>'.$pays_nom.'</option>';
					}
				?>
				</select>
				<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
		<?PHP
			}
			mysql_free_result($rs);
			mysql_close($conn);
		} 
		else  // Edition ou suppression = zone non modifiable (affichage seul)
		{
		?>
		<input name="codepays" type="hidden" id="codepays" value="<?PHP echo $villes_codepays ?>">	
		<input name="nompays" type="text" id="nompays" value="<?PHP echo Retournepays_open($villes_codepays,TRUE) ?>" size="50" maxlength="50" readonly="1">
		<?PHP 
		} 
		?>
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_dep" align="right">D&eacute;partement</td>
	<td>
	<?PHP
	if (($td =="add") OR ($td=="edit"))  // Ajout/Edition = zone modifiable (on peut choisir un mode)
	{ 
		// Création de la liste des départements
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM ".TBDEP." WHERE `dep_codepays`='".$villes_codepays."'";
		if ($td=="edit")
			$rech.= " AND dep_lng='".$villes_lng."'";
		$rech.= " ORDER BY dep_code";
		$rs = mysql_query($rech) or die(mysql_error());
	?>
	<select name="dep" size="1" id="dep">
	<?PHP
	echo "<option>".$sel_lst_makechx."</option>";

	if (mysql_num_rows($rs)>0)
	{
		while ($resultat = mysql_fetch_array($rs)) 
		{
			extract($resultat);
			if ($dep_code<>$dep_nom)
				$dep=$dep_code." (".$dep_nom.")";
			else
				$dep=$dep_nom;
			if ($villes_dep == $dep_code)
				echo "<option value=\"".$dep_code."\" selected>".$dep."</option>";
			else
				echo "<option value=\"".$dep_code."\">".$dep."</option>";
		}
	}
	else
		echo "<option value=\"-1\" selected>".$sel_lst_unknown."</option>";
	?>
	</select>
	 <span id="attn1"></span><span id="msg1" style="background-color:#FF0000; color:#FFFF00; font-weight:bold"><?PHP if ((mysql_num_rows($rs)==0) AND ($td<>"del")) echo htmlspecialchars($msg1_err_type)?></span>
	<?PHP
		mysql_free_result($rs);
		mysql_close($conn);
	}
	else
	{
		$dept=Retournedep_open($villes_dep,$villes_codepays,TRUE);
		if ($villes_dep<>$dept)
			$dep=$villes_dep." (".$dept.")";
		else
			$dep=$dept;
	?>
		<input name="dep" type="hidden" id="dep" value="<?PHP echo $age_dep ?>">
		<input name="dep_nom" type="text" id="dep_nom" value="<?PHP echo $dep ?>" size="40"  readonly="1">
	<?PHP
	}
	?>
	</td>
</tr>
<tr>
	<td width="10%" class="rubfrm" id="f_code" align="right">Code Postal</td>
	<td width="43%">
	<?PHP if ($td == "add") { ?>
	<input name="cp" type="text" id="cp" value="<?PHP echo htmlspecialchars($villes_cp) ?>" size="30" >
	<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	<?PHP } else { ?>
	<input name="cp" type="text" id="cp" value="<?PHP echo $villes_cp ?>" size="30" readonly="1">
	<?PHP } ?>
	</td>
</tr>
<tr>
	<td class="rubfrm" id="f_nom" align="right"><?PHP echo $txt_chp_nom; ?></td>
	<td><input name="nom" type="text" id="nom" value="<?PHP echo htmlspecialchars($villes_nom) ?>" size="50">
	<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>"></td>
</tr>
<tr>
	<td width="160" class="rubfrm" id="f_lng" align="right">Langue</td>
	<td>
		<?PHP if ($td == "add")
		{
			echo '<select name="lng" size="1" id="lng">';
			foreach ($lng_langues as $key=>$value)
			{
				echo '<option value="'.$key.'"'; 
				if ($villes_lng == $key)
					echo ' selected';
				echo '>';
				echo $value."</option>";
			}
			echo "</select>";
			echo ' <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">';
		} 
		else  // Edition ou suppression = zone non modifiable (affichage seul)
		{
		?>
		<input name="lng" type="hidden" id="lng" value="<?PHP echo $villes_lng ?>">	
		<input name="nlng" type="text" id="nlng" value="<?PHP echo $lng_langues[$villes_lng] ?>" size="50" maxlength="50" readonly="1">
		<?PHP 
		} 
		?>
	</td>
</tr>
</table>
<p align="center">
<?PHP if (($td == "add") OR ($td == "edit")) { ?>
<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?PHP echo $but_reinit; ?>">
<input name="submit" type="submit" class="bton_std" id="submit" value="<?PHP echo $but_enr; ?>">
<?PHP } ?>
<?PHP if ($td == "del") { ?>
<input name="del" type="submit" class="bton_std" id="del" value="<?PHP echo $but_del; ?>">
<?PHP }
	if ($tx=="f") {
?>
<input name="quit" type="button" class="bton_std" id="quit" value="<?PHP echo $but_quit; ?>" onclick="javascript:; self.close('fscomp');">
<?PHP
	}
?>
</p>
</form>
<?PHP
}
else
{
?>
	<script type="text/javascript">
<!--
<?PHP
	if ($tx=="r")
	{
?>
	self.close('fscomp');
<?PHP
	} else {
?>
	setTimeout("self.location.href=('index.php');",2000)
<?PHP
	}
?>
	//-->
	</script>
	<p align="center" class="annot_std_it">Probl&egrave;me de param&egrave;tres</p>
<?PHP
}
if ($tx<>"f")
	include_once("pied.php");
?>
