function showNewForumForm() {
	hideBoxes();
	if ($('newForum').style.display == 'none') {
		Element.show($('newForum'));
	}
	gotoAnchor('newForum');
	$('newForum').highlight({queue:'end'});
	$('newForumTitleInput').value = '';
	$('newForumTitleInput').focus();
}

function createForum(form) {
	var messageDiv = $('newForumMessage');
	url = 'forum_addForum.php';
	pars = Form.serialize(form);
	pars += '&isjs=1';
	Form.disable(form);
	
	messageDiv.innerHTML = 'Submitting...';
	
	var unlockTimeout = setTimeout(function() {
		Form.enable(form);
	}, 30000);
	
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				messageDiv.innerHTML = status.messages;
				location.href = status.redirect;
			}
			else {
				messageDiv.innerHTML = status.errors;
			}
			Form.enable(form);
			clearTimeout(unlockTimeout);
		}
	});
}

function showNewCategoryForm(forumId) {
	hideBoxes();
	if ($('newCategory').style.display == 'none') {
		Element.show($('newCategory'));
	}
	$('newCategoryForumId').value = forumId;
	$('newCategoryIn').innerHTML = $('forumTitle-'+forumId).innerHTML;
	gotoAnchor('newCategory');
	$('newCategory').highlight({queue:'end'});
	$('newCategoryTitleInput').value = '';
	$('newCategoryTitleInput').focus();
}

function createCategory(form) {
	var messageDiv = $('newCategoryMessage');
	url = 'forum_addCategory.php';
	pars = Form.serialize(form);
	pars += '&isjs=1';
	Form.disable(form);
	
	messageDiv.innerHTML = 'Submitting...';
	
	var unlockTimeout = setTimeout(function() {
		Form.enable(form);
	}, 30000);
	
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				messageDiv.innerHTML = status.messages;
				location.href = status.redirect;
			}
			else {
				messageDiv.innerHTML = status.errors;
			}
			Form.enable(form);
			clearTimeout(unlockTimeout);
		}
	});
}

function showEditForumForm(forumId) {
	hideBoxes();
	$('editForumTitleInput').value = $('forumTitle-' + forumId).innerHTML;
	$('editForumTitleInput').focus();
	$('editForumId').value = forumId;
	Element.show($('editForum'));
	gotoAnchor('editForum');
	$('editForum').highlight({queue:'end'});
}
function editForum(form) {
	var messageDiv = $('editForumMessage');
	url = 'forum_editForum.php';
	pars = Form.serialize(form);
	pars += '&isjs=1';
	Form.disable(form);
	messageDiv.innerHTML = 'Submitting...';
	
	var unlockTimeout = setTimeout(function() {
		Form.enable(form);
		$('editForumMessage').innerHTML = 'There was a problem submitting your form. Please try again';
	}, 30000);
	
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				messageDiv.innerHTML = status.messages;
				$('forumTitle-' + status.forumId).innerHTML = status.title;
				setTimeout(function() {
					new Effect.BlindUp($('editForum'), {
						afterFinish:function() {
							gotoAnchor('forumContainer_'+status.forumId);
							$('editForumMessage').innerHTML = '';
						}
					});
				}, 1000);
				//location.href = status.redirect;
			}
			else {
				messageDiv.innerHTML = status.errors;
			}
			Form.enable(form);
			clearTimeout(unlockTimeout);
		}
	});
}
function showDeleteForumForm(forumId) {
	hideBoxes();
	$('deleteForumTitleInput').value = $('forumTitle-' + forumId).innerHTML;
	$('deleteForumTitleHeader').innerHTML = $('forumTitle-' + forumId).innerHTML;
	$('deleteForumTitleInput').focus();
	$('deleteForumId').value = forumId;
	Element.show($('deleteForum'));
	gotoAnchor('deleteForum');
	$('deleteForum').highlight({queue:'end'});
}
function deleteForum(form) {
	var messageDiv = $('deleteForumMessage');
	url = 'forum_deleteForum.php';
	pars = Form.serialize(form);
	pars += '&isjs=1';
	Form.disable(form);
	messageDiv.innerHTML = 'Submitting...';
	
	var unlockTimeout = setTimeout(function() {
		Form.enable(form);
		$('deleteForumMessage').innerHTML = 'There was a problem submitting your form. Please try again';
	}, 30000);
	
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				messageDiv.innerHTML = status.messages;
				console.log(transport.responseText);
				if (status.cancel) {
					Element.hide($('deleteForum'));
				}
				else {
					setTimeout(function() {
						new Effect.Fade(('forumContainer_'+status.forumId));
						new Effect.BlindUp($('deleteForum'), {
							afterFinish:function() {
								$('deleteForumMessage').innerHTML = '';
							}
						});
					}, 1000);
				}
			}
			else {
				messageDiv.innerHTML = status.errors;
			}
			Form.enable(form);
			clearTimeout(unlockTimeout);
		}
	});
}
function hideBoxes() {
	Element.hide($('newForum'));
	Element.hide($('editForum'));
	Element.hide($('deleteForum'));
	Element.hide($('newCategory'));
	Element.hide($('editCategory'));
	Element.hide($('deleteCategory'));
}

