/*****************************************************************************
/* Biblioteca de validação de campos
/* Edmundo N. Oliveira Jr
/* Ultima Alteração: 08/11/2008
/****************************************************************************/

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraDataComHora(this,event)""
 * 04/08/2010 - Adriano
 */
/****************************************************************************/
function mascaraDataComHora(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var data = objeto.value;

	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if (data.length <= 15) {
			if 	((data.length == 2 || data.length == 5) && tecla != 8) {
				data += '/';
				objeto.value = data;
			} else {
				if(tecla != 8) {
					if(data.length == 10){
						data += ' ';
						objeto.value = data;
					}
					if((data.length == 13)){
						data += ':';
						objeto.value = data;
					}	
				} else {
					return true;
				}
			}
		} else {
			if(tecla == 8 || tecla == 37 || tecla == 9 || tecla == 46) {
				return true;
			} else {
				return false;
			}
		}
		return true;
	} else {
		return false;
	} 
}

/*****************************************************************************
/* Utilização: onBlur="formataString(this,'a')"
 * 15/07/2010 - Adriano
 * a - minusculo    A - maiusculo   p - Primeira Maiuscula
 */
/****************************************************************************/
function formataString (prInput ,prTipoFormatacao) {

	function formataPrimeiraMaiuscula(objeto) {
		var palavrasLivres = new Array ('de','da','do','das','des','dos','e');
		var ret = '';
		var string = objeto.value;
		var temp = new Array();
		temp = string.split(' ');
		for(var x = 0; x < temp.length; x++) {
			if (temp[x] == '') { continue; }
			var verificacao = Array.indexOf(palavrasLivres, temp[x].toLowerCase());
			if (x > 1) { ret += ' '; }
			if(verificacao == -1) {
				ret += primeiraLetraMaiuscula (temp[x])+' ';
			} else {
				ret += temp[x].toLowerCase()+' ';
			}
		}
		return ret;
	}
	function primeiraLetraMaiuscula(prString) {
		var string = prString.toLowerCase();
		str1 = string.substring(0,1);
		ret = string.replace(str1, str1.toUpperCase());
		return ret;
	}

	var prString = prInput.value;
	switch (prTipoFormatacao) {
		case 'a' : valorFormatado =  prString.toLowerCase(); break;
		case 'A' : valorFormatado =  prString.toUpperCase(); break;
		default  : valorFormatado =  formataPrimeiraMaiuscula (prInput); break;  
	}
	prInput.value = valorFormatado;
}


/*****************************************************************************
/* Utilização: onKeyDown="return mascaraFone(this, event)"
 * 17/09/2008
 */
/****************************************************************************/
function mascaraFone(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var fone = objeto.value;

	if	(tecla == 9 || tecla == 13) { /* TAB e ENTER */ 
		return true;
	}

	if 	(fone.length >= 9) {
		if	(tecla != 9 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) {
			return false;
		} else {
			return true;
		}
	}
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	(fone.length == 4 && tecla != 8) {
			fone += '-';
			objeto.value = fone;
		}
		return true;
	} else {
		return false;
	} 
}

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraFoneComDDD(this, event)" - 13/07/2009
/****************************************************************************/
function mascaraFoneComDDD(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var fone = objeto.value;
	if 	(fone.length >= 14) {
		if	(tecla != 9 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) {
			return false;
		} else {
			return true;
		}
	}
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	(fone.length == 2 && tecla != 8) {
			fone = '('+fone;
			objeto.value = fone;
		}
		if 	(fone.length == 3 && tecla != 8) {
			fone += ') ';
			objeto.value = fone;
		}
		if 	(fone.length == 9 && tecla != 8) {
			fone += '-';
			objeto.value = fone;
		}
		return true;
	} else {
		return false;
	} 
}

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraInt(this, event)"
/****************************************************************************/
function mascaraInt(objeto, event) {
	var valor = objeto.value;
	var tecla = event.keyCode?event.keyCode:event.which;
	if ( tecla < 48 || (tecla > 57 && tecla < 96) || tecla > 105 ) {
		if	(tecla==9 || tecla==8 ||tecla==46 || tecla==35 || tecla==36 || tecla==37 || tecla==39 || tecla==13) {
			return true;
		}
		objeto.value = valor;
		return false;
	} 
} 


