$(function(){
   
  $('.calendar').datepick({dateFormat: 'yy-mm-dd'});
  $('#RequestEditForm').bind('change', function(){
      $.ajax({
          url: $(this).attr('action'),
          type: 'POST',
          data: $(this).serialize()
      });
  });
  
  $('#RequestTypeId').change(function(){
      
      $.ajax({
          url:'/requests/responsible_user/' + $(this).val(),
          beforeSend: function(){
            $('#rp').html('');
            $('#loader').show();  
          },
          success: function(r){
             $('#rp').html(r);
             $('#loader').hide();
          }
      });
      
      $('#engineering_question, #spare_parts').hide();
      if(parseInt($(this).val()) == 1){
         $('#spare_parts').show();
      }
      if(parseInt($(this).val()) == 2){
         $('#engineering_question').show();
      }
  });
  
  $('#RequestTypeId').trigger("change");
  
  $('.onduty').bind('click', function(){
      var self = this;
      $(self).parents('tr').css('background-color', '#FFF8AF');
      
      $.ajax({
          url: '/users/onduty/' + $(self).val(),
          success: function(){
             $(self).parents('tr').css('background-color', '#FFFFFF');
             $('.mini-message').fadeIn(); 
          }
      });
      
  });
  
  $('#RequestSalePartsOperationType').change(function(){
      $('.pluscosts').toggle();
  });
  
  $('#RequestPriceAccepted').change(function(){
      if(parseInt($(this).val())){
          $('.accepted').show();
      }else{
          $('.accepted').hide();
      }
      
  });
  
  $('#previewQuotationForm').bind('click', function(){

      var self = this;
      
      $.ajax({
            url: $(self).parents('form').attr('action'),
            type: 'POST',
            data: $(self).parents('form').serialize(),
            success: function(){
                window.location.href = $(self).attr('rel');
            }
     });
      
  });
  
  $('#send_quotation').bind('click', function(){
      
      $.ajax({
          url: $(this).attr('rel'),
          type: 'POST',
          data: {email: $('#RequestClientEmailForEmail').val()},
          beforeSend: function(){
              $('#RequestEditForm').trigger('change');
              $('#loader').show();
          },
          success: function(){
              $('#loader').addClass('green').html('Done!')
          }
      });
      return false;
  });
  
  
  
  
});