var firstClick = true;
var secondClick = true;
var checkinDate = null;

function resetCalendar(selectBox, hotelCode)
{	
	var mainContainer   = crs.getRequiredElement('mainContainer');
	var loading   		= crs.getRequiredElement('loading');
	loading.style.display = 'block';
	mainContainer.style.backgroundColor = '#FFFFD4';

	var url = "/book/book/RefreshCalendar?monthAndYear=" + selectBox + "&hotelCode=" + hotelCode;
	window.location = url;
}

function clickedDay(date, month, year, totalDays, now)
{	
	var	ie 			= document.all;
	var firstDay 	= getDateFormatMMDDYYYYWithSlash(date, month, year);
	var secDayStr 	= new Date(firstDay);
	secDayStr.setDate(date + 1);
	var year = secDayStr.getYear();
	
	if(!ie){
		year = secDayStr.getYear() + 1900;
	}
	
	var secondDay 	= getDateFormatMMDDYYYYWithSlash(secDayStr.getDate(), secDayStr.getMonth() + 1, year);

	if(firstClick){
		parent.document.wizardForm.checkinDate.value = firstDay;
		parent.document.wizardForm.checkoutDate.value = secondDay;
		firstClick = false;
	}else if(secondClick){
		parent.document.wizardForm.checkoutDate.value = firstDay;
		secondClick = false;
	}else if(!firstClick && !secondClick){
		parent.document.wizardForm.checkinDate.value = firstDay;
		parent.document.wizardForm.checkoutDate.value = secondDay;
		firstClick = true;
		secondClick = true;
	}

	this.highlightStayDateCell(totalDays, now);
}

function leadZero(value)
{
	return ((value < 10) ? '0' + value : value);
}

function getDateFormatMMDDYYYYWithSlash(date, month, year)
{
	return (leadZero(month) + '/' + leadZero(date) + '/' + year);
}

function highlightStayDateCell(totalDays, now)
{
	var checkin 	= parent.document.wizardForm.checkinDate.value;
	var checkout 	= parent.document.wizardForm.checkoutDate.value;
	
	if(checkin > checkout) {
		alert("Departure should be equal or after Arrival");
      	parent.document.wizardForm.checkoutDate.select();
	}
	
	now = now.split('/');
	now = getDateFormatMMDDYYYYWithSlash(now[0], now[1], now[2]);
	
	now = new Date(now);
	var i = 0; 

	while(i < totalDays) { 

		now = getDateFormatMMDDYYYYWithSlash(now.getDate(), now.getMonth()+1, now.getFullYear());
		var cell = document.getElementById(now);
		
		if (cell != null) cell.style.border = "0px";
		
		now = new Date(now);
		now.setDate(now.getDate() + 1);
		
		i++;
	}	

	while(checkin  <= checkout) {

		var cell = document.getElementById(checkin);
	
		if (cell != null) cell.style.border = "1px solid #02900A";
		else break;
			
		checkin = new Date(checkin);
		checkin.setDate(checkin.getDate() + 1);

		checkin = getDateFormatMMDDYYYYWithSlash(checkin.getDate(), checkin.getMonth()+1, checkin.getFullYear());
	}
}

function refreshCalendar()
{
	var checkin = document.wizardForm.checkinDate.value;
	
	if(checkin.length == 10 && isNumeric(checkin) && checkin != checkinDate) {
		
		checkinDate = checkin;
		checkin = checkin.split('/');

		if(checkin.length == 3) {

			var monthAndYear = checkin[0] + '/' + checkin[2];
			var argStr = '&monthAndYear=';
			var iframe = document.getElementsByTagName('IFRAME');
	
			for(var i=0; i<iframe.length; i++) {
	
				if(iframe.item(i).name == 'beCalendar') {
	
					var arg = argStr + monthAndYear;
					var src = iframe.item(i).src;
					src = src.split(argStr);
					iframe.item(i).src = src[0] + arg;
					break;
				}	
			}
		}
	}
}

function isNumeric(number)
{
   var validChars = "0123456789";
   var isNumber=true;
   var Char, j = 0;
 
   for(var i=0; i<number.length && isNumber; i++) {

      Char = number.charAt(i); 
	
	  if(Char == '/') j++;

	  else if(validChars.indexOf(Char) == -1 || j > 2) isNumber = false;
   }

   return isNumber;
}
