/*
>--------------------------------------------------------------------------------<
| CREDITS INFORMATION															 |
>--------------------------------------------------------------------------------<
| Created By: Benny McLennan													 |
| File Name: mainAJAX.js														 |
| File Location: javascripts/ajax												 |
| Generic Description: All mainly used 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;
var RememberMe = 2;

function checkOption(id) {
	if ($("#"+id).val() == 'true') {
		$("#"+id).val("false")
	} else {
		$("#"+id).val("true")
	}
}
function error_message(){
	$('#senderror').dialog('destroy');		
	$('#senderror').dialog('open');			
	$("#senderror").dialog({
		bgiframe: false,
		closeOnEscape: true,
		draggable: true,
		resizable: false,
		width: 632,
		modal: true,
	});	
}
// Login script
function logger() {
	if (($("#Username").val()=="") || ($("#Username").val()=="Username") || ($("#Username").val()=="Enter username")) {
		noErrors = false;
		var usernameError = "Enter username<br />";
	} else {
		var usernameError = "";
		var username = $("#Username").val();
	}
	if (($("#Password").val()=="") || ($("#Password").val()=="Password") || ($("#Password").val()=="Enter password")) {
		noErrors = false;
		var passwordError = "Enter password<br />";
	}  else {
		var passwordError = "";
		var password = $("#Password").val();
	}
	if ($("#RememberMe").val() == 'true') {
		var RememberMe = 1;
	} else {
		var RememberMe = 2;
	}
	if (noErrors == true) {

		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/main/ajax/login.inc.php",
		data: "Username=" + username + "&Password=" + password + "&RememberMe=" + RememberMe,
		error: function() {
			error_message()
		},
		success: function (responseText) 
			{
				if (responseText == 69) {
					$.ajax({
					beforeSend: loadingScript(),
					type: "POST",
					url: "validatescripts/main/ajax/loggedin.inc.php",
					error: function() {
						error_message()
					},
					success: function (responseText) 
						{
							$("#ControlPanelBody").html(responseText);
							AR_getter();
						}
					});

				} else {
					$("#LoginBlurb").html("<span class=\"yellowtext\">" + responseText + "</span>");
					loadedScript()
				}
			}
		});
	} else {
		noErrors = true	;
		$("#LoginBlurb").html("<span class=\"yellowtext\">".concat(usernameError,passwordError) + "</span>");
	}
}
function sessioncontroller() {
	$.ajax({
	type: "POST",
	url: "validatescripts/main/ajax/sessioncontrol.inc.php",
	data: "Sent=1",
	success: function (responseText) 
		{
			$("#ControlPanelBody").html(responseText);
		}
	});
	$.ajax({
	type: "POST",
	url: "validatescripts/main/ajax/navigationcontrol.inc.php",
	data: "Sent=1",
	success: function (responseText) 
		{
			$("#NavBody").html(responseText);
		}
	});
	$.ajax({
	type: "POST",
	url: "validatescripts/main/ajax/recentthreadscontrol.inc.php",
	data: "Sent=1",
	success: function (responseText) 
		{
			$("#RecentThreads").html(responseText);
		}
	});
	loadedScript();
}
function member_profile(MemberID, MemberName) {
	if (MemberID > 0){
	} else {
		noErrors = false;	
	}

	if (noErrors == true) {
		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/profiles/ajax/memberprofile.inc.php",
		data: "MemberID=" + encodeURIComponent(MemberID),
		success: function (responseText) 
			{
				$('#dialogprofile').dialog('destroy');		
				$("#dialogprofile").html(responseText);
				$("#dialogprofile").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Member Profile - " + MemberName);
				$('#dialogprofile').dialog('open');			
				$("#dialogprofile").dialog({
					bgiframe: false,
					closeOnEscape: true,
					draggable: true,
					resizable: false,
					width: 632,
					modal: true,
				});
				loadedScript()
			}
		});
	} else {
	noErrors = true	;
	}
}
function submit_comments(MemberID, UserID) {
	if ($("textarea.#profilecomment").val().length > 500) {
		noErrors = false;
		$("#commentError").html("Max length of 500 characters <span class=\"yellowtext\">Your comment is too long.</span>");
	}  else {
		if ($("textarea.#profilecomment").val() === "") {
			noErrors = false;
			$("#commentError").html("Max length of 500 characters <span class=\"yellowtext\">You must enter a comment.</span>");
		}  else {
			var Comment = $("textarea.#profilecomment").val();
		}
	}
	if (noErrors == true) {
		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/profiles/ajax/profilecomments.inc.php",
		data: "MemberID=" + encodeURIComponent(MemberID) + "&UserID=" + encodeURIComponent(UserID) + "&Comment=" + encodeURIComponent(Comment) + "&Action=submit",
		success: function (responseText) 
			{
				$("#PostComments").attr("class", "CellLink");
				$("#ProfileComments").attr("class", "CellLinkOpt");
				$("#Comments").html(responseText);
				loadedScript()
			}
		});
	} else {
	noErrors = true	;
	}
}
function post_comments(MemberID, PostComment) {
	if (MemberID > 0){
	} else {
		noErrors = false;	
	}
	if (noErrors == true) {
		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/profiles/ajax/profilecomments.inc.php",
		data: "MemberID=" + encodeURIComponent(MemberID) + "&PostComment=" + encodeURIComponent(PostComment),
		success: function (responseText) 
			{
				if (PostComment === 'ListComments') {
					$("#PostComments").attr("class", "CellLink");
					$("#ProfileComments").attr("class", "CellLinkOpt");
				}
				if (PostComment === 'PostComment') {
					$("#PostComments").attr("class", "CellLinkOpt");
					$("#ProfileComments").attr("class", "CellLink");
				}
				$("#Comments").html(responseText);
				loadedScript()
			}
		});
	} else {
	noErrors = true	;
	}
}
function delete_comments(CommentID, MemberID) {
	if (CommentID > 0){
	} else {
		noErrors = false;	
	}
	if (MemberID > 0){
	} else {
		noErrors = false;	
	}
	if (noErrors == true) {
		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/profiles/ajax/deletecomments.inc.php",
		data: "CommentID=" + encodeURIComponent(CommentID) + "&MemberID=" + encodeURIComponent(MemberID),
		success: function (responseText) 
			{
				$("#Comments").html(responseText);
				loadedScript()
			}
		});
	} else {
	noErrors = true	;
	}
}
function kegsignup() {
	noErrors = true;
	if (($("#EmailListing").val()=="") || ($("#EmailListing").val()=="Email Address") || ($("#EmailListing").val()=="Please enter your email address")) {
		noErrors = false;
		$("#PowderKegMailError").css("color","#ffff00");
		var emailError = "Enter your email address<br />";
		$("#PowderKegMailError").html(emailError);
		if ($("#EmailListing").val()=="") {
			$("#EmailListing").css("color","#ff0000");
			$("#EmailListing").html('Please enter your email address');
		}
		loadedScript();
	}  else {
		var emailError = "";
		var email = $("#EmailListing").val();
	}
	if (noErrors == true) {
		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/main/ajax/powderkegmail.inc.php",
		data: "EmailAddress=" + encodeURIComponent(email),
		success: function (responseText) 
			{
				if (responseText == 69) {
					$("#PowderKegMail").html("<table class=\"TableSmall\" cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"CellTitle\"><span class=\"whitetitletext\">Thanks!</span></td></tr><tr><td class=\"CellEven\">You have been added to The Powder Keg's mailing list. You will recieve updates of all the updates for The Powder Keg.</td></tr><tr><td class=\"CellFooter\"></td></tr></table>");
					loadedScript();
				} else {
					$("#PowderKegMailError").html("<span class=\"yellowtext\">" + responseText + "</span>");
					loadedScript();
				}
				
			}
		});
	}
}

function update_profile(action) {
	var noErrors = true;
	$("#UploadError").html("");
	$("#UploadError").css("padding","0px");
	if (action === 'generic') {
		
		if (($("#FirstName").val()=="") || ($("#FirstName").val()=="First Name")) {
			var firstName = "N/A";
		} else {
			var firstName = $("#FirstName").val();
		}
		if (($("#LastName").val()=="") || ($("#LastName").val()=="Last Name")) {
			var lastName = "N/A";
		} else {
			var lastName = $("#LastName").val();
		}
		if (($("#City").val()=="") || ($("#City").val()=="City")) {
			var city = "N/A";
		} else {
			var city = $("#City").val();
		}
		if (($("#State").val()=="") || ($("#State").val()=="State/Province")) {
			var state = "N/A";
		} else {
			var state = $("#State").val();
		}
		if (($("#Country").val()=="") || ($("#Country").val()=="Country")) {
			var country = "N/A";
		} else {
			var country = $("#Country").val();
		}
		if (($("#CustomTitle").val()=="") || ($("#CustomTitle").val()=="Custom Title")) {
			var customTitle = "N/A";
		} else {
			var customTitle = $("#CustomTitle").val();
		}
		if ($("#HideAge").val() == 'true') {
			var hideAge = 1;
		} else {
			var hideAge = 0;
		}
		if ($("#HideLocation").val() == 'true') {
			var hideLocation = 1;
		} else {
			var hideLocation = 0;
		}
		var day = $("#BirthDay").val();
		var month = $("#BirthMonth").val();
		var year = $("#BirthYear").val();
		
		var Send = "Action=" + action + "&FirstName=" + encodeURIComponent(firstName) + "&LastName=" + encodeURIComponent(lastName) + "&BirthDay=" + day + "&BirthMonth=" + month + "&BirthYear=" + year + "&City=" + encodeURIComponent(city) + "&State=" + encodeURIComponent(state) + "&Country=" + encodeURIComponent(country) + "&CustomTitle=" + encodeURIComponent(customTitle) + "&HideLocation=" + hideLocation + "&HideAge=" + hideAge;
		var noErrors = true;
	}
	if (action === 'email') {
		var error = "";
		if ($("#Email").val()=="") {
			var noErrors = false;
			error = "<br />You must enter an email address";
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + error);
			$("#UploadError").css("padding","5px");
		} else {
			var emailAddress = $("#Email").val();
		}
		if (noErrors == true) {
			var Send = "Action=" + action + "&Email=" + encodeURIComponent(emailAddress);
			var noErrors = true;
		}	
	}
	if (action === 'password') {
		var currentPassword = $("#currentPassword").val();
		var error = "";
		if ($("#currentPassword").val()=="") {
			var noErrors = false;
			error = error + "<br />You must enter your old password";
		} else {
			var currentPassword = $("#currentPassword").val();
		}
		if ($("#Password").val()=="") {
			var noErrors = false;
			error = error + "<br />You must enter a new password";
		} else {
			var Password = $("#Password").val();
		}
		if ($("#confirmPassword").val()=="") {
			var noErrors = false;
			error = error + "<br />You must confirm your new password";
		}
		if (noErrors == true) {
			if ($("#Password").val() !== $("#confirmPassword").val()) {
				var noErrors = false;
				error = error + "<br />Your password and confirmation password do not match";	
			} else {
				var currentPassword = $("#currentPassword").val();
				var Send = "Action=" + action + "&CurrentPassword=" + encodeURIComponent(currentPassword) + "&Password=" + encodeURIComponent(Password);
			}
		}
		if (noErrors == false) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + error);
			$("#UploadError").css("padding","5px");
		}

	}
	if (action === 'profile') {
		if ($("#UploadProfile").val()=="") {
			var noErrors = false;
		} else {
			var noErrors = true;
			var filePlace = 'UploadProfile';
			var fileUploader = 'validatescripts/profiles/ajax/uploadprofile.inc.php';
		}
	}
	if (action === 'avatar') {
		if ($("#UploadAvatar").val()=="") {
			var noErrors = false;
		} else {
			var noErrors = true;
			var filePlace = 'UploadAvatar';
			var fileUploader = 'validatescripts/profiles/ajax/uploadavatar.inc.php';
		}
	}
	if (noErrors == true) {
		if (action === 'generic') {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/profiles/ajax/updateprofile.inc.php",
			data: Send,
			success: function (responseText) 
				{
					if ($("#FirstName").val()=="") {
						$("#FirstName").css("color","#999");
						$("#FirstName").val('First Name');
					}
					if ($("#LastName").val()=="") {
						$("#LastName").css("color","#999");
						$("#LastName").val('Last Name');
					}
					if ($("#City").val()=="") {
						$("#City").css("color","#999");
						$("#City").val('City');
					}
					if ($("#State").val()=="") {
						$("#State").css("color","#999");
						$("#State").val('State/Province');
					}
					if ($("#Country").val()=="") {
						$("#Country").css("color","#999");
						$("#Country").val('Country');
					}
					if ($("#CustomTitle").val()=="") {
						$("#CustomTitle").css("color","#999");
						$("#CustomTitle").val('Custom Title');
					}
					$("#genericSaved").css("display","block");
					$("#genericSaved").fadeOut(1000);
					loadedScript();
				}
			});
		}
		if (action === 'email') {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/profiles/ajax/updateprofile.inc.php",
			data: Send,
			success: function (responseText) 
				{
					if (responseText == 1) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />That email address is invalid");
						$("#UploadError").css("padding","5px");
						loadedScript();
					} else if (responseText == 2) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />That email address has already been taken");
						$("#UploadError").css("padding","5px");
						loadedScript();
					} else if (responseText == 3) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />That is already your email address");
						$("#UploadError").css("padding","5px");
						loadedScript();
					} else {
						$("#emailSaved").css("display","block");
						$("#emailSaved").fadeOut(1000);
						loadedScript();
					}
				}
			});
		}
		if (action === 'password') {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/profiles/ajax/updateprofile.inc.php",
			data: Send,
			success: function (responseText) 
				{
					if (responseText == 1) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />Your new password is invalid");
						$("#UploadError").css("padding","5px");
						loadedScript();
					} else if (responseText == 2) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />Your old password is invalid");
						$("#UploadError").css("padding","5px");
						loadedScript();
					} else if (responseText == 3) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />That is not your current password");
						$("#UploadError").css("padding","5px");
						loadedScript();
					} else if (responseText == 4) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />That is already your password");
						$("#UploadError").css("padding","5px");
						loadedScript();
					} else {
						$("#currentPassword").val('');
						$("#Password").val('');
						$("#confirmPassword").val('');

						$("#passwordSaved").css("display","block");
						$("#passwordSaved").fadeOut(2000);
						loadedScript();
					}
				}
			});
		}
		if ((action === 'profile') || (action === 'avatar')) {
			$.ajaxFileUpload
			(
				{
					beforeSend: loadingScript(),
					url:fileUploader,
					secureuri:false,
					fileElementId:filePlace,
					dataType: 'json',
					success: function (data, status)
					{
						if(typeof(data.error) != 'undefined')
						{
							if(data.error != '')
							{
								$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />" + data.error);
								$("#UploadError").css("padding","5px");
								loadedScript();
							}else
							{
								if (action === 'profile') {
									$("#profilePhotoPreview").attr("src", data.msg);
									$("#profileSaved").css("display","block");
									$("#profileSaved").fadeOut(2000);
								}
								if (action === 'avatar') {
									$("#avatarPhotoPreview").attr("src", data.msg);
									$("#avatarSaved").css("display","block");
									$("#avatarSaved").fadeOut(2000);
								}
								loadedScript();
							}
						}

					},
					error: function (data, status, e)
					{
						//alert(e);
					}
				}
			)
		}
	}
}
function AR_getter(actnum) {
	if (actnum >= 0) {
	} else {
		var actnum = false;
	}
	if ((window.location.hash != "") || (window.location.hash != "#"))	{
		var ARGet = window.location.hash;
		if (noErrors == true) {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/main/ajax/pagegeneration.inc.php",
			data: "ARGet=" + encodeURIComponent(ARGet),
			success: function (responseText) 
				{
					sessioncontroller();
					$("#ContentMain").html(responseText);
					var Stringup = $("#SectionHeader").html();
					Stringup = Stringup.replace(/\&amp;/g,'&');
					Stringup = Stringup.replace(/\&lt;/g,'<');
					
					document.title = Stringup;

					if ($("#scrollAnchor").html() != ""){
						scrolltotop.scrollup($("#scrollAnchor").html());
					}
					loadedScript();
					$(function() {
						$("#accordion").accordion({autoHeight: false, collapsible: true, active: (actnum)});
					});

				}
			});
		}
	}
}