//Função para validação de e-mail
function validarMail(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
	return true;
	}
	else{ 
	return false;
	}
}

// função da div
function mostra_div_muda_figura(x,img_1,img_2,valor){
	obj = eval (x);
	atual = obj.style.display
	if (atual == 'block'){
		obj.style.display = 'none'		
		imagem = document.getElementById('imagem_'+valor);

		imagem.src = img_1;		
	}else{
		obj.style.display = 'block'
		imagem = document.getElementById('imagem_'+valor);
		imagem.src = img_2;	
	}
}

//função que seleiciona todos os itens
var checkflag = "false";
function check(field) {
	if (checkflag == "false") {
		for (i = 0; i < field.length; i++) {
			field[i].checked = true;
		}
		checkflag = "true";
		return "Uncheck All"; 
	}else{
		for (i = 0; i < field.length; i++) {
			field[i].checked = false; 
		}
		checkflag = "false";
		return "Check All"; 
	}		
}	

//
function navegacao(lugar){
	if(lugar=='objetos_zona'){
			window.location.href='objetos_zona.cfm';
		}
}
	
function Passa_valor_combo(obj1, obj2){
	var indice = obj1.selectedIndex;
	if (indice != -1){
		var novaOpcao = document.createElement("OPTION");
		novaOpcao.text = obj1[indice].text;
		novaOpcao.value = obj1[indice].value;
		obj2.add(novaOpcao);	  
		obj1.remove(indice);
	}else{
		alert("Selecione pelo menos um pólo para realizar esta operação!");
	}
}  	

// ABRE UMA NOVA TELA TIPO POP UP.
function Abre_pop_up(theURL, winName){
	window.open(theURL, winName);
} 
function PopUp(theURL, winName, features) {
	window.open(theURL, winName, features);
} 
function Abre_pop_up(theURL){
	window.open(theURL);
} 


function Mascara_cnpj(objeto){
	var cnpj = objeto;
	switch (cnpj.value.length){
		case 2:
			cnpj.value += ".";
			break;
		case 6:
			cnpj.value += ".";
			break;
		case 10:
			cnpj.value += "/";
			break;
		case 15:
			cnpj.value += "-";
			break;
	}
}
function numero_258(pontos){
	alert("teste");
}
function numero_3(pontos){
 switch (pontos.value.length){
	case 3:
		pontos.value += ",";
	break;  
 }
}

function numero_2(pontos){
 switch (pontos.value.length){
	case 2:
		 pontos.value += ".";
	break;  
 }
}
// valida todos os campos do formulário
function ValidaTodos(){
	var campo = document.forms[0];
	var i=0;
	for (i=0; i<campo.elements.length; i++){
		var valida = campo.elements[i].value;
		if (valida == ''){
			alert('o campo ' +  campo.elements[i].title + ' deve ser preenchido!');
			return;
		}
	}
}
//Funcão que faz a pergunta de confirmação de exclusão do registro
function confirma_exclusao(texto,pagina){
	if(window.confirm('Tem certeza que deseja excluir '+texto+'?')){
		location.href=pagina;
		resultado = false;			
	}
}
// Janela animada do Help Online
function janelanimada(endereco){
	var velocidadev = 5;
	var topo = 30;
	var esquerda = 150;
	var velocidadeh = 8;
	if (document.all){
		var tamlar = window.screen.availHeight;
		var tamjan = window.screen.availWidth;
		var tamanho = window.open("","","left=" + esquerda + ",top=" + topo + ",width=1,height=1,scrollbars=yes");
		for(sizeheight = 1; sizeheight < tamlar; sizeheight += velocidadev){
			tamanho.resizeTo("1",sizeheight );
		}
		for (sizewidth = 1; sizewidth < tamjan; sizewidth += velocidadeh){
			tamanho.resizeTo(500,500 );
		}
		tamanho.location = endereco;
	}
	else
	window.location = endereco;
}
//caixa grande para selecionar vários itens ao mesmo tempo
function addOption(obj, value, text, selected){
 elem = document.createElement('OPTION');
 elem.value = value;
 elem.text = text;
 elem.selected = selected;
 obj.add(elem);
}
function MoveTo(obj1, obj2){	 		
	i = 0;
	while(i < obj1.length){
		if(obj1[i].selected){
			addOption(obj2, obj1[i].value, obj1[i].text, false);
			obj1.remove(i);
		}
		else
			i++;
	}
}  

