﻿//Funções de validação de e-mail
ValidaEmail = function(Email) {
	//var RegEx = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
	//var RegEx = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/;
	return ExpressaoRegular(Email, RegEx);
}

//Recebe arqumentos.valor, arqumentos.expressao para validar
ExpressaoRegular = function(Valor, Expressao) {
	if (Valor != '') {
		 return Expressao.test(Valor);
	} else return true;	
}

function ValidaCPF(VPCPF)
{ // F01
  
/* -------------------- Declaração de Variáveis */
	var VCPF = VPCPF; // Número do CPF a ser validado, recebido como parâmetro
	var VTamanhoCPF; // Tamanho do número digitado como CPF
	var I; // Contador
	var J = 10; // Contador
	var VNumeros; // Variável
	var VTotal = 0; // Recebe a Soma do Produto pelos seus respectivos pesos
	var VDigito1; // Recebe Primeiro Dígito
	var VResto; // Recebe o Resto da Divisão
	var VValor; // dd
	var VPosicao; // dd

	/* -------------------- Valida o primeiro dígito verificador do CPF */
	/* -------------------- Laço para a soma do produtos dos dígitos pelos seus respectivos pesos */
	for (I = 0; I < 9; I++)
	{ // Fo01
		VPosicao = VCPF.charAt(I);
		VTotal = VTotal + (VPosicao * J);
		J--;
	} // Fo01

	/* -------------------- Calcula o resto do primeiro dígito */
	VResto = VTotal * 10
	VResto %= 11

	/* -------------------- Verifica se o resto é igual a 10 */
	if (VResto == 10)
	{ // I02
		VResto = 0;
  	} // I02
	
	/* -------------------- Verifica se o dígito recebido é igual ao resto */
	if (VCPF.charAt(9) != VResto)
	{ // I03
		alert('CPF incorreto');
		return false;
	} // I03

	/* -------------------- Verifica 2º dígito verificador */
	
	J = 11;
	VTotal = 0
	for (I = 0; I < 10; I++)
	{ // Fo02
		VPosicao = VCPF.charAt(I);
		VTotal = VTotal + (VPosicao * J);
		J--;
	} // Fo02

	VResto = VTotal * 10
	VResto %= 11
	
	if (VResto == 10)
	{
    	VResto = 0;
	}

	if (VCPF.charAt(10) != VResto)
	{
		alert('CPF incorreto');
		return false;
	}

	return true;
	
} // F01

function ValidaData(data) {
    var y, m, d, s, dt;
    s = data.split("/");
    if (s.length != 3) return false;
    dt = new Date();
    dt.setFullYear(s[2], (s[1]-1), s[0]);   
    return ( (dt.getMonth()==(s[1]-1)) && (dt.getFullYear()==s[2]) );
}

function ProibirCaracteres(tipo, e) {
	
	var codigo;
	var tiposTestes = {	'apenas_numeros': '^[0-9 \r\t\n\f\b]+$',
						'apenas_letras': '^[a-zA-Z \r\t\n\f\b]+$',
						'letras_numeros': '^[a-zA-Z0-9 \r\t\n\f\b]+$',
						'letras_acentos_numeros': '^[a-zA-ZA-ÿ0-9 \r\t\n\f\b]+$',
						'letras_acentos_numeros_pontuacao': '^[a-zA-ZA-ÿ0-9\?!@\$\.\(\)\-: \r\t\n\f\b]+$',
						'letras_numeros_pontuacao': '^[a-zA-Z0-9\?!\.\-:, \r\t\n\f\b]+$'
					};
	
	if (!e) var e = window.event;
	if (e.keyCode) codigo = e.keyCode;
	else if (e.which) codigo = e.which;
	
	var teste = (tiposTestes[tipo] ? tiposTestes[tipo] : null);	
	var expReg = new RegExp(teste, "g");
	
	if (teste != null && expReg.test(String.fromCharCode(codigo))) {
		return true;
	} else 	{
		if (e && e.preventDefault) e.preventDefault();
		return false;
	}	
	
}

