// checkform.js - v0.4 - 25 July 2002 - MP
// JavaScript functions for checking data entered into a form

// Valid characters:
var letters = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var specials = "àáèéìíòóùúÀÁÈÉÌÍÒÓÙÚ";
var digits = "0123456789";
var nameString = letters + specials + "-_. '";
var textString = letters + digits + specials + "-., '?!;";
var commentString = textString + "\n\r";
var noticeString =textString + commentString + "!@#$%^&*(){}:|<>?'\/-_ ";
var phoneString = digits + " .-+()";
var emailString = letters + digits + "-_@.'";
var passwordString = letters + digits + "?";
var intString = digits + "-";
var floatString = digits +"-.";
// min/max values for co-ordinates:
var minLatDeg = 51
var minLatMin = 0
var minLatSec = 0
var maxLatDeg = 55
var maxLatMin = 59
var maxLatSec = 59.99999
var minLongDeg = -10
var minLongMin = 0
var minLongSec = 0
var maxLongDeg = -5
var maxLongMin = 59
var maxLongSec = 59.99999
var minUTMEast = 395883
var maxUTMEast = 742910
var minUTMNorth = 5706495
var maxUTMNorth = 6156000
var minITMEast = 400000
var maxITMEast = 800000
var minITMNorth = 500000
var maxITMNorth = 999999
var minIGEast = 0
var maxIGEast = 400000
var minIGNorth = 0
var maxIGNorth = 500000

function isEmpty(elem) {
	// Checks if a form element is empty:
	return ((elem.value == null) || (elem.value.length == 0))
}

function checkMinMax(elem, minValue, maxValue) {
	// Checks if the value of elem is between the minimum and maximum values:
	if ((elem.value >= minValue) && (elem.value <= maxValue)) {
		return true;
	} else {
		alert("Please enter a value for '" + elem.name + "' in the range: '" + minValue + "' to '" + maxValue + "'");
		elem.focus();
		return false;
	}		
}

function checkLength(elem, minLength, maxLength) {
	// Checks that the value of elem has the correct number of characters.
	if ((elem.value.length >= minLength) && (elem.value.length <= maxLength)) {
		return true;
	} else {
		if (minLength == maxLength) {
			alert("Please enter a value for '" + elem.name + "' with " + maxLength + " characters.");
		} else {
			alert("Please enter a value for '" + elem.name + "' with between " + minLength + " and " + maxLength + " characters.");
		}
		elem.focus();
		return false;
	}
}

function compareFields(elem1, elem2, msg) {
	// Checks that elem1 and elem2 have the same value, otherwise
	// displays an error message.

    if (elem1.value == elem2.value) {
        return true;
    } else {
        alert(msg);
        elem1.value = "";
        elem2.value = "";
        elem1.focus();
        return false;
    }
}

function validText(elem, range, msg, isOptional) {
	// Checks if the text entered is within a valid range of characters.
	// Alerts the user if this is not the case. 
	// isOptional indicates if the field can be left blank.
 
	var isOK = true;
	if (isEmpty(elem)) {
		if (! isOptional) {
			alert(msg);
			elem.focus();
			return false;
		} else {
			return true;
		}	
	} else { 
		for (var i = 0; i < elem.value.length; i++) {
			isOK = isOK && (range.indexOf(elem.value.charAt(i)) != -1);
		}
		if (! isOK) {
			alert(msg);
			elem.focus();
		}
		return isOK;
	} 
}

function validEmail(elem, range, msg, isOptional) {
	// Checks if the text entered is within a valid range of characters and that the text 
	// contains a single "@". Alerts user if this is not the case. 
	// isOptional indicates if the field can be left blank.
	
	var isOK = true;
	var atSign = 0;	
	if (isEmpty(elem)) {
		if (! isOptional) {
			alert(msg);
			elem.focus();
			return false;
		} else {
			return true;
		}	
	} else { 
		for (var i = 0; i < elem.value.length; i++) {
			isOK = isOK && (range.indexOf(elem.value.charAt(i)) != -1);
			if (elem.value.charAt(i) == "@") {
				atSign++;
			} 
		}
		if ((! isOK) || (atSign != 1)) {
			alert(msg);
			elem.focus();
			return false;
		}
	}
	return true;
}

function checkPulldown(elem, msg, isOptional) {
	// Checks that user has selected an option from a pulldown menu.
	// If not, displays error message.
	// isOptional indicates if the pulldown can be left blank.
	if (elem.selectedIndex == 0 && !isOptional) {
		alert(msg);
		elem.focus();
		return false;
	} else {
		return true;
	}
}

function checkTypes(elem1,elem2,msg) {
	// Checks that two pulldowns don't have the same value.
	// If they do, displays error message.
	if (elem1.selectedIndex == elem2.selectedIndex) {
		alert(msg);
		elem2.focus();
		return false;
	} else {
		return true;
	}
}

function checkFileExtension(elem,extString,msg) {
	// Checks that the file supplied has the correct file extension, extString.
	// If not, displays error message.
	if (elem.value.indexOf(extString,(elem.value.length - extString.length)) == (elem.value.length - extString.length)) {
		return true;
	} else {
		alert(msg);
		elem.focus();
		return false;
	}
}