// JavaScript Document
function loading(stats) {
	var div = document.getElementById("loading");
	
	if(stats == "remove") {
		div.style.display = "none";
	} else {
		div.style.display = "";
	}
}

HTTPRequest = function(){};
with({$: HTTPRequest.prototype}){
    $.isSupported = function(){
        return !!this.getConnection();
    };
    $.events = ["start", "open", "send", "load", "end"];
    $.filter = encodeURIComponent;
    $.getConnection = function(){
        var i, o = [function(){return new ActiveXObject("Msxml2.XMLHTTP");},
        function(){return new ActiveXObject("Microsoft.XMLHTTP");},
        function(){return new XMLHttpRequest;}];
        for(i = o.length; i--;) try{return o[i]();} catch(e){}
        return null;
    };
    $.formatParams = function(params){
        var i, r = [];
        for(i in params) r[r.length] = i + "=" + (this.filter ? this.filter(params[i]) : params[i]);
        return r.join("&");
    };
    $.get = function(url, params, handler, waitResponse){
        return this.request("GET", url + (url.indexOf("?") + 1 ? "&" : "?") + this.formatParams(params), null, handler, null, waitResponse);
    };
    $.post = function(url, params, handler, waitResponse){
        return this.request("POST", url, params = this.formatParams(params), handler, {
            "Connection": "close",
            "Content-Length": params.length,
            "Method": "POST " + url + " HTTP/1.1",
            "Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
        }, waitResponse);
    };
    $.request = function(method, url, params, handler, headers, waitResponse){
		var waitResponse = {"load": loading(), "end": loading('remove')};
        var i, o = this.getConnection(), f = handler instanceof Function;
        try{
            o.open(method, url, !waitResponse);
            waitResponse || (o.onreadystatechange = function(){
                var s = $.events[o.readyState];
                f ? handler(o) : s in handler && handler[s](o);
            });
            o.setRequestHeader("HTTP_USER_AGENT", "XMLHttpRequest");
            for(i in headers)
                o.setRequestHeader(i, headers[i]);
            o.send(params);
            waitResponse && (f ? handler(o) : handler["end"] && handler["end"](o));
            return true;
        }
        catch(e){
            return false;
        }
    };
}

var r = new HTTPRequest;

function carregaFuncionario(email)
{
	if(email == "") {
		alert("Preencha o campo e-mail!");
		document.getElementById("email").focus();
	} else {
		r.post("carrega_funcionario.php", {email: email}, function(o) {
			if(r.events[o.readyState] == "end") 
			{
				var html = o.responseText;
				if(html == "error1") {
					alert("Este e-mail não consta em nossa base de dados. Entre em contato com a organização através do fale conosco.");	
				} else if(html == "error2") {
					alert("Ocorreu um falha no tempo de resposta do servidor. Digite seu e-mail novamente por favor.");	
				} else {
					var html = html.split("|");
					document.getElementById("id_funcionario").value = html[10];
					document.getElementById("nome").value = html[0];
					document.getElementById("franquia").value = html[1];
					document.getElementById("cargo").value = html[2];
					document.getElementById("endereco").value = html[3];
					document.getElementById("bairro").value = html[4];
					if(html[5] == "00000-000") {
						html[5] = "";
					}
					document.getElementById("cep").value = html[5];
					document.getElementById("cidade").value = html[6];
					document.getElementById("estado").value = html[7];
					document.getElementById("pais").value = html[8];
					var celular = html[9];
					var celular = celular.split(")");
					var ddd = celular[0].replace("(", "");
					var celular = celular[1].replace(" ", "");
					var celular = celular.replace(".", "");
					if(ddd == "0") {
						var ddd = "";
					}
					if(celular == "00000000") {
						var celular = ""
					}
					document.getElementById("ddd").value = ddd;
					document.getElementById("celular").value = celular;
				}
			}
		});
	}
}

function carregaTipo(tipo) {
	if(tipo == 1) {
		document.getElementById("func_1").style.display = "";
		document.getElementById("func_2").style.display = "none";
	} else {
		document.getElementById("func_1").style.display = "";
		document.getElementById("func_2").style.display = "";
	}
}

function verificaFunc(tipo) {
	var valor_1 = document.getElementById("id_funcionario_1");
	var valor_2 = document.getElementById("id_funcionario_2");
	if((valor_1) && (valor_2)) {
		if(valor_1.value == valor_2.value) {
			alert("Escolha outro amigo(a)!");
			document.getElementById("id_funcionario_"+tipo).value = "";
		}
	}
}

function carregaFuncs(tipo, id_atual)
{
	if(id_atual!="") {
		var franquia = document.getElementById('franquia_'+tipo).value;
		r.post("carrega_inscrito.php", {tipo: tipo, franquia: franquia, id_atual: id_atual}, function(o) {
			if(r.events[o.readyState] == "end") 
			{
				var html = o.responseText;
				if(html == "error1") {
					alert("Você já escolheu este amigo(a)!");	
				} else if(html == "error2") {
					alert("Selecione a franquia!");
				} else {
					if(html == '') {
						alert("No momento não há ninguém inscrito desta franquia!");
					}
					document.getElementById("amigo_"+tipo).innerHTML = html;
				}
			}
		});
	} else {
		alert("Selecione novamente a franquia");
		document.getElementById('franquia_'+tipo).value = "";
	}
}

function seeIdFriend(email)
{
	if(email == "") {
		alert("Preencha o campo e-mail!");
		document.getElementById("email").focus();
	} else {
		r.post("carrega_id_funcionario.php", {email: email}, function(o) {
			if(r.events[o.readyState] == "end") 
			{
				var html = o.responseText;
				if(html == "error1") {
					alert("Este e-mail não consta em nossa base de dados. Entre em contato com a organização através do fale conosco.");	
				} else if(html == "error2") {
					alert("Ocorreu um falha no tempo de resposta do servidor. Digite seu e-mail novamente por favor.");	
				} else {
					document.getElementById("seeFriend").innerHTML = html;
				}
			}
		});
	}
}


function seeIdFriend2(email)
{
	if(email == "") {
		alert("Preencha o campo e-mail!");
		document.getElementById("email").focus();
	} else {
		r.post("carrega_fabi_id_funcionario.php", {email: email}, function(o) {
			if(r.events[o.readyState] == "end") 
			{
				var html = o.responseText;
				if(html == "error1") {
					alert("Este e-mail não consta em nossa base de dados. Entre em contato com a organização através do fale conosco.");	
				} else if(html == "error2") {
					alert("Ocorreu um falha no tempo de resposta do servidor. Digite seu e-mail novamente por favor.");	
				} else {
					document.getElementById("seeFriend2").innerHTML = html;
				}
			}
		});
	}
}

function otherFriend(id)
{
	r.post("altera_fabi_id_funcionario.php", {id: id}, function(o) {
		if(r.events[o.readyState] == "end") 
		{
			var html = o.responseText;
			alert("Todos os usuários estão disponíveis novamente!");
			reloaderPage();
		}
	});
}

function reloaderPage()
{
	location.href = 'hospedagem_fabi.php?email='+document.getElementById("email").value;
}
