﻿var AjaxNotFound = "O Servidor ajax não foi encontrado!";
var ComponentNotFound = 'Componente não encontrado!'

//** Load Inner Html ***************************************************************************************************************************
function LoadInnerHtml(strSender, Url, FormName)
{
 Sender = document.getElementById(strSender);

 if (Sender == null)
 {
  alert(ComponentNotFound);
  return;
 }
 
 if ((!FormName) || (FormName == NaN))
 {
  FormName = '';
 }

 try
 {
  var LoadInnerIndex = AddLoadHtml(strSender, Sender.style.backgroundImage,Sender.style.backgroundPosition,
                                   Sender.style.backgroundRepeat,Sender.innerHTML);
                                   
  if (LoadInnerIndex < 0)
   return false;
  
  if (Sender.style.backgroundImage.toString().indexOf("loading.gif") >= 0) 
   return false;
   
  Url = GetPageSession(Url);
  Height = parseInt(Sender.offsetHeight);
  Height = (Height < 24) ? 24 : Height;
 
  Sender.innerHTML               = "<img src='imagens/branco.gif' width='1' height='" + Height + "'>";
  Sender.style.backgroundImage    = "url(imagens/loading.gif)";
  Sender.style.backgroundPosition = "center center";
  Sender.style.backgroundRepeat   = "no-repeat";
 
  JRAjax.LoadInnerHtml(ID,SessionID,strSender,Url,LoadInnerIndex,FormName,cbk_LoadInnerHtml);
  return true;
 }
 catch(e) 
 {
  Sender.innerHTML                = innerHTML;
  Sender.style.backgroundImage    = backgroundImage;
  Sender.style.backgroundPosition = backgroundPosition;
  Sender.style.backgroundRepeat   = backgroundRepeat;
  alert(AjaxNotFound);
  return false;
 }
}

function cbk_LoadInnerHtml(Request)
{
 try
 {
  var LoadInnerIndex = parseInt("0" + Request.value[5].toString());
  var LoadInner      = GetLoadHtml(LoadInnerIndex);
  var Sender         = document.getElementById(Request.value[4]);
  
  if (Sender == null)
  {
   alert(ComponentNotFound);
   return false;
  }
  
  try
  {                                                      
   if (LoadInner == null)
   {
    alert(ContainerNotFound);
    return false;
   }
      
   Sender.style.backgroundImage    = LoadInner.BgImg;
   Sender.style.backgroundPosition = LoadInner.BgPos;
   Sender.style.backgroundRepeat   = LoadInner.BgRep;

   if (!Request.value[0])
   {                    
    if ((Request.value[1] == null) || (Request.value[1] == ""))
    {
     alert('Erro ao carregar o conteúdo!');
    }
    else 
    {
     alert(Request.value[1]);
    } 
    
    Sender.innerHTML = LoadInner.InnerHtml;
    return false;
   }
   
   Sender.innerHTML = Request.value[2];
   
   try
   {
    if ((Request.value[3] != null) && (Request.value[3].length > 0))
    {
     new Function(Request.value[3])();
    }
   }
   catch(e)
   {
   }
   
   return true;
  }
  catch(e)
  {
   alert(ComponentNotFound + "\n" + e.Message);
   Sender.innerHTML = LoadInner.InnerHtml;
  }
  return false;
 }
 finally
 {
  FreeLoadHtml(LoadInnerIndex);
  
  if (LoadInner != null)
  {
   LoadInner.InnerHtml = "";
   LoadInner.BgImg     = "";
   LoadInner.BgPos     = "";
   LoadInner.BgRep     = "";
   LoadInner.Name      = "";
  }  
 } 
}  