/*****************************************************************************
/* Utilização: onKeyDown="return mascaraInt2(this, event, max)"
/****************************************************************************/
function mascaraInt2(objeto, event, max) {
	var valor = objeto.value;
	var tecla = event.keyCode?event.keyCode:event.which;
	if	(valor.length >= max) {
		if	(tecla==9 || tecla==8 ||tecla==46 || tecla==35 || tecla==36 || tecla==37 || tecla==39 || tecla==13) {
			return true;
		} else {
			return false;
		}
	}
	if ( tecla < 48 || (tecla > 57 && tecla < 96) || tecla > 105 ) {
		if	(tecla==9 || tecla==8 ||tecla==46 || tecla==35 || tecla==36 || tecla==37 || tecla==39 || tecla==13) {
			return true;
		}
		objeto.value = valor;
		return false;
	} 
} 

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraCep(this, event)" - 17/09/2008
/****************************************************************************/
function mascaraCep(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var cep = objeto.value;

	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	(cep.length == 5 && tecla != 8) {
			cep += '-';
			objeto.value = cep;
		}
		return true;
	} else {
		return false;
	}
}

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraCPF(this, event)"
/****************************************************************************/
function mascaraCPF(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var cpf = objeto.value;

	if	(tecla == 9 || tecla == 13) { /* TAB e ENTER */ 
		return true;
	}

	if	(objeto.selectionEnd == objeto.value.length && objeto.alt != "1") {
		objeto.value = "";
		objeto.alt = "1";
	}

	if 	(cpf.length >= 14) {
		if	(tecla != 9 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) {
			return false;
		} else {
			return true;
		}
	}
	
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	((cpf.length == 3 || cpf.length == 7)  && tecla != 8) {
			cpf += '.';
			objeto.value = cpf;
		}
		if 	((cpf.length == 11) && tecla != 8) {
			cpf += '-';
			objeto.value = cpf;
		}
		return true;
	} else {
		return false;
	} 
}
	
/*****************************************************************************
/* Utilização: onKeyDown="mascaraCNPJ(this, event)"
/****************************************************************************/
function mascaraCNPJ(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var cnpj = objeto.value;
	if 	(cnpj.length >= 18) {
		if	(tecla != 9 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) {
			return false;
		} else {
			return true;
		}
	}
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	((cnpj.length == 2 || cnpj.length == 6) && tecla != 8) {
			cnpj += '.';
			objeto.value = cnpj;
		}
		if 	(cnpj.length == 10 && tecla != 6 && tecla != 8) {
			cnpj += '/';
			objeto.value = cnpj;
		}
		if 	(cnpj.length == 15 && tecla != 6 && tecla != 8) {
			cnpj += '-';
			objeto.value = cnpj;
		}
		return true;
	} else {
		return false;
	} 
}

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraFloat(this, event)" onBlur="formataFloat(this)"
/****************************************************************************/
function mascaraFloat(objeto, event) {
	var valor = objeto.value;
	var tecla = event.keyCode?event.keyCode:event.which;
	if	(tecla == 9 || tecla == 13) { /* TAB e ENTER */ 
		return true;
	}

	if	(objeto.selectionEnd == objeto.value.length && objeto.alt != "1") {
		objeto.value = "";
		objeto.alt = "1";
	}

	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	(valor.length == 2 && tecla != 8) {
			valor = valor.replace(",","");
			valor = valor.replace(".","");
			valor = valor.substring(0,1)+','+valor.substring(1,2);
		}
		if 	(valor.length >= 4 && tecla != 8) {
			valor = valor.replace(",","");
			valor = valor.substring(0,valor.length-1)+','+valor.substring(valor.length-1);
		}
		if 	(valor.length == 6 && tecla != 8) {
			valor = valor.substring(0,1)+'.'+valor.substring(1);
		}
		if 	(valor.length >= 7 && valor.length < 10 && tecla != 8) {
			valor = valor.replace(".","");
			valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);
		}
		if 	(valor.length == 10 && tecla != 8) {
			valor = valor.replace(".","");
			valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);			
			valor = valor.substring(0,valor.length-9)+'.'+valor.substring(valor.length-9);			
		}
		if 	(valor.length > 10 && tecla != 8) {
			valor = valor.replace(".","");
			valor = valor.replace(".","");
			valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);			
			valor = valor.substring(0,valor.length-9)+'.'+valor.substring(valor.length-9);			
		}
		objeto.value = valor;
		return true;
	} 
	objeto.value = valor;
	return false;
}

