var days = new Array();
days[0] = "Sunday";
days[1] = "Monday";
days[2] = "Tuesday";
days[3] = "Wednesday";
days[4] = "Thursday";
days[5] = "Friday";
days[6] = "Saturday";

var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";

var mon = new Array();
mon[0] = "Jan";
mon[1] = "Feb";
mon[2] = "Mar";
mon[3] = "Apr";
mon[4] = "May";
mon[5] = "Jun";
mon[6] = "Jul";
mon[7] = "Aug";
mon[8] = "Sep";
mon[9] = "Oct";
mon[10] = "Nov";
mon[11] = "Dec";

function createDropdownDate(b,e,s)
{
	if (s==0)
	{
		now = new Date();
		s = now.getDate();
	}
	for (i=b; i<=e; i++)
	{
		if (i==s)
			document.write('<option value="'+i+'" selected="selected">'+i+'</option>');
		else
			document.write('<option value="'+i+'">'+i+'</option>');
	}
}

function setYears(type, sel, abbr)
{
	f=document.form;
	now = new Date();
	m = now.getMonth();
	y = now.getFullYear();

	for (i=y; i<=(y+1); i++)
	{
		for (j=0; j<=11; j++)
		{
			if (i==y)
			{
				if (j>=m)
				{
					len = f[type].length++;
					f[type].options[len].value = j+"|"+i;
					if (abbr==1)
						f[type].options[len].text = mon[j]+" "+i.toString().substring(2);
					else
						f[type].options[len].text = month[j]+" "+i;
				}
			}
			else
			{
				len = f[type].length++;
				f[type].options[len].value = j+"|"+i;
				if (abbr==1)
					f[type].options[len].text = mon[j]+" "+i.toString().substring(2);
				else
					f[type].options[len].text = month[j]+" "+i;
			}
		}
	}
	if (sel!='')
		setDropdown(sel, type);
}

