var arrivalDate=0;
var departureDate=0;
var day = 24 * 60 * 60 * 1000;
// prices
var lowWeek = 50;
var lowDay = 60;
var highWeek = 60;
var highDay = 70;

function scwToIso(scwdate, lang){
    if (!scwdate)
	    return "";
	var months = new Array();
	months['en'] = new Array('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	var da = scwdate.split(' ');
	var month = da[1];
	month = month.substr(0, month.length-1);
	for (var i=0; i<months[lang].length; i++)
		if (month == months[lang][i])
			break;
	month = i;
	if (month <= 9) month = "0" + month;
	return da[2]+'-'+month+'-'+da[0];
}
function scwToDate(scwdate, lang){
    if (!scwdate)
	    return "";
	var months = new Array();
	months['en'] = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	var da = scwdate.split(' ');
	var month = da[1];
	month = month.substr(0, month.length-1);
	for (var i=0; i<months[lang].length; i++)
		if (month == months[lang][i])
			break;
	month = i;
	if (month <= 9) month = "0" + month;
//	return new Date(da[2]+'-'+month+'-'+da[0]);
	return new Date(Date.UTC(da[2], month, da[0]));
	
}

function setDisplayedPrice(arrival, departure){
	var displayedPeriod = 'Period of stay: ';
	var totalDays = Math.round((departure - arrival)/day);
	var weeks = Math.floor(totalDays/7);
	var extraDays = totalDays - (weeks * 7);

	if (weeks > 0)
		displayedPeriod += weeks+' weeks and ';
	if (extraDays == 1)
		displayedPeriod += '1 day; ';
	else	
		displayedPeriod += extraDays+' days; ';

	// if MOST of the stay is in high season, charge high,
	// and vice versa. If equal, charge low
	var endD = new Date(departure);
	var low = 0; var high = 0;
	var temp = arrival;
	for(var startD = new Date(temp); startD < endD; temp+=day, startD = new Date(temp)){
		if ((startD.getMonth() < 3) || (startD.getMonth() > 8))
			low++
		else
			high++;
	}
	if (low >= high){
		if (totalDays >= 7)
			cost = totalDays * lowWeek;
		else
			cost = totalDays * lowDay;
	}else{
		if (totalDays >= 7)
			cost = totalDays * highWeek;
		else
			cost = totalDays * highDay;
	}	
	var oStr = displayedPeriod + 'Cost: &pound;'+cost;
	document.getElementById('display_price').innerHTML = oStr;
	document.getElementById('display_price_container').style.visibility = 'visible';
	// set the hidden values for transmission to booking page
	document.getElementById('booking_start_par').value = 
		scwToIso(document.getElementById('date_from').value, 'en');
	document.getElementById('booking_end_par').value = 
		scwToIso(document.getElementById('date_to').value, 'en');
	document.getElementById('booking_cost_par').value = cost;
}

function setArrival(arrival){
	document.getElementById('display_price').value = '';
	document.getElementById('display_price_container').style.visibility = 'hidden';
	arrival = scwToDate(arrival.value, 'en');
	var today = new Date();
	if (arrival < today){
		alert("Please set arrival to a date in the future!");
		document.getElementById('date_from').value = '';
		return;
	}
	var midnight = arrival.getTime();
	var cellId = 'u'+midnight;
	if ( departureDate == 0){
		if (document.getElementById(cellId).className == 'booked'){
			alert("This date is already booked");
			document.getElementById('date_from').value = '';
			return;
		} else {
			arrivalDate = midnight;
			return;
		}
	} else { // we already have a departure date
		if (departureDate < midnight +( day * 3)){
			alert("Bookings must be for at least 3 nights");
			document.getElementById('date_from').value = '';
			return;
		} else {
			for(var pday=midnight; pday<=departureDate; pday+=day){
				if (document.getElementById('u'+pday).className=='booked'){
					alert("You cannot book a day which is already booked");
					document.getElementById('date_from').value = '';
					return;
				}
			} // all ok if exits loop
			arrivalDate = midnight;
			setDisplayedPrice(arrivalDate, departureDate);
			return;
		}
	}
}
function setDeparture(departure){
	document.getElementById('display_price').value = '';
	document.getElementById('display_price_container').style.visibility = 'hidden';
	departure = scwToDate(departure.value,'en');
	var today = new Date();
	var nextyear = new Date(today.getTime()+day*365);
	if (departure > nextyear){
		alert("Sorry, but we do not take bookings more than a year in advance");
		document.getElementById('date_to').value = '';
		return;
	}
	var midnight = departure.getTime();
	var cellId = 'u'+midnight;
	if ( arrivalDate == 0){
		if (document.getElementById(cellId).className == 'booked'){
			alert("This date is already booked");
			document.getElementById('date_from').value = '';
			return;
		} else {
			departureDate = midnight;
			return;
		}
	} else { // we already have an arrival date
		if (midnight < arrivalDate + (day * 3)){
			alert("Bookings must be for at least 3 nights");
			document.getElementById('date_to').value = '';
			return;
		} else {
			for(var pday=arrivalDate; pday<=midnight; pday+=day){
				if (document.getElementById('u'+pday).className=='booked'){
					alert("You cannot book a day which is already booked");
					document.getElementById('date_to').value = '';
					return;
				}
			}
			departureDate = midnight;
			setDisplayedPrice(arrivalDate, departureDate);
			return;
		}
	}
}

