function validaForm(f) {
	avisos = "";
	for(i=0;i<f.length;i++) {
		id = f.elements[i].id;
		id = id.substr(0,4);
		if(id == "vTxt") fTexto(f.elements[i]);
		if(id == "vEm0") fEmail(f.elements[i],false);
		if(id == "vEm1") fEmail(f.elements[i],true);
		/*
		if(id == "vfSelect") fSelect(f.elements[i]);
		if(id == "vfEmail") fEmail(f.elements[i]);
		if(id == "vfNumero") fNumero(f.elements[i]);
		if(id == "vfMoeda") fMoeda(f.elements[i]);
		if(id == "vfData") fData(f.elements[i],1);
		if(id == "vfDataN") fData(f.elements[i],0);
		if(id == "vfCnpjCpf") fCnpjCpf(f.elements[i]);
		if(id == "vfUnico") fUnico(f.elements[i],i);
		if(id == "vfUnicoEmail") fUnicoEmail(f.elements[i],i);
		*/
	}
	if(avisos!="") {
		alert("Verifique o preenchimento do formulário\n\n"+avisos);
		return false;
	}
	return true;
}
// Trata campos texto obrigatórios ----/
function fTexto(campo) {
	if(campo.type=="select-one") {
		if(campo.selectedIndex==0) {
			cp = campo.title;
			avisos = avisos + " - O campo "+cp+" é obrigatório!\n";
		}
	} else {
		if(campo.value.length < 3) {
			if(campo.alt)
				cp = campo.alt;
			else
				cp = campo.title;
			avisos = avisos + " - O campo "+cp+" é obrigatório!\n";
		}
	}
}
// Trata o e-mail válido ----/
function fEmail(campo,obrigatorio) {
	if(obrigatorio || campo.value!="") {
		rexp = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
		if(!rexp.test(campo.value)) {
			avisos = avisos + " - Preencha o campo "+campo.alt+" com um e-mail válido!\n";
		}
	}
}
function tvHome(obj,id,legenda,url,target,pos) {
	for (var i = 0; i < document.getElementsByClassName("link_img_02").length; i++) {
		document.getElementsByClassName("link_img_02")[i].className = "link_img_01";
	}
	pos = pos + 1;
	obj.className = "link_img_02";
	window.document.getElementById("tvHomeImagem").src = "media/home/img_"+id+".jpg";
	window.document.getElementById("tvHomeLegenda").innerHTML = legenda;
	window.document.getElementById("tvHomeUrl").href = url;
	window.document.getElementById("tvHomeUrl").target = target;
	window.clearTimeout(temp);
	temp = window.setTimeout("show("+pos+")",10000);
}
document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
}
function soNumeros(e) {
	if(window.event) { // IE
		tecla = e.keyCode
	} else if(e.which) { // Netscape/Firefox/Opera
		tecla = e.which
	}
	if((tecla < 48 && tecla != 8)||(tecla > 57)) {
		 	return false;
	}	
}
function mskData(e,f) {
	if(window.event) { // IE
		tecla = e.keyCode
	} else if(e.which) { // Netscape/Firefox/Opera
		tecla = e.which
	}
	if(tecla!=8) {
	if(f.value.length==2) f.value = f.value + "/";
	if(f.value.length==5) f.value = f.value + "/";
	}
	if((tecla < 48 && tecla != 8)||(tecla > 57)) {
		 	return false;
	}	
}
function mskTEL(e,f) {
	if(window.event) { // IE
		tecla = e.keyCode
	} else if(e.which) { // Netscape/Firefox/Opera
		tecla = e.which
	}
	if(tecla!=8) {
		if(f.value.length==0) f.value = f.value + "(";
		if(f.value.length==3) f.value = f.value + ") ";
		if(f.value.length==9) f.value = f.value + "-";
	}
	if((tecla < 48 && tecla != 8)||(tecla > 57)) {
		 	return false;
	}	
}
var hGal = function(t) { window.document.getElementById("gal_grande").innerHTML = t.responseText;}
var eGal = function(t) {return false;}
function imgGaleria(id) {
	window.document.getElementById("imgGaleria").src = "_images/galeria_carregando.gif";
	var url			= "ajax/galeria.php";
	new Ajax.Request(url, {method:'get', parameters:'id='+id, onSuccess:hGal, onFailure:eGal});
}

