// globals #######################################
ie = document.all ? true : false;
dom = document.getElementById ? true : false;
ns = document.layers ? true : false;
// ################################################

// these could be overridden by an ouside js file containing language specific month names
var monthnames_display = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

// these are the english monthnames which we need for comparation against <option value>
var monthnames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var daynames = new Array( "S", "M", "T", "W", "T", "F", "S" );
var monthdays  = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

// alert messages. these may be overridden by an outside js file containing language
// specific alerts
var activityEndAfterActivityStart="The activity end date must be later than the activity start date.";
var activityStartCurrentDateOrLater="The activity start date must be current date or later.";
var onlySearch365DaysInAdvance="You can only search for activities up to 365 days in advance.";

var selectedMonthYr = new Date()
var firstClick = true
var onCheckIn = 1 // 1 = on check in, 2 = on check out, 3 = on nothing
var searchCal = false;
var inBrowserCal;
var calForm;
var checkInName = "Check-in"
var checkOutName = "Check-out"


function setSearchCal( v ) {
  searchCal = v;
}

function setOnCheckIn( val ) {
  onCheckIn = val
}

function updateIECheckIn( checkIn ) {
  if ( checkIn == null ) {
    if ( searchCal )
      return;
    else
      checkIn = (getCheckInDate() == null ? new Date() : getCheckInDate())
  }
  resize( calForm.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 ) )
    if(getCheckOutDate().getDay() == getCheckInDate().getDay()){
	setCheckOutDate( new Date( checkIn.getTime() + 90000000 ))
    }
    resize( calForm.checkOutDay, getCheckOutDate() )
    if ( getCheckOutDate().getMonth() >= selectedMonthYr.getMonth() + 2 ||
	 ( getCheckOutDate().getMonth() == 0 && getCheckOutDate().getFullYear() >= selectedMonthYr.getFullYear() + 1 ) ){
      selectedMonthYr = checkIn;
}
  } else if ( searchCal && getCheckOutDate() == null  ) {
  }
  redrawCalendar()
}

function updateNSCheckIn( checkIn ) {
  if ( checkIn == null ) {
    if ( searchCal )
      return;
    else
      checkIn = (getCheckInDate() == null ? new Date() : getCheckInDate())
  }
  setCheckInDate( checkIn )
  if ( getCheckOutDate() <= checkIn ){
    setCheckOutDate( new Date( checkIn.getTime() + 86400000 ) )
    if(getCheckOutDate().getDay() == getCheckInDate().getDay()){
        setCheckOutDate( new Date( checkIn.getTime() + 90000000 ))
    }
  }
  else if ( searchCal )
    setCheckOutDate( getCheckOutDate() )
}
  

function updateIECheckOut( checkOut ) {
  if ( checkOut == null ) {
    if ( searchCal )
      return;
    else
      checkOut = (getCheckOutDate() == null ? new Date( new Date().getTime() + 86400000 ) : getCheckOutDate() )
  }
  resize( calForm.checkOutDay, checkOut )
  setCheckOutDate( checkOut )
  redrawCalendar();
}

function updateNSCheckOut( checkOut ) {
  if ( checkOut == null ) {
    if ( searchCal )
      return;
    else
      checkOut = (getCheckOutDate() == null ? new Date( new Date().getTime() + 86400000 ) : getCheckOutDate() )
  }
  setCheckOutDate( checkOut )
}

function resize( select, date ) {
  var dim = getDaysInMonth( date.getMonth(), date.getFullYear() )
  var size = select.length
  if ( searchCal ) size = size - 1
  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 ) {
      if ( searchCal ) size = size + 1
      select.remove( size - 1 )
    }
    size = select.length
    if ( searchCal ) size = size - 1
  }
}

function handleIECalClick( day, month, year ) {
  if ( onCheckIn == 1 ) {
    setOnCheckIn( firstClick ? 2 : 3 )
    updateCheckIn( new Date( year, month, day ) )
	if (calForm.los) {
    	getLOS(calForm.los.options[calForm.los.options.selectedIndex].value)
	}
  } else if ( onCheckIn == 2 ) {
    setOnCheckIn( 3 )
    updateCheckOut( new Date( year, month, day ) )
  } else {
    alert( "Please select either " + checkInName + " or " + checkOutName + " above the calendar." )
  }
  firstClick = false;
  calendarWindow.close()
}

