/**
 * Validate form given
 * If the form has errors create a list and show them.
 * Will return false if the form should not submit.
 */
function validateForm(form) {

	var submit = true;
	var msg = '<ul class="errList">';

	// Show validation Message and hide error message div
	$('#errmsg').hide();
	$('.sucess').hide();
	$('.error').hide();

	// Loop over form input and select elements
	$('#' + form + ' td > input, select').each(function() {

		// If element has the class required check for a value
		if($(this).hasClass('required')) {
		
			if($(this).val() == '') {
				
				// No value so add to error message and add css to mark element
				msg = msg + '<li>You must enter a value for ' + $(this).attr('id') + '</li>';
				$(this).addClass('missing');
				submit = false;
			
			} else { 
				// Remove class incase it had been set on previous try
				$(this).removeClass('missing'); 
				
			}
		}
		
	});
	
	// Make sure Valid email
	var email = $('#email').val();
	
	if ((email.indexOf(".") > 2) && (email.indexOf("@") > 0)) {
		
		// Remove class incase it had been set on previous try
		$('#email').removeClass('missing'); 
	
	} else {
		
		msg = msg + '<li>You entered an invalid email.</li>';
		$('#email').addClass('missing');
		
		submit = false;
		
	}
	
	// Captcha check
	var int1 = parseInt($('#math1').text());
	var int2 = parseInt($('#math2').text());
	var ans = int1 + int2;
	
	if (ans !== parseInt($('#ans').val())) {
		
		msg = msg + '<li>The answer provided for you math question is incorrect. Please try again.</li>';
		$('#ans').addClass('missing');
		$('#ans').val('');
		
		submit = false;
		
	} else { 
		// Remove class incase it had been set on previous try
		$('#ans').removeClass('missing'); 
		
	}

	// If errors show error message
	if (submit == false) {

		$('#errmsg').show();
		$('#errmsg').html(msg + '</ul>');
		$('.missing').first().focus();
		
	}

	// Return value on whether the form can submit or not
	return submit;
		
}

