function addText()
{
	if($('username').value == '')
	{
		$('username').value = 'AU Access Username';
		$('username').setStyle('color', '#ccc');
	}
}

function removeText()
{
	
	if($('username').value == 'AU Access Username')
	{
		$('username').value = '';
		$('username').setStyle('color', '#333');
	}
	
}

function logMeIn()
{	
	var postData = {
		'user': $('username').value,
		'pass': $('password').value
	}
	var myRequest  = new Request({url: 'req/login.php', method: 'post', onSuccess: doneCrunching, data: postData}).send();
	return false;
}

function doneCrunching(h)
{
	if(h == '_true_')
	{
		location.href='/frinancial/';
	}else
	{
	
		if(h == '_valid_')
		{
			$('error').set({
				html: 'That username exists, but is not verified: <a href="validate.php">Verify Now</a>',
				styles: {
					marginBottom: '-40px',
					background: '#fff',
					color: '#fff'
				}
			});
		} else
		{
			$('error').set({
				html: 'Sorry, that username and password combo didn\'t work',
				styles: {
					marginBottom: '-40px',
					background: '#fff',
					color: '#fff'
				}
			});
		}
		new Fx.Morph('error', {duration: 500}).start(
		{
			opacity: 1,
			backgroundColor: '#ff0000',
			color: '#fff'
		});

		$('login').shake();
		$('password').value = '';
		$('password').focus();
	}
}

function extendForm()
{
	$('submit').removeEvents('click');
	$('submit').addEvent('click', createAccount);
	
	// remove the events
	this.removeEvent('click', extendForm);
	
	// lets make a few other elements
	new Fx.Morph($('error')).start(
	{
		opacity: 0
	});
	
	var coords = $('login').getCoordinates();
	new Fx.Morph('login').start(
	{
		height: coords.height+40
	}).addEvent('onComplete', function()
	{
		var accCoords = $('createAccount').getCoordinates();
		$('createAccount').set({
			styles: {
				top: accCoords.top,
				left: accCoords.left-10,
				position: 'absolute',
				margin: 0
			},
			html: 'Submit &raquo;'
		}).addEvent('click', createAccount);
		new Fx.Morph('createAccount').addEvent('onStart', function()
		{
			var copy = $('createAccount').clone().inject('container').set(
			{
				id: 'cancelCreate',
				html: 'Cancel',
				'class': 'accCreate'
			}).addEvent('click', cancelCreate);
			
			var cancelCoords = $('cancelCreate').getCoordinates();
			new Fx.Morph('cancelCreate').start(
			{
				left: cancelCoords.left-80
			});
			
		}.bind(this)).addEvent('onComplete', function()
		{
			var cancelCoords = $('cancelCreate').getCoordinates();
			
			new Fx.Morph('cancelCreate').start(
			{
				top: cancelCoords.top-80
			});
			
			new Fx.Morph('createAccount').start(
			{
				top: accCoords.top-80
			});
		}).start(
		{
			left: accCoords.left-10
		});
		
		new Fx.Morph('submit').start(
		{
			opacity: 0
		});
		
		new Fx.Morph('accountFormWrapper').start(
		{
			height: '70px'
			
		}).addEvent('onComplete', function()
		{					
			new Fx.Morph('accountForm').start(
			{
				marginTop: 0
			});

			if($('username').value && $('username').value != 'AU Access Username')
			{
				$('confirm').value = '';
				$('password').value = '';
				$('password').focus();
			} else
			{
				$('confirm').value = '';
				$('password').value = '';
				$('username').focus();
			}
		});
	});
}