//Verifica data válida
function Critica_Data(data){
	var dia = parseInt(data.substring(0,2),10);
	var mes = parseInt(data.substring(3,5),10);
	var ano = parseInt(data.substring(6,10),10);
	var now = new Date();
	if(data.length == 10){
		if ( ((dia > 0) && (dia <= 31))  && ((mes > 0) && (mes <= 12)) && ((ano >=1900) && (ano <= ((now.getYear())+20)))){ //Máximo de vigência é de 20 anos
			if (((mes==2) && (dia>28)) && ((ano%4)!=0)){
				return false;
			}
			if ((mes==4) && (dia>30)){
				return false;
			}
			if ((mes==6) && (dia>30)){
				return false;
			}
			if ((mes==9) && (dia>30)){
				return(false);
			}
			if ((mes==11) && (dia>30)){
				return false;
			}
		}else{
		return false;
		}
	}else{
		if ((data.length != 10))
		return false;
	}
	return true;
}
//Verifica HORA válida
function Critica_Hora(hora){
	var h1 = parseInt(hora.substring(0,1),10);
	var h2 = parseInt(hora.substring(1,2),10);
	var m1 = parseInt(hora.substring(3,4),10);
	var m2 = parseInt(hora.substring(4,5),10);
	if(hora.length == 5){
		if (h1 > 2)
		return false;		
		if ((h1 == 2) && (h2 >= 4))
		return false;
		if (m1 > 5)
		return false;
	}
	if(hora.length != 5){
		return false;
	}
	return true;
}
//
function Mascara_Data(data){
	switch (data.value.length){
		case 2:
			data.value += "/";
			break;
		case 5:
			data.value += "/";
			break;
	}
}

