window.addEvent("domready", function(e)
{
	var form = document.forms['contactForm'];
	$('contactSubmit').addEvent('click', function(e)
	{
		var name 	= form.contactName.value;
		var email 	= form.contactEmail.value;
		var msg 	= form.contactMsg.value;
		
		if (name!='' && CheckEmail(email) && msg!='')
		{
			SendEmail(name, email, msg);
		}
		else
		{
			DisplayError("Please fill out the form completely. A valid email address is required.");
		}
	});
});

/**
 * A wrapper function that handles the sending of the email and any animation required.
 * Also handles errors.
 * 
 * @param 	name 	The name of the recipient
 * @param 	email 	The email address of the recipient
 * @param 	message The email message
 */
 
function SendEmail(name, email, message)
{
	var slider = new Fx.Slide('contactFormContainer', {duration:500});
	var emailer = new Emailer();
	
	emailer.Send(name, email, message, 
	{
		onSuccess:function(response)
		{
			if (response.error==undefined)
			{
				// fade out form
				var fadeOutFx = new Fx.Style('contactForm', 'opacity', { duration: 500, 
					onComplete:function(event)
					{
						// fade out done
						$('message').setStyle('display', 'block');
						
						// shrink box
						var shrinkBoxFx = new Fx.Style('contactFormContainer', 'height', { duration: 500, 
							onComplete:function(event)
							{
								// shrink done
								$('contactForm').setStyle('display', 'none');
								slider.slideOut();
								
								setTimeout(function()
									{
										$('message').setHTML("Thanks. Your message has been sent.");
										slider.slideIn();
									}, 600);
							}
						});
							
						// start shrink
						shrinkBoxFx.start($('contactFormContainer').getStyle("height").toInt(), $('message').getStyle('height').toInt()+40);
					}
				});
				
				// start fade out
				fadeOutFx.start(1, 0);
			}
			else
			{
				DisplayError(response.error);
			}
		}, 
			
		onFailure:function(response)
		{
			DisplayError("Unable to send email. Cannot connect. "+response);
		}
	});
}

/**
 * Verifies the format of the email address
 * 
 * @param 	email 	The email addressed to be checked
 * @return			True if email address is in the correct form
 */
 
function CheckEmail(email)
{
	return email!='' && email.match(/^(\w|\-|\_|\.)+\@((\w|\-|\_)+\.)+[a-zA-Z]{2,}$/i);
}

/**
 * Emailer class
 * 
 * Allows users to send email using Javascript (AJAX) without reloading the page (AJAX).
 * Must have a valid webservice URL specified
 */
 
 var API_KEY = '7j1Tg5kX';

function Emailer()
{
	this._server = '_ws/emailer.ws.php';
	this._apiKey = API_KEY;
	
	/**
	 * Connects to the webservice to send the email message.
	 * If an onSuccess or onFailure callback is passed in, these will be called depending on the outcome of the function
	 * It is up to the user to check if the email was actually sent.
	 * 
	 * @param 	name 	The name of the recipient
	 * @param 	email 	The email address of the recipient
	 * @param 	message The email message
	 * @param	options	Optional callbacks sent in as collection of Functions objects. Currently supports "onFailure" and "onSuccess".
	 * @return 			True if no errors were encountered trying to connect to the webservice.
	 */
	this.Send = function(name, email, message, options)
	{
		if (name=='' || message=='' || email=='' || !email.match(/^(\w|\-|\_|\.)+\@((\w|\-|\_)+\.)+[a-zA-Z]{2,}$/i))
		{
			if (options.onFailure)
				options.onFailure({error:"Missing input. Name and valid email address required."});
				
			return false;	
		}
		
		var queryString = "name="+escape(name)+"&email="+escape(email)+"&message="+escape(message)+"&apikey="+escape(this._apiKey);
		var xhr = new XHR(options);
		xhr.send(this._server, queryString);
		
		return true;
	}
}