/*

>--------------------------------------------------------------------------------<

| CREDITS INFORMATION															 |

>--------------------------------------------------------------------------------<

| Created By: Benny McLennan													 |

| File Name: forumAJAX.js														 |

| File Location: javascripts/ajax												 |

| Generic Description: All forum AJAX methods for the BMF Generic Framework	 	 |

| Date Created: May 20th 2009													 |

| Date Edited: N/A																 |

| Copyright: © Army Roster 2008 - 2009											 |

>--------------------------------------------------------------------------------<

| Website: http://www.armyroster.com											 |

| Website Restriction: Level 0 													 |

| Important Notes: Required on every page of the website. Website runs off AJAX  |

| Date Installed: May 27th 2009	 												 |

| Date Modified: N/A															 |

|																				 |

| Notes: If you wish to use this script, please feel free. No permission is 	 |

|        required. We would, however, like a brief email notifying us of your use|

| 		 of our script(s). 														 |

| 						 														 |

|		 Thank you,																 |

|		 Benny																	 |

>--------------------------------------------------------------------------------<

*/



var noErrors = true;



function topic_checkbox (ROW, Field) {

	if (document.forms.ViewTopic[Field].checked == true) {

		document.forms.ViewTopic[Field].checked = false;

		document.getElementById(ROW + 1).style.backgroundColor = ""; 

		document.getElementById(ROW + 2).style.backgroundColor = ""; 

		document.getElementById(ROW + 3).style.backgroundColor = ""; 

		document.getElementById(ROW + 4).style.backgroundColor = ""; 

		document.getElementById(ROW + 5).style.backgroundColor = ""; 

		document.getElementById(ROW + 6).style.backgroundColor = ""; 

		document.getElementById(ROW + 7).style.backgroundColor = ""; 

		document.getElementById(ROW + 8).style.backgroundColor = ""; 

		document.getElementById(ROW + 9).style.backgroundColor = ""; 

	} else {

		document.forms.ViewTopic[Field].checked = true;	

		document.getElementById(ROW + 1).style.backgroundColor = "#582323"; 

		document.getElementById(ROW + 2).style.backgroundColor = "#582323"; 

		document.getElementById(ROW + 3).style.backgroundColor = "#582323"; 

		document.getElementById(ROW + 4).style.backgroundColor = "#582323"; 

		document.getElementById(ROW + 5).style.backgroundColor = "#582323"; 

		document.getElementById(ROW + 6).style.backgroundColor = "#582323"; 

		document.getElementById(ROW + 7).style.backgroundColor = "#582323"; 

		document.getElementById(ROW + 8).style.backgroundColor = "#582323"; 

		document.getElementById(ROW + 9).style.backgroundColor = "#582323"; 

	}

}

function move_thread(Current) {

	var ModifyOption = $("select.#ModifyOption").val();

	if (ModifyOption == 1) {

		$("#MoveThreadSelection").css("display","block");

		$("#MoveThreadSelection").css("overflow","auto");

		$("#MoveThreadSelection").css("left","0");

		var MoveOption = $("#MoveOption").val();

		if (MoveOption == Current) {

			$("select.#MoveOption").val('0');

		}

	} else {

		$("#MoveThreadSelection").css("display","none");

		$("#MoveThreadSelection").css("overflow","hidden");

		$("#MoveThreadSelection").css("left","-9999em");



	}

}

