var time = null;			  
var TEMPO_PADRAO = 100;
var TAMANHOPADRAO = 10;
var TAMANHOMAXIMO = 100;
var ultimabusca = '';
var cont = 0;

var num_cesta = 0;
var qde_produto = new Array();
var vlr_produto = new Array();
var total_cesta = 0;

function mostratexto(vo)
{
var agora = new Date();
if (time==null)
   {
     time = agora;
   }
var dif = (agora-time);
cont=0;
monitoraTempo();
time=agora;
}					  

function monitoraTempo()
{
if (time!=null&&el.value!=ultimabusca)
   {
     var agora = new Date();
     var dif = (agora-time);
     if (dif>=TEMPO_PADRAO)
        {
		  // Processar a requisição
		  selecionarPacientes();
		  time=agora; // para não chamar novamente
		  cont = 99;
		  ultimabusca = el.value;
        }
   }
cont++;
if (cont==1)
   {
     setTimeout('monitoraTempo()',TEMPO_PADRAO);
   }
else
   {
     cont = 0;
   }
}

function selecionarPacientes()
{
var desc=el.value;
var ajax=new AJAX();
var params='?nomep='+desc;
//var params='?descricao='+desc;
if (document.BUSCA.offline.checked)
   {
     params=params + '&offline=N';
   }
if (document.BUSCA.modo.checked)
   {
     params=params + '&modo=T';
   }
params=params + '&linhas=' + document.BUSCA.tamanho.value;
ajax.url='altera_pacientes_desc_ajax.php' + params;
ajax.modo='X';
ajax.processaresultado=function(xmldoc)
                               {
								 qde_produto=new Array();
								 qde_nomes=new Array();
								 var titulo=new Array('Paciente','Volume Ini.','Hardware','Quarto','*Alterar*');
								 var s=document.getElementById("pacientes");
								 var nos=xmldoc.getElementsByTagName('paciente');
                                 var xy=0;
								 s.innerHTML='';
								 s.style.display='none';
								 var tab=document.createElement("table");
								 tab.className='tabela';
								 var tbo=document.createElement("tbody");
								 tab.appendChild(tbo);
                                 // cabeçalho da tabela
								 var tr = document.createElement("tr");
								 for (t in titulo)
                                     {
									   var td=document.createElement("td");
									   td.className='cabecalho';
									   var tx=document.createTextNode(titulo[t]);
									   td.appendChild(tx);
									   tr.appendChild(td);
                                     }
                                 tbo.appendChild(tr);
                                 if (xmldoc.hasChildNodes()&&nos.length>0)
                                    {
                                      for (var i=0;i<nos.length;i++)
                                          {
                                            var no=nos[i];
                                            var tr=document.createElement("tr");

                                            var td=document.createElement("td");
                                            var tx=document.createTextNode(no.childNodes[0].firstChild.nodeValue);
                                            xy=no.childNodes[4].firstChild.nodeValue;
                                            qde_nomes[xy]=no.childNodes[0].firstChild.nodeValue;
                                            td.className='linha';
                                            td.appendChild(tx);
                                            tr.appendChild(td);
                                            //Input 1
                                            var td = document.createElement("td");
										    td.align = 'center';
										    var qd = document.createElement("input");
										    qd.name = no.childNodes[4].firstChild.nodeValue;
										    qd.size = 5;
										    qd.value = no.childNodes[1].firstChild.nodeValue;
										    qde_produto[no.childNodes[4].firstChild.nodeValue] = no.childNodes[1].firstChild.nodeValue;
										    qd.onkeyup = 	function(e) {
															qde_produto[this.name] = this.value;
														}
										    td.appendChild(qd);
										    td.className = 'linha';
										    tr.appendChild(td);
                                            //Input 2
                                            var td = document.createElement("td");
										    td.align = 'center';
										    var qd = document.createElement("input");
                                            xy=no.childNodes[4].firstChild.nodeValue;
                                            xy++;
                                            qd.name = xy
										    qd.size = 5;
										    qd.value = no.childNodes[2].firstChild.nodeValue;
										    qde_produto[xy] = no.childNodes[2].firstChild.nodeValue;
										    qd.onkeyup = 	function(e) {
															qde_produto[this.name] = this.value;
														}
										    td.appendChild(qd);
										    td.className = 'linha';
										    tr.appendChild(td);
                                            //Input 3
                                            var td = document.createElement("td");
										    td.align = 'center';
										    var qd = document.createElement("input");
                                            xy=no.childNodes[4].firstChild.nodeValue;
                                            xy++;
                                            xy++;
                                            qd.name = xy
										    qd.size = 5;
										    qd.value = no.childNodes[3].firstChild.nodeValue;
										    qde_produto[xy] = no.childNodes[3].firstChild.nodeValue;
										    qd.onkeyup = 	function(e) {
															qde_produto[this.name] = this.value;
														}
										    td.appendChild(qd);
										    td.className = 'linha';
										    tr.appendChild(td);

										    var td=document.createElement("td");
										    var a=document.createElement("a");
                                            a.href='javascript: Monitora('+no.childNodes[4].firstChild.nodeValue+');';
										    a.className='monitorar';
										    var tx=document.createTextNode("Alterar");
										    a.appendChild(tx);
										    td.className='linha';
										    td.appendChild(a);
										    tr.appendChild(td);
										    tbo.appendChild(tr);
									      }
									s.appendChild(tab);
									s.style.display='inline';
                                  }
                               }
ajax.conectar();
}