function RetirarCaracteresEspeciais(texto) {
	
	var i=0;
	var expReg;	
	var substituir_este = new Array ('ä','á','à','ã','â','ë','é','è','ê','ï','í','ì','î','ö','ó','ò','õ','ô','ü','ú','ù','û','ç','ñ','ÿ','ý','Ý',
						  'Ä','Á','À','Ã','Â','Ë','É','È','Ê','Ï','Í','Ì','Î','Ö','Ó','Ò','Õ','Ô','Ü','Ú','Ù','Û','Ç','Ñ',
						  '\'','"','!','#','\\$','%','¨','&','\\*','§','º','ª','\\?','/','°',';',':','>','<',',','\\^','~',',','\\\\','/',
						  '\\(','\\)','\\[','\\]','\\{','\\}','\\-','\\+');
	
	var substituir_por = new Array('a','a','a','a','a','e','e','e','e','i','i','i','i','o','o','o','o','o','u','u','u','u','c','n','y','y','Y',
						  'A','A','A','A','A','E','E','E','E','I','I','I','I','O','O','O','O','O','U','U','U','U','C','N',
						  '','','','','','','','','','','','','','','','','','','','','','','','','',
						  '','','','','','','','');
	
	if (texto != "") {		
		for (i=0; i<substituir_este.length; i++) {
			expReg = new RegExp(substituir_este[i], "g");
			texto = texto.replace(expReg, substituir_por[i]);
		}
	}
	
	return texto;
	
}

function RetiraCaracteresEspeciaisColar(id, evento) {
	
	var e		= evento || event;
	var codigo	= e.keyCode || e.which || e.charCode;
	var TECLA_V = 86;
	
	if (document.getElementById) objeto = document.getElementById(id)
	else if (document.all) objeto = eval('document.all.' + id)	
	
	//Catprua combinações de teclas.
	if (e.ctrlKey && (codigo == TECLA_V)) {
		objeto.value = RetirarCaracteresEspeciais(objeto.value);
	} else if (evento.type == 'paste') {
		objeto.value = RetirarCaracteresEspeciais(window.clipboardData.getData('Text'));
	}
	
	if (e && e.preventDefault) e.preventDefault();
	return false;
	
}

function AbreSobre() {
	var w=window.open('../frmSobre.asp?sistema=GV', 'Sobre', 'toolbar=no,statusbar=no,width=470px,height=313px'); 
	if (window.focus) w.focus();
}

function LerCartao() {
		
	if (document.form.txtNumsMatricula.value.length == 35 && !IsNumeric(document.form.txtNumsMatricula.value)){
		alert("Leitura do cartão magnético efetuada!")
		document.form.txtFlagCartao.value = 1
		MontaMatricula()
	}
	
}

function MontaMatricula() {

	if (document.form.txtNumsMatricula.value != ""){
		var codigo = document.form.txtNumsMatricula.value
		document.form.txtNumsMatricula.value = codigo.substring(8,15)
		document.form.txtNumsComplemento.value = codigo.substring(15,17)
		document.form.txtNumsMatricula.readOnly = true

		if (!IsNumeric(document.form.txtNumsMatricula.value) || !IsNumeric(document.form.txtNumsComplemento.value)){
			document.form.txtNumsMatricula.value = ""
			document.form.txtNumsComplemento.value = ""
			document.form.txtNumsMatricula.readOnly = false
			alert("Efetue a leitura do cartão magnético novamente!")
			document.form.txtNumsMatricula.focus()
		}else{
			document.form.submit()
		}
		
	}
}

function ValidaData(data) {
		var y, m, d, s, dt;
		s = data.split("/");
		if (s.length != 3) return false;
		dt = new Date();
		dt.setFullYear(s[2], (s[1]-1), s[0]);   
		return ( (dt.getMonth()==(s[1]-1)) && (dt.getFullYear()==s[2]) );
}

