var fm;

	//Attach an "onLoad" event to the current window
	window.onload = init;
	
	//Initialization function
	function init() {
		//Attaching the onSubmit event to the login form
		fm = document.getElementById('msp-contact');
		fm.onsubmit = function () {
			return canSubmit(this);
		}
		
		fm.contName.focus();
		
	}

//Email Validation
//bFormSubmit is an optional boolean which indicates whether we hit the function from a form submission (the 
//default) or from an onChange event on an email address field.
function validEmail(sEmail,bFormSubmit) {
	if (typeof bFormSubmit == 'undefined')	{
		var bFormSubmit = true;
	}
	//var re = /[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/;
	var re = new RegExp("^[a-z0-9][a-z0-9_\.-]{0,}[a-z0-9]@[a-z0-9][a-z0-9_\.-]{0,}[a-z0-9][\.][a-z0-9]{2,4}$");
	//test the supplied email address
	if(!re.test(sEmail)) {
		if(bFormSubmit)	{		
			return false;
		}
		else	{
			alert('Please enter a valid email address.');
			setReqField('emailAddr');
			return false;
		}
	}
	return true;
}

function isBlank(s) {
		for (i=0;i<s.length;i++ ) {
			var c = s.charAt(i);
			if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		}
		return true;
	}

	function filled(field) {
		if (field.value == "" || field.value == null || isBlank(field.value)) {
			return false;
		} else {
			return true;
		}
	}
	
	function isNumber(parm) {
		var numb = '0123456789\.';
		for (i=0; i<parm.length; i++) {
			if (numb.indexOf(parm.charAt(i),0) == -1) return false;
		}
		return true;
	}

	
	function canSubmit(form) {
		
		if(!filled(form.contName))	{
			alert("Please enter your name.");
			form.contName.focus();
			return false;
		}
		if(!filled(form.emailAddr))	{
			alert("Please enter your email address.");
			form.emailAddr.focus();
			return false;
		}
		if(!validEmail(form.emailAddr.value))	{
			alert("Please enter a valid email address.");
			form.emailAddr.focus();
			return false;
		}
		if(!filled(form.telNo))	{
			alert("Please enter your contact number.");
			form.telNo.focus();
			return false;
		}
		
		if(!isNumber(form.telNo.value))	{
			alert("Please enter your contact number using numbers only.");
			form.telNo.focus();
			return false;
		}
	
		if(!filled(form.msg))	{
			alert("Please tell us how we can help you.");
			form.msg.focus();
			return false;
		}	
			
		return true;
	}
