function $(id) {
	return document.getElementById(id);
}

function mostra(show, hide){
	$(hide).style.display = "none";
	$(show).style.display = "block";
}

function apaga(id){
	$(id).value="";
}

function aparece(id){
	var campo = $(id);
	if( campo.value.length == 0 ) {
		campo.value = "Digite o Fabricante ou Modelo que você procura!";
	}
}

// Abre PopUp
function abrirpopup(url,ww,hh,nome) {

    var w = 1024, h = 768;

    if (document.all || document.layers) {
       w = screen.availWidth;
       h = screen.availHeight;
    }

    var popW = ww, popH = hh;
    var leftPos = (w-popW)/2, topPos = (h-popH)/2;
    	
    window.open(''+url+'',''+nome+'','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ',toolbar=no,menubar=no,scrollbars=no,resizable=no,status=no');

}

function BomDia()
  {
    var months=new Array(13);
    months[1]="Janeiro";
    months[2]="Fevereiro";
    months[3]="Marco";
    months[4]="Abril";
    months[5]="Maio";
    months[6]="Junho";
    months[7]="Julho";
    months[8]="Agosto";
    months[9]="Setembro";
    months[10]="Outubro";
    months[11]="Novembro";
    months[12]="Dezembro";
    var time=new Date();
    var lmonth=months[time.getMonth() + 1];
    var date=time.getDate();
    var year=time.getYear();
    if (year < 2000)    // Y2K Fix, Isaac Powell
    year = year + 1900; // http://onyx.idbsu.edu/~ipowell
    document.write("<b>Hoje &eacute; " + date + " de ");
    document.write(lmonth + " de " + year + "</b>");
  }  

function Verifica_Email(campo){

   var email = campo.value;
   var filtro=/^.+@.+\..{2,3}$/;
   if(!filtro.test(email)){
     window.alert("O e-mail informado não é válido!"); 
     campo.focus();
	 return false;
   }
   return true;
}

  
//Função para remover string. Exemplo: cpf = remove(cpf, ".");
function remove(str, sub) {
    i = str.indexOf(sub);
    r = "";
    if (i == -1) return str;
    r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
    return r;
}
  
function formatar(src, mask){ 
//Formata para os diversos tipos
/*
CNPJ - OnKeyPress="formatar(this, '##.###.###/####-##')"
CPF - OnKeyPress="formatar(this, '###.###.###-##')"
DATA - OnKeyPress="formatar(this, '##/##/####')"
*/
    if (SoNumeros(src) == true) { 
       var i = src.value.length;
       var saida = mask.substring(0,1);
       var texto = mask.substring(i);
       if (texto.substring(0,1) != saida){
          src.value += texto.substring(0,1);
       }
    }
}

function currencyFormat(fld, milSep, decSep, e){
//Formata o campo numerico
//onKeyPress="return(currencyFormat(this,'.',',',event));" 
//onKeyPress="return(currencyFormat(this,',','.',event));"
  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) return true;  // Enter
  if (whichCode == 8) return true;  // Delete
  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 = '';
  if (len == 1) fld.value = '0'+ decSep + '0' + aux;
  if (len == 2) fld.value = '0'+ decSep + aux;
  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;
}

function SoNumeros(e){
//Permite somente numeros
/*
onKeyPress="return SoNumeros(event);"
*/
 if (document.all) // Internet Explorer
  var tecla = event.keyCode;
 else if(document.layers) // Nestcape
  var tecla = e.which;
  if (tecla > 47 && tecla < 58) // numeros de 0 a 9 e "-"
   return true;
  else
   {
    if (tecla != 8) // backspace
     e.keyCode = 0;
     //return false;
    else
     return true;
   }
}	

function Limpar(valor, validos) {
// retira caracteres invalidos da string
    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 TrocaFotoAnuncio(idFoto){ 

    document.getElementById('ctl00_ContentPagina_ImgAnuncio_1').className = 'pequena';
    document.getElementById('ctl00_ContentPagina_ImgAnuncio_2').className = 'pequena';
    document.getElementById('ctl00_ContentPagina_ImgAnuncio_3').className = 'pequena';
    document.getElementById('ctl00_ContentPagina_ImgAnuncio_4').className = 'pequena';
    document.getElementById('ctl00_ContentPagina_ImgAnuncio_5').className = 'pequena';
    
    document.getElementById('ctl00_ContentPagina_ImgAnuncio_' + idFoto).className = 'grande';

} 

function FormataMoeda(campo, separador_milhar, separador_decimal, tecla) {
//Formata número tipo moeda usando o evento onKeyPress
/*
onKeyPress="return FormataMoeda(this,'.',',',event);"
*/
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? tecla.which : tecla.keyCode;

	if (whichCode == 13) return true; // Tecla Enter
	if (whichCode == 8) return true; // Tecla Delete
	key = String.fromCharCode(whichCode); // Pegando o valor digitado
	if (strCheck.indexOf(key) == -1) return false; // Valor inválido (não inteiro)
	len = campo.value.length;
	for(i = 0; i < len; i++)
	if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != separador_decimal)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) campo.value = '';
	if (len == 1) campo.value = '0'+ separador_decimal + '0' + aux;
	if (len == 2) campo.value = '0'+ separador_decimal + aux;

	if (len > 2) {
		aux2 = '';

		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += separador_milhar;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}

		campo.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		campo.value += aux2.charAt(i);
		campo.value += separador_decimal + aux.substr(len - 2, len);
	}

	return false;
}

function exibeLoading() {

    document.getElementById('Loading').className = 'exibeLoading';
    document.getElementById('bloqueia').className = 'bloqueia';
    
}

function max(txarea) 
{ 
  total = 255; 
  tam = txarea.value.length; 
  str=""; 
  str=str+tam; 
  Digitado.innerHTML = str; 
  Restante.innerHTML = total - str; 

  if (tam > total){ 
     aux = txarea.value; 
     txarea.value = aux.substring(0,total); 
     Digitado.innerHTML = total 
     Restante.innerHTML = 0 
  } 
} 
