function icoText(obj, texto, id)
{
	if(texto == '') {
		document.getElementById(id).innerHTML = "&nbsp;";
	}
	else { 
		document.getElementById(id).innerHTML = texto;
	}
	
	
}

function Contar(Campo){
if((255-Campo.value.length) <= 0){
   alert('Atenção! Você atingiu o limite máximo de caracteres!');
   Campo.value = Campo.value.substr(0,255);
}
document.getElementById("Qtd").innerText = 255-Campo.value.length
}

function openDir( form ) { 

	var newIndex = form.fieldname.selectedIndex; 

	if ( newIndex == 0 ) { 

		alert( "Selecione uma cidade" ); 

	} else { 

		cururl = form.fieldname.options[ newIndex ].value; 

		window.location.assign( cururl ); 

	} 

} 

function atualiza_bairro(){
		var cidade_id = document.getElementById("cidade_id").value;
		if(document.getElementById("bairro_id")){
			document.getElementById("area_novo_bairro").style.display = "none";
			document.getElementById("bairro_id").options.length = 0;
			document.getElementById("bairro_id").options[0] = new Option( document.getElementById("msg_busca_bairro").value, 0, false);
		}
		return;
	}


	function verifica_opcao(valor){
		if(valor == "_NAOLISTADO_"){
			document.getElementById("area_novo_bairro").style.display = "block";
			document.getElementById("bairro_novo").value = document.getElementById("busca_bairro").value;
			document.getElementById("bairro_novo").focus();
		}else{
			document.getElementById("area_novo_bairro").style.display = "none";
		}
		return;
	}
	
	function verifica_zona(){
		var zona_id = document.getElementById("zona_id").value;
		if(document.getElementById("banner_zona_id").value == zona_id){
			document.getElementById("vertical").style.display = "block";
			document.getElementById("horizontal").style.display = "block";
			document.getElementById("posvert").focus();
		}else{
			document.getElementById("vertical").style.display = "none";
			document.getElementById("horizontal").style.display = "none";
		}
		return;
	}
	
	function verifica_categoria(){
		var catpecas_id = document.getElementById("catpecas_id").value;
		var cataces_id = document.getElementById("cataces_id").value;
		if(document.getElementById("class_categoria_id").value == catpecas_id){
			document.getElementById("ano").style.display = "none";
			document.getElementById("combustivel").style.display = "none";
			document.getElementById("cor").style.display = "none";
			document.getElementById("placa").style.display = "none";
		}else if(document.getElementById("class_categoria_id").value == cataces_id){
			document.getElementById("ano").style.display = "none";
			document.getElementById("combustivel").style.display = "none";
			document.getElementById("cor").style.display = "none";
			document.getElementById("placa").style.display = "none";
		}else{
			document.getElementById("ano").style.display = "block";
			document.getElementById("combustivel").style.display = "block";
			document.getElementById("cor").style.display = "block";
			document.getElementById("placa").style.display = "block";
			document.getElementById("nome").focus();
		}
		return;
	}
	
	function formata_pad(campo,mascara){
		var dados  = campo.value;
		var res    = '';
		var c      = 0;
		var limpo  = '';
		var valido = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
		
		for(a=0;a<dados.length;a++){
			car_dados = dados.substr(a,1);
			if(valido.indexOf(car_dados) != -1 ) limpo = limpo + car_dados;
		}		

		for(a=0;a<mascara.length;a++){
			if(a > dados.length ) break;
			car_masc  = mascara.substr(a,1);
			car_dados = limpo.substr(c,1);
			if(car_masc == 9){
				res=res+car_dados; 
				c=c+1;
			}else{
				res=res+car_masc;
			}
		}

		document.getElementById(campo.name).value = res;
	}


	function formata_pad_max(campo,mascara,maximo){
		var dados  = campo.value;
		var res    = '';
		var c      = 0;
		var limpo  = '';
		var valido = '0123456789';
		
		for(a=0;a<dados.length;a++){
			car_dados = dados.substr(a,1);
			if(valido.indexOf(car_dados) != -1 ) limpo = limpo + car_dados;
		}		

		if(limpo>maximo){
				document.getElementById(campo.name).value = maximo;
				return;
		}
		
		for(a=0;a<mascara.length;a++){
			if(a > limpo.length ) break;
			car_masc  = mascara.substr(a,1);
			car_dados = limpo.substr(c,1);
			if(car_masc == 9){
				res=res+car_dados; 
				c=c+1;
			}else{
				res=res+car_masc;
			}
		}

		document.getElementById(campo.name).value = res;
	}

	function formata_data_br(campo,testar,data_atual){
		if(testar==1 && campo.value=="") return;
		formata_val_max(campo,'99/99/9999','31/12/2100',data_atual,'/',testar);
	}

	function formata_horas(campo,testar){
		if(testar==1 && campo.value=="") return;
		formata_val_max(campo,'99:99:99','23:59:59','00:00:00',':',testar);
	}

	function formata_val_max(campo,mascara,limites,preenchimento,separador,testar){
		var dados  = campo.value;
		var res    = '';
		var c      = 0;
		var limpo  = '';
		var valido = '0123456789';
		var max_div = mascara.split(separador);
		    max_div = max_div.length;
		
		if(testar == 1){
			if(dados.length<mascara.length){
				resta = preenchimento.length - dados.length;
				dados = dados + preenchimento.substr(dados.length,resta);
			}
		}
		
		for(a=0;a<dados.length;a++){
			car_dados = dados.substr(a,1);
			if(valido.indexOf(car_dados) != -1 ) limpo = limpo + car_dados;
		}		

		for(a=0;a<mascara.length;a++){
			if(a > dados.length ) break;
			car_masc  = mascara.substr(a,1);
			car_dados = limpo.substr(c,1);
			if(car_masc == 9){
				res=res+car_dados; 
				c=c+1;
			}else if(a < dados.length){
				res=res+car_masc;
			}
		}

		var retorno = res;
		pre_retorno = retorno.split(separador);

		if(pre_retorno.length != 1){
	
			var item_limite = limites.split(separador);
			var item = retorno.split(separador);
			var max = item.length;
			retorno = "";

			for(a=0;a<max;a++){

				if(item[a].length == item_limite[a].length){
					if(item[a] > item_limite[a]) item[a] = item_limite[a];	
					if(a+1 == max) separador = "";
					retorno = retorno + item[a] + separador;
				}else{
					retorno = retorno + item[a];
				}

			}		

		}

		document.getElementById(campo.name).value = retorno;

	}

