function OnlyNumbers(e){
var tecla=new Number();

if(window.event) {
tecla = e.keyCode;
}
else if(e.which) {
tecla = e.which;
}
else {
return true;
}

if(((tecla < 48) || (tecla > 57)) && (tecla!=8) )
{
return false;
}

}

//validacao genÃ¯Â¿Â½rica de pesquisas com os mesmos campos
function validaPesquisaComMunicipio( nome, uf, municipio ){ //nome = id
		
 	 	var idx = document.getElementById(uf).selectedIndex;
 	 	
 	 	 	 	
 	 	if( document.getElementById(uf).options[idx].value == '' 
 	 		&& ehVazio(document.getElementById(nome).value))
 	 	{
 	 			alert("O nome Ã© obrigatÃ³rio se o campo UF nÃ£o estiver preenchido.") 
	 	 	  	return false;	
 	 	
 	 		 		
 	 	}
 	 	else if(document.getElementById(uf).options[idx].value != ''
 	 	  && ehVazio(document.getElementById(nome).value) )
 	 	{
 	 	  
 	 	  	var idx2 = document.getElementById(municipio).selectedIndex;
 	 	  	if(document.getElementById(municipio).options[idx2].value == '')
 	 	  	{
 	 	  		alert("O municÃ­pio Ã© obrigatÃ³rio se o nome nÃ£o estiver preenchido")
	 	 	  	return false;
 	 	  	} 	 	  	
 	 	  	
 	 	  }	 	 	 
 	 	  return true;
 	 	  
 	 	  	
 	 	  	
}

function validaPesquisaSemMunicipio( nome, uf){ //nome = id
		
 	 	var idx = document.getElementById(uf).selectedIndex;
 	 	if( document.getElementById(uf).options[idx].value == ''
 	 	  && ehVazio(document.getElementById(nome).value) )
 	 	  {
 	 	  	alert("O nome Ã© obrigatÃ³rio se o campo UF nÃ£o estiver preenchido.") 
 	 	  	return false;
 	 	  }	 		
 	 	
 	 	  return true;
 	 	  
 	 	  	
 	 	  	
}

 function criaSelecioneNaSelectUfs(idDaSelect){

  var selectLength = document.getElementById(idDaSelect).length;
  var x=document.getElementById(idDaSelect);
  var posicao = document.getElementById(idDaSelect).selectedIndex;  
  if (x.selectedIndex>=0)
    {
    var y=document.createElement('option');
    y.text='Selecione';
    y.value='-1';
    var sel = x.options[0];
    try
      {
      x.add(y,sel); // standards compliant
      }
    catch(ex)
      {
      x.add(y,0); // IE only
      }
  }

   if((!document.getElementById(idDaSelect).options[posicao].defaultSelected)){
      if(document.getElementById('pesquisarUfPrefeitura_prefeitura_endereco_municipio_id')== null)
      {
         document.getElementById(idDaSelect).selectedIndex = 0;
         }
      }
  }


function validaEndereco(log,bairro,cidade,cep)
{
	
	if(ehVazio(document.getElementById(cep).value))
	{
		alert("O cep Ã© obrigatÃ³rio.");
		return false;
	}
	if(document.getElementById(cidade)== null)
	{
		alert("Selecione uma cidade.");
		return false;
	}
	if( ehVazio(document.getElementById(log).value) )
	{
		alert("O logradouro Ã© obrigatÃ³rio.");
		return false;
	}
	
	else if( ehVazio(document.getElementById(bairro).value) )
	{
		alert("O bairro Ã© obrigatÃ³rio.");
		return false;
	}
	return true;  
}

function validaNomeCNPJ(nome,cnpj,siafi)
{
	
	if( ehVazio(document.getElementById(nome).value) ){
		alert("O nome Ã© obrigatÃ³rio");
		return false;
	}
	else if( ehVazio(document.getElementById(cnpj).value) ){
		alert("O CNPJ Ã© obrigatÃ³rio");
		return false;
	}else if(document.getElementById(siafi)){ 
		if (isNaN(document.getElementById(siafi).value)){
		alert("SIAFI invÃ¡lido");
		return false;
		}
	}
	
	return true;
	
}

function validaDenominacaoCNPJ(nome,cnpj,siafi)
{
	
	if( ehVazio(document.getElementById(nome).value) ){
		alert("A DenominaÃ§Ã£o Ã© obrigatÃ³ria");
		return false;
	}
	else if( ehVazio(document.getElementById(cnpj).value) ){
		alert("O CNPJ Ã© obrigatÃ³rio");
		return false;
	}else if (isNaN(document.getElementById(siafi).value)){
		alert("SIAFI invÃ¡lido");
		return false;
	}
	
	return true;
	
}

function verificaTelefone(ddd,telefone,ramal,fax){
	
	if( (isNaN(document.getElementById(ddd).value))  ||(isNaN(document.getElementById(telefone).value))  ||(isNaN(document.getElementById(ramal).value))  ||(isNaN(document.getElementById(fax).value)) ){
		alert("Informe apenas numeros no campo de telefone");
		return false;	
	}	
}

function validaNome(nome)
{
	
	if( ehVazio(document.getElementById(nome).value) ){
		alert("nome Ã© obrigatÃ³rio");
		return false;
	}
	
	return true;
	
}

function validaUnidadePublica(nome,tipo,mantenedora,cnpj)
{
	var idx = document.getElementById(tipo).selectedIndex;
	if( ! validaDenominacaoCNPJ(nome,cnpj) )
		return false;
	if( document.getElementById(tipo).selectedIndex != 0)
		if( ehVazio(document.getElementsByName(mantenedora)[idx-1].value) ){
			alert("O campo entidade mantenedora Ã¡ obrigatÃ³rio.");
			return false;
		}
		
	return true;

}

function validaRedePrivada(denominacao,nome,tipo,mantenedora,cnpj,razaoSocial)
{
	
	if ( ehVazio(document.getElementById(denominacao).value ) ){
		alert("O campo nome e obrigatório.");
		return false;
	}
	if ( ehVazio(document.getElementById(razaoSocial).value ) ){
		alert("O campo razão social e obrigatório.");
		return false;
	}
	else if (! validaUnidadePublica(nome,tipo,mantenedora,cnpj) )
		return false;
	return true;

}

function ehVazio( s ){
	if( s == undefined || s == null || s.trim() == '' )
		return true;
	else
		return false;	
}

String.prototype.trim = function()
{
return this.replace(/^\s*/, "").replace(/\s*$/, "");
}



function validaTamanhoString(string,tam){
	if (string.length > tam){
		return false;
	}
	
	return true;
}
//adiciona um elemento na posicao pos da select
function adicionarElementoSelect(select,pos,texto,valor){
	alert('opa');
	var novaOpcao = new Option(texto,valor);
	document.getElementById(select).options[pos] = novaOpcao;
	document.getElementById(select).options[pos] = true;
}

function contaCaracteres(campo, campo_div, maximo_permitido){
if(campo.value != ""){
	total = campo.value.length;
	if(total > maximo_permitido){
		alert("Total excedido");
		campo.value = campo.value.substr(0,maximo_permitido);
		total = total-1;
		document.getElementById(campo_div).innerHTML="Total de caracteres: <b>"+total+"</b>. M&aacute;ximo permitido: " + maximo_permitido+".";
	}
	document.getElementById(campo_div).innerHTML="Total de caracteres: <b>"+total+"</b>. M&aacute;ximo permitido: " + maximo_permitido+".";
}else
	document.getElementById(campo_div).innerHTML="";
}

