/*
 Last Update: 12/12/2007
      Author: Ricardo Monteiro <suporte@w2r.com.br>
*/

// Executa funções ao carregar a página
window.onload = function() {
	fck("wy");
	sinopse();
}

// Erro padrão do Ajax
var errorAjax = function(t) {return false;}

// Apenas números nos campos de formulário
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;
	}	
}

// Apenas TEL no campo
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;
	}	
}

// Redireciona o usuário para uma página
function ir(url) {
	window.location.href=url;	
}

// Troca TEXTAREA id WY com FckEditor
function fck(id) {
	if(window.document.getElementById(id)) {
		var oFCKeditor = new FCKeditor(id);
		oFCKeditor.ReplaceTextarea();
	}	
}

// Dá o foco no campo sinopse
function sinopse() {
	for(i=1;i<10;i++) {
		if(eval("document.getElementById('vfCaixaContador_"+i+"')")) {
			eval("document.getElementById('vfCaixaContador_"+i+"').focus();");
			eval("document.getElementById('vfCaixaContador_"+i+"').blur();");
		}
	}
}

// Restringe máximo de caracteres em campos sinopse
function vfSinopse(campo,event,id) {
		if(maximo=="") 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;
}

// Valida os formulários
function validaForm(f) {
	avisos = "";
	for(i=0;i<f.length;i++) {
		id = f.elements[i].id;
		name = f.elements[i].name;
		id = id.substr(0,4);
		if(id == "vTxt") fTexto(f.elements[i]);
		if(name.substr(0,3) == "dd_") fData(f,i);
		if(name.substr(0,3) == "em_") fEmail(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);
	} else {
		f.submit();
	}
}
// 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) {
	if(!checkEmail(campo.value))
		avisos = avisos + " - Preencha o campo "+campo.alt+" com um e-mail válido!\n";
}

// Valida e-mail válido
var illegalChars1= /\t[\(\)\<\>\,\;\:\\\/\"\[\]]/
var illegalChars2= /\W/;
var regExDigitos= /\D/;
var emailFilter=/^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/

function checkEmail(strng) {
	if(strng.match(illegalChars1)) {
		return false;
	} else if (!(emailFilter.test(strng))) { 
		return false;
	}
	return true;
}

// Valida login 
function checkLogin(strng) {
	if(strng.match(illegalChars2)) {
		return false;
	}
	return true;	
}

// Valida numeros
function checkNumbers(strng) {
	if(strng.match(regExDigitos)) {
		return false;
	}
	return true;	
}


// Trata datas -----/
var dtCh= "/";
var minYear=1900;
var maxYear=3000;

function fData(f,i) {
	// dd_campo
	if(f.elements[i].value>0&&f.elements[i+1].value>0&&f.elements[i+2].value>0)
		isDate(f.elements[i+1].value+'/'+f.elements[i].value+'/'+f.elements[i+2].value); // mm/dd/aaaa
}

	function isInteger(s) {
	var i; for (i = 0; i < s.length; i++){var c = s.charAt(i);if (((c < "0") || (c > "9"))) return false;}
	return true;}
	function stripCharsInBag(s, bag){ var i; var returnString = "";
	for (i = 0; i < s.length; i++){var c = s.charAt(i);if (bag.indexOf(c) == -1) returnString += c; }
	return returnString;}
	function daysInFebruary (year){return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );}
	function DaysArray(n){
	for (var i = 1; i <= n; i++) {this[i] = 31;if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}if (i==2) {this[i] = 29}} 
	return this;}
	
	function isDate(dtStr) {
		var daysInMonth = DaysArray(12)
		var pos1=dtStr.indexOf(dtCh)
		var pos2=dtStr.indexOf(dtCh,pos1+1)
		var strMonth=dtStr.substring(0,pos1)
		var strDay=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1)
		strYr=strYear
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYr)
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			avisos = avisos + " - Escolha um dia válido para a data\n";
			return false
		}
		return true;
	}

