﻿// Properties
var daysArray = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var monthsArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var NewCalanderDate=0;

function ToggleDropdownCalendar( containerID )
{

    var calendar = document.getElementById(containerID);
    if ( calendar == null || typeof( calendar ) == 'undefined' )
        return;
    //alert(pos1[0]+'-----' + pos1[1]);
    //alert(pos2[0]+'-----' + pos2[1]);
    //debugger;
    if (calendar.style.display == "block" )
    {
        HidePopupDiv(containerID);
        //calendar.style.display = "none";
    }
    else
    {
      calendar.style.marginLeft = "0px";
      calendar.style.display = "block";
     
     
     //START : 7/6/2008 : Gulshan Gulati :Below code added to maintain dropdown calendar position with calendar image
      var imgArrPosition;
      var imgDptPosition;
      
      if (containerID.indexOf("ddcDepartureDate") == -1)
      {
        var imgArrDateID  = containerID.substring(0,containerID.lastIndexOf('_')) + '_imgCalendar';
        imgArrPosition = findPos(document.getElementById(imgArrDateID ));
        calendar.style.left = imgArrPosition[0] - calendar.offsetWidth - 10 + 'px';
        calendar.style.top = imgArrPosition[1] - calendar.offsetHeight + 20 + 'px';
      }
      else
      {
        var imgDptDateID = containerID.substring(0,containerID.lastIndexOf('_')) + '_imgCalendar';
        imgDptPosition = findPos(document.getElementById(imgDptDateID));
        calendar.style.left = imgDptPosition[0] - calendar.offsetWidth - 10 + 'px';
        calendar.style.top = imgDptPosition[1] - 5 + 'px';
      }
     //END : 7/6/2008 : Gulshan Gulati :Below code added to maintain dropdown calendar position with calendar image 
     
     if(calendar.offsetLeft < 0)
     {
        if (containerID.indexOf("ddcReservationDepartureDate") == -1)
        {
            var imgArrDateID  = containerID.substring(0,containerID.lastIndexOf('_')) + '_imgCalendar';
            imgArrPosition = findPos(document.getElementById(imgArrDateID ));
            calendar.style.left = imgArrPosition[0] + 'px';
            calendar.style.top = imgArrPosition[1] - calendar.offsetHeight - 15 + 'px';
        }
        else
        {
            var imgDptDateID = containerID.substring(0,containerID.lastIndexOf('_')) + '_imgCalendar';
            imgDptPosition = findPos(document.getElementById(imgDptDateID));
            calendar.style.left = imgArrPosition[0] + 'px';
            calendar.style.top = imgDptPosition[1] + 25 + 'px';
        }
      }
        ShowPopupDiv(containerID);
      
    }
}

    /// Code By Somil to fixed Combo -Div Display order problem. --- Starts
    //var g_PopupIFrame;
    function IsIE()
    {
        return ( navigator.appName=="Microsoft Internet Explorer" );
    }

    function HidePopupDiv(divID)
    {

        var divPopup;
        divPopup=document.getElementById(divID);
        divPopup.style.display = "none";

        if (IsIE())
        {
            var iFrame;
            iFrame = document.getElementById(divID + 'iFrame');
            document.body.removeChild(iFrame);
            iFrame=null;
        }
    }

    function ShowPopupDiv(divID)
    {
        var divPopup=document.getElementById(divID);
        if (!IsIE())
        {
            //Just display the div
            divPopup.style.display ="block";
            return;
        }

        //Increase default zIndex of div by 1, so that DIV appears before IFrame
        divPopup.style.zIndex=divPopup.style.zIndex+1;
    
        var iFrame = document.createElement("IFRAME");
        
        iFrame.id=divID + 'iFrame';
        
        iFrame.setAttribute("src", "/BlankHTMlForIframeSSL.htm");

        //Match IFrame position with divPopup
        iFrame.style.position="absolute";
        iFrame.style.left =divPopup.offsetLeft + 'px';
        iFrame.style.top =divPopup.offsetTop + 'px';
        iFrame.style.width =divPopup.offsetWidth + 'px';
        iFrame.style.height =divPopup.offsetHeight + 'px';
        document.body.appendChild(iFrame);
        divPopup.style.display ="block";
        
    }