function modify_thread() {

	var noErrors = true;

	var Length = document.ViewTopic.ModifyThreads.length;

	if (Length >= 2) {

	} else {

		Length = 1;	

	}

	for (var i = 0; i < Length; i++) {

		if (Length === 1) {

			var Fields = document.ViewTopic.ModifyThreads.value;

			noErrors = true;

		} else {

			if (document.ViewTopic.ModifyThreads[i].checked) {

				if (Count > 1) {

					Fields = Fields + "&&&" + document.ViewTopic.ModifyThreads[i].value;

					noErrors = true;

				} else {

					var Fields = document.ViewTopic.ModifyThreads[i].value;

					var Count = 2;

					noErrors = true;

				} 

			}

		}

	}



	var ModifyOption = $("#ModifyOption").val();

	var MoveOption = $("#MoveOption").val();

	if (ModifyOption == 1) {

		if (MoveOption == 0) {

			noErrors = false;

		} else {

			var Send = "ModifyOption=" + ModifyOption + "&Fields=" + encodeURIComponent(Fields) + "&NewTopicID=" + MoveOption;

		}

	} else if (ModifyOption == 0) {

		noErrors = false;

	} else {

		var Send = "ModifyOption=" + ModifyOption + "&Fields=" + encodeURIComponent(Fields);

	}

	// If no errors have been found, continue with the AJAX

	if (noErrors == true) {

		$.ajax({

		beforeSend: loadingScript(),

		type: "POST",

		url: "validatescripts/forums/ajax/modifythreads.inc.php",

		data: Send,

		success: function (responseText) 

			{

				if (responseText > 0) {

				newhash('action=viewtopic&TopicID=' + responseText);

				} else {

				// If the script was successful then swap out the main content of the website.

				$("#ContentMain").html(responseText);

				}

				// Prompt fake asthetic loaded

				loadedScript();

			}

		});

	}

}

// Mark all of the forums read for this current user.

function forumsread (ForumID){

	// If no forum has been set, allow for error messaging through the PHP script

	if (ForumID > 0){

		var Forum = ForumID;

	} else {

		noErrors = false;	

	}

	// If no errors have been found, continue with the AJAX

	if (noErrors == true) {

		$.ajax({

		beforeSend: loadingScript(),

		type: "POST",

		url: "validatescripts/forums/ajax/markforumsread.inc.php",

		data: "Forum=" + Forum,

		success: function (responseText) 

			{

				// If the script was successful then swap out the main content of the website.

				$("#ContentMain").html(responseText);

				// Prompt fake asthetic loaded

				loadedScript();

			}

		});

	} else {

		// Prompt fake asthetic loaded

		noErrors = true;

	}

}

// Mark all of the threads read for this current user.

function threadread (TopicID){	

	// Assign IDs and deny access for the wrong format of linkage

	if ((TopicID > 0) && (TopicID < 9999)){

		var TopicID = TopicID;

	} else {

		noErrors = false;	

	}

	// If no errors have been found, continue with the AJAX

	if (noErrors == true) {

		$.ajax({

		beforeSend: loadingScript(),

		type: "POST",

		url: "validatescripts/forums/ajax/marktopicsread.inc.php",

		data: "TopicID=" + TopicID,

		success: function (responseText) 

			{

				// If the script was successful then swap out the main content of the website.

				$("#ContentMain").html(responseText);

				// Prompt fake asthetic loaded

				loadedScript();

			}

		});

	} else {

		// Prompt fake asthetic loaded

		noErrors = true;

	}

}

// Script for editing the posts in the forums

