// -------------------------------------------------------------------------
//  Recherche en temps réel
//  PREFIXE : lrch0
// -------------------------------------------------------------------------

var lrch0_gtclsLiveSearchPool = new Array();
var lrch0_gintTimeOutID = null;
var lrch0_gintLastID = 0;
var lrch0_gintLinkPrefixes = 0;

// -------------------------------------------------------------------------
//  Constructeur classe
// -------------------------------------------------------------------------
//  [IN] pstrLiveSearchID Identifiant recherche
//  [IN] pstrSearchType   Type de recherche
//  [IN] pstrSoftID       Identifiant logiciel
//  [IN] pobjField        Champ de saisie de la recherche
//  [IN] pstrLinkPrefixes Préfixe identifiant des liens pour la validation au
//                        clavier
//  [IN] pblnFormSubmit   Validation automatique du formulaire après sélection O/N
// -------------------------------------------------------------------------

function lrch0_clsLiveSearch(pstrLiveSearchID, pstrSearchType, pstrSoftID, pobjField, pstrLinkPrefixes, pstrFormSubmit)
{
 // Proprietes
 this.strLiveSearchID = pstrLiveSearchID;
 this.objField = pobjField;
 this.strLinkPrefixes = pstrLinkPrefixes;
 this.objXmlHttpRequest = null;
 this.strSearchLast = '';
 this.objContainer = lrch0_CreateContainer();
 this.intCurrentLineID = -1;
 this.intLineNumber = -1;

 this.strPostData  = 'pstrLiveSearchID=' + shr_rqst0_Escape(pstrLiveSearchID);
 this.strPostData += '&pstrSearchType=' + shr_rqst0_Escape(pstrSearchType);
 this.strPostData += '&pstrSoftID=' + shr_rqst0_Escape(pstrSoftID);
 this.strPostData += '&pstrLinkPrefixes=' + shr_rqst0_Escape(pstrLinkPrefixes);
 this.strPostData += '&pstrFormSubmit=' + shr_rqst0_Escape(pstrFormSubmit);
 this.strPostData += '&pstrSearch=';

 // Evenements
 pobjField.onblur = function() { lrch0_LiveSearchHideDelayed(pstrLiveSearchID); };
}


// -------------------------------------------------------------------------
//  Gestion des fleches
// -------------------------------------------------------------------------
//  [IN] pintKeyCode : Code de la touche
// -------------------------------------------------------------------------

lrch0_clsLiveSearch.prototype.UpDown = function (pintKeyCode)
{
 var lobjRow;
 var lintCurrentLineID = this.intCurrentLineID;

 // Determine le nombre de ligne maxima
 if (this.intLineNumber == -1)
   {
    this.intLineNumber = 0;

    while (shr_tool0_GetElementByID(this.strLinkPrefixes + this.intLineNumber) != null)
         { this.intLineNumber++; }
   }

 // Flêche bas
 if (pintKeyCode == 40)
   {
    lintCurrentLineID++;

    if (lintCurrentLineID >= this.intLineNumber)
      { lintCurrentLineID = 0; }
   }

 // Flêche haut
 if (pintKeyCode == 38)
   {
    lintCurrentLineID--;

    if (lintCurrentLineID < 0)
      { lintCurrentLineID = this.intLineNumber - 1; }
   }

 // Effacer ancienne position barre
 this.HideBar();

 // Nouvelle position barre
 lobjLink = shr_tool0_GetElementByID(this.strLinkPrefixes + lintCurrentLineID)
 lobjRow = lrch0_GetRowObject(lobjLink);
 shr_pger0_SetBgColor(lobjRow);
 this.intCurrentLineID = lintCurrentLineID;
}


// -------------------------------------------------------------------------
//  Masque le container d'affichage du résultat de la recherche
// -------------------------------------------------------------------------

lrch0_clsLiveSearch.prototype.Hide = function ()
{
 this.objContainer.style.visibility = 'hidden';
 this.objContainer.style.display = 'none';
 this.intCurrentLineID = -1;
 this.intLineNumber = -1;

 // Afficher les combobox
 shr_tool0_ShowCombo(true, 0, 0, 0, 0);
}