//** Show Forms ******************************************************************************************************************************
function ShowForm(Caption, Url, Path, Base)
{
 if (Path == null)
    Path = "imagens/";
 
 try
 { 
  Url = (Url.indexOf("?",0) < 0) ? Url+"?ID="+ID+"&SessionID=" + SessionID + "&Matricula="+Matricula : Url;
      
  if (Url.indexOf("ID=",0) < 0)
  {
      Url += "&ID="+ID;
  }
  if (Url.indexOf("SessionID=",0) < 0)
  {
      Url += "&SessionID="+SessionID;
  }
  
  try
  {
      if (Url.indexOf("Matricula=",0) < 0)
      {
          Url += "&Matricula="+Matricula;
      }
  }    
  catch (e) {}

  try
  {
      if (Url.indexOf("Path=",0) < 0)
      {
          Url += "&Path=" + Path;
      }
  }    
  catch (e) {}

  try
  {
      if (Url.indexOf("Base=",0) < 0)
      {
          Url += "&Base=" + Base;
      }
  }    
  catch (e) {}

  ShowAguarde(true);
  
  JRAjax.ShowForm(ID, SessionID, Caption, Url, FormCount, Path, cbk_ShowForm);
 }
 catch(e) 
 {
  ShowAguarde(false);
  alert(AjaxNotFound);
 }
}

function cbk_ShowForm(Request)
{
 if (!Request.value[0])
 {                    
  ShowAguarde(false);
  if ((Request.value[1] == null) || (Request.value[1] == ""))
  {
   alert('Erro ao carregar o cadastro!');
  }
  else 
  {
   alert(Request.value[1]);
  } 
  return ;
 }
 
 try
 {
  var Func = new Function(Request.value[2]);
  
  if (!Func())
  {
   return false;
  }
  
  var Form   = document.getElementById('Form'+Request.value[4]);
  var Filtro = document.getElementById('Filtro'+Request.value[4]);
  
  try
  {
   Filtro.style.position        = 'absolute';
   Filtro.style.visibility      = 'hidden';
   Filtro.style.width           = '1px';
   Filtro.style.height          = '1px';
   Filtro.style.left            = '0px';
   Filtro.style.top             = '0px';
   Filtro.style.backgroundColor = '#000000';
   Filtro.style.opacity         = '0.7';
   Filtro.style.filter          = 'alpha(opacity=70)';
   
   Form.style.position        = 'absolute';
   Form.style.left            = '0px';
   Form.style.top             = '0px';
   Form.style.width           = '1px';
   Form.style.height          = '1px';
   Form.style.cursor          = 'default';
   Form.style.visibility      = 'hidden';
   Form.border                = '0';
   Form.cellpadding           = '0';
   Form.cellspacing           = '0';

  }
  catch(e)
  {
   alert('PropertyErro: '+e.Message);
  }

  ShowDinamicForm(Request.value[4]);
  
  try
  {
   var ActiveInput = Form.getElementsByTagName('input');
   var ActiveSelect = Form.getElementsByTagName('select');
   var ActiveTextArea = Form.getElementsByTagName('textarea');
   
   ActiveInput    = (ActiveInput.length >= 0) ? ActiveInput[0] : null;
   ActiveSelect   = (ActiveSelect.length >= 0) ? ActiveSelect[0] : null;
   ActiveTextArea = (ActiveTextArea.length >= 0) ? ActiveTextArea[0] : null;
   
   
   var frm = (ActiveInput || ActiveSelect || ActiveTextArea);
   
   if (frm != null)
   {
     frm = frm.form;
     
     for (i = 0; i < frm.length; i++)
     {
      if ((frm.elements[i] == ActiveInput) || (frm.elements[i] == ActiveSelect) || (frm.elements[i] == ActiveTextArea))
      {
       frm.elements[i].focus();
       break;
      }
     }
   }

  }
  catch(e)
  {
   //faz nada
  }

  try
  {
   new Function(Request.value[3])();
  }
  catch(e)
  {
   //faz nada
  }

  BringToFront('btnAguarde');
 }
 catch(e)
 {
  alert(ComponentNotFound+"\n"+e.Message);
 }
 finally
 {
  ShowAguarde(false);
 }
}  


