$(document).ready(function(){
	init_form();
});

function init_form() {
	$('#contact-form').submit(function(){
		email = $.trim($('#email').val());
		phone = $.trim($('#phone').val());
		
		var error = false;
		$(this).find('.alert').removeClass('alert');
		
		if(!email.length && !phone.length) {
			// no contact info supplied
			$('#phone, #email, .instructions').addClass('alert');
			return false;
		} else {
		
			if( email.length && !checkemail(email) ) {
				// invalid info supplied
				$('#email, .instructions').addClass('alert');
				error = true;
			}
			
			if( phone.length && !checkphone(phone) ) {
				// invalid info supplied
				$('#phone, .instructions').addClass('alert');
				error = true;
			}
			// we are good
			return !error;
		}
		
	});
}

function checkemail(email) {
	/* This RegExp requires an address of the form xxx@xxx.xxx where xxx is one or more alphanumeric characters.  The last xxx can't have any numbers.  */
	var address= /[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}/;
	var match = address.test(email);
	return match;
}

function checkphone(num) {
	/* This functionjust strips non-digits, and checks to see that there are at least 10 numbers left over */
	x = num.replace(/[\D]*/g,"");
	if (x.length<10) {
		return false;
	} else {
		return true;
	}
}