// Submit for New
$("#do_new").live('submit', function()
{
	$(this).find('button.submit').addClass('loading').attr('disabled', 'true');
	$.post($(this).attr('action'), $(this).serialize(), function(response)
	{
		if (response.substr(0, 7) == "[error]")
			$(".context").html(response);
		else
		{
			setTimeout(function()
			{

				var obj = $.evalJSON(response);
				switch (obj.section)
				{
					case 'invalid_add_account':
						$('p#error').remove();
						if(obj.noname)
							$('input#name').parent().removeClass('bad').removeClass('good').addClass('bad');
						else
							$('input#name').parent().removeClass('bad');
							
						if(obj.nopassword)
							$('input#password').parent().removeClass('bad').removeClass('good').addClass('bad');
						else
							$('input#password').parent().removeClass('bad');
						if(obj.noemail)
							$('input#email').parent().removeClass('bad').removeClass('good').addClass('bad');
						else
							$('input#email').parent().removeClass('bad');
							
						if(obj.nohost)
							$('input#host').parent().removeClass('bad').removeClass('good').addClass('bad');

						$('section#signup_modal form').prepend('<p id="error">There were some errors with your form.<br />Please double check your details.</p>');
						break;
					//THIS CREATES THE ACCOUNT
					case 'add_account':
						if(obj.exists == '1')
						{
							$('p#error').remove();
							$('section#signup_modal form').prepend('<p id="error">Your <b>email address</b> is already registered.</p>');
						}
						else
							window.location = '/hello/'+obj.host;
						break;
				}
				$('form').find('button.submit').removeAttr('disabled').removeClass('loading');
			}, savingTimeout);
		}
	});		
	return false;
});

// Submit to save
$("#do_save").live('submit', function()
{
	$('.saved').remove();
	$(this).find('button.submit').addClass('loading').attr('disabled', 'true');
	$.post($(this).attr('action'), $(this).serialize(), function(response)
	{
		if (response.substr(0, 7) == "[error]")
			$(".context").html(response);
		else
		{
			setTimeout(function() {
				var obj = $.evalJSON(response);
				switch (obj.section)
				{
					case 'foobar':
					break;
				}
			$('form').find('button.submit').removeAttr('disabled').removeClass('loading');
			}, savingTimeout);
		}
	});
	return false
});

// Submit to process some data
$("#do_process").live('submit', function()
{
	$(this).find('button.submit').addClass('loading').attr('disabled', 'true');
	$.post($(this).attr('action'), $(this).serialize(), function(response)
	{
		$('#do_process').html(response);
	});
	$('form').find('button.submit').removeAttr('disabled').removeClass('loading');
	return false;
});
