/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm) {
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1) {
				current = elements[i];
				if (!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1) {
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1) {
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1) {
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1) {
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

function select_user(idUser) {
    f=function() {       
        getParent(document.getElementById('infoUser'+idUser)).setAttribute("style", "inline-table");
        getParent(document.getElementById('User'+idUser)).setAttribute("class", "lineSelected");
        document.getElementById('infoUser'+idUser).innerHTML = xhreq.responseText;
        afficherAppartenances(idUser);
    }
    req_ajax('../../ajax/a_infoUser.php', '?idUser='+idUser, f);
    cacherDivInfos();
}

function select_stag(idStag) {
    f=function() {       
        document.getElementById('infoStagiaire'+idStag).parentNode.parentNode.setAttribute("style", "inline-table");
        document.getElementById('Stagiaire'+idStag).parentNode.parentNode.setAttribute("class", "lineSelected");
        document.getElementById('infoStagiaire'+idStag).innerHTML = xhreq.responseText;
        afficherAppartenancesStagiaire(idStag);
    }
    req_ajax('../../ajax/a_infoStag.php', '?idStag='+idStag, f);
    cacherDivStags();
}

function select_ca(idCa) {
    f=function() {       
        document.getElementById('infoCa'+idCa).parentNode.parentNode.setAttribute("style", "inline-table");
        document.getElementById('Ca'+idCa).parentNode.parentNode.setAttribute("class", "lineSelected");
        document.getElementById('infoCa'+idCa).innerHTML = xhreq.responseText;
        afficherAppartenancesCa(idCa);
    }
    req_ajax('../../ajax/a_infoCa.php', '?idCa='+idCa, f);
    cacherDivCas();
}

    /**
    * Sélectionne le CV d'un utilisateur en fonction de son Id et l'affiche dans la DIV InfoCv
    */
function select_cv(idUser) {
    f=function() {       
        //document.getElementById('infoCv'+idUser).parentNode.parentNode.setAttribute("style", "");
        document.getElementById('infoCv').innerHTML = xhreq.responseText;
        //afficherAppartenances(idUser);
    }
    req_ajax('../../ajax/a_infoCv.php', '?idUser='+idUser, f);
    cacherDivCvs();
    cacherDivboxlistcv();
}

function afficherAppartenances(idUser) {
    f=function() {
        document.getElementById('appartenancesUser'+idUser).innerHTML = xhreq.responseText;
    }
    req_ajax('../../ajax/a_appartenance_user.php', '?idUser='+idUser, f);
}

function afficherAppartenancesStagiaire(idStag) {
    f=function() {
        document.getElementById('appartenancesStag'+idStag).innerHTML = xhreq.responseText;
    }
    req_ajax('../../ajax/a_appartenance_stag.php', '?idStag='+idStag, f);
}

function afficherAppartenancesCa(idCa) {
    f=function() {
        document.getElementById('appartenancesCa'+idCa).innerHTML = xhreq.responseText;
    }
    req_ajax('../../ajax/a_appartenance_ca.php', '?idCa='+idCa, f);
}

function new_user() {
    f=function() {
        cacherDivInfos();
        document.getElementById('boxlistuser').innerHTML = '';
        document.getElementById('infoUserNew').innerHTML = xhreq.responseText;
    }
    req_ajax('../../ajax/a_infoUser.php', '?nouvUser=1', f);
}

function new_stag() {
    f=function() {
        cacherDivStags();
        document.getElementById('boxliststag').innerHTML = '';
        document.getElementById('infoStag').innerHTML = xhreq.responseText;
    }
    req_ajax('../../ajax/a_infoStag.php', '?nouvStag=1', f);
}

function new_ca() {
    f=function() {
        cacherDivCas();
        document.getElementById('boxlistca').innerHTML = '';
        document.getElementById('infoCa').innerHTML = xhreq.responseText;
    }
    req_ajax('../../ajax/a_infoCa.php', '?nouvCa=1', f);
}

function new_cv() {
    f=function() {
        cacherDivCvs();
        document.getElementById('boxlistcv').innerHTML = '';
        document.getElementById('infoCv').innerHTML = xhreq.responseText;
    }
    req_ajax('../../ajax/a_infoCv.php', '?nouvCv=1', f);
}

function afficherUserParLettre(i) {
    f = function() {
        //document.getElementById('imgChargement').setAttribute("style", "display:none");
        document.getElementById('boxlistuser').innerHTML = xhreq.responseText;
    }
    cacherDivInfos();
    req_ajax('../../ajax/a_liste_user.php', '?alpha='+i, f);
    document.getElementById('boxlistuser').innerHTML = '';
    //document.getElementById('imgChargement').setAttribute("style", "");
}

function afficherUsersParNomPrenom(sFiltre){
    f = function() {
        //document.getElementById('imgChargement').setAttribute("style", "display:none");
        
        document.getElementById('resultAutoCompUserResp').innerHTML = xhreq.responseText;
    }
    req_ajax('../../ajax/a_liste_user.php', '?filtre='+sFiltre+'&id=UserResp', f);
}

function afficherStagiaireParLettre(i) {

    f = function() {
        //document.getElementById('imgChargement').setAttribute("style", "display:none");
        document.getElementById('boxliststag').innerHTML = xhreq.responseText;
    }
    cacherDivStags();
    req_ajax('../../ajax/a_liste_stag.php', '?alpha='+i, f);
    document.getElementById('boxliststag').innerHTML = '';
    //document.getElementById('imgChargement').setAttribute("style", "");
}

function afficherCaParLettre(i) {
    greta=document.getElementById("choixgreta").value;
    annee=document.getElementById("choixannee").value;
    f = function() {
        //document.getElementById('imgChargement').setAttribute("style", "display:none");
        document.getElementById('boxlistca').innerHTML = xhreq.responseText;
    }
    cacherDivCas();
    req_ajax('../../ajax/a_liste_ca.php', '?alpha='+i+'&greta='+greta+'&annee='+annee, f);
    document.getElementById('boxlistca').innerHTML = '';
    //document.getElementById('imgChargement').setAttribute("style", "");
}

function afficherCvParLettre(i) {
    f = function() {
        //document.getElementById('imgChargement').setAttribute("style", "display:none");
        document.getElementById('boxlistcv').innerHTML = xhreq.responseText;
    }
    cacherDivCvs();
    req_ajax('../../ajax/a_liste_cv.php', '?alpha='+i, f);
    document.getElementById('boxlistcv').innerHTML = '';
    //document.getElementById('imgChargement').setAttribute("style", "");
}

function afficherUserParGroupe(idGroupe) {
    f = function() {
        //document.getElementById('imgChargement').setAttribute("style", "display:none");
        document.getElementById('boxlistuser').innerHTML = xhreq.responseText;
    }
    cacherDivInfos();
    req_ajax('../../ajax/a_user_liste.php', '?idGroupe='+idGroupe, f);
    document.getElementById('boxlistuser').innerHTML = '';
    //document.getElementById('imgChargement').setAttribute("style", "");
}


//                      Formations
//-----------------------------------------------------------------


function afficherInscrForm(id, fonc){
    f = function(){
        document.getElementById('divInscr').innerHTML = xhreq.responseText;
        // alert(xhreq.responseText);
        egaliserFieldsets('fsInscr', 'fsGroupes');
        if(fonc){
            fonc();
        }
    }
    req_ajax('../../ajax/a_liste_user.php', '?idFormation='+id, f);
}


function inscrireUserForm(idUser, idForm){
    f=function(){
        afficherInscrForm(idForm);
    }
    req_ajax('../../ajax/a_liste_user.php', '?idUserInscr='+idUser+'&idFormInscr='+idForm+'&inscr=1',f);
}

function desinscrireUserForm(idUser, idForm){
    f=function(){
        afficherInscrForm(idForm);
    }
    req_ajax('../../ajax/a_liste_user.php', '?idUserInscr='+idUser+'&idFormInscr='+idForm+'&inscr=0',f);
}

function inscrireGroupeForm(idGroupe, idForm){
    f=function(){
        // alert(xhreq.responseText);
        afficherInscrForm(idForm);
    }
    req_ajax('../../ajax/a_liste_user.php', '?idGroupeInscr='+idGroupe+'&idFormInscr='+idForm+'&inscr=1',f);
}

function desinscrireGroupeForm(idGroupe, idForm){
    f=function(){
        afficherInscrForm(idForm);
    }
    req_ajax('../../ajax/a_liste_user.php', '?idGroupeInscr='+idGroupe+'&idFormInscr='+idForm+'&inscr=0',f);
}

//       fin formation région

function cacherDivInfos() {
    var lstDivInfo = document.getElementsByName('infoUser');
    // alert(lstDivInfo.length);
    for(i=0;i<lstDivInfo.length;i++) {
        getParent(lstDivInfo[i]).setAttribute("style", "display:none");
        lstDivInfo[i].innerHTML='';
    }
    sel = document.getElementsByClassName("lineSelected");
    for(i=0;i<sel.length;i++) {
        sel[i].setAttribute("class", "");
    }
    document.getElementById('infoUserNew').innerHTML='';
}

function cacherDivStags() {
    var lstDivStag = document.getElementsByName('infoStag');
    for(i=0;i<lstDivStag.length;i++) {
        lstDivStag[i].parentNode.parentNode.setAttribute("style", "display:none");
    }
    sel = document.getElementsByClassName("lineSelected");
    for(i=0;i<sel.length;i++) {
        sel[i].setAttribute("class", "");
    }
    document.getElementById('infoStag').innerHTML='';
}

function cacherDivCas() {
    var lstDivCa = document.getElementsByName('infoCa');
    for(i=0;i<lstDivCa.length;i++) {
        lstDivCa[i].parentNode.parentNode.setAttribute("style", "display:none");
    }
    sel = document.getElementsByClassName("lineSelected");
    for(i=0;i<sel.length;i++) {
        sel[i].setAttribute("class", "");
    }
    document.getElementById('infoCa').innerHTML='';
}

function cacherDivCvs() {
    var lstDivCv = document.getElementsByName('infoCv');
    for(i=0;i<lstDivCv.length;i++) {
        lstDivCv[i].parentNode.parentNode.setAttribute("style", "display:none");
    }
    document.getElementById('infoCv').innerHTML='';
}

function cacherDivboxlistcv() {
    document.getElementById('boxlistcv').innerHTML='';
}

function suppressionUser() {
    if (confirm("Êtes-vous certain de vouloir supprimer cet utilisateur ?")) {
        document.getElementById("action").value="delete";
    }
}

function suppressionStag() {
    if (confirm("Êtes-vous certain de vouloir supprimer ce stagiaire ?")) {
        document.getElementById("action").value="delete";
    }
}

function suppressionCa() {
    if (confirm("Êtes-vous certain de vouloir supprimer ce contrat aidé ?")) {
        document.getElementById("action").value="delete";
    }
}

function suppressionCv() {
    if (confirm("Êtes-vous certain de vouloir supprimer ce CV ?")) {
        document.getElementById("action").value="delete";
    }
}

