//acceptable characters are A or Z followed by 8 digits
var allpidRegExp = /^[A|Z]\d{8}$/g
//acceptable characters are A followed by 8 digits
var apidRegExp = /^[A]\d{8}$/g
//acceptable characters are Z followed by 8 digits
var zpidRegExp = /^[Z]\d{8}$/g
//acceptable characters are a-Z, spaces, commas, and apostrophes
var nameRegExp = /^([a-zA-Z,\s\.\-\']+)$/g
//acceptable characters are a-Z, spaces, commas, and apostrophes
var searchRegExp = /^([a-zA-Z,%\s\']+)$/g
//acceptable characrers are numbers, a-Z, commas, hyphens, spaces, periods, and apostrophes
var addressRegExp = /^([0-9a-zA-Z,\#\-\/\s\.\']+)$/g
//acceptable charcters are numbers, hyphens, spaces, parentheses, and periods
var phoneRegExp = /^([0-9\-\s\.()]+)$/g
//acceptable characters are valid email addresses
var emailRegExp = /^([a-zA-Z0-9_\-\.\']+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/g
//acceptable characters are numbers and periods
var gpaRegExp = /^[0-5]\.[\d]+$/g
//acceptable characters are 0-9 and forward slashes in the form of MM/DD/YYYY 
var dateRegExp = /^(0?[1-9]|1[012])[\/](0?[1-9]|[12][0-9]|3[01])[\/](19|20)\d\d$/g
//acceptable characters are 0-9 and forward slashes in the form of MM/YYYY 
var shortDateRegExp = /^(0?[1-9]|1[012])[\/](19|20)\d\d$/g
//acceptable characters are integers
var numberRegExp = /^([0-9]+)$/g
//acceptable characters common to essays (incl. smart quotes), but not to hackers
var essayRegExp = /^([\w,;:#!%@()…"\$\-\—\–\?\/\s\.\‘\~\'\’\“\”]+)$/g
//acceptable characters are $, 0-9, commas, and periods
var moneyRegExp = /^[\$]*[0-9]+[,|\.]*[0-9]*[\.]*[0-9]*$/g
//acceptable characters are numbers or a-Z
var receiptRegExp = /^([0-9a-zA-Z]+)$/g

function isDate(dateStr) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?
	
    if (matchArray == null) {
        // alert("Please enter date in one of the following formats:\nmm/dd/yy, mm/dd/yyyy, mm-dd-yyyy, or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[4];

    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
       //alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("Month "+month+" doesn't have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
           //alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}


var formOkay = 0
var errorText = ""

function runRegExp(oForm, sRegExp, sValue, sDisplayText, bRequired, iLength, iMinimum) {
	var fieldOkay = 0
//runRegExp(form to test, regular expression to test against, value to be tested, display name of field for error message, required field flag)
	if (sValue == "" && bRequired == true) {
		errorText += "Please enter a response for the " + sDisplayText + " field.<br />"
		fieldOkay += 1;
	}
	else if (iLength !== "" && sValue.length > iLength) {
		errorText += "Please enter a response of length " + iLength + " characters or less for the " + sDisplayText + " field.<br />"
		fieldOkay += 1;
	}
	else if (sValue !== "" && sRegExp == essayRegExp && sValue.match(sRegExp) == null) {
		errorText += "Please enter a valid value for the " + sDisplayText + " field. Valid characters are any letter or number, commas, underscores, semicolons, colons, pound signs, exclamation points, percentage signs, open and closing parentheses, quotation marks, dollar signs, hyphens, question marks, forward slashes, spaces, tabs, carriage returns, apostrophes, @ signs, m-dashes, n-dashes, and periods."
		fieldOkay += 1;
	}
	else if (sValue !== "" && sRegExp !== null && sValue.match(sRegExp) == null) {
		errorText += "Please enter a valid value for the " + sDisplayText + " field.<br />"
		fieldOkay += 1;
 	}
	else if (iMinimum) {
		if (sValue.length < iMinimum) {
			errorText += "Please enter a response of at least " + iMinimum + " characters for the " + sDisplayText + " field.<br />"
			fieldOkay += 1;
		}
	} 
	
	if (fieldOkay !== 0) {
		formOkay +=1;
	}
}

function validateDate(oForm, sValue, bRequired, sDisplayText) {

	if (sValue == "" && bRequired == true) {
		errorText += "Please enter a response for the " + sDisplayText + " field.<br />"
		formOkay += 1;
	}
	else if (bRequired == false) {
		//do nothing
	}
	else if (!isDate(sValue)) {
		errorText += "Please enter a valid date for the " + sDisplayText + " field.<br />"
		formOkay += 1;
	}
}

function validateCheckboxOrRadio(oForm, oCheckboxGroup, sDisplayText) {
	var iCheckboxesChecked = 0;
	if (oCheckboxGroup.length == undefined) {
		if (oCheckboxGroup.checked) {
			iCheckboxesChecked += 1;
		}	
	}
	else {		
		for (i = 0; i < oCheckboxGroup.length; i++) {
			if (oCheckboxGroup[i].checked) {
				iCheckboxesChecked += 1;
			}
		}
	}
	
	if (iCheckboxesChecked == 0) {
		errorText += "Please check a box or radio button in the " + sDisplayText + " field.<br />"
		formOkay += 1;
	}
}

function validateSelect(oForm, oSelect, sDisplayText) {
//must have a "select one" option at the beginning of the options list
//	if (oSelect.selectedIndex == 0) { 
	if (oSelect.options[oSelect.selectedIndex].value == "") {
		errorText += "Please select an option for the the " + sDisplayText + " field.<br />"
		formOkay += 1;
	}
}

function notSameValueSelects(oForm, oSelect1, sDisplayText1, oSelect2, sDisplayText2) {
	if (oSelect1.options[oSelect1.selectedIndex].value !== "not applicable" && oSelect2.options[oSelect2.selectedIndex].value !== "not applicable") {
		if (oSelect1.options[oSelect1.selectedIndex].value == oSelect2.options[oSelect2.selectedIndex].value) {
			errorText += "The options selected for the " + sDisplayText1 + " and " + sDisplayText2  + " fields must be different.<br />"
			formOkay += 1;
		}
	}
}

function sameValueTwoTextboxes(oForm, oFirstField, sFirstDisplayText, oSecondField, sSecondDisplayText) {
	if (oFirstField.value !== oSecondField.value) {
		errorText += "The " + sFirstDisplayText + " field and the " + sSecondDisplayText + " field must have identical values.<br />"
		formOkay += 1;
	}
}

function specificValueRadio(oForm, oRadioGroup, sMatchToText, sDisplayText, sMatchToDisplayText) {
	var bProperValueSelected = false
	for (i = 0; i < oRadioGroup.length; i++) {
		if (oRadioGroup[i].checked) {
			if (oRadioGroup[i].value == sMatchToText) {
				bProperValueSelected = true
				break;
			}
		}
	}
	
	if (!bProperValueSelected) {
		errorText += "The " + sDisplayText + " field must be set to " + sMatchToDisplayText + ".<br />"
		formOkay += 1;
	}
}

function endValidation(oForm) {
	if (formOkay == 0) {
		oForm.submit();
	}
	else {
		//alert(errorText);
		//for Firefox
		errorText += "<br />"
		document.getElementById("sErrorMessage").innerHTML = errorText;
		//scroll back to top of page
		scroll(0,0);
		//reset variables
		errorText = ""
		formOkay = 0;
		
	}	
}

	function updateCount(theText, theCounter) {
			theCounter.value=theText.value.length;
}



