// Custom Validation Code for the PopupCalendar Control 
// Created by Tom Wisnowski
// Date: 3-2-2004
// NOTES:
//					this js file requires a javascript runtime of at least 1.4 to run correctly. 
//




//-------------------------------------------------------
//ValidateDateAgainstDate()
//
// Function: Compares the values of one popup date control to another. The control will render 
//					 form variables to specify what control to validate against and what type of comparison to perform.
//
// Input: 
//				source - validation element
//				arguments - object. 
//
//
// Remarks: This function is called by the framework and it's signature should not be changed. It is assigned to the custom validator's 
//						ClientSideValidation property.
//--------------------------------------------------------					 
function ValidateDateAgainstDate(source, arguments)
{
	if(window.GetElementById)
	{
		//the browser independant functions have been included.... try to validate.
		try
		{
			var source_cal_elm = GetElementById(source.controltovalidate);
			if(source_cal_elm != undefined)
			{
				var target_cal_elm = GetElementById(source_cal_elm.CtrlToCompareAgainst);
				if(target_cal_elm != undefined)
				{
					var comparison_type = source_cal_elm.CtrlComparisonType;
					arguments.IsValid = CompareForValidation(source_cal_elm, target_cal_elm, comparison_type);
				} 
			}
		}
		catch(e) 
		{
			arguments.IsValid = true; //default to true to allow the postback, let the server side code validate.
		}
	}
}


//-------------------------------------------------------
//CompareForValidation()
//
// Function: Compares the values of two elements converted into Dates. Comparison is based on the Compare type.
//
// Params:
//					CalSource - input element. value should be a valid date string. lvalue
//					CalTarget - input element. value should be a valid date string. rvalue
//					CompareType - string. Denotes the type of comparison to make.
//
// Remarks: Always default to return true. This way, if there is an error, the user won't be stuck on the page becuase the validator
//					control is stopping the postback. The server should always validate the dates anyways...
//--------------------------------------------------------					 

function CompareForValidation(CalSource, CalTarget, CompareType)
{
	var time1 = 0;
	var time2 = 0;
	try
	{
		time1 = Date.parse(CalSource.value);
		time2 = Date.parse(CalTarget.value);
	}
	catch(e)
	{
		return true;
	}
	
	switch(CompareType)
	{
		case 'Equals':
			return  time1 == time2;
		break;
		
		case 'LessThan':	
			return  time1 < time2;
		break;
		
		case 'GreaterThan':
			return  time1 > time2;
		break;
		
		case 'LessThanEquals':
			return  time1 <= time2;
		break;
		
		case 'GreaterThanEquals':
			return  time1 >= time2;
		break;
		
		case 'NotEqual':
			return  time1 != time2;
		break;
		
		default:
			return true;
	}	
}
