/**
 * @author Intip
 */

function autenticarUsuario(form)
{
	/**
	@sumary:
	@param formu: Formulário HTML, com os dados da pesquisa;
	@type formu: Object Html (Formulário);
	@author: Roberto Brasileiro
	*/
    document.getElementById("naoencontrado").display = 'none';
    document.getElementById("sucesso").display = 'none';
    var msgErrologin = document.getElementById("falha");
    msgErrologin.display = 'none';
	//Desable button
	btn = document.getElementById("btn_entrar");
	if (btn.disabled) return;
	btn.disabled = true;
    msgErrologin.innerHTML = 'Aguarde...';
    msgErrologin.style.display = 'block';
    var restLogin = document.getElementById('restricaologin');
    var debug = document.getElementById('debug');

	with(form)
	{
		if(login.value != "" && senha.value != "")
		{
			var handler = new XMLHandler();
			var xmlreq = new XMLClient('../modulos/cliente/autenticarUsuario/');
			xmlreq.addParam('login', login.value);
			xmlreq.addParam('senha', senha.value);
            if(restLogin != null && restLogin != 'undefined') {
                xmlreq.addParam('restricaologin', restLogin.value);
            }
            if(debug != null && debug != 'undefined') {
                xmlreq.addParam('debug', debug.value);
            }
			handler.onError = function (e) {alert(e)}
			handler.onProgress = function () {}
			handler.onInit = function () {}
			handler.onLoad = function(resultado)
			{
				if(resultado == 'logou')
				{
					form.submit();
				}
				else
				{
					document.getElementById("naoencontrado").style.display = "none";
                    document.getElementById("sucesso").style.display = "none";
                    document.getElementById("resultado").value = resultado;
                    msgErrologin.innerHTML=resultado;
					msgErrologin.style.display = 'block';
					btn.disabled = false;
				}
			}
			xmlreq.query(handler);
		}
		else
		{
			document.getElementById("naoencontrado").style.display = "none";
  	        document.getElementById("sucesso").style.display = "none";
  	        msgErrologin.innerHTML = 'Informe login e senha';
			btn.disabled = false;
		}
	}
}

function verificarEmail(form)
{
	/**
	@sumary:
	@param formu: Formulário HTML, com os dados da pesquisa;
	@type formu: Object Html (Formulário);
	@author: Roberto Brasileiro
	*/
	if(!isEmail(form.email.value)){
      window.alert('Informe um e-mail v\u00E1lido.');
      return;
	}

	with(form)
	{
		if(email.value != "")
		{
			var handler = new XMLHandler();
			var xmlreq = new XMLClient('../modulos/cliente/esqueceuEmail/');
			xmlreq.addParam('email', email.value);
			xmlreq.addParam('nomeagencia', nomeagencia.value);
			handler.onError = function (e) {alert(e)}
			handler.onProgress = function () {}
			handler.onInit = function () {}
			handler.onLoad = function(resultado)
			{
				if(resultado == 'enviado')
				{
					document.getElementById("naoencontrado").style.display = "none";
					document.getElementById("falha").style.display = "none";
					var id = document.getElementById("sucesso");
					id.style.display = 'block';
				}
				else
				{
					document.getElementById("sucesso").style.display = "none";
					document.getElementById("falha").style.display = "none";
					var id = document.getElementById("naoencontrado");
					id.style.display = 'block';
				}
			}
			xmlreq.query(handler);
			toggle();
			email.value = ""
		}
		else
		{
            alert("Digite o e-mail pra continuar.")
		}
	}
}