// Function checks whether over 1000 control numbers have been selected
// from control number list;
// Function also checks the validity of the beginning and ending dates
	function chkEntry(form)
	{
		var control = form.controlnumbervalue
		var leadingwhitespaces = /^\s+/;
		var trailingwhitespaces = /\s+$/;
		var multiwhitespaces = /\s+/g;
		var singlewhitespace = /\s/g;
		var numSelected = 0;
		if (control) {
			for (i = 0; i < control.options.length; i++) {
				if (control.options[i].selected) numSelected++
			}
			if (numSelected > 1000) {
				alert("You have selected over 1000 control numbers.  Please select less than 1000 control numbers.");
				return false;
			}
		}else{
			// Check the control number
			var control = form.controlnumvalue;
			if (control) {
				if (control.value != "") {
					var controlnum = control.value;
					if (controlnum.length > 7) {
						alert("The Control Number must be seven characters or less.");
						return false;
					}else{
						if (!controlnum.match( /^[a-zA-Z0-9\-]{1,7}$/)) {
							alert("The Control Number should only contain alphanumeric characters or a hyphen [-].");
							return false;
						}
					}
				}
			}
			// Check the beginning date of the Date Range
			var control = form.begindate;
			if (control) {
				if (control.value != "") {
					var beginDate = control.value;
					if (!beginDate.match( /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)) {
						alert("Please enter the From date in MM/DD/YYYY format.");
						return false;
					}else{
						var Year = beginDate.substring(6)-0;
						var Month = beginDate.substring(0,2)-0;
						var Date = beginDate.substring(3,5)-0;
						var currentDate = thisDate();
						var fromDate = setThisDate(Year,Month,Date);
						var endYear = currentDate.getFullYear() - 0;
						var Msg = checkDate(Year,Month,Date,1972,endYear);
						if (Msg.length != 0) {
							alert("The From date in the Date Range is not correctly formatted: " + Msg);
							return false;
						}
					}
				}
			}		
			// Check the end date of the Date Range
			var control = form.enddate;
			if (control) {
				if (control.value != "") {
					var endDate = control.value;
					if (!endDate.match( /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)) {
						alert("Please enter the To date in MM/DD/YYYY format.");
						return false;
					}else{
						var Year = endDate.substring(6)-0;
						var Month = endDate.substring(0,2)-0;
						var Date = endDate.substring(3,5)-0;
						var currentDate = thisDate();
						var endYear = currentDate.getFullYear() - 0;
						var Msg = checkDate(Year,Month,Date,1972,endYear);
						if (Msg.length != 0) {
							alert("The To date in the Date Range is not correctly formatted: " + Msg);
							return false;
						}else{
							if (fromDate) {
								var toDate = setThisDate(Year,Month,Date);
								if (fromDate >= toDate) {
									alert("The From date in the Date Range is not prior to the To date in the Date Range.");
									return false;
								}
							}
						}
					}
				}
			}		
		// Check the word search words and phrases
		var control = form.wordsearchtext;
		var expressionPiece = ""
		if (control) {
			if (control.value != 0) {
				var wordSearch = control.value;
				if (wordSearch.length > 0) {
					if (wordSearch.charAt(0) == ",") {
						alert('The Word Search cannot begin with a comma (,).');
						return false;
					}
					if (wordSearch.charAt(wordSearch.length - 1) == ",") {
						alert('The Word Search cannot end with a comma (,).');
						return false;
					}
				}
				if (wordSearch.search(/"/) != -1) {
					alert('The Word Search cannot contain quotation marks (").');
					return false;
				}
				if (wordSearch.search( /\(/) != -1 || wordSearch.search ( /\)/) != -1 ) {
					alert('The Word Search cannot contain parantheses, ( or ).');
					return false;
				}
				if (wordSearch.search( /\{/) != -1 || wordSearch.search ( /\}/) != -1 ) {
					alert('The Word Search cannot contain braces ({ or }).');
					return false;
				}
				if (wordSearch.search( /\[/) != -1 || wordSearch.search ( /\]/) != -1 ) {
					alert('The Word Search cannot contain brackets ([ or ]).');
					return false;
				}
				if (wordSearch.search( /\</) != -1 || wordSearch.search ( /\>/) != -1 ) {
					alert('The Word Search cannot contain a greater than (>) or less than symbol (<).');
					return false;
				}
				/* Check that "reserved" words are not in the word criteria */
				/* "Reserved" words include "AND","OR","NOT" */
				if (wordSearch.search( /\sAND\s/i) != -1 || wordSearch.search( /\WAND\s/i) != -1 || wordSearch.search( /\sAND\W/i) != -1 || wordSearch.search( /^AND\W/i) != -1 || wordSearch.search(/^AND\s/i) != -1 || wordSearch.search(/\WAND$/i) != -1 || wordSearch.search( /\sAND$/i) != -1) {
					alert('The Word Search cannot contain the reserved word "AND". On the following page, you can modify the word search criteria by using the logical operators, "AND","NOT", and "OR".');
					return false;
				}
				if (wordSearch.search( /\sNOT\s/i) != -1 || wordSearch.search( /\WNOT\s/i) != -1 || wordSearch.search( /\sNOT\W/i) != -1 || wordSearch.search( /^NOT\W/i) != -1 || wordSearch.search(/^NOT\s/i) != -1 || wordSearch.search(/\WNOT$/i) != -1 || wordSearch.search( /\sNOT$/i) != -1) {
					alert('The Word Search cannot contain the reserved word "NOT". On the following page, you can modify the word search criteria by using the logical operators, "AND","NOT", and "OR".');
					return false;
				}
				if (wordSearch.search( /\sOR\s/i) != -1 || wordSearch.search( /\WOR\s/i) != -1 || wordSearch.search( /\sOR\W/i) != -1 || wordSearch.search( /^OR\W/i) != -1 || wordSearch.search(/^OR\s/i) != -1 || wordSearch.search(/\WOR$/i) != -1 || wordSearch.search( /\sOR$/i) != -1) {
					alert('The Word Search cannot contain the reserved word "OR". On the following page, you can modify the word search criteria by using the logical operators, "AND","NOT", and "OR".');
					return false;
				}
				var expressionArr = wordSearch.split(",")
				for(var i = 0; i < expressionArr.length; i++) {
					expressionPiece = expressionArr[i];
					if (expressionPiece.search(/^\*/) != -1) {
						alert('The Word Search cannot begin with an asterisk (*).')
						return false;
					}
					expressionPiece = expressionPiece.replace(leadingwhitespaces,"");
					expressionPiece = expressionPiece.replace(trailingwhitespaces,"");
					expressionPiece = expressionPiece.replace(multiwhitespaces," ");
					expressionPiece = expressionPiece.replace(singlewhitespace,",");
					if (expressionPiece == "") {
						alert('The Word Search cannot contain an empty word or phrase.');
						return false;
					}
				}
			}
		}
		// End of Else Clause
		}
	return true;
	}

function checkDate(Year,Month,Date,BeginYear,EndYear) {
	var errMessage = "";
	if (Year < BeginYear || Year > EndYear) {
		errMessage = Year + " is not a valid year.  The acceptable range of years is from " + BeginYear + " to " + EndYear + ".";
		return errMessage;
	}
	if (Date == 0) {
		errMessage = Date + " is not a valid date.";
		return errMessage;
	}
	if (Month < 1 || Month > 12) {
		errMessage = Month + " is not a valid month.";
		return errMessage;
	}
	if ((Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12) && (Date > 31)) {
		errMessage = Date + " is not a valid date for month " + Month + ".";
		return errMessage;
	}
	if ((Month == 4 || Month == 6 || Month == 9 || Month == 11) && (Date > 30)) {
		errMessage = Date + " is not a valid date for month " + Month + ".";
		return errMessage;
	}
	if (Month == 2) {
		if ((Date > 28) && (Year%4 != 0)) {
			errMessage = Date + " is not a valid date for month " + Month + ".";
			return errMessage;
		}
		if ((Date > 29) && (Year%4 == 0)) {
			errMessage = Date + " is not a valid date for month " + Month + ".";
			return errMessage;
		}
	}
	return errMessage;
}

// Returns correct year for any year after 1000
function getFullYear(tempDate) {
	var tempYear = tempDate.getYear();
	if (tempYear < 1000) tempYear += 1900;
	return tempYear;
}

function thisDate() {
	var currDate = new Date();
	return currDate;
}
function setThisDate (tempYear,tempMonth,tempDate) {
	var tempDate = new Date(tempYear,tempMonth-1,tempDate);
	return tempDate;
}
function sendThis(item,form) {
	if (item.value == 'Display / Submit Query') {
		if(chkEntry(form) == true) {
			form.submit();
			item.value = 'Please wait.';
			return true;
		}
	}
	return false;
}
