<?php
header('Content-type: text/html; charset=iso-8859-1');
include ("../dyn/session.inc");
require_once "DB.php";
include_once ("../dyn/connexion.php");
$db=& DB :: connect($dsn, $db_option);
if (DB :: isError($db)) 
    die($db->getMessage());
$idstation=$_POST["idstation"];
$sql="select * from station where idstation=$idstation";
$res = $db->query($sql);
if (DB :: isError($res))
    die($res->getMessage().$sql);
echo 'var o = null;';   
echo 'var tlmo = document.forms["'.$_POST["form"].'"].elements["libmodelestation"];';
echo 'var sorg = document.forms["'.$_POST["form"].'"].elements["idorganisme"];';
echo 'var smod = document.forms["'.$_POST["form"].'"].elements["idmodele"];';
echo 'var smar = document.forms["'.$_POST["form"].'"].elements["idmarque"];';   
echo 'var styp = document.forms["'.$_POST["form"].'"].elements["idtypestation"];';   
echo 'var ssys = document.forms["'.$_POST["form"].'"].elements["idsysteme"];';   
echo 'var tdat = document.forms["'.$_POST["form"].'"].elements["dateachat"];';   
echo 'var sfou = document.forms["'.$_POST["form"].'"].elements["idfournisseur"];';   
echo 'var tbc = document.forms["'.$_POST["form"].'"].elements["bc"];';
echo 'var tgar = document.forms["'.$_POST["form"].'"].elements["garantie"];';   
while ($row=& $res->fetchRow(DB_FETCHMODE_ASSOC)){
  echo "tlmo.value = \"Modele".$row["numero"]."\";\n";
  if ($row["idorganisme"]>0)
    echo "sorg.value = ".$row["idorganisme"].";\n";
  if ($row["idmodele"]>0)
    echo "smod.value = ".$row["idmodele"].";\n";
  if ($row["idmarque"]>0)
    echo "smar.value = ".$row["idmarque"].";\n";
  if ($row["idtypestation"]>0)
    echo "styp.value = ".$row["idtypestation"].";\n";
  if ($row["idsysteme"]>0)
    echo "ssys.value = ".$row["idsysteme"].";\n";
  $valTemp=$row["dateachat"];
  if ($formatDate=="AAAA-MM-JJ"){
    $valTemp=explode("-",$valTemp);
    $valTemp=$valTemp[2]."/".$valTemp[1]."/".$valTemp[0];
  }
  echo "tdat.value = \"".$valTemp."\";\n";
  if ($row["idfournisseur"]>0)
    echo "sfou.value = ".$row["idfournisseur"].";\n";
  if ($row["bc"])
    echo 'tbc.value = "'.$row["bc"].'";';
  echo 'tgar.value = '.$row["garantie"].';';   
}
$res->free();
$sql="select modeleelement.idmodele as id, libmodele from element inner join modeleelement on element.idmodele=modeleelement.idmodele where classe='composant' and idstation=$idstation";
$res = $db->query($sql);
if (DB :: isError($res))
    die($res->getMessage().$sql);
echo 'var scom = document.forms["'.$_POST["form"].'"].elements["champcomp"];';
echo 'var hcom = document.forms["'.$_POST["form"].'"].elements["idscomp"];';
echo 'scom.options.length=0;';
echo 'hcom.value="";';
while ($row=& $res->fetchRow(DB_FETCHMODE_ASSOC)){
//  echo 'if (scom.selectedIndex>=0) {';
  echo '  scom.options[scom.options.length]=new Option("'.$row["libmodele"].'",'.$row["id"].');';
  echo '  hcom.value+="'.$row["id"].',";';
//  echo '}';
}
echo 'if (hcom.value!="")';
echo '  hcom.value=hcom.value.substring(0,hcom.value.length-1);';
$res->free();
$sql="select modeleelement.idmodele as id, libmodele from element inner join modeleelement on element.idmodele=modeleelement.idmodele where classe='logiciel' and idstation=$idstation";
$res = $db->query($sql);
if (DB :: isError($res))
    die($res->getMessage().$sql);
echo 'var slog = document.forms["'.$_POST["form"].'"].elements["champlogi"];';
echo 'var hlog = document.forms["'.$_POST["form"].'"].elements["idslogi"];';
echo 'slog.options.length=0;';
echo 'hlog.value="";';
while ($row=& $res->fetchRow(DB_FETCHMODE_ASSOC)){
//  echo 'if (scom.selectedIndex>=0) {';
  echo '  slog.options[slog.options.length]=new Option("'.$row["libmodele"].'",'.$row["id"].');';
  echo '  hlog.value+="'.$row["id"].',";';
//  echo '}';
}
echo 'if (hlog.value!="")';
echo '  hlog.value=hlog.value.substring(0,hlog.value.length-1);';
$res->free();
$sql="select modeleelement.idmodele as id, libmodele from element inner join modeleelement on element.idmodele=modeleelement.idmodele where classe='peripherique' and idstation=$idstation";
$res = $db->query($sql);
if (DB :: isError($res))
    die($res->getMessage().$sql);
echo 'var sper = document.forms["'.$_POST["form"].'"].elements["champperi"];';
echo 'var hper = document.forms["'.$_POST["form"].'"].elements["idsperi"];';
echo 'sper.options.length=0;';
echo 'hper.value="";';
while ($row=& $res->fetchRow(DB_FETCHMODE_ASSOC)){
//  echo 'if (scom.selectedIndex>=0) {';
  echo '  sper.options[sper.options.length]=new Option("'.$row["libmodele"].'",'.$row["id"].');';
  echo '  hper.value+="'.$row["id"].',";';
//  echo '}';
}
echo 'if (hper.value!="")';
echo '  hper.value=hper.value.substring(0,hper.value.length-1);';
$res->free();
$db->disconnect();
?>