
/**
 * When page is loaded
 * - hook validation method
 * - Check / clean message box
 */
$(document).ready(function() {
	$('#ContactForm').submit(validateContactForm);
	
});

/**
 * Client side validation
 * - hopefully reduce the number of server side validations needed.
 */
function validateContactForm(form) {
	clearMessageBox();
	var shouldSend = true;
	if($('[name=FullName]').val() == '') {
		appendErrorMessage("Name must be filled in.");
		shouldSend = false;
	}

	var email = $('[name=Email]').val();
	if(email != null && email != '') {
		var address = email.match(/[\w|\.]+@(\w+\.)+[a-z]+/i);
		if(address == null || address[0] == null || address[0] == '') {
			appendErrorMessage("Entered E-mail does not seem valid.");
			shouldSend = false;
		} else {
			$('[name=Email]').val(address[0]);
		}
	} else {
		appendErrorMessage("E-mail must be filled in.");
		shouldSend = false;
	}

	if($('[name=Comments]').val() == '') {
		appendErrorMessage("Comments must be filled in.");
		shouldSend = false;
	}
	return shouldSend;
}

/**
 * Clear all pending messages in this box
 */
function clearMessageBox() {
	$('#message_block').html('').hide();
}

/**
 * Add an error message to the screen
 */
function appendErrorMessage(msg) {
	var block = $('#message_block');
	var block_cont = block.html() + '<div class="error">' + msg + '</div>';

	block.html(block_cont);
	block.show();
}
