var monthnames = new Array("January","February","March","April","May","June","July","August","September","October","November","December")
var monthdays  = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
var daynames = new Array( "S", "M", "T", "W", "T", "F", "S" )

var selectedMonthYr = new Date()
var firstClick = true
var onCheckIn = 1 // 1 = on check in, 2 = on check out, 3 = on nothing
var inBrowserCal = (document.all && navigator.appVersion.toUpperCase().indexOf( "WIN" ) != -1) ? 1 : 0

function setOnCheckIn( val ) {
  onCheckIn = val
}

function updateIECheckIn( checkIn ) {
  if ( checkIn == null ) 
    checkIn = getCheckInDate()
  resize( document.main.checkInDay, checkIn )
  if ( checkIn.getMonth() != selectedMonthYr.getMonth() && checkIn.getMonth() != selectedMonthYr.getMonth() + 1 )
    selectedMonthYr = checkIn
  setCheckInDate( checkIn )
  if ( getCheckOutDate() <= checkIn ) {
    setCheckOutDate( new Date( checkIn.getTime() + 86400000 ) )
    resize( document.main.checkOutDay, getCheckOutDate() )
    if ( getCheckOutDate().getMonth() >= selectedMonthYr.getMonth() + 2 ||
	 ( getCheckOutDate().getMonth() == 0 && getCheckOutDate().getYear() >= selectedMonthYr.getYear() + 1 ) )
      selectedMonthYr = checkIn
  }
  redrawCalendar()
}

function updateNSCheckIn( checkIn ) {
  if ( checkIn == null )
    checkIn = getCheckInDate()
  setCheckInDate( checkIn )
  if ( getCheckOutDate() <= checkIn )
    setCheckOutDate( new Date( checkIn.getTime() + 86400000 ) )
}
  

function updateIECheckOut( checkOut ) {
  if ( checkOut == null )
    checkOut = getCheckOutDate()
  resize( document.main.checkOutDay, checkOut )
  if ( checkOut > getCheckInDate() ) {
    setCheckOutDate( checkOut )
  }
  redrawCalendar();
}

function updateNSCheckOut( checkOut ) {
  if ( checkOut == null )
    checkOut = getCheckOutDate()
  setCheckOutDate( checkOut )
  if ( checkOut <= getCheckInDate() )
    setCheckInDate( new Date( checkOut - 86400000 ) )
}

function resize( select, date ) {
  var dim = getDaysInMonth( date.getMonth(), date.getYear() )
  var size = select.length
  while ( dim != size ) {
    if ( dim > size ) {
      var opt = document.createElement( "OPTION" )
      select.add( opt )
      opt.innerText = size + 1
      opt.value = size + 1
    } else if ( dim < size ) {
      select.remove( size - 1 )
    }
    size = select.length
  }
}

function handleIECalClick( day, month, year ) {
  if ( onCheckIn == 1 ) {
    setOnCheckIn( firstClick ? 2 : 3 )
    updateCheckIn( new Date( year, month, day ) )
  } else if ( onCheckIn == 2 ) {
    setOnCheckIn( 3 )
    updateCheckOut( new Date( year, month, day ) )
  } else {
    alert( "Please select either Check in or Check out above the calendar." )
  }
  firstClick = false
}

function handleNSCalClick( day, month, year ) {
  if ( onCheckIn == 1 ) {
    updateNSCheckIn( new Date( year, month, day ) )
  } else {
    updateNSCheckOut( new Date( year, month, day ) )
  }
  calendarWindow.close()
}

function moveForward() {
  if ( selectedMonthYr.getMonth() == 11 )
    selectedMonthYr = new Date( selectedMonthYr.getFullYear() + 1, 0, 1 )
  else
    selectedMonthYr = new Date( selectedMonthYr.getFullYear(), selectedMonthYr.getMonth() + 1, 1 )
  redrawCalendar()
}

function moveBack() {
  if ( selectedMonthYr.getMonth() == 0 )
    selectedMonthYr = new Date( selectedMonthYr.getFullYear() - 1, 11, 1 )
  else
    selectedMonthYr = new Date( selectedMonthYr.getFullYear(), selectedMonthYr.getMonth() - 1, 1 )
  redrawCalendar()
}

