var _type = 'etudiants';
var _lastTypeSearched = 'etudiants';
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 changeType(type)
{
    $('line_etudiants').hide();
    $('line_apprentis').hide();

    // On met à jour le type
    _type = type;
    // On affiche les données du bon type
    $('line_'+_type).show();

}

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()
{

    // If we changed the type we reset the counter
    if(_lastTypeSearched != _type)
        {
        _currentPage = 0;
        _lastPageNumber = -1;    
    }

    var error = false;

    if(!$F('profilId').blank() && !isNumeric($F('profilId')))
        {
        alert('Erreur: Le numéro de profil doit être un nombre entier');
        error = true;
    }

    if(!error)
        {
        if(_type == 'etudiants') {
            getEtudiantsList();
        }
        else {
            getApprentisList();
        }
    }

}

function getEtudiantsList()
{
    var selPays = 1; // Defaut Suisse
    if( $('radPaysEU').checked )
    {
        selPays = $F('selPaysEu');
    }
    new Ajax.Request('getEtudiantsProfils.php?currentPage='+_currentPage+'&selPays='+selPays+'&selTypeEtudiants='+$F('selTypeEtudiants')+'&profilId='+$F('profilId'),
    {
        onSuccess : fillEtudiantsList
    } );
}

function fillEtudiantsList(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 tplEtudiants = new Template(' \
    <div class="toolbar"> \
    <span id="zoom_#{ETUId}"><img src="../../lib/pictures/zoom_in.png" border="0" alt="Ouvrir le profil" title="Ouvrir le profil" onclick="Javascript:displayInfo(\'#{ETUId}\');"/></span> \
    &nbsp; \
    <span id="mail_#{ETUId}"><a href="../../sol/contact.php?profil=e_#{ETUId}"><img src="../../lib/pictures/mail.png" border="0" alt="Demander les informations de ce profil" title="Demander les informations de ce profil" /></a></span> \
    &nbsp; \
    <span id="print_s#{ETUId}"><a href="printProfileStudent.php?profile=#{ETUId}" 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_#{ETUId}" cellpadding="2" cellspacing="0" class="short" title="Ouvrir le profil" onclick="Javascript:displayInfo(\'#{ETUId}\');"> \
    <tr> \
    <th># Profil</th> \
    <td colspan="3">#{ETUId}</td> \
    </tr> \
    <tr> \
    <th>Domaine d\'études</th> \
    <td colspan="3">#{domEtude}</td> \
    </tr> \
    <tr> \
    <th>Disponible depuis le</th> \
    <td style="width: 280px;">#{ETUAvailableFrom}</td> \
    <th style="width: 170px;">Nombre de mois désirés</th> \
    <td>#{ETUAvailableNbrMonth}</td> \
    </tr> \
    <tr> \
    <th>Travail désiré</th> \
    <td colspan="3">#{ETUPreferredWork}</td> \
    </tr> \
    <tr> \
    <th valign="top">Pays préférés</th> \
    <td colspan="3">#{prefCountries}</td> \
    </tr> \
    </table> \
    <table id="table_long_#{ETUId}" cellpadding="2" cellspacing="0" class="long" style="display: none;" onclick="Javascript:displayInfo(\'#{ETUId}\');"> \
    <tr> \
    <th>Genre</th> \
    <td style="width: 280px;">#{ETUSex}</td> \
    <th style="width: 170px;">Date de naissance</th> \
    <td>#{ETUBirthday}</td> \
    </tr> \
    <tr> \
    <th>Nationalité</th> \
    <td style="width: 280px;">#{nationality}</td> \
    <th style="width: 170px;">Pays</th> \
    <td>#{country}</td> \
    </tr> \
    <tr> \
    <th>Université / Ecole</th> \
    <td>#{ETUSchoolUniversity}</td> \
    <th># d\'années d\'études</th> \
    <td>#{ETUStudyYears}</td> \
    </tr> \
    <tr> \
    <th>Date d\'obtention du diplôme</th> \
    <td colspan="3">#{ETUGraduationDate}</td> \
    </tr> \
    <tr> \
    <th>Langue maternelle</th> \
    <td colspan="3">#{ETUMotherTongue}</td> \
    </tr> \
    <tr> \
    <th>Langue niveau faible</th> \
    <td colspan="3">#{ETULangBasic}</td> \
    </tr> \
    <tr> \
    <th>Langue niveau moyen</th> \
    <td colspan="3">#{ETULangFair}</td> \
    </tr> \
    <tr> \
    <th>Langue niveau bon</th> \
    <td colspan="3">#{ETULangGood}</td> \
    </tr> \
    <tr> \
    <th>Langue niveau excellent</th> \
    <td colspan="3">#{ETULangVeryGood}</td> \
    </tr> \
    <tr> \
    <th valign="top">Expérience professionnelle</th> \
    <td colspan="3">#{ETUProfExperience}</td> \
    </tr> \
    <tr> \
    <th valign="top">Autres activités</th> \
    <td colspan="3">#{ETUOtherActivities}</td> \
    </tr> \
    <tr> \
    <th valign="top">Compétences techniques</th> \
    <td colspan="3">#{ETUTechnicalSkills}</td> \
    </tr> \
    <tr> \
    <th valign="top">Compétences sociales</th> \
    <td colspan="3">#{ETUSocialSkills}</td> \
    </tr> \
    <tr> \
    <th valign="top">Remarques</th> \
    <td colspan="3">#{ETURemarks}</td> \
    </tr> \
    </table> \
    <div class="spacer"></div> \
    ');

    var HTMLOutput = '';

    for(var i = 0; i < cards.length; i++)
        {
        HTMLOutput += tplEtudiants.evaluate({
            'ETUId'               : cards.item(i).getAttribute('ETUId'),
            'domEtude'               : cards.item(i).getAttribute('domEtude'),
            'ETUAvailableFrom'               : cards.item(i).getAttribute('ETUAvailableFrom'),
            'ETUAvailableNbrMonth'               : cards.item(i).getAttribute('ETUAvailableNbrMonth'),
            'ETUPreferredWork'               : cards.item(i).getAttribute('ETUPreferredWork'),
            'prefCountries'         : cards.item(i).getAttribute('prefCountries'),
            'ETUSex'               : cards.item(i).getAttribute('ETUSex'),
            'nationality'               : cards.item(i).getAttribute('nationality'),
            'country'               : cards.item(i).getAttribute('country'),
            'ETUBirthday'               : cards.item(i).getAttribute('ETUBirthday'),
            'ETUSchoolUniversity'               : cards.item(i).getAttribute('ETUSchoolUniversity'),
            'ETUGraduationDate'               : cards.item(i).getAttribute('ETUGraduationDate'),
            'ETUStudyYears'               : cards.item(i).getAttribute('ETUStudyYears'),
            'ETUMotherTongue'               : cards.item(i).getAttribute('ETUMotherTongue'),
            'ETULangBasic'               : cards.item(i).getAttribute('ETULangBasic'),
            'ETULangFair'               : cards.item(i).getAttribute('ETULangFair'),
            'ETULangGood'               : cards.item(i).getAttribute('ETULangGood'),
            'ETULangVeryGood'               : cards.item(i).getAttribute('ETULangVeryGood'),
            'ETUProfExperience'               : cards.item(i).getAttribute('ETUProfExperience'),
            'ETUOtherActivities'               : cards.item(i).getAttribute('ETUOtherActivities'),
            'ETUTechnicalSkills'               : cards.item(i).getAttribute('ETUTechnicalSkills'),
            'ETUSocialSkills'               : cards.item(i).getAttribute('ETUSocialSkills'),
            'ETURemarks'               : cards.item(i).getAttribute('ETURemarks')
        });
    }

    $('resultats-content').innerHTML = HTMLOutput;
}

