function WriteResponse()
{
  // First inform the user that the message is being saved
  document.getElementById( 'MessageStatus' ).style.display = 'block';

  // Disable the text field
  document.getElementById( 'txt_Message' ).readOnly = true;
  document.getElementById( 'txt_Message' ).backgroundColor = '#990000';

  // Set the backend filename to process
  var filename = 'modules/Mail/ajax_save_message.php';

  // Get the new message body
  var txt_Message = document.getElementById('txt_Message').value;

  // Get the ThreadID
  var ThreadID = document.getElementById('ThreadID').value;

  // Format any ampersands with URL friendly characters
  txt_Message = txt_Message.replace( /&/g, '%26' );

  // Create the parameter list
  var params = "txt_Message=" + txt_Message + "&ThreadID=" + ThreadID;

  // Process the AJAX POST request
  AJAX_POST( filename, params );

  // Update the form when the AJAX process finishes
  xmlhttp.onreadystatechange = function()
  {
    if( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) )
    {
      // Create the new DIV section node
      var newDiv = document.createElement("DIV");

      // Fix the properties of the new div
      newDiv.innerHTML = xmlhttp.responseText;

      // Append the new element to the document
      document.getElementById( 'Thread' ).appendChild(newDiv);

      // FIX FOR IE 6.0+
      // Hide the message status once more - and ideally use the commented lines
      // Finally append and then hide the message status piece
      //document.getElementById( 'MessageStatus' ).innerHTML = '<strong>Your message has been posted!</strong>';
      document.getElementById( 'MessageStatus' ).style.display = 'none';
      document.getElementById( 'txt_Message' ).readOnly = false;
      document.getElementById( 'txt_Message' ).value = '';
      // END OF FIX FOR IE 6.0+

      // Finally, increment the thread count
      var ThreadCount = document.getElementById('ThreadCount').innerHTML;
      ThreadCount = parseInt( ThreadCount ) + 1;
      document.getElementById('ThreadCount').innerHTML = ThreadCount;
    }
  }

}

function ClearMessage()
{
  // Hide the message status once more
  document.getElementById( 'MessageStatus' ).style.display = 'none';

  // Reset the loading status message
  document.getElementById( 'MessageStatus' ).innerHTML = '<img src="themes/Default/images/ajax/ajax_loader.gif" /> &nbsp;Your message is being sent.';

  // Clear the existing post
  document.getElementById( 'txt_Message' ).value = "";
  document.getElementById( 'txt_Message' ).disabled = false;

} // end function ClearMessage()

function Mail_ShowUsers()
{
  // Display the DIV layer
  document.getElementById('MailUsers').style.display = 'block';
  document.getElementById('MailUsers_Close').style.display = 'block';

} // end function Mail_ShowUsers()

function Mail_HideUsers()
{
  // Hide the DIV layer
  document.getElementById('MailUsers').style.display = 'none';
  document.getElementById('MailUsers_Close').style.display = 'none';

} // end function Mail_HideUsers()

function Mail_SetUser( fk_RecipientID )
{
  document.getElementById('fk_RecipientID').value = fk_RecipientID;
  Mail_HideUsers();

} // end function Mail_SetUser()

function Mail_UpdateUsers()
{
  // Set the backend filename to process
  var filename = 'modules/Mail/ajax_update_recipients.php';

  // Get the current contents of the input field
  var fk_RecipientID = document.getElementById('fk_RecipientID').value;

  // Create the AJAX parameters
  var params = "fk_RecipientID=" + fk_RecipientID;

  // Process the AJAX POST request
  AJAX_POST( filename, params );

  // Update the form when the AJAX process finishes
  xmlhttp.onreadystatechange = function()
  {
    if( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) )
    {
      // Update the Display Types 
      document.getElementById("MailUsers").innerHTML = xmlhttp.responseText;
    }
  }

} // end function Mail_UpdateUsers()

function Mail_CancelMessage( URL )
{
  // Get the contents of the post
  var txt_Post = document.getElementById("txt_Post").value;

  // If there is any content, confirm that the user wants to leave
  if( txt_Post.length > 0 )
  {
    // Get confirmation of the request
    var cancel = confirm( 'Your message has not been sent.\n\nDiscard your message?' );

    // Redirect the page if requested
    if( cancel == true )
      window.location = URL;
  }
  else
  {
    window.location = URL;
  }

} // end function Mail_CancelMessage()


function textbox_insert_text( myField, myValue )
{

  // Because there are a variety of ways that the focus and
  // select works between browsers, we will need to handle 
  // a few different cases
  if( document.selection )
  {
    // This case will handle I.E.

    // First we need to get the focus of the element
    document.getElementById( myField ).focus();

    // Create a range of text at the cursor position
    // and replace that text with the value provided
    var selection = document.selection.createRange();
    selection.text = myValue;
  }
  else if( document.getElementById( myField ).selectionStart || document.getElementById( myField ).selectionStart == '0' )
  {
    // This case will handle FireFox

    // Get the start and end of the selection
    var startPos = document.getElementById( myField ).selectionStart;
    var endPos = document.getElementById( myField ).selectionEnd;

    // Insert the new text between the starting and ending position
    document.getElementById( myField ).value = document.getElementById( myField ).value.substring(0, startPos)+ myValue+ document.getElementById( myField ).value.substring(endPos, document.getElementById( myField ).value.length);
  }
  else
  {
    // This will handle other cases

    // Since we were unable to determine anything about the
    // user's current selection, we will simply insert the
    // new string at the end of the text field
    document.getElementById( myField ).value += myValue;
  }

} // end function textbox_insert_text() 