// -------------------------------------------------------------------------
//  Efface la barre correspondant à la ligne courante
// -------------------------------------------------------------------------

lrch0_clsLiveSearch.prototype.HideBar = function ()
{
 // Ligne courante existe ?
 if (this.intCurrentLineID != -1)
   {
    // Effacer la barre correspondant à cette ligne
    lobjLink = shr_tool0_GetElementByID(this.strLinkPrefixes + this.intCurrentLineID)
    lobjRow = lrch0_GetRowObject(lobjLink);
    shr_pger0_ResBgColor(lobjRow);
   }
}


// -------------------------------------------------------------------------
//  Recherche de la ligne container du lien
// -------------------------------------------------------------------------
//  [IN] pobjLink Lien
//  [RETURN] pobjRow Container du lien ou null si non trouvé
// -------------------------------------------------------------------------

function lrch0_GetRowObject(pobjLink)
{
 var lobjRow = pobjLink;

 if (pobjLink == null)
   { return(null); }

 // Recherche container ligne
 while (  (lobjRow.nodeName != 'TR')
        &&(lobjRow.parentNode != null) )
      { lobjRow = lobjRow.parentNode; }

 if (lobjRow.nodeName == 'TR')
   { return(lobjRow); }

 return(null);
}


// -------------------------------------------------------------------------
//  Evenement survol d'une ligne du résultat de la recherche par la souris
// -------------------------------------------------------------------------
//  [IN] pstrLiveSearchID Identifiant recherche
//  [IN] pintLineID Identifiant numéro de ligne
// -------------------------------------------------------------------------

function lrch0_OnMouseOver(pstrLiveSearchID, pintLineID)
{
 var lclsLiveSearch = lrch0_gtclsLiveSearchPool[pstrLiveSearchID];

 if (!lclsLiveSearch)
   { return; }

 // Effacer ancienne position barre
 lclsLiveSearch.HideBar();

 // Nouveau numéro de ligne courante
 lclsLiveSearch.intCurrentLineID = pintLineID;
}


// -------------------------------------------------------------------------
//  Sélection d'une ligne dans le résultat de la recherche
// -------------------------------------------------------------------------
//  Mise a jour de la zone de saisie de recherche et validation du
//  formulaire
// -------------------------------------------------------------------------
//  [IN] pstrLiveSearchID Identifiant recherche
//  [IN] pstrValueSelect  Valeur sélectionnée dans le résultat
//  [IN] pstrFormSubmit   Validation automatique du formulaire O/N
// -------------------------------------------------------------------------

function lrch0_SearchSelect(pstrLiveSearchID, pstrValueSelect, pstrFormSubmit)
{
 var lclsLiveSearch = lrch0_gtclsLiveSearchPool[pstrLiveSearchID];

 if (!lclsLiveSearch)
   { return; }

 var lobjField = lclsLiveSearch.objField;
 var lobjForm  = lobjField.form;

 // Mise a jour zone de saisie de la recherche
 lobjField.value = pstrValueSelect;

 // Validation du formulaire
 if (  (pstrFormSubmit == 'N')
     ||(lobjForm.onsubmit() == false) )
   { return; }

 lobjForm.submit();
 return;
}


// -------------------------------------------------------------------------
//  Masque le container d'affichage du résultat de la recherche
//  Execution différée
// -------------------------------------------------------------------------
//  [IN] pstrLiveSearchID Identifiant recherche
// -------------------------------------------------------------------------

function lrch0_LiveSearchHideDelayed(pstrLiveSearchID)
{ window.setTimeout('lrch0_LiveSearchHide(\'' + pstrLiveSearchID + '\')',400); }


// -------------------------------------------------------------------------
//  Masque le container d'affichage du résultat de la recherche
// -------------------------------------------------------------------------
//  [IN] pstrLiveSearchID Identifiant recherche
// -------------------------------------------------------------------------

