/* Time/Date Functions for Javascript Copyright (c) 2001-2002, Whale Media Inc. Module Name: time.js Abstract: Time functions for javascript provides basic functions for resolving strings to dates, conversions, and list box support. Author: Andre Joseph Daniels */ var weekdays = new Array( "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); var monthNames = [ "January","February","March","April","May","June","July","August","September","October","November","December"]; var daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var daysInMonthsDuringLeapYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var minute = 60 * 1000 var hour = 60 * minute var day = 24 * hour var week = day * 7 var beginDate,endDate; var defaultDepartureOffset = 2; var maximumDays = 31; var minBeginDate = cleanDate(new Date()); var maxYear = 2006; beginDate= new Date(); endDate = new Date(); endDate.setTime(beginDate.getTime() + defaultDepartureOffset * day); function adjustDates() { getDatesFromLists(); adjustDatesAndNumberOfNights(); initDayList(document.forms[0].lstBeginDay,beginDate); initDayList(document.forms[0].lstEndDay,endDate); setDates(); } //sets departure to arrival date plus num nights selected function offsetDepartureByNumberOfNights() { var numberOfNights=document.forms[0].numberOfNights.options.selectedIndex + 1; var newDate = new Date(); endDate.setTime(beginDate.getTime() + day * numberOfNights); setDates();//(document.forms[0].lstEndDay,document.forms[0].lstEndMonth,document.forms[0].lstEndYear,newDate); } function getFullYear(d) { var y = d.getYear(); if (y < 1000) y += 1900; return y; } function getDatesFromLists() { //get data from lists var beginMonth = document.forms[0].lstBeginMonth.selectedIndex; var endMonth = document.forms[0].lstEndMonth.selectedIndex; var beginYear = getText(document.forms[0].lstBeginYear); var endYear = getText(document.forms[0].lstEndYear); var beginDay = getText(document.forms[0].lstBeginDay); var endDay = getText(document.forms[0].lstEndDay); beginDate = new Date(beginYear,beginMonth,beginDay); endDate = new Date(endYear,endMonth,endDay); } function IsValidBeginDate() { if(beginDate==null) return false; else if(isNaN(beginDate.getTime())) return false; else if(beginDate.getTime() < cleanDate(new Date()).getTime()) { var src=window.html if(src!=null && src.srcElement!=null) { var lastChange=src.srcElement.name; if(src.srcElement.name=="lstBeginMonth") { beginDate.setYear(beginDate.getYear() + 1); } else return false; } } else return true; } function IsValidEndDate() { if(endDate==null) return false; else if(isNaN(endDate.getTime())) return false; else if(endDate.getTime()<=beginDate.getTime()) return false; else if((endDate.getTime()-beginDate.getTime())/ day > maximumDays) { endDate.setTime(beginDate.getTime()+ (maximumDays-1) * day); return true; } else return true; } function resetDates() { if(!IsValidBeginDate()) beginDate= new Date(); endDate.setTime(beginDate.getTime()+ getNumberOfNights() * day); } function parseDate(dateString) { var re=/(\w+)\/(\w+)\/(\w+)/; var values = re.exec(dateString); return new Date(parseInt(values[3]),parseInt(values[1])-1,parseInt(values[2])); } function validDates() { return (IsValidBeginDate() && IsValidEndDate()); } function getNumberOfNights() { return document.forms[0].numberOfNights.selectedIndex + 1; } function setNumberOfNights() { var len = ((endDate.getTime()- beginDate.getTime()) / day)-1; while(document.forms[0].numberOfNights.options==null || document.forms[0].numberOfNights.options.length <=len) { var count = 0; if(document.forms[0].numberOfNights.options!=null) count=document.forms[0].numberOfNights.options.length; addToList(document.forms[0].numberOfNights,count,count); } document.forms[0].numberOfNights.options[len].selected=true; } function adjustDatesAndNumberOfNights() { var minDays = defaultDepartureOffset; if(document.forms[0].numberOfNights!=null && !isNaN(document.forms[0].numberOfNights.value)) minDays = getNumberOfNights(); if(!validDates()) resetDates(); var src=window.html if(src!=null && src.srcElement!=null) { var lastChange=src.srcElement.name; if(lastChange.indexOf("lstBegin")==0) { if(!IsValidBeginDate()) beginDate= cleanDate(new Date()); endDate.setTime(beginDate.getTime()+ minDays * day); setNumberOfNights(); } else if(lastChange=="lstEndMonth") { var beginMonth = document.forms[0].lstBeginMonth.selectedIndex; var endMonth = document.forms[0].lstEndMonth.selectedIndex; if(!IsValidEndDate()) endDate.setTime(beginDate.getTime()+ minDays * day); else { if(beginMonth==endMonth) endDate.setTime(beginDate.getTime()+ defaultDepartureOffset * day); else setNumberOfNights(); } } else if(lastChange.indexOf("lstEnd")==0) { if(!IsValidEndDate()) endDate.setTime(beginDate.getTime()+ minDays * day); else setNumberOfNights(); } else if(lastChange.indexOf("numberOfNights")==0) { endDate.setTime(beginDate.getTime()+ getNumberOfNights() * day); } else { if(!IsValidBeginDate()) resetDates(); else if(!IsValidEndDate()) { endDate.setTime(beginDate.getTime()+ getNumberOfNights() * day); } } } } function cleanDate(date) { date.setHours(0); date.setMinutes(0); date.setSeconds(0); date.setMilliseconds(0); return date; } function setDates() { var dat=""; var dat2=""; if (navigator.appName == "Netscape" ) { dat+=beginDate.getYear()+1900; dat2+=endDate.getYear()+1900; } else { dat+=beginDate.getYear(); dat2+=endDate.getYear(); } setIndexByValue(dat,document.forms[0].lstBeginYear); setIndexByValue(dat2,document.forms[0].lstEndYear); setIndex(beginDate.getDate(),document.forms[0].lstBeginDay); setIndex(endDate.getDate(),document.forms[0].lstEndDay); document.forms[0].lstBeginMonth.options[beginDate.getMonth()].selected=true; document.forms[0].lstEndMonth.options[endDate.getMonth()].selected=true; if(document.forms[0].numberOfNights!=null) { if(endDate.getTime()>beginDate.getTime()) { var newIndex = (endDate.getTime()-beginDate.getTime())/day; while((newIndex-1)>=document.forms[0].numberOfNights.options.length) newIndex--; document.forms[0].numberOfNights.options[newIndex-1].selected=true; } } } function getNextMonth(monthDate) { var tempDate = new Date(monthDate); var monthNo = tempDate.getMonth(); while(tempDate.getMonth()==monthNo) { tempDate.setTime(tempDate.getTime() + day); } return tempDate; } function getLastMonth(monthDate) { var tempDate = new Date(monthDate); var monthNo = tempDate.getMonth(); while(tempDate.getMonth()==monthNo) { tempDate.setTime(tempDate.getTime() - day); } return tempDate; } function getLastDayOfMonth(month) { return monthMax(month-1) } function checkMonths() { adjustDates() getDatesFromLists() initDayListFromMonthAndYear(document.forms[0].lstBeginDay,document.forms[0].lstBeginDay.selectedIndex,document.forms[0].lstBeginMonth.selectedIndex-1,arrYear) initDayListFromMonthAndYear(document.forms[0].lstBeginDay,document.forms[0].lstBeginDay.selectedIndex,document.forms[0].lstBeginMonth.selectedIndex-1,depYear) } function getDaysInMonth(mtIDx, YrStr) { // all the rest have 31 var maxDays=31 // expect Feb. (of course) if (mtIDx==1) { if (isLeapYear(YrStr)) { maxDays=29; } else { maxDays=28; } } // thirty days hath... if (mtIDx==3 || mtIDx==5 || mtIDx==8 || mtIDx==10) { maxDays=30; } return maxDays; } function initDayListFromMonthAndYear(lst,day,month,year) { var newDate = new Date(month + " 1, " + year) lst.options.length = 0 //lst.options[0]= new Option(" ") for(var t = 1;t<= getDaysInMonth(month,year);t++) lst.options[t]= new Option(t) lst.selectedIndex=day } function daysInMonth(dt) { var monthNum=dt.getMonth(); var curMonth=monthNum; while(curMonth==monthNum) { dt.setTime(dt.getTime()+day); curMonth=dt.getMonth(); } dt.setTime(dt.getTime()-day); return dt.getDate(); } function initDayList(lst,dateToParse) { var dt = new Date(dateToParse); //correct day lists lst.options.length = 0 var totalDays = daysInMonth(dt); for(var t = 0;t<= totalDays;t++) lst.options[t]= new Option(t); lst.options[dt.getDate()].selected = true lst.options[0]=null; } function initMonthList(lst) { lst.options.length = 0 lst.options[1]= new Option("Jan") lst.options[2]= new Option("Feb") lst.options[3]= new Option("Mar") lst.options[4]= new Option("Apr") lst.options[5]= new Option("May") lst.options[6]= new Option("Jun") lst.options[7]= new Option("Jul") lst.options[8]= new Option("Aug") lst.options[9]= new Option("Sep") lst.options[10]= new Option("Oct") lst.options[11]= new Option("Nov") lst.options[12]= new Option("Dec") } function initYearList(lst) { lst.options.length = 0 lst.options[0]= new Option(" ") for(var t = 1;t< 10;t++) lst.options[t]= new Option("200" + t) lst.options[1].selected=true }