//var GLOBAL DP_config;
//DP input ids must be saved here
var DP2_elements = new Array('leave_dt_checkin', 'leave_dt_checkout');
//DP images ids must be saved here
var DP2_images = new Array('img_dt_checkin', 'img_dt_checkout'); 

//Colors of different day states
// GLOBAL var DP_dayColors 
//isMozilla browser
// GLOBAL var isMoz = (window.ActiveXObject)?false:true;

//Input element handler to write date
var DP2_oInput = null;
//Dual mode flag (used for check-out dates)
var DP2_dualMode = false;
//Check-out date is not set by default
var DP2_checkouSet = false;
//Rooms availability data
var DP2_roomsStatus = new Object();
var DP2_gotRoomsStatus = true;
var DP2_nextMonEnabled = new Array(true, true);
var DP2_lastMonEnabled = new Array(false, false);

//Shield layer to handle clicking on the page (browser compatibility issue)
// GLOBAL var DP_oShield;
//initializing dates
// GLOBAL var DP_today = new Date();
var DP2_checkin = new Date();
var DP2_checkout = new Date();
var DP2_checkin_visible = new Date();
var DP2_checkout_visible = null;

//Date picker layer
var DP2_oDiv = document.createElement("DIV");
DP2_oDiv.id = 'dp2_root_layer';
DP2_oDiv.className = "dp2_tbl_root";
DP2_oDiv.style.zIndex = 301;
$('calendarHolder').insert(DP2_oDiv);

//DP inputs initial values ids saved here
var DP2_values = new Array('-', '-');

//Legend DIV id
legend = "calendar_explanation";

/*
  function DP_openCalendar(obj, dual_mode, x_l, x_t) - initialize date picker control
  obj: date input object handler
  dual_mode: dual mode flag
  x_l, x_t: left, top clendar layer bias
  return none
*/
function DP2_openCalendar(obj, dual_mode, x_l, x_t) {
  DP2_oInput = obj;
  DP2_dualMode = dual_mode;

  if (DP2_values[0] == '-') { //save initial input values
    DP2_values[0] = $(DP2_elements[0]).value;
    DP2_values[1] = $(DP2_elements[1]).value;
  }
    
  //syncronize visible & working date as we will be drawing controls by working dates
  DP2_checkin.setYear(DP2_checkin_visible.getFullYear());
  DP2_checkin.setMonth(DP2_checkin_visible.getMonth());
  DP2_checkin.setDate(1); //so we don't jump 1 month later

  if (DP2_checkout_visible != null) {
    DP2_gotRoomsStatus = false;
    DP2_checkout.setYear(DP2_checkout_visible.getFullYear());
    DP2_checkout.setMonth(DP2_checkout_visible.getMonth());
  } else {
    DP2_checkout.setYear(DP2_checkin_visible.getFullYear());
    DP2_checkout.setMonth(DP2_checkin_visible.getMonth()+1);
  }
  DP2_checkout.setDate(1); //so we don't jump 1 month later

  var scwEle;
  if (dual_mode) scwEle = $(DP2_images[1]);
  else  scwEle = $(DP2_images[0]);

 

  DP2_buildTemplate();
	if (document.getElementById("img_" + DP2_oInput.id) != null)
		document.getElementById("img_" + DP2_oInput.id).src = "fileadmin/templates/images/img_cal_h.gif";

  return true;
}


