
// Limpa o valor do campo
function limpa_campo(campo){
	if (campo == 1) {
		document.tipo_veiculo.tipo_preco0.checked = true;
		document.tipo_veiculo.valor_preco2.value = '';	
	}
	if (campo== 2 ){
		document.tipo_veiculo.tipo_preco1.checked = true;
		document.tipo_veiculo.valor_preco1.value = '';
	}

}

//Formata número tipo moeda 
//Pode ser usado de dois modos:
//1 - Para formatar o valor de um campo deve-se usar o evento onKeyDown. Exemplo: onKeydown="FormataValor('',this,event,20)"
//2 - Para formatar o valor de uma variavel, deve-se usar a chamada FormataValor(variavel,'','',20);
function FormataValor(valorvariavel,valorcampo,teclapres,tammax) 
{
	
	// Verificação se valor por campo ou variavel.
	var tecla = teclapres.keyCode;
		
	if (!(tecla == 8 || (tecla >= 48 && tecla <= 57) || 
	(tecla >= 96 && tecla <= 105 || tecla == 188 || tecla == 9 || tecla == 190 || tecla == 194 || tecla == 110)))
		{
			valorcampo.value = '';
			teclapres.keyCode = 46;
		}
	
	// Trava de tamanho, para não executar a rotina novamente.
	if (valorvariavel=='')
	{
		var trava = "";	
		trava = valorcampo.value;				
		if (trava.length >= tammax)
		{			
			return false;		
		}
	}
	
	
	// Rotina de verificação de Decimal para acrescentar 00. 
	if (valorvariavel!=''){		
		var Decimal = false;
		var ValorDecimal = valorvariavel;				
		for(i = 0; i < ValorDecimal.length; i++) {
			if (isNaN(ValorDecimal.substring(i, i+1))) {
				if (ValorDecimal.substring(i, i+1) == '.' || ValorDecimal.substring(i, i+1) == ',' ) 
				{					
					var Decimal = true;
				}
			}		
		}		
		if (!(Decimal))
		{		
		valorvariavel+='00'						
		}
	}	
		
	if (valorvariavel==''){
		vr = LimparValor(valorcampo.value,"0123456789");
		var retorno = valorcampo.value
		tam = vr.length;	
		if (tam < tammax && tecla != 8){tam = vr.length + 1 ;}
		if (tecla == 8){tam = tam - 1 ;}
	}
	else{
		vr = LimparValor(valorvariavel,"0123456789");
		var retorno = valorvariavel
		tam = vr.length;	
	}		
	
	// Rotina de Formatacao.
	if ( tam > (tammax-2) || valorvariavel != '' || tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){		
		if ( tam <= 2 ){retorno = vr;}		
		if ((tam >= 2) && (tam <= 5)){retorno = vr.substr(0, tam - 2) + ',' + vr.substr(tam - 2, tam );}
		if ((tam >= 6) && (tam <= 8)){retorno = vr.substr(0, tam - 5) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam) ;}
		if ((tam >= 9) && (tam <= 11)){retorno = vr.substr(0, tam - 8) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam ) ; }
		if ((tam >= 12) && (tam <= 14)){retorno = 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);}
		if ((tam >= 15) && (tam <= 17)){retorno = vr.substr(0, tam - 14) + '.' + vr.substr(tam - 14, 3) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3 ) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);}
	}

	
	// Retorno em campo ou variavel.
	if (valorvariavel==''){
		valorcampo.value = retorno;		
	}
	else{		
		return retorno;		
	}			
}

// retira caracteres invalidos da string de valor
function LimparValor(valor, validos) {
var result = "";
var aux;
for (var i=0; i < valor.length; i++) {
aux = validos.indexOf(valor.substring(i, i+1));
if (aux>=0) {
result += aux;
}
}
return result;
}





