function somenteNumero(teclapres){
	if (teclapres.keyCode < 45 || teclapres.keyCode > 57) {
		teclapres.returnValue = false;
	}
}

function FormataCPF(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = Campo.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "-", "" );
	tam = vr.length + 1;
	//alert(tam);
	if ( tecla != 9 && tecla != 8 ){
		if ( tam <= 3 ){ Campo.value = vr ; }
	 	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 ) ; }
	 	if ( (tam > 9) && (tam <= 11) ){
		 	Campo.value = vr.substr( 0, 3 ) + '.' + vr.substr( 3, 3 ) + '.' + vr.substr( 6, 3 ) + '-' + vr.substr( 9, tam ) ; }
	}
}

function FormataDado(campo,tammax,pos,teclapres){
	var tecla = teclapres.keyCode;
	vr = campo.value;
	vr = vr.replace( "-", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length ;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){ tam = tam - 1 ; }
			
	if ( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){
	 		campo.value = vr ;}
		if ( tam > pos && tam <= tammax ){
			campo.value = vr.substr( 0, tam - pos ) + '-' + vr.substr( tam - pos, tam );}
	}
}

/************************************************
* function verificaCPF
* Verifica se um CPF é válido
* Input: cpf a ser verificado
************************************************/
function verificaCPF(cpf){
	if(cpf == "") {
		return true;
	}
	
	cpf = cpf.replace( ".", "" );
	cpf = cpf.replace( ".", "" );
	cpf = cpf.replace( ".", "" );
	cpf = cpf.replace( ".", "" );
	cpf = cpf.replace( "-", "" );
	
	var dac = "", inicio = 2, fim = 10, soma, digito, i, j
	for (j=1;j<=2;j++) {
		soma = 0
		for (i=inicio;i<=fim;i++) {
			soma += parseInt(cpf.substring(i-j-1,i-j))*(fim+1+j-i)
		}
		if (j == 2) { soma += 2*digito }
		digito = (10*soma) % 11
		if (digito == 10) { digito = 0 }
		dac += digito
		inicio = 3
		fim = 11
	}
	return (dac == cpf.substring(cpf.length-2,cpf.length))
}

/************************************************
* function verificaCPF
* Verifica se um CPF é válido
* Input: cpf a ser verificado
************************************************/
function modulo11(valor) {
	if (valor.length < 3){ return false; }
	
	valor = valor.replace( ".", "" );
	valor = valor.replace( ".", "" );
	valor = valor.replace( ".", "" );
	valor = valor.replace( "/", "" );
	
	arrValor = valor.split('-');
	x = arrValor[0];
	if (!isInteger(x)) { return false; }
	dv = arrValor[1];
	if (dv == "") { return false; }
	
	var vet = '9876543298765432987654';
	//var vet = '29876543298765432987654';
	var total = 0; var1 = 0; resto = 0; digito = 0;
	j = x.length-1;
	for (i=0;i<x.length;i++) {
	  var1 = parseInt(x.charAt(j)) * parseInt(vet.charAt(i));
	total = total + var1;
	  j = j - 1;
	}
	resto = total % 11;
	if (resto == 10) {	
		digito = 'X'; 
	} else {
		digito = resto
	}
	dv = dv.toUpperCase();
	if (digito != dv) {
		//alert (msg);
		return false;
	}
	return true;
}

function FormataCEP(Campo,teclapres) {
var tecla = teclapres.keyCode;
	vr = Campo.value;
	vr = vr.replace( "-", "" );
	tam = vr.length + 1;
	
	if ( tecla != 9 && tecla != 8 ){
		if ( tam <= 5 ){ Campo.value = vr ; }
		if ( tam > 5 && tam <= 9 )
			Campo.value = vr.substr( 0, 5  ) + '-' + vr.substr( 5, tam );
	}
}

function FormataData(Campo,teclapres) {
var tecla = teclapres.keyCode;
	vr = Campo.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;
	
	if ( tecla != 9 && tecla != 8 ){
	if ( tam > 2 && tam < 5 )
	Campo.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
	if ( tam >= 5 && tam <= 10 )
		Campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
	}
}