/*
  function DP_buildTemplate(x_day, x_mon, x_year) - build date picker control template
  x_day, x_mon, x_year - default day, month & year
  return none
*/
function DP2_buildTemplate() {
  var html = '' +
    '       <div style="display:block; padding-right:10px;" id="dp2_layer1"><table border="0" cellpadding="0" cellspacing="0" class="dp2_tbl_head">' +
    '         <tr>' +
    '           <td width="30">' +
    '             <img src="fileadmin/templates/images/calendar/pfeil_links.png" height="12" width="10" style="cursor: pointer;" id="dp2_cal_mon1" onClick="DP2_setMonth(-1, true)">' +
    '           </td>' +
    '           <td align="center" height="20" valign="top" nowrap class="dp_head" id="DP2_cal1_head">' + DP_config[DP_lang + '_mons'][DP2_checkin.getMonth()] + ' ' + ((isMoz)?DP2_checkin.getYear()+1900:DP2_checkin.getYear()) + '</td>' +
    '           <td width="30" align="right">' +
    '             <img src="fileadmin/templates/images/calendar/pfeil_rechts.png" height="12" width="10" style="cursor: pointer;" id="dp2_cal_mon2" onClick="DP2_setMonth(1, true)">' +
    '           </td>' +
    '         </tr>' +
    '       </table>' +
    '       <table border="0" cellpadding="1" cellspacing="0" class="dp_body" id="dp2_cal_tbl1">' +
    '         <tr>';
  for (var i=0; i<7; i++) html += '<td class=dp_day_header>' + DP_config[DP_lang + '_days'][i] + '</td>';
  html += '       </tr>' +
    '         <tr>' +
    '           <td colspan="7" class=dp_day_disabled>&nbsp;</td>' +
    '         </tr>';
  for (var i=0; i<6; i++) html += '<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
  html += '' +
    '       </table></div>' +
    '' +
    '        <div style="padding-right:10px; display:none;" id="dp2_layer2"><table border="0" cellpadding="0" cellspacing="0" class="dp2_tbl_head">' +
    '         <tr>' +
    '           <td width="30">' +
    '             <img src="fileadmin/templates/images/calendar/pfeil_links.png" height="12" width="10" style="cursor: pointer;" id="dp2_cal_mon3" onClick="DP2_setMonth(-1, false)">' +
    '           </td>' +
    '           <td align="center" height="20" valign="top" nowrap class="dp_head">' + DP_config[DP_lang + '_mons'][DP2_checkout.getMonth()] + ' ' + ((isMoz)?DP2_checkout.getYear()+1900:DP2_checkout.getYear()) + '</td>' +
    '           <td width="30" align="right">' +
    '             <img src="fileadmin/templates/images/calendar/pfeil_rechts.png" height="12" width="10" style="cursor: pointer;" id="dp2_cal_mon4" onClick="DP2_setMonth(1, false)">' +
    '           </td>' +
    '         </tr>' +
    '       </table>' +
    '       <table border="0" cellpadding="1" cellspacing="0" class="dp_body" id="dp2_cal_tbl2">' +
    '         <tr>';
  for (var i=0; i<7; i++) html += '<td class=dp_day_header>' + DP_config[DP_lang + '_days'][i] + '</td>';
  html += '       </tr>' +
    '         <tr>' +
    '           <td colspan="7" class=dp_day_disabled>&nbsp;</td>' +
    '         </tr>';
  for (var i=0; i<6; i++) html += '<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
  html += '       </table></div>' ;
  
    DP2_oDiv.innerHTML = html;

    DP2_displayCalendar('dp2_cal_tbl1', DP2_checkin.getDate(), DP2_checkin.getMonth(), DP2_checkin.getFullYear(), true);
    DP2_displayCalendar('dp2_cal_tbl2', DP2_checkout.getDate(), DP2_checkout.getMonth(), DP2_checkout.getFullYear(), false);

    $('dp2_layer1').setStyle({
        display: 'inline',
        'float': 'left' 
    });
    $('dp2_layer2').style.display = 'block';
    //$('dp2_cal_mon1').style.display = 'none';
    //$('dp2_cal_mon2').style.display = 'none';


    
    
  if (!DP2_nextMonEnabled[1]) $('dp2_cal_mon4').style.display = 'none';
  if (!DP2_nextMonEnabled[0]) $('dp2_cal_mon2').style.display = 'none';
  //if (!DP2_lastMonEnabled[1]) 
  $('dp2_cal_mon1').style.display = DP2_lastMonEnabled[0] ? 'block' : 'none';
  $('dp2_cal_mon3').style.display = DP2_lastMonEnabled[1] ? 'block' : 'none';
  //if (!DP2_lastMonEnabled[0]) $('dp2_cal_mon1').style.display = 'none';
  
  return true;
}