function ApenasNumeros(e) {    //função para somente mostrar numeros nos campos de entrada
	if (!e) var e = window.event;
	if (e.keyCode) codigo = e.keyCode;
	else if (e.which) codigo = e.which;
	
	 // Se for número (48 a 57)
	 if ( (codigo >= 48 && codigo <= 57) || codigo == 13) {
		 return true;
	 } else {
		  return false;
	 }
}

function ApenasLetras(e) {    //função para somente mostrar letras nos campos de entradaa
	
	if (!e) var e = window.event;
	if (e.keyCode) codigo = e.keyCode;
	else if (e.which) codigo = e.which;
	
	 // Se for letra (65 a 90) ou (97 a 122) ou espaço (32)
	 if ( (codigo >= 65 && codigo <= 90) || (codigo >= 97 && codigo <= 122) || (codigo == 32)  ) {
		 return true;
	 }
	 else {
		  return false;
	 }
}

function validaEmail(mail){
	var er = RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	if(er.test(mail) == false){
		window.alert("E-mail inválido!");
	}
}

function IsDate(pData){

	if(pData.length<10 || pData.length>10){

		return false;
	}

	var dia = '' + pData.substring(0, 2);
	var mes = '' + pData.substring(3, 5);
	var ano = '' + pData.substring(6, 10);

	if (!IsNumeric(dia)){
		return (false);
	}
	if (!IsNumeric(mes)){
		return (false);
	}
	if (!IsNumeric(ano)){
		return (false);
	}

	if(dia>'31'){
		return false;
	}

	if(mes>'12'){
		return false;
	}

	if(ano<='1900'){
		return false;
	}

	if(mes=='02'){
		if(ano%4!=0 && dia>'28'){
			return false;
		}
		else{
			if(dia>'29'){
				return false;
			}
		}
	}
	
	if(mes<='07'){
		if(mes%2==0 && dia>'30'){
			return false;
		}
	}
	else{
		if(mes>'09'){
			if(mes%2!=0 && dia>'30'){
				return false;
			}
		}
	}
					
	return true
}

function IsNumeric(pNum){

	if (pNum==''){
		return false;
	}
	for (var i = 0; i < pNum.length; i++){
		var ch = pNum.substring(i, i + 1);
		if (ch < '0' || '9' < ch){
			return false;
		}
	}
	return true;
}

function Letras(pstr) {

	if (pstr==''){
		return false;
	}
	var reg = /^[a-zA-Z ]+$/;
	if (reg.test(pstr)){
		return true;
	}
	else{
		return false;
	}	
}

function Left(campo,x){

	var res;
	if(x > campo.length)
		x = campo.length;
	res = campo.substring(0,x);
	return res;
}

	
function Right(campo,x){

	var res;
	if(x > campo.length){
		res = campo;
		return res;
	}
	res = campo.substring(campo.length-x,campo.length);
	return res;
}

	
function UCase(campo){

	return campo.toUpperCase()
}

	
function LCase(campo){

	return campo.toLowerCase()
}

	
function RTrim(campo){

	y=true;
	while(y==true){
		x = campo.length;
		if(Right(campo,1)==' '){
			campo = Left(campo,x-1);
			y=true;
		}
		else{
			y=false
		}
	}
	return campo;
}

	
function LTrim(campo){

	y=true;
	while(y==true){
		x = campo.length-1;
		if(Left(campo,1)==' '){
			campo = Right(campo,x);
			y=true;
		}
		else{
			y=false;
		}
	}
	return campo;
}

	
function Trim(campo){

	return RTrim(LTrim(campo));
}

function AbreLegislacaoPrincipal(idCat) {
	w = window.open("http://www.ipasgo.go.gov.br/Legislacao/frmLegislacaoListagem.asp?cat=" + idCat, 'LegislacaoPrincipal', 'width=700,height=600, menubar=no,status=no,top=0,left=0,scrollbars=yes, resizable=yes');
	if (w.focus) w.focus();
}