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 ("customerfirstTripPaymentDate", "quoteRequestquoteFirstTripPaymentDate");

    // IDs of  elements associated with date pickers with an lower bound of today
    var todayLowerBoundedDP = new Array ("quoteRequestdepartureDate",
            "quoteRequestreturnDate" );

    // IDs of  elements associated with date pickers with no date bounds
    var unboundedBoundedDP = new Array ("reportEndDatedate",
            "reportStartDatedate",
            "effectiveDatedate",
            "endDatedate",
            "startDatedate",
            "flagDuedate",
            "customerfinalTripPaymentDate",
            "transmitStartDatedate",
            "transmitEndDatedate");

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

    // ================  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);
    }

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

    // unbounded date pickers
    lArrLength = unboundedBoundedDP.length;
    for (i=0; i<lArrLength ; i++)
    {
        setUpCalendar(unboundedBoundedDP[i], null, null);
    }

} // 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)
{
    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"))
        {
            if (inLowerBound == null && inUpperBound == null)
            {
                // unbounded
                Calendar.setup(
                    {
                      inputField  : inId,   // ID of the input field
                      ifFormat    : format, // the date format
                      button      : inId + "calendarImage" // ID of the button
                    }
                );
                if (debug) {console.log("    set up : " + inId);}
            }
            else if (inLowerBound != null && inUpperBound == null)
            {
                // lower bound only
                Calendar.setup(
                    {
                      inputField  : inId,   // ID of the input field
                      ifFormat    : format, // the date format
                      button      : inId + "calendarImage", // ID of the button
                      lowerDateBound : inLowerBound
                    }
                );
                if (debug) {console.log("    set up : " + inId);}
            }
            else if (inLowerBound == null && inUpperBound != null)
            {
                // upper bound only
                Calendar.setup(
                    {
                      inputField  : inId,   // ID of the input field
                      ifFormat    : format, // the date format
                      button      : inId + "calendarImage",   // ID of the button
                      upperDateBound : inUpperBound
                    }
                );
                if (debug) {console.log("    set up : " + inId);}
            }
            else
            {
                // both upper and lower bounds
                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
                    }
                );
                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;