function handleNSCalClick( day, month, year ) {
  if ( onCheckIn == 1 ) {
    updateNSCheckIn( new Date( year, month, day ) )
    if (calForm.los) {
    	getLOS(calForm.los.options[calForm.los.options.selectedIndex].value)
	}
  } 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>" + checkInName + "&nbsp;"
  else
    out += "<INPUT onClick=\"javascript:setOnCheckIn(1)\" type=\"radio\" name=\"checkInOut\">" + checkInName + "&nbsp;"
  out += " | "
  if ( onCheckIn == 2 )
    out += "<INPUT onClick=\"javascript:setOnCheckIn(2)\" type=\"radio\" name=\"checkInOut\" checked>" + checkOutName
  else
    out += "<INPUT onClick=\"javascript:setOnCheckIn(2)\" type=\"radio\" name=\"checkInOut\">" + checkOutName
  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=\"/en/hi/res/images/previous.gif\"></a>" : "&nbsp;&nbsp;" )
  out += "</td>"
  out += "<td align='center'><b>" + monthnames_display[ month ] + " " + year + "</b>" 
  out += "</td>"
  out += "<td align='right'>" + ( prev_next != 1 && displayMoveForward( month, year ) ? "<a href=\"javascript:opener.moveForward()\"><img width='16' height='12' border='0' src=\"/en/hi/res/images/next.gif\"></a>" : "&nbsp;&nbsp;" )
  out += "</td>"
  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 += "</tr></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=\"images/previous.gif\"></a>" : "&nbsp&nbsp" )
  out += "<td class=cal align=center><font class=month_header>" + monthnames_display[ 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=\"images/next.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 ) {
  var today = new Date()
  var parm  = new Date( year, month, 1 )
  return ( parm.getFullYear() == today.getFullYear() ) || 
           ( parm.getFullYear() > today.getFullYear() && parm.getMonth() < today.getMonth() );
}

// 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"
}

function setInBrowserCal( ibc ) {
  inBrowserCal = ibc
  if ( ibc ) {
    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()

  if ( selectedMonthYr == null )
    selectedMonthYr = new Date()
  if ( ie ) {
    topABC = window.screenTop + 100
    leftABC = window.screenLeft + 250
  } else {
    topABC = window.screenY + 100
    leftABC = window.screenX + 250
  }
  calendarWindow = window.open("","Calendar","width=166,height=160,resizable=yes,scrollbars=no,status=no,top=" + topABC + ",left=" + leftABC )
  generateCalendar( calendarWindow )
  calendarWindow.focus()
}

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 =  calForm.checkInDay.options[calForm.checkInDay.selectedIndex].value
  var month = calForm.checkInMonthYr.options[calForm.checkInMonthYr.selectedIndex].value
  if ( day == -1 || month == "" ) {
		return null;
	}
  else if (month == "-1") {
		return null;
	}
  month = month.substring( 0, month.indexOf( " " ) )
  var year = calForm.checkInMonthYr.options[calForm.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 ) {
  for ( var i = 0 ; i < calForm.checkInDay.options.length ; i++ )
    if ( calForm.checkInDay.options[ i ].value == checkIn.getDate() )
      calForm.checkInDay.options[ i ].selected = true;
  var moyear = monthnames[ checkIn.getMonth() ] + " " + checkIn.getFullYear()
  for ( var i = 0 ; i < calForm.checkInMonthYr.options.length ; i++ )
    if ( calForm.checkInMonthYr.options[ i ].value == moyear )
      calForm.checkInMonthYr.options[ i ].selected = true
}

function getCheckOutDate() {
  var day = calForm.checkOutDay.options[calForm.checkOutDay.selectedIndex].value
  var month = calForm.checkOutMonthYr.options[calForm.checkOutMonthYr.selectedIndex].value

  if ( day == -1 || month == "" ) {
        return null;
	}
  else if (month == "-1") {
        return null;
	}
  month = month.substring( 0, month.indexOf( " " ) )
  var year = calForm.checkOutMonthYr.options[calForm.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 ) {
  for ( var i = 0 ; i < calForm.checkOutDay.options.length ; i++ )
    if ( calForm.checkOutDay.options[ i ].value == checkOut.getDate() )
      calForm.checkOutDay.options[ i ].selected = true;
  var moyear = monthnames[ checkOut.getMonth() ] + " " + checkOut.getFullYear()

  for ( var i = 0 ; i < calForm.checkOutMonthYr.options.length ; i++ )
    if ( calForm.checkOutMonthYr.options[ i ].value == moyear )
      calForm.checkOutMonthYr.options[ i ].selected = true
}

function setCalForm( f ) {
  calForm = f;
}

function setup() {
  if ( calForm == null ) calForm = document.main;
  if ( inBrowserCal ) {
    updateCheckIn( getCheckInDate() )
    updateCheckOut( getCheckOutDate() )
    redrawCalendar()
  }
}

setInBrowserCal( (document.all && navigator.appVersion.toUpperCase().indexOf( "WIN" ) != -1) ? 1 : 0 )


function validateDates() {
  var cid = getCheckInDate()
  var cod = getCheckOutDate()
  var tod = new Date()
  var yes = new Date( tod - 86400000 )
  //CR 21738
  var nyr = new Date( Date.parse(tod) + (86400000 * 365) )

if ( cid == null || cod == null ) {
 return true;
}
else {
if ( cod <= cid )
    alert(activityEndAfterActivityStart)
  else if ( cid < yes )
    alert(activityStartCurrentDateOrLater)
  else if ( cod > nyr || cid > nyr )
    alert(activityStartCurrentDateOrLater)
  else
  return true
 return false
 }

}
function performAvailSearch()
{
    if (checkNoDate('availSearch','checkIn','checkOut') && checkDate('availSearch','checkIn','checkOut')){document.availSearch.submit()}
}

function performRewardSearch()
{
    if (checkNoDate('rewardSearch','checkIn','checkOut') && checkDate('rewardSearch','checkIn','checkOut')){document.rewardSearch.submit()}
}

