// JavaScript Document
function parseString(func_name,acao){
  var strForm="";
  var strField="";
  var strValue=""; 
  for(i=0;i<document.form1.length;i++)
  {    				     
	strField+=document.form1[i].name+"|";				     
    strValue+=document.form1[i].value+"|";  
  }
  strForm=strField + '~~|~~' +strValue;
  
  if (acao=='inserir'){
    document.getElementById("msg").innerHTML='Cadastrando dados...'; 
    x_Inserir(strForm,func_name);	
  }else{
    document.getElementById("msg").innerHTML='Alterando dados...';
    x_Alterar(strForm,func_name);
  }   
}

//Sem essa função não há como separar o joio do trigo
function filtraDados(val){ 
  //busca a posição de ocorrência da string
  var srch=val.indexOf('+:');
  //pega somente o que vem a partir da posição do +: com 2 somado 
  if(srch==-1)
	  var str = val;
  else
	  var str = val.substring(srch+2);
  //Divide a string - a cada ~~|~~ representa uma nova linha de dados
  var sArr=str.split('~~|~~'); 
  
  return sArr;
}
function preencheCombo(val) 
{
  sArr=val.split("~~|~~");
  
  combo = document.getElementById(sArr[0]); 
  
  
  combo.length = 1;
  
  
  combo.options[0].text = "Aguarde...";
  combo.selectedIndex = 0;	
  
  for(i=1;i<(sArr.length-1);i++){
    if (sArr[i] != '') {	  
	  var arr=sArr[i].split("|_|");	  
      var i = combo.length;
	  combo.length = i + 1;
	  combo.options[i].value = arr[0];
	  combo.options[i].text = arr[1];
    }
  }		

  combo.options[0].text = ("[Selecionar]");
  combo.selectedIndex = 0;
}

//Antes de alterar chama função que recebe o id do registro o número da linha e a função que será chamada após
function antes_Alterar(id,line,func_name){
  x_obterDados(id+','+line,func_name);
}

//Atualiza grid após inserir nova linha	  
function apos_Inserir(val){
  escreveTela('Cadastro efetuado com sucesso!','mostra');
  document.form1.reset();
  setTimeout("escreveTela('','apaga')",3000);  
  apagaLinhas('1');
  //x_obterDados('x|_|3|_|login','linha',montaTabela);
}

//Atualiza grid após alterar linha
function apos_Alterar(val){  
  escreveTela('Cadastro alterado com sucesso!','mostra'); 
  setTimeout("escreveTela('','apaga')",3000);   
  x_obterDados(document.getElementById('id').value,'linha',alteraLinha);
}

function antes_Excluir(linha,indice){
  if (confirm('Tem certeza que deseja apagar esse usuário?')){
    x_Excluir(linha,apos_Excluir)
    document.getElementById('grid').deleteRow(indice); 
	
  }   
}
//Após excluir registro na tabela do banco apaga ele do grid
function apos_Excluir(){ 
  escreveTela('Registro excluído com sucesso!','mostra');
  setTimeout("escreveTela('','apaga')",3000); 
}

//Escreve na tela ação que acabou de ser efetuada
function escreveTela(msg,acao){
  var theTab = document.getElementById('theTab');  
  document.getElementById("msg").innerHTML=msg;
  if (acao=="mostra") {
      theTab.style.display="";      
  } else {
      theTab.style.display="none"; 
      
  }     
}

function browser () {
         var b = navigator.appName;
         var v = this.version = navigator.appVersion;
         var ua = navigator.userAgent.toLowerCase();
         this.v = parseInt(v);
         this.safari = ua.indexOf("safari")>-1; // always check for safari & opera
         this.opera = ua.indexOf("opera")>-1; // before ns or ie
         this.ns = !this.opera && !this.safari && (b=="Netscape");
         this.ie = !this.opera && (b=="Microsoft Internet Explorer");
       this.gecko = ua.indexOf('gecko')>-1; // check for gecko engine
       if (this.ns) {
               this.ns4 = (this.v==4);
               this.ns6 = (this.v>=5);
               this.b = "Netscape";
       }else if (this.ie) {
               this.ie4 = this.ie5 = this.ie55 = this.ie6 = false;
               if (v.indexOf('MSIE 4')>0) {this.ie4 = true; this.v = 4;}
               else if (v.indexOf('MSIE 5')>0) {this.ie5 = true; this.v = 5;}
               else if (v.indexOf('MSIE 5.5')>0) {this.ie55 = true; this.v = 5.5;}
               else if (v.indexOf('MSIE 6')>0) {this.ie6 = true; this.v = 6;}
               this.b = "MSIE";
       }else if (this.opera) {
               this.v=parseInt(ua.substr(ua.indexOf("opera")+6,1)); // set opera version
               this.opera6=(this.v>=6);
               this.opera7=(this.v>=7);
               this.b = "Opera";
       }else if (this.safari) {
               this.ns6 = (this.v>=5); // ns6 compatible correct?
               this.b = "Safari";
       }
        this.dom = (document.createElement && document.appendChild && document.getElementsByTagName)? true : false;
        this.def = (this.ie||this.dom);
        this.win32 = ua.indexOf("win")>-1;
        this.mac = ua.indexOf("mac")>-1;
        this.other = (!this.win32 && !this.mac);
        this.supported = (this.def||this.ns4||this.ns6||this.opera)? true:false;
}

function findOwner( evt )
{
    nav = new browser();

    var node;
    if (nav.gecko || nav.safari || nav.opera || nav.ns)
    {
        node = evt.target;
        while (node)
        {
            if ( node.nodeType == Node.ELEMENT_NODE &&
                 node.nodeName == "TR")
            {
                return node;
            }
            node = node.parentNode;
        }
    }
    else if(nav.ie) 
    {
        node = window.event.srcElement;
        while (node)
        {
            if (node.tagName == "TR")
            {
                return node;
            }
            node = node.parentElement;
        }
    }
    return null;
}



