ValidationUtil = new Object();
ValidationUtil.REGEX_NON_ASCII = new RegExp("[^\\x00-\\x7F]");
ValidationUtil.REGEX_EMAIL     = new RegExp("^([^()<>@,;:\\\\\".\\s\\x00-\\x1F\\x7F\\[\\]]+|\"([^\\\\\"\\x0D]|\\\\.)*\")(\\.([^()<>@,;:\\\\\".\\s\\x00-\\x1F\\x7F\\[\\]]+|\"([^\\\\\"\\x0D]|\\\\.)*\"))*@[^()<>@,;:\\\\\".\\s\\x00-\\x1F\\x7F\\[\\]]+(\\.[^()<>@,;:\\\\\".\\s\\x00-\\x1F\\x7F\\[\\]]+)*\\.([^()<>@,;:\\\\\".\\s\\x00-\\x1F\\x7F\\[\\]]{2,4}|museum)$");
ValidationUtil.REGEX_LTRIM     = new RegExp("^\\s*(.*)");
ValidationUtil.REGEX_RTRIM     = new RegExp("([^\\s]*)\\s*$");
ValidationUtil.REGEX_INTEGER   = new RegExp("^\\d+$");
ValidationUtil.REGEX_NUMERIC   = new RegExp("^\\d+(\\.\\d*)?$");

/**
 * Returns true if the given value looks like an e-mail address.
 */
ValidationUtil.isEmail = function(param) {
    return !ValidationUtil.REGEX_NON_ASCII.test(param) && ValidationUtil.REGEX_EMAIL.test(param);
}
/**
 * Returns true if the given value is numeric
 */
ValidationUtil.isNumeric = function(param) {
    return ValidationUtil.REGEX_NUMERIC.test(param);
}
/**
 * Returns true if the given value is an integer
 */
ValidationUtil.isInteger = function(param) {
    return ValidationUtil.REGEX_INTEGER.test(param);
}
/**
 * Returns true if javascript can parse the given value into a date.
 * JavaScript is more forgiving than the isDateYMD function, below.
 */
ValidationUtil.isDate = function(param) {
    return (Date.parse(param)) ? true : false;
}
/**
 * Returns true if the given parameters specify a date.  Year, Month, then Day
 * Example: 
 *   Feb 29, 2004 (2004,2,29) is a date, 
 *   Feb 29, 2003 (2003,2,29) is not.
 */
ValidationUtil.isDateYMD = function(year, month, day) {
    if( ValidationUtil.REGEX_INTEGER.test(month) &&
        ValidationUtil.REGEX_INTEGER.test(day) &&
        ValidationUtil.REGEX_INTEGER.test(year)) {
        var d = new Date(year, --month, day);
        return (d && d.getFullYear()==year && d.getMonth()==month && d.getDate()==day);
    }
    return false;
}
/**
 * Returns true if the given parameters specify a date. Month, Day, then Year.
 */
ValidationUtil.isDateMDY = function(month, day, year) {
	return ValidationUtil.isDateYMD(year, month, day);
}
/**
 * Returns true if the given parameter 'has a value'.
 */
ValidationUtil.hasValue = function(param) {
    if(!param || (typeof param!="string")) return false;
    param = param.replace(ValidationUtil.REGEX_LTRIM, "$1");
    param = param.replace(ValidationUtil.REGEX_RTRIM, "$1");
    return param != "";
}

/**
 * Returns a simplified version of the form field's value.
 * 
 * It handles values from select boxes, check/radio boxes, 
 * text areas and text fields (password, hidden and open text).
 * 
 * Multiple fields of the same name will have their values 
 * concatenated with commas (or the optionally given 
 * concatenation delimiter).
 */
ValidationUtil.getFieldValue = function(formObject, fieldName, delim) {
	var result = "";
    for(var i=0; i<formObject.elements.length; i++) {
        var elementObject = formObject[i];
        var name = elementObject.name;
        if(name && name.toLowerCase()==fieldName.toLowerCase()) {
			var value = "";
		    // handle a single checkbox/radio button
		    if((typeof elementObject.type != "undefined") && elementObject.type!=null && (elementObject.type.toLowerCase()=="checkbox" || elementObject.type.toLowerCase()=="radio")) {
		        if(elementObject.checked) {
		            value = elementObject.value;
		        }
		    // handle text input fields and textareas
		    } else if((typeof elementObject.value != "undefined") && elementObject.value!=null) {
		        value = elementObject.value;
		    // handle a select box
		    } else if(elementObject.options && (typeof elementObject.options.length == "number") && (typeof elementObject.selectedIndex == "number")) {
				if(typeof elementObject.options[elementObject.selectedIndex].value != "undefined") {
			        value = elementObject.options[elementObject.selectedIndex].value;
				} else {
			        value = elementObject.options[elementObject.selectedIndex].innerText;
				}
		    }
            if(!result)
                result = value;
            else if(value) 
                result = result + ((delim)?delim:",") + value;
        }
    }
    return result;
}

/**
 * Returns true the first time called, false if called again within one second.
 * This function can help prevent double-posts from users double-clicking the submit button.
 */
var debounceVal = true;
ValidationUtil.debounce = function() {
	if(!debounceVal) return false;
	debounceVal = false; window.setTimeout("debounceVal=true;", 1000); return true;
}

/**
 * Handles onclick for Generic Optin checkboxes.
 * Checkboxes must have id='cbGenOptinYes' and id='cbGenOptinNo' respectively
 */
ValidationUtil.onclickGenOptinYes = function() {
	var ocbYes = document.getElementById("cbGenOptinYes");
	var ocbNo = document.getElementById("cbGenOptinNo");
	if (ocbYes && ocbNo)	ocbNo.checked = (ocbYes.checked ? false : true );
}
ValidationUtil.onclickGenOptinNo = function() {
	var ocbYes = document.getElementById("cbGenOptinYes");
	var ocbNo = document.getElementById("cbGenOptinNo");
	if (ocbYes && ocbNo)	ocbYes.checked = (ocbNo.checked ? false : true );
}
/**
 * Generic Optin Checkboxes must have id='cbGenOptinYes' and id='cbGenOptinNo' respectively
 * Returns true if either checkbox is checked, false if neither is checked.
 */
ValidationUtil.hasGenOptinChoice = function() {
	var ocbYes = document.getElementById("cbGenOptinYes");
	var ocbNo = document.getElementById("cbGenOptinNo");
	return (ocbYes && ocbNo && (ocbYes.checked || ocbNo.checked));
}