// JavaScript Document

function CarregaCombo(url,campo,campo2,selecionar) {
	//alert(url+" "+campo+" "+campo2+" "+selecionar);
	formCampo = document.getElementById(campo);
	formCampo_valor = formCampo.value;
	formCampo.disabled = false;
	remove_tudo(campo2);
	adicionaItem('Aguarde...','',campo2);

	frame0 = document.getElementById('sframe');
	frame0.src = "inc_popular_combos.php?url="+url+"&LOCAL="+formCampo_valor+"&popular="+campo2+"&selecionar="+selecionar;	
}

function FormataData(Campo, Tecla, Proximo){
	var Tecla = Tecla.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");

	tam = vr.length;
		if (Tecla != 9 && Tecla != 8 && Tecla != 46 && Tecla != 47){
			if (tam >= 2 && tam < 4)
				Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2);
			if (tam >= 4 && tam <= 6)
				Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
			if (tam == 8){
				if(Proximo){
					Proximo.select();
					Proximo.focus();
				}
			}
		}
}

function Tecla(e)
{
	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
		return true;
	else
	{
		if (tecla != 8) // backspace
			event.keyCode = 0;
			//return false;
		else
			return true;
	}
}

function FormataCEP(Campo, Tecla, Proximo){
		var Tecla = Tecla.keyCode;
		var vr = new String(Campo.value);
		vr = vr.replace("-", "");
		tam = vr.length;
	if (Tecla != 45){
		if (tam >= 5 && tam < 8){
			Campo.value = vr.substr(0,5)+'-'+ vr.substr(5,3) 
		}
		
		if (tam == 8){
			if(Proximo){
				Proximo.select();
				Proximo.focus();
			}
		}
	}
}

function adicionaItem(texto,valor,objeto) {
	//alert(texto+" "+valor+" "+objeto);
	linha = document.createElement("OPTION");
	linha.text=texto;
	linha.value=valor;
	
	formObjeto = document.getElementById(objeto)
	formObjeto.add(linha);
	formObjeto.disabled = false;
}

function selecionaCombo(objeto,valor)
{
	var formValor = document.getElementById(valor);
	
	var formObjeto = document.getElementById(objeto);
	var tam = formObjeto.length -1;
	
	while( tam > 0 ) 
	{ 
	    if (parseInt(formObjeto.options[tam].value)  == parseInt(formValor.value) )
		{
			formObjeto[tam].selected = tam;
        }
		tam--;
		
	}
	
}

function remove_tudo(objeto) {
	formOjeto = document.getElementById(objeto);
	var tam = formOjeto.length;
	while( tam > 0 ) { 
		formOjeto.remove(tam-1); 
		tam--;
	}
}

function FormataCPF(Campo, Tecla)
{
  var Tecla = Tecla.keyCode;
 
  var vr = new String(Campo.value);
  vr = vr.replace(".", "");
  vr = vr.replace(".", "");
  vr = vr.replace(".", "");
  vr = vr.replace("-", "");
 
  tam = vr.length;
 
	if (Tecla != 9 && Tecla != 8 && Tecla != 46)
	{
		if (tam >= 3 && tam < 6)
		Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3);

			if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3);

				if (tam >= 10 && tam <= 11)
				Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,2);
   	}
 
}

function Tecla(e)
  {
  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
  return true;
  else
  {
  if (tecla != 8) // backspace
  event.keyCode = 0;
  //return false;
  else
  return true;
  }
  }
