var _currentPage = 0;             // # de la page affichée
var _lastPageNumber = -1;         // Dernière page affichée
var _openElement = new Array();   // Stock les éléments ouvert

function displayInfo(elementId)
{
  if(_openElement.indexOf(elementId) == -1) // Element close we open it
  {
    _openElement.push(elementId);
    Effect.Appear('table_long_'+elementId);
    $('zoom_'+elementId).innerHTML = '<img src="../../lib/pictures/zoom_out.png" border="0" alt="Fermer le profil" title="Fermer le profil" onclick="Javascript:displayInfo(\''+elementId+'\');"/>';
    $('table_short_'+elementId).title = 'Fermer le profil';
    $('table_long_'+elementId).title = 'Fermer le profil';
  }
  else  // Element open we close it
  {
    _openElement = _openElement.without(elementId);
    Effect.Fade('table_long_'+elementId);
    $('zoom_'+elementId).innerHTML = '<img src="../../lib/pictures/zoom_in.png" border="0" alt="Ouvrir le profil" title="Ouvrir le profil" onclick="Javascript:displayInfo(\''+elementId+'\');"/>';
    $('table_short_'+elementId).title = 'Ouvrir le profil';
    $('table_long_'+elementId).title = 'Ouvrir le profil';
  }
}

function getList()
{
    var error = false;
    var selPays = 1; // Defaut Suisse
    if( $('radPaysEU').checked )
    {
        selPays = $F('selPaysEu');
    }
    
    if(!$F('ENTId').blank() && !isNumeric($F('ENTId')))
    {
        alert('Erreur: Le numéro de profil doit être un nombre entier');
        error = true;
    }
    
    if(!error)
    {
        new Ajax.Request('getEntreprisesList.php?currentPage='+_currentPage+'&selPays='+selPays+'&selType='+$F('selTypeEtudiants')+'&ENTId='+$F('ENTId'),
          {
            onSuccess : fillEntrepriseList
          } );        
    }
  
}