//** Show Ballons ******************************************************************************************************************************
function ShowBallon(Sender, Caption, Url)
{
 if (!BallonInProcess)
 try
 { 
  ShowAguarde(false);
  BallonInProcess = true;
  Url = (Url.indexOf("?",0) < 0) ? Url+"?ID="+ID+"&SessionID=" + SessionID + "&Matricula="+Matricula : Url;
      
  if (Url.indexOf("ID=",0) < 0)
  {
   Url += "&ID="+ID;
  }
  if (Url.indexOf("SessionID=",0) < 0)
  {
   Url += "&SessionID="+SessionID;
  }
  if (Url.indexOf("Matricula=",0) < 0)
  {
   Url += "&Matricula="+Matricula;
  }
 
  var P = getElementPosition(Sender);
  P.left = parseInt(P.left + (Sender.offsetWidth / 2));
  
  JRAjax.ShowBallon(ID, SessionID, Caption, Url, BallonCount, P.left, P.top,cbk_ShowBallon);
 }
 catch(e) 
 {
  BallonInProcess = false;
  alert(AjaxNotFound);
 }
}

function cbk_ShowBallon(Request)
{
 if (!Request.value[0])
 {                    
  if ((Request.value[1] == null) || (Request.value[1] == ""))
  {
   alert('Erro ao carregar o balão!');
  }
  else 
  {
   alert(Request.value[1]);
  } 
  BallonInProcess = false;
  return ;
 }
 
 try
 {
  var Func = new Function(Request.value[2]);
  
  if (!Func())
  {
   return false;
  }
  
  var Ballon   = document.getElementById(Request.value[4]);
  
  try
  {
   var Left = parseInt(Request.value[5]);
   var Top  = parseInt(Request.value[6]) - (Ballon.offsetHeight) + 2;
   
   Ballon.style.opacity         = '1'; 
   Ballon.style.position        = 'absolute';
   Ballon.style.left            = Left+'px';
   Ballon.style.top             = Top+'px';
   Ballon.style.width           = '1px';
   Ballon.style.height          = '1px';
   Ballon.style.cursor          = 'default';
   Ballon.style.visibility      = 'visible';
   Ballon.border                = '0';
   Ballon.cellpadding           = '0';
   Ballon.cellspacing           = '0';
  }
  catch(e)
  {
   alert('PropertyErro: '+e.Message);
  }

  try
  {
   var ActiveControl = Ballon.getElementsByTagName('input')[0];
   if (ActiveControl)
    ActiveControl.focus();
  }
  catch(e)
  {
   //faz nada
  }

  try
  {
   new Function(Request.value[3])();
  }
  catch(e)
  {
   //faz nada
  }

  BringToFront('btnAguarde');
 }
 catch(e)
 {
  alert(ComponentNotFound+"\n"+e.Message);
 }
 finally
 {
  BallonInProcess = false;
 }
}  

//** Efetuar Loin no Sistema ********************************************************************************************************************
function EfetuarLogin(strMatricula, strIWAdmin)
{
 Matricula   = document.getElementById(strMatricula);
 IWAdmin     = document.getElementById(strIWAdmin);
 
 if (!Matricula || !IWAdmin)
 {
  return alert(ComponentNotFound);
 }
 
 try
 {
  ShowAguarde(true);
  JRAjax.EfetuarLogin(ID, SessionID, Matricula.value, false, false, IWAdmin.checked, cbk_EfetuarLogin)
 }
 catch(e)
 {
  alert(AjaxNotFound);
  ShowAguarde(false);
 }

}

function cbk_EfetuarLogin(Request)
{
 ShowAguarde(false);
 
 if (!Request.value[0])
 {
  return alert(Request.value[1]);
 }
 
 try
 {
  new Function(Request.value[2])();
 }
 catch(e)
 {
  alert(e.Message);
 }
 
}

