	function carregando(acao){
		if (acao==1){
			bt_alterar.style.display = "none";							
			aguarde.style.display = "";	
		}else{
			bt_alterar.style.display = "";
			aguarde.style.display = "none";		
		}
	}


	function aguarde2(acao){
		if (acao==1){
			aguarde_carregando.style.display = "";
		}else{
			aguarde_carregando.style.display = "none";	
		}
	}


function pula(origem, tamanho, destino) {
	if ((origem.value.length == tamanho) && destino)
		if (destino == 'CepPesquisa'){
			FreteCalcular();
		}else{
			destino.focus();
		}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


			//onKeyPress="FormataCep(this.value,this.name)"
			function FormataPeso(campo,tammax,teclapres) {
				if (event.keyCode < 48 || event.keyCode > 57) {
					event.returnValue = false;
					//alert("Informe apenas Números!")
				}else{
					var tecla = teclapres.keyCode;
					vr = event.srcElement.value;
					vr = vr.replace( "/", "" );
					vr = vr.replace( "/", "" );
					vr = vr.replace( ",", "" );
					vr = vr.replace( ".", "" );
					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 >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
						if ( tam <= 3 ){ 
							// document.form[campo].value = vr ; 
							event.srcElement.value = vr ; 
						}
						if ( (tam > 3) && (tam <= 6) ){
							//document.form[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; 
							event.srcElement.value = vr.substr( 0, tam - 3 ) + '.' + vr.substr( tam - 3, tam ) ; 
						}
				
					}
				}
			}


			// Formata o Campo Valor
			function FormataValor(campo,tammax,teclapres) {
				if (event.keyCode < 48 || event.keyCode > 57) {
					event.returnValue = false;
					alert("Informe apenas Números!")
				}else{
					var tecla = teclapres.keyCode;
					vr = event.srcElement.value;
					vr = vr.replace( "/", "" );
					vr = vr.replace( "/", "" );
					vr = vr.replace( ",", "" );
					vr = vr.replace( ".", "" );
					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 >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
						if ( tam <= 2 ){ 
							event.srcElement.value = vr ; 
						}
						if ( (tam > 2) && (tam <= 6) ){
							event.srcElement.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; 
						}

						if ( (tam >= 6) && (tam <= 8) ){
							//document.form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
							event.srcElement.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
						}
						if ( (tam >= 9) && (tam <= 11) ){
							//document.form[campo].value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
							event.srcElement.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
						}

				
					}
				}
			}



			//onFocusOut="FormataEmail(this.name,this.value);"
			emailRE = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
			function FormataEmail(campo){
				  var email = document.getElementById(campo).value;
				  if (emailRE.test(email)) {
					 return true;
				  }else{
					  alert("Email inválido\n\nNão use acentos, espaços, etc");
					  return false;
				  }
			}


			// Formata o Campo CPF 
			// onKeyPress="FormataCpf('cpf_cnpj',11,event)"
			 function FormataCpf(campo,tammax,teclapres) {
				if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false,alert('SÓ NUMEROS!')

				 var tecla = teclapres.keyCode;
				 vr = event.srcElement.value;
				 vr = vr.replace( "/", "" );
				 vr = vr.replace( "/", "" );
				 vr = vr.replace( ",", "" );
				 vr = vr.replace( ".", "" );
				 vr = vr.replace( ".", "" );
				 vr = vr.replace( ".", "" );
				 vr = vr.replace( ".", "" );
				 vr = vr.replace( "-", "" );
				 vr = vr.replace( "-", "" );
				 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 >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
				  if ( tam <= 2 ){ 
					event.srcElement.value = vr ; }
				   if ( (tam > 2) && (tam <= 5) ){
					event.srcElement.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
				   if ( (tam >= 6) && (tam <= 8) ){
					event.srcElement.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
				   if ( (tam >= 9) && (tam <= 11) ){
					event.srcElement.value = 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) ){
					event.srcElement.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 ) ; }
				   if ( (tam >= 15) && (tam <= 17) ){
					event.srcElement.value = 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 ) ;}
				 }  
				// Chama Função para Validar o CPF/CNPJ
			 }	
			 
			 function ValidaCpf(valor){
					var i;
					s = limpa_string(s);
					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) return false;
					d1 = 11 - (d1 % 11);
					if (d1 > 9) d1 = 0;
					if (dv.charAt(0) != d1)
					{
						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)
					{
						return false;
					}
						return true;
			 }
			function ValidaCnpj(valor){
				var i;
				s = limpa_string(s);
				var c = s.substr(0,12);
				var dv = s.substr(12,2);
				var d1 = 0;
				for (i = 0; i < 12; i++)
				{
					d1 += c.charAt(11-i)*(2+(i % 8));
				}
					if (d1 == 0) return false;
					d1 = 11 - (d1 % 11);
				if (d1 > 9) d1 = 0;
				if (dv.charAt(0) != d1)
				{
					return false;
				}
			
				d1 *= 2;
				for (i = 0; i < 12; i++)
				{
					d1 += c.charAt(11-i)*(2+((i+1) % 8));
				}
				d1 = 11 - (d1 % 11);
				if (d1 > 9) d1 = 0;
				if (dv.charAt(1) != d1)
				{
					return false;
				}
					return true;			
			}			 
			 
			 function FormataCnpj(campo,tammax,teclapres) {
				if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false,alert('SÓ NUMEROS!')

				 var tecla = teclapres.keyCode;
				 vr = event.srcElement.value;
				 vr = vr.replace( "/", "" );
				 vr = vr.replace( "/", "" );
				 vr = vr.replace( ",", "" );
				 vr = vr.replace( ".", "" );
				 vr = vr.replace( ".", "" );
				 vr = vr.replace( ".", "" );
				 vr = vr.replace( ".", "" );
				 vr = vr.replace( "-", "" );
				 vr = vr.replace( "-", "" );
				 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 >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
				  if ( tam <= 2 ){ 
					event.srcElement.value = vr ; }
				   if ( (tam > 2) && (tam <= 5) ){
					event.srcElement.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
				   if ( (tam >= 6) && (tam <= 8) ){
					event.srcElement.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
				   if ( (tam >= 9) && (tam <= 11) ){
					event.srcElement.value = 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) ){
					event.srcElement.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 ) ; }
				   if ( (tam >= 15) && (tam <= 17) ){
					event.srcElement.value = 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 ) ;}
				 }  
				// Chama Função para Validar o CPF/CNPJ

			 }
			 
			 
			function limpa_string(S){
			// Deixa so' os digitos no numero
			var Digitos = "0123456789";
			var temp = "";
			var digito = "";
				for (var i=0; i<S.length; i++){
				  digito = S.charAt(i);
				  if (Digitos.indexOf(digito)>=0){temp=temp+digito}
				}
				return temp
			}


			 
			// Formata o Campo CEP
			//onKeyPress="FormataCep(this.value,this.name)"
			function FormataCep(cep,campo){ 
				if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false,alert('SÓ NUMEROS!')
				var mycep = ''; 
				mycep = mycep + cep; 
				if (mycep.length == 5){ 
				mycep = mycep + '-'; 
				document.all[campo].value = mycep; 
				} 
			}

			// Formata o Campo Telefone
			// onKeyUp="FormataFone(this.value,this.name)"
			function FormataFone(telefone,campo){ 
				if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false,alert('SÓ NUMEROS!')
				if (window.event.keyCode != 8 ){
					  var mytelefone = ''; 
					  mytelefone = mytelefone + telefone; 
					  if (mytelefone.length == 1){ 
						if (mytelefone == 0){
						  alert("NÃO DIGITE 0 (ZERO) NO INICIO DO DDD")
						  document.forms[0][campo].value = ''; 
						}else{
						  document.forms[0][campo].value = '('+ mytelefone; 
						}
					  } 
					  if (mytelefone.length == 3){ 
						  mytelefone = mytelefone + ') '; 
						  document.forms[0][campo].value = mytelefone; 
					  } 
					  if (mytelefone.length == 9){ 
						  mytelefone = mytelefone + '-'; 
						  document.forms[0][campo].value = mytelefone; 
					  } 
				  }
			   } 


				function FormataData(Campo,teclapres) {
					if (event.keyCode != 8){
						if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false,alert('SÓ NUMEROS!')						
					}

					var tecla = teclapres.keyCode;
					vr = event.srcElement.value;
					vr = vr.replace( ".", "" );
					vr = vr.replace( "/", "" );
					vr = vr.replace( "/", "" );
					tam = vr.length + 1;
				
					if ( tecla != 9 && tecla != 8 ){
						if ( tam > 3 && tam < 5 )
						{
							event.srcElement.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
						}
						if ( tam >= 5 && tam <= 10 )
						{
							event.srcElement.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
						 }
					}
				}
				
				function ValidaData(Campo) {
					if (document.forms[0][Campo].value.length != 10){
						alert('Data Inválida!\n\nUse: DD\\MM\\AAAA');
						document.forms[0][Campo].focus();
						return false
					}else{
					var mySplitResult = document.forms[0][Campo].value.split("/");
						// Valida o DIA com 2 digitos
						if (mySplitResult[0].length != 2 || mySplitResult[0] > 31){
							alert('DIA Inválido!\n\nUse: DD\\MM\\AAAA');
							document.forms[0][Campo].focus();
							return false
						}
						// Valida o MES com 2 digitos
						if (mySplitResult[1].length != 2 || mySplitResult[1] > 12){
							alert('MÊS Inválido!\n\nUse: DD\\MM\\AAAA');
							document.forms[0][Campo].focus();
							return false
						}
						// Valida o ANO com 2 digitos
						if (mySplitResult[2].length != 4 || mySplitResult[2] < 2009){
							alert('ANO Inválido!\n\nUse: DD\\MM\\AAAA');
							document.forms[0][Campo].focus();
							return false
						}
					}
				}



         function mascara_fone(campo,data){ 
             var mydata = ''; 
             mydata = mydata + data; 
             if (mydata.length == 4){ 
                 mydata = mydata + '-'; 
                 document.pre[campo].value = mydata; 
             } 
         } 



	  		  



