function AccountInfoController(type, username)
{
	var AJAX_PAGE = "/ajax/editaccount.php";
	var TYPE_JOIN = 1;
	var TYPE_EDIT = 2;

	var MIN_PASSWORD_LENGTH = 6;

	// Whether or not we've kicked off an ajax request.
	var in_transition = false;

	// Bunch of stuff relating to form fields.
	var user_alias = "user_alias";
	var user_pass = "user_password";
	var user_new_pass = "user_new_password";
	var user_first = "user_firstname";
	var user_last = "user_lastname";
	var user_email = "user_email";
	var user_gender = "user_gender";

	var password_button = new Button("password_check");
	var post_button = new Button("postbutton");
	var password = null;
	var main_params;

	this.Init = function()
	{
		password_button.Disable("Check");
	}

	function GetElement(element)
	{
		return(document.getElementById(element));
	}

	this.CheckDetails = function()
	{
		if(in_transition)
		{
			return(false);
		}

		if(type == TYPE_JOIN)
		{
			// First thing's first - we need a username.
			var user_element = GetElement(user_alias);
			if(user_element.value.length == 0)
			{
				alert("Please enter a username.");
				user_element.focus();
				return(false);
			}
		}

		// First and last name.
		var first_element = GetElement(user_first);
		if(first_element.value.length == 0)
		{
			alert("Please enter your first name.");
			first_element.focus();
			return(false);
		}

		var last_element = GetElement(user_last);
		if(last_element.value.length == 0)
		{
			alert("Please enter your last name.");
			last_element.focus();
			return(false);
		}

		var pass_element = GetElement(user_pass);
		if(pass_element.value.length == 0)
		{
			alert("Please enter your password.");
			pass_element.focus();
			return(false);
		}

		if((type == TYPE_JOIN) && (pass_element.value.length < MIN_PASSWORD_LENGTH))
		{
			alert("Your password must be at least " + MIN_PASSWORD_LENGTH + " characters.");
			pass_element.focus();
			return(false);
		}

		if(type == TYPE_EDIT)
		{
			var newpass_element = GetElement(user_new_pass);
			var newpass_element2 = GetElement(user_new_pass + "2");
			if(newpass_element.value.length > 0)		// Main password field has something in it
			{
				if(newpass_element.value.length < MIN_PASSWORD_LENGTH)
				{
					alert("Your password must be at least " + MIN_PASSWORD_LENGTH + " characters.");
					newpass_element.focus();
					return(false);
				}

				if(newpass_element2.value.length == 0)
				{
					alert("You must confirm your password by re-typing it.");
					newpass_element2.focus();
					return(false);
				}

				if(newpass_element.value != newpass_element2.value)
				{
					alert("Your passwords don't match - please try retyping them.");
					newpass_element.value = "";
					newpass_element2.value = "";
					newpass_element.focus();
					return(false);
				}
			}
			else if(newpass_element2.value.length > 0)	// Main password field is blank but confirm field isn't
			{
				alert("If you're trying to change your password, you must enter it twice for confirmation purposes.\nIf you don't want to change it, leave this field blank.");
				newpass_element2.focus();
				return(false);
			}
		}

		var email_element = GetElement(user_email);
		if(email_element.value.length == 0)
		{
			alert("You haven't entered an e-mail address. You must enter your e-mail address in order to join Newgrounds. A confirmation e-mail is sent to this account, which you need to validate your account with.");
			email_element.focus();
			return(false);
		}

		if(GetRadioValue("mainform", user_gender) == null)
		{
			alert("Please enter your gender.");
			return(false);
		}

		if(type == TYPE_JOIN)
		{
			var captcha_element = GetElement("captcha_code");
			if(captcha_element.value.length == 0)
			{
				alert("Please enter the security code that's displayed in the image to the right.");
				captcha_element.focus();
				return(false);
			}
		}

		return(true);
	}

	this.CheckFurtherDetails = function()
	{
		var birth_year = SelectValue(document.getElementById("bday_year"));
		var birth_month = SelectValue(document.getElementById("bday_month"));
		var birth_day = SelectValue(document.getElementById("bday_day"));

		if((birth_year != "") || (birth_month != "") || (birth_day != ""))
		{
			if((birth_year == "") || (birth_month == "") || (birth_day == ""))
			{
				alert("If you're going to enter your birthday, you must enter your birth year, month and day.");
				return(false);
			}
		}

		return(true);
	}

	this.Register = function()
	{
		if(!(this.CheckDetails()))
		{
			return;
		}

		in_transition = true;

		// Reset this
		main_params = new Array();

		// Set our type
		main_params["type"] = type;

		// Set our username, as we don't have this in the edit function.
		main_params["username"] = GetElement(user_alias).value;

		GetBasicParams();

		post_button.Disable("Submit! &gt;");

		ShowProcessingAnimation();

		var ajax = new AjaxRequest(AJAX_PAGE);
		ajax.Send(main_params, HandleResponse, HandleError);
	}

	this.Edit = function()
	{
		if(in_transition)
		{
			return;
		}

		if(!(this.CheckDetails()))
		{
			return;
		}

		if(!(this.CheckFurtherDetails()))
		{
			return;
		}

		in_transition = true;

		// Reset this.
		main_params = new Array();

		// Set our type.
		main_params["type"] = type;

		// Get our basic stuff.
		GetBasicParams();

		// And now the advanced stuff.
		GetFurtherParams();

		post_button.Disable("Update All Info! &gt;");

		ShowProcessingAnimation();

		var ajax = new AjaxRequest(AJAX_PAGE);
		ajax.Send(main_params, HandleResponse, HandleError);
	}

	function ShowProcessingAnimation()
	{
		GetElement("statusbar").style.display = "block";
	}

	function HideProcessingAnimation()
	{
		GetElement("statusbar").style.display = "none";
	}

	function HandleResponse(response)
	{
		var userpageurl = response.GetField("userpageurl");
		if(type == TYPE_JOIN)
		{
			document.getElementById("userpageurl").value = userpageurl;
			document.getElementById("mainform").submit();
		} else
		if(type == TYPE_EDIT)
		{
			window.location.href = userpageurl;
		}
	}

	function HandleError()
	{
		post_button.Enable();

		HideProcessingAnimation();

		in_transition = false;
	}

	function GetBasicParams()
	{
		main_params["first_name"] = GetElement(user_first).value;
		main_params["last_name"] = GetElement(user_last).value;
		main_params["password"] = GetElement(user_pass).value;
		main_params["email"] = GetElement(user_email).value;
		main_params["gender"] = GetRadioValue("mainform", user_gender);
		main_params["receive_update"] = GetElement("user_receive_updates").checked ? "Y" : "N";
		main_params["receive_portal_daily"] = GetElement("receive_portal_daily").checked ? "Y" : "N";

		if(type == TYPE_JOIN)
		{
			main_params["captcha_id"] = GetElement("captcha_id").value;
			main_params["captcha_code"] = GetElement("captcha_code").value;
		}
	}

	function GetFurtherParams()
	{
		main_params["new_password"] = GetElement(user_new_pass).value;
		main_params["portal_name_pref"] = SelectValue(document.getElementById("portal_name_pref"));
		main_params["pm_option"] = SelectValue(document.getElementById("pm_option"));
		main_params["bday_year"] = SelectValue(document.getElementById("bday_year"));
		main_params["bday_month"] = SelectValue(document.getElementById("bday_month"));
		main_params["bday_day"] = SelectValue(document.getElementById("bday_day"));
		main_params["location"] = GetElement("location").value;
		main_params["website_url"] = GetElement("user_weburl").value;
		main_params["website_title"] = GetElement("user_webtitle").value;
		main_params["aimname"] = GetElement("user_aimname").value;
		main_params["occupation"] = GetElement("occupation").value;
		main_params["school"] = GetElement("school").value;
		main_params["aura"] = GetElement("aura").value;
		main_params["message"] = GetElement("message").value;
	}

	/********** Password functions ************/
	this.GetPassword = function()
	{
		if(password == null)
		{
			password = new Password();
		}

		return(password);
	}

	function Password()
	{
		var AJAX_PASS_PAGE = "/ajax/checkpassword.php";
		var strength = null;

		this.GetLength = function()
		{
			return(GetElement(user_pass).value.length);
		}

		this.ToggleCheckVis = function()
		{
			if(((type == TYPE_JOIN) && (GetElement(user_alias).value.length > 0) && (this.GetLength() >= MIN_PASSWORD_LENGTH)) || ((type == TYPE_EDIT) && (this.GetLength() >= MIN_PASSWORD_LENGTH)))
			{
				password_button.Enable();
			} else
			{
				password_button.Disable("Check");
			}
		}

		this.Check = function()
		{
			if(in_transition)
			{
				return;
			}

			in_transition = true;

			var params = new Array();

			if(type == TYPE_JOIN)
			{
				params["username"] = GetElement(user_alias).value;
			} else
			{
				params["username"] = username;
			}

			params["password"] = GetElement(user_pass).value;

			// It's not like this will matter, since multiple requests are ignored.
			// But... let's just have it there in case the server is hanging and the user
			// is wondering what just happened.
			password_button.Disable("Check");

			var ajax = new AjaxRequest(AJAX_PASS_PAGE);
			ajax.Send(params, HandleResponse, ErrorCleanup);
		}

		function HandleResponse(response)
		{
			in_transition = false;

			var stars = new Array("None", "Poor", "Average", "Good", "Great");

			strength = response.GetField("strength");

			GetElement("password_strength").firstChild.data = stars[strength];
			GetElement("password_strength").className = "star" + strength;
		}

		function HideChecks()
		{
		}

		function ErrorCleanup()
		{
			GetElement("password_strength").className = "star" + 0;
			in_transition = false;
		}
	}
	/********** End password functions ************/

}