////

function showEditCategoryForm(categoryId) {
	hideBoxes();
	$('editCategoryTitleInput').value = $('categoryTitle-' + categoryId).innerHTML;
	$('editCategoryDescription').value = $('categoryDescription-' + categoryId).innerHTML;
	$('editCategoryTitleHeader').innerHTML = $('categoryTitle-' + categoryId).innerHTML;
	$('editCategoryTitleInput').focus();
	$('editCategoryId').value = categoryId;
	Element.show($('editCategory'));
	gotoAnchor('editCategory');
	$('editCategory').highlight({queue:'end'});
}
function editCategory(form) {
	var messageDiv = $('editCategoryMessage');
	url = 'forum_editCategory.php';
	pars = Form.serialize(form);
	pars += '&isjs=1';
	Form.disable(form);
	messageDiv.innerHTML = 'Submitting...';
	
	var unlockTimeout = setTimeout(function() {
		Form.enable(form);
		$('editCategoryMessage').innerHTML = 'There was a problem submitting your form. Please try again';
	}, 30000);
	
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				messageDiv.innerHTML = status.messages;
				$('categoryTitle-' + status.categoryId).innerHTML = status.title;
				$('categoryDescription-' + status.categoryId).innerHTML = status.description;
				setTimeout(function() {
					new Effect.BlindUp($('editCategory'), {
						afterFinish:function() {
							$('editCategoryMessage').innerHTML = '';
						}
					});
				}, 1000);
				//location.href = status.redirect;
			}
			else {
				messageDiv.innerHTML = status.errors;
			}
			Form.enable(form);
			clearTimeout(unlockTimeout);
		}
	});
}

/**/

function showDeleteCategoryForm(categoryId) {
	hideBoxes();
	$('deleteCategoryTitleInput').value = $('categoryTitle-' + categoryId).innerHTML;
	$('deleteCategoryTitleHeader').innerHTML = $('categoryTitle-' + categoryId).innerHTML;
	$('deleteCategoryTitleInput').focus();
	$('deleteCategoryId').value = categoryId;
	Element.show($('deleteCategory'));
	gotoAnchor('deleteCategory');
	$('deleteCategory').highlight({queue:'end'});
}
function deleteCategory(form) {
	var messageDiv = $('deleteCategoryMessage');
	url = 'forum_deleteCategory.php';
	pars = Form.serialize(form);
	pars += '&isjs=1';
	Form.disable(form);
	messageDiv.innerHTML = 'Submitting...';
	
	var unlockTimeout = setTimeout(function() {
		Form.enable(form);
		$('deleteCategoryMessage').innerHTML = 'There was a problem submitting your form. Please try again';
	}, 30000);
	
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				messageDiv.innerHTML = status.messages;
				if (status.cancel) {
					Element.hide($('deleteCategory'));
				}
				else {
					setTimeout(function() {
						new Effect.Fade(('category_'+status.categoryId));
						new Effect.BlindUp($('deleteCategory'), {
							afterFinish:function() {
								$('deleteCategoryMessage').innerHTML = '';
							}
						});
					}, 1000);
				}
			}
			else {
				messageDiv.innerHTML = status.errors;
			}
			Form.enable(form);
			clearTimeout(unlockTimeout);
		}
	});
}

