function forum_publish()
{
  document.getElementById('form_topic').submit();
}

function forum_preview( PostID )
{

  // Set the backend filename to process
  var filename = 'modules/Forums/ajax_generate_preview.php';

  // Get the subject and message body
  var vc_Subject = document.getElementById('vc_Subject' + PostID ).value;
  var txt_Post = document.getElementById('txt_Post' + PostID ).value;

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

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

  // Create the parameter list
  var params = "vc_Subject=" + vc_Subject + "&txt_Post=" + txt_Post + "&TopicID=" + TopicID;

  // 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) )
    {
      document.getElementById("create_preview" + PostID ).innerHTML = xmlhttp.responseText;

      // Once the preview has loaded, change the text of the button
      document.getElementById("button_preview").value = "Update Preview";
    }
  }

}

function forum_hide_preview( PostID )
{

  // Hide the preview
  document.getElementById("create_preview" + PostID ).innerHTML = '&nbsp;';

  // Change the preview button text again
  document.getElementById("button_preview").value = "Show Preview";

}

function forum_insert_special( PostID, str )
{
  document.getElementById('txt_Post' + PostID ).value += str;
}


function forum_reply( PostID, quote )
{
  // Set the backend filename to process the loader
  var filename = 'modules/Forums/ajax_reply_inline.php';

  // Create the parameter list
  var params = "PostID=" + PostID + "&quote=" + quote;

  // 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) )
    {
      document.getElementById( 'Reply' + PostID ).innerHTML = xmlhttp.responseText;
      document.getElementById( 'Options' + PostID ).innerHTML = '<a href="#NULL" onclick="forum_discard_reply( ' + PostID + ' );">discard reply</a>';
    }
  }

}


function forum_discard_reply( PostID )
{
  document.getElementById( 'Reply' + PostID ).innerHTML = "&nbsp;";
  document.getElementById( 'Options' + PostID ).innerHTML = '<a href="#NULL" onclick="forum_reply( ' + PostID + ', true );">quote</a> | <a href="#NULL" onclick="forum_reply( ' + PostID + ', false );">reply</a>';
}

function forum_post_response( PostID )
{

  // Set the backend filename to process the save
  var filename = 'modules/Forums/ajax_save_inline.php';

  // Get the hidden variables
  var ForumID = document.getElementById( 'ForumID' ).value;
  var TopicID = document.getElementById( 'TopicID' ).value;

  // Get the token
  var token = document.getElementById( 'token' ).value;
 
  // Get the elements of the new message
  var vc_Subject = document.getElementById( 'vc_Subject' + PostID ).value;
  var txt_Post = document.getElementById( 'txt_Post' + PostID ).value;

  // Format safe characters for the post
  vc_Subject = vc_Subject.replace( /&/g, '%26' );
  txt_Post = txt_Post.replace( /&/g, '%26' );

  // Create the parameter list
  var params = "ForumID=" + ForumID + "&TopicID=" + TopicID + "&PostID=" + PostID + "&vc_Subject=" + vc_Subject + "&txt_Post=" + txt_Post + "&token=" + token;

  // 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) )
    {
      // Post the new response inline
      document.getElementById( 'Reply' + PostID ).innerHTML = xmlhttp.responseText;

      // Turn on the bottom border of the previous options
      document.getElementById( 'Options' + PostID ).style.borderBottom = "1px solid #cccccc";

      // Remove the previous formatting options
      document.getElementById( 'Options' + PostID ).innerHTML = "&nbsp;";
    }
  }

}


function forum_watch_topic( ForumID, TopicID )
{
  // Get confirmation of the request
  var watch = confirm( 'Would you like to begin watching this topic?\n\nEverytime a new post is made, you will be sent a notification.  You can stop watching a topic at any time' );

  // Only use the AJAX method if the user wants to add the topic
  if( watch )
  {
    // Set the backend filename to process the save
    var filename = 'modules/Forums/ajax_watch_topic.php';

    // Create the parameter list
    var params = "ForumID=" + ForumID + "&TopicID=" + TopicID + "&Active=1";

    // 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 'Watch this Topic' span
        document.getElementById( 'ForumWatch' ).innerHTML = '<a id="ForumWatch" href="#NULL" onclick="forum_cancel_watch( ' + ForumID + ',' + TopicID + ');">Stop Watching this Topic</a>';

        // Let the user know they are now watching the topic
        alert( xmlhttp.responseText );
      }
    }

  } // end if( watch )

} // end function forum_watch_topic()



