window.copyToClipboard = function( element )
{
  var $temp = $( "<input>" );
  $( "#" + element ).after( $temp );
  $temp.val( $( '#' + element ).text() ).select();
  document.execCommand( "copy" );
  $temp.remove();
}

function checkForm(id)
{
  check = true;
  $('form#' + id + ' input[type="text"]').each(function () {
    if ($(this).hasClass('require') && $(this).val() === '')
    {
      $(this).parents('.form-group').addClass('has-error');
      check = false;
    } else
      $(this).parents('.form-group').removeClass('has-error');
  });

  $('form#' + id + ' textarea').each(function () {
    if ($(this).hasClass('require') && $(this).val() === '')
    {
      $(this).parents('.form-group').addClass('has-error');
      check = false;
    } else
      $(this).parents('.form-group').removeClass('has-error');
  });

  if (check)
    $('form#' + id).submit();

  return false;
}

function number_format(number, decimals, dec_point, thousands_sep)
{
  number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
  var n = !isFinite(+number) ? 0 : +number,
          prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
          sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
          dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
          s = '',
          toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
          };
  // Fix for IE parseFloat(0.55).toFixed(0) = 0;
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  if (s[0].length > 3) {
    s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || '';
    s[1] += new Array(prec - s[1].length + 1).join('0');
  }
  return s.join(dec);
}

$(document).ready(function ()
{
  $('.date').datetimepicker({
    format: "YYYY-MM-DD",
    pickTime: false
  });

  $('body').on('click', '.date-range', function ()
  {
    $(this).daterangepicker().focus();
  });

  $('body').on('change', '.int-format', function ()
  {
    var value = $(this).val();
    value = value.replace(' ', '');

    value = parseFloat(value.replace(',', '.') * 1);
    value = number_format(value, 0, '.', '');
    $(this).val(value);
  });

  $('body').on('change', '.number-format', function ()
  {
    var value = $(this).val();

    if (value === '')
      return true;

    value = value.replace(' ', '');

    value = parseFloat(value.replace(',', '.') * 1);
    value = number_format(value, 2, '.', '');
    $(this).val(value);
  });
});

function disable_menu()
{
  $('.sidebar-menu li ul').remove();
  $('.sidebar-menu li ').addClass("disable_menu");
  $('.sidebar-menu li:last-child').removeClass("disable_menu");
}

$.fn.serializeObject = function() {
  var data = {};

  function buildInputObject(arr, val) {
      if (arr.length < 1) {
          return val;
      }
      var objkey = arr[0];
      if (objkey.slice(-1) == "]") {
          objkey = objkey.slice(0,-1);
      }
      var result = {};
      if (arr.length == 1){
          result[objkey] = val;
      } else {
          arr.shift();
          var nestedVal = buildInputObject(arr,val);
          result[objkey] = nestedVal;
      }
      return result;
  }

  function gatherMultipleValues( that ) {
      var final_array = [];
      $.each(that.serializeArray(), function( key, field ) {
          // Copy normal fields to final array without changes
          if( field.name.indexOf('[]') < 0 ){
              final_array.push( field );
              return true; // That's it, jump to next iteration
          }

          // Remove "[]" from the field name
          var field_name = field.name.split('[]')[0];

          // Add the field value in its array of values
          var has_value = false;
          $.each( final_array, function( final_key, final_field ){
              if( final_field.name === field_name ) {
                  has_value = true;
                  final_array[ final_key ][ 'value' ].push( field.value );
              }
          });
          // If it doesn't exist yet, create the field's array of values
          if( ! has_value ) {
              final_array.push( { 'name': field_name, 'value': [ field.value ] } );
          }
      });
      return final_array;
  }

  // Manage fields allowing multiple values first (they contain "[]" in their name)
  var final_array = gatherMultipleValues( this );

  // Then, create the object
  $.each(final_array, function() {
      var val = this.value;
      var c = this.name.split('[');
      var a = buildInputObject(c, val);
      $.extend(true, data, a);
  });

  return data;
};