function send_form(form,action,aguarde,confirma,erro) {
	if(valida_form(form)) {
		var lv = new LoadVars();
		// guardando dados do formulario
		with(form) {
			for(i=0;i<elements.length;i++) {
				eval("lv."+elements[i].name+"=\""+elements[i].value.replace(/\r\n/g, "<br>")+"\";\n");
			}
		}
		// ocultando mensagem de aguarde e exibindo mensagem de confirmacao
		lv.onLoad = function() {
			//document.write(lv.content);
			if(document.getElementById(aguarde)) document.getElementById(aguarde).style.display = "none";
			if(document.getElementById(confirma)) document.getElementById(confirma).style.display = "block";
			with(form) {
				for(i=0;i<elements.length;i++) {
					elements[i].value = "";
				}
			}

		}
		// enviando informacoes
		lv.send(action, "POST");
		// exibindo mensagem de aguarde
		if(document.getElementById(aguarde)) document.getElementById(aguarde).style.display = "block";
	}
}



function carrega_estado() {
	var lv = new LoadVars();

	lv.onLoad = function () {
		document.getElementById("listaestado").innerHTML = lv.content;
		document.getElementById("aguarde").style.display = "none";
	}

	lv.send("ajax.php?modelo=estado","POST");
	document.getElementById("aguarde").style.display = "block";
}

function carrega_cidade(estado) {
	var lv = new LoadVars();
	lv.estado = estado;
	lv.acao = "cidade";
	lv.onLoad = function () {
		document.getElementById("listacidade").innerHTML = lv.content;
	}

	lv.send("ajax.php?modelo=cidade_front","POST");
}

function carrega_resultado(estado,cidade) {
	
	var lv = new LoadVars();
	lv.estado 	= estado;
	lv.cidade 	= cidade;
	lv.acao 	= "result";
	lv.onLoad = function () {
		document.getElementById("result").innerHTML = lv.content;
	}

	lv.send("ajax.php?modelo=cidade_front","POST");
}

function carrega_bairro(cidade) {
	return void(0);
	/*
	var lv = new LoadVars();
	lv.cidade = cidade;

	lv.onLoad = function () {
	document.getElementById("listabairro").innerHTML = lv.content;
	document.getElementById("aguarde").style.display = "none";
	}

	lv.send("ajax.php?modelo=bairro","POST");
	document.getElementById("aguarde").style.display = "block";
	*/
}

function carrega_logradouro(bairro) {
	if(bairro == 0) {
		troca_campo('bairro',document.formulario.cidade.value,340);
	} else {
		var lv = new LoadVars();
		lv.bairro = bairro;

		lv.onLoad = function () {
			document.getElementById("listalogradouro").innerHTML = lv.content;
			document.getElementById("aguarde").style.display = "none";
		}

		lv.send("ajax.php?modelo=logradouro","POST");
		document.getElementById("aguarde").style.display = "block";
	}
}

function incrementaData(campo, data, tecla) {
	var lv = new LoadVars();
	lv.data = data;
	lv.tecla = tecla;

	lv.onLoad = function () {
		document.getElementById(campo).value = lv.content.trim();
	}

	if(tecla == 38 || tecla == 40 || tecla == 107 || tecla == 109) {
		lv.send("ajax.php?modelo=incrementa_data","POST");
	}
}

function incrementaCampo(campo, tecla) {
	switch(tecla) {
		case 37 :
		case 109 :
		campo.value = parseInt(campo.value) - 1;
		break;
		case 38 :
		campo.value = parseInt(campo.value) + 10;
		break;
		case 39 :
		case 107 :
		campo.value = parseInt(campo.value) + 1;
		break;
		case 40 :
		campo.value = parseInt(campo.value) - 10;
		break;
	}
	if(parseInt(campo.value) < 0) campo.value = 0;

}

/** **************************************************************************************************************************** **/

