﻿/*
    Client side date range checking for 2-calendar controls.
    
    Note: Some script must be created at run time: declaration of MaxDateRange, DateTextBoxStartID, and DateTextBoxEndID.
    
    Modification History
    6/19/2008 MES Initial version.
*/

var milliSecondsInADay = 1000 * 60 * 60 * 24;

/*
    Fired when end date changes.  Adjusts the start date if the max date range is exceeded.
*/
function OnEndDateChange(pTBId, pTime, pError)
{
    if (!pError)
    {
        if (pTime == null )
            DES_SetDTTBValue(DateTextBoxEndID, DES_GetDTTBValue(DateTextBoxStartID), null);
    
        if (DES_GetDTTBValue(DateTextBoxStartID) == null)
        {
             DES_SetDTTBValue(DateTextBoxStartID, pTime, null);
             DES_DTBAddDays(DateTextBoxStartID, (-1 * MaxDateRange()));                                    
        }
        else
        {
            daysBeyondMaxRange = DateRange() - MaxDateRange(); 
            if (daysBeyondMaxRange > 0 )
                DES_DTBAddDays(DateTextBoxStartID, daysBeyondMaxRange);
        }
    }
}

/*
    Fired when start date changes.   Adjusts the end date if the max date range is exceeded.
*/
function OnStartDateChange(pTBId, pTime, pError)
{
    if(!pError)
    {
        if (pTime == null )
            DES_SetDTTBValue(DateTextBoxStartID, DES_GetDTTBValue(DateTextBoxEndID), null);
            
        if (DES_GetDTTBValue(DateTextBoxEndID) == null)
        {
             DES_SetDTTBValue(DateTextBoxEndID, pTime, null);
             DES_DTBAddDays(DateTextBoxEndID, MaxDateRange());                                   
        }
        else
        {
            daysBeyondMaxRange = DateRange() - MaxDateRange(); 
            if (daysBeyondMaxRange > 0 )
                DES_DTBAddDays(DateTextBoxEndID, (-1 * daysBeyondMaxRange));
        }
    }
}

/*
    Calculates the date range between the two calendar dates.
*/
function DateRange()
{   
    startTime = DES_GetDTTBValue(DateTextBoxStartID).getTime();   
    endTime = DES_GetDTTBValue(DateTextBoxEndID).getTime();    
    return (Math.floor((endTime-startTime)/milliSecondsInADay))
}       
