<?php
// --------------------------------------------------------------------------- //
// Formulaire de recherche Sphinx
// --------------------------------------------------------------------------- //
define('DEBUG', 1);
define('LOCAL', 0);
define('NBREP', 25);

include_once('/var/www/includes/config.php');
require_once('rechercheFonc.php');

$vue = $_GET['vue'];
if (isset($_POST['formR']) == true) {
    $formR = $_POST['formR'];
} else {
    $formR = array('type' => $vue);
}
if (isset($_GET['deb']) == true) {
    $deb = $_GET['deb'];
} else {
    $deb = 0;
}

// --------------------------------------------------------------------------- //
// Affiche un champs dans le formulaire
// --------------------------------------------------------------------------- //
function afficheChamps(&$form, $nomForm, $nom, $size, $maxlength, $extra = '')
{
    print '<input type="text" name="'.$nomForm.'['.$nom.']" size="'.$size.
        '" maxlength="'.$maxlength.'" '.$extra;
    if (empty($form[$nom]) == false) {
        $valeur = str_replace('"', '&quot;', $form[$nom]);
        print ' value="'.$valeur.'"';
    }
    print ' />';
}

// --------------------------------------------------------------------------- //
// Affiche un libelle dans le formulaire
// --------------------------------------------------------------------------- //
function afficheLibelle($nom)
{
    print '<td width="208" align="right" class="StyleRechercheLib"><b>'.
        $nom.'</b></td>';
}

?>

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Extranet Scores &amp; D&eacute;cisions - Recherche</title>

<script language="javascript">
var http = null;
if (window.XMLHttpRequest) {
    // Firefox
    http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    // Internet Explorer
    http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest.");
}

// --------------------------------------------------------------------------- //
// selectCompletion
// --------------------------------------------------------------------------- //
function selectCompletion()
{
    if (http == null) {
        return;
    }
    val = document.completion_form.completion_text.value;
    if (val.length >= 3) {
        http.open("GET", "cpVilles.php?&val=" + escape(val), true);
        http.onreadystatechange = selectVilles;
        http.send(null);
    } else {
        var sel = document.completion_form.completion_select;
        sel.style.display = 'none';
    }
}

// --------------------------------------------------------------------------- //
// selectVilles
// --------------------------------------------------------------------------- //
function selectVilles()
{
    if (http.readyState == 4) {
        villes = eval('(' + http.responseText + ')');  // [id1,id2, ...]

        var sel = document.completion_form.completion_select;
        if (villes.length <= 10) {
            sel.attributes['size'].value = villes.length;
        } else {
            sel.attributes['size'].value = 10;
        }

        while (sel.options.length > 0) {
            sel.options[0] = null;
        }
        for (i = 0; i < villes.length; ++i) {
            sel.options[sel.options.length] = new Option(villes[i], villes[i]);
        }

        if (sel.options.length) {
            sel.style.display = 'block';
        } else {
            sel.style.display = 'none';
        }
    }
}

// --------------------------------------------------------------------------- //
// selectClic
// --------------------------------------------------------------------------- //
function selectClic()
{
    var sel = document.completion_form.completion_select ;
    document.completion_form.completion_text.value =
    sel.options[sel.selectedIndex].value ;
    sel.style.display = 'none';
}
</script>

</head>
<body>

<form method="post" name="completion_form">

<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top" width="580" height="200" align="center">