function lrch0_LiveSearchHide(pstrLiveSearchID)
{
 var lclsLiveSearch = lrch0_gtclsLiveSearchPool[pstrLiveSearchID];

 if (lclsLiveSearch) { lclsLiveSearch.Hide(); }
}


// -------------------------------------------------------------------------
//  Creation du container d'affichage du résultat de la recherche
// -------------------------------------------------------------------------
//  [RETURN] Container
// -------------------------------------------------------------------------

function lrch0_CreateContainer()
{
 var lobjContainer;
 var lstrLiveSearchID = 'LRCH0_' + lrch0_gintLastID;
 lrch0_gintLastID++;

 if (typeof document.body.insertAdjacentHTML != 'undefined')
   {
    document.body.insertAdjacentHTML('afterBegin', '<div id="' + lstrLiveSearchID + '"></div>');
    lobjContainer = shr_tool0_GetElementByID(lstrLiveSearchID);
   }
 else
   {
    lobjContainer = document.createElement(lstrLiveSearchID);
    lobjContainer.id = lstrLiveSearchID;
    document.body.appendChild(lobjContainer);
   }

 // Style
 lobjContainer.style.display = 'none';
 lobjContainer.style.visibility = 'hidden';
 lobjContainer.style.backgroundColor = '#ffffff';
 lobjContainer.style.borderStyle = 'solid';
 lobjContainer.style.borderWidth = '1px';
 lobjContainer.style.borderColor = '#000000';
 lobjContainer.style.top = '0px';
 lobjContainer.style.left = '0px';
 lobjContainer.style.position = 'absolute';

 return(lobjContainer);
}


// -------------------------------------------------------------------------
//  Activer la recherche
// -------------------------------------------------------------------------
//  [IN] pobjEvent        Evenement
//  [IN] pstrSearchType   Type de recherche
//  [IN] pstrSoftID       Identifiant logiciel
//  [IN] pobjField        Champ de saisie de la recherche
//  [IN] pblnFormSubmit   Validation automatique du formulaire après sélection O/N
//  [RETURN] True si la touche doit être prise en compte, sinon false
// -------------------------------------------------------------------------

function lrch0_LiveSearchStart(pobjEvent, pstrSearchType, pstrSoftID, pobjField, pstrFormSubmit)
{
 var lclsLiveSearch;
 var lstrLiveSearchID = pobjField.form.name + '.' + pobjField.name;
 var lintKeyCode = window.event ? window.event.keyCode : pobjEvent.which;
 var lstrLinkPrefixes;

 // Presence d'une classe de recherche associee au champ ?
 if (lrch0_gtclsLiveSearchPool[lstrLiveSearchID] == null)
   {
    // Nouvelle classe de recherche
    lstrLinkPrefixes = 'LRCH0_UPDOWN_' + lrch0_gintLinkPrefixes + '_';
    lrch0_gintLinkPrefixes = lrch0_gintLinkPrefixes + 1;
    lclsLiveSearch = new lrch0_clsLiveSearch(lstrLiveSearchID, pstrSearchType, pstrSoftID, pobjField, lstrLinkPrefixes, pstrFormSubmit);
    lrch0_gtclsLiveSearchPool[lstrLiveSearchID] = lclsLiveSearch;
   }
 else
   { lclsLiveSearch = lrch0_gtclsLiveSearchPool[lstrLiveSearchID]; }

 // Gestion des touches
 switch (lintKeyCode)
       {
        // Tabulation (tab)
        case 9:
            // Masque fenetre si visible
            if (lclsLiveSearch.objContainer.style.visibility == 'visible')
              {
               lclsLiveSearch.Hide();
               lclsLiveSearch.strSearchLast = '';
              }

            return(true);

        // Shift
        case 16:
            return(true);

        // Abandon (esc)
        case 27:
            // Masque fenetre si visible
            if (lclsLiveSearch.objContainer.style.visibility == 'visible')
              {
               lclsLiveSearch.Hide();
               lclsLiveSearch.strSearchLast = '';
               return(false);
              }

            return(true);

        // Touche enter
        case 13:
            // Lien correspondant
            if (  (lclsLiveSearch.intCurrentLineID != -1)
                &&((lobjLink = shr_tool0_GetElementByID(lclsLiveSearch.strLinkPrefixes + lclsLiveSearch.intCurrentLineID)) != null) )
              {
               lobjLink.onclick();

               if (lclsLiveSearch.objContainer.style.visibility == 'visible')
                 {
                  lclsLiveSearch.Hide();
                  lclsLiveSearch.strSearchLast = '';
                 }

               return(false);
              }

            return(true);

        // Fleche en bas et fleche en haut
        case 40:
        case 38:
            lclsLiveSearch.UpDown(lintKeyCode);
            return(false);
       }

 // Recherche effective apres 200 millisecondes
 if (lrch0_gintTimeOutID)
   {
    window.clearTimeout(lrch0_gintTimeOutID);
    lrch0_gintTimeOutID = null;
   }

 lrch0_gintTimeOutID = window.setTimeout('lrch0_LiveSearchDoSearch(\'' + lstrLiveSearchID + '\')',200);
 return(true);
}


