//  Template Name:  SCR_DateFunctions.js
//  Purpose:		Common functions for formatting, validating and comparing date fields.
//					Will do simple validation of dates, but will also allow you to pass in one format,
//					validate the date, and pass it back in a different format.
//  Author:			Rick Ibanez (The Seva Group)
//  Calling Instructions:
//  				1.  Include the script tag where SRC will be the fully qualified
//                      path and file name. 					
//                     <SCRIPT LANGUAGE=JAVASCRIPT SRC="/Common/Scripts/SCR_DateFunctions.js"></SCRIPT>
//
//  ===================================================================================
//  Function:		ValidateDate
//  Purpose:		Validates a given date.  Adds the current year if no year is given.
//  Called From:	YourModule, ValidateBetweenDates()
//  Calls:			none 				
//  Returns:		false if 
//  				1.  There are incorrect number of components found in date. 
//                  2.  The length of year is invalid i.e. not 2 or 4 digits.
//					3.  The format is not MM/DD/YY or MM/DD/YYYY or MM/DD format
//					4.  The date is not valid for that month e.g. 1/32
//				    TRUE if otherwise
//  Required Inputs:
//  				objInputDate - Any valid date object/field.
//					Pass an object, not a value.
//  Optional Inputs: strDelimiterOverride - can force a specific delimiter between MM/DD/YY.
//									Default if not specified will be whatever delimiter
//									is found in the entered date.
//					strDateFormatInput - string indicating the format of the date being passed in
//									Note that this routine does allow a user to enter in a format 
//									such as mm/dd and it will automatically append the year.  
//									See the format validationfunction for a list of valid date formats.
//					strDateFormatOutput - string indicating the format of the date to be passed back.
//										  If not provided, will default to the same as the input date
//										  format.
//					blnRequired - 0 is default.  0 indicates that blanks are okay.  1 indicates that
//								  a value is required.
//  Change Log:
//  Date			Name				Description
//  ----			----				-----------
//  08-03-2000		R. Ibanez			Original Creation
//  ===================================================================================
function ValidateDate(objInputDate, strDateFormatInput, strDelimiterOverride, blnRequired, strDateFormatOutput){

	if (!CheckRequiredField(objInputDate, blnRequired)) {
		objInputDate.focus()
		return false
	}
	
	if (objInputDate.value != '') { // only validate the date if it contains a value

		// Creating date object for use in various functions
		var objDateStructure = new DateStructure();

		objDateStructure.strDateValue = objInputDate.value
		objDateStructure.strDateFormatInput = strDateFormatInput
		objDateStructure.strDateFormatOutput = strDateFormatOutput
		objDateStructure.strDelimiterOverride = strDelimiterOverride

		// set the delimiter based on input data and possibly passed override.
		if (!DetermineDateDelimiter(objDateStructure)) { 
			objInputDate.focus()
			return false
		}

		// Confirming that a valid date format was received or setting the default format and
		//   confirmation of component numbers and splitting of date components
		if (!HandleDateFormat(objDateStructure)) { 
			objInputDate.focus()
			return false
		}

		// Format month, day, year (as required)
		if (!ValidateIndividualComponents(objDateStructure)) {
			if (objInputDate) {objInputDate.focus()}
			return false
		}

		if (objDateStructure.blnValidateWhole == true) { // validate the date as a whole systematically.
			strTempDateValue = objDateStructure.intOutputMonthValue + "/" + objDateStructure.intOutputDayValue + "/" + objDateStructure.intOutputYearValue
	        var strChkDate=new Date(Date.parse(strTempDateValue))
    	    var intCmpDate=(strChkDate.getMonth()+1) + "/" + (strChkDate.getDate()) + "/" + (strChkDate.getFullYear())
        	var intDate2=(Math.abs(objDateStructure.intOutputMonthValue)) + "/" + (Math.abs(objDateStructure.intOutputDayValue)) + "/" + (Math.abs(objDateStructure.intOutputYearValue))

	        if (intDate2 != intCmpDate){
	                alert("You've entered an invalid date or date format.  \rExpected format is: " + objDateStructure.strDateFormatInput + "\rThis may also be due to an invalid date such as 02/30. \rInvalid characters may also cause this error.")
					objInputDate.focus()
	                return false
	        }
	        else {
	        	if (intCmpDate=="NaN/NaN/NaN"){
	                alert("You've entered an invalid date or date format.  \rExpected format is: " + objDateStructure.strDateFormatInput + "\rThis may also be due to an invalid date such as 02/30. \rInvalid characters may also cause this error.")
					objInputDate.focus()
	                return false
	            }
	        }
		}

		objInputDate.value = BuildResultDate(objDateStructure)

	} // end of 'if value contained in date field.'
	
	return true
}

