//   JavaScript Document

var total_reg;

function load_grid(){

    $("login").value=="";
    $("pass").value=="";
    
    new ajax ('consultas_php/seleciona_banner.php', {onLoading: carregando, onComplete: imprime_banner});

    new ajax ('consultas_php/seleciona_total_agenda.php', {onLoading: carregando, onComplete: imprime_total});

	new ajax ('consultas_php/seleciona_agenda.php', {onLoading: carregando, onComplete: imprime});

}

function tipo_mail() {

    if ($("t_mail").value==1) {
         window.open("http://www.appc.pt/webmail")
         $("t_mail").value=0;
        }
    else {
        if ($("t_mail").value==2) {
             window.open("http://exchange.telepac.pt")
             $("t_mail").value=0;
        }
    }
}

function carregando(){

}


function imprime_banner(request){

    $("t_mail").value=0;
    xmldoc=request.responseXML;
    cabecalho = xmldoc.getElementsByTagName('cabecalho')[0];

    var campo = cabecalho.getElementsByTagName('campo');
    var registros = xmldoc.getElementsByTagName('registro');
    var itens = registros[0].getElementsByTagName('item');

    var tabela1="<table cellpadding='0' cellspacing='0'>";
    tabela1+="<tr>";
    tabela1+="<td width='972'>";
    tabela1+="<p><img src='banners/banner"+itens[0].firstChild.data+".jpg' width='972' height='282' border='0'></p>";
    tabela1+="</td>";
    tabela1+="</tr>";
    tabela1+="</table>";

    $("barnner").innerHTML=tabela1;
    tabela1=null;
}

function imprime_total(request){

 xmldoc=request.responseXML;
 try
    {
    cabecalho = xmldoc.getElementsByTagName('cabecalho')[0];
    }
    	catch(e)
    {
    	total_reg=0;
     return false
    }

    if(cabecalho!=null)
	{
              var campo = cabecalho.getElementsByTagName('campo');
              var registros = xmldoc.getElementsByTagName('registro');
              var itens = registros[0].getElementsByTagName('item');
              total_reg=itens[0].firstChild.data;

       }
}

function imprime(request){

 xmldoc=request.responseXML;
 try
    {
    cabecalho = xmldoc.getElementsByTagName('cabecalho')[0];
    }
    	catch(e)
    {
    	$("resultado").innerHTML="Nenhum registro encontrado...";
     return false
    }



	if(cabecalho!=null)
	{
 var campo = cabecalho.getElementsByTagName('campo');

 var tabela="<table width='450' style='border-collapse:collapse;' cellspacing='0' align='left'>";

   tabela+="<tr>";
     tabela+="<td width='7' rowspan='10'></td>";
        tabela+="<td width='370'></td>";
        tabela+="<td width='11' rowspan='10' background='images/latdir_table.gif'></td>";
     tabela+="</tr>";



		//corpo da tabela
  var registros = xmldoc.getElementsByTagName('registro');
  var itens = registros[0].getElementsByTagName('item');

  var lin=0;
  if (itens[4].firstChild.data!=0) {
         var lin=1;
  }

   tabela+="<tr id=linha0>";
       tabela+="<td width='370' height='30' align='left' valign='top'>";
       tabela+="<p align='left'><font face='Arial' color='#304569'><span style='font-size:11pt;'><b>";
       tabela+=itens[2].firstChild.data+"</b></span></font>";
       tabela+="</p></td>";
       tabela+="</tr>";

       tabela+="<tr>";
       tabela+="<td width='370' valign='middle'>";
       tabela+="<p align='justify'><span style='font-size:8pt;'>&nbsp;</span><span class='Apple-style-span' style='font-family:";
       tabela+="Verdana; font-size:10pt; color:rgb(102,102,102); text-align:left;'><font face='Verdana'>";
       tabela+=itens[3].firstChild.data+"</b></font></span></p>";
       if (lin!=0) {
              if (itens[5].firstChild.data!=1) {
                tabela+="<a href='n_agenda/noticia_"+itens[0].firstChild.data+".htm'><p align='right'><img src='images/mais.gif' width='59' border='0'></p>";
              }
              else {
                tabela+="<a href='n_agenda/noticia_"+itens[0].firstChild.data+".htm' target='_blank'><p align='right'><img src='images/mais.gif' width='59' border='0'></p>";
              }
       }
       else {
                tabela+="<p align='right'><img src='images/maisbranco.gif' width='59' height='15' border='0'></p>";

       }
       tabela+="</p></td>";
       tabela+="</tr>";

var erro=1;
  try
    {
       var itens = registros[1].getElementsByTagName('item');
    }
  catch(e)
    {
       var erro=0;
    }
    
   var lin=0;
   if(erro!=0) {
       var d=itens[1].firstChild.data;
       var t=itens[2].firstChild.data;
       var desc=itens[3].firstChild.data;
       if (itens[4].firstChild.data!=0) {
              var lin=1;
       }
       
   }
   else {
       var d="";
       var t="";
       var desc="";
   }



       tabela+="<tr id=linha1>";

       tabela+="<td width='370' height='30' align='left' valign='top'>";
       tabela+="<p align='left'><font face='Arial' color='#304569'><span style='font-size:11pt;'><b>";
       tabela+=t+"</b></span></font>";
       tabela+="</p></td>";
       tabela+="</tr>";

       tabela+="<tr>";
       tabela+="<td width='370' valign='middle'>";
       tabela+="<p align='justify'><span style='font-size:8pt;'>&nbsp;</span><span class='Apple-style-span' style='font-family:";
       tabela+="Verdana; font-size:10pt; color:rgb(102,102,102); text-align:left;'><font face='Verdana'>";
       tabela+=desc+"</b></font></span></p>";
       if (lin!=0) {
              if (itens[5].firstChild.data!=1) {
                tabela+="<a href='n_agenda/noticia_"+itens[0].firstChild.data+".htm'><p align='right'><img src='images/mais.gif' width='59' border='0'></p>";
              }
              else {
                tabela+="<a href='n_agenda/noticia_"+itens[0].firstChild.data+".htm' target='_blank'><p align='right'><img src='images/mais.gif' width='59' border='0'></p>";
              }
       }
       else {
                tabela+="<p align='right'><img src='images/maisbranco.gif' width='59' height='15' border='0'></p>";
       }
       tabela+="</p></td>";
       tabela+="</tr>";

  var erro=1;
  try
    {
       var itens = registros[2].getElementsByTagName('item');
    }
  catch(e)
    {
       var erro=0;
    }



   var lin=0;
   if(erro!=0) {
       var d=itens[1].firstChild.data;
       var t=itens[2].firstChild.data;
       var desc=itens[3].firstChild.data;
       if (itens[4].firstChild.data!=0) {
              var lin=1;
       }

   }
   else {
       var d="";
       var t="";
       var desc="";
   }

       tabela+="<tr id=linha2>";
       tabela+="<td width='370' height='30' align='left' valign='top'>";
       tabela+="<p align='left'><font face='Arial' color='#304569'><span style='font-size:11pt;'><b>";
       tabela+=t+"</b></span></font>";

       tabela+="</p></td>";

       tabela+="</tr>";
       
       tabela+="<tr>";
       tabela+="<td width='370' valign='middle'>";
       tabela+="<p align='justify'><span style='font-size:8pt;'>&nbsp;</span><span class='Apple-style-span' style='font-family:";
       tabela+="Verdana; font-size:10pt; color:rgb(102,102,102); text-align:left;'><font face='Verdana'>";
       tabela+=desc+"</font></span></p>";
        if (lin!=0) {
              if (itens[5].firstChild.data!=1) {
                tabela+="<a href='n_agenda/noticia_"+itens[0].firstChild.data+".htm'><p align='right'><img src='images/mais.gif' width='59' border='0'></p>";
              }
              else {
                tabela+="<a href='n_agenda/noticia_"+itens[0].firstChild.data+".htm' target='_blank'><p align='right'><img src='images/mais.gif' width='59' border='0'></p>";
              }
       }
       else {
                tabela+="<p align='right'><img src='images/maisbranco.gif' width='59' height='15' border='0'></p>";

       }

        tabela+="</p></td>";
        tabela+="</tr>";

       if (total_reg>3)  {
            tabela+="<tr>";
            tabela+="<td width='370' height='54' align='right' valign='bottom'>";
            tabela+="<a href='index_seg.php?seg=3'><img src='images/seguinte.gif' width='84' height='24' style='border-style:none;'></a></span></font></p>";
            tabela+="</td>";
            tabela+="</tr>";
       }
       tabela+="</table>";
       $("resultado").innerHTML=tabela;
       tabela=null;
	}
	else
	      $("resultado").innerHTML="Nenhum registro encontrado...";
}

