/*
-------------------------------------------------------
DETERMINES IF A DATE IS VALID.  RETURNS A BOOLEAN.
-------------------------------------------------------

---------INSTRUCTIONS----------------------------------
Include this ValidateDate.js file.

Validate Date:
	1.  Call validateDate(strDate).
		
HINT: FUNCTION REQUIRES MONTH, DATE, A FOUR DIGIT YEAR
	  IN THAT ORDER SEPERATED BY A NON NUMERIC DELIMETER.
	  
-------------------------------------------------------
*/

function validateDate(strDate) {
	var newDate;
	var dateChar;
	var dateDelimeter;
	var dateSplit;
	var validDate = false;
	
	//If string is NULL or EMPTY date is valid
	if (!strDate || strDate.length < 1) {
		validDate = true;
	}
	else {
		//Get Delimeter
		for (var i=0; i<strDate.length; i++) {
			dateChar = strDate.substring(i,(i+1));
			if (isNaN(dateChar)) {
				dateDelimeter = dateChar;
				break;
			}
		}
	}
	
	if (dateDelimeter) {
		dateSplit = strDate.split(dateDelimeter);
		//If a MONTH, DATE, and YEAR are provided
		if (dateSplit.length == 3) {
			validDate = true;
			
			var strMonth = dateSplit[0];
			var strDay = dateSplit[1];
			var strYear = dateSplit[2];
			
			if (parseFloat(strMonth) < 1 || parseFloat(strMonth) > 12) {
				validDate = false;
			}
			else if (strYear.length != 4 || parseFloat(strYear) < 1800) {
				validDate = false;
			}
			else {
				var daysInMonth;
				
				//Determine number of days in month
				switch (parseFloat(strMonth)) {
					case 1:
					case 3:
					case 5:
					case 7:
					case 8:
					case 10:
					case 12:
						daysInMonth = 31;
						break;
					case 4:
					case 6:
					case 9:
					case 11:
						daysInMonth = 30;
						break;
					case 2:
						if ((parseFloat(strYear)/4) == Math.round(parseFloat(strYear)/4)) {
							daysInMonth = 29;
						}
						else {
							daysInMonth = 28;
						}
						break;
					default:
						break;
				}
				
				if (strDay > daysInMonth || strDay < 1) {
					validDate = false;
				}
				//window.alert(daysInMonth);
			}
		}
	}
	
	if (!validDate) {
		window.alert("INVALID DATE\n\nThe date you have entered is not a valid date or is not in the correct format [MM/DD/YYYY].\nPlease correct the date before continuing.");

	}
	
	return validDate;
}

function validateDateRange(strDate,minDate,maxDate) {
	var validDate = validateDate(strDate);
	var msgInvalidDate = "";
	var today = new Date();
	today = (today.getMonth() + 1) + "/" + today.getDate() + "/" + getFourDigitYear(today);
	
	if (minDate && minDate.toLowerCase() == "today") {
		minDate = today;
	}
	if (maxDate && maxDate.toLowerCase() == "today") {
		maxDate = today;
	}
	
	if (!validateDate(minDate)) {
		minDate = null;
	}
	if (!validateDate(maxDate)) {
		maxDate = null;
	}
	
	if (validDate) {
		if (minDate && validDate) {
			validDate = validateDateMin(strDate,minDate);
			if (!validDate) {
				msgInvalidDate = "INVALID DATE\n\nThe date you have entered is before " + minDate + ".\nPlease correct the date before continuing.";
			}
		}
		
		if (maxDate && validDate) {
			validDate = validateDateMax(strDate,maxDate);
			if (!validDate) {
				msgInvalidDate = "INVALID DATE\n\nThe date you have entered is after " + maxDate + ".\nPlease correct the date before continuing.";
			}
		}
		
		if (!validDate) {
			window.alert(msgInvalidDate);
		}
	}
	
	return validDate;
}

function validateDateMin(strDate,minDate) {
	var validDate = true;
	strDate = createDate(strDate);
	minDate = createDate(minDate);
	
	if (strDate < minDate) {
		validDate = false;
	}
	
	return validDate;
}

function validateDateMax(strDate,maxDate) {
	var validDate = true;
	strDate = createDate(strDate);
	maxDate = createDate(maxDate);
	
	if (strDate > maxDate) {
		validDate = false;
	}
	
	return validDate;
}

function createDate(strDate) {
	try {
		var splitDate = strDate.split("/");
		var strMonth = splitDate[0];
		var strDay = splitDate[1];
		var strYear = splitDate[2];
		
		strDate = new Date(strYear,(strMonth-1),strDay);
	}
	catch(e) {
		//strDate = new Date();
		strDate = false;
	}
	
	if (isNaN(strDate)) {
		strDate = false;
	}
	
	return strDate;
}

function getFourDigitYear(dateVal) {
	var iYear = dateVal.getYear();
	if (iYear < 1500) {
		iYear += 1900;
	}
	return iYear;
}

/*
-------------------------------------------------------
END OF ValidateDate.js
-------------------------------------------------------
*/
