/**
 * validate.js - JavaScript validation functions
 * 
 * Various functions to help validate a web form
 * @package standard
 * @author Pat 'omnicolor' Adams (omni@digitaldarkness.com)
 * @copyright 2005 Digital Darkness (http://digitaldarkness.com)
 */

/**
 * trim - Get rid of whitespace
 *
 * @param string str String to trim
 * @return string String without the whitespace
 */
function trim(str) {
	return str.replace(/^\s+|\s+$/g, '')
}

/**
 * isEmpty - Returns true if the field is empty
 *
 * @param string s String to validate
 * @return bool True if the field is empty
 */
function is_empty(s) {
	return ((s == null) || (s.length == 0));
}

/**
 * is_whitespace - Returns true if the value is nothing but whitespace
 *
 * @param string s String to test
 * @return bool True if the string is just whitespace
 */
function is_whitespace(s) {
	var whitespace = " \t\n\r";

	// Is s empty?
	if (is_empty(s)) {
		return true;
	}

	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.
	for (var i = 0; i < s.length; i++) {
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) {
			return false;
		}
	}
	// All characters are whitespace.
	return true;
}

/**
 * is_integer - Returns true if the value is an integer
 * 
 * @param string i Value to test for integerosity
 * @return bool True if the value is an integer
 */
function is_integer(i) {
	if (isNaN(parseInt(i))) {
		return false;
	} else {
		return true;
	}
}

/**
 * check_integer_range - Make sure an integer is within range
 *
 * @param integer i Integer to test
 * @param integer minimum Lower end of the range
 * @param integer maximum Upper end of the range
 * @return bool True if the integer is within range
 */
function check_integer_range(i, minimum, maximum) {
	if (!is_integer(i)) {
		return false;
	}

	if ((i < minimum) || (i > maximum)) {
		return false;
	}
	
	return true;
}

/**
 * check_email - Make sure a given email address looks valid
 *
 * @param string s Email address to test
 * @return bool True if the email looks legitimate
 */
function check_email (s) {
	if (is_empty(s)) {
		return false;
	}

	// Make sure the email looks legit
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(s))) { 
		return false
	}

	// Test email for illegal characters
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
	if (s.match(illegalChars)) {
		return false;
	}
	return true;
}

/**
 * check_length - tests if a string is within the required length
 *
 * Use -1 for no minimum or a really high value for no maximum
 * @param string s String to test
 * @param integer minimum Minimum length for the string
 * @param integer maximum Maximum length for the string
 * @return bool True if the string is within the required length
 */
function check_length(s, minimum, maximum) {
	if ((s.length < minimum) || (s.length > maximum)) {
		return false;
	} else {
		return true;
	}
}

function val_field_exists(field, message) {
	if (is_empty(document.getElementById(field).value)) {
		document.getElementById(field).focus();
		document.getElementById('err_' + field).innerHTML = message;
		document.getElementById(field).style.backgroundColor = '#FFAAAA';
		old = field;
		return false;
	} else {
		return true;
	}
}

function val_field_integer(field, message) {
	if (!is_integer(document.getElementById(field).value)) {
		document.getElementById(field).focus();
		document.getElementById('err_' + field).innerHTML = message;
		document.getElementById(field).style.backgroundColor = '#FFAAAA';
		old = field;
		return false;
	} else {
		return true;
	}
}

function val_field_length(field, message, min_length, max_length) {
	if (!check_length(document.getElementById(field).value, min_length, max_length)) {
		document.getElementById(field).focus();
		document.getElementById('err_' + field).innerHTML = message;
		document.getElementById(field).style.backgroundColor = '#FFAAAA';
		old = field;
		return false;
	} else {
		return true;
	}
}

function val_drop_down(field, message) {
	if (is_empty(document.getElementById(field).value) || document.getElementById(field).value == 0) {
		document.getElementById(field).focus();
		document.getElementById('err_' + field).innerHTML = message;
		document.getElementById(field).style.backgroundColor = '#FFAAAA';
		old = field;
		return false;
	} else {
		return true;
	}
}

/**
 * val_field_integer_generic - Generic error message for number fields with text input
 *
 * Check to see if user submitted data is numeric IF it is filled in. Prints a generic error message and
 * changes the background of the field in question
 * @param string field Field name to check
 */
function val_field_integer_generic(field) {
	if (!is_empty(document.getElementById(field).value) && !is_integer(document.getElementById(field).value)) {
		document.getElementById(field).focus();
		document.getElementById(field).style.backgroundColor = '#FFAAAA';
		document.getElementById('err_' + field).innerHTML = 'Field must be numeric';
		old = field;
		return false;
	} else {
		return true;
	}
}