/*****************************************************************************
/* Utilização: onKeyDown="mascaraFloat(this, event)" onBlur="formataFloat(this)"
/****************************************************************************/
function formataFloat(objValor) {
	valor = objValor.value;
	if	(valor.length == 2) {
		valor = "0,"+valor;
		objValor.value = valor;
		return true;
	}
}

function formataFloat2(valor) {
	var ret = 0;
	ret = valor.toLocaleString();
	return ret;
	/*
	valor = valor.replace(".","");
	if 	(valor.length == 2) {
		valor = valor.replace(",","");
		valor = valor.replace(".","");
		valor = valor.substring(0,1)+','+valor.substring(1,2);
	}
	if 	(valor.length >= 4) {
		valor = valor.replace(",","");
		valor = valor.substring(0,valor.length-1)+','+valor.substring(valor.length-1);
	}
	if 	(valor.length == 6) {
		valor = valor.substring(0,1)+'.'+valor.substring(1);
	}
	if 	(valor.length >= 7 && valor.length < 10) {
		valor = valor.replace(".","");
		valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);
	}
	if 	(valor.length == 10) {
		valor = valor.replace(".","");
		valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);			
		valor = valor.substring(0,valor.length-9)+'.'+valor.substring(valor.length-9);			
	}
	if 	(valor.length > 10) {
		valor = valor.replace(".","");
		valor = valor.replace(".","");
		valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);			
		valor = valor.substring(0,valor.length-9)+'.'+valor.substring(valor.length-9);			
	}
	ret = valor;
	return ret;
	*/
}

/*****************************************************************************
/* Utilização: returnNumber(parameter)
/****************************************************************************/
function returnNumber(parameter) {
	var parameter = toFloat(parameter);
	var tmp = parseInt(parameter);
	if	( isNaN(tmp) ) {
		tmp = 0;
	} else {
		tmp = parseFloat(parameter);
	}
	return tmp;
}

/*****************************************************************************
/* Utilização: toFloat(parameter)
/****************************************************************************/
function toFloat(parameter) {
	var ret;
	var tmp = parameter.indexOf(",");
	var tmp2;
	if	(tmp != -1) {  // if have virgula
		tmp2 = parameter.replace('.','');
		tmp2 = tmp2.replace(',','.');
		ret = tmp2;
	} else {
		tmp = parameter.indexOf(".");
		if	(tmp != -1) {  // if have virgula
			ret = parameter;
		} else {
			if	(parameter == 0) {
				ret = 0;
			} else {
				ret = parseFloat(parameter+'.00');
			}
		}
	}
	return ret;
}

/*****************************************************************************
/* Utilização: numberToValueForm(parameter)
/****************************************************************************/
function numberToValueForm(parameter) {
	var valor = parameter.toString();
	var fponto;
	var sponto;
	var ret;
	valor = valor.replace(".",",");
	fponto = valor.indexOf(',')-4;
	sponto = valor.indexOf(',')-7;
	ret = valor.substr(0,1);
	for (i = 1; i < valor.length; i++) {
		ret = ret + valor.substr(i,1);
		if	(i == fponto || i == sponto) {
			ret = ret + '.';
		}
	}
	return ret;
}

