function contacts_onload()
{
	if (document.entity && document.entity.docid.value) {
		var type = document.entity.doctype.value;
		type = type.substring(0,1).toUpperCase() + type.substring(1);
		document.title = "View " + type + " : " + document.entity.name.value + " - " + document.title;
	} else if (document.add_entity) {
		var name = '';
		if (document.add_entity.person__given_names) {
			name = document.add_entity.person__given_names.value + ' '
				document.add_entity.person__family_name.value;
		}
		if (document.add_entity.organisation__name) {
			name = document.add_entity.organisation__name.value;
		}
		document.title = "Searching for " + name + " - " + document.title;
	} else if (document.location.toString().match(/contacts\/([a-z_]+)/)) {
		var location = document.location.toString().match(/contacts\/([a-z_]+)/)[1];
		location = location.replace(/_/g, ' ');
		location = location.replace(/\b\w+\b/g,
			function(word) {
				return word.substring(0,1).toUpperCase() + word.substring(1);
			}
		);
		document.title = location + " - " + document.title;
	}
	set_now();
	FormsClassParser.apply();
}

//
// validate and reformat date string on an element
//
function date_format(e)
{
	var str = e.value;

	function _dateCheck(day, month, year)
	{
		if (
			isNaN(day) || isNaN(month) || isNaN(year)
			|| day < 1 || day > daysInMonth(month,year)
			|| month < 1 || month > 12
			|| year < 1000 || year > 2100
		) {
			return false;
		}
		return true;
	}

	function _dateFmt(e, day, month, year)
	{
		e.value = sprintf('%02d/%02d/%04d', day, month, year);
		return true;
	}


	// dd/mm/yyyy
	var match = str.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
	if (match) {
		if (_dateCheck(match[1], match[2], match[3])) {
			return _dateFmt(e, match[1], match[2], match[3]);
		}
		return false;
	}
	// dd-mm-yyyy
	match = str.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/)
	if (match) {
		if (_dateCheck(match[1], match[2], match[3])) {
			return _dateFmt(e, match[1], match[2], match[3]);
		}
		return false;
	}
	// ddmmyyyy
	match = str.match(/^(\d\d)(\d\d)(\d{4})$/)
	if (match) {
		if (_dateCheck(match[1], match[2], match[3])) {
			return _dateFmt(e, match[1], match[2], match[3]);
		}
		return false;
	}
	// yyyy-mm-dd
	match = str.match(/^(\d{4})-(\d\d)-(\d\d)$/)
	if (match) {
		if (_dateCheck(match[3], match[2], match[1])) {
			return _dateFmt(e, match[3], match[2], match[1]);
		}
		return false;
	}

	return false;
}
