// checkform.js - 09/12/06 - FG
// JavaScript functions for checking data entered into a form

// Valid characters:
var letters = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var specials = "àáèéìíòóöùúÀÁÈÉÌÍÒÓÙÚ";
var specialcharacters = "\\(\\)<>@&,;:°\\\\\\\"\\.\\[\\]";
var digits = "0123456789";
var nameString = letters + digits + specials + "-_. '";
var textString = letters + digits + specials + specialcharacters + "() \ /-~., '?!&;:";
var typeString = letters + digits + specials + specialcharacters + textString + "+() \ /-~., '?!&;:";
var commentString = textString + "\n\r€";
var phoneString = digits + " .-+()";
var emailString = letters + digits + /^(.+)@(.+)$/;
var alphanumericString = letters + digits;
var intString = digits + "-";
var floatString = digits +"-.";
var currencyString = digits + ".,";
var dateString = digits + "/";
var timeString = digits + ":";

function isEmpty(elem) {
	// Checks if a form element is empty:
	return ((elem.value == null) || (elem.value.length == 0))
}


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)) {
	if ((elem.value.length <= 500)) {
		return true;
	} else {
		if ((elem.value.length > 500)) {
			alert("Too much text has been entered in the Information box, please delete some text.\nMaximum number of characters allowed is 500.");
		} 
		//else {
		//	alert("Please enter a value for '" + elem.name + "' with between " + minLength + " and " + maxLength + " characters.");
		//}
		elem.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, msg, isOptional)  {
	
	var isOK = true;
	if(!isOptional && elem.value =='') {
		alert(msg);
		elem.focus();
		return false;
		}
	with(elem.value) {
		if (length <8
			|| match(/[^A-Z0-9@\._-]/i)
			|| match(/(@.*@)|(@\.)|(\.@)|(^\.)|(\.{2})|(-{2})|(_{3})/)
			|| match(/(-@)|(\@-)|(\.-)|(-\.)/)
			|| !match(/^.+\@(\[?)[A-Z0-9\-\.]+\.([A-Z]{2,3}|[0-9]{1,3})(\]?)$/i)
		)
		{
			alert(msg);
			elem.value =''; //you can uncomment this to clear the field
			elem.focus();
			return false;
		}
	//alert('good one'); //remove this line in production mode
	return isOK;
	}
}




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;
	}
}
