//---------------------------------------------------------------------------------------
// 1999 - 2006 (c) Total Travel Insurance
//
// date: 03/13/2006 1.0.
//---------------------------------------------------------------------------------------

function Validate()
{
    var c_no_trav       = 10;
    var c_no_dep		= 8;
    var c_max_age       = 120;
    var c_max_age_dep	= 18;
    var c_max_cost      = 300000;
    var c_max_cost_disp = "300,000";
    var c_max_length    = 365;

//	alert ("&txt_primary_sex=" + document.QuoteForm.txt_primary_sex[0].checked );
//	alert ("&txt_spouse_sex="  + document.QuoteForm.txt_spouse_sex[0].checked);
//	return;

//.......................................................................................
//... 1. Validate trip cost per traveler or total trip.
//.......................................................................................
	if (document.QuoteForm.txt_trip_cost.value < 0 || document.QuoteForm.txt_trip_cost.value == "")
		document.QuoteForm.txt_trip_cost.value = 0;

	var l_trip_cost;
	var l_trip_cost_tot;

	if (document.QuoteForm.txt_trip_cost_type.value == "EA")
	{
		l_trip_cost = document.QuoteForm.txt_trip_cost.value;
		l_trip_cost_tot = l_trip_cost * l_no_trav;
	}
	else
	{
		l_trip_cost_tot = document.QuoteForm.txt_trip_cost.value;
		l_trip_cost = l_trip_cost_tot / l_no_trav;
	}

	if (l_trip_cost_tot > c_max_cost)
	{
		alert("The Maximum Total Trip Cost allowed is " + c_max_cost_disp + ".");
		document.QuoteForm.txt_trip_cost.focus();
		return (false);
	}
//.......................................................................................
//... 2. Validate all dates.
//.......................................................................................
    if (!IsDate(document.QuoteForm.txt_depart_date.value))
    {
        alert("Please input valid departure date.");
        document.QuoteForm.txt_depart_date.focus();
        return (false);
    }
    
    var l_depart_date = new Date(document.QuoteForm.txt_depart_date.value);

//.......................................................................................
    
    if (!IsDate(document.QuoteForm.txt_return_date.value))
    {
        alert("Please input valid return date.");
        document.QuoteForm.txt_return_date.focus();
        return (false);
    }
    
    var l_return_date = new Date (document.QuoteForm.txt_return_date.value);

//.......................................................................................

	if	(document.QuoteForm.txt_quote_type.value == 1)
	{
		if (!IsDate(document.QuoteForm.txt_deposit_date.value))
		{
			alert("Please input valid deposit date.");
			document.QuoteForm.txt_deposit_date.focus();
			return (false);
		}
    
		var l_deposit_date = new Date(document.QuoteForm.txt_deposit_date.value);

//.......................................................................................
//... 2a. Validate deposit vs departure date.
//.......................................................................................
        
	    if (l_deposit_date.valueOf() > l_depart_date.valueOf())
		{
			alert("Deposit date cannot be after departure date.");
			document.QuoteForm.txt_deposit_date.focus();
			return (false);
		}
	}
//.......................................................................................
//... 2b. Validate departure date vs. today.
//.......................................................................................
	var l_app_date = new Date();

	var w_depart_date;
	var w_app_date;
	
	w_depart_date = l_depart_date.getFullYear();
	
	if (l_depart_date.getMonth() < 9)	w_depart_date = w_depart_date + '/0' + (l_depart_date.getMonth() + 1)
	else								w_depart_date = w_depart_date + '/'  + (l_depart_date.getMonth() + 1);
	
	if (l_depart_date.getDate() < 10)	w_depart_date = w_depart_date + '/0' + l_depart_date.getDate()
	else								w_depart_date = w_depart_date + '/'  + l_depart_date.getDate();
	
	w_app_date = l_app_date.getFullYear();
	
	if (l_app_date.getMonth() < 9)		w_app_date = w_app_date + '/0' + (l_app_date.getMonth() + 1)
	else								w_app_date = w_app_date + '/'  + (l_app_date.getMonth() + 1);
	
	if (l_app_date.getDate() < 10)		w_app_date = w_app_date + '/0' + l_app_date.getDate()
	else								w_app_date = w_app_date + '/'  + l_app_date.getDate();
    
    if (w_depart_date < w_app_date)
    {
        alert("Departure date cannot be prior to Today.\n\nWe process transactions in Eastern Standard Time.");
        document.QuoteForm.txt_depart_date.focus();
        return (false);
    }

	if (l_app_date.getHours() > 21)
	{
		if (w_depart_date == w_app_date)
	    {
	        alert("Departure date cannot be prior to today.\n\nWe process transactions in Eastern Standard Time.");
	        document.QuoteForm.txt_depart_date.focus();
	        return (false);
	    }
    }
//.......................................................................................
//... 2c. Validate return date vs. departure date.
//.......................................................................................
    if (l_return_date.valueOf() < l_depart_date.valueOf())
    {
        alert("Departure date cannot be after return date.");
        document.QuoteForm.txt_return_date.focus();
        return (false);
    }
//.......................................................................................
//... 3. Validate citizenship.
//.......................................................................................
	if	(document.QuoteForm.txt_quote_type.value == 2)
	{
		if (document.QuoteForm.txt_citizen.value == "")
		{
			alert("Please Select your 'Country of Citizenship' from the list.");
			document.QuoteForm.txt_citizen.focus();
			return (false);
		}
	}
//.......................................................................................
//... 4. Validate residence.
//.......................................................................................
    
	if (document.QuoteForm.txt_reside_id.value == "")
	{
		alert("Please select where do you live 'US State / Canadian Province' or 'Part of the World' from the list.");
		document.QuoteForm.txt_reside_id.focus();
		return (false);
	}
//.......................................................................................
//... 5. Validate destination.
//.......................................................................................
	if	(document.QuoteForm.txt_quote_type.value == 2)
	{
		if (document.QuoteForm.txt_destination_id.value == -1)
		{
			alert("Please select your Primary Destination.");
			document.QuoteForm.txt_destination_id.focus();
			return (false);
		}
    }
//.......................................................................................
//... 6. Validate school.
//.......................................................................................
	if	(document.QuoteForm.txt_quote_type.value == 2)
	{
		if (document.QuoteForm.txt_student_flag[0].checked  == true)
		{
			if (document.QuoteForm.txt_school_id.value == "")
			{
				alert("Please input University/College that you will be attending.");
				document.QuoteForm.txt_school_id.focus();
				return (false);
			}
		}
    }
//.......................................................................................
//... 7. Med - validate age.
//.......................................................................................
	if	(document.QuoteForm.txt_quote_type.value == 2)
	{
		if (document.QuoteForm.txt_primary_age.value == 0)
		{
			alert("Please input your Primary Traveler's age.");
			document.QuoteForm.txt_primary_age.focus();
			return (false);
		}
    }
//.......................................................................................
//... 8. Med - determine number of dependents.
//.......................................................................................
	if	(document.QuoteForm.txt_quote_type.value == 2)
	{
		for (i = 0; i < c_no_dep; i ++)
		{
			var l_age = new Number(document.QuoteForm.txt_child_age[i].value)
		
			if (l_age > c_max_age_dep)
			{
				alert("The Maximum Age allowed for your dependents is "				+ 
						c_max_age_dep + ".\n\nIf your children are older, "			+
						"you need to obtain a separate policy for them.");
				document.QuoteForm.txt_child_age[i].focus();
				return (false);
			}
		}
    }
//.......................................................................................
//... 9. Determine number of travelers.
//.......................................................................................
	if	(document.QuoteForm.txt_quote_type.value == 1)
	{
	    var l_no_trav = 0;
    
	    for (i = 0; i < c_no_trav; i ++)
	    {
			if (document.QuoteForm.txt_age[i].value > 0) 
				l_no_trav ++;
		}
    
		if (l_no_trav == 0)
		{
			alert("Please input the age of the traveler.");
			document.QuoteForm.txt_age[0].focus();
			return (false);
		}
	}    
//.......................................................................................
//... 9b. Determine oldest traveler.
//.......................................................................................
	if	(document.QuoteForm.txt_quote_type.value == 1)
	{
		var l_max_age = 0
    
		for (i = 0; i < c_no_trav; i ++)
		{
			var l_age = new Number(document.QuoteForm.txt_age[i].value)
		
			if (l_age > c_max_age)
			{
				alert("The Maximum Age allowed is " + c_max_age + ".");
				document.QuoteForm.txt_age[i].focus();
				return (false);
			}
			if (l_age > l_max_age) l_max_age = l_age;
		}
    }
//.......................................................................................
	return (true);
}
//.......................................................................................
// Delete