/**/

function showReplyForm() {
	if ($('replyFormContainer').style.display == 'none') {
		Element.show($('replyFormContainer'));
	}
	gotoAnchor('replyFormContainer');
	$('replyFormContainer').highlight({queue:'end'});
	$('forumPostTextarea').value = '';
	$('forumPostTextarea').focus();
	setupEditBox();
}
function submitReply(form) {
	var messageDiv = $('replyMessage');
	url = 'forum_addPost.php';
	if (!$('forumPostTextarea').value.length) {
		$('forumPostTextarea').value = tinyMCE.get('forumPostTextarea').getContent();
	}
	pars = Form.serialize(form);
	pars += '&isjs=1';
	Form.disable(form);
	
	messageDiv.innerHTML = 'Submitting...';
	
	var unlockTimeout = setTimeout(function() {
		Form.enable(form);
	}, 30000);
	
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				messageDiv.innerHTML = status.messages;
				location.href = status.redirect;
			}
			else {
				messageDiv.innerHTML = status.errors;
			}
			Form.enable(form);
			clearTimeout(unlockTimeout);
		}
	});
}

function showThreadEdit() {
	if ($('threadEdit').style.display == 'none') {
		Element.show($('threadEdit'));
	}
	gotoAnchor('threadEdit');
	$('threadEdit').highlight({queue:'end'});
	$('editThreadTitleInput').focus();
}

function editThread(form) {
	var messageDiv = $('editThreadMessage');
	url = 'forum_editThread.php';
	pars = Form.serialize(form);
	pars += '&isjs=1';
	Form.disable(form);
	
	messageDiv.innerHTML = 'Submitting...';
	
	var unlockTimeout = setTimeout(function() {
		Form.enable(form);
	}, 30000);
	
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				messageDiv.innerHTML = status.messages;
				setTimeout(function() {
					new Effect.BlindUp($('threadEdit'), {
						afterFinish:function() {
							$('editThreadMessage').innerHTML = '';
							$('editThreadTitleInput').value = '';
						}
					});
				}, 1000);
			}
			else {
				messageDiv.innerHTML = status.errors;
			}
			Form.enable(form);
			clearTimeout(unlockTimeout);
		}
	});
}


function showThreadDelete() {
	if ($('deleteThread').style.display == 'none') {
		Element.show($('deleteThread'));
	}
	gotoAnchor('deleteThread');
	$('deleteThread').highlight({queue:'end'});
	//$('deleteThreadTitleHeader').innerHTML = 
}

function deleteThread(form) {
	var messageDiv = $('deleteThreadMessage');
	url = 'forum_deleteThread.php';
	pars = Form.serialize(form);
	pars += '&isjs=1';
	Form.disable(form);
	
	messageDiv.innerHTML = 'Submitting...';
	
	var unlockTimeout = setTimeout(function() {
		Form.enable(form);
	}, 30000);
	
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				messageDiv.innerHTML = status.messages;
				setTimeout(function() {
					new Effect.BlindUp($('deleteThread'), {
						afterFinish:function() {
							$('deleteThreadMessage').innerHTML = '';
						}
					});
				}, 1000);
				location.href = status.redirect;
			}
			else {
				messageDiv.innerHTML = status.errors;
			}
			Form.enable(form);
			clearTimeout(unlockTimeout);
		}
	});
}

function showNewThreadForm() {
	if ($('newThread').style.display == 'none') {
		Element.show($('newThread'));
	}
	gotoAnchor('newThread');
	$('newThread').highlight({queue:'end'});
	$('newThreadTitleInput').value = '';
}

