var applyForm;

	//Attach an "onLoad" event to the current window
	window.onload = init;
	
	//Initialization function
	function init() {
		//Attaching the onSubmit event to the login form
		applyForm = document.getElementById('smex-contact');
		applyForm.onsubmit = function () {
			return canSubmit(this);
		}
		
		//Setting focus to the user field
		applyForm.contname.focus();
	}

	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 validEmail(sEmail) {
		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)) {
			return false;
		}
		return true;
	}
	
	
	function canSubmit(form) {
		if (!filled(form.contname)) {
			alert("Please enter your name.");
			form.contname.focus();
			return false;
		}
	if (!filled(form.organisation)) {
			alert("Please enter your comapny details.");
			form.organisation.focus();
			return false;
		}
	if (!filled(form.emailaddr)) {
			alert("Please enter your email address. Otherwise we won't be able to contact you.");
			form.emailaddr.focus();
			return false;
		}
		
		
		//If we have an entry for the email address then ensure it's syntactically valid
		if (filled(form.emailaddr) && !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 telephone number. Otherwise we won't be able to contact you.");
			form.telno.focus();
			return false;
		}

	

		if (!filled(form.msg)) {
			alert("Please enter your message.");
			form.msg.focus();
			return false;
		}

		return true;
	}
		
	