<?php
if ($vue == 'ent') {
?>

<input type="hidden" name="formR[type]" value="ent" />

<table>
  <tr>
    <td align="center">
      <h2>RECHERCHE ENTREPRISE V2</h2>
    </td>
  </tr>
</table>

<table>
  <tr>
    <?php afficheLibelle('SIREN'); ?>
    <td width="300" align="left">
      <?php afficheChamps($formR, 'formR', 'siret', 25, 20,
                          'style=vertical-align:middle;'); ?>
     </td>
     <td width="72">&nbsp;</td>
  </tr>
  <tr>
    <?php afficheLibelle('RAISON SOCIALE<br/>ENSEIGNE / SIGLE'); ?>
    <td width="300" align="left">
      <?php afficheChamps($formR, 'formR', 'raisonSociale', 25, 250); ?>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
</table>

<br/>

<table>
  <tr>
    <?php afficheLibelle('N&deg; &amp; VOIE'); ?>
    <td width="300" align="left">
      <?php afficheChamps($formR, 'formR', 'numVoie', 4,  4); ?>
      <?php afficheChamps($formR, 'formR', 'voie',    17, 250); ?>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
  <tr>
    <?php afficheLibelle('CP OU D&Eacute;P. / VILLE'); ?>
    <td width="300" align="left">
      <?php afficheChamps($formR, 'formR', 'cpVille', 25, 250,
                          'onkeyup="selectCompletion();"'.
                          ' id="completion_text"'); ?>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
  <tr>
    <td width="208">&nbsp;</td>
    <td width="300" align="left">
      <select id="completion_select"
        size="1"
        onclick="selectClic();"
        style="display:none;">
      </select>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
</table>

<br/>

<table>
  <tr>
    <?php afficheLibelle('T&Eacute;L / FAX'); ?>
    <td width="300" align="left">
      <?php afficheChamps($formR, 'formR', 'telFax', 15, 15); ?>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
</table>

<table>
  <tr>
    <?php afficheLibelle('NAF'); ?>
    <td width="300" align="left">
      <?php afficheChamps($formR, 'formR', 'naf', 15, 666); ?>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
</table>

<?php
} else if ($vue == 'dir') {
?>

<input type="hidden" name="formR[type]" value="dir" />

<table>
  <tr>
   <td align="center">
     <h2>RECHERCHE PAR DIRIGEANT V2</h2>
   </td>
  </tr>
</table>

<table>
  <tr>
    <?php afficheLibelle('NOM'); ?>
    <td width="300" align="left">
      <?php afficheChamps($formR, 'formR', 'nom', 25, 40,
                          'style=vertical-align:middle;'); ?>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
  <tr>
    <?php afficheLibelle('PRENOM'); ?>
    <td width="300" align="left">
      <?php afficheChamps($formR, 'formR', 'prenom', 25, 40); ?>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
</table>

<br/>

<table>
  <tr>
    <?php afficheLibelle('DATE NAISSANCE'); ?>
    <td width="300" align="left">
      <?php afficheChamps($formR, 'formR', 'dirDateNaissJJ',   2, 2); ?>/
      <?php afficheChamps($formR, 'formR', 'dirDateNaissMM',   2, 2); ?>/
      <?php afficheChamps($formR, 'formR', 'dirDateNaissAAAA', 4, 4); ?>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
  <tr>
    <?php afficheLibelle('CP OU D&Eacute;P. / VILLE DE NAISSANCE'); ?>
    <td width="300" align="left">
      <?php afficheChamps($formR, 'formR', 'cpVille', 25, 40,
                          'onkeyup="selectCompletion();"'.
                          ' id="completion_text"'); ?>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
  <tr>
    <td width="208">&nbsp;</td>
    <td width="300" align="left">
      <select id="completion_select"
        size="1"
        onclick="selectClic();"
        style="display:none;">
      </select>
    </td>
    <td width="72">&nbsp;</td>
  </tr>
</table>

<?php
}
?>

</td>
</tr>
</table>

<table width="580">
  <tr>
    <td align="center" valign="top">
      <input type="submit" value="Envoyer" />
    </td>
  </tr>
</table>

</form>

<?php
if (count($formR) > 1) {
    print '<form method="post" action="?vue='.$vue.'&deb='.($deb + NBREP).'">';
    foreach ($formR as $label => $valeur) {
        print '<input type="hidden" name="formR['.$label.']"'.
            'value="'.$valeur.'" />';
    }
    print '<input type="submit" value="Suite" />';
    print '</form>';
}

if (empty($formR['siret']) == false) {
    $liste = verificationDuSiret($formR['siret']);
    if (DEBUG && $liste != false) {
        print 'Essayez :<br/>';
        foreach ($liste as $s) {
            if (sommeDeControle($s) != 0) {
                print 'erreur somme de controle sur '.$s.
                    ' ('.sommeDeControle($s).')<br/>';
            } else {
                print $s.'<br/>';
            }
        }
        // TODO: utilisation de la liste
        exit;
    }
}
recherche($formR, $deb, NBREP);
?>

</body>
</html>