/*****************************************************************************
/* 	Utilização: onKeyDown="mascaraCNPJ(this, event)" 
	Last Modify: 08/11/2008
/****************************************************************************/
function mascaraCNPJCPF(objeto, tecla, type) {
	var validChar =  "#48;#49;#50;#51;#52;#53;#54;#55;#56;#57;#96;#97;#98;#99;#100;#101;#102;#103;#104;#105;"; // Válidos
	validChar += "#8;#9;#13;#16;#27;#36;#46;"; // Controle
	/* validChar += "#45;"; // Especiais para o campo */
	if (validChar.indexOf("#" + String(event.keyCode + ";")) == -1) {
	    event.returnValue = false;
	}
	vlrCampo = objeto.value;
	if	(type == 'CNPJ') {
		if	(tecla.keyCode != 8) {
			if 	(vlrCampo.length == 2 || vlrCampo.length == 6) {
				vlrCampo += '.';
				objeto.value = vlrCampo;
			}
			if 	(vlrCampo.length == 10) {
				vlrCampo += '/';
				objeto.value = vlrCampo;
			}
			if 	(vlrCampo.length == 15) {
				vlrCampo += '-';
				objeto.value = vlrCampo;
			}
		}
	} else {
		if	(tecla.keyCode != 8) {
			if 	(vlrCampo.length == 3 || vlrCampo.length == 7) {
				vlrCampo += '.';
				objeto.value = vlrCampo;
			}
			if 	(vlrCampo.length == 11) {
				vlrCampo += '-';
				objeto.value = vlrCampo;
			}
		}
	}
	return true;
}


/*****************************************************************************
/* Utilização: onBlur="validaCNPJ(this.value,obrigatorio)"
/****************************************************************************/
function validaCNPJ(par,obrigatorio) {

	var temErro = false;
	CNPJ = par;
	if	(obrigatorio && CNPJ.length == 0) {
		erro = "Você deve informar o CNPJ";
	} else {
		if	(CNPJ.length == 0) {
			return true;
		}
		erro = new String;
		if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! \n\n"; 
		if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")) {
			if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! \n\n";
		}
	
	     //substituir os caracteres que não são números
	   	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 verificação de CNPJ suporta apenas números! \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 +="CNPJ Inválido!";
	    }
	    if (erro.length > 0){
	       alert(erro);
	       document.forms[0].cnpj.focus();
	       return false;
	    } 
	}
    return true;
}


/*****************************************************************************
/* Utilização: onBlur="validaCPF(this.value)"
/****************************************************************************/
function validaCPF(par) { 
  
 	if	(par != '') {
		var i; 
		var errorMsg = '';
		s = par.replace(".","");
		s = s.replace(".","");
		s = s.replace("-","");
		var c = s.substr(0,9); 
		var dv = s.substr(9,2); 
		var d1 = 0; 
		for (i = 0; i < 9; i++) { 
			d1 += c.charAt(i)*(10-i); 
		} 
		  
		if (d1 == 0){ 
			errorMsg = "CPF Invalido"; 
		} 
		  
		d1 = 11 - (d1 % 11); 
		if (d1 > 9) d1 = 0; 
		if (dv.charAt(0) != d1)  { 
	  		errorMsg = "CPF Invalido";
		} 
		  
		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) { 
		  	errorMsg = "CPF Invalido"; 
		} 
		if (errorMsg.length > 0) {
			alert(errorMsg);
			document.forms[0].cpf.focus();
		} else {
			return true; 
		}
	}
	return true;
} 

/*****************************************************************************
/* Utilização: onBlur="validaEmail(this)" tem que ter id mesmo que nome
/****************************************************************************/
function validaEmail(par,field) {
	var email = par.value;
	var fieldName = par.name;
	if	(email.length > 0) {
		if	(email.indexOf(".") == -1 || email.indexOf("@") == -1) {
			alert("Endereço de E-mail Inválido");
			document.getElementById(fieldName).focus();
			return false;
		}
	}
	return true;
}

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraData(this, event)"
/****************************************************************************/
function mascaraData(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var data = objeto.value;
	if	(tecla == 113) { /* data atual */
		var dataJava = new Date();
		var ano = dataJava.getFullYear();
		var mes = dataJava.getMonth()+1;
		mes = mes.toString();
		if	(mes.length == 1) {
			mes = "0" + mes;
		}
		var dia = dataJava.getDate();
		dia = dia.toString();
		if	(dia.length == 1) {
			dia = "0" + dia;
		}
		dataFormatada = dia+'/'+mes+'/'+ano;
		objeto.value = dataFormatada;
		return true;
	} 
	
	if 	(data.length >= 10) {
		if	(tecla != 9 && tecla != 13 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) {
			return false;
		} else {
			return true;
		}
	}
	
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	((data.length == 2 || data.length == 5) && tecla != 8) {
			data += '/';
			objeto.value = data;
		}
		return true;
	} else {
		return false;
	} 
}