/// Code By Somil to fixed Combo -Div Display order problem. --- Ends


//For Postion of calendar.

function findPos(obj)
{
var curleft = curtop = curBottom = curRight = 0;
    if (obj.offsetParent)
     {  curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent)
        {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
      }
    return [curleft,curtop];
}



function FormatTitle( month, year )
{
    return month + "\u00a0" + year;
}

function GetCalendarDate( ddlDayID, ddlMonthID, ddlYearID  )
{
    var ddlDay, ddlMonth, ddlYear
   
    ddlDay = document.getElementById(ddlDayID);
    ddlMonth = document.getElementById(ddlMonthID);
    ddlYear = document.getElementById(ddlYearID);
    
    var day = ddlDay.options[ddlDay.selectedIndex].value;
    var month = ddlMonth.options[ddlMonth.selectedIndex].value;
    var year = ddlYear.options[ddlYear.selectedIndex].value;
    
    month = parseInt( month,10) - 1;
    
    var date = new Date(parseInt(year,10),month,parseInt( day,10),11,0,0);
    return date;
}

function RefreshControls(date, ddlDayID, ddlMonthID, ddlYearID, txtDayOfWeekID )
{
    var tempMonth = date.getMonth() + 1;
    if (tempMonth < 10) tempMonth = "0" + tempMonth;
    tempMonth = tempMonth.toString();
    
    var day = date.getDate().toString();
    if ( day.length == 1 )
        day = "0" + day;

    var ddlDay = document.getElementById(ddlDayID);
    UpdateDayDropDown( ddlDay, date );
   
    var ddlMonth = document.getElementById(ddlMonthID);
    SelectCalendarDropdown(ddlMonth, tempMonth);
    
    var ddlYear = document.getElementById(ddlYearID);
    SelectCalendarDropdown(ddlYear, date.getFullYear());
    
    var txtDayOfWeek = document.getElementById( txtDayOfWeekID );

    if ( txtDayOfWeek )
        txtDayOfWeek.innerHTML = date.getDayOfWeekName();
}

function UpdateDayDropDown( ddlDay, date )
{
    if ( !ddlDay )
        return;
        
    var copyDate = new Date();
    copyDate.setFullYear( date.getFullYear() );
    copyDate.setMonth( date.getMonth() );
    copyDate.setDate( 1 );
    
    var day = date.getDate().toString();
    if ( day.length == 1 )
        day = "0" + day;
    day = day.toString();
    
    // clear the dropdown
    ddlDay.options.length = 0;
    
    var month = copyDate.getMonth();
    var i = 0;
    while ( month == copyDate.getMonth() )
    {
        var val = copyDate.getDate();
        if ( val.toString().length == 1 )
            val = "0" + val;
        val = val.toString();
        
        ddlDay.options[ i ] = new Option( val, val );
        if (  val == day  )
            ddlDay.options[ i ].selected = true;
        
        copyDate.setDate( copyDate.getDate() + 1 );    
        i++;
    }
}

function OnDropDownChange( id, ddlDayID, ddlMonthID, ddlYearID, txtDayOfWeekID )
{
    try
    {
    
        eval( 'ValidateDate_' + id )( id);
        
    }
    catch(ex)
    {}
    
}

function SelectCalendarDropdown(obj, sel)
{

    for (var index = 0; index < obj.options.length; index++ )
    {
        if ( obj.options[index].value == sel)
            obj.options[index].selected = true;
        else
            obj.options[index].selected = false;
    }
    
}

// Event handlers for the calendar elements

function addDropdownCalendarMonths( n, calendarID, controlID, ddlDayID, ddlMonthID, ddlYearID, txtDayOfWeekID )
{
    // Advance the calendar month and update the display
    var targetDate = GetCalendarDate( ddlDayID, ddlMonthID, ddlYearID );    
    NewCalanderDate = NewCalanderDate+n;
    targetDate.addMonths(NewCalanderDate);
    DrawCalendar( calendarID, controlID, ddlDayID, ddlMonthID, ddlYearID, txtDayOfWeekID, targetDate );
}

