/* Funções para Troca de Imagens */
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* Funções para o Menu */
var cssdropdown={
disappeardelay: 250, //set delay in miliseconds before menu disappears onmouseout
disablemenuclick: true, //when user clicks on a menu item with a drop down menu, disable menu item's link?
enableswipe: 1, //enable swipe effect? 1 for yes, 0 for no

dropmenuobj: null, ie: document.all, firefox: document.getElementById&&!document.all, swipetimer: undefined, bottomclip:0,

getposOffset:function(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
},

swipeeffect:function(){
if (this.bottomclip<parseInt(this.dropmenuobj.offsetHeight)){
this.bottomclip+=10+(this.bottomclip/10) //unclip drop down menu visibility gradually
this.dropmenuobj.style.clip="rect(0 auto "+this.bottomclip+"px 0)"
}
else
return
this.swipetimer=setTimeout("cssdropdown.swipeeffect()", 10)
},

showhide:function(obj, e){
if (this.ie || this.firefox)
this.dropmenuobj.style.left=this.dropmenuobj.style.top="-500px"
if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover"){
if (this.enableswipe==1){
if (typeof this.swipetimer!="undefined")
clearTimeout(this.swipetimer)
obj.clip="rect(0 auto 0 0)" //hide menu via clipping
this.bottomclip=0
this.swipeeffect()
}
obj.visibility="visible"
}
else if (e.type=="click")
obj.visibility="hidden"
},

iecompattest:function(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
},

clearbrowseredge:function(obj, whichedge){
var edgeoffset=0
if (whichedge=="rightedge"){
var windowedge=this.ie && !window.opera? this.iecompattest().scrollLeft+this.iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
this.dropmenuobj.contentmeasure=this.dropmenuobj.offsetWidth
if (windowedge-this.dropmenuobj.x < this.dropmenuobj.contentmeasure)  //move menu to the left?
edgeoffset=this.dropmenuobj.contentmeasure-obj.offsetWidth
}
else{
var topedge=this.ie && !window.opera? this.iecompattest().scrollTop : window.pageYOffset
var windowedge=this.ie && !window.opera? this.iecompattest().scrollTop+this.iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
this.dropmenuobj.contentmeasure=this.dropmenuobj.offsetHeight
if (windowedge-this.dropmenuobj.y < this.dropmenuobj.contentmeasure){ //move up?
edgeoffset=this.dropmenuobj.contentmeasure+obj.offsetHeight
if ((this.dropmenuobj.y-topedge)<this.dropmenuobj.contentmeasure) //up no good either?
edgeoffset=this.dropmenuobj.y+obj.offsetHeight-topedge
}
}
return edgeoffset
},

dropit:function(obj, e, dropmenuID){
if (this.dropmenuobj!=null) //hide previous menu
this.dropmenuobj.style.visibility="hidden" //hide menu
this.clearhidemenu()
if (this.ie||this.firefox){
obj.onmouseout=function(){cssdropdown.delayhidemenu()}
//obj.onclick=function(){return !cssdropdown.disablemenuclick} //disable main menu item link onclick?
this.dropmenuobj=document.getElementById(dropmenuID)
this.dropmenuobj.onmouseover=function(){cssdropdown.clearhidemenu()}
this.dropmenuobj.onmouseout=function(){cssdropdown.dynamichide(e)}
this.dropmenuobj.onclick=function(){cssdropdown.delayhidemenu()}
this.showhide(this.dropmenuobj.style, e)
this.dropmenuobj.x=this.getposOffset(obj, "left")
this.dropmenuobj.y=this.getposOffset(obj, "top")
this.dropmenuobj.style.left=this.dropmenuobj.x-this.clearbrowseredge(obj, "rightedge")+"px"
this.dropmenuobj.style.top=this.dropmenuobj.y-this.clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+1+"px"
}
},

contains_firefox:function(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
},

dynamichide:function(e){
var evtobj=window.event? window.event : e
if (this.ie&&!this.dropmenuobj.contains(evtobj.toElement))
this.delayhidemenu()
else if (this.firefox&&e.currentTarget!= evtobj.relatedTarget&& !this.contains_firefox(evtobj.currentTarget, evtobj.relatedTarget))
this.delayhidemenu()
},

delayhidemenu:function(){
this.delayhide=setTimeout("cssdropdown.dropmenuobj.style.visibility='hidden'",this.disappeardelay) //hide menu
},

clearhidemenu:function(){
if (this.delayhide!="undefined")
clearTimeout(this.delayhide)
},