//  ==================================================================================
//  Function:		CheckRequiredField
//  Purpose:		Sees whether a date field is required or not, and if so confirms that
//					a value was received.
//  Returns:		TRUE if acceptable, FALSE if not acceptable
//	Called By:		ValidateDate
//  Required Inputs: blnRequired - flag indicating if date is required (TRUE) or not 
//									required (false, default)
//					objInputDate - object of the actual date being passed to the routine.
//  Optional Inputs: none
//  Change Log:
//  Date			Name				Description
//  ----			----				-----------
//  08-09-2000		B.Bailey			Original Creation
//  ==================================================================================
function CheckRequiredField(objInputDate, blnRequired) {

	// Check whether the date is required or not and if so, confirm that a value was received.
	if (!blnRequired) {// not passed in.  Use default value
		blnRequired = false
	}
	else {
		if (blnRequired != true && blnRequired != false) {
			alert("PROGRAMMING ERROR:  Invalid required flag passed to program.\rContact System Administrator.\rValue was: " + blnRequired)
			return false
		}
	}
	
	if (blnRequired == true && objInputDate.value == '') {
		alert("Required date field contains no data.  Please enter and try again.")
		return false
	}
	
	return true
}

//  ==================================================================================
//  Function:		DateStructure
//  Purpose:		Initializes date structure object used in date validation routine
//  Returns:		TRUE if acceptable, FALSE if not acceptable
//	Called By:		ValidateDate
//  Required Inputs: objDateStructure
//  Optional Inputs: none
//  Change Log:
//  Date			Name				Description
//  ----			----				-----------
//  08-09-2000		B.Bailey			Original Creation
//  ==================================================================================
function DateStructure(objDateStructure) {

	this.strDateValue = ''
	this.strDateFormatInput = ''
	this.strDateFormatOutput = ''
	this.strDelimiter = ''
	this.strDelimiterOverride = ''
	this.intInputComponents = 0
	this.intInputComponent1 = 0
	this.intInputComponent2 = 0
	this.intInputComponent3 = 0
	this.intOutputComponents = 0
	this.intOutputYearLength = 0
	this.intOutputMonthLength = 0
	this.intOutputDayLength = 0
	this.intOutputYearNode = 0
	this.intOutputMonthNode = 0
	this.intOutputDayNode = 0
	this.intInputYearNode = 0
	this.intInputMonthNode = 0
	this.intInputDayNode = 0
	this.intOutputYearValue = 0
	this.intOutputMonthValue = 0
	this.intOutputDayValue = 0
	this.blnValidateWhole = true
	
	return true
}

//  ==================================================================================
//  Function:		DetermineDateDelimiter
//  Purpose:		Determines what the output delimiter should be based on the input date
//					and the optionally passed in override value.
//  Returns:		TRUE pretty much no matter what
//	Called By:		ValidateDate
//  Required Inputs: objDateStructure
//  Optional Inputs: none
//  Change Log:
//  Date			Name				Description
//  ----			----				-----------
//  08-09-2000		B.Bailey			Original Creation
//  ==================================================================================
function DetermineDateDelimiter(objDateStructure) {

	// determine date delimiter in the input date (regardless of the preferred delimiter)
       if (objDateStructure.strDateValue.indexOf("-")!=-1) {
            var strDate = objDateStructure.strDateValue.split("-")
			var strDelim = "-"
       }
       else {
            var strDate = objDateStructure.strDateValue.split("/")
			var strDelim = "/"
       }

	// Change the output delimiter if the program been passed a "preferred" delimiter.
	if (!objDateStructure.strDelimiterOverride) {
		// field is not defined
	}
	else {
		if (objDateStructure.strDelimiterOverride != '') {
			strDelim = objDateStructure.strDelimiterOverride
		}
	}

	objDateStructure.strDelimiter = strDelim;
	objDateStructure.intInputComponents = strDate.length;
	
	// Populate input component values
	for (idxDate = 0; idxDate < strDate.length; idxDate++) {
		if (eval("objDateStructure.intInputComponent" + (idxDate + 1) + " = strDate[" + idxDate + "]")) { // no action
		}
		else {
			alert("PROGRAMMING ERROR:  Not able to determine input date components. \rPlease notify system administrator")
			return false
		}
	}

	return true
}