//** Carrega Teclado Digital *******************************************************************************************************************
function IW_LoadDigitalKeyBord(ShowError)
{
 if (ShowError == null)
  ShowError = true;

 try
 {
  JRAjax.IW_LoadDigitalKeyBord(ShowError,cbk_IW_LoadDigitalKeyBord);
 }
 catch(e)
 {
  alert(AjaxNotFound);
 } 
}


function cbk_IW_LoadDigitalKeyBord(Request)
{
 if (!Request.value[0])
 {
  alert('Falha ao carregar o teclado digital!');
  return ;
 }

 var Img = null;
 for (i = 0; i < 10; i++)
 {
  Img = document.getElementById("btn0" + i.toString());
  
  if (Img != null)
  {
   Img.src = Request.value[2][i];
  }
 }
 
 var lbSenha    = document.getElementById("lbSenha");
 var SenhaIndex = document.getElementById("SenhaIndex");
 var Senha      = document.getElementById("Senha");
 
 if ((Request.value[4]) && (lbSenha != null))
 {
  lbSenha.innerHTML = "";
  lbSenha.style.color = "navy";
 }

 if (SenhaIndex != null)
 {
  SenhaIndex.value = Request.value[3];
 }

 if (Senha != null)
 {
  Senha.value = "";
 }
}

//** Verifica se a senha está correta ***********************************************************************************************************

function VerificarSenha(FormName,IntraWeb,Matricula)
{
 var Senha = document.getElementById('Senha');
 var SenhaIndex = document.getElementById('SenhaIndex');
 
 if (!Senha || !SenhaIndex)
 {
  return alert(ComponentNotFound);
 }
 
 try
 {
  ShowAguarde(true);
  JRAjax.VerificarSenha(ID,SessionID,FormName,IntraWeb,Matricula,Senha.value,SenhaIndex.value,cbk_VerificarSenha);
 }
 catch(e)
 {
  alert(AjaxNotFound);
  ShowAguarde(false);
 }
}


function cbk_VerificarSenha(Request)
{
 ShowAguarde(false);
 
 ShowAguarde(false);

 var lbSenha    = document.getElementById("lbSenha");
 var SenhaIndex = document.getElementById("SenhaIndex");
 var Senha      = document.getElementById("Senha");


 if ((lbSenha == null) || (SenhaIndex == null) || (Senha == null))
 {
  alert(ComponentNotFound);
  return ;
 }
 
 if (!Request.value[0])
 {
  IW_LoadDigitalKeyBord(false);

  if ((Request.value[1] != null) && (Request.value[1] != ""))
  {
   alert(Request.value[1]);
   return;
  }
  else
  {
   lbSenha.innerHTML   = Request.value[2];
   lbSenha.style.color = "Maroon";
   ;
  }  
  return ; 
 }
 
 //Senha Valida!
 lbSenha.innerHTML = "";
 lbSenha.style.color = "navy";
 Senha.value = "";
 
 try
 {
  new Function(Request.value[3])();
 }
 catch(e)
 {} 
}

//** Verifica Se o User já está na sessão *******************************************************************************************************

function isInSession(ChkPainel)
{
 if (ChkPainel == null)
  ChkPainel = false;
  
 try
 {
  JRAjax.GetSessionInfJS(ID,SessionID,ChkPainel,cbk_isInSession);
 }
 catch(e)
 {
  SetLogOff();
 }
}

function cbk_isInSession(Request)
{
 ShowAguarde(false); 
 try
 {
  if (Request.value[5])
  {
   try
   {
    new Function(Request.value[4])();
   }
   catch(e)
   {
   
   }
   return true;
  }

  if ((Request.value[0]) && (Request.value[1]) && (Request.value[3] != "") && (Request.value[4] != ""))
  {
   Matricula = Request.value[2];
   IrPagina(Request.value[3],'tdHome');
   
   try
   {
    new Function(Request.value[4])();
   }
   catch(e)
   {
   
   }
  } 
  else
  {
   SetLogOff();
  }
 }
 catch(e)
 {
  SetLogOff();
 }
 
}