/*****************************************************************************
/* Utilização: onKeyDown="mascaraHora(this, event)"
/****************************************************************************/
function mascaraHora(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var hora = objeto.value;
	
	if	(objeto.selectionEnd == objeto.value.length && objeto.alt != "1") {
		objeto.value = "";
		objeto.alt = "1";
	}

	if	(tecla != 13) {
		if 	(hora.length >= 5) {
			if	(tecla != 9 && tecla != 46 && tecla != 8 && tecla != 16 && tecla != 36 && tecla != 13) {
				return false;
			} else {
				return true;
			}
		}
		if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
			(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
			if 	(hora.length == 2 && tecla != 8) {
				hora += ':';
				objeto.value = hora;
			}
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}


/*****************************************************************************
/* Utilização: onBlur="return validaCPF(parametro)"
/****************************************************************************/
 function validaCPF(objeto) {

	 var i;
	 s = objeto.value;
	 var c = s.substr(0,3);
	 c = c + s.substr(4,3);
	 c = c + s.substr(8,3);
	 var dv = s.substr(12,2);
	 var d1 = 0;
	 for (i = 0; i < 9; i++) {
		 d1 += c.charAt(i)*(10-i);
	 }
	 
	 if (d1 == 0) {
		 alert("CPF Invalido!");
		 return false;
	 }
  
	 d1 = 11 - (d1 % 11);
	 if (d1 > 9) d1 = 0;
	 if (dv.charAt(0) != d1) {
		 alert("CPF Invalido!");
		 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 Invalido!");
		 return false;
	 }
  
	 return true;
 }
 
 
/*****************************************************************************
/* Utilização: formatarData(parametro)
/****************************************************************************/
function formatarData(data) {
	var saida = "";
	var tmp;
	tmp = "" + data.getDate();
	if( tmp.length == 1 ) {
		tmp = "0" + tmp;
	}
	saida = tmp + "/";
	tmp = "" + (data.getMonth() + 1);
	if( tmp.length == 1 ) {
		tmp = "0" + tmp;
	}
	saida = saida + tmp +"/";
	tmp = "" + data.getYear();
	for( var i = 0 ; i < (4 - tmp.length) ; i++ ) {
		saida = saida + "0";
	}
	saida = saida + tmp;
	return(saida);
}

/*****************************************************************************
/* Utilização: toFloat(parameter) - Converter .value em valor para soma
/****************************************************************************/
function toFloat(parameter) {
	var ret;
	var tmp = parameter.indexOf(",");
	var tmp2;
	if	(tmp != -1) {  // Se tem virgula
		tmp2 = parameter.replace('.','');
		tmp2 = tmp2.replace(',','.');
		ret = tmp2;
	} else {
		tmp = parameter.indexOf(".");
		if	(tmp != -1) {  // Se tem virugla
			ret = parameter;
		} else {
			if	(parameter == 0) {
				ret = 0;
			} else {
				ret = parseFloat(parameter+'.00');
			}
		}
	}
	return ret;
}

/*****************************************************************************
/* Utilização: numberToValueForm(parameter)
/****************************************************************************/
function numberToValueForm(parameter) {
	var valor = parameter.toString();
	var fponto;
	var sponto;
	var ret;                             
	valor = valor.replace(".",",");    	 
	fponto = valor.indexOf(',')-4;       
	sponto = valor.indexOf(',')-7;
	ret = valor.substr(0,1);
	for (i = 1; i < valor.length; i++) {
		ret = ret + valor.substr(i,1);
		if	(i == fponto || i == sponto) {
			ret = ret + '.';
		}
	}
	/* Verifica se os decimais tem 2 algarismos */
	var decimais = valor.substr(valor.indexOf(',')+1,3);
	alert(decimais);
	if	(decimais.length == 1) {
		ret = ret+'0';
	}
	return ret;
}

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraRef(this, event)" 
 * 19/06/2009
/****************************************************************************/
function mascaraRef(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var ref = objeto.value;
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	(ref.length == 2 && tecla != 8) {
			ref += '/';
			objeto.value = ref;
		}
		return true;
	} else {
		return false;
	} 
}

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraPlaca(this, event)" - 21/07/2009
/****************************************************************************/
function mascaraPlaca(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var placa = objeto.value;
	if 	(placa.length == 3) {
		placa += ' ';
		objeto.value = placa;
		return true;
	}
	if	(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) { /* Controle volta sem validar */
		return true;
	}

	if 	(placa.length >= 8) {
		if	(tecla != 9 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) {
			return false;
		} else {
			return true;
		}
	}
	
	if 	( (placa.length >= 0 && placa.length < 3) && tecla != 8) { /* posição 1-3 */
		if	( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ) {
			objeto.value = placa;
		 	return false;
		} else {
			objeto.value = placa;
			return true;
		}
	}

	if 	( (placa.length > 3) && tecla != 8) { /* posição 1-3 */
		if	( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ) {
			objeto.value = placa;
		 	return true;
		} else {
			objeto.value = placa;
			return false;
		}
	}
}


/*****************************************************************************
/* Utilização: onKeyDown="return mascaraAnoModelo(this, event)" - 21/07/2009
/****************************************************************************/
function mascaraAnoModelo(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var ano = objeto.value;
	if 	(ano.length >= 8) {
		if	(tecla != 9 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) {
			return false;
		} else {
			return true;
		}
	}
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	(ano.length == 4 && tecla != 8) {
			ano += '/';
			objeto.value = ano;
		}
		return true;
	} else {
		return false;
	} 
}