function FormataValor(Campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = Campo.value;
	sinal = "";
	if (vr.substring(0,1) == "0"){
		vr = vr.replace("0,0","");
		vr = vr.replace("0,","");
	}
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ",", "" );
	tam1 = vr.length;
	tam = vr.length+1;
	
	if (tam >= tammax){
		teclapres.returnValue = false;
	}else{
		if ( tecla != 9 && tecla != 8 ){
			//if ( tam <= 3 ){ valorFormatado = vr ; }
			if (tam > 0 && tam < 2 )
				Campo.value = '0,0' + vr.substr(0,tam - 1);
			if (tam >= 2 && tam < 3 )
				Campo.value = '0,' + vr.substr(0,tam);
			if ( (tam >= 3) && (tam <= 5) ){
				Campo.value = vr.substr(0,tam - 2) + ',' + vr.substr(tam - 2, tam);	}
			if ( (tam > 5) && (tam <= 8) ){
		 		Campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3) + ',' + vr.substr(tam - 2, tam) ; }
		 	if ( (tam > 8) && (tam <= 11) ){
			 	Campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam -5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam > 11) && (tam <= 14) ){
				Campo.value = vr.substr( 0, tam-11 ) + '.' + vr.substr( tam-11, 3 ) + '.' + vr.substr( tam-8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
		}
	}
}
	
/************************************************
* function warnInvalid
* Gera um alert para o usuário e volta o foco para
* o campo que está com problema
* Input: theField - campo do formulário com problema
*        warnText - texto a ser mostrado no alert
************************************************/

function warnInvalid (theField, warnText)
{   theField.focus()
    theField.select()
	alert(warnText)
    return false
}

function checkField(s) {
	if ((s.indexOf("&")>=0) || (s.indexOf("<")>=0) || (s.indexOf(">")>=0) || (s.indexOf("|")>=0) || (s.indexOf("\"")>=0) || (s.indexOf("/")>=0) )
		return false;
	return true;
}

// Verifica se uma string tem vogais acentuadas
function vogalAcentuada(s) {
	ls = s.toLowerCase();
	if ((ls.indexOf("á")>=0) || (ls.indexOf("à")>=0) || (ls.indexOf("ã")>=0) || (ls.indexOf("â")>=0) || (ls.indexOf("é")>=0) || (ls.indexOf("í")>=0) || (ls.indexOf("ó")>=0) || (ls.indexOf("õ")>=0) || (ls.indexOf("ô")>=0) || (ls.indexOf("ú")>=0) || (ls.indexOf("ü")>=0))
		return true;
}

/************************************************
* function verificaEmail
* Verifica se um email é válido
* Input: email a ser verificado
************************************************/

function verificaEmail(email) {
	var s = new String(email);
	// { } ( ) < > [ ] | \ /
	if ((s.indexOf("{")>=0) || (s.indexOf("}")>=0) || (s.indexOf("(")>=0) || (s.indexOf(")")>=0) || (s.indexOf("<")>=0) || (s.indexOf(">")>=0) || (s.indexOf("[")>=0) || (s.indexOf("]")>=0) || (s.indexOf("|")>=0) || (s.indexOf("\"")>=0) || (s.indexOf("/")>=0) )
		return false;
	if (vogalAcentuada(email))
		return false;
	// & * $ % ? ! ^ ~ ` ' "
	if ((s.indexOf("&")>=0) || (s.indexOf("*")>=0) || (s.indexOf("$")>=0) || (s.indexOf("%")>=0) || (s.indexOf("?")>=0) || (s.indexOf("!")>=0) || (s.indexOf("^")>=0) || (s.indexOf("~")>=0) || (s.indexOf("`")>=0) || (s.indexOf("'")>=0) )
		return false;
	// , ; : = #
	if ((s.indexOf(",")>=0) || (s.indexOf(";")>=0) || (s.indexOf(":")>=0) || (s.indexOf("=")>=0) || (s.indexOf("#")>=0) )
		return false;
	// procura se existe apenas um @
	if ( (s.indexOf("@") < 0) || (s.indexOf("@") != s.lastIndexOf("@")) )
		return false;
	// verifica se tem pelo menos um ponto após o @
	if (s.lastIndexOf(".") < s.indexOf("@"))
		return false;
	return true;
}

function verificaCEP (cep) {
	cep = cep.replace( "-", "" );
	s = new String(cep);
	if ((s.length > 9) || (s.length < 5))
		return false;
	if (!isInteger(cep))
		return false;
	return true;
}

