/*
=========================================================================================
Biblioteca de funções JavaScript - Testadas em IE7 e FireFox 2.0
Programador - Marcos
Data de criação.: 09/06/2008
Manutenção.: Marcos - 10/06/2008 - Inclusão de função clickBtnFecharModal()
=========================================================================================
*/
	/*Validação de CPF*/
	function VerificarCPF(Objcpf) {
		var cpf = Objcpf.value;
		if ((cpf == "00000000000")
	 	||  (cpf == "11111111111")
		||  (cpf == "22222222222")
		||  (cpf == "33333333333")
		||  (cpf == "44444444444")
		||  (cpf == "55555555555")
		||  (cpf == "66666666666")
		||  (cpf == "77777777777")
		||  (cpf == "88888888888")
		||  (cpf == "99999999999")
		||  (parseInt(Objcpf.value.toString().length) < 11)) {
			alert('CPF Invalido!, digite onze numeros para o nº do documento. \n Caso seja necessário insira zeros no inicio do número.');
			Objcpf.focus();
			return false;
		} else {
			if(cpf != "") {
				var i;
				exp = /\.|\-/g
		      cpf = cpf.toString().replace( exp, "" );
				s = cpf;
				var c  = s.substr(0,9);
				var dv = s.substr(9,2);
				var d1 = 0;
				var v = false;
				for (i = 0; i < 9; i++)	{
					d1 += c.charAt(i)*(10-i);
				}
				if (d1 == 0) {
					alert("CPF Inválido");
					v = true;
					Objcpf.focus();
					return false;
				}
				d1 = 11 - (d1 % 11);
				if (d1 > 9) d1 = 0;
				if (dv.charAt(0) != d1)	{
					alert("CPF Inválido")
					v = true;
					Objcpf.focus();
					return false;
				}
				d1 *= 2;
				for (i = 0; i < 9; i++)	{
					d1 += c.charAt(i)*(11-i);
				}
				d1 = 11 - (d1 % 11);
				if (d1 > 9) d1 = 0;
				if (dv.charAt(1) != d1)	{
					alert("CPF Inválido")
					v = true;
					Objcpf.focus();
					return false;
				}
				if (!v) {
					return true;
				}
			}
		}
	}
	/*Formata Entrada de valores com separadores de milhares e decimais*/
	function currencyFormat(fld, milSep, decSep, e) {
		var sep = 0;
	  	var key = '';
	  	var i = j = 0;
	  	var len = len2 = 0;
	  	var strCheck = '0123456789';
	  	var aux = aux2 = '';
	  	var whichCode = (window.Event) ? e.which : e.keyCode;

	  	if (whichCode == 13) return true;  // Enter
	  	if (whichCode == 8) return true;  // Delete
	  	key = String.fromCharCode(whichCode);  // Get key value from key code
	  	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	  	len = fld.value.length;
	  	for(i = 0; i < len; i++)
	  	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	  	aux = '';
	  	for(; i < len; i++)
	  	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	  	aux += key;
	  	len = aux.length;
	  	if (len == 0) fld.value = '';
	  	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	  	if (len == 2) fld.value = '0'+ decSep + aux;
	  	if (len > 2) {
	   	aux2 = '';
	    	for (j = 0, i = len - 3; i >= 0; i--) {
	      	if (j == 3) {
	        		aux2 += milSep;
	        		j = 0;
	      	}
	      	aux2 += aux.charAt(i);
	      	j++;
	    	}
	    	fld.value = '';
	    	len2 = aux2.length;
	    	for (i = len2 - 1; i >= 0; i--)
	    	fld.value += aux2.charAt(i);
	    	fld.value += decSep + aux.substr(len - 2, len);
	  	}
		return false;
	}

	/*Verifica digitação de caracteres inválidos
		Parametro w = special - Não aceita digitação de caracteres especiais como (like !@#$%* etc,) underscore é aceito
		Parametro w = quotes  - Não aceita digitação de aspas simples ou duplas.
		Parametro w = notnumbers - Verifica se foi digitado somente números
	*/
	function valid(o,w) {
		var r={
			'special':/[\W]/g,
		  	'quotes':/['\''&'\"']/g,
		  	'notnumbers':/[^\d]/g
		}
		o.value = o.value.replace(r[w],'');
	}

	/*Verifica digitação de caracteres válidos*/
	function validate(field, tipo) {
		/*
		var valid = "abcdefghijklmnopqrstuvwxyz0123456789"
		Tipos de validações
		1 = Somente Letras acentuadas maiusculas e minusculas mais caractere &
		2 = Somente Números e Letras maiusculas e minusculas sem acento
		3 = Somente Números e Letras minusculas acentuadas
		*/
		if(field.value == "") {
			return false;
		}
		if(tipo <= 0) {
			return false;
		}
		if(tipo == 1) {
			var valid = "abcdefghijklmnopqrstuvwxyzãõçáéíóúàèòùABCDEFGHIJKLMNOPQRSTUVWXYZÃÕÇÁÉÍÓÚÀÈÒÙ&";
		} else if(tipo == 2) {
			var valid = " 012345678abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		} else if(tipo == 3) {
			var valid = " 012345678abcdefghijklmnopqrstuvwxyz";
		}
		var ok = "yes";
		var temp;
		for (var i=0; i<field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				ok = "no";
			}
		}
		if (ok == "no") {
			/*
			alert("Informe Somente caracteres válidos : " + valid);
			field.focus();
			*/
			return false;
		} else {
			return true;
		}
	}

	/*Função para fechar Janela Modal Prototype.*/
	function clickBtnFecharModal() {
		parent.hidePopWin(false);
	}
	/*
	Função para validação de entrada de dados com expressões regulares
	Recebe os seguintes parametros.
	1 = Expressao Regular Que Permite Somente Letras Com Acentos
	2 = Expressao Regular Que Permite '.',letras E Numeros
	3 = Expressao Regular Que Permite '.',letras E Acentos
	4 = Expressao Regular Que Permite '.',letras,acentos E Numeros
	5 = Expressao Regular Que Permite Somente Numeros
	6 = Expressao Regular Que Permite Numeros e "-" e "()"
	*/
	function ValidCampoExpreReg(obj,Nome,ExprRegTip) {
	    var Valor = obj.value;
	    if (Valor == '' || Valor == ' ' || Valor == null) {
	        return false;   //Se O Campo Nao Foi Preenchido, Entao Eu Saio Da Funcao, Pois "" Nao É Válido
	    }
	    var ExprReg =  /^\d+$/;//expresao regular para validar inteiros
	    if (ExprReg.test(ExprRegTip) == false)//VERIFICO SE 'ExprRegTip' É UM INTEIRO
	    {
	        alert('Nenhuma Tipo de Expressão Regular Valida fornecida');
	        return false;
	    }
		//alert("Valor = " + Valor + "ExprReg = " + ExprReg);

	    var ErrMsg = ''; //Variavel Que Recebe O Retorno De Erro Da Expressao Regular
	    ExprReg    = ''; //Variavel Que Recebe A Expressao Regular
	    if (ExprRegTip == 1){//Expressao Regular Que Permite Somente Letras Com Acentos
	    	ExprReg = /(^([A-Za-zá-úÁ-Úà-ùÀ-Ù\s]){1,100})+$/;
	    	ErrMsg = 'Letras e acentuação';
	    }
	    if (ExprRegTip == 2){ //Expressao Regular Que Permite '.',letras E Numeros
	    	ExprReg = /(^([A-Za-z0-9.\s]){1,50})+$/;
	    	ErrMsg = 'Letras e números';
	    }
	    if (ExprRegTip == 3){ //Expressao Regular Que Permite '.',letras E Acentos
	    	ExprReg = /(^([A-Za-zá-úÁ-Úà-ùÀ-Ù.,\s]){1,50})+$/;
	    	ErrMsg = 'Letras, acentuação e sinais : .(ponto), (virgula)';
	    }
	    if (ExprRegTip == 4){//Expressao Regular Que Permite '.',letras,acentos E Numeros  ,
	    	ExprReg = /(^([A-Za-zá-úÁ-Úà-ùÀ-Ù0-9.,\s]){1,50})+$/;
	    	ErrMsg = 'letras, acentuação, números e sinais : .(ponto), (virgula)';
	    }
	    if (ExprRegTip == 5){//Expressao Regular Que Permite Numeros
	    	ExprReg = /(^([0-9\s]){1,50})+$/;
	    	ErrMsg = 'números';
	    }
	    if (ExprRegTip == 6){//Expressao Regular Que Permite Numeros e "-" e "()"
	    	ExprReg = /(^([0-9-()\s]){1,50})+$/;
	    	ErrMsg = 'Números e sinais : ()(Parentêses) e -(Traço) ';
	    }
	    if (ExprReg == '') {
	        alert('Nenhuma Tipo de Expressão Regular conhecida fornecida');
	        return false;
	    }

	    if (ExprReg.test(Valor) == false) {
	        alert('O campo ' + Nome + ' só aceita ' + ErrMsg + '.');//MENSAGEM DE ERRO PARA O CLIENTE
	        //document.getElementById(Campo).className = "FormCampoErr";//MUDO O ESTILO DO CAMPO PARA MELHOR VIZUALIZAÇÃO DO ERRO
	        return false;
	    }
	    return true;
	}
	function teste() {
		if(window.event.keyCode == 27) {
			parent.hidePopWin(false);
		}
	}
	//Valida E-mail
	function checaEmail(nform) {
		if (nform.value == "") {
			return false;
		} else {
			prim = nform.value.indexOf("@")
			if(prim < 2) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("@",prim + 1) != -1) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf(".") < 1) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf(" ") != -1) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("zipmeil.com") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("hotmeil.com") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("hotmail.com.") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("gmail.com.") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf(".@") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("@.") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf(".com.br.") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf(".combr") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("/") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("[") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("]") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("(") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf(")") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
			if(nform.value.indexOf("..") > 0) {
				alert("O e-mail informado parece não estar correto.");
				nform.focus();
				nform.select();
				return false;
			}
		}
	}
	function validarData(objdat) {
		/*var date = document.getElementById('data').value;*/
		var date = objdat.value;
		var array_data = new Array;
		var ExpReg = new RegExp("(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/[12][0-9]{3}");
		//vetor que contem o dia o mes e o ano
		array_data = date.split("/");
		erro = false;
		//Valido se a data esta no formato dd/mm/yyyy e se o dia tem 2 digitos e esta entre 01 e 31
		//se o mes tem d2 digitos e esta entre 01 e 12 e o ano se tem 4 digitos e esta entre 1000 e 2999
		if (date.search(ExpReg) == -1)
			erro = true;
		//Valido os meses que nao tem 31 dias com execao de fevereiro
		else if ( ( ( array_data[1] == 4 ) || ( array_data[1] == 6 ) || ( array_data[1] == 9 ) || ( array_data[1] == 11 ) ) && ( array_data[0] > 30 ) )
			erro = true;
		//Valido o mes de fevereiro
		else if ( array_data[1] == 2 ) {
			//Valido ano que nao e bissexto
			if ( ( array_data[0] > 28 ) && ( ( array_data[2] % 4 ) != 0 ) )
				erro = true;
			//Valido ano bissexto
			if ( ( array_data[0] > 29 ) && ( ( array_data[2] % 4 ) == 0 ) )
				erro = true;
		}
		if ( erro ) {
			/*alert("Data Invalida");
			form1.data.focus();*/
			return false;
		} else {
			/*alert("Data OK!!!!!!!");*/
			return true;
		}
	}
	function VerificaRadioButton(objForm) {
		var qtde_selecionada = 0;
		for(x=0; x<objForm.elements.length; x++) {
			if(objForm.elements[x].type == "radio") {
				if(objForm.elements[x].checked == true) {
					qtde_selecionada++;
			   }
			}
		}
		if ((qtde_selecionada) == 0) {
			return false;
		} else {
			return true;
		}
	}
	function validaTexto(id) {
		var badValue = document.getElementById(id).value.match(/([1234567890!@#$%¨&*()_-{[]}?.,)/gi);
		if (badValue) {
			return false;
		} else {
			return true;
		}
	}
