/* 
#  $Header: /aplic/ITED/src/repository/aplic/ITED/www/htdocs/common/scripts/keyboard.js,v 1.31 2003-05-10 01:41:11-03 b918145 Beta $
#  © Copyright HSBC Bank Brasil S.A. - Banco Múltiplo - All rights reserved
#  Source      : keyboard.js
#  Description : Prove funcoes para manipulacao dos teclados virtuais
*/
var _kf=null,_kf1=null,_kf2=null,_fsize=null,_sMsg=true,_tOutK=0,_FKC=false,
_iF=false,_opK=false,_SC_REXP=/[ABC]/g,currKeyb='',_arrK=[],
_arrSC=new Array(10),_IContrast=null,_hasKbCodif=false,__KEYBOARD_HIDE_CHAR=false;
function validaKbK(e){
var k=(typeof e.which!="undefined"&& e.which!=null?e.which:e.keyCode);
if(k<20)return true;return (_SC_REXP.test(String.fromCharCode(k))?true:_SC_REXP.test(String.fromCharCode(k)));
}
function validaKeyUpKb(f){
v=String(f.value);
	if(v.length >= f.size)hideKBCKeys();
	else if(__KEYBOARD_HIDE_CHAR==true){WKeys('same');}
}
function hideKBCKeys(){WKeys('hide');}
function fSCCmp(){
	if(currKeyb=='senhacod'||currKeyb=='keyboard')clearKeyboardVisor();
	if(currKeyb!='senhacod')return;
	var d=(document.all?document.all.senhacod:document.getElementById('senhacod'));
	if(!d || !d.style || !d.style.visibility || d.style.visibility=="hidden")return;
	setFocusKBCodif();
}
function _testKFocus(){
if(typeof(_FKC)!='undefined'&&_FKC==true&&typeof(fSCCmp)=='function'){_FKC=false;_condFoco();}
}
function _testKFocusR(){
if(typeof(_iF)!='undefined'&&_iF==true&&typeof(fSCCmp)=='function'){_iF=false;fSCCmp();}
}
function showKeyboard(inp,size,inp1,inp2){
function _chgL(p){
	if(!p)return;
	if(p.size && window.__CCD)p.size=(size?size*_KB_cSize():32);
	if(p.maxLength && window.__CCD)p.maxLength=(size?size*_KB_cSize():32);
}
var isTelebanco=(typeof KEYBOARD_TYPE!="undefined" && KEYBOARD_TYPE=='telebanco');
	if(typeof ContrastControl=="function")_IContrast=new ContrastControl();
	if(!size)size=4;
	_fsize=size;
	if(typeof KEYBOARD_TYPE!="undefined" && (KEYBOARD_TYPE=='codificado'||KEYBOARD_TYPE=='codificado_cb')){
		currKeyb='senhacod';
	}else if(size==4 && !isTelebanco){
		currKeyb=getCookie('HBBR_CURR_KEYB');
		if(currKeyb==null)currKeyb='senhacod';
	}
	else if(size==3 || isTelebanco)currKeyb='senhacod';
	else currKeyb='keyboard';
	if(typeof window._KB_INPUTS!="undefined"){
		_kf=eval(window._KB_INPUTS[0]);
		if(window.__CCD)_chgL(_kf);
	}else{
		_kf=inp;_kf1=inp1;_kf2=inp2;
		if(window.__CCD){_chgL(_kf);_chgL(_kf1);_chgL(_kf2);}
	}
	_hasKbCodif=(size!=7);
	WKeys();
  if(typeof _NEW_PROGBAR=="function")_NEW_PROGBAR();
	if(!currKeyb)return;
	dragKeyb(currKeyb);
  openKb(currKeyb);
  _opK=true;
	if(size==4 && inp && typeof inp.form.tipo_keyboard!="undefined"){inp.form.tipo_keyboard[(currKeyb=='keyboard'?0:1)].checked="true";}
}
function openKb(s){
	var x=getPosX(),y=getPosY();
	if(x<0)x=5;if(y<0)y=5;
	setKbPos(s,[x,y]);visKeyb(s,'s');
  fSCCmp();
}
function getWindWH(){
if(document.all)return [parseInt(document.body.clientWidth),parseInt(document.body.clientHeight)];
else return [parseInt(window.innerWidth),parseInt(window.innerHeight)];
}
function getPosX(){
if(typeof _getXKb=="function")return _getXKb();
var dK=new Panel(currKeyb),x=getWindWH()[0]-dK.get("width")-(document.all?12:20);
return random((x<340?0:340),x);
}
function getPosY(){
if(typeof _getYKb=="function")return _getYKb();
var dK=new Panel(currKeyb),dM=new Panel((currKeyb=='keyboard'?'msgkeyb':'msgsc')),
y=dK.get("height")+dM.get("height"),s=parseInt(document.body.scrollTop);
return random(20+s,(getWindWH()[1]-y-20)+s);
}
function clearKb(){visKeyb(currKeyb,'h');_hMsgK();}
function clearInput(){p=_GETINPUT();p.value="";}
function showKeyboardVisor(){
	if(currKeyb!='senhacod' && (typeof _CLEAR_KB_VISOR!="undefined"))_CLEAR_KB_VISOR();
}
function clearKeyboardVisor(){
	if(currKeyb=='senhacod'){
		var c=document.forms.formcod.campocod;
		c.value='';
    if(_kf)_kf.value="";
	}else{
    clearInput();
  }
	showKeyboardVisor();
}
function _sMsgK(){
var m=(currKeyb=='keyboard'?'msgkeyb':'msgsc');
if(!_sMsg)return;
_posMsgK(m);
visKeyb(m,'s');
_condFoco();
_sMsg=false;
_tOutK=setTimeout("_hMsgK();",20000);
}
function Panel(id){
this.element=null;this.style=null;
this.initialize=function(){
if(document.all){
	this.element=eval("document.all."+id);
	if(this.element&&this.element.style)this.style=this.element.style;
}else if(document.getElementById){
	this.element=document.getElementById(id);
	if(this.element&&this.element.style)this.style=this.element.style;
}
}
this.get=function(prop){
if(!this.style)return null;
var s=this.style[prop];
if(/^width|height|top|left$/.test(prop))s=Number(String(s).replace(/px/,""));
return s;
}
this.set=function(prop,valor){if(/^width|height|top|left$/.test(prop))valor=String(valor)+"px";this.style[prop]=valor;}
this.move=function(x,y){this.set("left",x);this.set("top",y);}
this.initialize();
}
function _posMsgK(){
	var dMsg=new Panel((currKeyb=='keyboard'?'msgkeyb':'msgsc')),dK=new Panel(currKeyb),
top=dK.get("top"),height=dK.get("height"),
left=dK.get("left"),width=dK.get("width");
	if(_fsize==7)dMsg.move(left,top-dMsg.get("height")-4);
	else dMsg.move(left,top+height+2);
}
function _hMsgK(){
	m=(currKeyb=='keyboard'?'msgkeyb':'msgsc');
	visKeyb(m,'h');_tOutK=0;
}
function _chgMsgK(k){
var m=(k=='keyboard'?'msgkeyb':'msgsc'),n=(k=='keyboard'?'msgsc':'msgkeyb');
visKeyb(n,'h');_posMsgK(m);
if(_tOutK!=0){visKeyb(m,'s');_condFoco();}
}
function chgKb(campo){
var k=currKeyb=(campo.value=='Teclado Virtual'?'keyboard':'senhacod'),n=(k=='senhacod'?'keyboard':'senhacod');
	if(campo.checked!=true)return;
	setCookie('HBBR_CURR_KEYB',currKeyb);
	setKbPos(k,[getPosX(),getPosY()]);
	visKeyb(n,'h');visKeyb(k,'s');_chgMsgK(k);dragKeyb(k);
	_opK=true;
  fSCCmp();
	//setFocusKBCodif();
} 
function visKeyb(tp,s){
	var s=(s=='h'?'hidden':'visible');
	if(document.all)eval('document.all.'+tp+'.style.visibility="'+s+'"');
	else if(document.getElementById)document.getElementById(tp).style.visibility=s;
}
function dragKeyb(tpKeyb){
var l;
if(!tpKeyb)return;
if(document.all)eval('l=document.all.'+tpKeyb);
else if(document.getElementById)l=document.getElementById(tpKeyb);
dragable(l);
}
function setKbPos(kb,p){
var z;
if(document.all)eval('z=document.all.'+kb+'.style');
else if(document.getElementById)z=document.getElementById(kb).style;
if(document.all){z.pixelLeft=p[0];z.pixelTop=p[1];
}else{z.left=p[0];z.top=p[1];}
}