//  ==================================================================================
//  Function:		ValidateDateFormat
//  Purpose:		Validates the input and output date formats and sets appropriate variables.
//  Returns:		TRUE if acceptable, FALSE if not acceptable
//	Called By:		ValidateDate
//  Calls:			ProcessDateFormat
//  Required Inputs: objDateStructure
//  Optional Inputs: none
//  Change Log:
//  Date			Name				Description
//  ----			----				-----------
//  08-09-2000		B.Bailey			Original Creation
//  ==================================================================================
function HandleDateFormat(objDateStructure) {

	if (!ProcessDateFormat(objDateStructure, 'I')) {
		return false
	}

	if (!ProcessDateFormat(objDateStructure, 'O')) {
		return false
	}
	
	return true
}

//  ==================================================================================
//  Function:		ProcessDateFormat
//  Purpose:		Validates that a received date format is acceptable.
//  Returns:		TRUE if acceptable, FALSE if not acceptable
//	Called By:		HandleDateFormat
//  Required Inputs: objDateStructure, flProcessType (I = input, O = output)
//  Optional Inputs: none
//  Change Log:
//  Date			Name				Description
//  ----			----				-----------
//  08-09-2000		B.Bailey			Original Creation
//  ==================================================================================
function ProcessDateFormat(objDateStructure, flProcessType) {

	if (flProcessType == 'O') {
		blnOutput = true
		blnInput = false
	}
	else {
		blnOutput = false
		blnInput = true
	}
	
	// Determine what date format to use based on default or passed in format.
	strDateFormatDefault = "M/D/YYYY"	
	
	if (blnInput == true) {
		if (!objDateStructure.strDateFormatInput) {
			objDateStructure.strDateFormatInput = strDateFormatDefault
		}
		else {
			if (objDateStructure.strDateFormatInput != '') {
				objDateStructure.strDateFormatInput = objDateStructure.strDateFormatInput.toUpperCase()
			}
			else {
				objDateStructure.strDateFormatInput = strDateFormatDefault
			}
		}	
	}
	else {
		if (!objDateStructure.strDateFormatOutput) {
			objDateStructure.strDateFormatOutput = objDateStructure.strDateFormatInput
		}
		else {
			if (objDateStructure.strDateFormatOutput != '') {
				objDateStructure.strDateFormatOutput = objDateStructure.strDateFormatOutput.toUpperCase()
			}
			else {
				objDateStructure.strDateFormatOutput = objDateStructure.strDateFormatInput
			}
		}
	}

	if (blnInput == true) {
   		strComponentCountError = "Incorrect number of components found in date.  Format expected was: " + objDateStructure.strDateFormatInput + ". \rFound " + objDateStructure.intInputComponents + " components."
		strDateFormat = objDateStructure.strDateFormatInput
	}
	else {
		strDateFormat = objDateStructure.strDateFormatOutput
	}
	
	switch(strDateFormat) {
		case "M/D/YYYY":
			if (blnInput == true) {
		        if (objDateStructure.intInputComponents != 3 && objDateStructure.intInputComponents != 2) {
					alert(strComponentCountError)
					return false
				}
				objDateStructure.intInputYearNode = 3
				objDateStructure.intInputMonthNode = 1
				objDateStructure.intInputDayNode = 2
			}
			else {
				objDateStructure.intOutputYearLength = 4
				objDateStructure.intOutputMonthLength = 1
				objDateStructure.intOutputDayLength = 1
				objDateStructure.intOutputYearNode = 3
				objDateStructure.intOutputMonthNode = 1
				objDateStructure.intOutputDayNode = 2
				objDateStructure.intOutputComponents = 3
			}
			break;
		case "MM/DD/YYYY":
			if (blnInput == true) {
		        if (objDateStructure.intInputComponents != 3 && objDateStructure.intInputComponents != 2) {
					alert(strComponentCountError)
					return false
				}
				objDateStructure.intInputYearNode = 3
				objDateStructure.intInputMonthNode = 1
				objDateStructure.intInputDayNode = 2
			}
			else {
				objDateStructure.intOutputYearLength = 4
				objDateStructure.intOutputMonthLength = 2
				objDateStructure.intOutputDayLength = 2
				objDateStructure.intOutputYearNode = 3
				objDateStructure.intOutputMonthNode = 1
				objDateStructure.intOutputDayNode = 2
				objDateStructure.intOutputComponents = 3
			}
			break;
		case "MM/DD":
			if (blnInput == true) {
		        if (objDateStructure.intInputComponents != 2) {
					alert(strComponentCountError)
					return false
				}
				objDateStructure.intInputYearNode = 0
				objDateStructure.intInputMonthNode = 1
				objDateStructure.intInputDayNode = 2
			}
			else {
				objDateStructure.intOutputYearLength = 0
				objDateStructure.intOutputMonthLength = 2
				objDateStructure.intOutputDayLength = 2
				objDateStructure.intOutputYearNode = 0
				objDateStructure.intOutputMonthNode = 1
				objDateStructure.intOutputDayNode = 2
				objDateStructure.intOutputComponents = 2
			}
			break;
		case "MM/DD/YY":
			if (blnInput == true) {
		        if (objDateStructure.intInputComponents != 3 && objDateStructure.intInputComponents != 2) {
					alert(strComponentCountError)
					return false
				}
				objDateStructure.intInputYearNode = 3
				objDateStructure.intInputMonthNode = 1
				objDateStructure.intInputDayNode = 2
			}
			else {
				objDateStructure.intOutputYearLength = 2
				objDateStructure.intOutputMonthLength = 2
				objDateStructure.intOutputDayLength = 2
				objDateStructure.intOutputYearNode = 3
				objDateStructure.intOutputMonthNode = 1
				objDateStructure.intOutputDayNode = 2
				objDateStructure.intOutputComponents = 3
			}
			break;
		case "MM/YY":
			if (blnInput == true) {
		        if (objDateStructure.intInputComponents != 2) {
					alert(strComponentCountError)
					return false
				}
				objDateStructure.intInputYearNode = 2
				objDateStructure.intInputMonthNode = 1
				objDateStructure.intInputDayNode = 0
			}
			else {
				objDateStructure.intOutputYearLength = 2
				objDateStructure.intOutputMonthLength = 2
				objDateStructure.intOutputDayLength = 0
				objDateStructure.intOutputYearNode = 2
				objDateStructure.intOutputMonthNode = 1
				objDateStructure.intOutputDayNode = 0
				objDateStructure.intOutputComponents = 2
				objDateStructure.blnValidateWhole = false
			}
			break;
		case "MM/YYYY":
			if (blnInput == true) {
		        if (objDateStructure.intInputComponents != 2) {
					alert(strComponentCountError)
					return false
				}
				objDateStructure.intInputYearNode = 2
				objDateStructure.intInputMonthNode = 1
				objDateStructure.intInputDayNode = 0
			}
			else {
				objDateStructure.intOutputYearLength = 4
				objDateStructure.intOutputMonthLength = 2
				objDateStructure.intOutputDayLength = 0
				objDateStructure.intOutputYearNode = 2
				objDateStructure.intOutputMonthNode = 1
				objDateStructure.intOutputDayNode = 0
				objDateStructure.intOutputComponents = 2
				objDateStructure.blnValidateWhole = false
			}
			break;
		case "YY/MM/DD":
			if (blnInput == true) {
		        if (objDateStructure.intInputComponents != 3) {
					alert(strComponentCountError)
					return false
				}
				objDateStructure.intInputYearNode = 1
				objDateStructure.intInputMonthNode = 2
				objDateStructure.intInputDayNode = 3
			}
			else {
				objDateStructure.intOutputYearLength = 2
				objDateStructure.intOutputMonthLength = 2
				objDateStructure.intOutputDayLength = 2
				objDateStructure.intOutputYearNode = 1
				objDateStructure.intOutputMonthNode = 2
				objDateStructure.intOutputDayNode = 3
				objDateStructure.intOutputComponents = 3
			}
			break;
		case "YYYY/MM/DD":
			if (blnInput == true) {
		        if (objDateStructure.intInputComponents != 3) {
					alert(strComponentCountError)
					return false
				}
				objDateStructure.intInputYearNode = 1
				objDateStructure.intInputMonthNode = 2
				objDateStructure.intInputDayNode = 3
			}
			else {
				objDateStructure.intOutputYearLength = 4
				objDateStructure.intOutputMonthLength = 2
				objDateStructure.intOutputDayLength = 2
				objDateStructure.intOutputYearNode = 1
				objDateStructure.intOutputMonthNode = 2
				objDateStructure.intOutputDayNode = 3
				objDateStructure.intOutputComponents = 3
			}
			break;
		case "DD/MM/YY":
			if (blnInput == true) {
		        if (objDateStructure.intInputComponents != 3) {
					alert(strComponentCountError)
					return false
				}
				objDateStructure.intInputYearNode = 3
				objDateStructure.intInputMonthNode = 2
				objDateStructure.intInputDayNode = 1
			}
			else {
				objDateStructure.intOutputYearLength = 2
				objDateStructure.intOutputMonthLength = 2
				objDateStructure.intOutputDayLength = 2
				objDateStructure.intOutputYearNode = 3
				objDateStructure.intOutputMonthNode = 2
				objDateStructure.intOutputDayNode = 1
				objDateStructure.intOutputComponents = 3
			}
			break;
		case "DD/MM/YYYY":
			if (blnInput == true) {
		        if (objDateStructure.intInputComponents != 3) {
					alert(strComponentCountError)
					return false
				}
				objDateStructure.intInputYearNode = 3
				objDateStructure.intInputMonthNode = 2
				objDateStructure.intInputDayNode = 1
			}
			else {
				objDateStructure.intOutputYearLength = 4
				objDateStructure.intOutputMonthLength = 2
				objDateStructure.intOutputDayLength = 2
				objDateStructure.intOutputYearNode = 3
				objDateStructure.intOutputMonthNode = 2
				objDateStructure.intOutputDayNode = 1
				objDateStructure.intOutputComponents = 3
			}
			break;
		default:
			alert("PROGRAMMING ERROR:  Invalid date format passed to date validation routine.\rContact System Administrator.\r" + objDateStructure.strDateFormatInput)
			return false
			break;
	}

	return true
}