function generateCal() {
  var out = ""
  out += "<CENTER>Choose : "
  if ( onCheckIn == 1 )
    out += "<INPUT onClick=\"javascript:setOnCheckIn(1)\" type=\"radio\" name=\"checkInOut\" checked>Check in&nbsp;"
  else
    out += "<INPUT onClick=\"javascript:setOnCheckIn(1)\" type=\"radio\" name=\"checkInOut\">Check in&nbsp;"
  out += " | "
  if ( onCheckIn == 2 )
    out += "<INPUT onClick=\"javascript:setOnCheckIn(2)\" type=\"radio\" name=\"checkInOut\" checked>Check out"
  else
    out += "<INPUT onClick=\"javascript:setOnCheckIn(2)\" type=\"radio\" name=\"checkInOut\">Check out"
  out += "<table class=\"pad2\" cellpadding=0 cellspacing=0 border=0>"
  out += "<tr valign=top><td class=\"pad6\">" + generateMonth( selectedMonthYr.getMonth(), selectedMonthYr.getFullYear(), 1 ) + "</td>"
  var nextMonth = selectedMonthYr.getMonth() == 11 ? 0 : selectedMonthYr.getMonth() + 1
  var nextYear  = selectedMonthYr.getMonth() == 11 ? selectedMonthYr.getFullYear() + 1 : selectedMonthYr.getFullYear()
  out += "<td class=\"pad6\">" + generateMonth( nextMonth, nextYear, 2 ) + "</td></tr>"
  out += "</table>"
  out += "</CENTER>"
  return out
}

function generateNSMonth( month, year, prev_next ) {
  var out = ""
  out += "<table width=100% border=0 cellpadding=1 cellspacing=0><tr bgcolor=#d0d0d0>"
  out += "<td align=left>" + ( prev_next != 2 && displayMoveBack( month, year ) ? "<a href=\"javascript:opener.moveBack()\"><img width=16 height=12 border=0 src=../ima/precedent.gif></a>" : "&nbsp&nbsp" )
  out += "<td align=center><b>" + monthnames[ month ] + " " + year + "</b>" 
  out += "<td align=right>" + ( prev_next != 1 && displayMoveForward( month, year ) ? "<a href=\"javascript:opener.moveForward()\"><img width=16 height=12 border=0 src=../ima/suivant.gif></a>" : "&nbsp&nbsp" )
  out += "</tr></table>"

  out += "<table width=100% border=0 cellpadding=1 cellspacing=0>"
  out += "<tr>"
  for ( var i = 0 ; i < daynames.length ; i++ )
    out += "<td align=center>" +  daynames[ i ] + "</td>"
  out += "</tr>"

  var fdom = getFirstDayOfMonth( month, year ) - 1
  var nd   = getDaysInMonth( month, year )

  var days_out = 0
  for ( var i = -fdom ; i <= nd ; i++ ) {

    if ( days_out == 0 )
      out += "<tr>"

    var dayClass = getDayClass( i, month, year )
    var isClickable = dayClass != "noselect-day"
    out += "<td align=center><font size=-1>"
    if ( isClickable )
      out += "<a href='javascript:opener.handleCalClick( " + i + ", " + month + ", " + year + ")'>"
    if ( i > 0 )
      out += i
    if ( isClickable )
      out += "</a>"
    out += "</font></td>"

    if ( days_out == 6 )
      out += "</tr>"

    if ( days_out == 6 )
      days_out = 0
    else
      days_out++
  }
  out += "</table>"

  return out;
}

