/* Checks if a username/nickname is available to be used or not.
   Puts a tick/ cross next to the input element.
   Requires: 1. input element to check and
             2. place-holder element for the notification
*/
function isNameAvailable( input_element, notification_element ) {
  if( (input_element.val()).length == 0) {
    notification_element.removeClass('notification-unavailable');
    notification_element.removeClass('notification-available');
    notification_element.html('');
    return;
  } else if( (input_element.val()).length < 4 ) {
    notification_element.html('');
    notification_element.removeClass('notification-available');
    notification_element.addClass('notification-unavailable');
    $('<span>Not Available</span>').appendTo(notification_element);
    return;
  }
  $.get(
    $('base').attr('href') + 'user/check_is_name_unique', 
    { name: input_element.val() }, 
    function(data) {
      if (data.match('true')) {
        notification_element.removeClass('notification-unavailable');
        notification_element.addClass('notification-available');
        notification_element.html('');
         $('<span>Available</span>').appendTo(notification_element);
      }
      else {
        notification_element.removeClass('notification-available');
        notification_element.addClass('notification-unavailable');
        notification_element.html('');
        $('<span>Not Available</span>').appendTo(notification_element);
      }
    }
  );
}

