var calendar_defaultContent = new Array();
var calendar_numCals = 2;
var calendar_dayConflict = false;

function calendar_storeDefault(){
	if(calendar_defaultContent.length == 0){
		for(i = 1; i <= calendar_numCals; i++){
			wrapperObj = document.getElementById('calendarBlock_' + i);
			calendar_defaultContent[i] = wrapperObj.innerHTML;
		}
	}
}

function calendar_highlightDay(day, month, year, isLast){
	cellId = 'bookingCalendarDay_' + leadingZero(day) + '/' + leadingZero(month) +  '/' + leadingZero(year);
	
	cellObj = document.getElementById(cellId);
	if(cellObj != null && cellObj != undefined){
		if(cellObj.className == 'calendar_day_avail' || cellObj.className == 'calendar_day_avail_start' || cellObj.className == 'calendar_day_changeover'){
			cellObj.className = 'calendar_day_avail_selected';
			//return true;
		}
		else{
			if(isLast == false){
				cellObj.className = 'calendar_day_unavail_selected';
				calendar_dayConflict = true;
				//return false;
			}
			else{
				if(calendar_dayConflict == true){
					cellObj.className = 'calendar_day_unavail_selected';
					//return false;
				}
				else{
					cellObj.className = 'calendar_day_avail_selected';
					//return true;
				}
			}
		}
	}
}

function calendar_reset(){
	calendar_dayConflict = false;
	for(i = 1; i <= calendar_numCals; i++){
		wrapperObj = document.getElementById('calendarBlock_' + i);
		wrapperObj.innerHTML = calendar_defaultContent[i];
	}
}

function calendar_highlightDates(day, month, year, duration){
	
	//alert(day + '/' + month + '/' + year + ', ' + duration);
	
	durationField = document.getElementById('bookingDuration');
	
	if(durationField != undefined){
		if(durationField.value != 'noneSet' && durationField.value != 'shortBreak'){
			duration = parseInt(durationField.value);
		}
	}
	
	daysInFirstMonth = calendar_daysInMonth(month, year);
	if(month + 1 > 12){ secondMonth = 1; secondYear = (year + 1); }
	else{ secondMonth = (month + 1); secondYear = year }
	daysInSecondMonth = calendar_daysInMonth(secondMonth, secondYear);
	
	calendar_storeDefault();
	calendar_reset();
	
	if(day + duration > daysInFirstMonth){
			
		selInFirstMonth = daysInFirstMonth - day;
		selInSecondMonth = (duration - selInFirstMonth);
				
		for(i = day; i <= day + selInFirstMonth; i++){
			if(i == day + selInFirstMonth){
				calendar_highlightDay(i, month, year, true);
			}
			else{
				calendar_highlightDay(i, month, year, false);
			}
		}
		
		for(i = 1; i <= selInSecondMonth; i++){
			if(i == selInSecondMonth){
				calendar_highlightDay(i, secondMonth, secondYear, true);
			}
			else{
				calendar_highlightDay(i, secondMonth, secondYear, false);
			}
		}		
	}
	else{
		for(i = day; i <= day + duration; i++){
			if(i == day + duration){
				calendar_highlightDay(i, month, year, true);
			}
			else{
				calendar_highlightDay(i, month, year, false);
			}
		}
	}
}

function calendar_daysInMonth(month, year){
	var days = new Array();
	
	days[2007] = new Array(false, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	days[2008] = new Array(false, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	days[2009] = new Array(false, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	days[2010] = new Array(false, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	days[2011] = new Array(false, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	days[2012] = new Array(false, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	days[2013] = new Array(false, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	days[2014] = new Array(false, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	days[2015] = new Array(false, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	days[2016] = new Array(false, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	return days[year][month];
	
	//return 32 - new Date(year, month, 32).getDate();
}

function leadingZero(num){
	if(num < 10){ return '0' + num; }
	else{ return num; }	 
}


// not part of the calendar highlighting!
function showHideDiv(divId){
	divObj = document.getElementById(divId);
	if(divObj.style.display == 'block'){
		divObj.style.display = 'none';
	}
	else{
		divObj.style.display = 'block';
	}
}