//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function teste(valorvariavel,valorcampo,teclapres,tammax) 
{
	
	// Verificação se valor por campo ou variavel.
	var tecla = teclapres.keyCode;
		
	if (!(tecla == 8 || (tecla >= 48 && tecla <= 57) || 
	(tecla >= 96 && tecla <= 105 || tecla == 188 || tecla == 9 || tecla == 190 || tecla == 194 || tecla == 110)))
		{
			valorcampo.value = '';
			teclapres.keyCode = 46;
		}
	
	// Trava de tamanho, para não executar a rotina novamente.
	if (valorvariavel=='')
	{
		var trava = "";	
		trava = valorcampo.value;				
		if (trava.length >= tammax)
		{			
			return false;		
		}
	}
}

    /***
    * Descrição.: formata um campo do formulário de
    * acordo com a máscara informada...
	* <form name="myForm">
    *
	* <input type="text" name="str_tel" maxlength="14" size="20" onkeypress="return txtBoxFormat(document.myForm, 'str_tel', '(99) 9999-9999', event);">
	*
    *<input type="text" name="str_cc" maxlength="6" size="10" onkeypress="return txtBoxFormat(document.myForm, 'str_cc', '9999-!', event);">
    *
	*<input type="text" name="str_cpf" maxlength="14" size="20" onkeypress="return txtBoxFormat(document.myForm, 'str_cpf', '999.999.999-99', event);">
	*
	*<input type="text" name="str_cep" maxlength="09" size="10"  onkeypress="return txtBoxFormat(document.myForm, 'str_cep', '99999-999', event);">	
	*
	* Observação: As máscaras podem ser representadas
    * como os exemplos abaixo:
    * CEP -> 99999-999
    * CPF -> 999.999.999-99
    * CNPJ -> 99.999.999/9999-99
    * C/C -> 999999-!
    * Tel -> (99) 9999-9999
    ***/
    function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
      var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

      if(navigator.appVersion.indexOf("MSIE")!= -1)
		 nTecla = evtKeyPress.keyCode;
	else
		nTecla = evtKeyPress.which;
			
      sValue = objForm[strField].value;

      // Limpa todos os caracteres de formatação que
      // já estiverem no campo.
      sValue = sValue.toString().replace( "-", "" );
      sValue = sValue.toString().replace( "-", "" );
      sValue = sValue.toString().replace( ".", "" );
      sValue = sValue.toString().replace( ".", "" );
      sValue = sValue.toString().replace( "/", "" );
      sValue = sValue.toString().replace( "/", "" );
      sValue = sValue.toString().replace( "(", "" );
      sValue = sValue.toString().replace( "(", "" );
      sValue = sValue.toString().replace( ")", "" );
      sValue = sValue.toString().replace( ")", "" );
      sValue = sValue.toString().replace( " ", "" );
      sValue = sValue.toString().replace( " ", "" );
      fldLen = sValue.length;
      mskLen = sMask.length;

      i = 0;
      nCount = 0;
      sCod = "";
      mskLen = fldLen;

      while (i <= mskLen) {
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

        if (bolMask) {
          sCod += sMask.charAt(i);
          mskLen++; }
        else {
          sCod += sValue.charAt(nCount);
          nCount++;
        }

        i++;
      }

      objForm[strField].value = sCod;

      if (nTecla != 8) { // backspace
        if (sMask.charAt(i-1) == "9") { // apenas números...
          return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
        else { // qualquer caracter...
          return true;
        } }
      else {
        return true;
      }
    }





function teste_email(str) {
		//verifica se o e-mail digitado é válido 
		var strAlfa = "ABCDEFGHIJKLMNOPQRSTUWVXYZ";
		var strNum = "0123456789";
		var strSpecial = "_.-@";
		var strValid = strAlfa + strNum + strSpecial;
		
		if (str != null){
				var email = str.toUpperCase();
				//verifica se os caracteres do e-mail são válidos
				for (i=0; i<email.length; i++) {
					if (strValid.indexOf(email.charAt(i)) < 0) {
						return false;
					}
				}
		
				var strC = "@."; 
				//verifica se o e-mail contém @ e . (caracteres obrigatórios)
				for (i=0; i<strC.length; i++) {
					if (email.indexOf(strC.charAt(i)) < 0) 
						return false;
				}
		
				if (email.indexOf("@") != email.lastIndexOf("@"))
					return false;
				if (email.indexOf("@") > email.lastIndexOf("."))
					return false;
				if (email.charAt(email.indexOf("@") + 1) == ".")
					return false;
				if (!(email.indexOf("@") != 0 && email.lastIndexOf(".") != email.length-1))
					return false;
		}					
		return true;
}