function forum_cancel_watch( ForumID, TopicID )
{
  // Get confirmation of the request
  var cancel = confirm( 'Would you like to stop watching this topic?\n\nYou will no longer receive email notifications when posts are made.' );

  // Only use the AJAX method if the user wants to stop watching the topic
  if( cancel )
  {
    // Set the backend filename to process the save
    var filename = 'modules/Forums/ajax_watch_topic.php';

    // Create the parameter list
    var params = "ForumID=" + ForumID + "&TopicID=" + TopicID + "&Active=0";

    // 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 'Watch this Topic' span
        document.getElementById( 'ForumWatch' ).innerHTML = '<a id="ForumWatch" href="#NULL" onclick="forum_watch_topic( ' + ForumID + ',' + TopicID + ' );">Watch this topic</a>';

        // Let the user know they are now watching the topic
        alert( xmlhttp.responseText );
      }
    }

  } // end if( cancel )

} // end function forum_cancel_watch()



function forum_edit_post( PostID )
{
  // Set the backend filename to process the loader
  var filename = 'modules/Forums/ajax_edit_post.php';

  // Create the parameter list
  var params = "PostID=" + PostID;

  // 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) )
    {
      // Remove the edit / quote / reply bar
      document.getElementById( 'Options' + PostID ).innerHTML = '&nbsp;';

      // Replace the textual body with a textarea
      document.getElementById( 'PostBody' + PostID ).innerHTML = '<textarea id="PostEdit' + PostID + '" style="width: 465px; height: 130px;">' + xmlhttp.responseText + '</textarea><br/><br/><input type="button" class="color" value="Save Changes" onclick="forum_edit_save( ' + PostID + ' );" /> <input type="button" class="gray" value="Discard Changes" onclick="forum_edit_discard( ' + PostID + ' );" />';
    }
  }

} // end function forum_edit_post()



function forum_edit_save( PostID )
{
    // Set the backend filename to process the save
    var filename = 'modules/Forums/ajax_edit_save.php';

    // Get the revision text
    var txt_Post = document.getElementById( 'PostEdit' + PostID ).value;

    // Format safe characters for the post
    txt_Post = txt_Post.replace( /&/g, '%26' );

    // Create the parameter list
    var params = "PostID=" + PostID + "&txt_Post=" + txt_Post;

    // Update the DIV layer with the saving table
    document.getElementById( 'PostBody' + PostID ).innerHTML = '<table cellpadding="0" cellspacing="0" style="width: 400px; background: #fffbe2; border: 1px solid #ffe222; padding: 10px; margin-bottom: 5px;"><tr><td valign="middle"><img src="themes/Default/images/ajax/ajax_loader.gif" /> &nbsp;Your changes are being saved.</td></tr></table>';

    // 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 original DIV text
        document.getElementById( 'PostBody' + PostID ).innerHTML = xmlhttp.responseText;

        // Replace the original edit / quote / reply buttons
        document.getElementById( 'Options' + PostID ).innerHTML = '<a href="#NULL" onclick="forum_edit_post( ' + PostID + ' );">edit</a> | <a href="#NULL" onclick="forum_reply( ' + PostID + ', true );">quote</a> | <a href="#NULL" onclick="forum_reply( ' + PostID + ', false );">reply</a>';
      }
    }

} // end function forum_edit_save()



function forum_edit_discard( PostID )
{
  // Get confirmation of the discard request
  var discard = confirm( 'Are you sure you want to discard your changes?' );

  // Only use the AJAX method if the user wants to discard the changes
  if( discard )
  {
    // Set the backend filename to process the save
    var filename = 'modules/Forums/ajax_edit_discard.php';

    // Create the parameter list
    var params = "PostID=" + PostID;

    // 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 original DIV text
        document.getElementById( 'PostBody' + PostID ).innerHTML = xmlhttp.responseText;

        // Replace the original edit / quote / reply buttons
        document.getElementById( 'Options' + PostID ).innerHTML = '<a href="#NULL" onclick="forum_edit_post( ' + PostID + ' );">edit</a> | <a href="#NULL" onclick="forum_reply( ' + PostID + ', true );">quote</a> | <a href="#NULL" onclick="forum_reply( ' + PostID + ', false );">reply</a>';
      }
    }

  } // end if( discard )

} // end function forum_edit_discard();