function editpost(PostID, UserID, Anchor, Action){

	if (PostID > 0){

		var PostID = PostID;

	} else {

		noErrors = false	

	}

	if (UserID > 0){

		var UserID = UserID;

	} else {

		noErrors = false;

	}

	if (Anchor > 0){

		var Anchor = Anchor;

	} else {

		noErrors = false;

	}

	if (Action == 'CompleteEdit') {

		if (document.getElementById(PostID + "Form").ThreadName.value=="") {

			noErrors = false;

			var nameError = "<br />You must enter a thread name.";

		} else {

			var ThreadName = document.getElementById(PostID + "Form").ThreadName.value;

		}	

		if (document.getElementById(PostID + "Form").Edit.value=="") {

			noErrors = false;

			var editError = "<br />You must enter content.";

		} else {

			var Body = document.getElementById(PostID + "Form").Edit.value;

		}

		if ((document.getElementById(PostID + "Form").Description.value=="") || (document.getElementById(PostID + "Form").Description.value=="Thread Description")) {

			var Description = "";

		} else {

			var Description = document.getElementById(PostID + "Form").Description.value;

		}	

		if (noErrors == true) {

			var Send = "Body=" + encodeURIComponent(Body) + "&PostID=" + PostID + "&Anchor=" + Anchor + "&Description=" + encodeURIComponent(Description) + "&ThreadName=" + encodeURIComponent(ThreadName) + "&Action=" + Action;

		}

	} else {

		var Send = "PostID=" + PostID + "&UserID=" + UserID + "&Anchor=" + Anchor + "&Action=" + Action;

	}

	if (noErrors == true) {

		$.ajax({

		beforeSend: loadingScript(),

		type: "POST",

		url: "validatescripts/forums/ajax/editpost.inc.php",

		data: Send,

		success: function (responseText) 

			{

				if (responseText > 0) {

					$("#newthread").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Access Denied");

					$("#newthread").html("<table cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"TablePopUp\" valign=\"top\" style=\"padding: 0px 12px 0px 13px; background-color: #3e3b35;\"><table cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"CellTitle\" style=\"border-right: 1px solid #000;\"><span class=\"whitetitletext\">Access Denied</span></td></tr><tr><td class=\"CellEven\" style=\"border: 1px solid #000; border-top: 1px solid #000\"><span class=\"yellowtext\"><b>Insufficient Access</b></span><br />You may also be attempting to access a page that has been either shut down for development or closed to certain members.<br /><br /><span class=\"yellowtext\"><b> - OR - </b><br /><br /><b>Not Logged In</b></span><br />We are sorry, but you are attempting to access a page that is restricted. Please make sure you are logged in. If you would like to avoid re-entering your login information every time you visit, then please use the \"Remember Me\" option just below the login fields in the control panel.<br /><br />There are many features at Army Roster that require you to login to use and view. If you have not yet registered then please do so <a href=\"home.php#action=register\" onclick=\"newhash(\'action=register\'); return false;\" title=\"Register\">here</a>!<br /><br />- Army Roster Administration Team.</td></tr></table></td></tr><tr><td class=\"TablePopUpFooter\" valign=\"top\" style=\"padding: 0px 12px 0px 13px; background-color: #3e3b35;\"></td></tr></table>");

					$('#newthread').dialog('open');			

					$("#newthread").dialog({

						bgiframe: false,

						closeOnEscape: true,

						draggable: true,

						resizable: false,

						width: 632,

						modal: true,

					});

/*					alert("Please Report Error Number: " + responseText);

*/				} else {

					// If the script was successful then swap out the main content of the website.

					if (Action == 'EditPost') {

						$("#" + Anchor).html("<tr><td><div id=\"" + PostID + "Error\"> </div><form id=\"" + PostID + "Form\" method=\"post\"><table cellpadding=\"0\" cellspacing=\"0\">" + responseText + "</table></form></td></tr>");

						scrolltotop.scrollup(PostID + "Error");

					} else if (Action == 'DeletePost') {

						$("#" + Anchor).html(responseText);

					} else if (Action == 'UndeletePost') {

						$("#" + Anchor).html(responseText);

					} else if (Action == 'CancelEdit') {

						$("#" + Anchor).html(responseText);

					} else 	if (Action == 'CompleteEdit') {

							

						if (responseText == 2) {

							$("#" + PostID + "Error").html("<br /><span class=\"redtext\">The following error occurred:</span><span class=\"yellowtext\"><br />You must enter a thread name</span>");

						} else if (responseText == 3) {

							$("#" + PostID + "Error").html("<br /><span class=\"redtext\">The following error occurred:</span><span class=\"yellowtext\"><br />The thread name must be at least 4 characters and may contain the following : _ , . , \' , - , & , &#163;, &euro;, ! , ? , and commas.</span>");

						} else if (responseText == 4) {

							$("#" + PostID + "Error").html("<br /><span class=\"redtext\">The following error occurred:</span><span class=\"yellowtext\"><br />The thread description must be at least 4 characters and may contain the following : _ , . , \' , - , & , &#163;, &euro;, ! , ? , and commas.</span>");

						} else if (responseText == 4) {

							$("#" + PostID + "Error").html("<br /><span class=\"redtext\">The following error occurred:</span><span class=\"yellowtext\"><br />You must enter content.</span>");

						} else {

							$("#" + Anchor).html("<div id=\"" + Anchor + "Scroll\"> </div>" + responseText);

							scrolltotop.scrollup(Anchor + "Scroll");

						}

					}

				}

				// Prompt fake asthetic loaded

				loadedScript();

			}

		});

	} else {

		$("#" + PostID + "Error").html("<table cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"CellEven\" style=\"border-right: 1px solid #000\"><div style=\"width: 596px;\"><span class=\"redtext\">The following error occurred:</span><span class=\"yellowtext\">".concat(nameError, editError) + "</span></div></td></tr></table>");

	}

}