function generateMonth( month, year, prev_next ) {

  var out = ""
  out += "<table border=0 cellpadding=1 cellspacing=0>"
  out += "<tr><td class=break colspan=7></td></tr>"
  out += "<tr>"
  out += "<td align=center class=month_header colspan=7>"
  out += "<table width=100% border=0 cellpadding=1 cellspacing=0><tr>"
  out += "<td class=prev_month align=left>" + ( prev_next != 2 && displayMoveBack( month, year ) ? "<a class=\"static_a\" href=\"javascript:moveBack()\"><img width=16 height=12 border=0 src=../ima/precedent.gif></a>" : "&nbsp&nbsp" )
  out += "<td class=cal align=center><font class=month_header>" + monthnames[ month ] + " " + year + "</font>"
  out += "<td class=next_month align=right>" + ( prev_next != 1 && displayMoveForward( month, year ) ? "<a class=\"static_a\" href=\"javascript:moveForward()\"><img width=16 height=12 border=0 src=../ima/suivant.gif></a>" : "&nbsp&nbsp" )
  out += "</tr></table>"
  out += "</td>"
  out += "</tr>"

  out += "<tr><td class=break colspan=7></td></tr>"

  out += "<tr>"
  for ( var i = 0 ; i < daynames.length ; i++ )
    out += "<td class=day_header>" +  daynames[ i ] + "</td>"
  out += "</tr>"

  out += "<tr><td class=break colspan=7></td></tr>"

  var fdom = getFirstDayOfMonth( month, year ) - 1
  var nd   = getDaysInMonth( month, year )

  var days_out = 0
  for ( var i = -fdom ; i <= nd ; i++ ) {

    if ( days_out == 0 )
      out += "<tr>"

    var dayClass = getDayClass( i, month, year )
    var isClickable = dayClass != "noselect-day"
    var mout = "onMouseOut=\"javascript:this.className='" + dayClass + "'\""
    var min  = "onMouseOver=\"javascript:this.className='" + dayClass + "-mover'\""
    var onc = "onClick=\"javascript:handleCalClick( " + i + ", " + month + ", " + year + " )\""
    var all = isClickable ? ( " " + mout + " " + min + " " + onc ) : ""
    out += "<td class=" + dayClass + all + ">"
    if ( isClickable )
      out += "<a>"
    if ( i > 0 )
      out += i
    if ( isClickable )
      out += "</a>"
    out += "</td>"
   
    if ( days_out == 6 )
      out += "</tr>"

    if ( days_out == 6 )
      days_out = 0
    else
      days_out++
  }
  out += "</table>"

  return out
}

// returns true if we should display a "next month" icon for the given month, year
function displayMoveForward( month, year ) {
  return true
}

// returns true if we should display a "previous month" icon for the given month, year
function displayMoveBack( month, year ) {
  var today = new Date()
  var parm  = new Date( year, month, 1 )
  return ( parm.getFullYear() == today.getFullYear() && parm.getMonth() > today.getMonth() ) ||
           parm.getFullYear() > today.getFullYear();
}

function getFirstDayOfMonth( month, year ) {
  var day = new Date( year, month, 1 )
  return day.getDay()
}

function getDaysInMonth( month, year ) {
  if ( month == 1 )
    return isLeapYear( year ) ? 29 : 28
  else
    return monthdays[ month ]
}

function isLeapYear( year ) {
  return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0
}

function getIEDayClass( day, month, year ) {
  if ( day < 1 )
    return "noselect-day"
  var checkInDate = getCheckInDate()
  var checkOutDate = getCheckOutDate()

  var today = new Date()
  var nyr = new Date( today.getTime() + (86400000 * 365) )
  var selectedDay = new Date( year, month, day + 1 )
  var lastSelectedDay = new Date( year, month, day )

  if ( selectedDay < today )
    return "noselect-day"

  if ( selectedDay > nyr )
    return "noselect-day"

  if ( selectedDay > checkInDate && lastSelectedDay <= checkOutDate )
    return "selected-day"
  
  return "day"
}

function getNSDayClass( day, month, year ) {
  if ( day < 1 )
    return "noselect-day"

  var today = new Date()
  var nyr = new Date( today.getTime() + (86400000 * 365) )
  var selectedDay = new Date( year, month, day + 1 )
  var lastSelectedDay = new Date( year, month, day )

  if ( selectedDay < today )
    return "noselect-day"

  if ( selectedDay > nyr )
    return "noselect-day"

  return "day"
}

if ( inBrowserCal ) {
  handleCalClick = handleIECalClick
  getDayClass = getIEDayClass
  updateCheckIn = updateIECheckIn
  updateCheckOut = updateIECheckOut
} else {
  handleCalClick = handleNSCalClick
  getDayClass = getNSDayClass
  updateCheckIn = updateNSCheckIn
  updateCheckOut = updateNSCheckOut
}

function redrawCalendar() {
  if ( inBrowserCal ) {
    var newCal = generateCal()
    calendar.innerHTML = newCal
  } else {
    generateCalendar( calendarWindow )
  }
}