function Mascara_Hora(hora){
	switch (hora.value.length){
		case 2:
			hora.value += ":";
			break;
	}
}
function bloqueia_nao_numerico(form){
	if(event.keyCode < 48 || event.keyCode > 57){
		event.keyCode = 0;
		return false;
	}else{
		return true;
	}
}
function aceita_numero_ponto_virgula(form){
	if((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode != 46) && (event.keyCode != 44)){
		event.keyCode = 0;
		return false;
	}else{
		return true;
	}
}
function verifica_branco(parametro){
	teste_parametro = "false";
	tamanho_parametro = parametro.length;
	if (tamanho_parametro != 0){
		for (i = 0; i < tamanho_parametro; i++){
			if (parametro.charAt(i) != " "){
				teste_parametro = "true";
			}
		}
		if (teste_parametro == "false"){
			return false;
		}else{
			return true;
		}
	}else{
		return false;
	}
}
//LIMITAR TAMANHO DO TEXTAREA
function Tamanho_texto(campo, maxlimit){
	if (campo.value.length > maxlimit){
	  campo.value = campo.value.substring(0, maxlimit);
    return false;
  }	
}
function mostra_oculta(x,y){
	if (x == 'FUN'){		
		mostra_div(y);
	}
	if (x != 'FUN'){		
		fecha_abertos(y);
	}
}
//mostra a div selecionada porém não fecha as demais.
function mostraSub(x){
	obj = eval (x);
	atual = obj.style.display;
	if (atual == 'block'){
		obj.style.display = 'none'
	}else{
		obj.style.display = 'block'
	}
}
//fecha todas as divs abertas
function FTodasDivs(acao){
	var division;
 	division = document.all.tags("div");
	for (i = 0; i <= document.all.tags("div").length - 1; i++){
		division[i].style.display = acao;
	}
}
//Deixa apenas a div selecionada aberta, fechando as outras.
function mostra_uma_div(x){
	obj = eval (x);	
	atual = obj.style.display
	if (atual == 'block'){
		obj.style.display = 'block'
	}else{
		FTodasDivs('none');
		obj.style.display = 'block'
	}
}
//abre div
function mostra_div(x){	
	obj = eval (x);	
	obj.style.display = 'block'
}
//fecha div
function fecha_abertos(aberto){
	atual = eval (aberto);
	atual.style.display = 'none'
}
function SaltaCampo(campo, prox, tammax, teclapres){
	var tecla = teclapres.keyCode;
	vr = campo.value;
	tam = vr.length;
	if (tecla != 0 && tecla != 10 && tecla != 24 && tecla!=8 && tecla!=46 && tecla!=39 && tecla!=37 && tecla!=35 && tecla!=36){
		if (tam == tammax){
		prox.focus();
		}
	}
}
function AbrirFecharTodas(acao){
	var division;
 	division = document.all.tags("div");
	for (i = 0; i <= document.all.tags("div").length - 1; i++){
		division[i].style.display = acao;
	}
}
function aceita_letras(form){
	if((event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 96 && event.keyCode < 123)){
		return true;
	}else{
		event.keyCode = 0;
		return false;
	}
}
function Mascara_Matricula(mat){
	switch (mat.value.length){
		case 1:
			mat.value += ".";
			break;
		case 5:
			mat.value += ".";
			break;
		case 9:
			mat.value += "-";
			break;
	}
}
function Mascara_cpf(cpf){
	switch (cpf.value.length){
		case 3:
			cpf.value += ".";
			break;
		case 7:
			cpf.value += ".";
			break;
		case 11:
			cpf.value += "-";
			break;
	}
}
function Valida_Cpf(obj){
	if(obj.value.length>0){   
		obj = eval(obj);
		str1= obj.value;
		str2=obj.value;
		vazio= "";
		number=0;
		for(i=0;i<14;i++){
			str1 = str1.replace( ".", "" );
			str1 = str1.replace( ".", "" );
			str1 = str1.replace( "-", "" );
		}
			var i =  0;
			var tamanho = (str1.length-1)-11;
			var restante = "";
			if(tamanho < 0)
				tamanho = tamanho*-1;
		if(tamanho!=13){
			for(i=0;i<tamanho;i++)
					restante =  restante + "0";
			str1 = restante + str1;
		}
		obj.value = str1.substring(1,str1.length);		
			if (vazio.indexOf(str1.substr(str1.length-1,1))==-1)
				number=str1.substr(1,str1.length);
		if ((str1.length > 0 && str1.length < 11)){
				alert("O CPF de número "+str2+" é inválido! Favor digite um número de CPF válido." ); 
				obj.value=""; 
				obj.focus();
		}
		soma=0;
			if(number.length == 11){
				if (str1 == "000000000000" || str1 == "011111111111" || str1 == "022222222222" || str1 == "033333333333" || str1 == "044444444444" || str1 == "055555555555" || str1 == "066666666666" || str1 == "077777777777" ||	str1 == "088888888888" || str1 == "099999999999"){
					alert("O CPF de número "+str2+" é inválido! Favor digite um número de CPF válido." ); 
					obj.value=""; 
					obj.focus();
					return false;  
				}
				for ( i=0; i < 9; i++)   
					soma += (10 - i) * (number.charAt(i));	  
					soma = 11 - (soma % 11);
					if (soma > 9) soma = 0;
						if (soma == (number.charAt(9) )){
							soma = 0;
								for (i=0; i < 10; i++)
									soma += (11 - i) * (number.charAt(i) );
									soma = 11 - (soma % 11);
								if (soma > 9) soma = 0;
									if (soma == (number.charAt(10))){
										//obj.value = str2;
										return true;
									}
					}
				alert("O CPF de número "+str2+" é inválido! Favor digite um número de CPF válido." ); 
				obj.value=""; 
				obj.focus();
				return false;				
			}
	}
}	
function Valida_cnpj(campo){
	//Valida CNPJ	
	var i, j, k, Soma;	
	campo = campo; 
	campo = campo.replace( ".", "" );
	campo = campo.replace( ".", "" );
	campo = campo.replace( "/", "" );
	campo = campo.replace( "-", "" );
	//Verifica se o Tamanho do cnpj informado é diferente de 14.
	if (campo.length != 14 && campo.length>0){
		alert('CNPJ Inválido !');
		return false;
	}
	k = 0;
	while (k <= 1){
		Soma = 0;
		j = 5 + k;
		for (i=0; i <= 11+k; i++){
			Soma += parseInt(campo.charAt(i),10)*j;
			if ( j != 2)
				j--;
			else
				j = 9;
		}
		Soma = 11 - Soma%11;
		if(Soma >= 10)
			Soma = 0;
		if( Soma != parseInt(campo.charAt(12+k)) && campo.length>0){
			alert('CNPJ Inválido !');
			return false;
		}
		k++;
	}
	return true;
}
function Valida_Cpf_Mascara(obj){
	if(obj.value.length>0){   
		obj = eval(obj);
		str1= obj.value;
		str2=obj.value;
		vazio= "";
		number=0;
		for(i=0;i<14;i++){
			str1 = str1.replace( ".", "" );
			str1 = str1.replace( ".", "" );
			str1 = str1.replace( "-", "" );
		}
			var i =  0;
			var tamanho = (str1.length-1)-11;
			var restante = "";
			if(tamanho < 0)
				tamanho = tamanho*-1;
		if(tamanho!=13){
			for(i=0;i<tamanho;i++)
					restante =  restante + "0";
			str1 = restante + str1;
		}
		obj.value = str1.substring(1,str1.length);		
			if (vazio.indexOf(str1.substr(str1.length-1,1))==-1)
				number=str1.substr(1,str1.length);
		if ((str1.length > 0 && str1.length < 11)){
				alert("O CPF de número "+str2+" é inválido! Favor digite um número de CPF válido." ); 
				obj.value=""; 
				obj.focus();
		}
		soma=0;
			if(number.length == 11){
				if (str1 == "000000000000" || str1 == "011111111111" || str1 == "022222222222" || str1 == "033333333333" || str1 == "044444444444" || str1 == "055555555555" || str1 == "066666666666" || str1 == "077777777777" ||	str1 == "088888888888" || str1 == "099999999999"){
					alert("O CPF de número "+str2+" é inválido! Favor digite um número de CPF válido." ); 
					obj.value=""; 
					obj.focus();
					return false;  
				}
				for ( i=0; i < 9; i++)   
					soma += (10 - i) * (number.charAt(i));	  
					soma = 11 - (soma % 11);
					if (soma > 9) soma = 0;
						if (soma == (number.charAt(9) )){
							soma = 0;
								for (i=0; i < 10; i++)
									soma += (11 - i) * (number.charAt(i) );
									soma = 11 - (soma % 11);
								if (soma > 9) soma = 0;
									if (soma == (number.charAt(10))){
										obj.value = str2;
										return true;
									}
					}
				alert("O CPF de número "+str2+" é inválido! Favor digite um número de CPF válido." ); 
				obj.value=""; 
				obj.focus();
				return false;				
			}
	}
}	