startchrome:function(){
for (var ids=0; ids<arguments.length; ids++){
var menuitems=document.getElementById(arguments[ids]).getElementsByTagName("a")
for (var i=0; i<menuitems.length; i++){
if (menuitems[i].getAttribute("rel")){
var relvalue=menuitems[i].getAttribute("rel")
menuitems[i].onmouseover=function(e){
var event=typeof e!="undefined"? e : window.event
cssdropdown.dropit(this,event,this.getAttribute("rel"))
}
}
}
}
}

}

/* Função para menu de ficha */
function MostraMenu(pThis,pMenu){
  var bot = $x(pThis);
  var nmenu = 'menu_ficha'+pMenu;
  var menu = document.getElementById(nmenu);

  menu.style.top = parseInt(findPosY(bot) + parseInt(bot.offsetHeight))+3;
  menu.style.left = parseInt(findPosX(bot)) - 46;/* parseInt(bot.offsetWidth));*/
  menu.style.zIndex = 100;
  menu.style.position = 'absolute';

  html_ToggleElement(nmenu);
}

/*Funções usada para formatar a data no evento OnKeyPress*/
function mascara_data(pthis){
        v_tam = pthis.value;
        if (v_tam.length == 2){
          pthis.value += '/';
        }
        if (v_tam.length == 5){
          pthis.value += '/';
        }
}

/* Função usada para formatar mês e ano no evento OnKeyPress */
function mascara_mes_ano(pthis){
  v_tam = pthis.value;
  if (v_tam.length == 2){ pthis.value += '/'; }
}

/*Funções usada para formatar a hora no evento OnKeyPress*/
function mascara_hora(pthis){
        v_tam = pthis.value;

    if (v_tam.length == 2){
      pthis.value += ':';
    }

    if (v_tam.length == 5){
      pthis.value += ':';
    }
}

/*Funções usada para formatar CPF no evento OnKeyPress*/
function mascara_cpf(pthis){
        v_tam = pthis.value;

        if (v_tam.length == 3){
          pthis.value += '.';
        }
        if (v_tam.length == 7){
          pthis.value += '.';
        }
        if (v_tam.length == 11){
          pthis.value += '-';
        }
        if (v_tam.length == 14){
        }
}

/*Funções usada para formatar CNPJ no evento OnKeyPress*/
function mascara_cnpj(pthis){
        v_tam = pthis.value;

        if (v_tam.length == 2){
          pthis.value += '.';
        }
        if (v_tam.length == 6){
          pthis.value += '.';
        }
        if (v_tam.length == 10){
          pthis.value += '/';
        }
        if (v_tam.length == 15){
          pthis.value += '-';
        }
}

/*Funções usada para formatar CEP no evento OnKeyPress*/
function mascara_cep(pthis){
  v_tam = pthis.value;

  if (v_tam.length == 5){
    pthis.value += '-';
  }
}

function mascara_cartao(pthis){
  v_tam = pthis.value;

  if (v_tam.length == 4){
    pthis.value += ' ';
  }
  if (v_tam.length == 9){
    pthis.value += ' ';
  }
  if (v_tam.length == 14){
    pthis.value += ' ';
  }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
 janela=window.open(theURL,winName,features);
}

/* Função para abrir uma url dentro de um popup de tamanho passado como parâmetro */
function popup (v_url,v_width,v_height, v_scroll) {
  w = open(v_url,"winLov","Scrollbars=1,resizable=1,menubar=1,width="+v_width+",height="+v_height);
  if (w.opener == null)
  w.opener = self;
  w.focus();
}

/* Função genérica para mensagem de confirmação */
function confirmar_msg(msg, req){
  if(req == null){
    req = 'Acao'
  }

  var conf = msg;

  if(conf == null){
    conf = confirm("Confirma a ação?");
  }else{
    conf = confirm(msg);}

  if (conf == true){
    doSubmit(req);
  }else{
    doSubmit('CANCELADO');
  }
}

/* Mensagens para o usuário */
htmldb_delete_message='Gostaria de executar esta ação de deleção?';
apex_nova_proposta='Deseja lançar nova proposta?';
bloqueia_cartao='Deseja realmente bloquear o cartão?';
cancela_cartao='Deseja realmente cancelar o cartão?';
desbloqueia_cartao='Deseja realmente desbloquear o cartão?';
gera_2via_cartao='Confirma a geração de 2ª via?';
msg_usu_demitir='Esta operação será realizada para todos os produtos do Usuário que estão relacionados com a Empresa! Deseja continuar?';
msg_usu_demitir_confirmar='O usuário será demitido, todas as movimentações que possuem lançamentos futuros serão antecipadas, esta operação não poderá ser desfeita! Deseja continuar?';
msg_usu_readmitir_confirmar='O usuário será readmitido pelo sistema. Deseja continuar?';
msg_confirmar_operacao='Deseja realmente confirmar a operação?';


