var contact = {
	message: null,
	url: null,
	onShow: function () {
		$('#cboxLoadedContent #send').click(function (e) {
			e.preventDefault();
			// validate form
			if (contact.validate()) {
				$('#cboxLoadedContent .message').fadeOut(function () {
					$('#cboxLoadedContent .message').removeClass('error').empty();
				});
				$('#cboxLoadedContent .title').html('Sending...');
				$('#cboxLoadedContent form').fadeOut(200);
				
				$.ajax({
					url: contact.url + '&action=send',
					data: $('#cboxLoadedContent form').serialize(),
					dataType: 'text',
					type: 'post',
					success: function (data) {
						$('#cboxLoadedContent .loading').fadeOut(200, function () {
							if (data == "true") {
								$('#cboxLoadedContent .title').html('Thank you!');
								$('#cboxLoadedContent .title').animate({
									opacity: 1.0
								}, 1000, function(){
									contact.onClose();
								});
							} else {
								$('#cboxLoadedContent .title').html('There was an error sending the email. Please try again later or write an email directly to contactus@well.ca. <a href="#" id="close_window_link">Close this window</a>');
								$('#cboxLoadedContent #close_window_link').click(function(e) {e.preventDefault(); contact.close(dialog)});
							}
						});
					},
					error: contact.error
				});
			}
			else {
				if ($('#cboxLoadedContent .message:visible').length > 0) {
					$('#cboxLoadedContent .message div').fadeOut(200, function () {
						$('#cboxLoadedContent .message div').empty();
						contact.showError();
						$('#cboxLoadedContent .message div').fadeIn(200);
					});
				}
				else {
					$('#cboxLoadedContent .message').animate({
						height: '30px'
					}, contact.showError);
				}
				
			}
		});
	},
	onClose: function () {
		$.fn.colorbox.close()
	},
	error: function (xhr) {
		$('#cboxLoadedContent .title').html('There was an error sending the email. Please try again later or write an email directly to contactus@well.ca. <a href="#" id="close_window_link">Close this window</a>');
		$('#cboxLoadedContent #close_window_link').click(function(e) {e.preventDefault(); contact.close(dialog)});
	},
	validate: function () {
		contact.message = '';
		invalid_msg = '';
		required_msg = '';
		if (!$('input[name=name]').val()) {
			required_msg += 'Name';
		}

		var email = $('input[name=email]').val();
		if (!email) {
			if(required_msg.length > 0) {
				required_msg += ', ';
			}
			required_msg += 'Email';
		}
		else {
			// Regex from: http://regexlib.com/REDetails.aspx?regexp_id=599
			var filter = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/;
			if (!filter.test(email)) {
				invalid_msg += 'Email';
			}
		}

		if (!$('input[name=subject]').val()) {
			if(required_msg.length > 0) {
				required_msg += ', ';
			}		
			required_msg += 'Subject';
		}

		if (!$('textarea[name=enquiry]').val()) {
			if(required_msg.length > 0) {
				required_msg += ', ';
			}		
			required_msg += 'Message';
		}

		if(required_msg.length > 0) {
			contact.message += "The following fields are required: " + required_msg + ".";
		}
		if(invalid_msg.length > 0) {
			if (contact.message.length > 0) {
				contact.message += " ";
			}
			contact.message += "The following fields are invalid: " + invalid_msg  + ".";
		}

		if (contact.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	showError: function () {
		$('#cboxLoadedContent .message').html($('<div class="messageStackError"></div>').append(contact.message)).fadeIn(200);
	}
};