function initialise() 
{
	if (document.QuoteForm.txt_reside_id.value == "" && document.QuoteForm.txt_reside_id.selectedIndex != -1) 
		document.QuoteForm.txt_reside_id.selectedIndex = -1;
}
//.......................................................................................

function change_destination() 
{
	if	(document.QuoteForm.txt_quote_type.value == 2)
	{
		if (document.QuoteForm.txt_student_flag[0].checked)
		{
			location.href =	document.QuoteForm.txt_page_return.value									+
						"?page_name="			+ document.QuoteForm.txt_page_name.value				+
						"&txt_visitor_id="		+ document.QuoteForm.txt_visitor_id.value				+
						"&txt_quote_type="		+ document.QuoteForm.txt_quote_type.value				+
						"&txt_deposit_date="	+ document.QuoteForm.txt_deposit_date.value				+
						"&txt_depart_date="		+ document.QuoteForm.txt_depart_date.value				+
						"&txt_return_date="		+ document.QuoteForm.txt_return_date.value				+
						"&txt_trip_cost_type="	+ document.QuoteForm.txt_trip_cost_type.value			+
						"&txt_trip_cost="		+ document.QuoteForm.txt_trip_cost.value				+
						"&txt_reside_id="		+ document.QuoteForm.txt_reside_id.value				+
						"&txt_citizen="			+ document.QuoteForm.txt_citizen.value					+
						"&txt_coverage_usa="	+ document.QuoteForm.txt_coverage_usa[0].checked		+
						"&txt_coverage_can="	+ document.QuoteForm.txt_coverage_can[0].checked		+
						"&txt_student_flag="	+ document.QuoteForm.txt_student_flag[0].checked		+
						"&txt_school_id="		+ document.QuoteForm.txt_school_id.value				+
						"&txt_missionary_flag=0"														+
						"&txt_marine_flag=0"															+
						"&txt_primary_age="		+ document.QuoteForm.txt_primary_age.value				+
						"&txt_primary_sex="		+ document.QuoteForm.txt_primary_sex[0].checked			+
						"&txt_spouse_age="		+ document.QuoteForm.txt_spouse_age.value				+
						"&txt_spouse_sex="		+ document.QuoteForm.txt_spouse_sex[0].checked			+
						"&txt_child_age1="		+ document.QuoteForm.txt_child_age[0].value				+
						"&txt_child_age2="		+ document.QuoteForm.txt_child_age[1].value				+
						"&txt_child_age3="		+ document.QuoteForm.txt_child_age[2].value				+
						"&txt_child_age4="		+ document.QuoteForm.txt_child_age[3].value				+
						"&txt_child_age5="		+ document.QuoteForm.txt_child_age[4].value				+
						"&txt_child_age6="		+ document.QuoteForm.txt_child_age[5].value				+
						"&txt_child_age7="		+ document.QuoteForm.txt_child_age[6].value				+
						"&txt_child_age8="		+ document.QuoteForm.txt_child_age[7].value				+
						"&txt_age1="			+ document.QuoteForm.txt_age[0].value					+
						"&txt_age2="			+ document.QuoteForm.txt_age[1].value					+
						"&txt_age3="			+ document.QuoteForm.txt_age[2].value					+
						"&txt_age4="			+ document.QuoteForm.txt_age[3].value					+
						"&txt_age5="			+ document.QuoteForm.txt_age[4].value					+
						"&txt_age6="			+ document.QuoteForm.txt_age[5].value					+
						"&txt_age7="			+ document.QuoteForm.txt_age[6].value					+
						"&txt_age8="			+ document.QuoteForm.txt_age[7].value					+
						"&txt_age9="			+ document.QuoteForm.txt_age[8].value					+
						"&txt_age10="			+ document.QuoteForm.txt_age[9].value					+
						"&txt_destination_id="	+ document.QuoteForm.txt_destination_id.value			+
						"&txt_airline_id="		+ document.QuoteForm.txt_airline_id.value				+
						"&txt_cruise_id="		+ document.QuoteForm.txt_cruise_id.value				+
						"&txt_tour_op_id="		+ document.QuoteForm.txt_tour_op_id.value				+
						"&txt_rental_id="		+ document.QuoteForm.txt_rental_id.value
		}
	}	
}
//.......................................................................................
function hide_school()
{
	if	(document.QuoteForm.txt_quote_type.value == 2)	
	{
		document.QuoteForm.txt_school_id.style.visibility      = "hidden";
		document.getElementById("lbl_school").style.visibility = "hidden";
	}
}
//.......................................................................................
function show_school()
{
	if (document.QuoteForm.txt_destination_id.value == -1)
	{
		alert("Please select your Primary Destination.");
		document.QuoteForm.txt_destination_id.focus();
		return (false);
	}
	var msg = 'Are you a full time student? '										+
				'\n\n"Full-time Student" is defined as a student at a college '		+
					'or university'													+
					'\n   who is taking 10 credit hours (undergraduate students)'	+
					'\n   or 6 credit hours (graduate students).'
	
	var ask = confirm(msg)
	
	if  (ask)	change_destination();
	else
	{		
		document.QuoteForm.txt_student_flag[0].checked = false;
		document.QuoteForm.txt_student_flag[1].checked = true;

		hide_school()
	}
}
//---------------------------------------------------------------------------------------
