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 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(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(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 hCad = function(t) {
	if(t.responseText=="ok")
	   	window.document.fDados.submit();
	else
		alert("Este e-mail já está cadastrado!");
}
var eCad = function(t) {return false;}
function cad(email,tabela) {
	var url			= "exec/verifica.php";
	new Ajax.Request(url, {method:'get', parameters:'email='+email+'&tabela='+tabela, onSuccess:hCad, onFailure:eCad});
}
function vfSinopse(campo,event,id) {
		maximo = campo.title;
		var texto = campo.value;
		var falta = maximo - texto.length;
		var aviso = falta;
		document.getElementById("vfContador").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("vfCaixaContador")) {
		document.getElementById("vfCaixaContador").focus();
		document.getElementById("vfCaixaContador").blur();
	}
}
function fCnpjCpf(campo) {
	if(!isCnpj(campo.value)) {
		if(!isCpf(campo.value)) {
			return false;
		}
	}
}
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(confirm("A imagem será excluída!\nVocê tem certeza?"))
		new Ajax.Request(url, {method:'get', parameters:'c='+c+'&i='+i, onSuccess:hImagem, onFailure:eImagem});
}
// 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);
	}
}
window.onload = function() {
	sinopse();	
	fck("wysiwyg");
	fck2("wysiwyg-01");
	fck2("wysiwyg-02");
	fck2("wysiwyg-03");
	fck2("wysiwyg-04");
	fck2("wysiwyg-05");
	fck2("wysiwyg-06");
	fck2("wysiwyg-07");
	fck2("wysiwyg-08");
	fck2("wysiwyg-09");
}
function fck(id) {
	if(window.document.getElementById(id)) {
		var oFCKeditor = new FCKeditor(id);
		oFCKeditor.ReplaceTextarea();
	}	
}
function fck2(id) {
	if(window.document.getElementById(id)) {
		var oFCKeditor = new FCKeditor2(id);
		oFCKeditor.ReplaceTextarea();
	}	
}
function carregaFlash(caminho,largura,altura) {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+largura+'" height="'+altura+'">');
	document.write('<param name="movie" value="'+caminho+'">');
	document.write('<param name="quality" value="high">');
	document.write('<param name="wmode" value="transparent">');
	document.write('<param name="menu" value="false">');
	document.write('<embed src="'+caminho+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+largura+'" height="'+altura+'"></embed>');
	document.write('</object>');
}