///////////////////////////////////////////////////////////////////////////////
//SETA VARIAVEIS
///////////////////////////////////////////////////////////////////////////////
var xmlHttp = createXmlHttpRequestObject(); 
var nav;
var path = "_ajax.php";
///////////////////////////////////////////////////////////////////////////////
//CRIA o XML HTTP REQUEST OBJECT
///////////////////////////////////////////////////////////////////////////////
function createXmlHttpRequestObject() {  
	var xmlHttp;
	if(window.ActiveXObject){
		try{
		  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		  nav = 'ie';
		}catch(e){
		  xmlHttp = false;
		}
	}else{
		try {
		  xmlHttp = new XMLHttpRequest();
     	 	  nav = 'ff';
		}catch(e){
		  xmlHttp = false;
		}
	}
	
	if(!xmlHttp){
		alert("Erro na criação do XMLHTTP.");
	}else{
		return xmlHttp;
	}
}
///////////////////////////////////////////////////////////////////////////////
//ENVIA VIA POST
///////////////////////////////////////////////////////////////////////////////
function dar_post(x,parametros){
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("POST", path+x, true);  
		xmlHttp.setRequestHeader('Content-Type', "application/x-www-form-urlencoded");
  		xmlHttp.setRequestHeader('Content-Length', parametros.length);
		xmlHttp.send(parametros);
	}else{
		setTimeout('process('+x+','+parametros+')', 1000);
	}
}
///////////////////////////////////////////////////////////////////////////////
//ENVIA VIA GET
///////////////////////////////////////////////////////////////////////////////
function dar_get(x){
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		if (xmlHttp.overrideMimeType) {
        	xmlHttp.overrideMimeType('text/xml');
        }
		xmlHttp.open("GET", path+x, true);  
		xmlHttp.send(null);
	}else{
		setTimeout('dar_get('+x+')', 1000);
	}
}
///////////////////////////////////////////////////////////////////////////////
//MOSTRA ERRO DO AJAX
///////////////////////////////////////////////////////////////////////////////
function erro_ajax(){
	alert("Problemas durante a conexÃ£o com o servidor: "+ xmlHttp.statusText);	
}
///////////////////////////////////////////////////////////////////////////////
//FUNÇÃO PARA LOGAR
///////////////////////////////////////////////////////////////////////////////
function login(login,senha,tipo){
	if(login.length == 0){
		alert("Digite um login!");
		return false;
	}
	if(senha.length == 0){
		alert("Digite uma senha!");
		return false;
	}	
	if(xmlHttp.readyState == 4){
		xmlHttp = null;
		xmlHttp = createXmlHttpRequestObject(); 
	}	

	xmlHttp.onreadystatechange = res_login;
	k = Math.random();
	dar_get("?x=login&login="+unescape(login)+"&senha="+unescape(senha)+"&tipo="+unescape(tipo)+"&k="+k);	
}
///////////////////////////////////////////////////////////////////////////////
//FUNÇÃO PARA VALIDAR O RETORNO DO LOGIN()
///////////////////////////////////////////////////////////////////////////////
function res_login(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){	
			if(nav=='ie'){
				res_xml = xmlHttp.responseXML;
				dados = res_xml.documentElement;
				cont = dados.childNodes(0).childNodes(0).data;
			}else{
				res_xml= xmlHttp.responseXML;
				dados = res_xml.getElementsByTagName('retorno').item(0);
				cont =  dados.firstChild.data;
			}		
			if(cont=='false'){
				alert("Usuário e/ou senha inválidas!");
			}else{
				window.open("./restrita/entrada.php");
			}			
		}else{
			erro_ajax();
		}
	}		
}