// $Header: /vc/cvsroot/netbooker/IBE/web/sources/js/calendar/IBECalUtil.js,v 1.4 2004/07/19 22:30:51 jdavis Exp $
      //************************************************************************
      // Global variables
      //************************************************************************

       function addDays(myDate,days)
       {
          return new Date(myDate.getTime() + days*24*60*60*1000);
       }
      //************************************************************************
      // Function:  updateDATES
      // Purpose:   Update the Departure Date to the Arrival Date + 1.
      // Input:     language - language used by the WEB page.
      //            locale - locale used by the WEB page.
      // Output:    None
      //************************************************************************

      function updateDATES(formStr, language, locale, days)
      {

        // Get the current arrival date.
        var startYear = parseInt (document.getElementById('DATERANGESTART_YEAR').options[document.getElementById('DATERANGESTART_YEAR').selectedIndex].value, 10);
        var startMonth = parseInt (document.getElementById('DATERANGESTART_MONTH').options[document.getElementById('DATERANGESTART_MONTH').selectedIndex].value, 10);
        var startDay = parseInt (document.getElementById('DATERANGESTART_DAY').options[document.getElementById('DATERANGESTART_DAY').selectedIndex].value, 10);

        var startDate = new Date (startYear, startMonth - 1, startDay);

        // Get the current departure date.
        var endYear = parseInt (document.getElementById('DATERANGEEND_YEAR').options[document.getElementById('DATERANGEEND_YEAR').selectedIndex].value, 10);
        var endMonth = parseInt (document.getElementById('DATERANGEEND_MONTH').options[document.getElementById('DATERANGEEND_MONTH').selectedIndex].value, 10);
        var endDay = parseInt (document.getElementById('DATERANGEEND_DAY').options[document.getElementById('DATERANGEEND_DAY').selectedIndex].value, 10);

        var endDate = new Date (endYear, endMonth - 1, endDay);

        // If the arrival date is greater than the departure date then update the departure date.
        var done = true;
        var daydiff = DateDiff( startDate,endDate,'d');
        var nights=0;
        if (days != -1)
        {
          endDate = addDays(startDate,days);
          endYear = endDate.getFullYear() ;
          endMonth = endDate.getMonth() + 1;
          endDay = endDate.getDate() ;
          updateDateCombos(formStr,language, locale,endYear,endMonth,endDay,'DATERANGEEND_YEAR', 'DATERANGEEND_MONTH', 'DATERANGEEND_DAY', 'DATERANGEEND_DOW');
          updateDOW (formStr, 'DATERANGESTART_YEAR', 'DATERANGESTART_MONTH', 'DATERANGESTART_DAY', 'DATERANGESTART_DOW', language, locale);
        }
        else
        {
          if(daydiff > 0)
          {
            nights = daydiff;
          }
          else
          {
            nights = document.getElementById('NUMBER_OF_NIGHTS').value;
            /*var new_date = addDays(endDate,(nights*-1));
            startYear = new_date.getYear();
            startMonth = new_date.getMonth() + 1;
            startDay = new_date.getDate();
            updateDateCombos(formStr,language, locale,startYear,startMonth,startDay,'DATERANGESTART_YEAR', 'DATERANGESTART_MONTH', 'DATERANGESTART_DAY', 'DATERANGESTART_DOW');*/
          }
          if (document.getElementById('NUMBER_OF_NIGHTS')!=null)
          {
            document.getElementById('NUMBER_OF_NIGHTS').value = nights;
          }
          updateDOW (formStr, 'DATERANGEEND_YEAR', 'DATERANGEEND_MONTH', 'DATERANGEEND_DAY', 'DATERANGEEND_DOW', language, locale);
        }
      }

      function updateDateCombos(formStr,language, locale,endYear,endMonth,endDay,yearID,monthID,dayID,wkID)
      {
        try
        {
          var objYear = eval("document.getElementById('" + yearID + "')");
          var objMonth = eval("document.getElementById('" + monthID + "')");
          var objDay = eval("document.getElementById('" + dayID + "')");
          // Update departure year.
          for (index = 0; (index < objYear.length) &&
                          (objYear.options[index].value != endYear);
               index++);
          objYear.options[index].selected = true;

          // Update departure month.
          for (index = 0; (index < objMonth.length) &&
                          (objMonth.options[index].value != endMonth);
               index++);
          objMonth.options[index].selected = true;

          // Update departure day.
          for (index = 0; (index < objDay.length) &&
                          (objDay.options[index].value != endDay);
               index++);
          objDay.options[index].selected = true;
          updateDOW (formStr, yearID, monthID, dayID, wkID, language, locale);
        }catch(errorObject)
        {
          return false;
        }
        return true;
      }
      //************************************************************************
      // Function:  updateNUMBER_OF_NIGHTS
      // Purpose:   Update the nights as per the arrival and departure date diff.
      // Input:     nights - number of nights.
      // Output:    none.
      //************************************************************************
      function updateNUMBER_OF_NIGHTS(formStr,language,locale)
      {
        var nights = 0;
        if (document.getElementById('NUMBER_OF_NIGHTS')!=null)
        {
          nights = document.getElementById('NUMBER_OF_NIGHTS').value;
        }
        if (nights.substring(0,1) != '-')
        {
          updateDATES(formStr, language, locale, nights);
        }
      }
      function updateSTART_DATE(formStr, language, locale)
      {
          updateNUMBER_OF_NIGHTS(formStr, language, locale);
      }
      function updateEND_DATE(formStr, language, locale)
      {
          updateDATES(formStr, language, locale,-1);
      }
      function setArrivalDate(date,formStr,language,locale)
      {
          updateDateCombos(formStr,language, locale,date.getFullYear(),date.getMonth()+1,date.getDate(),'DATERANGESTART_YEAR', 'DATERANGESTART_MONTH', 'DATERANGESTART_DAY', 'DATERANGESTART_DOW')
      }
      function setDepartureDate(date,formStr,language,locale)
      {
          updateDateCombos(formStr,language, locale,date.getFullYear(),date.getMonth()+1,date.getDate(),'DATERANGEEND_YEAR', 'DATERANGEEND_MONTH', 'DATERANGEEND_DAY', 'DATERANGEEND_DOW');
      }

      //************************************************************************
      // Function:  DateDiff
      // Purpose:   Diff of two dates.
      // Input:     startDate, endDate, interval (m-month, d-day, y-year)
      // Output:    difference in interval.
      //************************************************************************
      function DateDiff( start, end, interval )
      {
        var iOut = 0;
        var rounding =0;

        // Create 2 error messages, 1 for each argument.
        var startMsg = "Check the Start Date and End Date\n"
            startMsg += "must be a valid date format.\n\n"
            startMsg += "Please try again." ;

        var intervalMsg = "Sorry the dateAdd function only accepts\n"
            intervalMsg += "d, h, m OR s intervals.\n\n"
            intervalMsg += "Please try again." ;

        var bufferA = Date.parse( start ) ;
        var bufferB = Date.parse( end ) ;

        // check that the start parameter is a valid Date.
        if ( isNaN (bufferA) || isNaN (bufferB) ) {
            alert( startMsg ) ;
            return null ;
        }

        // check that an interval parameter was not numeric.
        if ( interval.charAt == 'undefined' ) {
            // the user specified an incorrect interval, handle the error.
            alert( intervalMsg ) ;
            return null ;
        }

        var number = bufferB-bufferA ;

        // what kind of add to do?
        switch (interval.charAt(0))
        {
            case 'd': case 'D':
                iOut = parseInt(number / 86400000) ;
                if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
                break ;
            case 'h': case 'H':
                iOut = parseInt(number / 3600000 ) ;
                if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
                break ;
            case 'm': case 'M':
                iOut = parseInt(number / 60000 ) ;
                if(rounding) iOut += parseInt((number % 60000)/30001) ;
                break ;
            case 's': case 'S':
                iOut = parseInt(number / 1000 ) ;
                if(rounding) iOut += parseInt((number % 1000)/501) ;
                break ;
            default:
            // If we get to here then the interval parameter
            // didn't meet the d,h,m,s criteria.  Handle
            // the error.
            alert(intervalMsg) ;
            return null ;
        }
        return iOut ;
      }