﻿// JScript File
YAHOO.util.Event.onDOMReady(function () {

    function onButtonClickStart() {
        /*
             Create an empty body element for the Overlay instance in order 
             to reserve space to render the Calendar instance into.
        */

        oCalendarMenuStart.setBody("&#32;");

        oCalendarMenuStart.body.id = "calendarcontainerStart";


        // Render the Overlay instance into the Button's parent element

        oCalendarMenuStart.render(this.get("container"));
        

        // Align the Overlay to the Button instance

        oCalendarMenuStart.align();
        
        
        /*
             Create a Calendar instance and render it into the body 
             element of the Overlay.
        */

        var oCalendar = new YAHOO.widget.Calendar("buttoncalendar", oCalendarMenuStart.body.id);

        oCalendar.render();

        /* 
            Subscribe to the Calendar instance's "changePage" event to 
            keep the Overlay visible when either the previous or next page
            controls are clicked.
        */

        oCalendar.changePageEvent.subscribe(function () {
            
            window.setTimeout(function () {

                oCalendarMenuStart.show();
            
            }, 0);
        
        });


        /*
            Subscribe to the Calendar instance's "select" event to 
            update the month, day, year form fields when the user
            selects a date.
        */

        oCalendar.selectEvent.subscribe(function (p_sType, p_aArgs) {

            var aDate;

            if (p_aArgs) {
                    
                aDate = p_aArgs[0][0];
                var dateField = document.getElementsByClassName('requestFieldDateStart');
                if (dateField && dateField[0])
                dateField[0].value = getMonthName(aDate[1]) + ' ' + aDate[2] + ', ' + aDate[0];
                setDateFieldsActive();
            }
            
            oCalendarMenuStart.hide();
        
        });
        
        /*
             Unsubscribe from the "click" event so that this code is 
             only executed once
        */
        this.unsubscribe("click", onButtonClickStart);
    
    }
    
    function onButtonClickEnd() {

        /*
             Create an empty body element for the Overlay instance in order 
             to reserve space to render the Calendar instance into.
        */

        oCalendarMenuEnd.setBody("&#32;");

        oCalendarMenuEnd.body.id = "calendarcontainerEnd";


        // Render the Overlay instance into the Button's parent element

        oCalendarMenuEnd.render(this.get("container"));
        

        // Align the Overlay to the Button instance

        oCalendarMenuEnd.align();
        
        
        /*
             Create a Calendar instance and render it into the body 
             element of the Overlay.
        */

        var oCalendar = new YAHOO.widget.Calendar("buttoncalendarEnd", oCalendarMenuEnd.body.id);

        oCalendar.render();


        /* 
            Subscribe to the Calendar instance's "changePage" event to 
            keep the Overlay visible when either the previous or next page
            controls are clicked.
        */

        oCalendar.changePageEvent.subscribe(function () {
            
            window.setTimeout(function () {

                oCalendarMenuEnd.show();
            
            }, 0);
        
        });


        /*
            Subscribe to the Calendar instance's "select" event to 
            update the month, day, year form fields when the user
            selects a date.
        */

        oCalendar.selectEvent.subscribe(function (p_sType, p_aArgs) {

            var aDate;

            if (p_aArgs) {
                    
                aDate = p_aArgs[0][0];
                var dateField = document.getElementsByClassName('requestFieldDateEnd');
                if (dateField && dateField[0])
                dateField[0].value = getMonthName(aDate[1]) + ' ' + aDate[2] + ', ' + aDate[0];
                setDateFieldsActive();
            }
            
            oCalendarMenuEnd.hide();
        
        });
        

        /*
             Unsubscribe from the "click" event so that this code is 
             only executed once
        */

        this.unsubscribe("click", onButtonClickEnd);
    
    }


    // Create an Overlay instance to house the Calendar instance

    var oCalendarMenuStart = new YAHOO.widget.Overlay("calendarmenu");
    var oCalendarMenuEnd = new YAHOO.widget.Overlay("calendarmenu");


    // Create a Button instance of type "menu"

    var oButtonStart = new YAHOO.widget.Button({
                                        type: "menu", 
                                        id: "calendarpickerStart",
                                        label: "Choose A Date", 
                                        menu: oCalendarMenuStart, 
                                        container: "dateStartCell" });
    
    var oButtonEnd = new YAHOO.widget.Button({ 
                                        type: "menu", 
                                        id: "calendarpickerEnd",
                                        label: "Choose A Date", 
                                        menu: oCalendarMenuEnd,
                                        container: "dateEndCell" });

    /*
        Add a "click" event listener that will render the Overlay, and 
        instantiate the Calendar the first time the Button instance is 
        clicked.
    */

    oButtonStart.on("click", onButtonClickStart);
    oButtonEnd.on("click", onButtonClickEnd);
});

Event.observe(window, 'load', function ()
{
  // Event listener for # Children field
  var ddlChildren = document.getElementsByClassName('selectFieldChildren');
  if (ddlChildren && ddlChildren[0])
  {
    Event.observe(ddlChildren[0], 'change', function ()
    {
      if (ddlChildren[0].selectedIndex == 0)
      {
        $('childrensAgesBox').style.display = 'none';
      }
      else
      {
        $('childrensAgesBox').style.display = 'block';
      }
    });
  }
  
  // Event listener for Date fields
  $A(document.getElementsByClassName('requestFieldDates')).each(
    function(field)
    {
      Event.observe(field, 'focus', function (e)
      {
        setDateFieldsActive();
      });
    }
  );
});

function setDateFieldsActive()
{
  $A(document.getElementsByClassName('requestFieldDates')).each(
    function(field)
    {
      field.style.color = 'Black';
      if (field.value == 'Start' || field.value == 'End')
      {
        field.value = '';
      }
    }
  );
}

function getMonthName(monthNum)
{
  var monthName = "";
  switch (monthNum)
  {
    case 1:
      monthName = "Jan";
      break;
    case 2:
      monthName = "Feb";
      break;
    case 3:
      monthName = "Mar";
      break;
    case 4:
      monthName = "Apr";
      break;
    case 5:
      monthName = "May";
      break;
    case 6:
      monthName = "Jun";
      break;
    case 7:
      monthName = "Jul";
      break;
    case 8:
      monthName = "Aug";
      break;
    case 9:
      monthName = "Sep";
      break;
    case 10:
      monthName = "Oct";
      break;
    case 11:
      monthName = "Nov";
      break;
    case 12:
      monthName = "Dec";
      break;
  }
  return monthName;
}

// Validation Control Function
function validateWordLength(sender, args)
{
  var intMaxWords = 250;
  var strValue = args.Value;
  var objRegExp = new RegExp('[\\n|\\r]+', 'gi');
  strValue = strValue.replace(objRegExp, ' ');
  var arrWords = strValue.split(' ');
  if(arrWords.length > intMaxWords)
	  args.IsValid = false;
  else
	  args.IsValid = true;
}