function guessName() {
	// Try and guess the name based on the email address.  Does nothing is a
	// forename or surname have already been entered.
	if (!document.getElementById) return false;
	var forename = document.getElementById('id_forename');
	var surname = document.getElementById('id_surname');
	if (forename.value || surname.value) return false;

	var email = document.getElementById('id_email');
	if (email.value.indexOf('@') > 0) {
		var emailAddress = email.value.split('@')
		if (emailAddress[0].indexOf('.') > 0) {
			var splitChar = '.';
		}
		else if (emailAddress[0].indexOf('_') > 0) {
			var splitChar = '_';
		}
		if (splitChar) {
			var name = emailAddress[0].split(splitChar);
			forename.value = name[0].substring(0, 1).toUpperCase() + name[0].substring(1);
			surname.value = name[1].substring(0, 1).toUpperCase() + name[1].substring(1);
		}
	}
}


function guessSchoolCountry() {
	// Called once the applicant filled out their school's address; let's help
	// them out and assume the school is in the same country as they are.
	// Does nothing if school has already been entered.
	if (!document.getElementById) return false;
	var school_country = document.getElementById('id_school_country');
	var school_address = document.getElementById('id_school_address');
	if (!school_country.value && school_address.value) {
		var country = document.getElementById('id_country');
		school_country.value = country.value;
	}
};


applicationRules = {
	'#id_school_address': function(el) {
		el.onblur = guessSchoolCountry;
	},
	'#id_email': function(el) {
		el.onblur = guessName;
	}
};


Behaviour.addLoadEvent(function() {
	document.getElementsByTagName('input')[0].focus();
});
Behaviour.register(applicationRules);