function addDropdownCalendarYears( n, calendarID, controlID, ddlDayID, ddlMonthID, ddlYearID, txtDayOfWeekID  )
{
    var targetDate = GetCalendarDate( ddlDayID, ddlMonthID, ddlYearID );
    targetDate.addYears(n);
    DrawCalendar( calendarID, controlID, ddlDayID, ddlMonthID, ddlYearID, txtDayOfWeekID, targetDate);
}

function getMonthNumber(month)
{
    for (var i in monthsArray)
    {
        if (monthsArray[i] == month)
        {
            i++;
            if (i < 10) i = "0" + i;
            return i;
        }
    }
}

// Add new properties and methods to the Date object
// Methods
Date.prototype.getDayOfWeekName = dateGetDayOfWeekName;
Date.prototype.getMonthName = dateGetMonthName;
Date.prototype.getDays      = dateGetDays;
Date.prototype.addDays      = dateAddDays;
Date.prototype.addMonths    = dateAddMonths;
Date.prototype.addYears     = dateAddYears;

// getMonthName(): Returns the name of the date's month
function dateGetMonthName()
{
    return this.monthNames[this.getMonth()];
}

function dateGetDayOfWeekName()
{
    return this.daysOfWeekNames[ this.getDay() ];
}

// getDays(): Returns the number of days in the date's month
function dateGetDays()
{
    var tmpDate, d, m;

    tmpDate = new Date(Date.parse(this));
    m = tmpDate.getMonth();
    d = 28;
    do
    {
        d++;
        tmpDate.setDate(d);
    } while (tmpDate.getMonth() == m);

    return d - 1;
}

// addDays(n): Adds the specified number of days to the date
function dateAddDays(n)
{
    // Add the specified number of days
    this.setDate(this.getDate() + n);

    // Reset the new day of month.
    this.savedDate = this.getDate();
}

// addMonths(n): Adds the specified number of months to the date, adjusting
// the day of the month if necessary
function dateAddMonths(n)
{
    // Save the day of month if not already set
    if (this.savedDate == null)
    this.savedDate = this.getDate();

    // Set the day of month to the first to avoid rolling
    this.setDate(1);

    // Add the specified number of months
    this.setMonth(this.getMonth() + n);

    // Restore the saved day of month, if possible
    this.setDate(Math.min(this.savedDate, this.getDays()));
}

