﻿/***********************************************
* Javascript associé aux controles wuc_aide_saisie
***********************************************/
// nommer le timer pour pouvoir m'arrêter :
// peut être partagé par plusieurs controles car il est peu probable qu'il soit appellé par 2 controles en même temps
var timert
var timert2
// cette fct evite d'exécuter plusieurs fois une fct (qd ce n'est justement pas nécessaire). La dernière fera foi.
function attendAvant(fct,temps) {
    clearTimeout(timert);
    timert = setTimeout(fct, temps);
}
function attendAvant2(fct, temps) {
    clearTimeout(timert2);
    timert2 = setTimeout(fct, temps);
}
//Positionnement du Div
function posAideSaisie(wucId,txtId){
    var wuc = document.getElementById(wucId);
    var tb = document.getElementById(txtId);

    var letop = tb.clientHeight;
    var leleft = 0;
    if (tb.offsetParent) {
        do {
            leleft += tb.offsetLeft;
            letop += tb.offsetTop;
        } while (tb = tb.offsetParent);
    }

    wuc.style.top = letop + 1 + "px";
    wuc.style.left = leleft + "px";
    wuc.style.position = "absolute";
}

function posAideSaisie2(wucId, txtId) {
    var wuc = document.getElementById(wucId);
    var txt = document.getElementById(txtId);
    var of = getOffset(txt, false, true)
    wuc.style.top = of[1] + 1 + txt.clientHeight + "px";
    wuc.style.left = of[0] + "px";
    wuc.style.position = "absolute";
}
//Positionnement de l'image par rapport à un control texte
function posElt(wucId, txtId, difftop, diffleft) {
    var wuc = document.getElementById(wucId);
    var tb = document.getElementById(txtId);
    var letop = difftop;
    var leleft = diffleft;
    if (tb.offsetParent) {
        do {
            leleft += tb.offsetLeft;
            letop += tb.offsetTop;
        } while (tb = tb.offsetParent);
    }
    wuc.style.top = letop + "px";
    wuc.style.left = leleft + "px";
    wuc.style.position = "absolute";
}

function posElt2(wucId, txtId, difftop, diffleft) {
    var wuc = document.getElementById(wucId);
    var txt = document.getElementById(txtId);
    var of = getOffset(txt, false, true)

    wuc.style.top = of[1] + difftop + "px";
    wuc.style.left = of[0] + diffleft + "px";
    wuc.style.position = "absolute";
}
//Actualise le UpdatePanel via le bouton
function actuUp(btnId) { document.getElementById(btnId).click(); }
//Actualise le UpdatePanel via le bouton ssi le text est vide
function actuUpifVide(txtId, btnId) {
    if (document.getElementById(txtId).value == '') {
        attendAvant2("actuUp('" + btnId + "')", 90); 
    } 
}
//Actualise le contenu du textbox
function actuTxt(txtId, valeur) {document.getElementById(txtId).value = valeur; }
function changeElt(txtId, lbId) {
    var s = document.getElementById(lbId);
    if (s.options.selectedIndex != -1) { actuTxt(txtId, s.options[s.options.selectedIndex].text); }
}
//Actions si touche appuyée sur textbox
function actuKey(e, btnId, txtId, lbId, btnId2) {
    var uc=e.keyCode? e.keyCode : e.charCode;
    var s = document.getElementById(lbId);
    
        if (uc==40){
            if (s.options.length !=0) {
                if (s.options.selectedIndex ==-1) {
                    actuTxt(txtId, s.options[0].text);
                    s.options[0].selected = true;
                    }
                else {
                    if (s.options.selectedIndex != s.options.length -1) {
                        actuTxt(txtId,s.options[s.options.selectedIndex+1].text);
                        s.options[s.options.selectedIndex + 1].selected = true;
                    }
                }
            }
        } else if (uc==38){
            if (s.options.length !=0) {
                if ( s.options.selectedIndex > 0) {
                    actuTxt(txtId,s.options[s.options.selectedIndex-1].text);
                    s.options[s.options.selectedIndex - 1].selected = true;
                }
            }
        } else {attendAvant("actuUp('" + btnId + "')",220);}
}
// cache la div
function cachediv(divId) { document.getElementById(divId).style.display = "none";}
// montre la div
function montrediv(divId, txtId) {
    //positionnement
    var d =document.getElementById(divId);
    posAideSaisie(divId, txtId);
    d.style.display = "block";
    d.style.zIndex = 99;
}
function montrediv2(divId, txtId) {
    //positionnement
    var d = document.getElementById(divId);
    posAideSaisie2(divId, txtId);
    d.style.display = "block";
    d.style.zIndex = 99;
}
function changeClassName(id, classe) { document.getElementById(id).className = classe; }

function nextEltFocus(txtId) {
    var cnext = false;
    var ctodefocus = document.getElementById(txtId);
     with (document.forms[0]) {
        nb = elements.length;
        for (i = 0; i < nb; i++) {
            if (elements[i].name != undefined) {
                if (cnext == true) {
                    elements[i].focus(); break;
                }
                if (elements[i].name == ctodefocus.name) {
                    cnext = true;
                }
            }
        }
    }
}
