
var imgTcd;
var imgTcdCaps;
var imgTcdShift;
var imgTcdCapsShift;
var vSaida = '';
var vCampoSenha;
var vIE;

var indCaps = false;
var indShift = false;
var arrTecladoAtivo = new Array();

var arrTeclado = new Array();
arrTeclado[0] = new Array('\'','1','2','3','4','5','6','7','8','9','0','-','=');
arrTeclado[1] = new Array('q','w','e','r','t','y','u','i','o','p','´','\[');
arrTeclado[2] = new Array('a','s','d','f','g','h','j','k','l','ç','~','\]');
arrTeclado[3] = new Array('','z','x','c','v','b','n','m',',','.',';','/');

var arrTecladoCaps = new Array();
arrTecladoCaps[0] = new Array('\'','1','2','3','4','5','6','7','8','9','0','-','=');
arrTecladoCaps[1] = new Array('Q','W','E','R','T','Y','U','I','O','P','´','\[');
arrTecladoCaps[2] = new Array('A','S','D','F','G','H','J','K','L','Ç','~','\]');
arrTecladoCaps[3] = new Array('','Z','X','C','V','B','N','M',',','.',';','/');

var arrTecladoShift = new Array();
arrTecladoShift[0] = new Array('','!','@','#','$','%','¨','&','*','(',')','_','+');
arrTecladoShift[1] = new Array('Q','W','E','R','T','Y','U','I','O','P','`','\{');
arrTecladoShift[2] = new Array('A','S','D','F','G','H','J','K','L','Ç','^','\}');
arrTecladoShift[3] = new Array('|','Z','X','C','V','B','N','M','\<','\>',':','?');

var arrTecladoCapsShift = new Array();
arrTecladoCapsShift[0] = new Array('','!','@','#','$','%','¨','&','*','(',')','_','+');
arrTecladoCapsShift[1] = new Array('Q','W','E','R','T','Y','U','I','O','P','`','\{');
arrTecladoCapsShift[2] = new Array('A','S','D','F','G','H','J','K','L','Ç','^','\}');
arrTecladoCapsShift[3] = new Array('|','Z','X','C','V','B','N','M','\<','\>',':','?');

function getSaida(){
    vIE = false;
    if (document.all || document.getElementById){
        vIE = true;
        document.write('<span name="countupie" id="countupie" width="80" height="7"></span>');
        if(document.getElementById && !document.all){
            vSaida = document.getElementById("countupie")
        }else{
            vSaida = document.all.countupie;
        }
    }else{
        document.write('<ilayer id="countupnsmain" width="80" height="7">')
        document.write('<layer id="countupnssub" width="80" height="7" left="0" top="0"></layer>')
        document.write('</ilayer>')
        vSaida = document.countupnsmain.document.countupnssub.document;
    }
}

function setSaida(vpSaida){
    var vStrSaisa = '';
    for(x=0;x<vpSaida.length;x++)
        vStrSaisa = vStrSaisa + '*';
    if(vIE){
        vSaida.innerHTML = vStrSaisa;
    }else{
        vSaida.open()
        vSaida.write(vStrSaisa)
        vSaida.close()
    }
}

function setCampoSenha(vpCampoSenha){
    vCampoSenha = vpCampoSenha;
    vCampoSenha.value = ''
}

function setImgsTeclado(vpImgTcd, vpImgTcdCaps, vpImgTcdShift, vpImgTcdCapsShift){
    imgTcd = vpImgTcd;
    imgTcdCaps = vpImgTcdCaps;
    imgTcdShift = vpImgTcdShift;
    imgTcdCapsShift = vpImgTcdCapsShift;
}

function montaTeclado(vpLargura, vpAltura, vpColunas, vpLinhas){
    var vAlturaLinha;
    var vLarguraColuna;
    var x1, x2, y1, y2;
    arrTecladoAtivo = arrTeclado;

    vAlturaLinha = vpAltura / vpLinhas;
    vLarguraColuna = vpLargura / vpColunas;
    document.write('<MAP NAME=\"mapTeclado\">')
    for(l=0;l<vpLinhas;l++){
        x1 = l*vAlturaLinha;
        x2 = (l+1)*vAlturaLinha;
        //alert('altura: ' + x1 + ' à ' + x2)
        for(c=0;c<vpColunas;c++){
            y1 = c*vLarguraColuna;
            y2 = (c+1)*vLarguraColuna;
            if(l<4){
                var vLetra = arrTecladoAtivo[l][c];
            }else{
                var vLetra = '';
            }
            document.write('<AREA NAME=\"' + vLetra + '\" SHAPE=\"RECT\" ')
            document.write('      COORDS=\"' + y1 + ',' + x1 + ',' + y2 + ',' + x2 + '\"')
            document.write('      HREF=\"javaScript:teclado_onClick('+ l +','+ c +')\">')
        }
    }
    document.write('</MAP>')
}

function replaceTeclado(){
    if (indCaps && indShift){
        document.imgTecladoVirtual.src = imgTcdCapsShift;
        arrTecladoAtivo = arrTecladoCapsShift;
    }else{
        if (indCaps){
            document.imgTecladoVirtual.src = imgTcdCaps;
            arrTecladoAtivo = arrTecladoCaps;
        }else{
            if (indShift){
                document.imgTecladoVirtual.src = imgTcdShift;
                arrTecladoAtivo = arrTecladoShift;
            }else{
                document.imgTecladoVirtual.src = imgTcd;
                arrTecladoAtivo = arrTeclado;
            }
        }
    }
}

function teclado_onClick(vpL, vpC){
    if(vpL == 4){
        //verifica a posição do CAPS LOCK
        if(vpC >= 0 && vpC <= 2){
            if (indCaps)
                indCaps = false
            else
                indCaps = true;
            replaceTeclado()
        }else{
            //verifica a posição do SHIFT
            if(vpC >= 3 && vpC <= 5){
                if (indShift)
                    indShift = false
                else
                    indShift = true;
                replaceTeclado()
            }else{
                //verifica a posição do APAGAR --ESPAÇO
                if(vpC >= 6 && vpC <= 12){
                    vCampoSenha.value = '';
//kcl                    setSaida(vCampoSenha.value)
                }
            }
        }
    }else{
        if ((vpL == 1 || vpL == 2 || vpL == 3) && vpC == 12){
        	document.agenda.submit();
            //vCampoSenha.value = vCampoSenha.value.substr(0, vCampoSenha.value.length - 1);
//kcl           // setSaida(vCampoSenha.value)
        }else{
            if (vCampoSenha.value.length < 21){
                vCampoSenha.value = vCampoSenha.value + arrTecladoAtivo[vpL][vpC];
//kcl                setSaida(vCampoSenha.value);
                if (indShift){
                    indShift = false;
                    replaceTeclado()
                }
            }
        }
    }
}