function tete_usuario(){

        if($("login").value==""){
    		alert("Preencha o campo Login")
	       	return false;
	   }
	   else if($("pass").value==""){
            alert("Preencha o campo Password")
            return false;
	   }


        new ajax ('consultas_php/seleciona_dia.php', {onLoading: carregando, onComplete: tete_data});

}

function tete_data(request){
    calcula_dia()

    xmldoc=request.responseXML;
    cabecalho = xmldoc.getElementsByTagName('cabecalho')[0];

    var campo = cabecalho.getElementsByTagName('campo');
    var registros = xmldoc.getElementsByTagName('registro');
    var itens = registros[0].getElementsByTagName('item');
    if (data_igual(itens[0].firstChild.data,data1)==true) {
        new ajax ('consultas_php/teste_user.php?login=' + document.getElementById('login').value +
        '&pass=' + document.getElementById('pass').value+ '&tipo=1', {onLoading: carregando, onComplete: tete_user});
     }
     else {
        alert ("Acerte por favor a data do seu computador com a data do servidor: " + itens[0].firstChild.data)
     }
}

function tete_user(request){
    xmldoc=request.responseXML;
        try
        {
            cabecalho = xmldoc.getElementsByTagName('cabecalho')[0];
            new ajax ('consultas_php/teste_user.php?login=' + document.getElementById('login').value +
            '&pass=' + document.getElementById('pass').value + '&tipo=2', {onLoading: carregando, onComplete: tete_pass});
        }
    	catch(e)
        {
            $("login").value=="";
            $("pass").value=="";
            alert("User inexistente!")
            return false
    }
}

function tete_pass(request){
    xmldoc=request.responseXML;
        try
        {
            cabecalho = xmldoc.getElementsByTagName('cabecalho')[0];
            new ajax ('consultas_php/inicio_secao.php?login=' + document.getElementById('login').value +
            '&pass=' + document.getElementById('pass').value, {onLoading: carregando, onComplete: inicio});

        }
    	catch(e)
        {
            $("pass").value=="";
            alert("Password errada!")
            return false
    }
}
function inicio(request){

           window.location = ("a_reservada/inicio_area_reservada.php");

}