function getApprentisList()
{
    
    var selPays = 1; // Defaut Suisse
    if( $('radPaysEU').checked )
    {
        selPays = $F('selPaysEu');
    }
    new Ajax.Request('getApprentisProfils.php?currentPage='+_currentPage+'&selPays='+selPays+'&selTypeApprentis='+$F('selTypeApprentis')+'&profilId='+$F('profilId'),
    {
        onSuccess : fillApprentisList
    } );
}

function fillApprentisList(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 tplApprentis = new Template(' \
    <div class="toolbar"> \
    <span id="zoom_#{APPId}"><img src="../../lib/pictures/zoom_in.png" border="0" alt="Ouvrir le profil" title="Ouvrir le profil" onclick="Javascript:displayInfo(\'#{APPId}\');"/></span> \
    &nbsp; \
    <span id="mail_#{APPId}"><a href="../../sol/contact.php?profil=a_#{APPId}"><img src="../../lib/pictures/mail.png" border="0" alt="Demander les informations de ce profil" title="Demander les informations de ce profil" /></a></span> \
    &nbsp; \
    <span id="print_a#{APPId}"><a href="printProfileApprentice.php?profile=#{APPId}" 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_#{APPId}" cellpadding="2" cellspacing="0" class="short" title="Ouvrir le profil" onclick="Javascript:displayInfo(\'#{APPId}\');"> \
    <tr> \
    <th># Profil</th> \
    <td colspan="3">#{APPId}</td> \
    </tr> \
    <tr> \
    <th>Domaine d\'études</th> \
    <td colspan="3">#{domEtude}</td> \
    </tr> \
    <tr> \
    <th>Disponible depuis le</th> \
    <td style="width: 280px;">#{APPAvailableFrom}</td> \
    <th style="width: 170px;">Nombre de mois désirés</th> \
    <td>#{APPAvailableNbrMonth}</td> \
    </tr> \
    <tr> \
    <th>Travail désiré</th> \
    <td colspan="3">#{APPPreferredWork}</td> \
    </tr> \
    <tr> \
    <th valign="top">Pays préférés</th> \
    <td colspan="3">#{prefCountries}</td> \
    </tr> \
    </table> \
    <table id="table_long_#{APPId}" cellpadding="2" cellspacing="0" class="long" style="display: none;" onclick="Javascript:displayInfo(\'#{APPId}\');"> \
    <tr> \
    <th>Genre</th> \
    <td style="width: 280px;">#{APPSex}</td> \
    <th style="width: 170px;">Date de naissance</th> \
    <td>#{APPBirthday}</td> \
    </tr> \
    <tr> \
    <th>Nationalité</th> \
    <td style="width: 280px;">#{nationality}</td> \
    <th style="width: 170px;">Pays</th> \
    <td>#{country}</td> \
    </tr> \
    <tr> \
    <th>Date d\'obtention du diplôme</th> \
    <td colspan="3">#{APPGraduationDate}</td> \
    </tr> \
    <tr> \
    <th>Langue maternelle</th> \
    <td colspan="3">#{APPMotherTongue}</td> \
    </tr> \
    <tr> \
    <th>Langue niveau faible</th> \
    <td colspan="3">#{APPLangBasic}</td> \
    </tr> \
    <tr> \
    <th>Langue niveau moyen</th> \
    <td colspan="3">#{APPLangFair}</td> \
    </tr> \
    <tr> \
    <th>Langue niveau bon</th> \
    <td colspan="3">#{APPLangGood}</td> \
    </tr> \
    <tr> \
    <th>Langue niveau excellent</th> \
    <td colspan="3">#{APPLangVeryGood}</td> \
    </tr> \
    <tr> \
    <th valign="top">Autres activités</th> \
    <td colspan="3">#{APPOtherActivities}</td> \
    </tr> \
    <tr> \
    <th valign="top">Compétences techniques</th> \
    <td colspan="3">#{APPTechnicalSkills}</td> \
    </tr> \
    <tr> \
    <th valign="top">Compétences sociales</th> \
    <td colspan="3">#{APPSocialSkills}</td> \
    </tr> \
    <tr> \
    <th valign="top">Remarques</th> \
    <td colspan="3">#{APPRemarks}</td> \
    </tr> \
    </table> \
    <div class="spacer"></div> \
    ');

    var HTMLOutput = '';

    for(var i = 0; i < cards.length; i++)
        {
        HTMLOutput += tplApprentis.evaluate({
            'APPId'               : cards.item(i).getAttribute('APPId'),
            'domEtude'               : cards.item(i).getAttribute('domEtude'),
            'APPAvailableFrom'               : cards.item(i).getAttribute('APPAvailableFrom'),
            'APPAvailableNbrMonth'               : cards.item(i).getAttribute('APPAvailableNbrMonth'),
            'APPPreferredWork'               : cards.item(i).getAttribute('APPPreferredWork'),
            'prefCountries'         : cards.item(i).getAttribute('prefCountries'),
            'APPSex'               : cards.item(i).getAttribute('APPSex'),
            'nationality'               : cards.item(i).getAttribute('nationality'),
            'country'               : cards.item(i).getAttribute('country'),
            'APPGraduationDate'               : cards.item(i).getAttribute('APPGraduationDate'),
            'APPBirthday'               : cards.item(i).getAttribute('APPBirthday'),
            'APPStudyYears'               : cards.item(i).getAttribute('APPStudyYears'),
            'APPMotherTongue'               : cards.item(i).getAttribute('APPMotherTongue'),
            'APPLangBasic'               : cards.item(i).getAttribute('APPLangBasic'),
            'APPLangFair'               : cards.item(i).getAttribute('APPLangFair'),
            'APPLangGood'               : cards.item(i).getAttribute('APPLangGood'),
            'APPLangVeryGood'               : cards.item(i).getAttribute('APPLangVeryGood'),
            'APPOtherActivities'               : cards.item(i).getAttribute('APPOtherActivities'),
            'APPTechnicalSkills'               : cards.item(i).getAttribute('APPTechnicalSkills'),
            'APPSocialSkills'               : cards.item(i).getAttribute('APPSocialSkills'),
            'APPRemarks'               : cards.item(i).getAttribute('APPRemarks')
        });
    }

    $('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;

}