function cleanMask(val) {
	var strCheck = "'[](){}<>=+-*/_|\~`!?@#$%^&:;,.";
	var aux="";
	var i;
	
	for(i=0; i<val.length; i++) {
		if(strCheck.indexOf(val.charAt(i))==-1) {
			aux+=val.charAt(i);
		}
	}
	return aux;
}

function maskCurrency(val, milSep, decSep) {
	var aux="";
	var aux2="";
	
	var i,j;
			
	len = val.length;
	if (len == 0) {
		aux = '';
	} else if (len == 1) {
		aux = '0'+ decSep + '0' + val;
	} else if (len == 2) {
		aux = '0'+ decSep + val;
	} else if (len > 2) {
		aux2 = '';

		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += val.charAt(i);
			j++;
		}
		aux = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--) {
			aux += aux2.charAt(i);
		}
		aux += decSep + val.substr(len - 2, len);
	}	
	return aux;
}
function mask(_mask, val) {
	var i, mki;
	var aux="";
	
	for(i=mki=0; i<val.length; i++, mki++) {
		if(_mask.charAt(mki)=='' || _mask.charAt(mki)=='#' || _mask.charAt(i)==val.charAt(i)) {
			aux+=val.charAt(i);
		} else {
			aux+=_mask.charAt(mki)+val.charAt(i);
			mki++;
		}
	}
	return aux;
}

function maskEvent(field, _mask, event) {
	var key ='';
	var aux='';
	var len=0;
	var i=0;
	var strCheck = '0123456789';
	var rcode = (window.Event) ? event.which : event.keyCode;
	
	if(rcode == 13 || rcode == 0 || field.value.length == _mask.length  ) {
		//Enter
		key=String.fromCharCode(rcode);
				
		if(rcode!=13 && rcode!=0) {
			return false;
		}
		
		return true;
	}
	
	//Get key value from key code
	key=String.fromCharCode(rcode);
	
	if(strCheck.indexOf(key)==-1) {
		//Not a valid key
		return false;
	}
	
	aux=field.value+key;
	//window.alert(aux);
	aux=mask(_mask,aux);
	//window.alert(aux);
	field.value=aux;	
	return false;
}

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 || whichCode == 0) {		//Enter
		return true;  
	}
	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 = '';
	} else if (len == 1) {
		fld.value = '0'+ decSep + '0' + aux;
	} else if (len == 2) {
		fld.value = '0'+ decSep + aux;
	} else 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;
}

