var debug = false;
var format = "%m/%d/%Y";

function init()
{
    var id = "";
    var today = new Date();
    var todayString = today.getFullYear() + "/" + (today.getMonth()+1) + "/" + today.getDate();
    var yearFromToday = new Date();
    yearFromToday.setDate( yearFromToday.getDate() + 364 );
    var yearFromTodayString = yearFromToday.getFullYear() + "/" + (yearFromToday.getMonth()+1) + "/" + yearFromToday.getDate();

    // IDs of  elements associated with date pickers with an upper bound of today
    var todayUpperBoundedDP = new Array (
        "Customer[FirstTripPaymentDate]", "QuoteRequest[InitialTripPaymentDate]");

    // IDs of  elements associated with date pickers with an lower bound of today
    var todayLowerBoundedDP = new Array (
        "QuoteRequest[DepartureDate]", "QuoteRequest[EffectiveDate]",
        "QuoteRequest[ReturnDate]", "QuoteRequest[ExpirationDate]" );

    // IDs of  elements associated with date pickers with no date bounds
    var unboundedBoundedDP = new Array (
        "reportEndDate[Date]",
        "reportStartDate[Date]",
        "effectiveDate[Date]",
        "endDate[Date]",
        "startDate[Date]",
        "flagDue[Date]",
        //"Customer[FinalTripPaymentDate]",
        "transmitStartDate[Date]",
        "transmitEndDate[Date]");


    setUpCalendar( "addDepartureDate", todayString, yearFromTodayString, null );
    setUpCalendar( "addReturnDate", todayString, yearFromTodayString, null );

    if (typeof jQuery != 'undefined')
    { // only try it if jQuery is loaded
        var returnDateLB = todayString;
        var returnDateVal = new Date($("#QuoteRequest\\[DepartureDate\\]").val());
        if (!isNaN(returnDateVal.valueOf())) returnDateLB = $("#QuoteRequest\\[DepartureDate\\]").val();

        var expirationDateLB = todayString;
        var expirationDateVal = new Date($("#QuoteRequest\\[EffectiveDate\\]").val());
        if (!isNaN(expirationDateVal.valueOf())) expirationDateLB = $("#QuoteRequest\\[EffectiveDate\\]").val();
    }

    setUpCalendar( "QuoteRequest[ReturnDate]", returnDateLB, null, null );
    setUpCalendar( "QuoteRequest[ExpirationDate]", expirationDateLB, null, null );

    // ================  set up the date picker calendars ================

    // date pickers with an upper date bound
    var lArrLength = todayUpperBoundedDP.length;
    for (i=0; i<lArrLength ; i++)
    {
        setUpCalendar(todayUpperBoundedDP[i], null, todayString, null);
    }

    // date pickers with a lower date bound
    lArrLength = todayLowerBoundedDP.length;
    for (i=0; i<lArrLength ; i++)
    {
        setUpCalendar(todayLowerBoundedDP[i], todayString, null, null);
    }

    // unbounded date pickers
    lArrLength = unboundedBoundedDP.length;
    for (i=0; i<lArrLength ; i++)
    {
        setUpCalendar(unboundedBoundedDP[i], null, null, null);
    }
    if(document.getElementById("Customer[FinalTripPaymentDate]")){
        var itp=document.getElementById("Customer[FirstTripPaymentDate]");
        if(itp)inLowerBound=itp.value;
        else inLowerBound=todayString;
        setUpCalendar("Customer[FinalTripPaymentDate]",inLowerBound,null,itp)
    }

} // end init()

/*
 *  This function will set up a calendar for the given ID.  Clicking on a date
 *  on the calendar will change the value in the element with the given ID to
 *  the clicked on date.
 *
 *  Additionally, date pickers will only be enabled if the input field they
 *  are associated with is not readonly or disabled.  This prevents the user
 *  from using the date picker to change data in fields they shouldn't be
 *  able to change.
 *
 *  The given date strings (if any) are used to bound the date which is
 *  pickable by the user
 *
 *  @param inId String containing the ID of the element on the page which
 *          this calendar will be associated with
 *  @param inLowerBound String containing the date of the earliest pickable date
 *          if null there will be no lower bound
 *  @param inUpperBound String containing the date of the latest pickable date
 *          if null there will be no upper bound
 */
function setUpCalendar(inId, inLowerBound, inUpperBound, defaultDate)
{
    var element = "";
    var elementAttribs = "";
    var elementReadonlyValue = "";

    if (debug) {console.log("setting up: " + inId);}

    element = document.getElementById( inId );
    if(element != null)
    {
        elementAttribs = element.attributes;
        var elementReadonly = elementAttribs.getNamedItem("readonly");
        if (elementReadonly != null) {
            elementReadonlyValue = elementReadonly.nodeValue;
        }

        // most browsers will return null or an empty string if a parameter
        // like readonly is not set.  IE6 and 7 return "false"
        if(element.disabled != true &&
                (elementReadonlyValue == "" || elementReadonlyValue == "false"))
        {
            
            Calendar.setup(
                {
                  inputField  : inId,   // ID of the input field
                  ifFormat    : format, // the date format
                  button      : inId + "calendarImage",   // ID of the button
                  lowerDateBound : inLowerBound,
                  upperDateBound : inUpperBound,
                  date: defaultDate
                }
            );
            if (debug) {console.log("    set up : " + inId);}
        } // and if element.disabled != true && elementReadonlyFlag==null
        else
        {
            if (debug) {console.log("    element is disabled or readonly");}
        }
    } // end if(element != null
    else
    {
        if (debug) {console.log("    element is not on page");}
    }
} // end setUpCalendar

window.onload = init;

