// API's para identificação de perfil jurid 
// requer inclusão do sha1.js também na mesma URL

function criaCookie(nome,valor,dias) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+dias);
  document.cookie=nome+"="+escape(valor)+((dias==null)?"":";expires="+exdate.toGMTString());	
}

function leCookie(nome) {
  if (document.cookie.length>0){
    c_start=document.cookie.indexOf(nome + "=");
    if (c_start!=-1){
      c_start=c_start + nome.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
      }
    }
  return "";
}

function apagaCookie(nome) {
	createCookie(nome,"",-1);
}

function retornaValorVetor(vetor, name){
  var i;
  var aux;
  
  for (i=0;i<vetor.length;i++){
    aux = vetor[i].split("=");
	  if (aux[0] == name) return aux[1];
  }
  return "";
}

function api_perfiljurid_autentica(login,senha,sid,urlRetornoSucesso,objMensagemErro){
  var _id = null;
  var _script = document.createElement('script');
  _script.setAttribute('type', 'text/javascript');
  _script.setAttribute('src', 'http://www.jurid.com.br/valida?info=entrar&api=1&login='+login+'&senha='+senha+'&jrdsid=' + sid + '&urlretorno='+urlRetornoSucesso+'&objmsgerro='+objMensagemErro);
  _script.setAttribute('id', '_idautenticaperfil');

  _id = document.getElementById('_idautenticaperfil');  
  if(_id) document.getElementsByTagName('head')[0].removeChild(_id);    

  // Insert <script> into DOM  
  document.getElementsByTagName('head')[0].appendChild(_script); 
}

function api_perfiljurid_autenticaCallback(data){
  var Vetor = new Array();   
  for(var key in data) {
    Vetor[Vetor.length] = key + "=" + data[key];
  }  
  if (retornaValorVetor(Vetor, "ERRO") != 0){
    if (retornaValorVetor(Vetor, "objmsgerro") != ""){
      var _o = document.getElementById(retornaValorVetor(Vetor, "objmsgerro"));
      if (_o){ 
        _o.style.display = "block";
        _o.innerText = retornaValorVetor(Vetor, "MSG");
      }
      } else {
      alert(retornaValorVetor(Vetor, "MSG"));
    }
    } else {
    if (retornaValorVetor(Vetor, "urlretorno") != ""){
      var url = retornaValorVetor(Vetor, "urlretorno");
      criaCookie("jrdsid", retornaValorVetor(Vetor, "jrdsid"));
      window.location.href = retornaValorVetor(Vetor, "urlretorno");
    }
  }          
}

function api_perfiljurid_iniciaAutenticacao(nomeObjLogin,nomeObjSenha,sid,urlRetornoSucesso,objMensagemErro){
  var _l = document.getElementById(nomeObjLogin);
  var _s = document.getElementById(nomeObjSenha);
  
  if ((!nomeObjLogin)||(!nomeObjSenha)){
    alert('Informações de autenticação não encontradas! Contate nosso suporte técnico.');
    return;
  }
  
  ((_l.value != "")&&(_s.value != "")) ? api_perfiljurid_autentica(_l.value, _s.value, sid, urlRetornoSucesso, objMensagemErro) : alert('Por favor, digite o e-mail e a senha corretamente!'); 
}

function api_perfiljurid_logoff(sid,urlRetornoSucesso){
  var _id = null;
  var _script = document.createElement('script');
  _script.setAttribute('type', 'text/javascript');
  _script.setAttribute('src', 'http://www.jurid.com.br/valida?info=sair&api=1&jrdsid=' + sid + '&urlretorno='+urlRetornoSucesso);
  _script.setAttribute('id', '_idsairperfil');

  _id = document.getElementById('_idsairperfil');  
  if(_id) document.getElementsByTagName('head')[0].removeChild(_id);    

  // Insert <script> into DOM  
  document.getElementsByTagName('head')[0].appendChild(_script); 
}

function api_perfiljurid_logoffCallback(data){
  var Vetor = new Array();   
  for(var key in data) {
    Vetor[Vetor.length] = key + "=" + data[key];
  }  
  if (retornaValorVetor(Vetor, "ERRO") != 0){
  	alert(retornaValorVetor(Vetor, "MSG"));
    } else {
    if (retornaValorVetor(Vetor, "URLRETORNO") != "")
      window.location = retornaValorVetor(Vetor, "URLRETORNO");
  }          
}
