/*
*
* simple functions to enhance form functionality
*
* CJS NHM (c) GPL
*
*/

/*
* checkOnLoad will validate whether certain fields need to be present or not in the formpage, dependant on department code etc.
*  biggest issue is that disabled fields do not return empty parameters, so we can't yet turn the copyright ones off without
* building an object model of expected values; currently we work with received values only
*/
function checkOnLoad() {
	try {
		checkAuthOfficer()
	} catch(err) {}
	try {
		/* checkCopyright()	*/
	} catch (err) {}
}

/*
*
* checkAuthOfficer validates whether the user's chosen department is one which needs authorisation by a named officer
*
*/
function checkAuthOfficer() {
	if (document.mainform.FK_DeptID.value==4 || document.mainform.FK_DeptID.value==7 || document.mainform.FK_DeptID.value==8 
	|| (document.mainform.FK_DeptID.value>=10 && document.mainform.FK_DeptID.value<=23)) {
		document.mainform.FK_AOID.disabled=true
	} else {
		document.mainform.FK_AOID.disabled=false
	}
	return
}

/*
*
* disable copyright subfields if we own the overall rights; cannot use currently as the data values are removed entirely from the
* forms meaning that the array which captures the data breaks.
*
*/
function checkCopyright() {
	if (document.imageform.Copyright_Status.value=="Copyright owned by Natural History Museum") {
		document.imageform.non_nhm_copyright_owner.disabled=true
		document.imageform.CopyrightURL.disabled=true
		document.imageform.FK_CopyID.disabled=true
		document.imageform.ModelReleaseURL.disabled=true
		document.imageform.LocationReleaseURL.disabled=true
	} else {
		document.imageform.non_nhm_copyright_owner.disabled=false
		document.imageform.CopyrightURL.disabled=false
		document.imageform.FK_CopyID.disabled=false
		document.imageform.ModelReleaseURL.disabled=false
		document.imageform.LocationReleaseURL.disabled=false
	}	
	return
}

/*
* enforce maximum data length if we've exceeded a given limit on a textarea
* and optionally display an alert
*/
function enforceMaxLength (limit,currentContent,showAlert) {
	if(currentContent.value.length > limit) {
		if (showAlert) {	
     		alert('You have reached the maximum amount of data allowed in this field');
     		}
     	currentContent.value = currentContent.value.substring(0,limit);
     }
}