//
// Finds a form element in a form based on the given name
function findFormElement(form, name) {
	for(var i=0; i<form.elements.length; i++)
		if(form.elements[i].name == name)
			return form.elements[i];
}
//
// Reads the value of a form element independent of it's type.
function getFieldValueByName(form, name) {
	formObj = findFormElement(form, name);
	if (formObj) {
		if (formObj.type.toLowerCase().indexOf('select') != -1) {
			var selected = '';
			for(var i=0; i<formObj.options.length; i++) {
				if (formObj.options[i].selected) {
					return formObj.options[i].value;
				}
			}
		} else if (formObj.type.toLowerCase() == 'radio') {
			for(var i=0; i<formObj.form.elements.length; i++) {
				if ((formObj.form.elements[i].name == formObj.name) && (formObj.form.elements[i].checked)) {
					return formObj.form.elements[i].value;
				}
			}
		} else {
			return formObj.value;
		}
	}
	return '';
}
//
// Trims leading zero characters from any string
function trimLeadingZeroes(str) {
	while(str.charAt(0) == '0') str = str.substring(1);
	return str;
}
//
// Validates a number (and checks that it is within the boundaries)
function validateNumber(str, min, max) {
	var intVal = parseInt(trimLeadingZeroes(trimString(str)));
	if (isNaN(intVal) || (intVal != str)) {
		return false;
	} else {
		if ((min && (intVal < min)) || (max && (intVal > max))) {
			return false
		} else {
			return true;
		}
	}
}
//
// Trims control characters from the beginning and the end of a string.
// If removeChars is defined, all characters in that string is removed as well.
function trimString(str, removeChars) {
	if (!removeChars) removeChars = "";
	result = ""; startIndex = 0; doLoop = true;
	while ((startIndex < str.length) && doLoop) {
		if ((str.charCodeAt(startIndex) > 32) && (removeChars.indexOf(str.charAt(startIndex)) == -1)) {
			doLoop = false;
		} else startIndex++;
	}
	if (!doLoop) {
		endIndex = str.length - 1; doLoop = true;
		while ((endIndex > startIndex) && doLoop) {
			if ((str.charCodeAt(endIndex) > 32) && (removeChars.indexOf(str.charAt(endIndex)) == -1)) {
				doLoop = false;
			} else endIndex--;
		}
		return str.substring(startIndex, endIndex + 1);
	} else return "";
}

//
// Validates a date value.
// {constraint} can be "older", "newer" or "any", indicating what date values are accepted (compared to today's date).
function validateDate(str, constraint) {
	//
	// Return the empty string as valid date
	if (str.length > 0) {
		//
		// Determine the separator. If can not be determined, return failure.
		if (str.indexOf('/') > -1) {
			separator = '/'
		} else if (str.indexOf('-') > -1) {
			separator = '-'
		} else if (str.indexOf('.') > -1) {
			separator = '.'
		} else return false;
		//
		// Split the string value (after trimming) by the separator
		dateParts = trimString(str, separator).split(separator);
		//
		// Only continue if we have all three date parts
		if (dateParts.length == 3) {
			//
			// Trim, convert into an integer all dateparts
			year	= parseInt(trimLeadingZeroes(trimString(dateParts[0])));
			month	= parseInt(trimLeadingZeroes(trimString(dateParts[1])));
			day	= parseInt(trimLeadingZeroes(trimString(dateParts[2])));
			//
			// Continue only if the integer conversion was sucessful,
			// and all the dateparts are within a 'normal' range
			if (!isNaN(year) && !isNaN(month) && !isNaN(day) && (year > 1900) && (year < 3000) && (month >= 1) && (month <= 12) && (day >= 1) && (day <= 31)) {
				//
				// Make a javascript date object out of the dateparts
				dateVal = new Date(year, month - 1, day);
				//
				// Check the values in the javascript object is the same as we entered.
				// This ensures that noone can enter 31th of February for example.
				if ((dateVal.getMonth() == (month - 1)) && (dateVal.getFullYear() == year) && (dateVal.getDate() == day)) {
					if (constraint && (constraint != "any")) {
						now = new Date();
						today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
						if ((constraint == "older") && (today >= dateVal)) {
							return true;
						} else if ((constraint == "newer") && (today <= dateVal)) {
							return true;
						} else return false;
					} else return true;
				} else return false;
			} else return false; // Return false if the conversion failed
		} else return false; // Return false if the datepart count is not exactly three
	} else return true; // Return true when called with an empty string
}

