//----------------------------------------------------------------------

var M1 = null;
var M2 = null;

//----------------------------------------------------------------------

function openCal( sType )
{
  var re = null;
  var iYYYY = 0;
  var iMM = 0;
  
  // House-cleaning...
  var obj = document.getElementById( "cal_Cal" );

  if ( obj.style.visibility == "visible" )
    showHideCalendar( obj, false );

  // Prepare...
  obj = document.getElementById( "cal_ChooseDate" );

  if ( obj )
    obj.innerHTML = ( sType == "S" )? "Choose a check-in date:" : "Choose a check-out date:";
  
  obj = document.getElementById( "qb"+sType+"Month" );

  if ( obj ) {
    iYYYY = parseInt( obj.value.substr(0, 4) );
    re = new RegExp( "" + iYYYY );
    iMM = parseInt( obj.value.replace( re, "" ) );
    setCalendarMonths( iYYYY, iMM, ( (obj.selectedIndex + 1) >= obj.options.length ) );
    M1.auxCallerParams = sType;
    M2.auxCallerParams = sType;
    M1.render();
    M2.render();
    showHideNavButtons( M1.YYYY, M1.MM, "cal_Prior", "cal_Next" );

    // Show it...
    var btn = document.getElementById( "qb"+sType+"MonthCal" );
    obj = document.getElementById( "cal_Cal" );
    setCalendarPos( obj, btn );
    showHideCalendar( obj, true );
  }
}

//----------------------------------------------------------------------

function setCalendarMonths( iYYYY, iMM, bLastMonth )
{
  if ( !bLastMonth ) {
    M1.YYYY = iYYYY;
    M1.MM = iMM - 1;
    M2.YYYY = M1.YYYY;
    M2.MM = M1.MM + 1;
  
    if ( M2.MM > 11 ) {
      M2.YYYY = M2.YYYY + 1;
      M2.MM = 0;
    }
  }
  else {
    M2.YYYY = iYYYY;
    M2.MM = iMM - 1;
    M1.YYYY = M2.YYYY;
    M1.MM = M2.MM - 1;
  
    if ( M1.MM < 0 ) {
      M1.YYYY = M1.YYYY - 1;
      M1.MM = 11;
    }
  }
}

//----------------------------------------------------------------------

function showHideCalendar( obj, bVisible )
{
  obj.style.visibility = ( bVisible )? "visible" : "hidden";
  
  // Patch for IE6/5.x-Select-Ignore-zIndex-Problem
  if ( window.ActiveXObject ) {
    if ( bVisible )
      insertIE6iFrame( obj );
    else
      removeIE6iFrame( obj );
  }
}

//----------------------------------------------------------------------

function cDt( iYYYY, iMM, iDD, auxParams )
{
  var sVal = "" + iYYYY + "" + iMM;

  matchOptionByValue( "qb"+auxParams+"Month", sVal );
  matchOptionByValue( "qb"+auxParams+"Day", iDD );

  if ( auxParams == "S" )
    advanceTDate();
  
  showHideCalendar( document.getElementById( "cal_Cal" ), false );
}

//----------------------------------------------------------------------

function showHideNavButtons( iCurrYYYY, iCurrMM, idPrior, idNext )
{
  var dToday = new Date();
  var obj = document.getElementById( idPrior );

  if ( obj )
    obj.style.display = ( iCurrYYYY == dToday.getFullYear() && iCurrMM == dToday.getMonth() )? "none" : "inline";

  obj = document.getElementById( idNext );
  
  if ( obj ) {
    var iYYYYM = parseInt( dToday.getFullYear() + 1 ) * 100 + dToday.getMonth() - 1;
    var M2YYYYM = M2.YYYY * 100 + M2.MM;
    obj.style.display = ( iYYYYM > M2YYYYM )? "inline" : "none";
  }
}

//----------------------------------------------------------------------

function Calendar( idNS, idCal )
{
  M1 = new Month( 0, 0, idNS+"Month1", "cDt", "" );
  M2 = new Month( 0, 1, idNS+"Month2", "cDt", "" );
  M1.render();
  M2.render();

  // Hide yourself...
  document.getElementById( idNS+"HideMe" ).onclick = function() {
    showHideCalendar( document.getElementById( idCal ), false );
  }

  // Move back...
  document.getElementById( idNS+"Prior" ).onclick = function() {
    M1.prior();
    M2.prior();
    showHideNavButtons( M1.YYYY, M1.MM, idNS+"Prior", idNS+"Next" );
  }
  
  // Move up...
  document.getElementById( idNS+"Next" ).onclick = function() {
    M1.next();
    M2.next();
    showHideNavButtons( M1.YYYY, M1.MM, idNS+"Prior", idNS+"Next" );
  }
  
  // Jump to...
  document.getElementById( idNS+"YYYYMM" ).onchange = function() {
    if ( this.value != "" ) {
      var iYYYY = parseInt( this.value.substr(0, 4) );
      var re = new RegExp( "" + iYYYY );
      var iMM = parseInt( this.value.replace( re, "" ) );
      setCalendarMonths( iYYYY, iMM, ( (this.selectedIndex + 1) >= this.options.length ) );
      M1.render();
      M2.render();
      showHideNavButtons( M1.YYYY, M1.MM, idNS+"Prior", idNS+"Next" );
    }
  }
}

//----------------------------------------------------------------------
