
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.email.value == '' || f.email2.value == '') {
			showAlert("Please fill in your name and email address.");
		} else if (f.mailingList.checked) {
			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') {
								
								if (f.comments.value) {
									alreadyOnList();
									sendEmail();
								} else {
									showAlert('This e-mail address is already on the mailing list. Please try another.');
									$('signupForm').email.select();
									$('signupForm').email.focus();
								}
								
							} else if (received.result == 'OK') {
								
								if (f.comments.value) {
									add();
									sendEmail();
								} else {
									add();
								}
								
							} else {
								showAlert('An error occurred. Please try submitting the form again.')
							}
						},
						on404: function() {
							showAlert('Page not found');
						}
					}
				);
			}
		} else if (f.comments.value) {
			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 {
				sendEmail();
			}
		} else {
			showAlert("Choose to join our mailing list, or simply leave us a comment.");
		}
	
	}
}

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);
					submitMoreInformationForm();
				} 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 sendEmail() {
	new Ajax.Request(
		'ajax/contactform/',
		{
			async: true,
			postBody: Form.serialize($('signupForm')),
			onComplete: function(req) {								
				var received = fl_decodeJSON(req.responseText);
				if (received.result = 'OK') {
					emailSent();
				} else {
					
				}
			},
			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($('signupForm')),
			onComplete: function(req) {
				var received = fl_decodeJSON(req.responseText);
				if (received.result = 'OK') {
					prefsSent();
				} 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';
	$('thankYou').style.display = 'block';
	$('addedMailingList').style.display = 'list-item';
}

function prefsSent() {
	$('mainDetails').style.display = 'none';
	$('thankYou').style.display = 'block';
	$('prefsSent').style.display = 'list-item';
}

function alreadyOnList() {
	$('alreadyOnList').style.display = 'list-item';
}

function emailSent() {
	$('mainDetails').style.display = 'none';
	$('thankYou').style.display = 'block';
	$('emailSent').style.display = 'list-item';
}


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