//  ==================================================================================
//  Function:		ValidateIndividualComponents
//  Purpose:		Expands the year to 4 digits (if required) and/or populates year.
//					Also determines month and day and does some minor validation of them.
//  Returns:		TRUE if acceptable, FALSE if not acceptable
//	Called By:		ValidateDate
//  Required Inputs: objDateStructure
//  Optional Inputs: none
//  Change Log:
//  Date			Name				Description
//  ----			----				-----------
//  08-09-2000		B.Bailey			Original Creation
//  ==================================================================================
function ValidateIndividualComponents(objDateStructure) {

	// Determine location of input month
	switch(objDateStructure.intInputMonthNode) {
		case 1:
			intInputMonth = objDateStructure.intInputComponent1
			break;
		case 2:
			intInputMonth = objDateStructure.intInputComponent2
			break;
		case 3:
			intInputMonth = objDateStructure.intInputComponent3
			break;
		default:
			intInputMonth = 0
			break;
	}

	intCharLocation = intInputMonth.search(/[a-z, A-Z]/)
	if (intCharLocation != -1) {
		alert("Invalid month value entered.  You entered a non numeric value: " + intInputMonth)
		return false
	}

	tmpMonth = eval(intInputMonth)

	if (tmpMonth < 1 || tmpMonth > 12) {
		alert("Invalid month value entered.  Must be a value between 1 and 12.\rYou entered: " + intInputMonth)
		return false
	}
	else {
		objDateStructure.intOutputMonthValue = intInputMonth
	}

	// Determine location of input day
	switch(objDateStructure.intInputDayNode) {
		case 1:
			intInputDay = objDateStructure.intInputComponent1
			break;
		case 2:
			intInputDay = objDateStructure.intInputComponent2
			break;
		case 3:
			intInputDay = objDateStructure.intInputComponent3
			break;
		default:
			intInputDay = 1
			break;
	}

	intCharLocation = intInputDay.search(/[a-z, A-Z]/)
	if (intCharLocation != -1) {
		alert("Invalid day value entered.  You entered a non numeric value: " + intInputDay)
		return false
	}
	
	tmpDay = eval(intInputDay)

	if (objDateStructure.intInputDayNode != 0) {
		if (tmpDay < 1 || tmpDay > 31) {
			alert("Invalid Day value entered.  You entered: " + intInputDay)
			return false
		}
	}

	objDateStructure.intOutputDayValue = intInputDay
		
	// Determine location of input year
	switch(objDateStructure.intInputYearNode) {
		case 1:
			intInputYear = objDateStructure.intInputComponent1
			break;
		case 2:
			intInputYear = objDateStructure.intInputComponent2
			break;
		case 3:
			intInputYear = objDateStructure.intInputComponent3
			break;
		default:
			intInputYear = 0
			break;
	}

	if (intInputYear == 0) { // year was not in input date.  will use current year
		var strCurrDate = new Date()
		objDateStructure.intOutputYearValue = strCurrDate.getFullYear()
	}	
	else { // year was in input date		
   	    // Make sure that we have a 4 digit year.  If not, add the century to the front.

        if (intInputYear.length == 4) {
       	    objDateStructure.intOutputYearValue = intInputYear
   	    }
        else if (intInputYear.length == 2) { // tack century onto front.

           	if (intInputYear > 83) {objDateStructure.intOutputYearValue = "19" + intInputYear}
       	    else {
				objDateStructure.intOutputYearValue = "20" + intInputYear
			}
   	    }
        else {
			if (objDateStructure.intInputYearNode == 0) {
				alert("PROGRAM ERROR:  Could not calculate the current year for validation of date. \rContact System Administrator")
			}
			else {
	       	    alert("Invalid input length of year.  Enter either a 2 or 4 digit year")
			}
   	        return false
        }
       }

	return true
}