var hCad = function(t) {
	if(t.responseText=="ok")
	   	window.document.fDados.submit();
	else
		alert("Este e-mail já está cadastrado!");
}
var hCad2 = function(t) {
	if(t.responseText=="ok") {
	   	cad(t.responseText);
	} else {
		if(t.responseText==1)
			alert("O e-mail primário fornecido já esta cadastrado");
		if(t.responseText==2)
			alert("O e-mail secundário fornecido já esta cadastrado");
		if(t.responseText==3)
			alert("Os e-mails primário e secundário fornecidos já estão cadastrados");
	}
}
var eCad = function(t) {return false;}
function cad(email) {
	var url			= "ajax/cadastro.php";
	new Ajax.Request(url, {method:'get', parameters:'email='+email, onSuccess:hCad, onFailure:eCad});
}
function cad_contato(priemail,pridb,secemail,secdb) {
	var url			= "ajax/cadastro_alt.php";
	new Ajax.Request(url, {method:'get', parameters:'priemail='+priemail+'&pridb='+pridb+'&secemail='+secemail+'&secdb='+secdb, onSuccess:hCad2, onFailure:eCad});
}
var hImagem = function(t) {window.document.getElementById("resp_imagens").innerHTML = t.responseText;}
var eImagem = function(t) {return false;}
function excluiImg(c,i) {
	var url	= "../../ajax/admImgExclui.php";
	if(c!="") {
	if(confirm("O arquivo será excluído!\nVocê tem certeza?"))
		new Ajax.Request(url, {method:'get', parameters:'i='+i+'&c='+c, onSuccess:hImagem, onFailure:eImagem});
	} else {
		new Ajax.Request(url, {method:'get', parameters:'i='+i+'&c='+c, onSuccess:hImagem, onFailure:eImagem});
	}
}
function admmenu(id) {
	if(window.document.getElementById(id).className=="adm_menu_off") {
		window.document.getElementById(id).className="adm_menu_on";
		window.document.getElementById("img"+id).src="../../media/icons/minus.gif";
	} else {
		window.document.getElementById(id).className="adm_menu_off";
		window.document.getElementById("img"+id).src="../../media/icons/plus.gif";
	}
}
function enviaIndica() {
	window.open('incs/indicar.php','popIndica','width=460,height=200');
	void(0)
}
function rascunho() {
	window.document.fDados.in_ativo.value=0;
	if(window.document.fDados.tx_titulo.value=="")
		validaForm(window.document.fDados);
	else
		window.document.fDados.submit();
}
function publicar() {
	window.document.fDados.in_ativo.value=1;
	if(window.document.fDados.tx_titulo.value=="")
		validaForm(window.document.fDados);
	else
		window.document.fDados.submit();
}
function aprovar() {
	window.document.fDados.st_cadastro.value=1;
	window.document.fDados.submit();
}
function reprovar() {
	window.document.fDados.st_cadastro.value=2;
	window.document.fDados.submit();
}

function vfSinopse(campo,event,id) {
		maximo = campo.title;
		var texto = campo.value;
		var falta = maximo - texto.length;
		var aviso = falta;
		document.getElementById(id).innerHTML = aviso;
		if(falta<0) 
			campo.value = campo.value.substr(0,maximo);
		if(falta>0)
			return true;
		else
			if(event.keyCode==46 || event.keyCode == 8)
				return true;
			else
				return false;
}

function sinopse() {
	if(document.getElementById("vfCaixaContador01")) {
		document.getElementById("vfCaixaContador01").focus();
		document.getElementById("vfCaixaContador01").blur();
	}
	if(document.getElementById("vfCaixaContador02")) {
		document.getElementById("vfCaixaContador02").focus();
		document.getElementById("vfCaixaContador02").blur();
	}
	if(document.getElementById("vfCaixaContador03")) {
		document.getElementById("vfCaixaContador03").focus();
		document.getElementById("vfCaixaContador03").blur();
	}
	if(document.getElementById("vfCaixaContador04")) {
		document.getElementById("vfCaixaContador04").focus();
		document.getElementById("vfCaixaContador04").blur();
	}
}
window.onload = function() {
	fcksite("wysiwyg");
}
function fcksite(id) {
	if(window.document.getElementById(id)) {
		var oFCKeditor = new FCKeditor(id);
		oFCKeditor.ReplaceTextarea();
	}	
}

function fCnpjCpf(campo) {
	if(!isCnpj(campo.value)) {
		if(!isCpf(campo.value)) {
			return false;
		}
	}
}

// Trata campos CPF ou CNPJ
var NUM_DIGITOS_CPF  = 11;
var NUM_DIGITOS_CNPJ = 14;
var NUM_DGT_CNPJ_BASE = 8;

String.prototype.lpad = function(pSize, pCharPad)
{
	var str = this;
	var dif = pSize - str.length;
	var ch = String(pCharPad).charAt(0);
	for (; dif>0; dif--) str = ch + str;
	return (str);
} //String.lpad

String.prototype.trim = function()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
} //String.trim

function unformatNumber(pNum)
{
	return String(pNum).replace(/\D/g, "").replace(/^0+/, "");
} //unformatNumber

