$(document).ready(
  function ()
  {
    $('#menu-top > dl:first').droppy();

    var button_section = $('img.bt-'.concat($('body').attr('id').replace(/^mod\-/, '')))[0];

    if (typeof(button_section) !== 'undefined')
      $(button_section).attr('src', $(button_section).attr('src').replace(/\/unselected\//, '/selected/'));

    sospcidf_modal.add('request-intervention',
                       '/intervention/index',
                       {onShow:
                         function (dialog)
                         {
                            $('#f-intervention').submit(
                              function (ev)
                              {
                                ev.preventDefault();
                                var ptrs  = {};
                                var data  = {};
                                $('button, input, select, textarea', this).each(
                                  function ()
                                  {
                                    var ptr = $(this);
                                    ptr.removeClass('error');
                                    if (wcm_has_len(ptr.attr('name')) === true)
                                    {
                                      ptrs[ptr.attr('name')] = ptr;
                                      data[ptr.attr('name')] = ptr.val();
                                    }
                                  });

                                $.ajax({'url':      '/json.php/intervention/create',
                                        'dataType': 'json',
                                        'data':     data,
                                        'type':     'post',
                                        'success':  function (data, textStatus, jqhrx)
                                                    {
                                                      if (jqhrx.status === 201)
                                                      {
                                                        $('#f-intervention').addClass('sospcidf-nodisplay');
                                                        $('#f-intervention-success').removeClass('sospcidf-nodisplay');
                                                        return;
                                                      }

                                                      $.each(data,
                                                             function(key, val)
                                                             {
                                                               if (typeof(ptrs[key]) !== 'undefined')
                                                                 ptrs[key].addClass('error');
                                                             });
                                                    }});
                              });
                         }})
                  .add('request-quote',
                       '/quote/index',
                       {onShow:
                         function (dialog)
                         {
                            $('#quote_kind').change(
                              function (ev)
                              {
                                if ($(this).val() === 'professional')
                                  $('#quote_company_name').parents('tr:first').removeClass('sospcidf-nodisplay');
                                else
                                  $('#quote_company_name').parents('tr:first').addClass('sospcidf-nodisplay');
                              });

                            $('#quote_equipment').change(
                              function (ev)
                              {
                                if ($(this).val() === 'other')
                                  $('#quote_equipment_other').parents('tr:first').removeClass('sospcidf-nodisplay');
                                else
                                  $('#quote_equipment_other').parents('tr:first').addClass('sospcidf-nodisplay');
                              });

                            $('#quote_type').change(
                              function (ev)
                              {
                                if ($(this).val() === 'other')
                                  $('#quote_type_other').parents('tr:first').removeClass('sospcidf-nodisplay');
                                else
                                  $('#quote_type_other').parents('tr:first').addClass('sospcidf-nodisplay');
                              });

                            $('#f-quote').submit(
                              function (ev)
                              {
                                ev.preventDefault();
                                var ptrs  = {};
                                var data  = {};
                                $('button, input, select, textarea', this).each(
                                  function ()
                                  {
                                    var ptr = $(this);
                                    ptr.removeClass('error');
                                    if (wcm_has_len(ptr.attr('name')) === true)
                                    {
                                      ptrs[ptr.attr('name')] = ptr;
                                      data[ptr.attr('name')] = ptr.val();
                                    }
                                  });

                                $.ajax({'url':      '/json.php/quote/create',
                                        'dataType': 'json',
                                        'data':     data,
                                        'type':     'post',
                                        'success':  function (data, textStatus, jqhrx)
                                                    {
                                                      if (jqhrx.status === 201)
                                                      {
                                                        $('#f-quote').addClass('sospcidf-nodisplay');
                                                        $('#f-quote-success').removeClass('sospcidf-nodisplay');
                                                        return;
                                                      }

                                                      $.each(data,
                                                             function(key, val)
                                                             {
                                                               if (typeof(ptrs[key]) !== 'undefined')
                                                                 ptrs[key].addClass('error');
                                                             });
                                                    }});
                              });
                         }})
                  .init();
  }
);