//  ==================================================================================
//  Function:		BuildResultDate
//  Purpose:		Builds the date that will be returned to the input field.
//  Returns:		TRUE if acceptable, FALSE if not acceptable
//	Called By:		ValidateDate
//  Required Inputs: objDateStructure
//  Optional Inputs: none
//  Change Log:
//  Date			Name				Description
//  ----			----				-----------
//  08-09-2000		B.Bailey			Original Creation
//  ==================================================================================
function BuildResultDate(objDateStructure) {

	var strResultDate = ""
	
	for (idxNodes = 1; idxNodes <= 3; idxNodes++) {

		var strNode = ""
	
		if (objDateStructure.intOutputDayNode == idxNodes) {
			if (objDateStructure.intOutputDayLength == 2 && objDateStructure.intOutputDayValue.length == 1) {
				strNode = strNode + "0"
			}
			strNode = strNode + objDateStructure.intOutputDayValue
		}
		else if (objDateStructure.intOutputMonthNode == idxNodes) {
			if (objDateStructure.intOutputMonthLength == 2 && objDateStructure.intOutputMonthValue.length == 1) {
				strNode = strNode + "0"
			}
			strNode = strNode + objDateStructure.intOutputMonthValue
		}
		else if (objDateStructure.intOutputYearNode == idxNodes) {
			if (objDateStructure.intOutputYearLength == 2) {
				strTemp = objDateStructure.intOutputYearValue.toString()
				strNode = strTemp.substring(2,4)
			}
			else {
				strNode = strNode + objDateStructure.intOutputYearValue		
			}
	
		}
		
		if (strNode != "") {
			if (idxNodes > 1) {
				strResultDate = strResultDate + objDateStructure.strDelimiter
			}
			strResultDate = strResultDate + strNode
		}
	}
	
	return strResultDate
}

