function getForumSettings(fid) {
	$('#reply_editor_replace').slideUp();
	$.post("/bsiadmin/core.php",
	{ 
		action: 'getForumSettings',
		action_script: 'bsi_forum',
		fid: fid
 	}, 
 	function (data){ 
 		if(data.error) {
 			$('#reply_error').html('You cannot make setting changes to this forum.');
 		}
 		else {
	 		$('#reply_editor_replace').slideDown();
			$('#reply_editor_replace').html(data.output);
		}
	 },"json");
}

function submitForum() {
	mods = $('#moderators').val() || [];
	
	if($('#fid').val()) {
		message = 'The forum\'s settings has been successfully changed.';
	}
	else {
		message = 'The forum has been created.';
	}
	
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Processing Forum...'),
	  	ajaxComplete: showOverlay(message)
	});
	
	$.post("/bsiadmin/core.php",
	{ 
		action: 'submitForum',
		action_script: 'bsi_forum',
		title: $('#title').val(),
		desc: $('#desc').val(),
		post_perm: $('input[name=p_perms]:checked').val(),
		read_perm: $('input[name=r_perms]:checked').val(),
		forum_setting: $('input[name=forum_setting]:checked').val(),
		moderators: mods.join('|'),
		fid: $('#fid').val()
 	}, 
 	function (data){ 
 		if(data.error) {
 			$('#reply_error').html('You must enter all of the required options.');
 		}
 		else {
 			$('#reply_editor_replace').slideUp();
			getContent('bsi_forums','forum_name ASC',0,'','forums');
		}
	 },"json");
}

function submitThread(fid) {
	$.post("/bsiadmin/core.php",
	{ 
		action: 'submitThread',
		action_script: 'bsi_forum',
		title: $('#title').val(),
		content: tinyMCE.get('elm1').getContent(),
		fid: fid
 	}, 
 	function (data){ 
 		if(data.error) {
 			$('#reply_error').html('You must enter text to post.');
 		}
 		else {
 			$('#reply_editor_replace').slideUp();
 			if(data.has_permission) {
				getContent('bsi_forum_threads','pinned DESC, id DESC',0,'forum_id = \''+fid+'\'','forum_threads');
			}
			else {
				getContent('bsi_forum_threads','pinned DESC, id DESC',0,'forum_id = \''+fid+'\' AND approved = 1','forum_threads');
			}
		}
	 },"json");
}

function submitReply(fid,tid) {
	$.post("/bsiadmin/core.php",
	{ 
		action: 'submitReply',
		action_script: 'bsi_forum',
		title: $('#title').val(),
		content: tinyMCE.get('elm1').getContent(),
		fid: fid,
		tid: tid
 	}, 
 	function (data){ 
 		if(data.error) {
 			$('#reply_error').html('You must enter text to post.');
 		}
 		else {
 			if(data.has_permission) {
				getContent('bsi_forum_posts','id ASC',0,'thread_id = \''+tid+'\'','forum_posts');
			}
			else {
				getContent('bsi_forum_posts','id ASC',0,'thread_id = \''+tid+'\' AND approved = 1','forum_posts');
			}
		}
	 },"json");
}

function editReply(fid,tid,pid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Deleting Post...'),
	  	ajaxComplete: showOverlay('Your post has been edited.')
	});
	
	$.post("/bsiadmin/core.php",
	{ 
		action: 'editReply',
		action_script: 'bsi_forum',
		title: $('#title').val(),
		content: tinyMCE.get('elm1').getContent(),
		fid: fid,
		tid: tid,
		pid: pid
 	}, 
 	function (data){ 
 		if(data.error) {
 			$('#reply_error').html('You must enter text to post.');
 		}
 		else {
 			window.location = '#';
			getContent('bsi_forum_posts','id ASC',0,'thread_id = \''+tid+'\'','forum_posts');
		}
	 },"json");
}

function deleteThread(fid,tid) {
	var answer = confirm('Are you sure you want to delete this thread and all replies under it?  This cannot be undone.');
	if(answer) {
		$.ajaxSetup({
	 		global: false,
		  	type: "POST",
		  	ajaxStart: showOverlay('Deleting Thread...'),
		  	ajaxComplete: showOverlay('This thread has been deleted.')
		});
		
		$.post("/bsiadmin/core.php",
		{ 
			action: 'deleteThread',
			action_script: 'bsi_forum',
			fid: fid,
			tid: tid
	 	}, 
	 	function (data){ 
	 		if(data.error) {
	 			$('#reply_error').html('You cannot delete this thread.');
	 		}
	 		else {
				window.location = '/?page=forum_threads&id='+fid;
			}
		 },"json");
	 }
}

function approveThread(fid,tid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Approving Thread...'),
	  	ajaxComplete: showOverlay('This thread has been approved.')
	});
		
	$.post("/bsiadmin/core.php",
	{ 
		action: 'approveThread',
		action_script: 'bsi_forum',
		fid: fid,
		tid: tid
 	}, 
 	function (data){ 
 		if(data.error) {
 			$('#reply_error').html('You cannot approve this thread.');
 		}
 		else {
			getContent('bsi_forum_threads','pinned DESC, id DESC',0,'forum_id = \''+fid+'\'','forum_threads');
		}
	 },"json");
}