/*
  function DP_displayCalendar(x_day, x_month, x_year) - fill date picker control template
  x_day, x_mon, x_year - default day, month & year
  return none
*/
function DP2_displayCalendar(obj_id, x_day, x_month, x_year, is_checkin) {

  month = parseInt(x_month);
  year = parseInt(x_year);
  day = x_day;

  var CalTable = $(obj_id);
  var days = DP_getDaysInMonth(month + 1, year);
  if (month > 1) var days_before = DP_getDaysInMonth(month, year);
  else var days_before = DP_getDaysInMonth(12, year-1);
  var curr_day = 0;
  var firstOfMonth = new Date(year, month, 1);
  var startingPos = firstOfMonth.getDay();
  (startingPos > 0) ? startingPos-- : startingPos = 6;

  var tmp = new Date();

  var str_day;
  var str_month = x_month + 1;
  var str_year = x_year;
  if (str_month < 10) str_month = "0" + str_month;
  str_value = str_year + '' + str_month;

  var room_state = -1;

  for (i=0; i<42; i++) {
    curr_day = i - startingPos + 1;

    if (curr_day <= 0) {
      CalTable.rows[Math.floor(i/7)+2].cells[i%7].innerHTML = '&nbsp;';
      CalTable.rows[Math.floor(i/7) + 2].cells[i % 7].className="dp_day_disabled";
    } else if (curr_day > 0 && curr_day <= days) { //this month

      CalTable.rows[Math.floor(i/7)+2].cells[i%7].innerHTML = curr_day;


      if ((DP_today.getDate() > curr_day) && (DP_today.getMonth() == x_month) && (DP_today.getFullYear() == x_year)) {
        (CalTable.rows[Math.floor(i/7) + 2].cells[i % 7].cellIndex < 5) ? CalTable.rows[Math.floor(i/7) + 2].cells[i % 7].className="dp_day_disabled":CalTable.rows[Math.floor(i/7) + 2].cells[i % 7].className="dp_day_off_disabled";
      } else {
        (CalTable.rows[Math.floor(i/7) + 2].cells[i % 7].cellIndex < 5) ? CalTable.rows[Math.floor(i/7) + 2].cells[i % 7].className="dp_day":CalTable.rows[Math.floor(i/7) + 2].cells[i % 7].className="dp_day_off";

        if (is_checkin) {
          CalTable.rows[Math.floor(i/7)+2].cells[i%7].onclick=DP2_eventHandlerInClick;
          CalTable.rows[Math.floor(i/7)+2].cells[i%7].ondblclick=DP2_eventHandlerInClick;
        } else {
          CalTable.rows[Math.floor(i/7)+2].cells[i%7].onclick=DP2_eventHandlerOutClick;
          CalTable.rows[Math.floor(i/7)+2].cells[i%7].ondblclick=DP2_eventHandlerOutClick;
        }
      }
    } else if (curr_day > days) {
      CalTable.rows[Math.floor(i/7)+2].cells[i%7].innerHTML = '&nbsp;';
      CalTable.rows[Math.floor(i/7) + 2].cells[i % 7].className="dp_day_disabled";
    }

    if ((DP2_checkin_visible.getDate() == curr_day) && (DP2_checkin_visible.getMonth() == x_month) && (DP2_checkin_visible.getFullYear() == x_year)) {
      CalTable.rows[Math.floor(i/7)+2].cells[i%7].className="dp_day_checkin";
      //if (isMoz) CalTable.rows[Math.floor(i/7)+2].cells[i%7].style.backgroundImage = "url('fileadmin/templates/images/calendar/Booking_Process_071019_03_ff.gif')";
    }

    if (DP2_checkout_visible != null) {

      if ((DP2_checkout_visible.getDate() == curr_day) && (DP2_checkout_visible.getMonth() == x_month) && (DP2_checkout_visible.getFullYear() == x_year)) {
        CalTable.rows[Math.floor(i/7)+2].cells[i%7].className="dp_day_checkout";
        //if (isMoz) CalTable.rows[Math.floor(i/7)+2].cells[i%7].style.backgroundImage = "url('fileadmin/templates/images/calendar/Booking_Process_071019_05_ff.gif')";
      }

      tmp.setDate(curr_day);
      tmp.setMonth(x_month);
      tmp.setYear(x_year);

      if ((tmp > DP2_checkin_visible) && (tmp < DP2_checkout_visible) && (curr_day > 0) && (curr_day <= days)) {
        if (CalTable.rows[Math.floor(i/7)+2].cells[i%7].className != 'dp_day_checkin')
        CalTable.rows[Math.floor(i/7)+2].cells[i%7].className = "dp_day_selected";
      }
    }

    if (DP2_gotRoomsStatus) {
      (curr_day.toString().length==1) ? str_day="0" + curr_day : str_day = curr_day;
      eval('room_state = DP2_roomsStatus.date' + str_value + str_day);
      if ((typeof(room_state) != "undefined") && (!isNaN(room_state))) {
        Element.extend(CalTable.rows[Math.floor(i/7)+2].cells[i%7]);
        CalTable.rows[Math.floor(i/7)+2].cells[i%7].setStyle({'color':DP_dayColors[room_state]});
      }
    }

  }
  return true;
}

function compareDates(d1,d2,checkDays) {
    if (d1.getYear() != d2.getYear()) {
        return d1.getYear() < d2.getYear() ? -1 : 1;
    }    
    if (d1.getMonth() != d2.getMonth()) {
        return d1.getMonth() < d2.getMonth() ? -1 : 1;        
    }
    if (!checkDays) {
        return 0;
    }
    if (d1.getDate() != d2.getDate()) {
        return d1.getDate() < d2.getDate() ? -1 : 1;        
    }
    return 0;
    
}