function forum_search_users()
{

  // Set the backend filename 
  var filename = 'modules/Forums/ajax_search_users.php';

  // Get the contents of the search form
  var vc_UserName = document.getElementById( 'vc_UserName' ).value;

  // Create the parameter list
  var params = "vc_UserName=" + vc_UserName;

  // 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) )
    {
      document.getElementById( 'UserSearch' ).innerHTML = xmlhttp.responseText;
    }
  }

} // end function forum_search_users();


function forum_attach_file()
{
  // Change the content of the upload button
  document.getElementById( 'PostFileButton' ).innerHTML = '<input type="button" class="color" value="Attach Another File" onclick="forum_attach_file();" />';

  // Set the backend filename 
  var filename = 'modules/Forums/ajax_add_attachment.php';

  // Get the token that has been generated for this element
  var token = document.getElementById( 'token' ).value;

  // Create the parameter list
  var params = "token=" + token;

  // 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 housing
      var newDiv = document.createElement( 'DIV' );

      // Insert the Flash Uploader into the new div
      newDiv.innerHTML = xmlhttp.responseText;

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


} // end function forum_attach_files()


function forum_custom_remaining( element )
{
  var txt = document.getElementById( 'forum_custom_' + element ).value;

  var len = txt.length;

  if( element == 'Title' )
  {
    var remaining = ( 40 - len );

    if( remaining < 0 )
    {
      document.getElementById( 'forum_custom_Title' ).value = document.getElementById( 'forum_custom_Title' ).value.substring( 0, 40 );
      remaining = 0;
    }

    if( remaining == 1 )
      document.getElementById( 'forum_remaining_Title' ).innerHTML = '(' + remaining + ' character remaining)';
    else
      document.getElementById( 'forum_remaining_Title' ).innerHTML = '(' + remaining + ' characters remaining)';

  }
  else if( element == 'Description' )
  {
    var remaining = ( 160 - len );

    if( remaining < 0 )
    {
      document.getElementById( 'forum_custom_Description' ).value = document.getElementById( 'forum_custom_Description' ).value.substring( 0, 160 );
      remaining = 0;
    }

    if( remaining == 1 )
      document.getElementById( 'forum_remaining_Description' ).innerHTML = '(' + remaining + ' character remaining)';
    else
      document.getElementById( 'forum_remaining_Description' ).innerHTML = '(' + remaining + ' characters remaining)';

  }


} // end function forum_custom_remaining()


function Forum_Show_Ignored( PostID )
{
  document.getElementById( 'PostCellLeft' + PostID ).style.display = 'block';
  document.getElementById( 'PostCellRight' + PostID ).style.display = 'block';
  document.getElementById( 'PostCellOptions' + PostID ).style.display =
'block';

  if( document.getElementById( 'PostCellAttachment' + PostID ) )
    document.getElementById( 'PostCellAttachment' + PostID ).style.display =
'block';

  document.getElementById( 'PostCellIgnore' + PostID ).style.display = 'none';

} // end function Forum_Show_Ignored()


function Forum_Ignore_User( UserID )
{
  // Get confirmation of the request
  var ignore = confirm( 'Are you sure you want to ignore this user?' );

  // Only use the AJAX method if the user wants to add the topic
  if( ignore )
  {
    // Set the backend filename to process the save
    var filename = 'modules/Forums/ajax_ignore_user.php';

    // Create the parameter list
    var params = "UserID=" + UserID;

    // 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) )
      {
        // Let the user know they are now ignoring a user
        alert( xmlhttp.responseText );
      }
    }

  } // end if( ignore )


} // end function Forum_Ignore_User()


function Forum_UnIgnore_User( UserID )
{
  // Get confirmation of the request
  var ignore = confirm( 'Are you sure you want to stop ignoring this user?' );

  // Only use the AJAX method if the user wants to add the topic
  if( ignore )
  {
    // Set the backend filename to process the save
    var filename = 'modules/Forums/ajax_unignore_user.php';

    // Create the parameter list
    var params = "UserID=" + UserID;

    // 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) )
      {
        // Let the user know they are now ignoring a user
        alert( xmlhttp.responseText );
      }
    }

  } // end if( ignore )


} // end function Forum_UnIgnore_User()