/************************************************
* function isInteger
* Verifica se um campo é inteiro, inclui dígitos de 0 a 9, vírgula, ponto, espaços e -
* Input: campo a ser verificado
************************************************/

function isInteger(s){
	var i;
	if (isEmpty(s))
		return false;
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (!isNumber(c)) return false;
	}
	return true;
}

// Retorna verdadeiro se a string for vazia ou composta apenas de espaços
function isEmpty(str) { 
	if (str==null) return true;
	if (str.length == 0) return true;
	for (var intLoop = 0; intLoop < str.length; intLoop++){
		if (" " != str.charAt(intLoop)){
			return false;
		}
	}	            
	return true;
}

// Verifica se a string possui apenas caracteres numéricos
function isNumber(strNumber) {
	for(var i = 0; i < strNumber.length; i++) {
		if(strNumber.charAt(i) < '0' || strNumber.charAt(i) > '9'){return(false)}
	}
	return(true);
}

// Verifica se a string representa um valor do tipo date
function IsDate(strDate) {
	var
		i, intDay, intMonth, intYear,
		strFormat = "/dmy",
		intSep1 = strDate.indexOf(strFormat.charAt(0)),
		intSep2 = strDate.lastIndexOf(strFormat.charAt(0)),
		strParts = new Array(strDate.substring(0, intSep1), strDate.substring(intSep1 + 1, intSep2), strDate.substr(intSep2 + 1, 4));
	for(i = 0; i < 3; i++) {
		switch(strFormat.charAt(i + 1)) {
			case "d": intDay = parseInt(strParts[i], 10); break;
			case "m": intMonth = parseInt(strParts[i], 10); break;
			case "y": intYear = parseInt(strParts[i], 10);
		}
	}
	if(!isNumber(intYear)){return(false)}
	return(IsBetween(intDay, 1, MonthDays(intMonth, intYear)));
}

// Verifica se um valor está dentro da faixa representada por outros dois
function IsBetween(intValue, intMin, intMax) {
	return(intValue >= intMin && intValue <= intMax);
}

// Retorna o nº de dias de um mês num certo ano
function MonthDays(intMonth, intYear){
	switch(intMonth)	{
		case 1: //conJan
		case 3: //conMar
		case 5: //conMay
		case 7: //conJul
		case 8: //conAug
		case 10: //conOct
		case 12: return(31); //conDec
		case 4: //conApr
		case 6: //conJun
		case 9: //conSep
		case 11: return(30); //conNov
		case 2: return((((intYear - 96) % 4) == 0) ? 29 : 28); //conFeb
		default: return(-1);
	}
}

/************************************************
* function verificaCGC
* Verifica se um CGC é válido
* Input: cgc a ser verificado
************************************************/

function verificaCGC(scgc) {
	cgc = trimtodigits(scgc);
	if ((cgc.indexOf("-") != -1) || (cgc.indexOf(".") != -1) || (cgc.indexOf("/") != -1)){
		return( false )
	}
	var df, resto, dac = ""
	df = 5*cgc.charAt(0)+4*cgc.charAt(1)+3*cgc.charAt(2)+2*cgc.charAt(3)+9*cgc.charAt(4)+8*cgc.charAt(5)+7*cgc.charAt(6)+6*cgc.charAt(7)+5*cgc.charAt(8)+4*cgc.charAt(9)+3*cgc.charAt(10)+2*cgc.charAt(11)
	resto = df % 11
	dac += ( (resto <= 1) ? 0 : (11-resto) )
	df = 6*cgc.charAt(0)+5*cgc.charAt(1)+4*cgc.charAt(2)+3*cgc.charAt(3)+2*cgc.charAt(4)+9*cgc.charAt(5)+8*cgc.charAt(6)+7*cgc.charAt(7)+6*cgc.charAt(8)+5*cgc.charAt(9)+4*cgc.charAt(10)+3*cgc.charAt(11)+2*parseInt(dac)
	resto = df % 11
	dac += ( (resto <= 1) ? 0 : (11-resto) )
	return (dac == cgc.substring(cgc.length-2,cgc.length))
}

function maiuscula(campo){
	strTexto = campo.value;
	campo.value = strTexto.toUpperCase();
}

function imprimir(){
	print();
}