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-register');
		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 isChecked(buttonGroup)	{
		var bChecked = false;
	
		for (var i=0; i<buttonGroup.length; i++)  {
	
		    if (buttonGroup[i].checked)  {
		
		    	bChecked = true;
	
		    	break;	
		    } 
		   
		}
		return bChecked;
	}
			

	
	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.contname)) {
			alert("Please enter your Name. We need this information for your invitation.");
			form.contname.focus();
			return false;
		}
		if (!filled(form.contposition)) {
			alert("Please enter your Job title. We need this information for your invitation.");
			form.contposition.focus();
			return false;
		}
		if (!filled(form.organisation)) {
			alert("Please enter your Company's name. We need this information for your invitation.");
			form.organisation.focus();
			return false;
		}
		if (!filled(form.address1)) {
			alert("Please enter your address. We need this information for your invitation.");
			form.address1.focus();
			return false;
		}
		if (!filled(form.town)) {
			alert("Please enter your town. We need this information for your invitation.");
			form.town.focus();
			return false;
		}
		if (!filled(form.postcode)) {
			alert("Please enter your postcode. We need this information for your invitation.");
			form.postcode.focus();
			return false;
		}
		if (!filled(form.telno)) {
			alert("Please enter your telephone. We need this information to be able to contact you in the event of a problem.");
			form.telno.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;
		}

		
		//Ensure we have a value on mfr/distributor
		if ( !isChecked(form.manufacturer) ) {
			alert("Please indicate whether you are a manufacturer or distributor.");
			return false;
		}
		

		return true;
	}
		
	