// addYears(n): Adds the specified number of years to the date, adjusting the
// day of the month for leap years
function dateAddYears(n)
{
    // Save the day of month if not already set
    if (this.savedDate == null)
    this.savedDate = this.getDate();

    // Set the day of month to the first to avoid rolling
    this.setDate(1);

    // Add the specified number of years
    this.setFullYear(this.getFullYear() + n);

    // Restore the saved day of month, if possible
    this.setDate(Math.min(this.savedDate, this.getDays()));
}


               
function DrawCalendar( containerID, controlID, ddlDayID, ddlMonthID, ddlYearID, txtDayOfWeekID, targetDate  )
{
    var container = document.getElementById(containerID);
    if ( container == null || typeof( container ) == 'undefined' )
        return;
    
    // clear old results
    container.innerHtml = null;
    
    if ( !targetDate )
        targetDate = GetCalendarDate( ddlDayID, ddlMonthID, ddlYearID );
        
    // get month name
    var headerTitle = FormatTitle( targetDate.getMonthName(), targetDate.getFullYear() );
    
    var currentDate = new Date();
    var hidePreviousLink = ((targetDate.getMonth() - 1) < currentDate.getMonth() && targetDate.getFullYear() == currentDate.getFullYear());
    
    if(isNaN(targetDate))
		targetDate = new Date();
    
    // Start with the first day of the month and go back if necessary to the previous Sunday
    tmpDate = new Date(Date.parse(targetDate));
    tmpDate.setDate(1);
    while (tmpDate.getDay() != 0)
    {
        tmpDate.addDays(-1);
    }
     
    // draw the table
    var calendarHtml = "<table id='nuggetCalendar' class='calendar'>";
    calendarHtml += "<tr class='header'>";
    calendarHtml += "<th id='nuggetCalendarPrevLink'><a href='#' title='Previous Month' onclick=\"addDropdownCalendarMonths(-1,";
    calendarHtml += "'" + containerID + "', '" + controlID + "', '" + ddlDayID + "', '" + ddlMonthID + "', '" + ddlYearID + "', '" + txtDayOfWeekID + "' ); return false;\">";
        
    if ( !hidePreviousLink )
        calendarHtml += "&lt;&lt;";
    else  
        calendarHtml += "";
    calendarHtml += "</a></th>";
        
    calendarHtml += "<th id='nuggetCalendarHeader' colspan='5'>" + headerTitle + "</th>";
    
    calendarHtml += "<th id='nuggetCalendarNextLink'><a href='#' title='Next Month' onclick=\"addDropdownCalendarMonths(1, ";
    calendarHtml += "'" + containerID + "', '" + controlID + "', '" + ddlDayID + "', '" + ddlMonthID + "', '" + ddlYearID + "', '" + txtDayOfWeekID + "' ); return false;\">";
    calendarHtml += "&gt;&gt;</a></th>" ;
    calendarHtml += "</tr>";
    
    calendarHtml += "<tr class='days'>";
    
    var daysOfWeek = new Date();
    for( var i = 0; i < daysOfWeek.daysOfWeekNames.length; i++ )
    {
        calendarHtml += "<th>" + daysOfWeek.daysOfWeekNames[ i ] + "</th>";
    }
     
    calendarHtml += "</tr>";
    
    // Go through each calendar day cell in the table and update it
    for (i = 0; i <= 5; i++)
    {
        // Hide row if it contains no dates in the current month
        tmpDate2 = new Date(Date.parse(tmpDate));
        tmpDate2.addDays(6);
        
        calendarHtml += "<tr ";
        if (tmpDate.getMonth()  != targetDate.getMonth() && tmpDate2.getMonth() != targetDate.getMonth() ) 
            calendarHtml += "class='empty'";
        
        calendarHtml += ">";
            
         // Loop through a week
        for (j = 0; j < 7; j++)
        {
            calendarHtml += "<td ";
            
            var cellClass = "";
            if ( j == 0 || j == 6 )
                 cellClass = "weekend";
                 
            // check if this is the current date
            var now = new Date();
            if ( (now.getFullYear() == tmpDate.getFullYear()) &&
                    (now.getDate() == tmpDate.getDate()) &&
                    (now.getMonth() == tmpDate.getMonth()))
                 cellClass = cellClass + " target";
                 
            // set the class
            if ( cellClass.length > 0 )
                calendarHtml += "class='" + cellClass + "'";
            
            calendarHtml += "><a href='#' style='cursor:hand' ";
                        
            if (tmpDate.getMonth() == targetDate.getMonth())
            {
                calendarHtml += " date='" + new Date(Date.parse(tmpDate)) + "'";
                calendarHtml += " DayID='" + ddlDayID + "'";
                calendarHtml += " MonthID='" + ddlMonthID + "'";
                calendarHtml += " YearID='" + ddlYearID + "'";
                calendarHtml += " DayOfWeekID='" + txtDayOfWeekID + "'";
                calendarHtml += " ContainerID='" + containerID + "'";
                calendarHtml += " ControlID='" + controlID + "'";
                                
                var s = tmpDate.toString().split(" ");
                calendarHtml += " title='" + s[0] + " " + s[1] + " " + s[2] + ", " + s[s.length - 1] + "'";
                calendarHtml += " onclick='DateClicked(event, true);'";
                calendarHtml += ">" + tmpDate.getDate();
            }
            else
            {
                calendarHtml += " style='display:none' ";
                calendarHtml += ">";
            }
            
            calendarHtml += "</a></td>";
            
            // Go to the next day
            tmpDate.addDays(1);
        }
        
        calendarHtml += "</tr>";
        
        if (tmpDate.getMonth() > targetDate.getMonth())
            i = 20;
    }
    
    calendarHtml += "</table>";

    container.innerHTML = calendarHtml;
}

