//FUNCOES DO AJAX-------------------------------------------------------------------------------------------------------------------------
function getLista(id, oque, pagina){
			fc = "getLista";
			if (oque=="autor"){
				//if (id=="" || id=="-1"){					
					//if (document.getElementById("idTipo").value != "") document.getElementById("idSubTipo").value = "";
					document.getElementById("idTipo").value = "";
				//}
			}
			if (id!=""){
				if (id != "-1"){		
					u="getLista.asp?sid=" + Math.random() + "&oque="+oque+"&id="+id+"&pagina="+pagina;
					if (oque == "tipo"){
						u="getLista.asp?sid=" + Math.random() + "&oque="+oque+"&id="+id+"&pagina="+pagina;
						if (document.getElementById("idAutor").value != "" && document.getElementById("idAutor").value != "-1"){
							u="getLista.asp?sid=" + Math.random() + "&oque="+oque+"&id="+id+"&pagina="+pagina+"&idAutor="+document.getElementById("idAutor").value;
						}						
					}else if (oque == "subTipo"){
						u="getLista.asp?sid=" + Math.random() + "&oque="+oque+"&id="+id+"&pagina="+pagina+"&idTipo="+document.getElementById("idTipo").value;
						if (document.getElementById("idAutor").value != "" && document.getElementById("idAutor").value != "-1"){
							u="getLista.asp?sid=" + Math.random() + "&oque="+oque+"&id="+id+"&pagina="+pagina+"&idTipo="+document.getElementById("idTipo").value+"&idAutor="+document.getElementById("idAutor").value;
						}						
					}
				}else{				    														
					u="getLista.asp?sid=" + Math.random() + "&oque="+oque+"Todos&id="+id+"&pagina="+pagina;
					
				}
				eval("document.getElementById(\"divConteudo\").innerHTML=\"<p align=center><font color=#FF0000>AGUARDE...</font></p>\"");			
				fazRequest(u);
			}			
}

function getSubTipo(id){
			fc = "getSubTipo";
			if (id!=""){			
				u="getSubTipo.asp?sid=" + Math.random() + "&id="+id;														
				eval("document.getElementById(\"comboSubTipo\").innerHTML=\"<p align=center><font color=#FF0000>AGUARDE...</font></p>\"");			
				//fazRequest(u);
				eval("document.getElementById(\"comboSubTipo\").innerHTML=loadHTTP(u)");
			}else{
				eval("document.getElementById(\"comboSubTipo\").innerHTML=\"\"");
			}			
}

function getTemplate(id){
			fc = "getTemplate";
			if (id!=""){			
				u="template.asp?sid=" + Math.random() + "&idTexto="+id;														
				eval("document.getElementById(\"divConteudo\").innerHTML=\"<p align=center><font color=#FF0000>AGUARDE...</font></p>\"");			
				fazRequest(u);				
			}else{
				eval("document.getElementById(\"divConteudo\").innerHTML=\"\"");
			}			
}

//usada no usuario.asp
function getCidade(estado){			
			if (estado!=""){			
				u="getCidade.asp?sid=" + Math.random() + "&estado="+estado;
				eval("document.getElementById(\"linhaCidade\").style.display=\"\"");													
				eval("document.getElementById(\"divCidade\").innerHTML=\"<p align=center><font color=#FF0000>AGUARDE...</font></p>\"");							
				eval("document.getElementById(\"divCidade\").innerHTML=loadHTTP(u)");
			}else{
				eval("document.getElementById(\"linhaCidade\").style.display=\"none\"");
				eval("document.getElementById(\"divCidade\").innerHTML=\"\"");
			}			
}


//usada no modo assincrono
function checaEstadoAjax() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{	//alert(xmlHttp.responseText);
		if (fc == "getLista"){			
			eval("document.getElementById(\"divConteudo\").innerHTML=xmlHttp.responseText");			
		}else if (fc == "getSubTipo" ){
			eval("document.getElementById(\"comboSubTipo\").innerHTML=xmlHttp.responseText");			
		}else if (fc == "getTemplate" ){
			eval("document.getElementById(\"divConteudo\").innerHTML=xmlHttp.responseText");			
		}				
	}
} 

//faz request de modo assincrono
function fazRequest(u){
	xmlHttp=GetXmlHttpObject(checaEstadoAjax);
	xmlHttp.open("GET", u , true);
	xmlHttp.send(null);
}

//modo sincrono
// Essa funcao tenta pegar o HTTPrequest adequado, e retorna em forma de texto(string) a pagina solicitada.
var req;
function loadHTTP(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		try{
			req.open("GET",url,false);
			req.send(null);
			return req.responseText;
		}catch(e){
			//return "0#Erro ao chamar httpRequest";
			return false;
		}
	}else{
		return false;
	}
}

//modo assincrono
function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null
	
	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		alert("This example doesn't work in Opera");
		return;
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP";
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler; 
			return objXmlHttp;
		} 
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled"); 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler; 
		return objXmlHttp;
	}
}

//FIM FUNCOES DO AJAX---------------------------------------------------------------------------------------------------------------------


function CampoVazio(x)
{

	if (x.value == "") 
	{
		window.alert(x.name+": Campo obrigatório, favor preencher!");
		x.focus();
		return false;
	}

	return true;
}


function validaBusca(){
	if (document.getElementById("texto").value.length <3 ){
		alert("Preencha o campo Busca com pelo menos 3 caracteres!");
		document.getElementById("texto").focus();
		return false;
	}
	
	return true;
}

function valida(quem){   
   switch (quem){
      case "usuario":
	  			{	if (document.getElementById("estado").value == "")
						var varray = new Array("nome","email","estado","telefone"); 
					else
						var varray = new Array("nome","email","estado","cidade","telefone"); 	
						break;
				}	 	 
   }	
    var temp;
	for(var i in varray) // alternate: declare var inline
	{  temp = eval("document.getElementById(\""+varray[i]+"\").value");	   
 	   if(temp == '')		
		{ 	
		  alert("O campo "+varray[i]+" deve ser preenchido!");
		  eval("document.getElementById(\""+varray[i]+"\").focus()");
		  return false;
		}
	}			
}
function form_submit(action, form){
    //alert("teste"+action+form);
    document.form1.action = action;
	document.form1.submit();
}

function ajustar_data(input , evento)
{
         var BACKSPACE=  8; 
         var DEL=  46; 
         var FRENTE=  39; 
         var TRAS=  37; 
         var key; 
         var tecla; 
         var strValidos = "0123456789" ;
         var temp;
         tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
         //alert(tecla)
         if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)) {
             return true; 
             }
         if ( tecla == 13 )     return false; 
         if ((tecla<48)||(tecla>57)){
		    return false;
		    }
         key = String.fromCharCode(tecla); 
         input.value = input.value+key;
         temp="";
         for (var i = 0; i<input.value.length;i++ )
             {
               if (temp.length==2) temp=temp+"/";
               if (temp.length==5) temp=temp+"/";
               if ( strValidos.indexOf( input.value.substr(i,1) ) != -1 ) {
                  temp=temp+input.value.substr(i,1);
               }   
             }
         input.value = temp.substr(0,10);
         return false;
	//
}	     



