var AccountInfoController = Class.create({
	initialize: function() {
		this.AJAX_SUBMIT_PAGE = '/users/add';
		this.AJAX_EDIT_PAGE = '/users/edit';
		this.AJAX_RECOVER_PAGE = '/users/recover';
		
		this.busy = false;
	},
	
	Save: function() {
		// No simultaneous requests
		if(this.busy){
			alert('Please wait until all file activity on the page finishes.');
			return;	
		}
		
		// Normal registration
		if(!this.IsEdit()) {
			if(Trim($F('username_field')).length == 0){
				$('username_field').focus();
				alert('Please enter a username.');
				return;
			}
			
			if(!this.UsernameIsValid(Trim($F('username_field')))){
				$('username_field').focus();
				alert('Please enter only alphanumeric characters in your username.');
				return;
			}
			
			if($F('password_field').length == 0){
				$('password_field').focus();
				alert('Please enter a password.');
				return;
			}
			
			if($F('password_field').length < 6){
				$('password_field').focus();
				alert('Please enter a password that is at least 6 characters.');
				return;
			}
			
			if(!this.PasswordIsValid($F('password_field'))){
				$('password_field').focus();
				alert('Please enter only alphanumeric characters, hyphens, and underscores in your password.');
				return;
			}
			
			if($F('confirmpassword_field').length == 0){
				$('confirmpassword_field').focus();
				alert('Please confirm your password.');
				return;
			}
			
			if($F('confirmpassword_field') != $F('password_field')){
				$('confirmpassword_field').focus();
				alert('The entered passwords don\'t match.');
				return;
			}
			
			if(!this.EmailIsValid(Trim($F('email_field')))){
				$('email_field').focus();
				alert('Please enter a valid e-mail address.');
				return;
			}
			
			$('submit_button').disabled = 'disabled';
			
			var params = new Array();
			params['data[User][username]'] = Trim($F('username_field'));
			params['data[User][password]'] = $F('password_field');
			params['data[User][email]'] = Trim($F('email_field'));
	
			this.busy = true;
			new Ajax.Request(this.AJAX_SUBMIT_PAGE,
			{
				method:'post',
				parameters: params,
				onSuccess:  function(transport) {
					//Let them be able to try again if there were errors
					this.busy = false;
					
					var response = transport.responseText.evalJSON();
					
					if(response.usernameTaken){
						$('username_field').focus();
						alert('That username has already been taken.  Please choose a different one.');
						$('submit_button').disabled = null;
						return;
					}
					
					if(response.emailTaken){
						$('email_field').focus();
						alert('An account has already been registered at that e-mail address.  Please choose a different one.');
						$('submit_button').disabled = null;
						return;
					}
					
					//Let them know it was successful then it's off to the homepage
					alert('Thank you for registering, your account is now live and ready for action!');
					window.location = 'http://reflashed.com/';
				}.bind(this)
			});
		} else {
			// Account Edit
			
			// First get old password
			if($F('password_field').length == 0){
				$('password_field').focus();
				alert('Please enter your old password.');
				return;
			}
			
			// Now for the new password - not required
			if($F('new_password_field').length != 0){
				if($F('new_password_field').length < 6){
					$('new_password_field').focus();
					alert('Please enter a password that is at least 6 characters.');
					return;
				}
				
				if(!this.PasswordIsValid($F('new_password_field'))){
					$('new_password_field').focus();
					alert('Please enter only alphanumeric characters, hyphens, and underscores in your password.');
					return;
				}
				
				if($F('confirm_new_password_field').length == 0){
					$('confirm_new_password_field').focus();
					alert('Please confirm your password.');
					return;
				}
				
				// The passwords have to match
				if($F('confirm_new_password_field') != $F('new_password_field')){
					$('confirm_new_password_field').focus();
					alert('The entered passwords don\'t match.');
					return;
				}
			}
			
			if(!this.EmailIsValid(Trim($F('email_field')))){
				$('email_field').focus();
				alert('Please enter a valid e-mail address.');
				return;
			}
			
			$('submit_button').disabled = 'disabled';
			
			var params = new Array();
			//params['data[User][username]'] = Trim($F('username_field'));
			params['data[User][password]'] = $F('password_field');
			params['data[User][new_password]'] = $F('new_password_field');
			params['data[User][email]'] = Trim($F('email_field'));
			params['data[User][filter_text]'] = $('filter_text').checked ? 1 : 0;
			
			this.busy = true;
			new Ajax.Request(this.AJAX_EDIT_PAGE,
			{
				method:'post',
				parameters: params,
				onSuccess:  function(transport) {
					// Let them be able to try again if there were errors
					this.busy = false;
					
					var response = transport.responseText.evalJSON();
					
					if(!response.correctPassword){
						$('password_field').focus();
						alert('It seems like the password you entered was incorrect.  Please try again.');
						$('submit_button').disabled = null;
						return;
					}
					
					if(response.emailTaken){
						$('email_field').focus();
						alert('An account has already been registered at that e-mail address.  Please choose a different one.');
						$('submit_button').disabled = null;
						return;
					}
					
					window.location = '/account/';
				}.bind(this)
			});
		}
	},
	
	RecoverAccount: function() {
		var email = Trim($F('email_field'));
		
		if(!this.EmailIsValid(email)) {
			$('email_field').focus();
			alert('Please enter a valid e-mail address.');
			return;
		}

		// No simultaneous requests
		if(this.busy){
			alert('Please wait until all file activity on the page finishes.');
			return;	
		}

		$('submit_button').disabled = 'disabled';
		
		var params = new Array();
		params['data[User][email]'] = email;
		
		this.busy = true;
		new Ajax.Request(this.AJAX_RECOVER_PAGE,
		{
			method:'post',
			parameters: params,
			onSuccess:  function(transport) {
				// Let them be able to try again if there were errors
				this.busy = false;
				
				var response = transport.responseText.evalJSON();
				
				if(!response.emailExists){
					$('email_field').focus();
					alert('The e-mail address you entered is not associated with an account.  Please try again.');
					$('submit_button').disabled = null;
					return;
				}
				
				alert('An e-mail containing your new account infromation has been sent to your e-mail address.');
			}.bind(this)
		});
	},

	UsernameIsValid: function(username){
		var pattern = /[^a-zA-Z0-9]/;
		var result = pattern.test(username);
		if(!result){
			return true;
		}
		return false;
	},
	
	PasswordIsValid: function(password){
		var pattern = /[^a-zA-Z0-9-_]/;
		var result = pattern.test(password);
		if(!result){
			return true;
		}
		return false;
	},
	
	EmailIsValid: function(email){
		var pattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
		if(!pattern.test(email)) {
			return false;
		}
		return true;
	},
	
	IsEdit: function(){
		return($F('is_edit') == 1);
	}
});