function postreply(ThreadID, QuoteID, Action) {

	var noErrors = true;

	var Body = "";

	

	if (Action == 'CompleteReply') {

		if ($("textarea.#ReplyPost").val()=="") {

			noErrors = false;

			$("#ReplyError").html("<table cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"CellEven\" style=\"border-right: 1px solid #000\"><div style=\"width: 596px;\"><span class=\"redtext\">The following error occurred:</span><span class=\"yellowtext\"><br />You must enter content.</span></div></td></tr></table>");

			$("#ReplyError").css("marginTop","10px");

		} else {

			Body = $("textarea.#ReplyPost").val();

		}

		var Send = "ThreadID=" + ThreadID + "&QuoteID=" + QuoteID + "&Action=" + Action + "&Body=" + encodeURIComponent(Body);

	} else {

		var Send = "ThreadID=" + ThreadID + "&QuoteID=" + QuoteID + "&Action=" + Action;

	}

	if (noErrors == true) {

		$.ajax({

		beforeSend: loadingScript(),

		type: "POST",

		url: "validatescripts/forums/ajax/postreply.inc.php",

		data: Send,

		error: function() {

			error_message()

		},

		success: function (responseText) 

			{

				// If the script was successful then swap out the main content of the website.

				if ((Action == 'PostReply') || (Action == 'PostQuote')) {

					if (responseText > 0) {

						//alert(responseText)

						$("#newthread").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Access Denied");

						$("#newthread").html("<table cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"TablePopUp\" valign\"top\" style=\"padding: 0px 12px 0px 13px; background-color: #3e3b35;\"><table cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"CellTitle\" style=\"border-right: 1px solid #000;\"><span class=\"whitetitletext\">Access Denied</span></td></tr><tr><td class=\"CellEven\" style=\"border: 1px solid #000; border-top: 1px solid #000\"><span class=\"yellowtext\"><b>Insufficient Access</b></span><br />You may also be attempting to access a page that has been either shut down for development or closed to certain members.<br /><br /><span class=\"yellowtext\"><b> - OR - </b><br /><br /><b>Not Logged In</b></span><br />We are sorry, but you are attempting to access a page that is restricted. Please make sure you are logged in. If you would like to avoid re-entering your login information every time you visit, then please use the \"Remember Me\" option just below the login fields in the control panel.<br /><br />There are many features at Army Roster that require you to login to use and view. If you have not yet registered then please do so <a href=\"home.php#action=register\" onclick=\"newhash('action=register'); return false;\" title=\"Register\">here</a>!<br /><br />- Army Roster Administration Team.</td></tr></table></td></tr><tr><td class=\"TablePopUpFooter\" valign=\"top\" style=\"padding: 0px 12px 0px 13px; background-color: #3e3b35;\"></td></tr></table>");

						$('#newthread').dialog('open');	

						$("#newthread").dialog({

							bgiframe: false,

							closeOnEscape: true,

							draggable: true,

							resizable: false,

							width: 632,

							modal: true,

						});

					} else {

						$("#ReplyError").css("marginTop","10px");

						$("#ReplyBox").html(responseText);

						scrolltotop.scrollup("ReplyError");

					}

					

				} else if (Action == 'CancelReply') {

					scrolltotop.scrollup("Cancel" + QuoteID);

					$("#ReplyError").html(' ');

					$("#ReplyBox").html(' ');

					$("#ReplyError").css("marginTop","0px");					

				} else if (Action == 'CompleteReply') {

					newhash(responseText);

				}

				loadedScript();

			}

		});

	}

}

