var SmartmoveNumericalInput = Class.create({
  initialize: function(input) {
    this.input = input;
    this.setup();
  },

  setup: function() {
    this.input.observe('focus', this.focusHandler.bindAsEventListener(this));
    this.input.observe('blur', this.blurHandler.bindAsEventListener(this));
  },

  focusHandler: function(event) {
    if(this.input.getValue() == '0') {
      this.input.value = '';
    }
  },

  blurHandler: function(event) {
    if(this.input.getValue().match(/^\s*$/))
      this.input.value = '0';
  }
});

$(document).observe('dom:loaded', function(event) {
  $$('.room td input').each(function(input) {
    new SmartmoveNumericalInput(input);
  });

  ['quote_request_move_date', 'quote_request_arrive_date'].each(function(id) {
    var element = $(id);

    if (element) {
      var container = element.up('.field-item');

      Calendar.setup({
        dateField: element,
        triggerElement: element,
        hoverElement: element,
        dateFormat: '%d/%m/%Y'
      });
      
      element.readOnly = true;

      if(container.down('.calendar-help'))
        container.down('.calendar-help').hide();

      if (container.down('.calendar-select')) {
        Calendar.setup({
          dateField: element,
          triggerElement: container.down('.calendar-select'),
          hoverElement: element,
          dateFormat: '%d/%m/%Y'
        });
        
        container.down('.calendar-select').show();
      }
    }
  });
});

function loadJson(json) {
  return eval('(' + json + ')');
}

function ajaxify(link, options) {
  var ajax_options = {
    method: 'get'
  };
  if (options) Object.extend(ajax_options, options);

  link.observe('click', function(event) {
    event.stop();
    new Ajax.Request(link.href, ajax_options);
  });
}