/*****************************************************************************
/* Utilização: if	(!checkCPF(document.cadastroForm.cpf.value)) { na checkForm
/****************************************************************************/
function checkCPF(pCPF) {
	cpf = pCPF.replace(".","");
	cpf = cpf.replace(".","");
	cpf = cpf.replace(".","");
	cpf = cpf.replace("-","");

	// Aqui começa a checagem do cpf
	var POSICAO, I, SOMA, DV, DV_INFORMADO;
	var DIGITO = new Array(10);
	DV_INFORMADO = cpf.substr(9, 2); // Retira os dois últimos dígitos do número informado

	// Desemembra o número do cpf na array DIGITO
	for (I=0; I<=8; I++) {
	  DIGITO[I] = cpf.substr( I, 1);
	}
	
	// Calcula o valor do 10º dígito da verificação
	POSICAO = 10;
	SOMA = 0;
	   for (I=0; I<=8; I++) {
	      SOMA = SOMA + DIGITO[I] * POSICAO;
	      POSICAO = POSICAO - 1;
	   }
	DIGITO[9] = SOMA % 11;
	   if (DIGITO[9] < 2) {
	        DIGITO[9] = 0;
	}
	   else{
	       DIGITO[9] = 11 - DIGITO[9];
	}
	
	// Calcula o valor do 11º dígito da verificação
	POSICAO = 11;
	SOMA = 0;
	   for (I=0; I<=9; I++) {
	      SOMA = SOMA + DIGITO[I] * POSICAO;
	      POSICAO = POSICAO - 1;
	   }
	DIGITO[10] = SOMA % 11;
	   if (DIGITO[10] < 2) {
	        DIGITO[10] = 0;
	   }
	   else {
	        DIGITO[10] = 11 - DIGITO[10];
	   }
	
	// Verifica se os valores dos dígitos verificadores conferem
	DV = DIGITO[9] * 10 + DIGITO[10];
	   if (DV != DV_INFORMADO) {
	      return false;
	   }
	return true;
}