function fillEntrepriseList(xmlHttp)
{

  var ret = xmlHttp.responseXML;
  var xmlRoot = ret.documentElement;

  var cards = xmlRoot.getElementsByTagName('card');
  var pageNav = xmlRoot.getElementsByTagName('pageNav');

  
  var navHTML = '';
  navHTML += '<div class="buttons">';
  navHTML += '<div class="pageRows"><span style="font-weight: bold;">'+pageNav.item(0).getAttribute('totalRows')+'</span> résultats trouvés</div>';  
  if( (pageNav.length > 0) && (pageNav.item(0).getAttribute('nbrPages') > 1) )
  {

    //navHTML += '<span class="label">Page&nbsp;&nbsp;</span>';
    if(_currentPage != 0)
    {
    navHTML += '<a href="#" onclick="Javascript:goToFirstPage();">&laquo;&laquo; Première page</a> \n';
    navHTML += '<a href="#" onclick="Javascript:goToPreviousPage();">&lt;&lt;</a> \n';
    }
    for(var i = 1; i <= pageNav.item(0).getAttribute('nbrPages'); i++)
    {
      if(i == (_currentPage + 1))
      {
        navHTML += '<a href="#" onclick="Javascript:changePage(\''+i+'\');" ><span style="font-weight: bold;">'+i+'</span></a> \n';
      }
      else
      {
        navHTML += '<a href="#" onclick="Javascript:changePage(\''+i+'\');" >'+i+'</a> \n';
      }

    }
    
    if(_currentPage != _lastPageNumber)
    {
    navHTML += '<a href="#" onclick="Javascript:goToNextPage();">&gt;&gt;</a> \n';
    navHTML += '<a href="#" onclick="Javascript:goToLastPage();">Dernière page &raquo;&raquo;</a> \n';
    }

    _lastPageNumber = pageNav.item(0).getAttribute('nbrPages') -1;
  }
  navHTML += '</div><!-- End of buttons -->';
  $('navigation').innerHTML = navHTML;
  
  
  var tplEntreprise = new Template(' \
  <div class="toolbar"> \
    <span id="zoom_#{ENTId}"><img src="../../lib/pictures/zoom_in.png" border="0" alt="Ouvrir le profil" title="Ouvrir le profil" onclick="Javascript:displayInfo(\'#{ENTId}\');"/></span> \
    &nbsp; \
    <span id="mail_#{ENTId}"><a href="../../sol/contact.php?profil=ent_#{ENTId}"><img src="../../lib/pictures/mail.png" border="0" alt="Demander les informations de cette entreprise" title="Demander les informations de  cette entreprise" /></a></span> \
    &nbsp; \
    <span id="print_#{ENTId}"><a href="printProfile.php?profile=#{ENTId}" target="_blank"><img src="../../lib/pictures/print.png" border="0" alt="Imprimer le profil" title="Imprimer le profil" /></a></span> \
  </div> \
  <table id="table_short_#{ENTId}" cellpadding="2" cellspacing="0" class="short" title="Ouvrir le profil" onclick="Javascript:displayInfo(\'#{ENTId}\');"> \
      <tr> \
        <th># Profil</th> \
        <td colspan="3">#{ENTId}</td> \
      </tr> \
      <tr> \
        <th>Branche(s)</th> \
        <td colspan="3">#{fields}</td> \
      </tr> \
      <tr> \
        <th>Ville, Pays</th> \
        <td style="width: 280px;">#{ENTCity}, #{ENTCountry}</td> \
        <th style="width: 170px;">Salaire mensuel</th> \
        <td>#{ENTPlacementSalary}</td> \
      </tr> \
      <tr> \
        <th>Description du projet</th> \
        <td colspan="3">#{ENTPlacementDescription}</td> \
      </tr> \
   </table> \
   <table id="table_long_#{ENTId}" cellpadding="2" cellspacing="0" class="long" style="display: none;" onclick="Javascript:displayInfo(\'#{ENTId}\');"> \
      <tr style="background: #93C0DF; color: #fff; text-align: center; font-style: italic;"> \
        <th colspan="4">Dates</th> \
      </tr> \
      <tr> \
        <th>Au plus tôt</th> \
        <td style="width: 280px;">#{ENTDatesErliest}</td> \
        <th style="width: 170px;">Au plus tard</th> \
        <td>#{ENTDatesLatest}</td> \
      </tr> \
      <tr> \
        <th>Durée</th> \
        <td colspan="3">#{ENTDuration}</td> \
      </tr> \
      <tr style="background: #93C0DF; color: #fff; text-align: center; font-style: italic;"> \
        <th colspan="4">Informations de la société</th> \
      </tr> \
      <tr> \
        <th>Offre hébergement</th> \
        <td>#{ENTProvideAccommodation}</td> \
        <th>Paye l\'hébergement</th> \
        <td>#{ENTPaysAccommodation}</td> \
      </tr> \
      <tr> \
        <th>Repas subventionnés</th> \
        <td colspan="3">#{ENTProvideSubsidisedMeals}</td> \
      </tr> \
      <tr> \
        <th>Autre</th> \
        <td colspan="3">#{ENTSupportOther}</td> \
      </tr> \
      <tr style="background: #93C0DF; color: #fff; text-align: center; font-style: italic;"> \
        <th colspan="4">Profil souhaité</th> \
      </tr> \
      <tr> \
        <th>Type de travail</th> \
        <td colspan="3">#{ENTJobField}</td> \
      </tr> \
      <tr> \
        <th>Compétences linguistique</th> \
        <td colspan="3">#{ENTLinguisticSkills}</td> \
      </tr> \
      <tr> \
        <th>Compétences techniques</th> \
        <td colspan="3">#{ENTTechnicalSkills}</td> \
      </tr> \
      <tr> \
        <th>Compétences sociales</th> \
        <td colspan="3">#{ENTSocialCompetencies}</td> \
      </tr> \
    </table> \
    <div class="spacer"></div> \
  ');
  
  var HTMLOutput = '';
  
  for(var i = 0; i < cards.length; i++)
  {
    HTMLOutput += tplEntreprise.evaluate({
    'ENTId'                     : cards.item(i).getAttribute('ENTId'),
    'fields'                    : cards.item(i).getAttribute('fields'),
    'ENTPlacementDescription'   : cards.item(i).getAttribute('ENTPlacementDescription'),
    'ENTCity'                   : cards.item(i).getAttribute('ENTCity'),
    'ENTCountry'                : cards.item(i).getAttribute('ENTCountry'),
    'ENTPlacementSalary'        : cards.item(i).getAttribute('ENTPlacementSalary'),
    'ENTDatesErliest'           : cards.item(i).getAttribute('ENTDatesErliest'),
    'ENTDatesLatest'            : cards.item(i).getAttribute('ENTDatesLatest'),
    'ENTDuration'               : cards.item(i).getAttribute('ENTDuration'),
    'ENTProvideAccommodation'   : cards.item(i).getAttribute('ENTProvideAccommodation'),
    'ENTPaysAccommodation'      : cards.item(i).getAttribute('ENTPaysAccommodation'),
    'ENTProvideSubsidisedMeals' : cards.item(i).getAttribute('ENTProvideSubsidisedMeals'),
    'ENTSupportOther'           : cards.item(i).getAttribute('ENTSupportOther'),
    'ENTJobField'               : cards.item(i).getAttribute('ENTJobField'),
    'ENTLinguisticSkills'       : cards.item(i).getAttribute('ENTLinguisticSkills'),
    'ENTTechnicalSkills'        : cards.item(i).getAttribute('ENTTechnicalSkills'),
    'ENTSocialCompetencies'     : cards.item(i).getAttribute('ENTSocialCompetencies')
    });
  }
  
  $('resultats-content').innerHTML = HTMLOutput;
}



function changePage(numberPage)
{
  _currentPage = (numberPage-1);
  getList();
}

function goToFirstPage()
{
  _currentPage = 0;
  getList();
}

function goToLastPage()
{
  if(_lastPageNumber != -1) _currentPage = _lastPageNumber;

  getList();
}

function goToPreviousPage()
{

  if(_currentPage > 0)
  {
    _currentPage--;
    getList();
  }

}

function goToNextPage()
{
  if(_lastPageNumber != -1)
  {
    if(_currentPage < _lastPageNumber)
    {
      _currentPage++;
      getList();
    }
  }
}

// Tels us if we entered a number or not allows anything
function isNumeric(text)

{
  var validChars = "0123456789.";
  var isNumber = true;
  var aChar = '';


  for (i = 0; i < text.length && isNumber == true; i++)
  {
    aChar = text.charAt(i);
    if (validChars.indexOf(aChar) == -1)
    {
      isNumber = false;
    }
  }
  return isNumber;

}