var user_agent 	= navigator.userAgent.toLowerCase();
var click_event = user_agent.match(/(iphone|ipod|ipad)/)  ? "touchend" : "click";

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
  });

  $('.date-time').datetimepicker({
    format: "YYYY-MM-DD HH:mm:SS"
  });

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

  $('body').on('change', '.number-format', function ()
  {
      var value = $(this).val();
      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");
}