//Language configuration. Add add\remove any, 3 arrays must be added
var DP_config = ({
  'en_days' : [ 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN' ],
  'en_mons' : [ 'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY',
            'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER' ],
  'en_misc' : [ 'RESET', 'Close', 'There was an error while checking availability. Please, try again later' ],

  'de_days' : [ 'MON', 'DIE', 'MIT', 'DON', 'FRE', 'SAM', 'SON' ],
  'de_mons' : [ 'JANUAR', 'FEBRUAR', 'M&#228;RZ', 'APRIL', 'MAI', 'JUNI',
            'JULI', 'AUGUST', 'SEPTEMBER', 'OKTOBER', 'NOVEMBER', 'DEZEMBER' ],
  'de_misc' : [ 'ZURÜCKSETZEN', 'Schließen', 'Fehler beim Prüfen der Verfügbarkeit. Versuchen Sie bitte später noch einmal' ],

  'fr_days' : [ 'LUN', 'MAR', 'MER', 'JEU', 'VEN', 'SAM', 'DIM' ],
  'fr_mons' : [ 'JANVIER', 'F&#233;VRIER', 'MARS', 'AVRIL', 'MAI', 'JUIN',
            'JUILLET', 'AOUT', 'SEPTEMBRE', 'OCTOBRE', 'NOVEMBRE', 'D&#233;CEMBRE' ],
  'fr_misc' : [ 'RÉINITIALISATION', 'Fermer', '' ],

  'es_days' : [ 'LUN', 'MAR', 'MIE', 'JUE', 'VIE', 'S&#224;B', 'DOM' ],
  'es_mons' : [ 'ENERO', 'FEBRERO', 'MARZO', 'ABRIL', 'MAYO', 'JUNIO',
            'JULIO', 'AGOSTO', 'SEPTIEMBRE', 'OCTUBRE', 'NOVIEMBRE', 'DICIEMBRE' ],
  'es_misc' : [ 'BORRAR TODO', 'Cierre', '' ],

  'it_days' : [ 'LUN', 'MAR', 'MER', 'GIO', 'VEN', 'SAB', 'DOM' ],
  'it_mons' : [ 'GENNAIO', 'FEBBRAIO', 'MARZO', 'APRILE', 'MAGGIO', 'GIUGNO',
            'LUGLIO', 'AGOSTO', 'SETTEMBRE', 'OTTOBRE', 'NOVEMBRE', 'DICEMBRE' ],
  'it_misc' : [ 'CANCELLA', 'Fine', '' ],

  'pt_days' : [ 'SEG', 'TER', 'QUA', 'QUI', 'SEX', 'S&#225;', 'DOM' ],
  'pt_mons' : [ 'JANEIRO', 'FEVEREIRO', 'MAR&#231;O', 'ABRIL', 'MAIO', 'JUNHO',
            'JULHO', 'AGOSTO', 'SETEMBRO', 'OUTUBRO', 'NOVEMBRO', 'DEZEMBRO' ],
  'pt_misc' : [ 'RESET', 'Fim', '' ],


  'ru_days' : [ 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN' ],
  'ru_mons' : [ 'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY',
            'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER' ],
  'ru_misc' : [ 'Сбросить', 'Close', 'There was an error while checking availability. Please, try again later' ],

  'cn_days' : [ 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN' ],
  'cn_mons' : [ 'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY',
            'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER' ],
  'cn_misc' : [ '重置', 'Close', 'There was an error while checking availability. Please, try again later' ],

  'jp_days' : [ 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN' ],
  'jp_mons' : [ 'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY',
            'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER' ],
  'jp_misc' : [ 'リセット ', 'Close', 'There was an error while checking availability. Please, try again later' ]
});


function dateToString(dateObject) {
    str_day= ''+dateObject.getDate();
    str_day = (str_day.length==1) ? '0' + str_day : str_day;
    str_month = dateObject.getMonth()+1;
    str_month = str_month < 10  ? "0" + str_month : str_month;
    str_year = dateObject.getFullYear();

    if (window.dateFormat == 'new') {
        return str_day + '/' + str_month + '/' + str_year; 
    } else {
        return str_year+ '-' + str_month + '-' + str_day; 
    }    
}


//language to use
var DP_lang = 'en';
//DP input ids must be saved here
var DP_elements = new Array('dt_checkin', 'dt_checkout');
//DP images ids must be saved here
var DP_images = new Array('img_dt_checkin', 'img_dt_checkout');

//Colors of different day states
var DP_dayColors = new Array(
  '#99CC67', // available
  '#FF6600', // restrictions only
  '#800315' // no free rooms
);

//isMozilla browser
var isMoz = (window.ActiveXObject)?false:true;

//Input element handler to write date
var DP_oInput = null;
//Dual mode flag (used for check-out dates)
var DP_dualMode = false;
//Check-out date is not set by default
var DP_checkouSet = false;
//Rooms availability data
var DP_roomsStatus = new Object();
var DP_gotRoomsStatus = true;
var DP_nextMonEnabled = new Array(true, true);

//Shield layer to handle clicking on the page (browser compatibility issue)
var DP_oShield = document.createElement("DIV");
DP_oShield.style.position = 'absolute';
DP_oShield.style.top = '100px';
DP_oShield.style.left = '0px';
DP_oShield.style.width = '100%';
DP_oShield.style.height = '100%';
DP_oShield.style.filter = 'Alpha(Opacity=0)';
DP_oShield.style.opacity = 0;
DP_oShield.style.zIndex = 300;
DP_oShield.style.backgroundColor = 'black';
DP_oShield.onclick = DP_close;

//initializing dates
var DP_today = new Date();
var DP_checkin = new Date();
var DP_checkout = new Date();
var DP_checkin_visible = new Date();
var DP_checkout_visible = null;

//Date picker layer
var DP_oDiv = document.createElement("DIV");
DP_oDiv.id = 'dp_root_layer';
DP_oDiv.className = "dp_tbl_root";
DP_oDiv.style.position = "absolute";
DP_oDiv.style.top = "550px";
DP_oDiv.style.left = "550px";
DP_oDiv.style.zIndex = 301;

//DP inputs initial values ids saved here
var DP_values = new Array('-', '-');

//Legend DIV id
legend = "calendar_explanation";


function jgi_openCalendar(target, dual_mode, x_l, x_t, mirror){
  if(mirror === undefined)
    mirror = false;
      
	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/; // dd/mm/yyyy
	
	var isCheckinSet = re.test($(DP_elements[0]).value);
	var isCheckoutSet = re.test($(DP_elements[1]).value);
	
	if(target.id == $(DP_elements[1]).id && isCheckinSet){
		DP_oInput = $(DP_elements[1]);
		DP_openCalendar(DP_oInput, true, 0, 0, mirror);
	}
	else  {
		DP_oInput = $(DP_elements[0]);
		DP_openCalendar(DP_oInput, false, 0, 0, mirror);
	}
	return;
}

function jgi_openCalendarExtended(checkin, checkout, image_checkin, image_checkout, dual_mode, x_l, x_t) {	
	DP_elements = [$(checkin), $(checkout)];
	
	if(image_checkin  != null) DP_images[0] = $(image_checkin);
	if(image_checkout  != null) DP_images[1] = $(image_checkout);
	
	jgi_openCalendar(checkin, dual_mode, x_l, x_t);
}


/*
  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 DP_openCalendar(obj, dual_mode, x_l, x_t, mirror) {
  if(mirror === undefined)
    mirror = false;
    

  DP_oInput = obj;
  DP_dualMode = dual_mode;

  if (DP_values[0] == '-') { //save initial input values
    DP_values[0] = $(DP_elements[0]).value;
    DP_values[1] = $(DP_elements[1]).value;
  }

  //syncronize visible & working date as we will be drawing controls by working dates
  DP_checkin.setYear((DP_checkin_visible.getFullYear()))
  DP_checkin.setMonth(DP_checkin_visible.getMonth());
  DP_checkin.setDate(1); //so we don't jump 1 month later

  if (DP_checkout_visible != null) {
    DP_gotRoomsStatus = false;    
    DP_checkout.setYear(DP_checkout_visible.getFullYear());
    DP_checkout.setMonth(DP_checkout_visible.getMonth());
  } else {
    DP_checkout.setYear(DP_checkin_visible.getFullYear());
    DP_checkout.setMonth(DP_checkin_visible.getMonth());
  }
  DP_checkout.setDate(1); //so we don't jump 1 month later

  var scwEle;
  if (dual_mode) scwEle = $(DP_images[1]);
  else  scwEle = $(DP_images[0]);

  //jgi
  if(scwEle == null) scwEle = $(obj);
  
  scwTargetEle = scwEle;


  var obj_helper = scwEle;
  var ol = 0;	
  var ot = 0;
  
  if (obj_helper.offsetParent) {
    do {
      ol += obj_helper.offsetLeft;
      ot += obj_helper.offsetTop;
    }while (obj_helper = obj_helper.offsetParent);
  };

/*
  var offsetTop = parseInt(scwEle.offsetTop, 10);
  var offsetLeft = parseInt(scwEle.offsetLeft, 10); */

  var offsetTop = parseInt(ot, 10);
  var offsetLeft = parseInt(ol, 10)  - (mirror ? 344 : 0);
  


  if (!window.opera) {
    while (scwEle.tagName != 'BODY' && scwEle.tagName != 'HTML') {
      offsetTop -= parseInt(scwEle.scrollTop, 10);
      offsetLeft -= parseInt(scwEle.scrollLeft, 10);
      scwEle = scwEle.parentNode;
    }
    scwEle = scwTargetEle;
  }
/*	
  do {
    scwEle = scwEle.offsetParent;
    offsetTop += parseInt(scwEle.offsetTop, 10);
    offsetLeft += parseInt(scwEle.offsetLeft, 10) - (mirror ? 344 : 0);
  }
  while (scwEle.tagName != 'BODY' && scwEle.tagName != 'HTML');
*/
  DP_oDiv.style.top = (offsetTop + 2) + 'px';
  DP_oDiv.style.left = (offsetLeft + 2) + 'px';
  DP_oDiv.style.margin = '12px 0 0 18px'; 
  DP_oDiv.style.margin = '0 0 0 5px';
  
  //show layers
  document.body.appendChild(DP_oShield);
  document.body.appendChild(DP_oDiv);

  DP_buildTemplate(mirror);
	/*
	
	if (document.getElementById("img_" + DP_oInput.id) != null)
		document.getElementById("img_" + DP_oInput.id).src = "fileadmin/templates/images/img_cal_h.gif";
		
	*/

  return true;
}

/*
  function DP_close() - close date picker control
  return none
*/
function DP_close(evt) {
  //remove layers
  document.body.removeChild(DP_oDiv);
  document.body.removeChild(DP_oShield);

	/*if (document.getElementById("img_" + DP_oInput.id) != null)
		document.getElementById("img_" + DP_oInput.id).src = "fileadmin/templates/images/img_cal.gif";*/


  //might be will need to show new calendar here
  //get imput coordinate (from to &)
/*
if (evt != -1) {
    if (isMoz) event = evt;
    var element = $(DP_elements[0];
    if (Position.within(element, Event.pointerX(event), Event.pointerY(event))) DP_openCalendar(element, false);

    element = $(DP_elements[1]);
    if (Position.within(element, Event.pointerX(event), Event.pointerY(event))) DP_openCalendar(element, true);
  }
  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
*/

var globalMirror = false; //sorry found no other fast way

function DP_buildTemplate(mirror) {
  if(mirror === undefined)
    mirror = false;  

  globalMirror = mirror;

 var html = '' +
    ' <table border="0" cellpadding="0" cellspacing="0" class="dp_tbl_root" >' +
    '   <tr>' +
    '     <td valign="top" id="dp_td_top_left"><img border="0" src="fileadmin/templates/images/calendar/Booking_Process_071019_' + (mirror ? '32_m' : '07') + '.png" width="44" height="42"></td>' +
    '     <td rowspan="2" valign="top" id="dp_td_top_center" nowrap="true">' +
    '       <div style="display:block; padding-right:10px;" id="dp_layer1"><table border="0" cellpadding="0" cellspacing="0" id="dp_tbl_head">' +
    '         <tr>' +
    '           <td width="30">' +
    '             <img src="fileadmin/templates/images/calendar/pfeil_links.png" height="12" width="10" style="cursor: pointer;" id="dp_cal_mon1" onClick="DP_setMonth(-1, true)">' +
    '           </td>' +
    '           <td align="center" height="20" valign="top" nowrap class="dp_head" id="DP_cal1_head">' + DP_config[DP_lang + '_mons'][DP_checkin.getMonth()] + ' ' + (DP_checkin.getFullYear()) + '</td>' +
    '           <td width="30" align="right">' +
    '             <img src="fileadmin/templates/images/calendar/pfeil_rechts.png" height="12" width="10" style="cursor: pointer;" id="dp_cal_mon2" onClick="DP_setMonth(1, true)">' +
    '           </td>' +
    '         </tr>' +
    '       </table>' +
    '       <table border="0" cellpadding="1" cellspacing="0" class="dp_body" id="dp_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="dp_layer2"><table border="0" cellpadding="0" cellspacing="0" id="dp_tbl_head">' +
    '         <tr>' +
    '           <td width="30">' +
    '             <img src="fileadmin/templates/images/calendar/pfeil_links.png" height="12" width="10" style="cursor: pointer;" id="dp_cal_mon3" onClick="DP_setMonth(-1, false)">' +
    '           </td>' +
    '           <td align="center" height="20" valign="top" nowrap class="dp_head">' + DP_config[DP_lang + '_mons'][DP_checkout.getMonth()] + ' ' + (DP_checkout.getFullYear()) + '</td>' +
    '           <td width="30" align="right">' +
    '             <img src="fileadmin/templates/images/calendar/pfeil_rechts.png" height="12" width="10" style="cursor: pointer;" id="dp_cal_mon4" onClick="DP_setMonth(1, false)">' +
    '           </td>' +
    '         </tr>' +
    '       </table>' +
    '       <table border="0" cellpadding="1" cellspacing="0" class="dp_body" id="dp_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>' +
    '       <!-- <img alt="' + DP_config[DP_lang + '_misc'][0] + '"src="fileadmin/templates/images/calendar/Booking_Process_071019_27.png" height="18" width="100" class="dp_btn_aviliable" id="dp_btn_avaliable" onClick="DP_checkAvailiability()" style="display:' + ((DP_gotRoomsStatus)?'block/*none*/':'block') + '"> -->' +
    '         <div title="' + DP_config[DP_lang + '_misc'][0] + '" align="right" onClick="DP_checkAvailiability(' + ((DP_gotRoomsStatus)?'true':'') + ')" id="dp_btn_avaliable" class="dp_div_available" style="display:' + ((DP_gotRoomsStatus)?'block/*none*/':'block') + '"><div align="right"><table border="0" cellpadding="0" cellspacing="0"><tr><td><img src="fileadmin/templates/images/calendar/Booking_Process_071019_01ca.png"></td><td class="dp_button_available" nowrap="true">' + DP_config[DP_lang + '_misc'][0] + '<td><img src="fileadmin/templates/images/calendar/Booking_Process_071019_03ca.png"></td></tr></table></div></div>' +
    '     </td>' +
    '     <td id="dp_td_top_right' + (mirror ? '_mirror' : '') + '">' +
    '       <img title="' + DP_config[DP_lang + '_misc'][1] + '" onClick="DP_close()" class="dp_btn_close" src="fileadmin/templates/images/calendar/Booking_Process_0710191_16.png" height="17" widht="19">' +
    '     </td>' +
    '   </tr>' +
    '   <tr>' +
    '     <td id="dp_td_middle_left">&nbsp;</td>' +
    '     <td id="dp_td_middle_right">&nbsp;</td>' +
    '   </tr>' +
    '   <tr>' +
    '     <td width="44" height="24"><img border="0" src="fileadmin/templates/images/calendar/Booking_Process_071019_32.png" width="44" height="24"></td>' +
    '     <td id="dp_td_bottom_center"><img id="img_width" src="fileadmin/templates/images/calendar/1x1.gif" height="24" widht="262"></td>' +
    '     <td width="24" height="24"><img border="0" src="fileadmin/templates/images/calendar/Booking_Process_071019_37.png" width="24" height="24"></td>' +
    '   </tr>' +
    ' </table>';
  DP_oDiv.innerHTML = html;
	
	if ((DP_dualMode) && (((DP_checkout.getMonth() > DP_checkin.getMonth()) && (DP_checkout.getYear() == DP_checkin.getYear())) || ((DP_checkout.getYear() > DP_checkin.getYear())))) {
	
		//console.log('if 1');
		//2 calendars
		DP_displayCalendar('dp_cal_tbl1', DP_checkin.getDate(), DP_checkin.getMonth(), DP_checkin.getFullYear(), true);
		//first with checkin & second with checkout
		DP_displayCalendar('dp_cal_tbl2', DP_checkout.getDate(), DP_checkout.getMonth(), DP_checkout.getFullYear(), false);

		$('dp_layer1').setStyle({
			display: 'inline',
			'float': 'left'
		});
		$('dp_layer2').style.display = 'block';
		$('img_width').style.width = '500px';
		$('dp_cal_mon2').style.display = 'none';
		if (!isMoz) $('dp_btn_avaliable').style.right = '0px';

		$('dp_cal_mon2').style.display = 'none';
		$('dp_cal_mon1').style.display = 'none';
	} 
	else {
		//JGI: THIS HAPPENS FOR BOTH
		//1 calendar only
		//we should decide here, what should this calendar to show. First cal is showing checkin date normally, but what if it it is later than checkout
		if (((DP_checkout.getMonth() < DP_checkin.getMonth()) && (DP_checkout.getYear() == DP_checkin.getYear())) || ((DP_checkout.getYear() < DP_checkin.getYear()))) {
			//JGI: THIS NEVER HAPPENS?
			DP_checkout.setYear(DP_checkin.getFullYear());
			DP_checkout.setMonth(DP_checkin.getMonth());
		}
		
		var ischeckin = !(DP_oInput.id == $(DP_elements[1]).id);
		
		if($('dp_cal_tbl1') == null) return;
		DP_displayCalendar('dp_cal_tbl1', DP_checkin.getDate(), DP_checkin.getMonth(), DP_checkin.getFullYear(), ischeckin);

		if (DP_today.getYear() < DP_checkin.getYear()) $('dp_cal_mon1').style.display = 'block';
		else if (DP_today.getMonth() < DP_checkin.getMonth()) $('dp_cal_mon1').style.display = 'block';
		else $('dp_cal_mon1').style.display = 'none';
	}
	
	if (!DP_nextMonEnabled[1]) $('dp_cal_mon4').style.display = 'none';
	if (!DP_nextMonEnabled[0]) $('dp_cal_mon2').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 DP_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=DP_eventHandlerInClick;
          CalTable.rows[Math.floor(i/7)+2].cells[i%7].ondblclick=DP_eventHandlerInClick;
        } else {
          CalTable.rows[Math.floor(i/7)+2].cells[i%7].onclick=DP_eventHandlerOutClick;
          CalTable.rows[Math.floor(i/7)+2].cells[i%7].ondblclick=DP_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 ((DP_checkin_visible.getDate() == curr_day) && (DP_checkin_visible.getMonth() == x_month) && (DP_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 (DP_checkout_visible != null) {

      if ((DP_checkout_visible.getDate() == curr_day) && (DP_checkout_visible.getMonth() == x_month) && (DP_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 > DP_checkin_visible) && (tmp < DP_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 (DP_gotRoomsStatus) {
      (curr_day.toString().length==1) ? str_day="0" + curr_day : str_day = curr_day;
      eval('room_state = DP_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 DP_setMonth(val) - change date picker control date
  val - integer defines number of month to add to currently selected month
  return none
*/
function DP_setMonth(val, is_checkin) {
  if (!isNaN(val)) {
    if (DP_dualMode) {
      if (val == 1) { //this is next mon click
        var x_month = DP_checkout.getMonth();
        var i = x_month + 1;
        x_month = i % 12;
        if (x_month < 0) x_month = x_month + 12;
        year = DP_checkout.getFullYear() + Math.floor(i/12);

        if ((is_checkin) && ((DP_checkin.getMonth() != DP_checkin_visible.getMonth()) || (DP_checkin.getYear() != DP_checkin_visible.getYear()))) {
          DP_checkin.setYear(year);
          DP_checkin.setMonth(x_month);
        }

        DP_checkout.setMonth(x_month);
        DP_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 = DP_checkout.getMonth();
        var i = x_month - 1;
        x_month = i % 12;
        if (x_month < 0) x_month = x_month + 12;
        year = DP_checkout.getFullYear() + Math.floor(i/12);

        DP_checkout.setMonth(x_month);
        DP_checkout.setYear(year);

        if (is_checkin) {
          DP_checkin.setYear(year);
          DP_checkin.setMonth(x_month);
        }
      }

      if ((DP_checkout.getMonth() == DP_today.getMonth()) && (DP_checkout.getYear() == DP_today.getYear() + 2)) {
        DP_nextMonEnabled[1] = false;
      } else DP_nextMonEnabled[1] = true;

      DP_buildTemplate();
    } else { //update chekin cal
      var x_month = DP_checkin.getMonth();
      var i = x_month + val;
      x_month = i % 12;
      if (x_month < 0) x_month = x_month + 12;
      year = DP_checkin.getFullYear() + Math.floor(i/12);

      DP_checkin.setMonth(x_month);
      DP_checkin.setYear(year);

      if ((DP_checkin.getMonth() == DP_today.getMonth()) && (DP_checkin.getYear() == DP_today.getYear() + 2)) {
        DP_nextMonEnabled[0] = false;
      } else DP_nextMonEnabled[0] = true;

      DP_buildTemplate();
    }
  }
}

//for chekin calc only
function DP_eventHandlerInClick(anEvObj) {
  var date = parseInt(this.innerHTML);
  var str_day;
  var str_month;
  var str_year;
  
  if (DP_dualMode) {
    DP_checkout_visible = new Date(DP_checkin.getFullYear(),DP_checkin.getMonth(), date);
    DP_oInput.value= dateToString(DP_checkout_visible);
	//console.log(dp_oInput);
    $(DP_oInput).fire('wh:change');    
    //DP_buildTemplate();
  } else {
    DP_checkin_visible = new Date(DP_checkin.getFullYear(), DP_checkin.getMonth(), date);
    DP_oInput.value = dateToString(DP_checkin_visible);
    $(DP_oInput).fire('wh:change');
    DP_gotRoomsStatus = false;
    DP_close(-1); //close calendar
    DP_openCalendar($(DP_elements[1]), true, 0, 0, globalMirror);
	//JGI CHECK ABOVE LINES
	
	//console.log('if click open calendar');
    //DP_openCalendar($('dt_checkout'), true);
  }
  return true;
}
//for chekout calc only
function DP_eventHandlerOutClick(anEvObj) {
  var date = parseInt(this.innerHTML);
  var str_day;
  var str_month;
  var str_year;
  if (DP_dualMode) {
    DP_checkout_visible = new Date(DP_checkout.getFullYear(), DP_checkout.getMonth(), date);
    DP_oInput.value = dateToString(DP_checkout_visible);
    $(DP_oInput).fire('wh:change');
    DP_gotRoomsStatus = false;
	DP_close(-1); //close calendar
    //DP_buildTemplate();
  }
  return true;
}
/*

  function DP_getDaysInMonth(x_month, x_year) - get number of days in supplied month
  x_month, x_year - month & year integers
  returns number of days (int)
*/
function DP_getDaysInMonth(x_month, x_year) {
  var days;
  if (x_month == 1 || x_month == 3 || x_month == 5 || x_month == 7 || x_month == 8 || x_month == 10 || x_month == 12) days = 31;
  else if (x_month == 4 || x_month == 6 || x_month == 9 || x_month == 11) days = 30;
  else if (x_month == 2) {
    if (DP_isLeapYear(x_year)) days = 29;
    else days = 28;
  }
  return days;
}

/*
  function DP_isLeapYear(x_year) - get leap year state in supplied year
  x_year - year
  returns whether the year is a leap year (bool)
*/
function DP_isLeapYear(x_year) {
  if (((x_year % 4) == 0) && ((x_year % 100) != 0) || ((x_year % 400) == 0)) return true;
  else return false;
}

/*
  function myParseInt(in_str) - ParseInt function to handle MSIE parseInt bug
  in_str - string, that contains number, needs to be turned into integer
  return integer value
*/
function myParseInt(in_str) {
  var numbers = '0123456789';
  var out_str = '';
  var int_passed = false;
  var ch;
  var num_index;

  for (var i=0; i<in_str.length; i++) {
    ch = in_str.substring(i, i+1);
    num_index = numbers.indexOf(ch);
    if (num_index != -1) {
      if (num_index != 0) int_passed = true;
      if (((num_index == 0) && (int_passed)) || (num_index != 0)) out_str += ch;
    }
  }

  if (out_str == '') return 0;
  else return parseInt(out_str);
}

/*
  Make AJAX request for rooms statuses
*/
function DP_checkAvailiability(reopen) {
  /*
  Special fix for WorldHotels.com
  */
//var reopen = false;
//console.log('reset');
/*if(!DP_gotRoomsStatus){
	reopen = true;
	console.log('got no room status');
}*/
	DP_values[0] = $(DP_elements[0]).title;
	DP_values[1] = $(DP_elements[1]).title;

  $(DP_elements[0]).value = DP_values[0];
  $(DP_elements[1]).value = DP_values[1];

  DP_gotRoomsStatus = true;


//  objHotelName = document.getElementById('search_hotel_string');
//  if(objHotelName)
//    objHotelName.value = '';

  DP_checkin = new Date();
  DP_checkout = new Date();
  DP_checkin_visible = new Date();
  DP_checkout_visible = null;

  DP_close(0); //close calendar

if(reopen){
	//console.log('reopen here', 'mit window', window.DP_elements);
	//jgi_openCalendar($('dt_checkin'), false);
return;
}
  //DP_openCalendar($(DP_elements[0]), false);
  //DP_openCalendar($('dt_checkin'), false);

  /*
  var opt = {
      method: 'post', postBody: '', onSuccess: DP_gotAvailiability,
      on404: function(t) { return true; }, // Handle 404
      onFailure: DP_failureAvailiability, // Handle other errors
      onException: DP_failureAvailiability
  }
  new Ajax.Request('dates.php', opt);
  legendelayer = document.getElementById(legend);
    legendelayer.style.visibility = "visible";
    */
}

/*
  AJAX request for rooms statuses callback
*/
function DP_gotAvailiability(t) {
  DP_roomsStatus = t.responseText.evalJSON();
  DP_gotRoomsStatus = true;
  DP_buildTemplate();
}

/*
  AJAX request for rooms statuses callback error
*/
function DP_failureAvailiability(t, e) {
  alert(DP_config[DP_lang + '_misc'][2]);
  return true;
}

function writeMonth(value) {
  var new_value = '';
  for (var i=0; i<value.length; i++) {
    ch = value.substring(i, i+1);
    if (i == 0) ch = ch.toUpperCase();
    else ch = ch.toLowerCase();
    new_value += ch;
  }
  return new_value;
}

