function iniDates()
{
	document.form1.DepartureDay.value = stref.getDate();
	document.form1.DepartureMonth.value = stref.getMonth() + 1;
	document.form1.DepartureYear.value = stref.getFullYear();		
	document.form1.ReturnDay.value = temp.getDate();
	document.form1.ReturnMonth.value = temp.getMonth() + 1;
	document.form1.ReturnYear.value = temp.getFullYear();
}

function resetform()
{
	//document.form1.reset();
                document.form1.CountryOfResidence.selectedIndex = 0;
                document.form1.Age1.value = "";
                document.form1.Age2.value = "";
                document.form1.Age3.value = "";
                document.form1.Age4.value = "";
                document.form1.Age5.value = "";
                document.form1.TripCost1.value = "";
                document.form1.TripCost2.value = "";
                document.form1.TripCost3.value = "";
                document.form1.TripCost4.value = "";
                document.form1.TripCost5.value = "";
	temp.setFullYear(stref.getFullYear(),stref.getMonth()+1,stref.getDate()-1);
	document.form1.DepartureYear.value=stref.getFullYear();
	document.form1.DepartureMonth.value=stref.getMonth()+1;
	document.form1.DepartureDay.value=stref.getDate();
	document.form1.ReturnYear.value=temp.getFullYear();
	document.form1.ReturnMonth.value=temp.getMonth()+1;
	document.form1.ReturnDay.value=temp.getDate();
}

function iniForm()
{
	var tmp;

	if(document.form1.tl.value!='')
	{
		tmp=new Date(stref.getFullYear(),stref.getMonth(),stref.getDate()+parseInt(document.form1.tl.value,10)-1);
	}
	else
		tmp=temp;
	
	if(document.referrer)
	{			
		document.form1.DepartureDay.value = document.form1.DD.value!=''?document.form1.DD.value: stref.getDate();
		document.form1.DepartureMonth.value = document.form1.DM.value!=''?document.form1.DM.value: stref.getMonth() + 1;
		document.form1.DepartureYear.value = document.form1.DY.value!=''?document.form1.DY.value: stref.getFullYear();
		document.form1.ReturnDay.value = document.form1.RD.value!=''?document.form1.RD.value: tmp.getDate();
		document.form1.ReturnMonth.value = document.form1.RM.value!=''?document.form1.RM.value: tmp.getMonth()+1;
		document.form1.ReturnYear.value = document.form1.RY.value!=''?document.form1.RY.value: tmp.getFullYear();
		document.form1.CountryOfResidence.value= document.form1.ra.value!=''?document.form1.ra.value:'';
		for(var i=1; i <= 5; i++)
			if(document.getElementById("ta"+i).value!=-1)
			{
				document.getElementById("Age"+i).value=document.getElementById("ta"+i).value;
				document.getElementById("TripCost"+i).value=document.getElementById("tc"+i).value;
			}						
	}
	else
	{iniDates();}
}

function validate()
{
	//check that atleast one of the traveler's ages is entered
	if (checkFieldBlank(document.form1.Age1.value) == false)
	{
		alert("Please enter the age of the first traveler and then click Get Quote.");
		return false;
	}

	//check that the place of residence is selected
	if ((document.form1.CountryOfResidence.value)==false)
	{
		alert("Please select the correct place of residence.");
		return false;
	 }

	//check if trip cost corresponding to traveler's age is entered
	if ((checkFieldBlank(document.form1.Age1.value) == true) && (checkFieldBlank(document.form1.TripCost1.value) == false))
	{
		alert("Please enter the trip cost for the first traveler and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age2.value) == true) && (checkFieldBlank(document.form1.TripCost2.value) == false))
	{
		alert("Please enter the trip cost for the second traveler and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age3.value) == true) && (checkFieldBlank(document.form1.TripCost3.value) == false))
	{
		alert("Please enter the trip cost for the third traveler and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age4.value) == true) && (checkFieldBlank(document.form1.TripCost4.value) == false))
	{
		alert("Please enter the trip cost for the fourth traveler and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age5.value) == true) && (checkFieldBlank(document.form1.TripCost5.value) == false))
	{
		alert("Please enter the trip cost for the fifth traveler and then click Get Quote.");
		return false;
	}

	//check if age corresponding to trip cost is entered
	if ((checkFieldBlank(document.form1.Age2.value) == false) && (checkFieldBlank(document.form1.TripCost2.value) == true))
	{
		alert("Please enter the age for the second traveler and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age3.value) == false) && (checkFieldBlank(document.form1.TripCost3.value) == true))
	{
		alert("Please enter the age for the third traveler and then click Get Quote.");
 		return false;
	}

	if ((checkFieldBlank(document.form1.Age4.value) == false) && (checkFieldBlank(document.form1.TripCost4.value) == true))
	{
		alert("Please enter the age for the fourth traveler and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age5.value) == false) && (checkFieldBlank(document.form1.TripCost5.value) == true))
	{
		alert("Please enter the age for the fifth traveler and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age1.value) == true) && (checkFieldBlank(document.form1.Age2.value) == false) && (checkFieldBlank(document.form1.Age3.value) == true))
	{
		alert("Please enter the age for the second traveler and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age2.value) == true) && (checkFieldBlank(document.form1.Age3.value) == false) && (checkFieldBlank(document.form1.Age4.value) == true))
	{
		alert("Please enter the age for the third traveler and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age2.value) == false) && (checkFieldBlank(document.form1.Age3.value) == false) && (checkFieldBlank(document.form1.Age4.value) == true))
	{
		alert("Please enter the ages for the second and third travelers and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age3.value) == true) && (checkFieldBlank(document.form1.Age4.value) == false) && (checkFieldBlank(document.form1.Age5.value) == true))
	{
		alert("Please enter the age for the fourth traveler and then click Get Quote.");
		return false;
	}

	if ((checkFieldBlank(document.form1.Age3.value) == false) && (checkFieldBlank(document.form1.Age4.value) == false) && (checkFieldBlank(document.form1.Age5.value) == true))
	{
		alert("Please enter the ages for the third and fourth travelers and then click Get Quote.");
		return false;
	}

	//check that selected trip start date is valid
	 if (!validdate( document.form1.DepartureDay.value, document.form1.DepartureMonth.value-1, document.form1.DepartureYear.value))
	 {
		return false;
	 }

	 if (!validdate(document.form1.ReturnDay.value, document.form1.ReturnMonth.value-1, document.form1.ReturnYear.value))
	 {
		return false;
	 }

	//check that the departure date is after today
	var today=new Date()
	var departureDate=new Date(document.form1.DepartureYear.value, document.form1.DepartureMonth.value, document.form1.DepartureDay.value)
	if (today.getTime()>=departureDate.getTime())
	{
		alert("The trip start date has to be after today. Please reselect the date and click Get Quote.");
		return false;
	 }

	//check that the return date is after today
	var returnDate=new Date(document.form1.ReturnYear.value, document.form1.ReturnMonth.value, document.form1.ReturnDay.value)
	if (today.getTime()>=returnDate.getTime())
	{
		alert("The trip end date has to be after today. Please reselect the date and click Get Quote.");
		return false;
	 }

	//check that the return date is after the departure date
	if (departureDate.getTime()>=returnDate.getTime())
	{
		alert("The trip end date has to be after the trip start date. Please reselect the dates and click Get Quote.");
		return false;
	 }
	document.form1.quote.disabled=true;
	document.form1.resetquote.disabled=true;
	document.form1.submit();
}

function checkFieldBlank(field) 
{
	if (field == "") {return false;}
	else { return true;};
}