function DateClicked(e, toggle) {
    if (!e) e = window.event;
    var eventElement = window.event ? e.srcElement : e.target;
    var DayID = eventElement.getAttribute("DayID");
    var MonthID = eventElement.getAttribute("MonthID");
    var YearID = eventElement.getAttribute("YearID");
    var DayOfWeekID = eventElement.getAttribute("DayOfWeekID");
    var ContainerID = eventElement.getAttribute("ContainerID");
    var ControlID = eventElement.getAttribute("ControlID");
    var TargetDate = new Date(Date.parse(eventElement.getAttribute("date")));
    DropDownSelectDate(TargetDate, DayID, MonthID, YearID, DayOfWeekID, ContainerID, ControlID, toggle);
    if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
}
//Modified by Arshad 10 Oct, 07.
function DropDownSelectDate(TargetDate, DayID, MonthID, YearID, DayOfWeekID, ContainerID, ControlID, toggle) {
    //alert(ControlID);
    //CheckCurrent( TargetDate,ErrorMessage );
    RefreshControls( TargetDate, DayID, MonthID, YearID, DayOfWeekID );
    OnDropDownChange( ControlID, DayID, MonthID, YearID, DayOfWeekID );
    if (toggle) ToggleDropdownCalendar( ContainerID );
}

function CheckCurrent( verifyDate,errorMessage )
{
    var currentDate = new Date();
    currentDate.setHours( 0, 0, 0, 0 );
    
    var changedDate = false;
    
    /////////////////Added by Tony Guo on August 16, 2007 Ver5.11/////////////////////
    if (verifyDate.getFullYear() == currentDate.getFullYear() && verifyDate.getMonth() < currentDate.getMonth())
       {
        verifyDate.setFullYear( currentDate.getFullYear()+1 );       
       return true;
       }
    else if (verifyDate.getFullYear() == currentDate.getFullYear() && verifyDate.getMonth() == currentDate.getMonth() && verifyDate.getDate() < currentDate.getDate())
       {
       if (currentDate.getMonth()<11)
           verifyDate.setMonth( currentDate.getMonth()+1 );       
       else
          {
           verifyDate.setMonth( 0 ); 
           verifyDate.setFullYear( currentDate.getFullYear()+1 );                       
          }
       return true;
       }
        
    //////////////////////////////////////////////////////////////////////////////////
    
    // if the arrival date is in the past then update the year
    if ( ( verifyDate.getFullYear() == currentDate.getFullYear() ) && 
            ( ( verifyDate.getMonth() < currentDate.getMonth() ) ||
                ( ( verifyDate.getMonth() == currentDate.getMonth() ) &&
                    ( verifyDate.getDate() < currentDate.getDate() ) )
            ) 
        )
    {
        
        alert(errorMessage);
        
        changedDate = false;
        
        //verifyDate.setFullYear( currentDate.getFullYear() + 1 );
    }
    if ( verifyDate.getFullYear() < currentDate.getFullYear() )
    {
        changedDate = true;
        verifyDate.setFullYear( currentDate.getFullYear() );
    }
        
    if ( ( verifyDate.getFullYear() == currentDate.getFullYear() ) && 
            ( verifyDate.getMonth() < currentDate.getMonth() ) )
     {
        changedDate = true;
        verifyDate.setMonth( currentDate.getMonth() );
    }
        
    if ( ( verifyDate.getFullYear() == currentDate.getFullYear() ) && 
            ( verifyDate.getMonth() == currentDate.getMonth() ) && 
            ( verifyDate.getDate() < currentDate.getDate() ) )
     { 
        changedDate = true;
        verifyDate.setDate( currentDate.getDate() );
     }  
     return changedDate; 
}