/*
  function DP_setMonth(val) - change date picker control date
  val - integer defines number of month to add to currently selected month
  return none
*/
function DP2_setMonth(val, is_checkin) {
    console.log(DP2_dualMode); 


  if (!isNaN(val)) {
    if (DP2_dualMode) {
      if (val == 1) { //this is next mon click
        var x_month = DP2_checkout.getMonth();
        var i = x_month + 1;
        x_month = i % 12;
        if (x_month < 0) x_month = x_month + 12;
        year = DP2_checkout.getFullYear() + Math.floor(i/12);

        if ((is_checkin) && ((DP2_checkin.getMonth() != DP2_checkin_visible.getMonth()) || (DP2_checkin.getYear() != DP2_checkin_visible.getYear()))) {
          DP2_checkin.setYear(year);
          DP2_checkin.setMonth(x_month);
        }

        DP2_checkout.setMonth(x_month);
        DP2_checkout.setYear(year);

      } else { //this is previous month click on the first cal
        //if it will be the same month & year - hide second calc
        //else update the date
        var x_month = DP2_checkout.getMonth();
        var i = x_month - 1;
        x_month = i % 12;
        if (x_month < 0) x_month = x_month + 12;
        year = DP2_checkout.getFullYear() + Math.floor(i/12);

        DP2_checkout.setMonth(x_month);
        DP2_checkout.setYear(year);

        if (is_checkin) {
          DP2_checkin.setYear(year);
          DP2_checkin.setMonth(x_month);          
        }
      }

     
        
      DP2_nextMonEnabled[1] = !((DP2_checkout.getMonth() == DP_today.getMonth()) && (DP2_checkout.getYear() == DP_today.getYear() + 2));
      DP2_lastMonEnabled[0] = ((DP2_checkin.getMonth() > DP_today.getMonth()) && (DP2_checkin.getYear() >= DP_today.getYear() ));
      DP2_lastMonEnabled[1] = ((DP2_checkout.getMonth() > DP_checkin.getMonth()) && (DP2_checkout.getYear() >= DP_checkin.getYear() )); 
     
      
      DP2_buildTemplate();
    } else { //update chekin cal
      var x_month = DP2_checkin.getMonth();
      var i = x_month + val;
      x_month = i % 12;
      if (x_month < 0) x_month = x_month + 12;
      year = DP2_checkin.getFullYear() + Math.floor(i/12);

      DP2_checkin.setMonth(x_month);
      DP2_checkin.setYear(year);
      
      if (compareDates(DP2_checkin,DP2_checkout) >= 0) {        
        DP2_checkout.setMonth(DP2_checkin.getMonth()+1);
      }      

      DP2_nextMonEnabled[0] = !((DP2_checkin.getMonth() == DP_today.getMonth()) && (DP2_checkin.getYear() == DP_today.getYear() + 2));

      DP2_lastMonEnabled[0] = ((DP2_checkin.getMonth() > DP_today.getMonth()) && (DP2_checkin.getYear() >= DP_today.getYear() ));
      DP2_lastMonEnabled[1] = ((DP2_checkout.getMonth() > DP_today.getMonth()) && (DP2_checkout.getYear() >= DP_today.getYear() ));
      
      DP2_buildTemplate();
    }
    
  }
}
 
//for chekin calc only
function DP2_eventHandlerInClick(anEvObj) {
  var date = parseInt(this.innerHTML);
  console.log(date);
  var str_day;
  var str_month;
  var str_year;
  if (DP2_dualMode) {
    DP2_checkout_visible = new Date(DP2_checkin.getFullYear(),DP2_checkin.getMonth(),date );
    $('dt_checkout').value = DP2_oInput.value = dateToString(DP2_checkout_visible);
    
    DP2_buildTemplate();
  } else {
    DP2_checkin_visible = new Date(DP2_checkin.getFullYear(),DP2_checkin.getMonth(),date);
    $('dt_checkin').value = DP2_oInput.value = dateToString(DP2_checkin_visible);
    
    DP2_gotRoomsStatus = false;
    DP2_openCalendar($(DP2_elements[1]), true);
    
  }
  return true;
}

//for chekout calc only
function DP2_eventHandlerOutClick(anEvObj) {
  var date = parseInt(this.innerHTML);
  var str_day;
  var str_month;
  var str_year;
  if (DP2_dualMode) {
    DP2_checkout_visible = new Date(DP2_checkout.getFullYear(),DP2_checkout.getMonth(),date);
    $('dt_checkout').value = DP2_oInput.value= dateToString(DP2_checkout_visible);
    DP2_gotRoomsStatus = false;
    DP2_buildTemplate();
  }
  return true;
}