// Testa campos Formulário obrigatórios

function isEmpty(str) {

            for (var intLoop = 0; intLoop < str.length; intLoop++)
               if (" " != str.charAt(intLoop))
                  return false;
			return true;
}


function checkRequired(f) {
            var strError = "";
            for (var intLoop = 0; intLoop<f.elements.length; intLoop++)
               if (null!=f.elements[intLoop].getAttribute("required")) 
                  if (isEmpty(f.elements[intLoop].value))
                     strError += "  " + f.elements[intLoop].name + "\n";
            if ("" != strError) {
               alert("Preencha os seguintes campos:\n" + strError);
               return false;
            }
}


//

// Testa se tem o minimo preenchido

function test_pos(obj, minim){
if(obj.value<minim || isNaN(obj.value)){
obj.value = '1';
alert('Valor Inválido!');}
}

// Pesquisa

function pesquisa(tipo){
	var obj = document.pesquisar.p;
	if(tipo==0){if(obj.value=="Pesquisar...") obj.value = ""; obj.style.color="";}
	if(tipo==1){if(obj.value==""){obj.value = "Pesquisar..."; obj.style.color="#CCCCCC";}}
}

// TIME

var montharray=new Array("Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro")
var serverdate=new Date(currenttime)

function padlength(what){
var output=(what.toString().length==1)? "0"+what : what
return output
}

function displaytime(){
serverdate.setSeconds(serverdate.getSeconds()+1)
var timestring=padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds())
document.getElementById("servertime").innerHTML="Açores, "+timestring
}

// AJAX GET

var httpRequest = null

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

fila=[]
ifila=0

function Abrir(url,id){
	
	document.getElementById('loading').style.display = 'block';

    fila[fila.length]=[url,id]

    if((ifila+1)==fila.length)ajaxRun()
}


function ajaxRun(){

	var string = unescape(fila[ifila][0]);
   
	httpRequest.open("GET", string+'&'+Math.random()+'&ajax=true', true);

	httpRequest.onreadystatechange=function() {
        
	if (httpRequest.readyState==4){

            retorno=httpRequest.responseText

            document.getElementById(fila[ifila][1]).innerHTML = retorno
			document.getElementById('loading').style.display = 'none';


            ifila++
            if(ifila<fila.length)setTimeout("ajaxRun()",20)
        }
    }

    httpRequest.send(null)
}


/// AJAX POST
   function makePOSTRequest(url, parameters, div) {
      
	var loading = "<p align='center'><img border='0' align='center' src='"+base_path+"imagens/loading.gif'/></p>";
      document.getElementById(div).innerHTML = loading;            
      http_request = false;
      
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Não foi possível criar o XMLHTTP!');
         return false;
      }
      
	  http_request.onreadystatechange = function(){alertContent(http_request, div);};
	  var string = unescape(url);
      http_request.open('POST', string+'&'+Math.random()+'&ajax=true', true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

    function alertContent(http_request, div) {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
			document.getElementById(div).innerHTML = result;            
         } else {
            alert("Ocorreu um erro com o pedido!");
         }
      }
   }
   
  
function GetElements(inRoot){
var elem_array = new Array;
if(typeof inRoot.firstChild!= 'undefined'){
var elem = inRoot.firstChild;
while (elem!= null){
if(typeof elem.firstChild!= 'undefined'){
elem_array = elem_array.concat(GetElements(elem));}
if(typeof elem.value!= 'undefined'){
elem_array.push(elem);}
elem = elem.nextSibling;}}
return elem_array;
}

function DisplayElements(in_elem_array, link, div){
if(in_elem_array.length){
var poststr = "";
for(var c=0; c<in_elem_array.length; c++){
poststr += encodeURIComponent(in_elem_array[c].name) +"="+ encodeURIComponent(in_elem_array[c].value) +"&";
}}
makePOSTRequest(link, poststr, div);
}
