/*/////////////////////////////////////////////////////////////////////////////////
// Pega o objeto pelo ID (CrossBrowser)
/////////////////////////////////////////////////////////////////////////////////*/
function o(Objeto){
	return document.getElementById(Objeto);
}
/*/////////////////////////////////////////////////////////////////////////////////
//Abre uma janela no formato para vizualizar imagens
/////////////////////////////////////////////////////////////////////////////////*/
function JanelaImagem(Caminho, Width, Height){
	if(!Width)
		Width = 700;
	
	if(!Height)
		Height = 500;
	
	TopJanela = (screen.availHeight / 2) - (Height / 2);
	LeftJanela = (screen.availWidth / 2) - (Width / 2);
	
	try{
		Janela = window.open("Imagem.php?CAMINHO=" + Caminho,"Imagem","scrollbars=1,resizable=1,width=" + Width + ",height=" + Height + ",top=" + TopJanela + ",left=" + LeftJanela);
	}
	catch(e){
	}
	if(!Janela)
		alert("Erro ao abrir janela. Verifique seu bloqueador de pop-ups");
}

/*/////////////////////////////////////////////////////////////////////////////////
// Abre janelas centralizadas
/////////////////////////////////////////////////////////////////////////////////*/
function AbreJanela(Caminho, Nome, Largura, Altura, Opcoes){
	TopJanela = (screen.availHeight / 2) - (Altura / 2);
	LeftJanela = (screen.availWidth / 2) - (Largura / 2);

	
	if(Opcoes != "")
		Opcoes += ",";
		
	try{
		Janela = window.open(Caminho, Nome ,Opcoes + "width=" + Largura + ",height=" + Altura + ",top=" + TopJanela + ",left=" + LeftJanela);
	}
	catch(e){
	}
	if(!Janela)
		alert("Erro ao abrir janela. Verifique seu bloqueador de pop-ups");
}
/*/////////////////////////////////////////////////////////////////////////////////
// Muda o status de visualização de um bloco, tendo id's multiplos ou não
/////////////////////////////////////////////////////////////////////////////////*/
function AlternaStatus(Obj, Status){
    if(Status){
        if(!Obj.length){
		    Obj.style.display=Status;
	    }else{
		    for(i=0;i<=Obj.length-1;i++){
			    Obj[i].style.display=Status;
		    }
	    }
        return;
    }
	if(!Obj.length){
		if(Obj.style.display=="none"){
			Obj.style.display="";
		}else{
			Obj.style.display="none";
		}
	}else{
		for(i=0;i<=Obj.length-1;i++){
			if(Obj[i].style.display=="none"){
				Obj[i].style.display="";
			}else{
				Obj[i].style.display="none";
			}
		}
	}
}
/*/////////////////////////////////////////////////////////////////////////////////
// Muda o status de visualização de um bloco de seção, cujo os ids são múltiplos
/////////////////////////////////////////////////////////////////////////////////*/
function Secao(Tr){
	for(i=0;i<=Tr.length-1;i++){
		if(Tr[i].style.display=="none"){
			Tr[i].style.display="block";
		}else{
			Tr[i].style.display="none";
		}
	}
}
/*/////////////////////////////////////////////////////////////////////////////////
// Função padrão para ir ao formulário de edição
/////////////////////////////////////////////////////////////////////////////////*/
function Editar(Pagina, NomeID, ID){
	if(Pagina.indexOf("?")==-1){
		Pagina += "?";
	}else{
		Pagina += "&";
	}
	self.location.href = Pagina + NomeID + "=" + ID;
}
/*/////////////////////////////////////////////////////////////////////////////////
// Função padrão para executar exclusão
/////////////////////////////////////////////////////////////////////////////////*/
function Excluir(Pagina, NomeID, ID){
	//if(confirm(MsgExclusao)){
		if(Pagina.indexOf("?")==-1){
			Pagina += "?";
		}else{
			Pagina += "&";
		}
		self.location.href = Pagina + "E=1&" + NomeID + "=" + ID;
	//}
}
/*/////////////////////////////////////////////////////////////////////////////////
// Muda a classe do objeto para a classe passada
/////////////////////////////////////////////////////////////////////////////////*/
function MudaClasse(Obj,classe){
	Obj.className = classe;
}
/*/////////////////////////////////////////////////////////////////////////////////
// Coloca o foco automático na primeira caixa de texto que encontra na página
/////////////////////////////////////////////////////////////////////////////////*/
function Foco(){
	var para;
	try{
		if(document.forms.length>=1){
			for(cFrm=0;cFrm<=document.forms.length-1;cFrm++){
				for(cFocus=0;cFocus<=document.forms[cFrm].length-1;cFocus++){
					campo = document.forms[cFrm].elements[cFocus];
					if((campo.type=="select-one"||campo.type=="text")&&campo.disabled==false&&campo.name!="NEW_EMAIL"){
						campo.focus();
						if(campo.type=="text"){
							//campo.select();
						}
						para=1;
						break;
					}
				}
				if(para)break;
			}
		}
	}
	catch(e){
		// ignora erros de foco	
	}
}
setTimeout("Foco()",500)
/*/////////////////////////////////////////////////////////////////////////////////
// Valida um e-mail passado
//	|- email - o email que deverá ser validado
/////////////////////////////////////////////////////////////////////////////////*/
function  Validaemail(email){
	var regexp;
	regexp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	if (regexp.test(email)){
		return true;
	}else{
		return false;
	}
}
/*/////////////////////////////////////////////////////////////////////////////////
// Valida uma data passada
//	|- data - a data que deverá ser validada
/////////////////////////////////////////////////////////////////////////////////*/
function ValidaData(data){
	var regexp;
	regexp = /^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d{4}/;
	if (regexp.test(data)){
		return true;
	}else{
		return false;
	}
}
/*/////////////////////////////////////////////////////////////////////////////////
// Valida uma hora passada
//	|- hora - a hora que deverá ser validada
/////////////////////////////////////////////////////////////////////////////////*/
function ValidaHora(hora){
	var regexp;
	regexp = /(^\d{2}:\d{2})/;
	if (regexp.test(hora)){
		return true;
	}else{
		return false;
	}
}
/*/////////////////////////////////////////////////////////////////////////////////
// Valida um CEP passado
//	|- CEP - o CEP que deverá ser validado
/////////////////////////////////////////////////////////////////////////////////*/
function ValidaCEP(CEP){
	var regexp;
	regexp = /(^\d{5}-\d{3}$)|(^\d{8}$)/;
	if (regexp.test(CEP)){
		return true;
	}else{
		return false;
	}
}
/*/////////////////////////////////////////////////////////////////////////////////
// Mascara um campo em que o Data está sendo digitado
//	|- Data - o campo que deverá ser mascarado
/////////////////////////////////////////////////////////////////////////////////*/
function MascaraData(Data){
    if (Data.value.length == 2) {
        Data.value += '/';
    }
	
	if (Data.value.length == 5) {
        Data.value += '/';
    }
}
/*/////////////////////////////////////////////////////////////////////////////////
// Mascara um campo em que o CEP está sendo digitado
//	|- CEP - o campo que deverá ser mascarado
/////////////////////////////////////////////////////////////////////////////////*/
function MascaraCEP(CEP){
    if (CEP.value.length == 5) {
        CEP.value += '-';
    }
}
/*/////////////////////////////////////////////////////////////////////////////////
// Valida um CPF passado
//	|- CPF - o CPF que deverá ser validado
/////////////////////////////////////////////////////////////////////////////////*/
function ValidaCPF(CPF,silencioso) {
	valor = true;
	erro = new String;
	if (CPF.length < 11) erro += "São necessários 11 dígitos para verificação do CPF! \n\n"; 
	if ((CPF.charAt(3) != ".") || (CPF.charAt(7) != ".") || (CPF.charAt(11) != "-")){
		if (erro.length == 0) erro += "É necessários preencher corretamente o numero do CPF! \n\n";
	}
	CPF = CPF.replace(".","");
	CPF = CPF.replace(".","");
	CPF = CPF.replace("-","");
	var nonNumbers = /\D/;
	if (nonNumbers.test(CPF)) erro += "A verificação de CPF suporta apenas números! \n\n";	
	/*if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
		  erro += "Numero de CPF invalido!"
	}*/
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++){
		a[i] = CPF.charAt(i);
		if (i < 9) b += (a[i] *  --c);
	}
	if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	b = 0;
	c = 11;
	for (y=0; y<10; y++) b += (a[y] *  c--); 
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	if ((CPF.charAt(9) != a[9]) || (CPF.charAt(10) != a[10])){
		erro += "Dígito verificador com problema!";
	}
	if (erro.length > 0){
		if(!silencioso){
			alert(erro);
		}
		return false;
	}
	return true;
}
/*/////////////////////////////////////////////////////////////////////////////////
// Valida um CNPJ passado
//	|- CNPJ - o CNPJ que deverá ser validado
/////////////////////////////////////////////////////////////////////////////////*/
function ValidaCNPJ(CNPJ,silencioso) {
	erro = new String;
	if (CNPJ.length < 18) erro += "É necessários preencher corretamente o numero do CNPJ! \n\n";
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		if (erro.length == 0) erro += "É necessários preencher corretamente o numero do CNPJ! \n\n";
	}
	//substituir os caracteres que nao sao numeros
	if(document.layers && parseInt(navigator.appVersion) == 4){
		x = CNPJ.substring(0,2);
		x += CNPJ.substring(3,6);
		x += CNPJ.substring(7,10);
		x += CNPJ.substring(11,15);
		x += CNPJ.substring(16,18);
		CNPJ = x;	
	} else {
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace("-","");
		CNPJ = CNPJ.replace("/","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n";	
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++){
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	b = 0;
	for (y=0; y<13; y++) {
		b += (a[y] * c[y]); 
	}
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		erro += "Digito verificador com problema!";
	}
	if (erro.length > 0){
		if(!silencioso){
			alert(erro);
		}
		return false;
	}
	return true;
}
/*/////////////////////////////////////////////////////////////////////////////////
// Coloca o campo no formato CNPJ
/////////////////////////////////////////////////////////////////////////////////*/
function FormataCNPJ(Campo, Tecla, Proximo){
	var tecla = Tecla.keyCode;
	var vr = new String(Campo.value);
	
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1 ;
	
	if (tecla != 9 && tecla != 8 && tecla != 46){
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if (tam >= 13 && tam < 15)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
		if (vr.length == 14){
			if(Proximo){
				Proximo.focus();
			}
		}
	}
	
}

