
var ok = true;

errorOccured = 'no';

function showAlert(which) {								
	var errorContainer = document.getElementById('errorContainer');
	
	if (errorOccured == "no") {
		errorContainer.innerHTML = which;
		new Effect.Appear(errorContainer, { queue: { position: 'end', scope: 'errors', limit: '1' }});
		errorOccured = 'yes';
	} else {
		errorContainer.innerHTML = which;
		new Effect.Pulsate(errorContainer, { queue: { position: 'end', scope: 'errors', limit: '1' }});
	}
}

function submitRegisterForm() {
	
	if (ok) {
	
		var f = $('signupForm');
		
		if (f.firstname.value == '' || f.lastname.value == '' || f.email.value == '' || f.email2.value == '') {
			showAlert("Please fill in all the fields.");
		} else if (f.email.value.indexOf('@') == -1 || f.email.value.split('@')[1].indexOf('.') == -1) {
			if (f.email.value != '') {
				showAlert("'" + f.email.value + "' is not a valid e-mail address.");
			}
			window.setTimeout("$('signupForm').email.focus()", 10);
		} else if (f.email.value != f.email2.value) {
			showAlert("The e-mail and re-type e-mail fields to not match.");
		} else {
			new Ajax.Request(
				'ajax/check_email/',
				{
					async: true,
					postBody: Form.serialize($('signupForm')),
					onComplete: function(req) {
						var received = fl_decodeJSON(req.responseText);
						if (received.result == 'EXISTS') {
							showAlert('This e-mail address is already on the mailing list. Please try another e-mail address.');
							
							$('signupForm').email.select();
							$('signupForm').email.focus();
						} else if (received.result == 'OK') {
						
							add();
							
						} else {
							showAlert('An error occurred. Please try submitting the form again.')
						}
					},
					on404: function() {
						showAlert('Page not found');
					}
				}
			);
		
		}
	
	}
}

function checkEmail(v) {
	
}

function add() {
	new Ajax.Request(
		'ajax/add/',
		{
			async: true,
			postBody: Form.serialize($('signupForm')),
			onComplete: function(req) {								
				var received = fl_decodeJSON(req.responseText);
				if (received.result = 'OK') {
					success(received);
				} else {
					showAlert('An error occurred. Please try submitting the form again.')
				}
			},
			onFailure: function(t) {
				showAlert('Error ' + t.status + ' -- ' + t.statusText);
				showAlert(t.responseText);
			},
			on404: function() {
				showAlert('Page not found');
			}
		}
	);
}

function submitMoreInformationForm() {
	new Ajax.Request(
		'ajax/submit_more_info/',
		{
			async: true,
			postBody: Form.serialize($('moreInformationForm')),
			onComplete: function(req) {								
				var received = fl_decodeJSON(req.responseText);
				if (received.result = 'OK') {
					end();
				} else {
					showAlert('An error occurred. Please try submitting the form again.')
				}
			},
			onFailure: function(t) {
				showAlert('Error ' + t.status + ' -- ' + t.statusText);
				showAlert(t.responseText);
			},
			on404: function() {
				showAlert('Page not found');
			}
		}
	);
}

function success(received) {
	$('moreInformationForm').id.value = received.id;
	$('mainDetails').style.display = 'none';
	$('success').style.display = 'block';
}

function end() {
	$('success').style.display = 'none';
	$('thankYou').style.display = 'block';
}


window.onload = function() {
	$('signupForm').firstname.focus();
}