var quote = null;
var timer = 0;
var i = 0;
var user_hash = null;

Event.observe(window, 'load', function() {
// Testimonial code
	quote = new Array(
	"<p><img src=\"images/index_quote.png\" />An effective and simple way to combine the freedom of handling today&rsquo;s mail tomorrow with the peace of mind in knowing urgent matters will find their way to you in time.<img src=\"images/index_small_quote.png\" /></p><p align=\"right\"><span class=\"name\">About.com</span></p>",
	"<p><img src=\"images/index_quote.png\" />I now check email two to three times a day, when my schedule permits. Email does not control me, I control it.<img src=\"images/index_small_quote.png\" /></p><p align=\"right\"><span class=\"name\">DidIGetThingsDone.com</span></p>",
	"<p><img src=\"images/index_quote.png\" />The online equivalent of having an administrative assistant screen your mail and only bother you for the important things.<img src=\"images/index_small_quote.png\" /></p><p align=\"right\"><span class=\"name\">Web Worker Daily</span></p>");
	clearTimeout(timer);
	timer = setTimeout( "fadeTestimonial();", 10000 );
});

function fadeTestimonial() {
	$('quoteSpace').fade({ duration: 4.0 });
	clearTimeout(timer);
	timer = setTimeout( "appearTestimonial();", 4000 );
}
function appearTestimonial() {
	if (i >= quote.length-1) {i = 0;} else {i++;}
	$('quoteSpace').update(quote[i]);
	$('quoteSpace').appear({ duration: 4.0 });
	clearTimeout(timer);
	timer = setTimeout( "fadeTestimonial();", 10000 );
}

// Signup code

function checkYes() {
	$('yesCheckbox').addClassName('checked');
	$('noCheckbox').removeClassName('checked');
	$('have_an_account').value = "TRUE";
}

function checkNo() {
	$('yesCheckbox').removeClassName('checked');
	$('noCheckbox').addClassName('checked');
	$('have_an_account').value = "FALSE";
}

function add_user_request() {
	$('orchant_error').hide();
	$('request_invite').hide();
	$('loader1').show();
	$('email_input').removeClassName('input_error');
	new Ajax.Request( 'ajax.php?url=/ajax/add_user_request/', {
		method : 'post',
		parameters : {
			'email' : $('email_input').value,
			'referrer' : $('referrer_input').value
		},
		onSuccess : function(transport) {
			var json = transport.responseText.evalJSON();
			if (json.errors == null) {
				pageTracker._trackPageview('/ajax/register_user_homepage');
				if (json.result == null) {
					$('orchant_form').hide();
					$('success_1').show();
				} else {
					user_hash = json.result;
					$('orchant_form').hide();
					$('orchant_form_2').show();
					// Set "No" as the default answer.
					checkNo();
				}
			} else { 
				// Hide loading logo, show input so user can retry, show error message
				$('loader1').hide();
				$('request_invite').show();
				$('email_input').addClassName('input_error');
				// We'll only display the first error
				if (typeof json.errors.email != 'undefined' && json.errors.email == 'email') {
					//Invalid email error
					$('orchant_error').innerHTML = "<b>Oops!</b> That email address appears to be invalid.";
					$('orchant_error').show();
				} else if (typeof json.errors.email != 'undefined' && json.errors.email.error == '_email_is_unique') {
					//User already registered error
					$('orchant_error').innerHTML = "The email address you entered is already registered, we will be in touch soon!";
					$('orchant_error').show();
				} else {
					//Added to catch other errors
					$('orchant_error').innerHTML = "<b>Oops!</b> That email address appears to be invalid.";
					$('orchant_error').show();
				}
			}
		},
		onFailure : function(transport) {
			//Other errors
			$('loader1').hide();
			$('request_invite').show();
			$('email_input').addClassName('input_error');
			$('orchant_error').show();
			$('orchant_error').innerHTML = "Yikes! We're having some technical troubles. Please try again later.";
		}
	});
	// Don't submit the form
	return false;
}

function update_user_request() {
	$('request_update').hide();
	$('loader2').show();
	new Ajax.Request( 'ajax.php?url=/ajax/update_user_request/', {
		method : 'post',
		parameters : {
			'is_existing_user' : $('have_an_account').value,
			'hash' : user_hash
		},
		onSuccess : function() {
			if ($('have_an_account').value == "FALSE") {
				$('orchant_form_2').hide();
				$('success_3').show();
			}
			else {
				$('orchant_form_2').hide();
				$('success_2').show();
			}
		},
		onFailure : function() {
			$('loader2').hide();
			$('orchant_error').show();
			$('orchant_error').innerHTML = "Yikes! We're having some technical troubles. Please try again later.";
		}
	});
	// Don't submit the form
	return false;
}