/*/////////////////////////////////////////////////////////////////////////////////
// Coloca o campo no formato CPF
/////////////////////////////////////////////////////////////////////////////////*/
function FormataCPF(Campo, Tecla, Proximo){
	var tecla = Tecla.keyCode;
	var vr=new String(Campo.value);
	
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");
	
	tam=vr.length;
	if (tecla != 9 && tecla != 8 && tecla != 46){
		if (tam > 3 && tam < 6){
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,tam);
		}
		if (tam >= 6 && tam < 9){
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		}
		if (tam >= 9 && tam < 11){
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
		}
		if (tam == 11){
			if(Proximo){
				Proximo.focus();
			}
		}
	}	

}
/*/////////////////////////////////////////////////////////////////////////////////
// Função que filtra a tecla, verificando se é numerica
/////////////////////////////////////////////////////////////////////////////////*/
function FiltraNumero(Campo,Tipo){
	if(Tipo==1){
		var n = new String("0123456789,.-");
	}else{
		var n = new String("0123456789");
	}
	var p = new String(Campo.value.substr(parseInt(Campo.value.length)-1,1));
	if(n.indexOf(p) == -1){
		Campo.value = Campo.value.substr(0,parseInt(Campo.value.length)-1);
	}	
}
/*/////////////////////////////////////////////////////////////////////////////////
// Função replace semelhante a do ASP, VB e etc...
/////////////////////////////////////////////////////////////////////////////////*/
function replace(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
/*/////////////////////////////////////////////////////////////////////////////////
// Formata um número para formato float
/////////////////////////////////////////////////////////////////////////////////*/
function CFloat(nro){
	return replace(replace(nro,".",""),",",".")
}
/*/////////////////////////////////////////////////////////////////////////////////
// Formata um número para formato monetário
/////////////////////////////////////////////////////////////////////////////////*/
function FormataMonetario(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+'.'+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + ',' + cents);
}
/*/////////////////////////////////////////////////////////////////////////////////
// Cria um objeto array com o máximo passado em N
/////////////////////////////////////////////////////////////////////////////////*/
function CriaArray (n){
	this.length = n 
}
/*/////////////////////////////////////////////////////////////////////////////////
// Gera a data baseada no computador local
/////////////////////////////////////////////////////////////////////////////////*/
function GeraData(){
	hoje = new Date();
	dia = hoje.getDate();
	dias = hoje.getDay();
	mes = hoje.getMonth();
	ano = hoje.getYear();
	if (dia < 10){
		dia = "0" + dia;
	}
	if(ano < 2000){
		ano = 1900 + ano;
	}else {
		ano = ano;
	}

	NomeDia = new CriaArray(7);
	NomeDia[0] = "Domingo";
	NomeDia[1] = "Lunes";
	NomeDia[2] = "Martes";
	NomeDia[3] = "Miércoles";
	NomeDia[4] = "Jueves";
	NomeDia[5] = "Sexta-feira";
	NomeDia[6] = "Sábado";
	//
	NomeMes = new CriaArray(12);
	NomeMes[0] = "Janeiro";
	NomeMes[1] = "Fevereiro";
	NomeMes[2] = "Março";
	NomeMes[3] = "Abril";
	NomeMes[4] = "Maio";
	NomeMes[5] = "Junho";
	NomeMes[6] = "Julho";
	NomeMes[7] = "Agosto";
	NomeMes[8] = "Setembro";
	NomeMes[9] = "Outubro";
	NomeMes[10] = "Novembro";
	NomeMes[11] = "Dezembro";
	if(document.getElementById("DivData")){
		document.getElementById("DivData").innerHTML = "<b>" + dia + " de " + NomeMes[mes] + "</b>";
	}
}
GeraData();