function carrega_lib(lib,tipo) {
	var lv = new LoadVars();
	lv.tipo = tipo;
	lv.pagina = 1;

	lv.onLoad = function () {
		document.getElementById("container").innerHTML = lv.content;
	}
	if(lib !== 0) {
		lv.send("ajax.php?modelo="+lib,"POST");

	}
}

function lib_img_troca_dado(arr) {
	dado = eval(arr);
	document.getElementById("imgdimens").innerHTML = dado[1]+" x "+dado[2];
	document.getElementById("imgpeso").innerHTML = dado[3]+"KB";
}

var radical = '';
var formato = '';
var pagat = 1;

function lib_img_busca(palavra,tipo) {
	var lv = new LoadVars();
	lv.busca = palavra;
	lv.formato = tipo;
	lv.acao = "busca";
	lv.pagina = 1;
	radical = palavra;
	formato = tipo;
	pagat = 1;

	lv.onLoad = function () {
		document.getElementById("listaimagem").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	if(palavra.length >= 2) {
		lv.send("ajax.php?modelo=imagem","POST");
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "block";
		}
	} else {
		alert('Você deve informar uma palavra-chave com mais de 2 caracteres.');
	}
}

function lib_lista_imagem(pagina) {
	var lv = new LoadVars();
	lv.busca = radical;
	lv.formato = formato;
	lv.acao = "busca";
	lv.pagina = pagina;
	pagat = pagina;

	lv.onLoad = function () {
		document.getElementById("listaimagem").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=imagem","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_img_carrega(id) {
	var lv = new LoadVars();
	lv.iid = id;
	lv.acao = "visualiza";

	lv.onLoad = function () {
		document.getElementById("container").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=imagem","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_img_retorna() {
	carrega_lib('lib_imagem','busca');
	setTimeout("lib_lista_imagem(pagat);",1000);
}

function lib_img_edita(id) {
	var lv = new LoadVars();
	lv.iid = id;
	lv.acao = "edita";

	lv.onLoad = function () {
		document.getElementById("container").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=imagem","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_img_remove(id) {
	var lv = new LoadVars();
	lv.iid = id;
	lv.acao = "remove";

	lv.onLoad = function () {
		//document.getElementById("container").innerHTML = lv.content;
		alert('A imagem foi removida com sucesso.');
		lib_img_retorna();

		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	if(confirm('Você confirma a remoção desta imagem?')) {
		lv.send("ajax.php?modelo=imagem","POST");
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "block";
		}
	}
}

function lib_noticia_busca(palavra) {
	var lv = new LoadVars();
	lv.busca = palavra;
	lv.tipo = "resultado";
	lv.pagina = 1;
	radical = palavra;
	pagat = 1;

	lv.onLoad = function () {
		document.getElementById("listanoticia").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	if(palavra.length >= 2) {
		lv.send("ajax.php?modelo=lib_noticia","POST");
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "block";
		}
	} else {
		alert('Você deve informar uma palavra-chave com mais de 2 caracteres.');
	}
}

function lib_lista_noticia(pagina) {
	var lv = new LoadVars();
	lv.busca = radical;
	lv.tipo = "resultado";
	lv.pagina = pagina;
	pagat = pagina;

	lv.onLoad = function () {
		document.getElementById("listanoticia").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_noticia","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_noticia_retorna() {
	carrega_lib('lib_noticia','busca');
	setTimeout("lib_lista_noticia(pagat);",1000);
}

function lib_noticia_carrega(id) {
	var lv = new LoadVars();
	lv.nid = id;
	lv.tipo = "visualiza";

	lv.onLoad = function () {
		document.getElementById("container").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_noticia","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_checar_imagem(id) {
	var lv = new LoadVars();
	lv.nid = id;
	lv.tipo = "checar";
	lv.cat = valida_categoria;

	lv.onLoad = function () {
		if(valida_categoria) {
			if(lv.content.trim() == 1) {
				document.formulariocad.foto.value = lv.nid;
			} else {
				alert("O padrão da imagem escolhido não é aceito para as imagens de notícia!");
			}
		} else {
			document.formulariocad.foto.value = lv.nid;
		}

		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_noticia","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}

}

function lib_atribuir_imagem(id,galeria) {
	switch(galeria) {
		case 1 : atribuir_galeria(id); break;
		default :
		lib_checar_imagem(id);
		break;
	}
}

function lib_copiar_imagem(id) {
	var lv = new LoadVars();
	lv.iid = id;
	lv.tipo = "copiar";

	lv.onLoad = function () {
		alert(lv.content.UNhtmlEntities());
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_imagem","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_galeria_busca(palavra) {
	var lv = new LoadVars();
	lv.busca = palavra;
	lv.tipo = "resultado";
	lv.pagina = 1;
	radical = palavra;
	pagat = 1;

	lv.onLoad = function () {
		document.getElementById("listagaleria").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	if(palavra.length >= 2) {
		lv.send("ajax.php?modelo=lib_galeria","POST");
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "block";
		}
	} else {
		alert('Você deve informar uma palavra-chave com mais de 2 caracteres.');
	}
}

function lib_lista_galeria(pagina) {
	var lv = new LoadVars();
	lv.busca = radical;
	lv.tipo = "resultado";
	lv.pagina = pagina;
	pagat = pagina;

	lv.onLoad = function () {
		document.getElementById("listagaleria").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_galeria","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_galeria_retorna() {
	carrega_lib('lib_galeria','busca');
	setTimeout("lib_lista_galeria(pagat);",1000);
}

function lib_galeria_carrega(id) {
	var lv = new LoadVars();
	lv.nid = id;
	lv.tipo = "visualiza";

	lv.onLoad = function () {
		document.getElementById("container").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_galeria","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}


function adiciona_link_galeria(id,chave) {
	if(valida_categoria == true) {
		document.formulariocad.galeria.value = 'AbrePagina(\'?target=galeria_frame&ch='+chave+'&gid='+id+'\', 650,550,\'no\');';
	} else {
		document.formulariocad.link.value = 'javascript:AbrePagina(\'?target=galeria_frame&ch='+chave+'&gid='+id+'\', 650,550,\'no\');';
	}
}

function salva_sessao() {

	var lv = new LoadVars();

	lv.onLoad = function () {
		if(lv.content.trim() == 1) {
			alert("Sessão expirada!\nPor favor efetue login novamente.");
			window.location = "index.php";
		}
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
		setTimeout("salva_sessao()",1000*60*10);

	}

	lv.send("ajax.php?modelo=sessao","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}

}


function lib_enquete_busca(palavra) {
	var lv = new LoadVars();
	lv.busca = palavra;
	lv.tipo = "resultado";
	lv.pagina = 1;
	radical = palavra;
	pagat = 1;

	lv.onLoad = function () {
		document.getElementById("listaenquete").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	if(palavra.length >= 2) {
		lv.send("ajax.php?modelo=lib_enquete","POST");
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "block";
		}
	} else {
		alert('Você deve informar uma palavra-chave com mais de 2 caracteres.');
	}
}

function lib_lista_enquete(pagina) {
	var lv = new LoadVars();
	lv.busca = radical;
	lv.tipo = "resultado";
	lv.pagina = pagina;
	pagat = pagina;

	lv.onLoad = function () {
		document.getElementById("listaenquete").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_enquete","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_enquete_retorna() {
	carrega_lib('lib_enquete','busca');
	setTimeout("lib_lista_enquete(pagat);",1000);
}

function lib_enquete_carrega(id) {
	var lv = new LoadVars();
	lv.nid = id;
	lv.tipo = "visualiza";

	lv.onLoad = function () {
		document.getElementById("container").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_enquete","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}


function adiciona_link_enquete(id,chave) {
	if(enquete_as_id == true) {
		document.formulariocad.enquete.value = id;
	} else {
		document.formulariocad.link.value = '?target=enquete&ch='+chave+'&gid='+id;
	}
}



function lib_coluna_busca(palavra) {
	var lv = new LoadVars();
	lv.busca = palavra;
	lv.tipo = "resultado";
	lv.pagina = 1;
	radical = palavra;
	pagat = 1;

	lv.onLoad = function () {
		document.getElementById("listacoluna").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	if(palavra.length >= 2) {
		lv.send("ajax.php?modelo=lib_coluna","POST");
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "block";
		}
	} else {
		alert('Você deve informar uma palavra-chave com mais de 2 caracteres.');
	}
}

function lib_lista_coluna(pagina) {
	var lv = new LoadVars();
	lv.busca = radical;
	lv.tipo = "resultado";
	lv.pagina = pagina;
	pagat = pagina;

	lv.onLoad = function () {
		document.getElementById("listacoluna").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_coluna","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_coluna_retorna() {
	carrega_lib('lib_coluna','busca');
	setTimeout("lib_lista_coluna(pagat);",1000);
}

function lib_coluna_carrega(id) {
	var lv = new LoadVars();
	lv.nid = id;
	lv.tipo = "visualiza";

	lv.onLoad = function () {
		document.getElementById("container").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_coluna","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}


function adiciona_link_coluna(id,chave) {
	document.formulariocad.link.value = '?target=coluna&ch='+chave+'&cid='+id;
}

function lib_ensaio_busca(palavra) {
	var lv = new LoadVars();
	lv.busca = palavra;
	lv.tipo = "resultado";
	lv.pagina = 1;
	radical = palavra;
	pagat = 1;

	lv.onLoad = function () {
		document.getElementById("listaensaio").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	if(palavra.length >= 2) {
		lv.send("ajax.php?modelo=lib_ensaio","POST");
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "block";
		}
	} else {
		alert('Você deve informar uma palavra-chave com mais de 2 caracteres.');
	}
}

function lib_lista_ensaio(pagina) {
	var lv = new LoadVars();
	lv.busca = radical;
	lv.tipo = "resultado";
	lv.pagina = pagina;
	pagat = pagina;

	lv.onLoad = function () {
		document.getElementById("listaensaio").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_ensaio","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_ensaio_retorna() {
	carrega_lib('lib_ensaio','busca');
	setTimeout("lib_lista_ensaio(pagat);",1000);
}

function lib_ensaio_carrega(id) {
	var lv = new LoadVars();
	lv.nid = id;
	lv.tipo = "visualiza";

	lv.onLoad = function () {
		document.getElementById("container").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_ensaio","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}


function adiciona_link_ensaio(id,chave) {
	document.formulariocad.link.value = '?target=ensaio_modelo&ch='+chave+'&eid='+id;
}




function lib_evento_busca(palavra) {
	var lv = new LoadVars();
	lv.busca = palavra;
	lv.tipo = "resultado";
	lv.pagina = 1;
	radical = palavra;
	pagat = 1;

	lv.onLoad = function () {
		document.getElementById("listaevento").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	if(palavra.length >= 2) {
		lv.send("ajax.php?modelo=lib_evento","POST");
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "block";
		}
	} else {
		alert('Você deve informar uma palavra-chave com mais de 2 caracteres.');
	}
}

function lib_lista_evento(pagina) {
	var lv = new LoadVars();
	lv.busca = radical;
	lv.tipo = "resultado";
	lv.pagina = pagina;
	pagat = pagina;

	lv.onLoad = function () {
		document.getElementById("listaevento").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_evento","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}

function lib_evento_retorna() {
	carrega_lib('lib_evento','busca');
	setTimeout("lib_lista_evento(pagat);",1000);
}

function lib_evento_carrega(id) {
	var lv = new LoadVars();
	lv.nid = id;
	lv.tipo = "visualiza";

	lv.onLoad = function () {
		document.getElementById("container").innerHTML = lv.content;
		if(document.getElementById("aguarde")) {
			document.getElementById("aguarde").style.display = "none";
		}
	}
	lv.send("ajax.php?modelo=lib_evento","POST");
	if(document.getElementById("aguarde")) {
		document.getElementById("aguarde").style.display = "block";
	}
}


function adiciona_link_evento(id,chave) {
	document.formulariocad.link.value = '?target=happyhour_agenda&eid='+id;
}