var calendarWindow
function createCalendar( ci ) {
  onCheckIn = ci
  selectedMonthYr = ( onCheckIn == 1 ) ? getCheckInDate() : getCheckOutDate()
  calendarWindow = window.open("","Calendar","width=166,height=150,resizable=yes,scrollbars=no,status=no")
  generateCalendar( calendarWindow )
}

function generateCalendar( target ) {
  target.document.open()
  var nextMonth = selectedMonthYr.getMonth() == 11 ? 0 : selectedMonthYr.getMonth() + 1
  var nextYear  = selectedMonthYr.getMonth() == 11 ? selectedMonthYr.getFullYear() + 1 : selectedMonthYr.getFullYear()
  var calStuff = "<html><head><title>calendar</title></head>"
  calStuff += "<body bgcolor=#ffffff topmargin=0 leftmargin=0 marginwidth=0 marginheight=0><center>"
  calStuff += generateNSMonth( selectedMonthYr.getMonth(), selectedMonthYr.getFullYear(), 3 )
  calStuff += "</center></body></html>"
  target.document.write( calStuff )
  target.document.close()
}

function getCheckInDate() {
  var day =  document.main.checkInDay.options[document.main.checkInDay.selectedIndex].value
  var month = document.main.checkInMonthYr.options[document.main.checkInMonthYr.selectedIndex].value
  month = month.substring( 0, month.indexOf( " " ) )
  var year = document.main.checkInMonthYr.options[document.main.checkInMonthYr.selectedIndex].value
  year = year.substring( year.indexOf( " " ) + 1, year.length )
  if ( !( month >= 0 && month <= 12 ) )
    for ( var j = 0 ; j < monthnames.length ; j++ )
      if ( monthnames[ j ] == month )
        month = j
  // if "31 Feb" is selected in the drop down, doing new Date( 31 feb ) will create a date on
  // march 1/2/3 or thereabouts.  this is a workaround:
  var date = new Date( year, month, day )
  if ( month != date.getMonth() )
    date = new Date( year, month, getDaysInMonth( month, year ) - 1 )
  return date
}

function setCheckInDate( checkIn ) {
  document.main.checkInDay.options[ checkIn.getDate() - 1 ].selected = true
  var moyear = monthnames[ checkIn.getMonth() ] + " " + checkIn.getFullYear()
  for ( var i = 0 ; i < document.main.checkInMonthYr.options.length ; i++ )
    if ( document.main.checkInMonthYr.options[ i ].value == moyear )
      document.main.checkInMonthYr.options[ i ].selected = true
}

function getCheckOutDate() {
  var day = document.main.checkOutDay.options[document.main.checkOutDay.selectedIndex].value
  var month = document.main.checkOutMonthYr.options[document.main.checkOutMonthYr.selectedIndex].value
  month = month.substring( 0, month.indexOf( " " ) )
  var year = document.main.checkOutMonthYr.options[document.main.checkOutMonthYr.selectedIndex].value
  year = year.substring( year.indexOf( " " ) + 1, year.length )
  if ( !( month >= 0 && month <= 12 ) )
    for ( var j = 0 ; j < monthnames.length ; j++ )
      if ( monthnames[ j ] == month )
        month = j
  // if "31 Feb" is selected in the drop down, doing new Date( 31 feb ) will create a date on
  // march 1/2/3 or thereabouts.  this is a workaround:
  var date = new Date( year, month, day )
  if ( month != date.getMonth() )
    date = new Date( year, month, getDaysInMonth( month, year ) )
  return date
}

function setCheckOutDate( checkOut ) {
  document.main.checkOutDay.options[ checkOut.getDate() - 1 ].selected = true
  var moyear = monthnames[ checkOut.getMonth() ] + " " + checkOut.getFullYear()
  for ( var i = 0 ; i < document.main.checkOutMonthYr.options.length ; i++ )
    if ( document.main.checkOutMonthYr.options[ i ].value == moyear )
      document.main.checkOutMonthYr.options[ i ].selected = true
}

function setup() {
  if ( inBrowserCal ) {
    updateCheckIn()
    updateCheckOut()
    redrawCalendar()
  }
}

