function addToMyViewings(id) {
  $.post(
    '/viewings/addtomyviewings',
    {id: id},
    function(data)
    {
      if(data == 'success')
			{
        $('#viewings_total').html(parseInt($('#viewings_total').html()) + 1);
        $('#add_to_viewings').html('Viewing added');
			}
      else
      {
        //alert(data);
      }
		}
  );
}

function confirmSingleProperty(id) {
  $.post(
    '/admin/confirmproperty',
    {id: id},
    function(data)
    {
      if(data == 'success')
			{
        $('#confirm_property').html('Property confirmed');
			}
      else
      {
        //alert(data);
      }
		}
  );
}

function removeFromMyViewings(id) {
  $.post(
    '/viewings/removefrommyviewings',
    {id: id},
    function(data)
    {
      if(data == 'success')
			{
        $('.remove_' + id).fadeOut('fast');

        $('.property_' + id).slideUp('slow', function() {
          if(parseInt($('#viewings_total').html()) == 0)
          {
            $('.propertylist').after('<p id="no_viewings" style="display: none;">You have removed all of your viewings!</p>');
            $('#arrange_viewings_link').hide();
            $('#no_viewings').fadeIn();
          }
        });

        $('#viewings_total').html(parseInt($('#viewings_total').html()) - 1);
			}
      else
      {
        alert(data);
      }
		}
  );
}

function clearMyViewings() {
  $.post(
    '/viewings/clearmyviewings',
    {},
    function(data)
    {
      if(data == 'success')
			{
        $('.propertylist').fadeOut();
        $('#viewings_total').html(0);
			}
      else
      {
        alert(data);
      }
		}
  );
}

function arrangeViewings() {
  $.post(
    '/viewings/arrangeviewings',
    {
      name: $('#name').val(),
      email: $('#email').val(),
      phone: $('#phone').val(),
      date: $('#actualDate').val(),
      time: $('#time').val()
    },
    function(data)
    {
      if(data == 'success')
			{
        clearMyViewings();
        $('.propertylist').after('<div id="thank_you" style="display: none;"><p class="success">Thank you, your viewing arrangements have been sent. We will be in touch shortly.</p></div>');
        $('#thank_you').fadeIn();
        $('.propertylist').hide();
        $('#arrange_viewings_link').hide();
			}
      else
      {
        alert(data);
      }
		}
  );
}

function confirmProperty(id) {
  $.post(
    '/admin/confirmproperty',
    {id: id},
    function(data)
    {
      if(data == 'success')
			{
        $('.confirm_' + id).fadeOut('fast');
        $('.property_' + id).slideUp('slow', function() {
          if(parseInt($('#unconfirmed_total').html()) == 0)
          {
            $('.propertylist').after('<p id="no_unconfirmed" style="display: none;">You have confirmed all the properties!</p>');
            $('#no_unconfirmed').fadeIn();
          }
        });

        $('#unconfirmed_total').html(parseInt($('#unconfirmed_total').html()) - 1);
			}
      else
      {
        alert(data);
      }
		}
  );
}

function deleteProperty(id, url)
{
  $.post(
    '/admin/deleteproperty',
    {id: id},
    function(data)
    {
      if(data == 'success')
			{
        window.location = url;
			}
      else
      {
        alert(data);
      }
		}
  );
}

function swapBannerImage()
{
  var bannerDir = '/img/banners/';

  var currentImageNumber;

  if(strpos($('#image_banner').css('background-image'), '1') !== false)
    currentImageNumber = 1;
  else if(strpos($('#image_banner').css('background-image'), '2') !== false)
    currentImageNumber = 2;
  else if(strpos($('#image_banner').css('background-image'), '3') !== false)
    currentImageNumber = 3;
  else if(strpos($('#image_banner').css('background-image'), '4') !== false)
    currentImageNumber = 4;

  if(currentImageNumber < 3)
  {
    $('#image_banner').css(
      'background-image',
        'url(\'' +
        bannerDir +
        (
          currentImageNumber + 1
        ) +
        '.jpg\')'
    );
  }
  else
  {
    $('#image_banner').css(
      'background-image',
        'url(\'' +
        bannerDir +
        '1.jpg\')'
    );
  }
}

function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another
    //
    // version: 909.322
    // discuss at: http://phpjs.org/functions/strpos
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman
    // +   bugfixed by: Daniel Esteban
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

function updatePPPW()
{
  //If a valid number is present
  if(!isNaN(parseInt($('#rent').val())) && $('#bedrooms').val() != '')
  {
    $('#rent_alt').fadeIn('fast');
    $('#rent_pppw').html(
      Math.round(
      (
        ((parseInt($('#rent').val()) * 12) //rent per year
        / 52) //rent per week
        / parseInt($('#bedrooms').val()) //rent per person per week
      ) * 100) / 100 //to 2 decimal places
    );
  }
  else
  {
    $('#rent_alt').fadeOut('fast');
  }
}

function updateEPCInfo()
{
  if($('#epc_ref1').val() != '' || $('#epc_ref2').val() != '' || $('#epc_ref3').val() != '' || $('#epc_ref4').val() != '' || $('#epc_ref5').val() != '')
    $('#epc_info').fadeOut('fast');
  else
    $('#epc_info').fadeIn('fast');
}

function calculateTotal()
{
  var total = 0;
  var epc_qty = $('#epc_qty').val();
  var gsc_qty = $('#gsc_qty').val();
  var pat_qty = $('#pat_qty').val();
  var tsu_qty = $('#tsu_qty').val();
  var yu_qty = $('#yu_qty').val();

  if(isNaN(epc_qty) || epc_qty < 0 || strpos(epc_qty, '.') ||
  isNaN(gsc_qty) || gsc_qty < 0 || strpos(gsc_qty, '.') ||
  isNaN(pat_qty) || pat_qty < 0 || strpos(pat_qty, '.') ||
  isNaN(tsu_qty) || tsu_qty < 0 || strpos(tsu_qty, '.') ||
  isNaN(yu_qty) || yu_qty < 0 || strpos(yu_qty, '.')
  )
  {
    alert('The quantity must be a whole number above 0.');
    return;
  }

  $('#epc_total').html(Math.round((epc_qty * $('#epc_price').html()) * 100) / 100);
  $('#gsc_total').html(Math.round((gsc_qty * $('#gsc_price').html()) * 100) / 100);
  $('#pat_total').html(Math.round((pat_qty * $('#pat_price').html()) * 100) / 100);
  $('#tsu_total').html(Math.round((tsu_qty * $('#tsu_price').html()) * 100) / 100);
  $('#yu_total').html(Math.round((yu_qty * $('#yu_price').html()) * 100) / 100);
  $('#total').html(
    Math.round((parseFloat($('#epc_total').html()) +
    parseFloat($('#gsc_total').html()) +
    parseFloat($('#pat_total').html()) +
    parseFloat($('#tsu_total').html()) + 
    parseFloat($('#yu_total').html())) * 100) / 100
  );

}

function toggleTennantInfo()
{
  if($('.tennant_info').css('display') == 'none')
  {
    $('#tennant_name').val('');
    $('#tennant_phone').val('');
    $('#tennant_email').val('');
    $('.tennant_info').fadeIn();
  }
  else
  {
    $('.tennant_info').hide();
    $('#tennant_name').val('none');
    $('#tennant_phone').val('none');
    $('#tennant_email').val('');
  }
}

function autoTab(from, to)
{
  if($('#' + from).val().toString().length == $('#' + from).attr('maxlength'))
    $('#' + to).focus();
}