function createThread(form) {
	$('newThreadMessageInput').value = tinyMCE.get('newThreadMessageInput').getContent();
	var messageDiv = $('newThreadMessage');
	url = 'forum_addThread.php';
	pars = Form.serialize(form);
	pars += '&isjs=1';
	Form.disable(form);
	
	messageDiv.innerHTML = 'Submitting...';
	
	var unlockTimeout = setTimeout(function() {
		Form.enable(form);
	}, 30000);
	
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				messageDiv.innerHTML = status.messages;
				location.href = status.redirect;
			}
			else {
				messageDiv.innerHTML = status.errors;
			}
			Form.enable(form);
			clearTimeout(unlockTimeout);
		}
	});
}

/**/
function deletePost(postId, forumId, categoryId){
	var url = 'forum_deletePost.php';
	var pars = 'postId=' + postId + '&forumId=' + forumId + '&categoryId=' + categoryId;
	
	var answer = confirm("Are you sure you want to delete this post?");
	if(answer){
		myAjax = new Ajax.Request(url, {
			method: 'post',
			postBody: pars,
			onLoading: function() { },
			onComplete: function(transport) {
				var status = transport.responseJSON;
				if (status.good) {
					new Effect.Fade(('post' + postId));
				}
				else {

				}
			}
		});
	}
}

function setupEditBox() {
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		plugins: 'inlinepopups,spellchecker,paste',
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,link,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,spellchecker,|,cut,copy,paste,pastetext,pasteword",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		content_css : "/css/styles.css"
	});
}

function editPost(postId, userId){
	if($('editPost-' + postId) == null){
		var contentEl = $('postData-' + postId);
		var contentHTML = contentEl.innerHTML;
		
		//Create text Area and Buttons
		var textArea = '<textarea id="editPost-'+postId+'" style="width:90%; height:150px;"></textarea>';
		var oldPostData = '<input type="hidden" id="oldPostData-'+postId+'" \>';
		var saveButton = '<input type="button" name="savePost" value="Save" onclick="savePost(\''+postId+'\', \''+userId+'\');" />';
		var closeButton = '<input type="button" name="closePost" value="Close" onclick="restorePost(\''+postId+'\');" />';
		
		contentEl.innerHTML = '<div>' + textArea + '<br />' + oldPostData + saveButton + ' ' + closeButton + '</div>';
		$('editPost-' + postId).value = contentHTML;
		$('oldPostData-'+postId).value = contentHTML;
		setupEditBox();
	}
}

function restorePost(postId){
	var contentEl = $('postData-' + postId);
	var oldPost = $('oldPostData-' + postId).value;
	contentEl.innerHTML = oldPost;
	
}

function savePost(postId, userId){
	var editUserId = userId;
	var contentEl = $('postData-' + postId);
	$('editPost-' + postId).value = tinyMCE.get('editPost-' + postId).getContent();
	var newPost = $('editPost-' + postId).value;
	var dateEl = $('postDate-' + postId);
	
	contentEl.innerHTML = newPost;
	dateEl.innerHTML = makeDate();
	
	//Set up ajax items
	var url = 'forum_editPost.php';
	var pars = 'postId=' + postId + '&message=' + urlEncode(newPost) + '&editUserId=' + editUserId;
	
	
	//Do an ajax call to update
	myAjax = new Ajax.Request(url, {
		method: 'post',
		postBody: pars,
		onLoading: function() { },
		onComplete: function(transport) {
			var status = transport.responseJSON;
			if (status.good) {
				//Set the date string
			}
			else {

			}
		}
	});
	
}

function makeDate(){
	var d = new Date();
	var month = d.getMonth();
	var day = d.getDay();
	var year = d.getFullYear();
	var hour = d.getHours();
	var minute = d.getMinutes();
	var ampm;
	var returnString = '';
	
	//Correct Hours
	if(hour > 12){
		hour -= 12;
	}
	
	//Set AMPM
	if(hour >= 12){
		ampm = 'pm';
	}
	else{
		ampm = 'am';
	}
	
	//Create String
	returnString = ' <b>edited</b> on ' + month+'/'+day+'/'+year+' '+hour+':'+minute+ampm;
	return returnString;
	
}
/**/