// -------------------------------------------------------------------------
//  Début effectif de la recherche
// -------------------------------------------------------------------------
//  [IN] pstrLiveSearchID Identifiant recherche
// -------------------------------------------------------------------------

function lrch0_LiveSearchDoSearch(pstrLiveSearchID)
{
 var lclsLiveSearch = lrch0_gtclsLiveSearchPool[pstrLiveSearchID];

 if (!lclsLiveSearch)
   { return; }

 // Nouvelle recherche
 if (lclsLiveSearch.strSearchLast != lclsLiveSearch.objField.value)
   {
    // Arret requete precedente
    if (  (lclsLiveSearch.objXmlHttpRequest != null)
        &&(lclsLiveSearch.objXmlHttpRequest.readyState < 4) )
      { lclsLiveSearch.objXmlHttpRequest.abort(); }

    // Pas de valeur de recherche
	if (lclsLiveSearch.objField.value == '')
	  {
	   // Masque affichage resultat recherche
       lclsLiveSearch.Hide();
       lclsLiveSearch.strSearchLast = '';
	   return;
	  }

	// Appel procedure serveur de traitement de la recherche
	lclsLiveSearch.objXmlHttpRequest = shr_rqst0_GetXmlHttpRequest();
	lclsLiveSearch.objXmlHttpRequest.onreadystatechange = function() { lrch0_CallBackLiveSearchDoSearch(pstrLiveSearchID); };
	lclsLiveSearch.objXmlHttpRequest.open('POST', lrch0_gstrURL);
    lclsLiveSearch.objXmlHttpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	lclsLiveSearch.strSearchLast = lclsLiveSearch.objField.value;
	lclsLiveSearch.objXmlHttpRequest.send(lclsLiveSearch.strPostData + shr_rqst0_Escape(lclsLiveSearch.objField.value));
   }
}


// -------------------------------------------------------------------------
//  Début effectif de la recherche (callback)
// -------------------------------------------------------------------------
//  [IN] pstrLiveSearchID Identifiant recherche
// -------------------------------------------------------------------------