function WKeys(type){
if(!type)type="default";
if(_fsize==7)return;
var c,i,s,arr=null,
arrSC=(_fsize==4?["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Z"]:["0","1","2","3","4","5","6","7","8","9"]),
fColor=(_IContrast?_IContrast.getContrast():"bbbbbb"),
fSize=(_fsize==7?"10px":"8pt"),
fKb='font-family:Arial,Helvetica,sans-serif;text-decoration:none;color:#'+fColor+';font-size:'+fSize+';',
rex='',
arr=(_fsize==4 && !(typeof KEYBOARD_TYPE!="undefined" && KEYBOARD_TYPE=='telebanco')?["3","9","6","7","5","1","0","2","4","8"]:_KKeys);
if(!arr || arr.length==0)return;
for(var count=0;count<arr.length;count++){
	i=(_fsize==7?count:random(0,(arr.length-1)-count));
	if(_hasKbCodif){
		c=random(0,(arrSC.length-1)-count);
		if(type=="default")_arrSC[count]=arrSC[c];
		rex+=_arrSC[count];
		s2='"<a id=\\\"kbc_link'+count+'\\\" href=\\\"javascript:void(0)\\\" style=\\\"cursor:default;'+fKb+'\\\" onfocus=\\\"if(this.blur)this.blur()\\\" onclick='+'\\\"shwConv();return false\\\" nowrap><b style=\\\"color:#101010\\\">'+(_fsize==3?arr[count]:count)+'&nbsp;=</b>&nbsp;'
    	+(type=='hide'?'*':_arrSC[count])+'</a>"';
	}
  s=String(arr[i]).toUpperCase();
	if(document.all){
		str='if(document.all.keyboard&&document.all.keyboard.all.bt'+count+')document.all.keyboard.all.bt'+count+'.innerHTML='+s;
		if(_hasKbCodif)str2='document.all.senhacod.all.btS'+count+'.innerHTML='+s2;
	}else if(document.getElementById){
		str='if(document.getElementById(\'bt'+count+'\'))document.getElementById(\'bt'+count+'\')'+'.innerHTML='+s;
		if(_hasKbCodif)str2='document.getElementById(\'btS'+count+'\')'+'.innerHTML='+s2;
  }
	eval(str);
	if(_hasKbCodif)eval(str2);
  _arrK[count]=arr[i];
	if(_fsize<7){
		if(c!=(arrSC.length-1-count))arrSC[c]=arrSC[arrSC.length-1-count];
	}
	if(_fsize==4)if(i!=(arr.length-1-count))arr[i]=arr[arr.length-1-count];
}
if(_hasKbCodif)eval('_SC_REXP=/['+rex+rex.toLowerCase()+']/g');
if(type=='hide')__KEYBOARD_HIDE_CHAR=true;
else __KEYBOARD_HIDE_CHAR=false;
}
function descSC(){
var s='',val=String(document.forms.formcod.campocod.value);
	if(_kf)_kf.value="";
	for(var i=0;i<val.length;i++){
		for(c=0;c<_KKeys.length;c++){
			if(val.charAt(i).toUpperCase()==_arrSC[c]){s+=(_fsize==3?_KKeys[c]:c.toString());break;}
		}
	}
	if(_kf){
		if(window.__CCD)s=__GTC(s);
		_kf.value=s;
	}
}
function shwConv(){
	if(document.all)document.all.senhacod.all.txtsc.style.visibility="visible";
	else if(document.getElementById)document.getElementById('txtsc').style.visibility="visible";
	_condFoco();
}
function helpSC(){window.open('/ITE/app/ib/login/frm_info.html?info=teclado_codificado.html'+(window.KEYBOARD_TYPE?'&type='+window.KEYBOARD_TYPE:''),'tec_cod','left=6,top=23,width=450,height=450,directories=no,location=no,menubar=no,resizable=no,scrollbars=yes,status=no');}
function setFocusKBCodif(){
var f=document.forms['formcod'];
	if(!f || !f.campocod || currKeyb!='senhacod')return;
	if(typeof Browser!="function")return;
	var b=new Browser(),v=b.getVersion();
	if(document.getElementById || (b.isIE()&&b.getVersion()>5))
		eval("try{f.campocod.focus();}catch(e){}");
	else f.campocod.focus();
	return;
}
function _condFoco(){
	if(currKeyb!='senhacod')return;
	var d=(document.all?document.all.senhacod:document.getElementById('senhacod'));
	if(!d || !d.style || !d.style.visibility || d.style.visibility=="hidden")return;
	setFocusKBCodif();
}
function setFocusTeclado(){_focoTeclado();}
function _focoTeclado(){_iF=_FKC=true;fSCCmp();}
function isOpenK(){
	if(_opK)return true;
	alert("Você deve selecionar o tipo de teclado para digitação da senha do "+(_fsize==4?"cartão de débito.":"caixa automático"));return false;
}
function _clrCmps(){clearInput();} //compatibilidade.
function _setContrast(acao){
if(_IContrast)_IContrast.setContrast(acao);
var fColor=(_IContrast?_IContrast.getContrast():"bbbbbb"),size,divB,dK,lnk,dId;

if(currKeyb=='keyboard'){size=(typeof _KKeys!="undefined"?_KKeys.length:_arrK.length);divB='bt';}
else {size=10;divB='btS';}
for(var i=0;i<size;i++){
	if(document.all)dK=(currKeyb=="keyboard"?document.all.keyboard:document.all.senhacod).all[divB+i];
  else dK=document.getElementById(divB+i);
  if(!dK)return;
  if(currKeyb=="keyboard"){
    if(document.all)lnk=eval('document.all.bt'+i);
  	else lnk=eval('document.getElementById(\'bt'+i+'\')');
  }else{
    if(document.all)lnk=eval('document.all.kbc_link'+i);
  	else lnk=eval('document.getElementById(\'kbc_link'+i+'\')');
  }
  if(lnk&&lnk.style)lnk.style.color="#"+fColor;
}
}
function ContrastControl(){
this.nomeCookie="HSBC_CONTRAST_KEYBOARD";this.expires=365*24;
this.corAtual=-1;this.corDefault=3;
this.cores=["eeeeee","dddddd","cccccc","bbbbbb","999999","777777","000000"];
this.initialize=function(){
	var c=this.getCookieColor();
	if(!c)this.setCookieColor(this.corDefault);
}
this.getContrast=function(){return (this.corAtual==-1?null:this.cores[this.corAtual]);}
this.setContrast=function(a){
	if(a=='+'&& this.corAtual==this.cores.length-1)return;
	if(a=='-'&& this.corAtual==0)return;
	this.setCookieColor(this.corAtual+(a=='+'?1:-1));
}
this.setCookieColor=function(i){this.corAtual=i;setCookie(this.nomeCookie,this.cores[i],this.expires,"/");}
this.getCookieColor=function(){
	var c=getCookie(this.nomeCookie);
	if(!c||c=="")return null;
	for(i=0;i<this.cores.length;i++)if(this.cores[i]==c){this.corAtual=i;break;}
	return c;
}
this.initialize();
}
var PASSO_ATIVO=1;
function __ativaPassoKB(n){
	var dAtual=null,dNovo=null,atOpt=null,novoOpt=null;
	if(document.all){
		dAtual=eval('document.all.kb_passo'+PASSO_ATIVO);
		dNovo=eval('document.all.kb_passo'+n);
		atOpt=eval('document.all.kb_opt'+PASSO_ATIVO);
		novoOpt=eval('document.all.kb_opt'+n);
  }else{
		dAtual=document.getElementById('kb_passo'+PASSO_ATIVO);
		dNovo=document.getElementById('kb_passo'+n);
		atOpt=document.getElementById('kb_opt'+PASSO_ATIVO);
		novoOpt=document.getElementById('kb_opt'+n);
  }
	dAtual.style.fontWeight="normal";
	dNovo.style.fontWeight="bold";
	atOpt.checked=false;
	novoOpt.checked=true;
	PASSO_ATIVO=n;
  if(currKeyb!='senhacod'){
		var b=_GET_BAR();
		if(b)b.setCurrentInp(PASSO_ATIVO-1);
  }else{
		_kf=eval(_KB_INPUTS[PASSO_ATIVO-1]);
		var n=_KB_getPSize();	
		if(_kf.size)_kf.size=n;	
		if(_kf.maxLength)_kf.maxLength=n;
  } 
	__finalizaPassoKB(PASSO_ATIVO,'hidden');
	for(var i=n;i<=_KB_INPUTS.length;i++)__finalizaPassoKB(i,'hidden');
	WKeys();
  _focoTeclado();
}

