/*	Application  : John Day River Boater Permits, Oregon
	Name         : js/forms.js
	Author       : Tim Carley 
	Created      : March 01, 2009
	Last Updated : 3/7/2011
	History      : 3/7/2011; changed isDate(): fixed error when user specifies a date with dashes (mm-dd-yyyy)
					replace the date value with the new one formatted correctly
	Purpose		 : common Javascript functions used on many forms
*/
function isEmpty(inputStr){
	// regular expression for checking for spaces in fields
	var regsp = /^\s{1,}$/ ;
	if (inputStr==null || inputStr=="" || regsp.test(inputStr)){
			return true;
	}
	return false;
}
// check the entered month for too high a value
function checkMonthLength(mm,dd) {
	if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30) {
		alert(months[mm] + " has only 30 days.");
		return false;
	} else if (dd > 31) {
		alert(months[mm] + " has only 31 days.");
		return false;
	}
	return true;
}
// check the entered February date for too high a value 
function checkLeapMonth(mm,dd,yyyy) {
	if (yyyy % 4 > 0 && dd > 28) {
		alert("February of " + yyyy + " has only 28 days.");
		return false;
	} else if (dd > 29) {
		alert("February of " + yyyy + " has only 29 days.");
		return false;
	}
	return true;
}
// date field validation (called by other validation functions that specify minYear/maxYear)
function isDate(gField) {
	var theEle = gField;
	var inputStr = theEle.value;
	// convert hyphen delimiters to slashes
	if (inputStr.indexOf("-") >= 0) {
		inputStr = inputStr.replace(/-/g,"/");
	}
	var delim1 = inputStr.indexOf("/");
	var delim2 = inputStr.lastIndexOf("/");
	if (delim1 != -1 && delim1 == delim2) {
		// there is only one delimiter in the string
		return false;
	}
	if (delim1 != -1) {
		// there are delimiters; extract component values
		var mm = parseInt(inputStr.substring(0,delim1),10);
		var dd = parseInt(inputStr.substring(delim1 + 1,delim2),10);
		var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10);
	} else {
		// there are no delimiters
		return false;
	}
	if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) {
		// there is a non-numeric character in one of the component values
		return false;
	}
	if (mm < 1 || mm > 12) {
		// month value is not 1 thru 12
		return false;
	}
	if (dd < 1 || dd > 31) {
		// date value is not 1 thru 31
		return false;
	}
	// validate year, allowing for checks between year ranges
	// passed as parameters from other validation functions
	if (yyyy < 100) {
		// entered value is two digits, which we allow for 1930-2029
		if (yyyy >= 30) {
			yyyy += 1900;
		} else {
			yyyy += 2000;
		}
	}
	var today = new Date();
	if (!checkMonthLength(mm,dd)) {
		return false;
	}
	if (mm == 2) {
		if (!checkLeapMonth(mm,dd,yyyy)) {
			return false;
		}
	}
	theEle.value = mm +'/'+dd+'/'+yyyy; return true;
}