function verificaTamanho(v)
{
if (v.value<=0)
   {
     v.value=TAMANHOPADRAO;
   }
else if (v.value>TAMANHOMAXIMO)
        {
          v.value=TAMANHOMAXIMO;
        }
}

function Monitora(paciente)
{
// Envia os dados para o servidor
//http://10.1.1.82/Biota/alterapaciente.php?id=11&quarto=101&volini=1000&hwserial=50
var ajax = new AJAX();
var params = '?id='+paciente;
params = params + '&volini='+qde_produto[paciente];
params = params + '&hwserial='+qde_produto[paciente+1];
params = params + '&quarto='+qde_produto[paciente+2];
ajax.url = 'alterapaciente.php' + params;
ajax.modo = 'X';
ajax.produto = paciente;
ajax.quantidade = 1;
ajax.processaresultado = function(xmldoc) {}
var texto='Paciente: '+qde_nomes[paciente]+', I1='+qde_produto[paciente]+', I2='+qde_produto[paciente+1]+', I3='+qde_produto[paciente+2];
var e  = document.getElementById('resposta');
var n  = e.childNodes;
n[0].nodeValue = texto;
ajax.conectar();
/*
for (var i=0;i<30;i++)
    {
      document.write('<br>'+'qde_produto['+i+']='+qde_nomes[i]);
    }
*/
//var e=document.getElementById('resposta');
//var no=document.createElement('DIV');
//var t=document.createTextNode(texto);
//e.appendChild(no);
//no.appendChild(t);
/*
var tr = document.createElement("tr");
tr.setAttribute("id","resposta");
var td = document.createElement("td");
var tx = document.createTextNode(texto);
td.className = 'linha_cesta';
td.appendChild(tx);
tr.appendChild(td);
*/
/*
document.write('Paciente='+paciente+',Input1='+qde_produto[paciente]+',Input2='+qde_produto[paciente+1]+',Input3='+qde_produto[paciente+2]);
for (var i=0;i<30;i++)
    {
      document.write('<br>'+'qde_produto['+i+']='+qde_produto[i]);
    }
var ajax = new AJAX();
var params = '?cesta=' + num_cesta;
params = params + '&produto='+produto;
params = params + '&quantidade='+qde_produto[produto];
ajax.url = 'cesta_compras_ajax.php' + params;
ajax.modo = 'X';
ajax.produto = produto;
ajax.quantidade = qde_produto[produto];
*/
}