// No form onKeyUp="Mascara_volume(this,9,event);"
function Mascara_volume(cur,len,ok){
	n='__0123456789';
  d=cur.value;
  l=d.length;
  r='';
  if (l > 0){
		z=d.substr(0,l-1);
		s='';
		a=2;
		for (i=0; i < l; i++){
			c=d.charAt(i);
			if (n.indexOf(c) > a){
				a=1;
				s+=c;
			};
		};
		l=s.length;
		t=len-1;
		if (l > t){
			l=t;
			s=s.substr(0,t);
		};
		if (l > 2){
			if(s.length > 5)
				r=s.substr(0,l-2)+','+s.substr(l-2,2);
			else
				r=s.substr(0,l-2)+'.'+s.substr(l-2,2);
		}
	else{
		if (l == 2){
			r='0.'+s;
		}
		else{
			if (l == 1){
				r='0.0'+s;
			};
		};
	};
	if (r == ''){
		r='0.00';
	}
	else{
		l=r.length;
		if (l > 6){
			j=l%3;
			w=r.substr(0,j);
			wa=r.substr(j,l-j-6);
			wb=r.substr(l-6,6);
			if (j > 0){
				w+='.';
			};
			k=(l-j)/3-2;
			for (i=0; i < k; i++){
				w+=wa.substr(i*3,3)+'.';
			};
			r=w+wb;
		};
	};
 };
 if (r.length <= len){
	cur.value=r;
 }
 else{
	cur.value=z;
 };
 return 'ok';
};