/* Função para o Help dos Itens */
function ajax_help(pThis,pId){
  this.dTimeout;
  clearTimeout(this.dTimeout);
  this.dGet = dGet;
  this.dShow = dShow;
  this.dCancel = dCancel;
  var get = new htmldb_Get(null,$x('pFlowId').value,'APPLICATION_PROCESS=AJAX_ITEM_HELP',0);
  this.dGet();
  return;

  function dGet(){
    this.dTimeout = setTimeout("this.dCancel()",60000);
    get.add('ITEM_HELP_NAME',pId);
    get.add('ITEM_HELP_PAGE',$x('pFlowStepId').value);
    get.GetAsync(dShow);
  }
  	
  function dShow(){
    $x_Hide('rollover');
    if(p.readyState == 1){
    }else if(p.readyState == 2){
    }else if(p.readyState == 3){
    }else if(p.readyState == 4){
      $x('rollover_content').innerHTML = p.responseText;
      $x_Show('rollover');
      htmldb_IE_Select_Item_Fix($x('rollover'));
      $x_Style('rollover','left',findPosX(pThis)+pThis.offsetWidth+5);
      $x_Style('rollover','top',findPosY(pThis));
      document.onclick = function(e){
        dCheckClick(e);
      }
    }else{return false;}
  }
  
  function dCheckClick(e){
    var elem = html_GetTarget(e);
    try{
      var lTable = $x_UpTill(elem,"DIV");
      if(lTable.id!='rollover_content'){dCancel();}
      else{}
    }catch(err){dCancel();}
  }
  	
  function dCancel(){
    $x_Hide('rollover');
    document.onclick = null;
    get = null;
  }
}

/* Função de validar mascara generica #61762 */
/* Efetuada correção #69801 */
function validar_mascara(pthis,pitem){
  for (var i = 0; i < 8; i++)
  {
    if (document.getElementById(pitem+'_'+i).checked)
    {
      mascara(pthis,pitem+'_'+i);
    }
  }
}

/* Função usada para formatar CEI no evento OnKeyPress */
function mascara_cei(pthis){
  v_tam = pthis.value;
  if (v_tam.length ==  2){ pthis.value += '.'; }
  if (v_tam.length ==  6){ pthis.value += '.'; }
  if (v_tam.length == 12){ pthis.value += '/'; }
}

/* Função de validar mascara generica #61762 */
function mascara(pthis,pitem){
  if (document.getElementById(pitem).value == 'F')
  {
    pthis.maxLength=14;
    mascara_cpf(pthis);
  }
  else if (document.getElementById(pitem).value == 'J')
  {
    pthis.maxLength=18;
    mascara_cnpj(pthis);
  }
  else if (document.getElementById(pitem).value == 'E')
  {
    pthis.maxLength=15;
    mascara_cei(pthis);
  }
}


bp_aguarde='Aguarde...';

/* Função que retorna o nome abreviado, usada no evento onblur */
function abrevia_nome(pVal,pRet){
/*   var get = new htmldb_Get(null,html_GetElement('pFlowId').value,'APPLICATION_PROCESS=ABREVIAR_NOME',0);
  get.add('APP_NOME_ABREVIADO',pVal.value);
  gReturn = get.get(); 
  var str = gReturn;
 
  html_GetElement(pRet).value = str; 
*/
  doSubmit(pVal);
  html_GetElement(pRet).focus();
  return;
/*  str=null;
  pRet=null;
*/
}

/* Função que exibe a mensagem de aguardar e logo após exibe o nome abreviado */
function msg_aguarde_abreviar(pThis,pMsg,pTexto,pTempo,pIcone,pRet){
  var vIcone;
  var p;
  p = new XMLHttpRequest();
  if  (html_GetElement(pRet).value == '') 
  {
     if (pIcone == 'P') vIcone = Ext.MessageBox.QUESTION;
     if (pIcone == 'E') vIcone = Ext.MessageBox.ERROR;
     if (pIcone == 'I') vIcone = Ext.MessageBox.INFO;
     if (pIcone == 'A') vIcone = Ext.MessageBox.WARNING;
     if (pIcone == 'D') vIcone = 'ext-mb-download';
   
     Ext.MessageBox.show({
       msg: pMsg,
       progressText: pTexto,
       width:300,
       wait:true,
       waitConfig: {interval:200},
       icon: vIcone,
       animEl: pThis
     });
     /*  doSubmit(pThis);*/
     p=p;
     if(p.readyState == 0){
       setTimeout(function(){
         Ext.MessageBox.hide(abrevia_nome(pThis,pRet));
       }, pTempo);
     }
  }
  else
  {
    return;
  }
};

