//
//	This utility is a forms inspector copied from the Wrox publication "Professional
//	JavaScript 2nd Edition"
//
function formLook() {
	var output = "";
	for (var i=0; i < document.forms.length; i++) {
		for(var j=0; j < document.forms[i].elements.length; j++) {
			output += "Form " + i + " -- # " + j + " -- Type: "
			+ document.forms[i].elements[j].type + ": "
			+ document.forms[i].elements[j].name + ": "
			+ document.forms[i].elements[j].value + "<br>"
		}
	};
	var newWin = window.open("","newWin","width=850,height=350");
	with(newWin.document) {
		open()
		write(output);
		close();
	}
}
//
//	Create Pop-Up window collecting the URL passed
//
function doPopUp(url) {
	window.open(url,"","scrollbars,resizable,width=700,height=500")
}
//
//	Obtain the value of which radio button was selected given the radiogroup.
//
function getSelectedRadioValue(radioGroup) {
	var selectedRadioValue = "";
	var radIndex;

	for (radIndex = 0; radIndex < radioGroup.length; radIndex++) {
		if (radioGroup[radIndex].checked) {
			selectedRadioValue = radioGroup[radIndex].value;
			break;
		}
	}
	return selectedRadioValue;
}
//
//	Strip begining and ending spaces from the string passed and return the value.
//
function stripBlanks(theString) {
	var theStringLength = -1;
	var i = -1;
	var begNonBlank = -1;
	var endNonBlank = -1;

	theStringLength = theString.length;

	if (theStringLength == 0) {
		return theString
	}

	for (i = 0; i <= theStringLength - 1; i++) {
		if (theString.charAt(i) == " ") {
			continue
		}
		else {
			begNonBlank = i;
			break
		}
	}

	if (begNonBlank == -1) {
		theString = ""
		return theString
	}

	for (i = theStringLength - 1; i >= 0; i--) {
		if (theString.charAt(i) == " ") {
			continue
		}
		else {
			endNonBlank = i
			theString = theString.substring(begNonBlank, endNonBlank + 1);
			break;
		}
	}
	return theString
}
//
//	Validate a gregorian date in the form mm/dd/yy or mm/dd/yyyy
//
function validateDate(theDate, yearSize) {
	var theMonth;
	var theDay;
	var theYear;
	var dateArray = new Array()
	var reDate = /^\d?\d\/\d?\d\/\d\d|\d\d\d\d$/
	var reMonth = /^\d\d?$/;
	var reDay = /^\d?\d$/;
	var reYearLong = /^\d\d\d\d$/;
	var reYearShort = /^\d\d$/;

	if (yearSize == "Short" || yearSize == "Long") {
	}
	else {
		alert("Syntax error. yearSize must be equal to the literal string\n \"Long\" or \"Short\" (case sensitive).");
		return false;
	}

	if (reDate.test(theDate)) {
	}
	else {
		if (yearSize == "Short") {
			alert("Invalid date format. Must be MM/DD/YY\nwhere 'MM' is month, 'DD' is day and 'YY' is  the 2 digit year, respectively.");
			return false;
		}
		if (yearSize == "Long") {
			alert("Invalid date format. Must be MM/DD/YYYY\nwhere 'MM' is month, 'DD' is day and 'YY' is the 4 digit year, respectively.");
			return false;
		}
	}

	if (yearSize == "Short") {
		if (theDate.length >= 6 && theDate.length <= 8) {
		}
		else {
			alert("Invalid date format. Must be MM/DD/YY\nwhere 'MM' is month, 'DD' is day and 'YY' is year, respectively.");
			return false;
		}
	}

	if (yearSize == "Long") {
		if (theDate.length >= 8 && theDate.length <= 10) {
		}
		else {
			alert("Invalid date format. Must be MM/DD/YYYY\nwhere 'MM' is month, 'DD' is day and 'YYYY' is the 4 digit year.");
			return false;
		}
	}

	dateArray = theDate.split("/");
	theMonth = dateArray[0];
	theDay = dateArray[1];
	theYear = dateArray[2];

	if (yearSize == "Short") {
		if (theYear.length == 2) {
		}
		else {
			alert("Year must be 2 digits.");
			return false;
		}

		if (reYearShort.test(theYear)) {
		}
		else {
			alert("Year must be 2 digits.");
			return false;
		}
	}

	if (yearSize == "Long") {
		if (theYear.length == 4) {
		}
		else {
			alert("Year must be 4 digits.");
			return false;
		}

		if (reYearLong.test(theYear)) {
		}
		else {
			alert("Year must be 4 digits.");
			return false;
		}
	}


	theMonth = theMonth - 0;
	theDay = theDay - 0;
	theYear = theYear - 0;

	if (reMonth.test(theMonth)) {
	}
	else {
		alert("Month must be numeric");
		return false;
	}
	if (reDay.test(theDay)) {
	}
	else {
		alert("Day must be numeric");
		return false;
	}

	if (theMonth >= 1 && theMonth <= 12) {
	}
	else {
		alert("Month must be between 1 and 12, inclusive.");
		return false;
	}
	if (theMonth == 1 ||
	   theMonth == 3  ||
	   theMonth == 5  ||
	   theMonth == 7  ||
	   theMonth == 8  ||
	   theMonth == 10 ||
	   theMonth == 12) {
		if (theDay >= 1 && theDay <= 31) {
		}
		else {
			alert("The day must be between 1 and 31, inclusive, for the month entered.");
			return false;
		}
	}

	if (theMonth == 4 ||
	   theMonth == 6 ||
	   theMonth == 9 ||
	   theMonth == 11) {
		if (theDay >= 1 && theDay <= 30) {
		}
		else {
			alert("The day must be between 1 and 30, inclusive, for the month entered.");
			return false;
		}
	}

	if (theMonth == 2 && theYear % 4 == 0) {
		if (theDay >= 1 && theDay <= 29) {
		}
		else {
			alert("The month entered is February and it is a leap year, therefore,\nthe day must be between 1 and 29, inclusive.");
			return false;
		}
	}
	if (theMonth == 2 && theYear % 4 != 0) {
		if (theDay >= 1 && theDay <= 28) {
		}
		else {
			alert("The month entered is February and it is not a leap year, therefore,\nthe day must be between 1 and 28, inclusive.");
			return false;
		}
	}
	return true;
}
