(function() {
  $.fn.symfonyformajaxer = function(options) {
    //self
    var self = this;
    var ajax_timeout;
    var changed_field;

    //get errors by ajax
    symfonyFormErrors = function() {
      $.getJSON(options['url'],
      $(self).find('input').serialize(),
      function(json) {
        $.renderErrors(json);
      })
    };

    $.clearErrors = function () {
      $(self).find('div[class=reg-error red]').each(function() {
        $(this).html('');
      });
    }

    $.renderErrors = function (errors_json) {
        $.clearErrors();
        var ch_id = $(changed_field).attr('id');
        $('#' + options['err_prefix'] + ch_id).html(errors_json[ch_id]);
    }

    //determine changes and try to ajax
    tryAjaxian = function() {
      clearTimeout(ajax_timeout);
      changed_field = this;
      ajax_timeout = setTimeout('symfonyFormErrors()', 100);
    };

    var inputs = this.find("div[class=reg-right] input");
    inputs.each(function(){
      $(this).blur(tryAjaxian);
    });

    disableReg = function() {
      if ($(this).attr('checked')) {
        $('#registration_form :submit').removeAttr('disabled');
      } else {
        $('#registration_form :submit').attr('disabled', 'disabled');
      }
    }

    checkSubmit = function() {
      if (!$(this).attr('checked')) {
        return false;
      }
    }

    $('#userreg_agreement').click(disableReg);

  }
})(jQuery);