function __finalizaPassoKB(n,s){
	if(!s)s="visible";
	var dFim=(document.all?eval('document.all.kb_imgopt'+n):document.getElementById('kb_imgopt'+n));
	dFim.style.visibility=s;
}
function __resetPassosKB(n){
	if(typeof n=="undefined")n=0;
	for(var i=n;i<_KB_INPUTS.length;i++)__finalizaPassoKB(i+1,'hidden');
	__ativaPassoKB(1);
}
function _GETINPUT(){
	var p;
	if(currKeyb=='senhacod'){p=_kf;}
	else{var b=_GET_BAR();if(b)p=b.getInput();}
	return p;
}
function __GTC(st){
if(!st)return '';
var c='',n=0,s=String(st).toLowerCase(),
a=["ub8t","$k74","pv65","!a3@","01u*","fr5#","7d%h","bi2%","y49w","$n27","!ew2","tr6h","ne*w","xo05","g6#2","xp03","kr%9","hn40","3bt5","!!ga","t*c8","o3nq","v8*4","dn81","ja!%","z9%d","al69","5$3!","s90a","$$er","p4fo","qit4","@eb3","817y","f!g4","ct23"];
	for(var i=0;i<s.length;i++){n=s.charCodeAt(i);c+=(n>=97&& n<=122?a[n-97]:(n>=48 && n<=57?a[26+n-48]:''));}
	return c;
}
function ProgressClickControl(pSize,idIniDivs,cOut,cIn,t,l,w,h,esp,p1,p2,p3){
this.currInput=0;this.inps=[];
this.addP=function(p){if(p)this.inps[this.inps.length]=p;}
if(p1)this.addP(p1);
if(p2)this.addP(p2);
if(p3)this.addP(p3);
this.size=pSize;
this.idIniDivs=idIniDivs;
this.colorIn=cIn;this.colorOut=cOut;
this.divsProgr=[];this.filled=0;
this.getInput=function(){
 	var p=this.inps[this.currInput];
	if(typeof p=="string")return eval(p);
	else return p;
}
this.add=function(k){
	if(this.filled == this.size)return;
	this.filled++;
	var d=this.getDiv(this.filled);
	if(d && d.style){
		d.style.backgroundColor=this.colorIn;
		if(/Win/i.test(navigator.userAgent)){//Windows?
			d.innerHTML='<table cellpadding=0 cellspacing=0 width="'+w+'" height="'+h+'"><tr><td align="center" style="color:'+this.colorIn+';background-color:'+this.colorIn+'"><img width=4 height=4 style="border:1px solid "+this.colorIn+"></td></tr></table>';
		}
	}
	if(k)this.putKey(k);
}
this.sub=function(){
	if(this.filled == 0)return;
	this.filled--;
	var d=this.getDiv(this.filled+1);
	if(d && d.style){
		d.style.backgroundColor=this.colorOut;
		d.innerHTML="<img width=1 height=1>";
	}
  this.rmKey();
}
this.clearVisor=function(){
var d;
	this.filled=0;
	for(var i=0;i<this.size;i++){
		d=this.getDiv(i+1);
		if(d && d.style){
			d.style.backgroundColor=this.colorOut;
			d.innerHTML="<img width=1 height=1>";
    }
	}
}
this.erase=function(){
	this.clearVisor();
  for(var i=0;i<this.inps.length;i++){p=this.inps[i];if(p)p.value="";}
  this.setCurrentInp(0);
}
this.refresh=function(){
var p=this.getInput();
	if(!p)return;
	for(var i=0;i<(p.value.length/_KB_cSize());i++)this.add();
}
this.getObj=function(n){
 	if(document.all)o=eval('document.all.'+n);
  else if(document.getElementById)o=document.getElementById(n);
  else o=null;
  return o;
}
this.getDiv=function(i){return this.getObj(idIniDivs+i);}
this.clearInput=function(){var p=this.getInput();if(p)p.value="";}
this.putKey=function(k){
var p=this.getInput();
	if(!p)return;
	if(p.value.length < (this.size * _KB_cSize()) ){
		if(window.__CCD)k=__GTC(k);
		p.value+=k;
	}
	if(p.value.length==(this.size * _KB_cSize()) ){
		if(this.inps.length>1){
			if(this.currInput<(this.inps.length-1) && (typeof window._KB_INPUTS=="undefined")){  
					this.clearVisor();
					this.currInput++;
			}
		}else if(typeof KEYBOARD_AUTOSKIP !="undefined" && KEYBOARD_AUTOSKIP){
				if(typeof KEYBOARD_CONFIRM_CODE!="undefined"){
					var ret=eval(KEYBOARD_CONFIRM_CODE);
					if(ret)clearKb();
				}
		}
	}
}
this.rmKey=function(){
var p=this.getInput(),n=(window.__CCD?_KB_cSize():1);
if(!p)return;
	if(p.value.length>0)p.value=p.value.substring(0,(p.value.length-n));
	if(p.value.length==0 && this.inps.length>1 && this.currInput>0 && (typeof window._KB_INPUTS=="undefined")){
		this.currInput--;
		this.refresh();
  }
}
this.setCurrentInp=function(i){
	this.currInput=i;
	var p=this.getInput();
	if(p && p.size)p.size=_KB_getPSize();
	if(p && p.maxLength)p.maxLength=_KB_getPSize();
}
}
function _KB_getPSize(s){
	if(!s)s=_fsize;
	return(window.__CCD?s*_KB_cSize():s);
}
function _KB_cSize(){if(window.__CCD)return 4;else return 1;} 
