/*
   FormValidation: Provides a set of validation methods.
   no constructor parameters
   Used by the GiftCatalogOrderForm
*/

function FormValidation() {

	this.logClass = null;
	this.popupw = null;

	//- START log settings
	//*** method setLog : sets the internal log class
	function validate_setLog(param_active, param_newwindow) {
		this.logClass = new jsLOG(param_active, param_newwindow);
	}
	this.setLog = validate_setLog;

	//*** method log : sends the text to the log
	function Page_log( param_str ) {
		this.logClass.log ( param_str );
	}
	this.log = Page_log;
	//- END log settings

	//
	// Form validation functions
	//

	// method to validate text box input is present
	function validate_isPresent(param_obj) {
		var re = /\S/;
		return re.test(param_obj.value);
		/*
		if (param_obj.value == '') {
			return false;
		} else {
			return true;
		}
		*/
	}
	this.isPresent = validate_isPresent;

	// method to validate dropdown input is present
	function validate_isPresentDD(param_obj) {
		if (param_obj.options[param_obj.selectedIndex].value == '') {
			return false;
		} else {
			return true;
		}
	}
	this.isPresentDD = validate_isPresentDD;

	// method to validate text is a certain length -- can use NaN to specify no min or max
	function validate_isCorrectSize(param_obj, minLength, maxLength) {
		var strVal = param_obj.value;
		var strLen = strVal.length;
		var valid = true;
		if (! isNaN(minLength)) {
			if (strLen < minLength) {
				valid = false;
			}
		}
		if (! isNaN(maxLength)) {
			if (strLen > maxLength) {
				valid = false;
			}
		}
		return valid;
	}
	this.isCorrectSize = validate_isCorrectSize;
	
	// method to validate text box input is a valid password (5-15 chars a-z, A-Z, 0-9)
	function validate_isPassword(param_obj) {
		var re = /^[0-9a-zA-Z]{5,15}$/;
		return re.test(param_obj.value);
	}
	this.isPassword = validate_isPassword;

	// method to validate text box input is a valid username (5-10 chars, a-z,A-Z, 0-9)
	function validate_isUsername(param_obj) {
		var re = /^[0-9a-zA-Z]{5,10}$/;
		return re.test(param_obj.value);
	}
	this.isUsername = validate_isUsername;

	// method to validate text box input is a valid email address (VERY simple tests)
	function validate_isEmail(param_obj) {
		var re = /^[^@ ]+@[-_a-zA-Z0-9]+\.[a-zA-Z]{2,}$/;
		return re.test(param_obj.value);
	}
	this.isEmail = validate_isEmail;

	// method to validate text box input is the same
	function validate_isIdentical(param_obj1, param_obj2) {
		if (param_obj1.value == param_obj2.value) {
			return true;
		} else {
			return false;
		}
	}
	this.isIdentical = validate_isIdentical;

	// validate drop down is selected
	function validate_isSelected(param_obj) {
		if(param_obj.options.value == '' || param_obj.options.value == '0') {
			return false;
		} else {
			return true;
		}
	}
	this.isSelected = validate_isSelected;

	// validate input is a number
	function validate_isNumber(param_obj) {
		if (isNumber(param_obj.value)) {
			return true;
		} else {
			return false;
		}
	}
	this.isNumber = validate_isNumber;

	// validate input is a money amount
	function validate_isMoney(param_obj) {
		var moneyVal = parseFloat(param_obj.value);
		if (isNaN(moneyVal)) {
			return false;
		} else {
			if (parseFloat(param_obj.value * 100) == parseInt(param_obj.value * 100)) {
				return true;
			} else {
				return false;
			}
		}
	}
	this.isMoney = validate_isMoney;

	// validate input is an integer
	function validate_isInteger(param_obj) {
		if (isNaN(parseInt(param_obj.value))) {
			return false;
		} else {
			if (parseInt(param_obj.value) == parseFloat(param_obj.value)) {
				return true;
			} else {
				return false;
			}
		}
	}
	this.isInteger = validate_isInteger;

	// validate a number falls within a given range
	function validate_isInRange(param_obj, param_min, param_max) {
		if (param_obj.value) {
			return true;
		} else {
			return false;
		}
	}
	this.isInRange = validate_isInRange;
	
	// validate a number is a valid reservation ID (7 digit number)
	function validate_isReservationID(param_obj) {
		var re = /\d{7}/;
		if (re.test(param_obj.value)) {
			return true;
		} else {
			return false;
		}
	}
	this.isReservationID = validate_isReservationID;
	
	// validate that a radio button in groupName is selected in formName
	function validate_isRadioSelected(formName, groupName) {
		var selected = false;
		var radioGroup = eval("document.forms['" + formName + "']." + groupName);
		var radioGroupLength = radioGroup.length;
		for (var i = 0; i < radioGroupLength; i++) {
			if (radioGroup[i].checked) {
				selected = true;
				break;
			}
		}
		return selected;
	}
	this.isRadioSelected = validate_isRadioSelected;
	
	// validate that at least one checkbox in the array is checked
	function validate_atLeastOneChecked(boxArray) {
		var selected = false;
		for (var i = 0; i < boxArray.length; i++) {
			if (boxArray[i].checked) {
				selected = true;
			}
		}
		return selected;
	}
	this.atLeastOneChecked = validate_atLeastOneChecked;
	
	// validate that a given date is valid
	function validate_isValidDate(month, day, year) {
		var mVal = parseInt(month);
		var dVal = parseInt(day);
		var yVal = parseInt(year);
		// if any element is invalid, the date is invalid
		if (isNaN(mVal) || isNaN(dVal) || isNaN(yVal)) {
			return false;
		}
		// if the day is not valid for the month, the date is invalid
		if (mVal == 9 || mVal == 4 || mVal == 6 || mVal == 11) {
			// 30 days has september, april, june and november
			// so if the day is 31, the date is invalid
			if (dVal == 31) {
				return false;
			}
		}
		if (mVal == 2) {
			// februrary has 28 days except in leap years, when it has 29
			if ((yVal % 4 == 0)  && (yVal % 100 != 0) || (yVal % 400 == 0)) {
				// a leap year
				if (dVal > 29) {
					return false;
				}
			} else {
				// not a leap year
				if (dVal > 28) {
					return false;
				}
			}
		}
		// the date must be valid
		return true;
	}
	this.isValidDate = validate_isValidDate;

}