function ini_cons_cep(pend,pbairro,pcidade,puf){
  $x(pend).disabled="true";
  $x(pbairro).disabled="true";
  $x(pcidade).disabled="true";
  $x(puf).disabled="true";
}

function delay(pmillis) 
{
  var complete = new Date();   
  complete.setTime(complete.getTime() + pmillis);   
  while (new Date().getTime() < complete.getTime());   
} 

function consulta_cep(pThis,pcep,pend,pbairro,pcidade,puf,pend1,pbairro1,pcidade1,puf1,pdisable){
 if ($x(pcep).value == '') return;
  var v_codresp  = -1;
  var v_retorno = new Array(6);
  this.dTimeout;
  clearTimeout(this.dTimeout);
  this.dGet = dGet;
  this.dShow = dShow;
  this.dCancel = dCancel;
  var get = new htmldb_Get(null,$x('pFlowId').value,'APPLICATION_PROCESS=AJAX_CONSULTA_CEP_WS',0);
  this.dGet();
  return;

  function dGet(){
    this.dTimeout = setTimeout("this.dCancel()",60000);
    get.add('ITEM_CEP_VALOR',$x(pcep).value);
    $x_Show(pcep+'_PROG');
    get.GetAsync(dShow);
    if ($x(pdisable ).value == '') ini_cons_cep(pcep,pend,pbairro,pcidade,puf);
  }
  	
  function dShow(){
   try{
    if(p.readyState == 1){
    }else if(p.readyState == 2){
    }else if(p.readyState == 3){
    }else if(p.readyState == 4){    
     v_retorno = p.responseText.split('@');
     v_codresp = parseInt(v_retorno[4]);
     if (parseInt(v_codresp) == 0){
       $x_Hide(pcep+'_PROG');
       var v_aux = v_retorno[0];
       if (v_aux == ''){
         $x_disableItem(pend, false);
         html_GetElement(pend).focus();
       }else{
         $x(pend).value = v_aux;
         $x(pend).disabled="true";   
       }
       v_aux =  v_retorno[1];
       if (v_aux == ''){
         $x_disableItem(pbairro, false);
       }else{
         $x(pbairro).value = v_aux;
         $x(pbairro).disabled="true";
       }
       $x(pcidade).value = v_retorno[2];
       if ($x(pcidade).value == ''){
         $x_disableItem(pcidade, false);
       }else $x(pcidade).disabled="true";
       $x(puf).value     = v_retorno[3];       
       if ($x(puf).value == ''){
         $x_disableItem(puf, false);
        }else $x(puf).disabled="true";
       if ($x(pend).value == '') $x(pend).value = $x(pend1).value; else $x(pend1).value = $x(pend).value;
       if ($x(pbairro).value == '') $x(pbairro).value = $x(pbairro1).value; else $x(pbairro1).value = $x(pbairro).value;
       $x(pcidade1).value = $x(pcidade).value;
       $x(puf1).value     = $x(puf).value;
    }else{ 
       $x_Hide(pcep+'_PROG');
       $x_disableItem(pend, false); 
       html_GetElement(pend).focus();
       $x_disableItem(pbairro, false);
       $x_disableItem(pcidade, false);
       $x_disableItem(puf, false);
       $x('rollover_content').innerHTML = v_retorno[5];
       $x_Show('rollover');
       htmldb_IE_Select_Item_Fix($x('rollover'));
       $x_Style('rollover','left',findPosX(pThis)+pThis.offsetWidth+5);
       $x_Style('rollover','top',findPosY(pThis));
       document.onclick = function(e,v_codresp){
         dCheckClick(e);}
     }
    }else{
      $x_Hide(pcep+'_PROG');
      return false;
    }
    }catch(e){
      $x_Hide(pcep+'_PROG');
      return false;
    }
  }
  function dCheckClick(e){
    var elem = html_GetTarget(e);
    try{
      var lTable = $x_UpTill(elem,"DIV");
      if(lTable.id!='rollover_content'){dCancel();}
      else{}
    }catch(err){dCancel();}
  }
  function dCancel(){
    html_GetElement(pend).focus();
    $x_Hide('rollover');
    document.onclick = null;
    get = null;
  }
}