function formatCpfCnpj(pCpfCnpj, pUseSepar, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	if (pUseSepar==null) pUseSepar = true;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var numero = unformatNumber(pCpfCnpj);

	numero = numero.lpad(maxDigitos, '0');
	if (!pUseSepar) return numero;

	if (pIsCnpj)
	{
		reCnpj = /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;
		numero = numero.replace(reCnpj, "$1.$2.$3/$4-$5");
	}
	else
	{
		reCpf  = /(\d{3})(\d{3})(\d{3})(\d{2})$/;
		numero = numero.replace(reCpf, "$1.$2.$3-$4");
	}
	return numero;
} //formatCpfCnpj

function dvCpfCnpj(pEfetivo, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	var i, j, k, soma, dv;
	var cicloPeso = pIsCnpj? NUM_DGT_CNPJ_BASE: NUM_DIGITOS_CPF;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var calculado = formatCpfCnpj(pEfetivo, false, pIsCnpj);
	calculado = calculado.substring(2, maxDigitos);
	var result = "";

	for (j = 1; j <= 2; j++)
	{
		k = 2;
		soma = 0;
		for (i = calculado.length-1; i >= 0; i--)
		{
			soma += (calculado.charAt(i) - '0') * k;
			k = (k-1) % cicloPeso + 2;
		}
		dv = 11 - soma % 11;
		if (dv > 9) dv = 0;
		calculado += dv;
		result += dv
	}

	return result;
} //dvCpfCnpj

function isCpf(pCpf)
{
	//if(pCpf=="") return false;
	var numero = formatCpfCnpj(pCpf, false, false);
	var base = numero.substring(0, numero.length - 2);
	var digitos = dvCpfCnpj(base, false);
	var algUnico, i;

	// Valida dígitos verificadores
	if (numero != base + digitos) return false;

	/* Não serão considerados válidos os seguintes CPF:
	 * 000.000.000-00, 111.111.111-11, 222.222.222-22, 333.333.333-33, 444.444.444-44,
	 * 555.555.555-55, 666.666.666-66, 777.777.777-77, 888.888.888-88, 999.999.999-99.
	 */
	algUnico = true;
	for (i=1; i<NUM_DIGITOS_CPF; i++)
	{
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	return (!algUnico);
} //isCpf

function isCnpj(pCnpj)
{
	if(pCnpj=="") return false;
	var numero = formatCpfCnpj(pCnpj, false, true);
	var base = numero.substring(0, NUM_DGT_CNPJ_BASE);
	var ordem = numero.substring(NUM_DGT_CNPJ_BASE, 12);
	var digitos = dvCpfCnpj(base + ordem, true);
	var algUnico;

	// Valida dígitos verificadores
	if (numero != base + ordem + digitos) return false;

	/* Não serão considerados válidos os CNPJ com os seguintes números BÁSICOS:
	 * 11.111.111, 22.222.222, 33.333.333, 44.444.444, 55.555.555,
	 * 66.666.666, 77.777.777, 88.888.888, 99.999.999.
	 */
	algUnico = numero.charAt(0) != '0';
	for (i=1; i<NUM_DGT_CNPJ_BASE; i++)
	{
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	if (algUnico) return false;

	/* Não será considerado válido CNPJ com número de ORDEM igual a 0000.
	 * Não será considerado válido CNPJ com número de ORDEM maior do que 0300
	 * e com as três primeiras posições do número BÁSICO com 000 (zeros).
	 * Esta crítica não será feita quando o no BÁSICO do CNPJ for igual a 00.000.000.
	 */
	if (ordem == "0000") return false;
	return (base == "00000000"
		|| parseInt(ordem, 10) <= 300 || base.substring(0, 3) != "000");
} //isCnpj
function admmenu(id) {
	if(window.document.getElementById(id).className=="adm_menu_off") {
		window.document.getElementById(id).className="adm_menu_on";
		window.document.getElementById("img"+id).src="../../media/icons/minus.gif";
	} else {
		window.document.getElementById(id).className="adm_menu_off";
		window.document.getElementById("img"+id).src="../../media/icons/plus.gif";
	}
}
function rascunho() {
	window.document.fDados.in_ativo.value=0;
	if(window.document.fDados.tx_titulo.value=="")
		validaForm(window.document.fDados);
	else
		window.document.fDados.submit();
}
function publicar() {
	window.document.fDados.in_ativo.value=1;
	if(window.document.fDados.tx_titulo.value=="")
		validaForm(window.document.fDados);
	else
		window.document.fDados.submit();
}
function ir(url) {
	window.location.href=url;	
}
function voltar() {
	window.history.back();
}
function apagar(id) {
	if(confirm("O registro será excluído\nVocê tem certeza?")) {
		ir('exec.php?e='+id);
	}
}
function abre(url) {
	window.open(url,'pop','width=1,height=1');
	void(0);
}