//** Efetuar LogOff *****************************************************************************************************************************
function IW_LogOff()
{
 
 try
 {
  JRAjax.LogOff(ID,SessionID,cbk_IW_LogOff);
 }
 catch(e)
 {
 
 }
}

function cbk_IW_LogOff(Request)
{
 if (Request.value)
 {
  SetLogOff();
 }
}

function SetLogOff()
{
 Matricula = "";
 
 LoadInnerHtml("tdHome", (SWA) ? "webadmin.aspx" : "home.aspx");
 
 var btnLogin = document.getElementById('btnLogin');
                 
 if (btnLogin)
 {
  btnLogin.innerHTML = '<span onClick="ShowWebAdmin();" ><img src="Imagens/arrow.gif" align="absmiddle"/>Login</span>';
 }  
}

function ShowWebAdmin()
{
 IrPagina("webadmin.aspx","tdHome");
}

//** Carrega a Capa *****************************************************************************************************************************

function LoadNextCapa(StrImgCapa, Index, CodGaleria, Ordem)
{
 try
 {
  var imgCapa = document.getElementById(StrImgCapa);
  
  tmrOutGallery[Index] = null;
  
  if (imgCapa != null)
  {
   imgCapa.onload = null;
   JRAjax.LoadNextCapa(ID, SessionID, StrImgCapa,Index, CodGaleria,Ordem,cbk_LoadNextCapa);      
  }      
 }
 catch(e)
 {
 }
}

function cbk_LoadNextCapa(Response)
{
 var imgCapa = document.getElementById(Response.value[1]);
 
 if ((!Response.value[0]) || (imgCapa == null))
 {
  return;
 }
 
 try
 {
  if (!tmrOverGallery[parseInt(Response.value[2])])
  {
   imgCapa.src = Response.value[5];
  } 
  StartGalleryTimeout(Response.value[1],parseInt(Response.value[2]),parseInt(Response.value[3]),parseInt(Response.value[4]));
 }
 catch(e)
 {
  alert('Erro');
 }
}


//* Função Calendário **************************************************************************************************************
function GetCalendario(StrControl)
{
 Control = document.getElementById(StrControl);
 
 if (!Control)
  return alert(ComponentNotFound);
 
 if ((!Control.disabled) && (Control.style.visibility.toLowerCase() != 'hidden'))
 {
  ShowForm('Calendário','Calendario.aspx?Control=' + StrControl + '&Data='+Control.value);
 } 
}

function SetCalendario(StrControl,StrData,FormName)
{
 Control = document.getElementById(StrControl);
 
 if (!Control)
  return alert(ComponentNotFound);
  
 Control.value = StrData;
 CloseForm(FormName);
}



//** Gerar PDF ***********************************************************************************************************************

function IW_GerarPDF(Url, AdicionalPath, Width, Height, Paisagem, ID, SessionID)
{
 if ((Width == null) || (Width == NaN))
  Width = '';
 if ((Height == null)  || (Height == NaN) )
  Height = '';
 if ((Paisagem == null)  || (Paisagem == NaN) )
  Paisagem = false;
 if ((AdicionalPath   == null)  || (AdicionalPath == NaN) )
  AdicionalPath = "";

  
 try
 {
  ShowAguarde(true);
  
  JRAjax.IW_GerarPDF(ID,SessionID,Url, AdicionalPath,Width,Height,Paisagem,cbk_IW_OpenPDF);
 }
 catch(e)
 {
  ShowAguarde(false);
  alert(AjaxNotFound);
 }
}

//** Abrir PDF Gerado ****************************************************************************************************************

function cbk_IW_OpenPDF(Request)
{
 ShowAguarde(false);
 
 if (!Request.value[0])
 {
  return alert(Request.value[1]);
 }

 self.open(Request.value[3],"_blank");
}

