// ## the variable tellFriendValidator MUST be declared somewhere in the calling code
// since it should come *after* the form, and that can't be done here.

function tellFriend() {
  // check for errors
  if (tellFriendValidator.validate()) {
    // start animated icon
    setWaiting('submit-area');
    //  $('submit-area').innerHTML = '<img src="/imgs/please-wait.gif" />loading...';
    // send via AJAX
    // if dates are filled in, repopulate fees section
    var params = {
      your_name     : $F('your_name'),
      your_email    : $F('your_email'),
      friend_name   : $F('friend_name'),
      friend_email  : $F('friend_email'),
      message       : $F('message'),
      price         : $F('price'),      
      property_id   : $F('p_id'),
      bookurl       : $F('bookurl')
    };

    new Ajax.Updater('submit-area', '/accommodation/AJAXtellFriend', {
      method: 'post',
      onComplete: endTellFriend,
      parameters: params,
      evalScripts: true
    });
  }
}

function initTellFriend() {
  // reset the submit button part
  clearTellFriendForm();
  $('submit-area').innerHTML = '<span class="submit-orange-button" onclick="tellFriend()">send!</span>';
}

function clearTellFriendForm() {
  $('friend_name').value = '';
  $('friend_email').value = '';
}

function endTellFriend() {
  $('submit-area').innerHTML = 'Message sent. <span class="AJAX-selectable" onclick="$(\'tellafriend\').toggleFade();">Close this window</span>.';
  clearTellFriendForm();
}

function toggleTellFriendClick() {
  initTellFriend();
  $('tellafriend').toggleFade();
  return false;
}
