var Retorno=0;
var ConectionLost=false;
var OldConectionLost=false;
var ContTempoConexao='0';
var OldContTempoConexao='-1';
var IntervalID=0;
var VerifConexao=false;

function TesteConexao(){
  VerifConexao=true;	
  
  ConectionLost=((ContTempoConexao==OldContTempoConexao) && (ContTempoConexao!=0));
  
  if (ConectionLost){
    hideLoadingMessage();
    xajax.loadingFunction = null;  
    divPrincipal.innerHTML='<table height="100%"><tr><td><font color="red"><h4><b>Conexão Perdida</b></h4></font></td><tr></table>';
  } else if (OldConectionLost) window.location.reload();
  OldContTempoConexao=ContTempoConexao;
  OldConectionLost=ConectionLost;
  xajax_TesteConexao();  
  VerifConexao=false;  
}

function TesteConexaoInicio(){
  TesteConexao();
  IntervalID=setInterval('TesteConexao()',30000);
}

function StopTesteConexao(){
  if (IntervalID>0)	
    clearInterval(IntervalID);
}

function Reload(tempo){
  if (tempo>0) setTimeout('window.location.reload()',tempo*1000);
  else window.location.reload();  
}


//**************************************
//* Event Listener Function v1.4       *
//* Autor: Carlos R. L. Rodrigues      *
//**************************************

addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};


//==================================================================================

function KeyRedirect(key,func){
  var k=event.keyCode;
  if (k==key) eval(func);
}

function KeyEnterFunc(func){
  var k=event.keyCode;
  if (k==13) eval(func);
}

function EnterPress(c){
  var kc = window.event.keyCode;
      tecla = String.fromCharCode(kc);

if (event.keyCode==13) c.focus();
else {
  if(!((tecla >= "0") && (tecla <= "9")))
    window.event.keyCode = 0;

  }
}

function TabPress(c){
  var kc = window.event.keyCode;
  if (kc == 9) return false;	
}

function MsgAlert(Msg) {
 if (Msg!="") alert(Msg);
}

function WindowResizeAndPos(Left,Top,Width,Height){
 window.resizeTo(Width,Height);
 window.moveTo(Left,Top); 
 setTimeout("window.resizeTo("+Width+","+Height+")", 10);
}

function WindowCentered(Width,Height){
 window.resizeTo(Width,Height);
 window.moveTo((screen.width-Width)/2,(screen.height-Height)/2); 
 setTimeout("window.resizeTo("+Width+","+Height+")", 10);
}

function GetVerIE(){
 var v=navigator.appVersion;
 return parseFloat(v.substr(v.indexOf('MSIE')+5,3));
}


//==================================================================================

function KeyRetorno(){
  var k=event.keyCode;
  if (k!=27) return false;
  GoToRetorno();
}

function GoToRetorno(){
  switch (Retorno){
   case 0:
    window.location.reload();
    break;
   case 1:
    xajax_MenuPrincipal();
    break;	
   case 2:
    xajax_Boleto();
    break;		
   case 3:
    xajax_EmprestimosReservas();
	break;
  }
}

function KeyPressNotas(){
 var k =event.keyCode;
 if (k==49) location.href="#fim";
 if (k==50) location.href="#inicio";
 if (k==13) xajax_ImpNotas();
}


function SetEventKeysNotas(){
addEvent(document,"keypress",KeyPressNotas);	
}

function UnsetEventKeysNotas(){
removeEvent(document,"keypress",KeyPressNotas);		
}

function ImpNotas(codta,tipo){ 
  if (tipo==0)
    window.location.href='impterminal.php?codturmaaluno='+codta+'&tipo=0';//apenas redireciona para pág de impressão	
  if (tipo==1){
     setTimeout('ImpNotas('+codta+',2);',10000); //depois de imp. manda confirmação de impressão
    window.open('impterminal.php?codturmaaluno='+codta+'&tipo=1'); //imprime
  }
  if (tipo==2)
    window.location.href='impterminal.php?codturmaaluno='+codta+'&tipo=2';
}

function SalvarSenha(s,ns,rs){
 if ((s=='')||(ns=='')||(rs=='')) alert('Todos os campos devem ser preenchidos');
 else if (ns!=rs) alert('A nova senha é diferente da redigitada');
      else if (ns.length<4) alert("A nova senha deve ter no mínimo 4 digitos");
	       else xajax_SalvarSenha(s,ns);
 senha.focus();		   
}

function ExReserva(codreserva){
  if (confirm("Deseja realmente excluir esta reserva?"))
     xajax_ExReserva(codreserva);	
}

function Renovar(codemp){
  if (confirm("Deseja realmente renovar este material?"))
     xajax_Renovar(codemp);
}

function RespRenovacao(r){
 switch (r){
   case 0:
    alert('Renovação realizada com sucesso');
    break;
   case 1:
    alert('Impossível renovar! Este material esta sob reserva, favor devolvê-lo até a data estipulada');
    break;	
   case 2:
    alert('Não é permitido a renovação deste material'); 
    break;		
   case 3:
    alert('Houve um erro ao reservar');
	break;
  }
}

/*================================================================================================*/
function AbreAreaPessoal(){
 window.open("areapessoal/fprincipal.php","AreaPessoal","top=0,left=0,height=200,width=250,scrollbars=1,toolbar=0,location=0,menubar=0,resizable=0,statusbar=0");
}

function AbreAreaDocente(){
 var h=(screen.height-10);
 var w=(screen.width-5);
 window.open("areadocente/logindocente.php","AreaDocente","top=0,left=0,height="+h+",width="+w+",scrollbars=1,toolbar=0,location=0,menubar=0,resizable=0,statusbar=0");
}

function AbrePesqMateriais(){
 var h=(screen.height-10);
 var w=(screen.width-5);
 window.open("areapessoal/fpesqmateriais.php","AreaPessoal","top=0,left=0,height="+h+",width="+w+",scrollbars=1,toolbar=0,location=0,menubar=0,resizable=0,statusbar=0");
}

function GetFileDownload(codd){
 var h=(110);
 var w=(350);
 window.open("../call_functions.php?function=GetFileDownload&coddownload="+codd,"DownloadFile","top=0,left=0,height="+h+",width="+w+",scrollbars=1,toolbar=0,location=0,menubar=0,resizable=0,statusbar=0");
	
}

function BloqButtonMouseRigth() {
  if (event.button==2) 
   alert("Use o botão esquerdo do mouse");
  else return true;
}

