// Check-whole-form container
// Runs check functions for all data types
function checkForm() {
	if (typeof document.notNullItems != "undefined") {
		if( !checkNotEmpty(document.notNullItems)) {
			return false;
		}
	}

	if (typeof document.integerItems != "undefined") {
		if( !checkIntegers(document.integerItems)) {
			return false;
		}
	}

	if (typeof document.percentItems != "undefined") {
		if( !checkPercentages(document.percentItems)) {
			return false;
		}
	}

	if (typeof document.percentListItems != "undefined") {
		if( !checkPercentageLists(document.percentListItems)) {
			return false;
		}
	}

	if (typeof document.dDmMyyYYItems != "undefined") {
		if( !checkdDmMyyYY(document.dDmMyyYYItems)) {
			return false;
		}
	}

	return true;
}

function getInputNameIfPoss(thisElem) {
	var humanReadable = "The highlighted field";

	var label = getLabelForInput(thisElem);
	if (label != null) {
		humanReadable = '"' + label.innerHTML.replace(/<.*>/g, "") + '"';
	}
	return humanReadable;
}

// Check not empty, powered by NOT NULL (@notnull) in SQL (XML) config
function checkNotEmpty(checkThese) {
	for (var iCheck = 0; iCheck < checkThese.length; iCheck++) {

		thisElem = document.getElementById(checkThese[iCheck]);
		if (thisElem == null) {
		} else {
			// Generic prepare function
			prepareForCheck(thisElem);

			if (thisElem.value == "") {
				// Generic highlight function
				highlightInvalid(thisElem, getInputNameIfPoss(thisElem) + " is required. Please complete and resubmit.");
				return false;
			}
		}
	}
	return true;
}

function getLabelForInput(input) {
	var inputID = input.id;

	var labels = document.getElementsByTagName("label");
	for (var i = 0; i < labels.length; i++) {
		var label = labels[i];
		if (label.htmlFor == inputID) {
			return label;
		}
	}

	return null;
}

// Check dates
function checkdDmMyyYY(checkThese,doIComplain) {

	if (typeof doIComplain == "undefined") {
		var doIComplain = true;
	}

	for (var iCheck = 0; iCheck < checkThese.length; iCheck++) {

		thisElem = document.getElementById(checkThese[iCheck]);
		if (thisElem == null) {
			alert('Cant find ' + checkThese[iCheck]);
		} else {

			prepareForCheck(thisElem);

			if (thisElem.value.search(/^[0-3]?\d\/[0-1]?\d\/(2\d)?\d\d$/) == -1) {
				doIComplain && highlightInvalid(thisElem,getInputNameIfPoss(thisElem) + "The highlighted field is not a valid date of format dD/mM/yyYY");
				return false;
			}

			// Add 20- to the year
			thisElem.value = thisElem.value.replace(/\/(\d\d)$/g, "/20$1");
		}
	}
	return true;
}

// Check numerics; powered by single_input_custom_tweaks.cfm
function checkIntegers(checkThese) {
	for (var iCheck = 0; iCheck < checkThese.length; iCheck++) {

		thisElem = document.getElementById(checkThese[iCheck]);
		if (thisElem == null) {
		} else {

			prepareForCheck(thisElem);

			if (thisElem.value.search(/^\s*\d*(\.\d+)?\s*$/) == -1) {
				highlightInvalid(thisElem,getInputNameIfPoss(thisElem) + " is not a valid number");
				return false;
			}

			// We have a percentage
			thisElem.value = thisElem.value.replace(/%/g, "");
			// We now have a number
			thisElem.value = thisElem.value.replace(/(.\d\d)\d+$/g, "$1");
		}
	}
	return true;
}

// Check percentages; powered by single_input_custom_tweaks.cfm
function checkPercentages(checkThese) {
	for (var iCheck = 0; iCheck < checkThese.length; iCheck++) {

		thisElem = document.getElementById(checkThese[iCheck]);
		if (thisElem == null) {
		} else {

			prepareForCheck(thisElem);

			// Remove validation, FOR NOW.
			return true;

			if (thisElem.value.search(/^\s*(100|\d\d?(\.\d+)?)%?\s*$/) == -1) {
				highlightInvalid(thisElem,getInputNameIfPoss(thisElem) + " is not a valid percentage (with or without the %) no greater than 100%.");
				return false;
			}

			// We have a percentage, so remove optional %
			thisElem.value = thisElem.value.replace(/%/g, "");
			// We now have a number, so round to 2 decimal places
			thisElem.value = thisElem.value.replace(/(.\d\d)\d+$/g, "$1");
		}
	}
	return true;
}

// Check percentage lists e.g. percentage,percentage,percentage
// powered by single_input_custom_tweaks.cfm
function checkPercentageLists(checkThese) {
	for (var iCheck = 0; iCheck < checkThese.length; iCheck++) {

		thisElem = document.getElementById(checkThese[iCheck]);
		if (thisElem == null) {
		} else {

			prepareForCheck(thisElem);

			if (thisElem.value.search(/^\s*(((100|\d\d?(\.\d+)?)%?)(\s*;\s*(100|\d\d?(\.\d+)?)%?)*)?\s*$/) == -1) {
				highlightInvalid(thisElem, getInputNameIfPoss(thisElem) + "The highlighted field is not a list of percentages separated by semicolons ';' .");
				return false;
			}
		}
	}
	return true;
}

// Generic find-elements function; finds elements based on input/@name
// Superseded by with-ID checking
function findFormElems(elemNames) {
	var arrayToReturn = new Array();
	for (var iName = 0; iName < elemNames.length; iName++) {
		for (var iForm = 0; iForm < document.forms.length; iForm++) {
			if (typeof elemNames[iName] != "undefined") {
				arrayToReturn.push(document[iForm][elemNames[iName]]);
			}
		}
	}
	return arrayToReturn;
}

// Generic remove-any-red-border function
// NB most <input>s aren't actually 'gray', but a bit lighter
function prepareForCheck(inputObj) {
	// Clear of any class
	Element.removeClassName(inputObj.parentNode,"javascript-alerted");
}

// Generic put-a-red-border and whine function
function highlightInvalid(inputObj,alertText) {

	Element.addClassName(inputObj.parentNode,"javascript-alerted");
	alert(alertText);

	// Focus and move!
	inputObj.focus();
	Position.prepare();
	var xy = Position.cumulativeOffset(inputObj);
	window.scrollTo(xy[0], xy[1]-20);

}

