function login()
{
  $('#login').hide();
  $('#spinner').show();

  $.ajax({
    type: 'POST',
    url: '/ajax/calls.php',
    data: 'action=login&invoice='+$('#invoice').val()+'&password='+$('#password').val(),
    success: function(msg)
    {
      
      if (msg == '1')
      {
        location.href = '/';
      }
      else
      {
        $('#login').show();
        $('#spinner').hide();

        $('#output').html('Neplatné heslo alebo číslo zmluvy.');
        $('#password').val('');
      }
    }
  });
}

function delete_msg(msg_id)
{
  if (msg_id > 0)
  {
    $.ajax({
      type: 'POST',
      url: '/ajax/calls.php',
      data: 'action=delete_msg&message_id='+msg_id,
      success: function(msg)
      {

        if (msg == '1')
        {
          $('#msg-'+msg_id).hide();
        }

      }
    });
  }
}

function send()
{
  $('#send').hide();
  $('#spinner').show();

  $.ajax({
    type: 'POST',
    url: '/ajax/calls.php',
    data: 'action=send&number='+$('#number').val()+'&message='+$('#message').val(),
    success: function(msg)
    {
      $('#send').show();
      $('#spinner').hide();

      if (msg == '1')
      {
        $('#output').html('Správa úspešne odoslaná.');
        $('#message').val('');
      }
      else
      {
        $('#output').html(msg);
      }
    }
  });
}

function cdown(chars)
{  
  var txt = $('#message').val();
  var cnt = chars - txt.length;

  $('#chars').html(cnt > 0 ? cnt : 0);
  
  if (cnt < 0)
  {
    $('#message').val(txt.substring(0, chars));
  }
}

function add_contact()
{
  var name = $('#name').val();
  var number = $('#number').val();
 
  if ((name != '') || (number != ''))
  {
    $('#add').hide();
    $('#spinner').show();
  
    $.ajax({
      type: 'POST',
      url: '/ajax/calls.php',
      data: 'action=add_contact&number='+number+'&name='+name,
      success: function(msg)
      {
        $('#add').show();
        $('#spinner').hide();

        if (msg != '')
        {
          $('.list').html(msg + $('.list').html());
          
          $('#name').val('');
          $('#number').val('');
        }
      }
    });
  }
}

function delete_contact(contact_id)
{
  if (contact_id > 0)
  {
    $.ajax({
      type: 'POST',
      url: '/ajax/calls.php',
      data: 'action=delete_contact&contact_id='+contact_id,
      success: function(msg)
      {

        if (msg == '1')
        {
          $('#contact-'+contact_id).hide();
        }

      }
    });
  }
}

function edit_contact(contact_id)
{
  var name = $('#name-'+contact_id).val();
  var number = $('#number-'+contact_id).val();
  
  if (contact_id > 0 && (name != '' && number != ''))
  {
    $.ajax({
      type: 'POST',
      url: '/ajax/calls.php',
      data: 'action=edit_contact&contact_id='+contact_id+'&name='+name+'&number='+number,
      success: function(msg)
      {
        if (msg == '1')
        {
          $('#edit-'+contact_id).css('color','#3cb878');
          $('#edit-'+contact_id).html('hotovo');
        }
      }
    });
  }
}

function passwd()
{
  var old_pass = $('#old_password').val();
  var new_pass = $('#new_password').val();
  var invoice = $('#invoice').val()
  
  if (old_pass != '' && new_pass != '' && invoice != '')
  {
    $('#passws').hide();
    $('#spinner').show();
    
    $.ajax({
      type: 'POST',
      url: '/ajax/calls.php',
      data: 'action=passwd&invoice='+invoice+'&old_pass='+old_pass+'&new_pass='+new_pass,
      success: function(msg)
      {
        $('#add').show();
        $('#spinner').hide();
        
        $('#output').html(msg);
      }
    });
  }
}