// Salva o registro -------/
function salva(st) {
	window.document.fDados.in_ativo.value=st;
	validaForm(window.document.fDados);
}
// Apaga o registro ------/
function apagar(id,mod) {
	if(confirm("O registro será excluído\nVocê tem certeza?")) {
		ir('exec.php?mod='+mod+'&e='+id);
	}
}

// Gera a lista de imagens do Repositorio
var hImagem = function(t) {window.document.getElementById("saidaMedia").innerHTML = t.responseText; return false; }
function manageFiles(mod,id,del) {
	var url	= "../../_ajax/repositorio.php";
	if(del!="")
		if(confirm("A imagem será excluída!\nVocê tem certeza?"));

	new Ajax.Request(url, {method:'post', postBody:'del='+del+'&id='+id+'&mod='+mod, onSuccess:hImagem, onFailure:errorAjax});
}

// Mostra e esconde objetos
function meDIV(id,st) {
	window.document.getElementById(id).style.visibility=st;
}

// Troca o stilo da página
function changeStyle(style) {
	i = document.styleSheets.length;
	while(i--) {
		if(document.styleSheets[i].title==style || document.styleSheets[i].title=="geral")
			document.styleSheets[i].disabled = false;
		else
			document.styleSheets[i].disabled = true;
	}
	var url	= "../../_ajax/styleadm.php";
	new Ajax.Request(url, {method:'post', postBody:'style='+style});
}

// Abre a janela de CROP
function crop(mod,campo,imagem,id,cps) {
	window.open('cropPop.php?m='+mod+'&c='+campo+'&i='+imagem+'&id='+id+'&cps='+cps,'popCrop','width=1,height=1');
}

// Verifica em qual país o usuário esta
function checkCountry(pais) {
	if(parseInt(pais)>0) {
		if(parseInt(pais)==28) {
			if(window.document.getElementById("us")) {
				window.document.getElementById("us").style.display = "block";
				window.document.getElementById("us").style.disabled = false;
			}
			if(window.document.getElementById("ut")) {
					window.document.getElementById("ut").style.display = "none";
					window.document.getElementById("ut").style.disabled = true;
			}
			if(window.document.getElementById("cs")) {
				window.document.getElementById("cs").style.display = "block";
				window.document.getElementById("cs").style.disabled = false;
			}
			if(window.document.getElementById("ct")) {
				window.document.getElementById("ct").style.display = "none";
				window.document.getElementById("ct").style.disabled = true;
			}			
		} else {
			if(window.document.getElementById("us")) {
				window.document.getElementById("us").style.display = "none";
				window.document.getElementById("us").style.disabled = true;
			}
			if(window.document.getElementById("ut")) {
				window.document.getElementById("ut").style.display = "block";
				window.document.getElementById("ut").style.disabled = false;
			}
			if(window.document.getElementById("cs")) {
				window.document.getElementById("cs").style.display = "none";
				window.document.getElementById("cs").style.disabled = true;
			}
			if(window.document.getElementById("ct")) {
				window.document.getElementById("ct").style.display = "block";
				window.document.getElementById("ct").style.disabled = false;
			}			
		}
	}	
}

// Verifica o estado (UF) do usuário
var hCidades = function(t) {window.document.getElementById("cidade").innerHTML = t.responseText; return false; }
function checkUF(estado,cidade) {
	var url	= "/feajr/_ajax/lista_cidades.php";
	new Ajax.Request(url, {method:'post', postBody:'uf='+estado+'&cidade='+cidade, onSuccess:hCidades, onFailure:errorAjax});
}

function admmenu(id) {
	if(window.document.getElementById(id).className=="menuOff") {
		window.document.getElementById(id).className="menuOn";
		window.document.getElementById("img"+id).src="/feajr/_img/sys/layout/icon_down.gif";
	} else {
		window.document.getElementById(id).className="menuOff";
		window.document.getElementById("img"+id).src="/feajr/_img/sys/layout/icon_up.gif";
	}
}