function enviar_formulario(form,ajax,div_atualizar) {
  if(ajax) {
    url = $(form).action;
    new Ajax.Request(
      url, 
      {
        asynchronous:true, 
        evalScripts:true, 
        parameters:Form.serialize(form),
        onLoading:function(request, json){
          Element.show('indicator');
        },
        onComplete: function(transport, json) {
          if(div_atualizar == false || div_atualizar == undefined) {
            if(! json.div_atualizar) {
              alert('ERRO:Não foi passado o div a ser atualizado no terceiro parâmetro e nem foi recebido no json.');
            } 
            else {
              div_atualizar = json.div_atualizar;
            }
          }
          else if(json.div_atualizar)  {
            alert('ERRO:Você definiu um div na chamada do enviar_formulario e veio outro pelo json. Escolha um só controle para não se confundir.');
          }
          if($(div_atualizar) == null) {
            alert('ERRO:O div solicitado: '+div_atualizar+' não foi encontrado na página');
          }
          else {
            $(div_atualizar).update(transport.responseText);
          }
          if ($('mensagem_erro')!=null) {
	        $("mensagem_erro").hide();
		  }
		  if ($('mensagem_sucesso')!=null) {
	        $("mensagem_sucesso").hide();
		  }
		  if ($('indicator')!=null) {
            Element.hide('indicator');
          }
          if(json.mensagem) {
            alert(json.mensagem);
          }
        },
        onFailure:function(transport,json) {
          Element.hide('indicator');
          erroAjax(transport.status);
        }
      }
    ); 
  }
  else {
    $(form).submit();  
  }
}

function enviar_comando(url, div_atualizar) {
  new Ajax.Request(
    url,
    {
      asynchronous:true, 
      evalScripts:true, 
      onLoading:function(request, json){
        Element.show('indicator');
      },
      onComplete: function(transport, json) {
        if(div_atualizar == false || div_atualizar == undefined) {
          if( json.div_atualizar!=null) {
            div_atualizar = json.div_atualizar;
          }
        }
        if(div_atualizar) {
          if ($(div_atualizar) == null) {
            alert('O div solicitado: '+div_atualizar+' não foi encontrado na página');
          }
          else {
            $(div_atualizar).update(transport.responseText);
          }
        }
        if ($('mensagem_erro')!=null) {
          $("mensagem_erro").hide();
        }
        if ($('mensagem_sucesso')!=null) {
          $("mensagem_sucesso").hide();
        }
        if ($('indicator')!=null) {
          Element.hide('indicator');
        }
        if(json.mensagem) {
          alert(json.mensagem);
        }
      },
      onFailure:function(transport,json) {
        Element.hide('indicator');
        erroAjax(transport.status);
      }
    }
  ); 

}
 
function trocar_div(div_atualizar,url) {
  if($(div_atualizar) == null) {
    alert('O div que você solitou para atualizar não existe');
  }
  
  new Ajax.Updater(
    div_atualizar, 
    url,
    {
      evalScripts:true,
      onLoading:function(request, json){
          Element.show('indicator');
      },
      onComplete:function(request, json){
          Element.hide('indicator');
      }
    }
  );
}

function carregarSelectPorJson( value , id , chave , texto , url , selected )
{
  if( value ) {
    params = "value=" + value + "&name=" + id + "&selected=" + selected;
    $(id).hide();
    $('carregando_'+id).show(); //Imagem que ficará no lugar da combo enquanto estiver carregando
    new Ajax.Request( url ,
      {
        asynchronous: true ,
        method: 'post',
        parameters: params ,

        onSuccess: function( response )
        {
          var objResult = eval( "(" + response.responseText + ")");
          $(id).options.length = 0;
          
          objResult.each( 
            function( objEntidade )
            {
              $('carregando_'+id).hide();//Imagem que ficará no lugar da combo enquanto estiver carregando
              $(id).show();
              newOp = new Option();
              newOp.text = eval( "objEntidade." + texto ) ;
              newOp.value = eval( "objEntidade." + chave ) ;
              if(objEntidade.selected=='true'){
                newOp.setAttribute( "selected" , "selected" );
              }
              $(id).options[$(id).length] = newOp;

            }
          )
        }
      }
    );
  }
}