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('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;
            }
          }
          if($(div_atualizar) == null) {
            alert('O div solicitado: '+div_atualizar+' não foi encontrado na página');
          }
          else {
            if(transport.responseText.substr(0,2)=='1;'){
              window.location = transport.responseText.substr(2);
            }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 trocar_div(div_atualizar,url) {
  if($(div_atualizar) == null) {
    alert('O div '+div_atualizar+' que você solicitou 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 trocar_div_com_msg(div_atualizar,url) {
  if($(div_atualizar) == null) {
    alert('O div que você solicitou 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');
        if(json.mensagem) {
          alert(json.mensagem);
        }
      }
    }
  );
}

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;

            }
          )
        }
      }
    );
  }
}
