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_name = new Array();
month_name[0] = "January";
month_name[1] = "February";
month_name[2] = "March";
month_name[3] = "April";
month_name[4] = "May";
month_name[5] = "June";
month_name[6] = "July";
month_name[7] = "August";
month_name[8] = "September";
month_name[9] = "October";
month_name[10] = "November";
month_name[11] = "December";

var mon_short = new Array();
mon_short[0] = "Jan";
mon_short[1] = "Feb";
mon_short[2] = "Mar";
mon_short[3] = "Apr";
mon_short[4] = "May";
mon_short[5] = "Jun";
mon_short[6] = "Jul";
mon_short[7] = "Aug";
mon_short[8] = "Sep";
mon_short[9] = "Oct";
mon_short[10] = "Nov";
mon_short[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_short[j]+" "+i.toString().substring(2);
					else
						f[type].options[len].text = month_name[j]+" "+i;
				}
			}
			else
			{
				len = f[type].length++;
				f[type].options[len].value = j+"|"+i;
				if (abbr==1)
					f[type].options[len].text = mon_short[j]+" "+i.toString().substring(2);
				else
					f[type].options[len].text = month_name[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.setFullYear(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.getFullYear(),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();
	//now.setFullYear(2011);
	now.setMonth(0);
	now.setDate(1);
	month = getMonthValue(m);
	//year = getYearValue(m);
	//now.setFullYear(year);
	now.setMonth(month);
	now.setDate(d);
	return now.getTime();
}

function getDayName(d,m)
{
	now = new Date();
	//now.setFullYear(2011);
	now.setMonth(0);
	now.setDate(1);
	//year = getYearValue(m);
	month = getMonthValue(m);
	//now.setFullYear(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 = Math.ceil((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);
	//f.nights.selectedIndex = dtime;
}

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()
{
	var new_kids = true;
	if ($('input[name="kidsage"]').is(':text'))
	{
		new_kids = false;
	}
	
	f = document.form;
	document.cookie="";
	document.cookie = "sd=" + f.arrivedate.selectedIndex + "; domain=" + domain + "; path=/;";
	document.cookie = "sm=" + f.arrivedate2.selectedIndex + "; domain=" + domain + ";path=/;";
	document.cookie = "nights=" + f.nights.selectedIndex + "; domain=" + domain + "; path=/;";
	document.cookie = "rooms=" + f.rooms.selectedIndex + "; domain=" + domain + "; path=/;";
	document.cookie = "room1=" + f.roomtype1.selectedIndex + "; domain=" + domain + "; path=/;";
	document.cookie = "room2=" + f.roomtype2.selectedIndex + "; domain=" + domain + "; path=/;";
	document.cookie = "room3=" + f.roomtype3.selectedIndex + "; domain=" + domain + "; path=/;";
	document.cookie = "room4=" + f.roomtype4.selectedIndex + "; domain=" + domain + "; path=/;";
	document.cookie = "room5=" + f.roomtype5.selectedIndex + "; domain=" + domain + "; path=/;";
	if (new_kids === false)
	{
		var kids1 = 0;
		var kids2 = 0;
		var kids3 = 0;
		var kids4 = 0;
		var kids5 = 0;
		if ($('#roomtype1').val() === 'family1')
		{
			kids1 = 1;
		}
		else if ($('#roomtype1').val() === 'family2')
		{
			kids1 = 2;
		}
		if ($('#roomtype2').val() === 'family1')
		{
			kids2 = 1;
		}
		else if ($('#roomtype2').val() === 'family2')
		{
			kids2 = 2;
		}
		if ($('#roomtype3').val() === 'family1')
		{
			kids3 = 1;
		}
		else if ($('#roomtype3').val() === 'family2')
		{
			kids3 = 2;
		}
		if ($('#roomtype4').val() === 'family1')
		{
			kids4 = 1;
		}
		else if ($('#roomtype4').val() === 'family2')
		{
			kids4 = 2;
		}
		if ($('#roomtype5').val() === 'family1')
		{
			kids5 = 1;
		}
		else if ($('#roomtype5').val() === 'family2')
		{
			kids5 = 2;
		}
		document.cookie = "room1value=" + $('#roomtype1').val() + "|" + kids1 + "; domain=" + domain + "; path=/;";
		document.cookie = "room2value=" + $('#roomtype2').val() + "|" + kids2 + "; domain=" + domain + "; path=/;";
		document.cookie = "room3value=" + $('#roomtype3').val() + "|" + kids3 + "; domain=" + domain + "; path=/;";
		document.cookie = "room4value=" + $('#roomtype4').val() + "|" + kids4 + "; domain=" + domain + "; path=/;";
		document.cookie = "room5value=" + $('#roomtype5').val() + "|" + kids5 + "; domain=" + domain + "; path=/;";
	}
	else
	{
		document.cookie = "room1value=" + $('#roomtype1').val() + "|" + $('#roomtype1 option:selected').attr('data-kids') + "; domain=" + domain + "; path=/;";
		document.cookie = "room2value=" + $('#roomtype2').val() + "|" + $('#roomtype2 option:selected').attr('data-kids') + "; domain=" + domain + "; path=/;";
		document.cookie = "room3value=" + $('#roomtype3').val() + "|" + $('#roomtype3 option:selected').attr('data-kids') + "; domain=" + domain + "; path=/;";
		document.cookie = "room4value=" + $('#roomtype4').val() + "|" + $('#roomtype4 option:selected').attr('data-kids') + "; domain=" + domain + "; path=/;";
		document.cookie = "room5value=" + $('#roomtype5').val() + "|" + $('#roomtype5 option:selected').attr('data-kids') + "; domain=" + domain + "; path=/;";
	}
	document.cookie = "kidsage=" + f.kidsage.value + "; domain=" + domain + "; path=/;";
	// advanced fields
	if ($('select[name="attraction"]').is('*'))
	{
		document.cookie = "attraction=" + f.attraction.options[f.attraction.selectedIndex].value + "; domain=" + domain + "; path=/;";
	}
	if ($('select[name="attraction_2"]').is('*'))
	{
		document.cookie = "attraction=" + f.attraction_2.options[f.attraction_2.selectedIndex].value + "; domain=" + domain + "; path=/;";
	}
	if ($('select[name="rail"]').is('*'))
	{
	document.cookie = "rail=" + f.rail.options[f.rail.selectedIndex].value + "; domain=" + domain + "; path=/;";
	}
	if ($('select[name="tube"]').is('*'))
	{
	document.cookie = "tube=" + f.tube.options[f.tube.selectedIndex].value + "; domain=" + domain + "; path=/;";
	}
	if ($('input[name="postcode"]').is('*'))
	{
	document.cookie = "postcode=" + f.postcode.value + "; domain=" + domain + "; path=/;";
	}
	if ($('select[name="location"]').is('*'))
	{
		document.cookie = "location=" + f.location.options[f.location.selectedIndex].value + "; domain=" + domain + "; path=/;";
	}
	if ($('select[name="location_2"]').is('*'))
	{
		document.cookie = "location=" + f.location_2.options[f.location_2.selectedIndex].value + "; domain=" + domain + "; path=/;";
	}
	if ($('select[name="category"]').is('*'))
	{
	document.cookie = "category=" + f.category.options[f.category.selectedIndex].value + "; domain=" + domain + "; path=/;";
	}
	if ($('select[name="price"]').is('*'))
	{
	document.cookie = "price=" + f.price.options[f.price.selectedIndex].value + "; domain=" + domain + "; path=/;";
	}
	if ($('select[name="hotelname"]').is('*'))
	{
	document.cookie = "hotelname=" + f.hotelname.options[f.hotelname.selectedIndex].value + "; domain=" + domain + "; path=/;";
	}
	if ($('select[name="facilities"]').is('*'))
	{
	document.cookie = "facilities=" + f.facilities.options[f.facilities.selectedIndex].value + "; domain=" + domain + "; path=/;";
	}
	// advanced fields
	document.cookie = "dateset=yes; domain=" + domain + "; path=/;";
}

function readCookieDates(t)
{
	f = document.form;
	cookie = document.cookie;
	broken_cookie = cookie.split(";");
	m = broken_cookie.length;
	dateset = 0;
	var age_cookie = 0;
	var new_kids = true;
	if ($('input[name="kidsage"]').is(':text'))
	{
		new_kids = false;
	}

	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=')
			{
				if ($('select[name="rooms"]').is('*'))
				{
					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);*/

			if (new_kids === true)
			{
				if (broken_cookie[k].substring(0,11)=='room1value=')
				{
					if ($('select[name="roomtype1"]').is('*'))
					{
						roomtypename = broken_cookie[k].substring(11).split('|');
						$('select[name="roomtype1"] option').each(function(i){
							if ($(this).val() == roomtypename[0] && $(this).attr('data-kids') == roomtypename[1])
							{
								$('select[name="roomtype1"]').prop('selectedIndex', i);
							}
						});
					}
				}
				if (broken_cookie[k].substring(0,11)=='room2value=')
				{
					if ($('select[name="roomtype2"]').is('*'))
					{
						roomtypename = broken_cookie[k].substring(11).split('|');
						$('select[name="roomtype2"] option').each(function(i){
							if ($(this).val() == roomtypename[0] && $(this).attr('data-kids') == roomtypename[1])
							{
								$('select[name="roomtype2"]').prop('selectedIndex', i);
							}
						});
					}
				}
				if (broken_cookie[k].substring(0,11)=='room3value=')
				{
					if ($('select[name="roomtype3"]').is('*'))
					{
						roomtypename = broken_cookie[k].substring(11).split('|');
						$('select[name="roomtype3"] option').each(function(i){
							if ($(this).val() == roomtypename[0] && $(this).attr('data-kids') == roomtypename[1])
							{
								$('select[name="roomtype3"]').prop('selectedIndex', i);
							}
						});
					}
				}
				if (broken_cookie[k].substring(0,11)=='room4value=')
				{
					if ($('select[name="roomtype4"]').is('*'))
					{
						roomtypename = broken_cookie[k].substring(11).split('|');
						$('select[name="roomtype4"] option').each(function(i){
							if ($(this).val() == roomtypename[0] && $(this).attr('data-kids') == roomtypename[1])
							{
								$('select[name="roomtype4"]').prop('selectedIndex', i);
							}
						});
					}
				}
				if (broken_cookie[k].substring(0,11)=='room5value=')
				{
					if ($('select[name="roomtype5"]').is('*'))
					{
						roomtypename = broken_cookie[k].substring(11).split('|');
						$('select[name="roomtype5"] option').each(function(i){
							if ($(this).val() == roomtypename[0] && $(this).attr('data-kids') == roomtypename[1])
							{
								$('select[name="roomtype5"]').prop('selectedIndex', i);
							}
						});
					}
				}
			}
			else
			{
				if (broken_cookie[k].substring(0,11)=='room1value=')
				{
					if ($('select[name="roomtype1"]').is('*'))
					{
						roomtypename = broken_cookie[k].substring(11).split('|');
						found=false;
						i=0;
						while (i<f.roomtype1.options.length && !found)
						{
							if (f.roomtype1.options[i].value==roomtypename[0])
							{
								f.roomtype1.selectedIndex=i;
								found=true;
							}
							else i++;
						}
					}
				}
				if (broken_cookie[k].substring(0,11)=='room2value=')
				{
					if ($('select[name="roomtype2"]').is('*'))
					{
						roomtypename = broken_cookie[k].substring(11).split('|');
						found=false;
						i=0;
						while (i<f.roomtype2.options.length && !found)
						{
							if (f.roomtype2.options[i].value==roomtypename[0])
							{
								f.roomtype2.selectedIndex=i;
								found=true;
							}
							else i++;
						}
					}
				}
				if (broken_cookie[k].substring(0,11)=='room3value=')
				{
					if ($('select[name="roomtype3"]').is('*'))
					{
						roomtypename = broken_cookie[k].substring(11).split('|');
						found=false;
						i=0;
						while (i<f.roomtype3.options.length && !found)
						{
							if (f.roomtype3.options[i].value==roomtypename[0])
							{
								f.roomtype3.selectedIndex=i;
								found=true;
							}
							else i++;
						}
					}
				}
				if (broken_cookie[k].substring(0,11)=='room4value=')
				{
					if ($('select[name="roomtype4"]').is('*'))
					{
						roomtypename = broken_cookie[k].substring(11).split('|');
						found=false;
						i=0;
						while (i<f.roomtype4.options.length && !found)
						{
							if (f.roomtype4.options[i].value==roomtypename[0])
							{
								f.roomtype4.selectedIndex=i;
								found=true;
							}
							else i++;
						}
					}
				}
				if (broken_cookie[k].substring(0,11)=='room5value=')
				{
					if ($('select[name="roomtype5"]').is('*'))
					{
						roomtypename = broken_cookie[k].substring(11).split('|');
						found=false;
						i=0;
						while (i<f.roomtype5.options.length && !found)
						{
							if (f.roomtype5.options[i].value==roomtypename[0])
							{
								f.roomtype5.selectedIndex=i;
								found=true;
							}
							else i++;
						}
					}
				}
			}
			if (broken_cookie[k].substring(0,8)=='kidsage=')
			{
				f.kidsage.value = broken_cookie[k].substring(8);
				age_cookie = k;
			}
		}
		if (new_kids === true)
		{
			showHideKids();
			age = broken_cookie[age_cookie].substring(8).split(',');
			agelen = 0;
			$('select.kidsage').each(function(x){
				if ($(this).is(':visible'))
				{
					$(this).val(age[agelen]);
					agelen++;
				}
			});
			showHideKids();
		}
	}
	return dateset;
}

function readAdvancedCookie()
{
	f = document.form;
	cookie = document.cookie;
	broken_cookie = cookie.split(";");
	m = broken_cookie.length;

	for (i=0; i<m; i++)
	{
		broken_cookie[i] = trimString(broken_cookie[i]);
	}
	
	for (k=0; k<m; k++)
	{
		if (broken_cookie[k].substring(0,11)=='attraction=')
		{
			attraction = broken_cookie[k].substring(11);
			found=false;
			i=0;
			if ($('select[name="attraction"]').is('*'))
			{
				while (i<f.attraction.options.length && !found)
				{
					if (f.attraction.options[i].value==attraction)
					{
						f.attraction.selectedIndex=i;
						found=true;
					}
					else i++;
				}
			}
			if ($('select[name="attraction_2"]').is('*'))
			{
				while (i<f.attraction_2.options.length && !found)
				{
					if (f.attraction_2.options[i].value==attraction)
					{
						f.attraction_2.selectedIndex=i;
						found=true;
					}
					else i++;
				}
			}
		}
		if (broken_cookie[k].substring(0,5)=='rail=')
		{
			rail = broken_cookie[k].substring(5);
			found=false;
			i=0;
			while (i<f.rail.options.length && !found)
			{
				if (f.rail.options[i].value==rail)
				{
					f.rail.selectedIndex=i;
					found=true;
				}
				else i++;
			}
		}
		if (broken_cookie[k].substring(0,5)=='tube=')
		{
			tube = broken_cookie[k].substring(5);
			found=false;
			i=0;
			while (i<f.tube.options.length && !found)
			{
				if (f.tube.options[i].value==tube)
				{
					f.tube.selectedIndex=i;
					found=true;
				}
				else i++;
			}
		}
		if (broken_cookie[k].substring(0,9)=='location=')
		{
			loc = broken_cookie[k].substring(9);
			found=false;
			i=0;
			if ($('select[name="location"]').is('*'))
			{
				while (i<f.location.options.length && !found)
				{
					if (f.location.options[i].value==loc)
					{
						f.location.selectedIndex=i;
						found=true;
					}
					else i++;
				}
			}
			if ($('select[name="location_2"]').is('*'))
			{
				while (i<f.location_2.options.length && !found)
				{
					if (f.location_2.options[i].value==loc)
					{
						f.location_2.selectedIndex=i;
						found=true;
					}
					else i++;
				}
			}
		}
		if (broken_cookie[k].substring(0,9)=='category=')
		{
			category = broken_cookie[k].substring(9);
			found=false;
			i=0;
			while (i<f.category.options.length && !found)
			{
				if (f.category.options[i].value==category)
				{
					f.category.selectedIndex=i;
					found=true;
				}
				else i++;
			}
		}
		if (broken_cookie[k].substring(0,6)=='price=')
		{
			price = broken_cookie[k].substring(6);
			found=false;
			i=0;
			while (i<f.price.options.length && !found)
			{
				if (f.price.options[i].value==price)
				{
					f.price.selectedIndex=i;
					found=true;
				}
				else i++;
			}
		}
		if (broken_cookie[k].substring(0,10)=='hotelname=')
		{
			hotelname = broken_cookie[k].substring(10);
			found=false;
			i=0;
			while (i<f.hotelname.options.length && !found)
			{
				if (f.hotelname.options[i].value==hotelname)
				{
					f.hotelname.selectedIndex=i;
					found=true;
				}
				else i++;
			}
		}
		if (broken_cookie[k].substring(0,11)=='facilities=')
		{
			facilities = broken_cookie[k].substring(11);
			found=false;
			i=0;
			while (i<f.facilities.options.length && !found)
			{
				if (f.facilities.options[i].value==facilities)
				{
					f.facilities.selectedIndex=i;
					found=true;
				}
				else i++;
			}
		}
		if (broken_cookie[k].substring(0,9)=='postcode=')
		{
			f.postcode.value = broken_cookie[k].substring(9);
		}
	}
}


function setCookieDatesFree()
{
	f = document.form;
	document.cookie = "sd=" + f.arrivedate.selectedIndex + "; domain=" + domain + "; path=/;";
	document.cookie = "sm=" + f.arrivedate2.selectedIndex+ "; domain=" + domain + "; path=/;";
	document.cookie = "nights=" + f.nights.selectedIndex+ "; domain=" + domain + "; path=/;";
	rooms=-1;
	if (f.family1.selectedIndex>0) rooms=rooms+f.family1.selectedIndex;
	if (f.family2.selectedIndex>0) rooms=rooms+f.family2.selectedIndex;
	document.cookie = "rooms=" + rooms + "; domain=" + domain + "; path=/;";
	document.cookie = "family1=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
	document.cookie = "family2=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";

	if (f.family1.selectedIndex>0)
	{
		if (f.family1.selectedIndex==1)
		{	
			document.cookie = "room1=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room1value=family1; domain=" + domain + "; path=/;";
			if (f.family2.selectedIndex>0)
			{
				document.cookie = "room2=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room2value=family2; domain=" + domain + "; path=/;";
			}
			else
			{
				document.cookie = "room2=0; domain=" + domain + "; path=/;";
				document.cookie = "room2value=;domain=" + domain + "; path=/;";
			}
		}
		else if (f.family1.selectedIndex==2)
		{	
			document.cookie = "room1=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room1value=family1; domain=" + domain + "; path=/;";
			document.cookie = "room2=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room2value=family1; domain=" + domain + "; path=/;";
			if (f.family2.selectedIndex>0)
			{
				document.cookie = "room3=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room3value=family2; domain=" + domain + "; path=/;";
			}
			else
			{
				document.cookie = "room3=0; domain=" + domain + "; path=/;";
				document.cookie = "room3value=;domain=" + domain + "; path=/;";
			}

		}
		else if (f.family1.selectedIndex==3)
		{	
			document.cookie = "room1=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room1value=family1; domain=" + domain + "; path=/;";
			document.cookie = "room2=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room2value=family1; domain=" + domain + "; path=/;";
			document.cookie = "room3=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room3value=family1; domain=" + domain + "; path=/;";
			if (f.family2.selectedIndex>0)
			{
				document.cookie = "room4=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room4value=family2; domain=" + domain + "; path=/;";
			}
			else
			{
				document.cookie = "room4=0; domain=" + domain + "; path=/;";
				document.cookie = "room4value=;domain=" + domain + "; path=/;";
			}
		}
		else if (f.family1.selectedIndex==4)
		{	
			document.cookie = "room1=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room1value=family1; domain=" + domain + "; path=/;";
			document.cookie = "room2=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room2value=family1; domain=" + domain + "; path=/;";
			document.cookie = "room3=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room3value=family1; domain=" + domain + "; path=/;";
			document.cookie = "room4=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room4value=family1; domain=" + domain + "; path=/;";
			if (f.family2.selectedIndex>0)
			{
				document.cookie = "room5=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room5value=family2; domain=" + domain + "; path=/;";
			}
			else
			{
				document.cookie = "room5=0; domain=" + domain + "; path=/;";
				document.cookie = "room5value=;domain=" + domain + "; path=/;";
			}
		}
		else if (f.family1.selectedIndex==5)
		{	
			document.cookie = "room1=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room1value=family1; domain=" + domain + "; path=/;";
			document.cookie = "room2=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room2value=family1; domain=" + domain + "; path=/;";
			document.cookie = "room3=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room3value=family1; domain=" + domain + "; path=/;";
			document.cookie = "room4=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room4value=family1; domain=" + domain + "; path=/;";
			document.cookie = "room5=" + f.family1.selectedIndex+ "; domain=" + domain + "; path=/;";
			document.cookie = "room5value=family1; domain=" + domain + "; path=/;";
			foundfamily1=5;
		}
	}
	else
	{
		if (f.family2.selectedIndex>0)
		{
			if (f.family2.selectedIndex==1)
			{	
				document.cookie = "room1=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room1value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room2=0; domain=" + domain + "; path=/;";
				document.cookie = "room2value=;domain=" + domain + "; path=/;";
			}
			else if (f.family2.selectedIndex==2)
			{	
				document.cookie = "room1=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room1value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room2=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room2value=family2; domain=" + domain + "; path=/;";
			}
			else if (f.family2.selectedIndex==3)
			{	
				document.cookie = "room1=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room1value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room2=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room2value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room3=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room3value=family2; domain=" + domain + "; path=/;";
			}
			else if (f.family2.selectedIndex==4)
			{	
				document.cookie = "room1=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room1value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room2=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room2value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room3=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room3value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room4=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room4value=family2; domain=" + domain + "; path=/;";
			}
			else if (f.family2.selectedIndex==5)
			{	
				document.cookie = "room1=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room1value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room2=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room2value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room3=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room3value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room4=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room4value=family2; domain=" + domain + "; path=/;";
				document.cookie = "room5=" + f.family2.selectedIndex+ "; domain=" + domain + "; path=/;";
				document.cookie = "room5value=family2; domain=" + domain + "; path=/;";
			}
		}
		else
		{
			document.cookie = "room1=0; domain=" + domain + "; path=/;";
			document.cookie = "room1value=;domain=" + domain + "; path=/;";
			document.cookie = "room2=0; domain=" + domain + "; path=/;";
			document.cookie = "room2value=;domain=" + domain + "; path=/;";
		}
	}

	document.cookie = "kidsage=" + f.kidsage.value + "; domain=" + domain + "; path=/;";
	document.cookie = "room3=0; domain=" + domain + "; path=/;";
	document.cookie = "room4=0; domain=" + domain + "; path=/;";
	document.cookie = "room5=0; domain=" + domain + "; path=/;";
	document.cookie = "room3value=;domain=" + domain + "; path=/;";
	document.cookie = "room4value=;domain=" + domain + "; path=/;";
	document.cookie = "room5value=;domain=" + domain + "; 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=" + domain + "; 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);
			*/

			if (broken_cookie[k].substring(0,11)=='room1value=')
			{
				roomtypename = broken_cookie[k].substring(11);
				found=false;
				i=0;
				while (i<6 && !found)
				{
					if (roomtypename=='family1')
					{
						f.family1.selectedIndex=i;
						found=true;
					}
					else i++;
				}
			}
			if (broken_cookie[k].substring(0,11)=='room2value=')
			{
				roomtypename = broken_cookie[k].substring(11);
				found=false;
				i=0;
				while (i<6 && !found)
				{
					if (roomtype=='family2')
					{
						f.roomtype2.selectedIndex=i;
						found=true;
					}
					else i++;
				}
			}
		}
	}
	return dateset;
}

function trimString(str)
{
	return str.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s+/g," ");
}