function formataValorMask(campo)
{
	var campoPostNome = campo.name.replace('mask_',''); 
	var valorFormatatadoBanco = 0;
	formataValor(campo);
	valorFormatatadoBanco = document.getElementById(campo.name).value.replace('.','');
	valorFormatatadoBanco = valorFormatatadoBanco.replace(',','.');
	document.getElementById(campoPostNome).value = valorFormatatadoBanco;
}


function formataValor(campo) {
		var dados  = campo.value;
		var res    = '';
		var c      = 0;
		var b 	   = 5;
		var valido = '0123456789';
		var limpo  = '';
		var mask   = '';
		
		for(a=0;a<dados.length;a++){
			car_dados = dados.substr(a,1);
			if(valido.indexOf(car_dados) != -1 ) limpo = limpo + car_dados;
		}		
	
		c = limpo.length-1;
		if(c>1){
			for(a=0;a<limpo.length;a++){
				if(a==2) res=res+',';
				if(a==b){ 
					res=res+'.';
					b=b+3;
				}
				res=res+'9'; 
			}
			
			var tam = res.length;
			b = tam-1;
			for(n=0;n<tam;n++){
				car_dados = res.substr(b,1);
				mask=mask+car_dados;
				b=b-1;
			}

			c = 0;			
			res = '';
			for(a=0;a<mask.length;a++){
				car_masc  = mask.substr(a,1);
				car_dados = limpo.substr(c,1);
				if(car_masc == 9){
					res=res+car_dados; 
					c=c+1;
				}else{
					res=res+car_masc;
				}
			}

		}else{
			res = limpo;
		}
document.getElementById(campo.name).value = res;
}

function isNumVlr( caractere ) {
	var strValidos = "0123456789.," ;
	if ( strValidos.indexOf( caractere ) == -1 ) return false ;
	return true; 
}

function validaNumero( campo, event ){
	var BACKSPACE=8;
	var key;
	var tecla;
	
	CheckTAB=true;
	if(navigator.appName.indexOf("Netscape")!= -1) 	tecla=event.which;
	else											tecla=event.keyCode;
	
	key = String.fromCharCode(tecla);
	
	if (tecla == 13) return false;
	if (tecla == BACKSPACE) return true;
	
	return(isNumVlr(key));

}


function autofoco(formulario) {
	var field = eval("document."+formulario);
	var ulimo_campo = "";
	var primeiro_campo = "";
	
	if(document.getElementById(formulario)){
			for (i = 0; i < field.length; i++) {
					if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type == "select-one") || (field.elements[i].type.toString().charAt(0) == "s")) {
						if(field.elements[i].name != ""){
							if( field.elements[i].disabled == false ) ultimo_campo = field.elements[i].name;
							if( field.elements[i].disabled == false && primeiro_campo == "" ) primeiro_campo = field.elements[i].name;
						}
					 }
			  }

			eval("document."+formulario+"."+ultimo_campo+".focus()");
		
			eval("document."+formulario+"."+primeiro_campo+".focus()");
	}		
}

function gera_ac(nome)
{
	var date = new Date();
	date.setTime(date.getTime()+675000); 
	var expires = "; expires="+date.toGMTString();
	document.cookie = nome+"=1"+expires+"; path=/";
}


function ver_ac(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function grava_tela(acao_volta){
	createCookie('pos_tela',document.body.scrollTop);
	createCookie('acao_antiga',acao_volta);
}

function testa_pre(campos){
//	if(testa_pre('data_ini/data_fim/mes')==false){ return; } Ok quando qualquer campo estiver preenchido.
	var conc="";
	var comp_conc="";
	var cmp=campos.split("/");
	for(c=0;c<=cmp.length-1;c++){
		comp_conc=comp_conc+"0";
		if(document.getElementById(cmp[c]).value.length==0){conc=conc+"0";}else{ if(document.getElementById(cmp[c]).value=="0"){conc=conc+"0";}else{conc=conc+"1";}}
	}
	if(conc==comp_conc){alert("Preencha algum campo para pesquisar!"); return false;}
	return true;
}

function valida_email(email){
	var ok = 1;
	if(email=='') return false;
	if(email.indexOf('@')==-1) ok=0;
	if(email.indexOf('.')==-1) ok=0;
	if(email.length<8) ok=0;
	
	if(ok) return true; else{ alert("O email está incorreto!"); return false;}
}

function popup(URL, w, h){
	var x = (screen.availWidth - w)/2;
	var y = (screen.availHeight - h)/2;
	window.open(URL,'popup',"toolbar=0,location=0,address=0,directories=0,status=0,menubar=0,resizable=0,copyhistory=0,maximized=0,scrollbars=1,width="+w+",height="+h+",left="+x+",top="+y+"")
}
