// checkform.js - 10 Apr 2003 - triggercommunication
// 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 phoneString = digits + " .-+()";
var emailString = letters + digits + "-_@.'";

function clearElement(elem) {
	// Clears the content of the field when the user clicks on it.
	// Only clears the content if the current value is the default value.
	if (elem.value == elem.defaultValue) {
		elem.value = "";
	}
}

function isEmpty(elem) {
	// Checks if a form element is empty:
	return ((elem.value == null) || (elem.value.length == 0))
}

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 checkContactType(f) {
	// Function to ensure that the user has entered the correct contact details for the contact type selected.
	if (f.contactby.selectedIndex == 0) {
		// e-mail selected:
		return validEmail(f.email,emailString,emailMsg,false);
	} else if (f.contactby.selectedIndex == 1) {
		// phone selected:
		return validText(f.phone,phoneString,phoneMsg,false);
	} else if (f.contactby.selectedIndex == 2) { 
		// fax selected:
		return validText(f.fax,phoneString,phoneMsg,false);
	} else if (f.contactby.selectedIndex == 3) {
		// post selected:
		return validText(f.address,commentString,addressMsg,false);
	} else {
		return true;
	}	
}