function lrch0_CallBackLiveSearchDoSearch(pstrLiveSearchID)
{
 var lclsLiveSearch = lrch0_gtclsLiveSearchPool[pstrLiveSearchID];

 if (!lclsLiveSearch)
   { return; }

 // Recherche effectuee ?
 if (  (lclsLiveSearch.objXmlHttpRequest.readyState == 4)
     &&(lclsLiveSearch.objXmlHttpRequest.status == 200) )
   {
    // Resultat de la recherche
    var lstrLiveSearchResult = unescape(lclsLiveSearch.objXmlHttpRequest.responseText);
    lclsLiveSearch.objContainer.innerHTML = lstrLiveSearchResult;

    if (lstrLiveSearchResult != '')
      {
       var lobjFieldPos = shr_tools_GetElementPos(lclsLiveSearch.objField);

       // Positionner le container d'affichage recherche
       lobjFieldPos.y += lclsLiveSearch.objField.clientHeight + 5;
       lclsLiveSearch.objContainer.style.left = lobjFieldPos.x + 'px';
       lclsLiveSearch.objContainer.style.top = lobjFieldPos.y + 'px';

       // Affichage container
       lclsLiveSearch.objContainer.style.display = 'block';
       lclsLiveSearch.objContainer.style.visibility = 'visible';

       // Curseur sur premiere ligne
       lclsLiveSearch.intCurrentLineID = -1;
       lclsLiveSearch.intLineNumber = -1;

       // Masquer les combobox
       shr_tool0_ShowCombo(true, lobjFieldPos.y, lobjFieldPos.x, lclsLiveSearch.objContainer.clientHeight,  lclsLiveSearch.objContainer.clientWidth);
       shr_tool0_ShowCombo(false, lobjFieldPos.y, lobjFieldPos.x, lclsLiveSearch.objContainer.clientHeight,  lclsLiveSearch.objContainer.clientWidth);
      }
    else
      { lclsLiveSearch.Hide(); }
   }
}


// -------------------------------------------------------------------------
//  Recherche client avec affichage des résultat sans rechargement de la
//  page
// -------------------------------------------------------------------------
//  [IN] pobjEvent  Evenement
//  [IN] pobjField  Champs
//  [RETURN] True si la touche doit être prise en compte, sinon false
// -------------------------------------------------------------------------

function lrch0_LiveSearchCustomerStart(pobjEvent, pobjField)
{ return(lrch0_LiveSearchStart(pobjEvent, 'CustomerSearch', '', pobjField, 'N')); }


// -------------------------------------------------------------------------
//  Recherche prospect avec affichage des résultat sans rechargement de la
//  page
// -------------------------------------------------------------------------
//  [IN] pobjEvent  Evenement
//  [IN] pobjField  Champs
//  [RETURN] True si la touche doit être prise en compte, sinon false
// -------------------------------------------------------------------------

function lrch0_LiveSearchProspectStart(pobjEvent, pobjField)
{ return(lrch0_LiveSearchStart(pobjEvent, 'ProspectSearch', '', pobjField, 'N')); }


// -------------------------------------------------------------------------
//  Recherche article avec affichage des résultat sans rechargement de la
//  page
// -------------------------------------------------------------------------
//  [IN] pobjEvent Evenement
//  [IN] pobjField Champs
//  [RETURN] True si la touche doit être prise en compte, sinon false
// -------------------------------------------------------------------------

function lrch0_LiveSearchProductStart(pobjEvent, pobjField)
{ return(lrch0_LiveSearchStart(pobjEvent, 'ProductSearch', '', pobjField, 'N')); }


// -------------------------------------------------------------------------
//  Recherche article client avec affichage des résultat sans rechargement
//  de la page
// -------------------------------------------------------------------------
//  [IN] pobjEvent Evenement
//  [IN] pobjField Champs
//  [RETURN] True si la touche doit être prise en compte, sinon false
// -------------------------------------------------------------------------

function lrch0_LiveSearchProductCustomerStart(pobjEvent, pobjField)
{ return(lrch0_LiveSearchStart(pobjEvent, 'ProductCustomerSearch', '', pobjField, 'N')); }


// -------------------------------------------------------------------------
//  Recherche affaire avec affichage des résultat sans rechargement de la
//  page
// -------------------------------------------------------------------------
//  [IN] pobjEvent  Evenement
//  [IN] pobjField  Champs
//  [RETURN] True si la touche doit être prise en compte, sinon false
// -------------------------------------------------------------------------

function lrch0_LiveSearchDealStart(pobjEvent, pobjField)
{ return(lrch0_LiveSearchStart(pobjEvent, 'DealSearch', '', pobjField, 'N')); }