//  ==================================================================================
//  Function:		GetDateDiff(objDateLow, objDateHigh)
//  Purpose:		Get the difference (in days) between two dates.  Value can be negative
//                  if strDateLow > strDateHigh
//  Called From:	YourModule, ValidateBetweenDates()
//  Calls:			none
//  Returns:		number of days difference
//  Required Inputs:
//  				Two valid dates.  Pass actual values as opposed to objects.
//  Optional Inputs: strDateFormatInput - Allows for specification of date format of the input dates
//  Calling Example:
//			intDateDiff = GetDateDiff('01/01/2000', '01/02/2000')
//			if (intDateDiff < 0) {
//				take some action
//			}
//  Optional Inputs: none
//  Change Log:
//  Date			Name				Description
//  ----			----				-----------
//  08-03-2000		R. Ibanez			Original Creation
//  ==================================================================================
function GetDateDiff(strFirstDate, strSecondDate, strDateFormatInput) {

	blnRequired = true
	strDelimiter = ''
	objFirstDate = new Object
	objFirstDate.value = strFirstDate
	objSecondDate = new Object
	objSecondDate.value = strSecondDate
	strDateFormatOutput = "mm/dd/yyyy"
	
	// Confirm that both input dates contain valid dates	
	if (ValidateDate(objFirstDate, strDateFormatInput, strDelimiter, blnRequired, strDateFormatOutput)) {// no action
	}
	else {
		alert("Error in date difference routine with validating the first input date: " + strFirstDate)
		return false
	}

	if (ValidateDate(objSecondDate, strDateFormatInput, strDelimiter, blnRequired, strDateFormatOutput)) {// no action
	}
	else {
		alert("Error in date difference routine with validating the second input date: " + strSecondDate)
		return false
	}

	strTempFirst = Date.parse(objFirstDate.value)
	strTempSecond = Date.parse(objSecondDate.value)

	intDaysDiff = strTempSecond - strTempFirst
	intDaysDiff = Math.round(intDaysDiff / (1000 * 60 * 60 * 24));
	return intDaysDiff;
}