function deleteReply(fid,tid,pid) {
	var answer = confirm('Are you sure you want to delete this reply?  This cannot be undone.');
	if(answer) {
		$.ajaxSetup({
	 		global: false,
		  	type: "POST",
		  	ajaxStart: showOverlay('Deleting Post...'),
		  	ajaxComplete: showOverlay('Your post has been deleted.')
		});
		
		$.post("/bsiadmin/core.php",
		{ 
			action: 'deleteReply',
			action_script: 'bsi_forum',
			fid: fid,
			tid: tid,
			pid: pid
	 	}, 
	 	function (data){ 
	 		if(data.error) {
	 			$('#reply_error').html('You cannot delete this reply.');
	 		}
	 		else {
				$('#post_box_'+pid).remove();
			}
		 },"json");
	 }
}

function approveReply(fid,tid,pid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Approving Reply...'),
	  	ajaxComplete: showOverlay('This reply has been approved.')
	});
		
	$.post("/bsiadmin/core.php",
	{ 
		action: 'approveReply',
		action_script: 'bsi_forum',
		fid: fid,
		tid: tid,
		pid: pid
 	}, 
 	function (data){ 
 		if(data.error) {
 			$('#reply_error').html('You cannot approve this reply.');
 		}
 		else {
			getContent('bsi_forum_posts','id ASC',0,'thread_id = \''+tid+'\'','forum_posts');
		}
	 },"json");
}

function getEditPost(fid,tid,pid) {
	$.post("/bsiadmin/core.php",
	{ 
		action: 'getEditPost',
		action_script: 'bsi_forum',
		fid: fid,
		tid: tid,
		pid: pid
 	}, 
 	function (data){ 
 		if(data.error) {
 			$('#reply_error').html('You do not own this post.');
 		}
 		else {
 			window.location = '#reply_anchor';
	 		$('#comment_span').html('Editing post:');
			$('#reply_editor_replace').html(data.output);
		}
	 },"json");
}

function addSubscription(id,type) {
	if(type=='thread') {
		message = 'You have been subscribed to this thread.';
	} 
	else {
		message = 'You have been subscribed to this forum.';
	}
	
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Creating Subscription...'),
	  	ajaxComplete: showOverlay(message)
	});
	
	$.post("/bsiadmin/core.php",
	{ 
		action: 'addSubscription',
		action_script: 'bsi_forum',
		id: id,
		type: type
 	}, 
 	function (data){
		$('#sub_link_'+id).text('Unsubscribe');
		$('#sub_link_'+id).attr('title','Unsubscribe to stop notifications');
		$('#sub_link_'+id).attr('href','javascript:removeSubscription('+id+',\''+type+'\');');
 	},"json");
}

function removeSubscription(id,type) {
	if(type=='thread') {
		message = 'You have removed your subscription from this thread.';
	} 
	else {
		message = 'You have removed your subscription from this forum.';
	}
	
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Removing Subscription...'),
	  	ajaxComplete: showOverlay(message)
	});
	
	$.post("/bsiadmin/core.php",
	{ 
		action: 'removeSubscription',
		action_script: 'bsi_forum',
		id: id,
		type: type
 	}, 
 	function (data){ 
		$('#sub_link_'+id).text('Subscribe');
		$('#sub_link_'+id).attr('title','Get notified of new posts');
		$('#sub_link_'+id).attr('href','javascript:addSubscription('+id+',\''+type+'\');'); 		
 	},"json");
}

function addPin(tid,fid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Pinning thread...'),
	  	ajaxComplete: showOverlay('You have pinned this thread.')
	});
	
	$.post("/bsiadmin/core.php",
	{ 
		action: 'addPin',
		action_script: 'bsi_forum',
		tid: tid
 	}, 
 	function (data){ 
		getContent('bsi_forum_threads','pinned DESC, id DESC',0,'forum_id = \''+fid+'\'','forum_threads');
 	},"json");
}

function removePin(tid,fid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Removing Pin from thread...'),
	  	ajaxComplete: showOverlay('You have removed the pin from this thread.')
	});
	
	$.post("/bsiadmin/core.php",
	{ 
		action: 'removePin',
		action_script: 'bsi_forum',
		tid: tid
 	}, 
 	function (data){ 
		getContent('bsi_forum_threads','pinned DESC, id DESC',0,'forum_id = \''+fid+'\'','forum_threads');
 	},"json");
}

function addLock(tid,fid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Locking thread...'),
	  	ajaxComplete: showOverlay('You have locked this thread.')
	});
	
	$.post("/bsiadmin/core.php",
	{ 
		action: 'addLock',
		action_script: 'bsi_forum',
		tid: tid
 	}, 
 	function (data){ 
		getContent('bsi_forum_threads','pinned DESC, id DESC',0,'forum_id = \''+fid+'\'','forum_threads');
 	},"json");
}

function removeLock(tid,fid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Removing Lock from thread...'),
	  	ajaxComplete: showOverlay('You have removed the lock from this thread.')
	});
	
	$.post("/bsiadmin/core.php",
	{ 
		action: 'removeLock',
		action_script: 'bsi_forum',
		tid: tid
 	}, 
 	function (data){ 
		getContent('bsi_forum_threads','pinned DESC, id DESC',0,'forum_id = \''+fid+'\'','forum_threads');
 	},"json");
}

function getQuote(pid) {
	$.post("/bsiadmin/core.php",
	{ 
		action: 'getQuote',
		action_script: 'bsi_forum',
		pid: pid
 	}, 
 	function (data){ 
 		window.location = '#reply_anchor';
 		tinyMCE.get('elm1').setContent(data.output);
 		$('#title').val('RE: '+data.title);
 	},"json");
}
