var days = new Array();
days[0] = "Dimanche";
days[1] = "Lundi";
days[2] = "Mardi";
days[3] = "Mercredi";
days[4] = "Jeudi";
days[5] = "Vendredi";
days[6] = "Samedi";

var month = new Array();
month[0] = "Janvier";
month[1] = "Fevrier";
month[2] = "Mars";
month[3] = "Avril";
month[4] = "Mai";
month[5] = "Juin";
month[6] = "Juillet";
month[7] = "Aout";
month[8] = "Septembre";
month[9] = "Octobre";
month[10] = "Novembre";
month[11] = "Decembre";

var mon = new Array();
mon[0] = "Jan";
mon[1] = "Fev";
mon[2] = "Mar";
mon[3] = "Avr";
mon[4] = "Mai";
mon[5] = "Juin";
mon[6] = "Juil";
mon[7] = "Aou";
mon[8] = "Sep";
mon[9] = "Oct";
mon[10] = "Nov";
mon[11] = "Dec";

function createDropdownDate(b,e,s)
{
	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+2); 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();
		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.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();
	f.departdate2.selectedIndex = f.arrivedate2.selectedIndex + (now.getMonth() - smonth);
}

function checkDate(d)
{
	thisdate = new Date();
	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\n\nLa date d'arrive slectionne n'est pas valable");
			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\n\nLa date de dpart slectionne n'est pas valable");
			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\n\nMerci de choisir une priode infrieure  31 jours");
			return false;
		}
		else if (dtime<0)
		{
			alert("Please select a longer period\n\nMerci de choisir une priode plus longue");
			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.setFullYear(year);
	now.setMonth(month);
	now.setDate(d);
	return now.getTime();
}

function getDayName(d,m)
{
	now = new Date();
	month = getMonthValue(m);
	year = getYearValue(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 checkEnteredDate()
{
	f = document.form;
	
	if (f.arrivedate.selectedIndex != 0 && f.arrivedate2.selectedIndex != 0)
	{
		passed = checkArrivalDate();
		if (passed < 0)
		{
			alert("The arrival date you have entered has already passed\n\nLa date d'arrive slectionne est dj expire");
			f.arrivedate.selectedIndex = 0;
			f.arrivedate2.selectedIndex = 0;
		}
		else
		{
			valid = validDate(f.arrivedate.selectedIndex,f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
			if (valid == 1)
			{
				alert("The arrival date you have entered is invalid\n\nLa date d'arrive slectionne n'est pas valable");
				f.arrivedate.selectedIndex = 0;
				f.arrivedate2.selectedIndex = 0;
			}
			else
			{
				setNights();
			}
		}
	}
}

function setNights()
{
	f = document.form;
	
	if (f.arrivedate.selectedIndex != 0 && f.arrivedate2.selectedIndex != 0 && f.departdate.selectedIndex != 0 && f.departdate2.selectedIndex != 0)
	{
		valid = validDate(f.departdate.selectedIndex,f.departdate2.options[f.departdate2.selectedIndex].value);
		if (valid==1)
		{
			alert("The end date you have entered is invalid\n\nLa date de dpart slectionne n'est pas valable");
			f.departdate.selectedIndex = 0;
			f.departdate2.selectedIndex = 0;
		}
		else
		{
			check = compareDates();
			if (check == 1)
			{
				alert("Departure date before arrival date\n\nDate de dpart mentionne avant date d'arrive");
				f.departdate.selectedIndex = 0;
				f.departdate2.selectedIndex = 0;
			}
			else if (check == 0)
			{
				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);
				f.nights.value = Math.round(dtime);
			}
		}
	}
}

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.setFullYear(syear);
	now.setMonth(smonth);
	now.setDate(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.setFullYear(2007);
	arr.setMonth(0);
	arr.setDate(1);

	arr.setFullYear(syear);
	arr.setMonth(smonth);
	arr.setDate(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.setFullYear(2007);
	dep.setMonth(0);
	dep.setDate(1);

	dep.setFullYear(eyear);
	dep.setMonth(emonth);
	dep.setDate(f.departdate.selectedIndex);
	
	checkdate = checkDate(now.getTime());

	if (dep.getTime()<=arr.getTime())
		check = 1;
	else
		check = 0;

	return check;
}

function selectYears(ad2, dd2)
{
	f = document.form;	
	for (i=0; i<f.arrivedate2.length; i++)
	{
		if (f.arrivedate2.options[i].value == ad2)
		{
			f.arrivedate2.selectedIndex = i;
		}
	}
	for (i=0; i<f.departdate2.length; i++)
	{
		if (f.departdate2.options[i].value == dd2)
		{
			f.departdate2.selectedIndex = i;
		}
	}
}