function cancelCreate()
{
	$('submit').removeEvents('click');
	$('submit').addEvent('click', logMeIn);
	
	// everything in reverse
	// merge submit and cancel
	$('createAccount').removeEvent('click', createAccount);
	var cancelCoords = $('cancelCreate').getCoordinates();
	var cancelCreate = new Fx.Morph('cancelCreate', { link: 'chain'});
	cancelCreate.start(
	{
		left: cancelCoords.left+80,
	});
	cancelCreate.start(
	{
		opacity: 0
	}).addEvent('onComplete', function()
	{
		var submitCoords = $('createAccount').getCoordinates();
		var submitButton = new Fx.Morph('createAccount', {link: 'chain'});
		submitButton.start(
		{
			top: submitCoords.top+40
		}).addEvent('onComplete', function()
		{
			new Fx.Morph('createAccount').start(
			{
				width: submitCoords.width+70,
			}).addEvent('onComplete', function()
			{
				$('createAccount').set('html', 'Create an account &raquo;').addEvent('click', extendForm);
											
				new Fx.Morph('accountForm').start(
				{
					marginTop: -100
					
				}).addEvent('onComplete', function()
				{					
					new Fx.Morph('accountFormWrapper').start(
					{
						height: 0
					});
					
					new Fx.Morph('login').start(
					{
						height: 220
					});
					
					var createCoords = $('createAccount').getCoordinates();
					new Fx.Morph('createAccount').start(
					{
						'top': createCoords.top-30
					});
					
					new Fx.Morph('submit').start(
					{
						opacity: 1
					});
					
					$('createAccount').set({
						styles: {
							position: 'static',
							'height': 'auto',
							'width' : 'auto',
							'top'   : 'auto',
							'left'  : 'auto',
							'marginTop': 10
						}
					});
					$('cancelCreate').dispose();
					if($('username').value && $('username').value != 'AU Access Username')
					{
						$('confirm').value = '';
						$('password').value = '';
						$('password').focus();
					} else
					{
						$('confirm').value = '';
						$('password').value = '';
						$('username').focus();
					}
				});
			});
		});
	});
	
}

function createAccount()
{
	// send info to create an account
	var confirm  = $('confirm').value;
	var password = $('password').value;
	var username = $('username').value;
	
	var postData = {
		'confirm' : confirm,
		'password': password,
		'username': username
	}
	var myRequest  = new Request({url: 'req/create.php', method: 'post', onSuccess: validateAccount, data: postData}).send();
	
	return false;
}

function validateAccount(r)
{
	if($('username').value && $('password').value && $('confirm').value)
	{
		if($('confirm').value == $('password').value)
		{
			if(r == '_true_')
			{
				location.href = 'validate.php';
			} else if(r == '_duplicate_')
			{
				$('error').set({
					html: 'Sorry, user is already registered',
					styles: {
						marginTop: 10,
						marginBottom: '-39px',
						background: '#fff',
						color: '#fff'
					}
				});
				new Fx.Morph('error', {duration: 500}).start(
				{
					opacity: 1,
					backgroundColor: '#ff0000',
					color: '#fff'
				});
			} else if(r == '_valid_')
			{
				$('error').set({
					html: 'That user is registered, but not verified: <a href="validate.php">verify now</a>!',
					styles: {
						marginTop: 10,
						marginBottom: '-39px',
						background: '#fff',
						color: '#fff'
					}
				});
				new Fx.Morph('error', {duration: 500}).start(
				{
					opacity: 1,
					backgroundColor: '#ff0000',
					color: '#fff'
				});
			} else
			{
				$('error').set({
					html: 'Sorry, That AU Access User was not authorized to look at the Financials.',
					styles: {
						marginTop: 10,
						marginBottom: '-39px',
						background: '#fff',
						color: '#fff'
					}
				});
				new Fx.Morph('error', {duration: 500}).start(
				{
					opacity: 1,
					backgroundColor: '#ff0000',
					color: '#fff'
				});
			}
		} else
		{
			$('error').set({
				html: 'The password and confirm fields must match',
				styles: {
					marginTop: 10,
					marginBottom: '-39px',
					background: '#fff',
					color: '#fff'
				}
			});
			new Fx.Morph('error', {duration: 500}).start(
			{
				opacity: 1,
				backgroundColor: '#ff0000',
				color: '#fff'
			});
		}
	} else
	{
		$('error').set({
			html: 'Sorry, All fields must be filled out',
			styles: {
				marginTop: 10,
				marginBottom: '-39px',
				background: '#fff',
				color: '#fff'
			}
		});
		new Fx.Morph('error', {duration: 500}).start(
		{
			opacity: 1,
			backgroundColor: '#ff0000',
			color: '#fff'
		});
	}
}