// Script for editing the posts in the forums

function newthread (TopicID, Action){

	var noErrors = true;

	var ThreadName = "";

	var Description = "";

	var Edit = "";

	var Notice = "";

	var threadnameError = "";

	var editError = "";

	var i = "";

	var Send = "";

	

	if (TopicID > 0){

		TopicID = TopicID;

	} else {

		noErrors = false;

	}

	if (Action == 'SubmitThread'){

		if ((document.getElementById("NewThread").ThreadName.value=="") || (document.getElementById("NewThread").ThreadName.value=="Thread Name") || (document.getElementById("NewThread").ThreadName.value=="Please enter a thread name")) {

			noErrors = false;

			threadnameError = "Please enter a thread name<br />";

			document.getElementById("NewThread").ThreadName.style.color = "#ff0000";

			document.getElementById("NewThread").ThreadName.value = "Please enter a thread name";

		} else {

			ThreadName = document.getElementById("NewThread").ThreadName.value;

		}	

		if ((document.getElementById("NewThread").Description.value=="") || (document.getElementById("NewThread").Description.value=="Thread Description")) {

		} else {

			Description = document.getElementById("NewThread").Description.value;

		}	

		if (document.getElementById("NewThread").Edit.value=="") {

			noErrors = false;

			editError = "Please enter content<br />";

		} else {

			Edit = document.getElementById("NewThread").Edit.value;

		}

		if (document.getElementById("NewThread").NoticeHidden.value=="None") {

			Notice = "None";

		} else {

			for (i = 0; i < document.Thread.Notice.length; i++) {

				if (document.Thread.Notice[i].checked){

					Notice = document.Thread.Notice[i].value;

				}

			}

		}

		Send = "TopicID=" + TopicID + "&Action=" + encodeURIComponent(Action) + "&ThreadName=" + encodeURIComponent(ThreadName) + "&Description=" + encodeURIComponent(Description) + "&Edit=" + encodeURIComponent(Edit) + "&Notice=" + Notice;

	} else if (Action == 'NewThread') {

		Send = "TopicID=" + TopicID + "&Action=" + encodeURIComponent(Action);

	}

	if (noErrors == true) {

		$.ajax({

		beforeSend: loadingScript(),

		type: "POST",

		url: "validatescripts/forums/ajax/newthread.inc.php",

		data: Send,

		error: function() {

			error_message()

		},

		success: function (responseText) 

			{

				// If the script was successful then swap out the main content of the website.

				if (Action == 'NewThread') {

					$("#newthread").html(responseText);

					$('#newthread').dialog('open');			

					$("#newthread").dialog({

						bgiframe: false,

						closeOnEscape: true,

						draggable: true,

						resizable: false,

						width: 632,

						modal: true,

					});

				} else {

					

					if (responseText == 1) {

						$("#ThreadError").html("<span class=\"redtext\">The following errors occurred:</span><br /><span class=\"yellowtext\"><br />Enter a valid thread name</span>");

					} else if (responseText == 2) {

						$("#ThreadError").html("<span class=\"redtext\">The following errors occurred:</span><br /><span class=\"yellowtext\"><br />Enter a valid thread description</span>");

					} else if (responseText == 3) {

						$("#ThreadError").html("<span class=\"redtext\">The following errors occurred:</span><br /><span class=\"yellowtext\"><br />Please enter content</span>");

					} else {

						newhash(responseText);

					}

				}

			loadedScript();

			}

		});

	} else {

		$("#ThreadError").html("<span class=\"redtext\">The following errors occurred:</span><br /><span class=\"yellowtext\"><br />".concat(threadnameError,editError) + "</span>");

	}

}