//  ==================================================================================
//  Function:		ValidateHighLowRange
//  Purpose:		Validates that an end range date is after the begin range date
//  Called From:	YourModule
//  Calls:			ValidateIndividualDate(), GetDateDiff()
//  Returns:		false if 
//  				1.  There are incorrect number of components found in date. 
//                  2.  The length of year is invalid i.e. not 2 or 4 digits.
//					3.  The format is not MM/DD/YY or MM/DD/YYYY format
//					4.  The date is not valid for that month e.g. 1/32
//					5.  Not both date entries are provided
//					6.  The first date argument is > the second date.
//				    TRUE if otherwise
//  Required Inputs:
//  				Two valid dates in MM/DD, MM/DD/YY OR MM/DD/YYYY format
//                  where the second date argument is > the first date.
//					Pass actual date objects, not just values.
//  Optional Inputs: strDateFormatInput - Allows for specification of date format of the input dates
//	Examples:
//					Pass 02/01/2000, 02/02/2000 - returns true
//					Pass 02/01/2000, 01/31/2000 - returns false
//  Optional Inputs: none
//  Change Log:
//  Date			Name				Description
//  ----			----				-----------
//  08-03-2000		R. Ibanez			Original Creation
//  ==================================================================================
function ValidateHighLowRange(objDateLow, objDateHigh, strDateFormatInput) {

	// Make sure that both input date objects contain actual values

	if (objDateLow.value != "" && objDateHigh.value == "") {
		alert("You must also enter a high end date if you enter a low end date for a date range")
		objDateHigh.focus()
		return false
	}
	
	if (objDateHigh.value != "" && objDateLow.value == "") {
		alert("You must also enter a low end date if you enter a high end date for a date range")
		objDateLow.focus()
		return false
	}
	
	if (objDateLow.value != "" && objDateHigh.value != "") {	

		blnRequired = true
		strDelimiter = ''
		dtLowCopy = objDateLow.value
		dtHighCopy = objDateHigh.value
		strDateFormatOutput = "mm/dd/yyyy"

		// Confirm that both date objects contain valid dates	
		if (ValidateDate(objDateLow, strDateFormatInput, strDelimiter, blnRequired, strDateFormatOutput)) {// no action
		}
		else {
			objDateLow.value = dtLowCopy
			objDateHigh.value = dtHighCopy			
			return false
		}

		if (ValidateDate(objDateHigh, strDateFormatInput, strDelimiter, blnRequired, strDateFormatOutput)) {// no action
		}
		else {
			objDateLow.value = dtLowCopy
			objDateHigh.value = dtHighCopy			
			return false
		}

		// Make sure that high date is beyond the low date
		if (GetDateDiff(objDateLow.value, objDateHigh.value) < 0) {
			alert("The high end date in a date range must be greater than or equal to the low end date")
			objDateLow.focus()
			objDateLow.value = dtLowCopy
			objDateHigh.value = dtHighCopy			
			return false
		}

	objDateLow.value = dtLowCopy
	objDateHigh.value = dtHighCopy			

	}

	return true;
}


