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 isTouchDevice() {
  return true == ("ontouchstart" in window || window.DocumentTouch && document instanceof DocumentTouch);
}

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;
      };
  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()
{
  $( '#overlay' ).click( function()
  {
    $( this ).hide();
    $( '#ajax-container, #comment-info' ).hide();
  });

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

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

  $( '.colorpicker' ).colorpicker();

  $( 'body' ).on( 'change', '.number-format', function() {
    var value = $( this ).val();
    value = (value + '').replace(/[^\d.,-]/g, '');
    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 a' ).css( "pointer-events", "none" );
  $( '#sidebar_left' ).css( 'opacity', '0.5' );
}

function ajax_refresh_phrase_position( site_id, phrase_id )
{
  jQuery.ajax(
  {
    type: 'POST',
    cache: false,
    url: '/ajax.php',
    timeout: 2000,
    data:
    {
      site_id:    site_id,
      phrase_id:  phrase_id,
      a:          'refresh_phrase_position'
    },
    success: function()
    {
      $( '#' + phrase_id + '-' + moment().format( 'YYYY-MM-DD' ) );
      $( "#" + phrase_id + "-" + moment().format( 'YYYY-MM-DD' ) + " span" ).html( "<i class='fa fa-refresh'></i>" ).addClass( 'spin' );
      $( "#" + phrase_id + "-" + moment().format( 'YYYY-MM-DD' ) ).removeClass( 'pos_11' ).removeClass( 'pos_7' ).removeClass( 'pos_4' ).removeClass( 'pos_2' ).removeClass( 'pos_1' );
      $( '#phrase-url-' + phrase_id ).text( '' );
      ajax_check_phrase_position( phrase_id );
    },
    error: function (err) {
      ajax_refresh_phrase_position( site_id, phrase_id );
    }
  });
}

function ajax_check_phrase_position( phrase_id )
{
  $.ajax(
  {
    type: 'POST',
    cache: false,
    url: '/ajax-check.php',
    timeout: 2000,
    data:
    {
      a: 'phrase-position',
      phrase_id: phrase_id
    },
    success: function( response )
    {
      data = jQuery.parseJSON( response );
      if ( data.position !== false && data.position !== null )
      {
        $( "#" + phrase_id + "-" + moment().format( 'YYYY-MM-DD' ) ).removeClass( 'pos_11' ).removeClass( 'pos_7' ).removeClass( 'pos_4' ).removeClass( 'pos_1' );

        if ( data.position == 1 )
          new_class = 'pos_1';
        if ( data.position == 2 || data.position == 3 )
          new_class = 'pos_2';
        if ( data.position >= 4 && data.position <= 6 )
          new_class = 'pos_4';
        if ( data.position >= 7 && data.position <= 10 )
          new_class = 'pos_7';
        if ( data.position >= 11 && data.position <= 20 )
          new_class = 'pos_11';
        if ( data.position == 0 || data.position >= 21 )
          new_class = 'pos_21';

        $( '#' + phrase_id + '-' + moment().format( 'YYYY-MM-DD' ) ).removeClass( 'error' ).removeClass( 'to-check' ).addClass( new_class );
        $( '#' + phrase_id + '-' + moment().format( 'YYYY-MM-DD' ) + ' span' ).html( data.position ).removeClass( 'spin' );
        $( '#' + phrase_id + '-' + moment().format( 'YYYY-MM-DD' ) + ' input' ).val( data.position );
        $( '#phrase-url-' + phrase_id ).text( data.last_url ).attr( 'href', data.last_url );
      }
      else
      {
        $.wait( 5000, function() {
          ajax_check_phrase_position( phrase_id );
        });
      }
    },
    error: function( err )
    {
      $.wait( 5000, function() {
          ajax_check_phrase_position( phrase_id );
      });
    }
  });
}

function ajax_site_confirm( site_id, next_site_id )
{
  jQuery.ajax(
  {
    type: 'POST',
    cache: false,
    url: '/ajax.php',
    data:
    {
      site_id: site_id,
      a: 'site_confirm'
    },
    beforeSend: function()
    {
      jQuery( '#overlay' ).show();
    },
    success: function()
    {
      document.location.href = '/ranker/main_view/id=' + next_site_id;
    }
  });
}

function get_today_date()
{
   var tdate  = new Date();
   var dd     = tdate.getDate();
   var MM     = tdate.getMonth();
   var yyyy   = tdate.getFullYear();

   return yyyy + '-' + ( MM+1) + '-' + dd;
}

(function($) {
    $.fn.hasScrollBar = function() {
        return this.get(0).scrollWidth > this.width();
    }
})(jQuery);

(function($) {

  $.wait = function(duration, completeCallback, target) {
    $target = $(target || '<queue />');
    return $target.delay(duration).queue(function(next){completeCallback.call($target); next();});
  }

  $.fn.wait = function(duration, completeCallback) {
    return $.wait.call(this, duration, completeCallback, this);
  };

})(jQuery);