function setSunday(t)
{
	if (t==-1)
		dateset = readCookieDatesFree();
	else
		dateset = readCookieDates(t);
	if (dateset==0)
	{
		f = document.form;
		now = new Date();
		today = now.getDay();
		smonth = getMonthValue(f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
		if (today==0)
			n = 7;
		else
			n = 7 - today;

		now.setTime(now.getTime() + n * 24 * 60 * 60 * 1000);
		f.arrivedate.selectedIndex = now.getDate();
		if (now.getMonth()==0 && smonth==11)
		  smonth=-1;
		f.arrivedate2.selectedIndex = f.arrivedate2.selectedIndex + (now.getMonth() - smonth);
		setEndDate(1);
	}
}

function setEndDate(n)
{
	f = document.form;
	now = new Date();
	smonth = getMonthValue(f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	syear = getYearValue(f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	now.setYear(syear);
	now.setDate(1);
	now.setMonth(smonth);
	now.setDate(f.arrivedate.selectedIndex);

	checkdate = checkDate(now.getTime());

	now.setTime(now.getTime() + n * 24 * 60 * 60 * 1000);
	f.departdate.selectedIndex = now.getDate();
	if (now.getMonth() < smonth)
		f.departdate2.selectedIndex = f.arrivedate2.selectedIndex + ((now.getMonth() + 12) - smonth);
	else
		f.departdate2.selectedIndex = f.arrivedate2.selectedIndex + (now.getMonth() - smonth);

	setDayName('arrive',f.arrivedate.selectedIndex,f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	setDayName('depart',f.departdate.selectedIndex,f.departdate2.options[f.departdate2.selectedIndex].value);
}

function checkDate(d)
{
	thisdate = new Date();
	thisdate = new Date(thisdate.getYear(),thisdate.getMonth(),thisdate.getDate());
	checkdate = d - thisdate.getTime();
	return checkdate;
}

function userDate(sdate,smonth,syear,edate,emonth,eyear)
{
	f = document.form;
	f.arrivedate.selectedIndex = sdate;
	smy = smonth + "|" + syear;
	setDropdown(smy,'arrivedate2');

	f.departdate.selectedIndex = edate;
	emy = emonth + "|" + eyear;
	setDropdown(emy,'departdate2');
}

function setDropdown(date,field)
{
	f = document.form;
	for (i=0; i < f[field].options.length; i++)
	{
		check = f[field].options[i].value;
		if (date == check)
		{
			f.elements[field].selectedIndex = i;
			break;
		}
	}
}

function checkPeriod()
{
	f = document.form;
	valid = 0;
	if (f.date_select[1].checked==true)
	{
		valid = validDate(f.arrivedate.selectedIndex,f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
		if (valid==1)
		{
			alert("The start date you have entered is invalid");
			return false;
		}
		valid = validDate(f.departdate.selectedIndex,f.departdate2.options[f.departdate2.selectedIndex].value);
		if (valid==1)
		{
			alert("The end date you have entered is invalid");
			return false;
		}
		stime = getTimeValue(f.arrivedate.selectedIndex,f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
		etime = getTimeValue(f.departdate.selectedIndex,f.departdate2.options[f.departdate2.selectedIndex].value);

		dtime = (etime - stime) / (24 * 60 * 60 * 1000);
		if (dtime>31)
		{
			alert("Please select a period no longer than 31 days");
			return false;
		}
		else if (dtime<0)
		{
			alert("Please select a longer period");
			return false;
		}
	}
}

function validDate(d,m)
{
	valid = 0;
	cmonth = getMonthValue(m);
	cyear = getYearValue(m);

	if (cyear % 4 != 0)
	{
		if ((cmonth == 1)&&(d > 28))
			valid = 1;
		if (((cmonth == 3)||(cmonth == 5)||(cmonth == 8)||(cmonth == 10))&&(d > 30))
			valid = 1;
	}
	if (cyear % 4 == 0)
	{
		if ((cmonth == 1)&&(d > 29))
			valid = 1;
		if (((cmonth == 3)||(cmonth == 5)||(cmonth == 8)||(cmonth == 10))&&(d > 30))
			valid = 1;
	}
	return valid;
}

function getMonthValue(m)
{
	pos = m.indexOf("|");
	month = m.substring(0,pos);
	return month;
}

function getYearValue(y)
{
	pos = y.indexOf("|");
	year = y.substring(pos+1);
	return year;
}

function getTimeValue(d,m)
{
	now = new Date();
	month = getMonthValue(m);
	year = getYearValue(m);
	now.setYear(year);
	now.setMonth(month);
	now.setDate(d);
	return now.getTime();
}

function getDayName(d,m)
{
	now = new Date();
	month = getMonthValue(m);
	year = getYearValue(m);
	now.setYear(year);
	now.setMonth(month);
	now.setDate(d);
	return now.getDay();
}

function setToday(n)
{
	f = document.form;
	now = new Date();
	now.setTime(now.getTime() + n * 24 * 60 * 60 * 1000);
	m = now.getMonth();
	y = now.getFullYear();
	dropMonth = m + "|" + y;

	f.arrivedate.selectedIndex = now.getDate();
	setDropdown(dropMonth, 'arrivedate2');
	f.departdate.selectedIndex = now.getDate() + 1;
	setDropdown(dropMonth, 'departdate2');
}

function setNights()
{
	f = document.form;
	f.nights.length = 20;

	stime = getTimeValue(f.arrivedate.selectedIndex,f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	etime = getTimeValue(f.departdate.selectedIndex,f.departdate2.options[f.departdate2.selectedIndex].value);

	setDayName('arrive',f.arrivedate.selectedIndex,f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	setDayName('depart',f.departdate.selectedIndex,f.departdate2.options[f.departdate2.selectedIndex].value);

	dtime = (etime - stime) / (24 * 60 * 60 * 1000);

	if (dtime > 20)
	{
		for (i=21; i<=dtime; i++)
		{
			len = f.nights.length++;
			f.nights.options[len].value = i;
			f.nights.options[len].text = i;
		}
	}

	f.nights.selectedIndex = dtime - 1;
}

function setDayName(t,d,m)
{
	f = document.form;
	f[t+'day'].value = days[getDayName(d,m)];
}

function populateDates()
{
	w = window.opener;
	f = document.form;
	if (w.f)
	{
		city = w.f.city.value;
		city_set = 0;
		for (i=0; i<f.city.length; i++)
		{
			if (f.city[i].value==city)
			{
				f.city[i].checked = true;
				city_set = 1;
			}
		}
		if (city_set==0)
		{
			for (i=0; i<f.other_city.length; i++)
			{
				if (f.other_city.options[i].value==city)
					f.other_city.options[i].selected = true;
			}
		}
	}
	dateset = readCookieDates();
	if (dateset==0)
	{
	  if (w.f)
	  {
		f.arrivedate.selectedIndex = w.f.arrivedate.selectedIndex;
		f.arrivedate2.selectedIndex = w.f.arrivedate2.selectedIndex;
		f.nights.selectedIndex = w.f.nights.selectedIndex;
		setEndDate(f.nights.options[f.nights.selectedIndex].value);
		f.rooms.selectedIndex = w.f.rooms.selectedIndex;
		showRooms(f.rooms.options[f.rooms.selectedIndex].value)
		for (i=1; i<=f.rooms.options[f.rooms.selectedIndex].value; i++)
		{
			f['roomtype'+i].selectedIndex = w.f['roomtype'+i].selectedIndex;
		}
          }
	  else setSunday('0');
	}
}

function checkArrivalDate()
{
	f = document.form;
	//now = new Date();
	smonth = getMonthValue(f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	syear = getYearValue(f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	//now.setYear(syear);
	//now.setMonth(smonth);
	//now.setDate(f.arrivedate.selectedIndex);
	now = new Date(syear,smonth,f.arrivedate.selectedIndex);

	checkdate = checkDate(now.getTime());
	
	return checkdate;
}

function compareDates()
{
	f = document.form;
	//arr = new Date();
	smonth = getMonthValue(f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	syear = getYearValue(f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	//arr.setYear(syear);
	//arr.setMonth(smonth);
	//arr.setDate(f.arrivedate.selectedIndex);
	//arr.setFullYear(syear,smonth,f.arrivedate.selectedIndex);
	arr = new Date(syear,smonth,f.arrivedate.selectedIndex);

	//dep = new Date();
	emonth = getMonthValue(f.departdate2.options[f.departdate2.selectedIndex].value);
	eyear = getYearValue(f.departdate2.options[f.departdate2.selectedIndex].value);
	//dep.setYear(eyear);
	//dep.setMonth(emonth);
	//dep.setDate(f.departdate.selectedIndex);
	dep = new Date(eyear,emonth,f.departdate.selectedIndex);

	checkdate = checkDate(now.getTime());

	if (dep.getTime()<=arr.getTime())
		check = 1;
	else
		check = 0;

	return check;
}

function setCookieDates()
{
	f = document.form;
	document.cookie="";
	document.cookie = "sd=" + f.arrivedate.selectedIndex + "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "sm=" + f.arrivedate2.selectedIndex + "; domain=www.hoteldirect.co.uk;path=/;";
	document.cookie = "nights=" + f.nights.selectedIndex + "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "rooms=" + f.rooms.selectedIndex + "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "room1=" + f.roomtype1.selectedIndex + "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "room2=" + f.roomtype2.selectedIndex + "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "room3=" + f.roomtype3.selectedIndex + "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "room4=" + f.roomtype4.selectedIndex + "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "room5=" + f.roomtype5.selectedIndex + "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "dateset=yes; domain=www.hoteldirect.co.uk; path=/;";
}

function readCookieDates(t)
{
	f = document.form;
	cookie = document.cookie;
	broken_cookie = cookie.split(";");
	m = broken_cookie.length;
	dateset = 0;

	for (i=0; i<m; i++)
	{
		broken_cookie[i] = trimString(broken_cookie[i]);
		if (broken_cookie[i]=='dateset=yes')
			dateset = 1;
	}
	if (dateset==1)
	{
		for (k=0; k<m; k++)
		{
			if (broken_cookie[k].substring(0,3)=='sd=')
				f.arrivedate.selectedIndex = broken_cookie[k].substring(3);
			if (broken_cookie[k].substring(0,3)=='sm=')
				f.arrivedate2.selectedIndex = broken_cookie[k].substring(3);
			if (broken_cookie[k].substring(0,7)=='nights=')
			{
				f.nights.selectedIndex = parseInt(broken_cookie[k].substring(7));
				setEndDate(f.nights.options[f.nights.selectedIndex].value);
			}
			if (broken_cookie[k].substring(0,6)=='rooms=')
			{
				f.rooms.selectedIndex = broken_cookie[k].substring(6);
				showRooms(f.rooms.options[f.rooms.selectedIndex].value, t);
			}
			if (broken_cookie[k].substring(0,6)=='room1=')
				f.roomtype1.selectedIndex = broken_cookie[k].substring(6);
			if (broken_cookie[k].substring(0,6)=='room2=')
				f.roomtype2.selectedIndex = broken_cookie[k].substring(6);
			if (broken_cookie[k].substring(0,6)=='room3=')
				f.roomtype3.selectedIndex = broken_cookie[k].substring(6);
			if (broken_cookie[k].substring(0,6)=='room4=')
				f.roomtype4.selectedIndex = broken_cookie[k].substring(6);
			if (broken_cookie[k].substring(0,6)=='room5=')
				f.roomtype5.selectedIndex = broken_cookie[k].substring(6);
		}
	}
	return dateset;
}


function setCookieDatesFree()
{
	f = document.form;
	document.cookie = "sd=" + f.arrivedate.selectedIndex + "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "sm=" + f.arrivedate2.selectedIndex+ "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "nights=" + f.nights.selectedIndex+ "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "family1=" + f.family1.selectedIndex+ "; domain=www.hoteldirect.co.uk; path=/;";
	document.cookie = "family2=" + f.family2.selectedIndex+ "; domain=www.hoteldirect.co.uk; path=/;";
	roomnum = parseInt(f.family1.selectedIndex) + parseInt(f.family2.selectedIndex) - 1;
	if (roomnum > 4)
		roomnum = 4;
	document.cookie = "rooms=" + roomnum + "; domain=www.hoteldirect.co.uk; path=/;";
	maxroom = 0;
	if (f.family1.selectedIndex>0)
	{
		for (i = maxroom+1; i <= f.family1.selectedIndex; i++)
		{
			document.cookie = "room" + i + "=5" + "; domain=www.hoteldirect.co.uk; path=/;";
			maxroom = i;
		}
	}
	if ((f.family2.selectedIndex>0)&&(maxroom<5))
	{
		for (j = maxroom+1; j <= (f.family2.selectedIndex + maxroom); j++)
		{
			document.cookie = "room" + j + "=6" + "; domain=www.hoteldirect.co.uk; path=/;";
		}
	}
	document.cookie = "dateset=yes" + "; domain=www.hoteldirect.co.uk; path=/;";
}


function readCookieDatesFree()
{
	f = document.form;
	cookie = document.cookie;
	broken_cookie = cookie.split(";");
	m = broken_cookie.length;
	dateset = 0;

	for (i=0; i<m; i++)
	{
		broken_cookie[i] = trimString(broken_cookie[i]);
		if (broken_cookie[i]=='dateset=yes')
			dateset = 1;
	}
	if (dateset==1)
	{
		for (k=0; k<m; k++)
		{
			if (broken_cookie[k].substring(0,3)=='sd=')
				f.arrivedate.selectedIndex = broken_cookie[k].substring(3);
			if (broken_cookie[k].substring(0,3)=='sm=')
				f.arrivedate2.selectedIndex = broken_cookie[k].substring(3);
			if (broken_cookie[k].substring(0,7)=='nights=')
			{
				f.nights.selectedIndex = parseInt(broken_cookie[k].substring(7));
				setEndDate(f.nights.options[f.nights.selectedIndex].value);
			}
			if (broken_cookie[k].substring(0,8)=='family1=')
				f.family1.selectedIndex = broken_cookie[k].substring(8);
			if (broken_cookie[k].substring(0,8)=='family2=')
				f.family2.selectedIndex = broken_cookie[k].substring(8);
		}
	}
	return dateset;
}

function trimString(str)
{
	return str.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s+/g," ");
}