var start=new Date(stref.getFullYear(),stref.getMonth(),stref.getDate());
var endd=new Date(stref.getFullYear(),stref.getMonth()+1,stref.getDate()-1);
var temp= new Date(stref.getFullYear(),stref.getMonth()+1,stref.getDate()-1);

function iniForm()
{
	if(document.referrer)
	{
			document.form1.Age.value = hidden[0]!=''?hidden[0]:'';
			document.form1.spouseAge.value = hidden[1]!=''?hidden[1]:'';
			document.form1.MaxPolicyLimit.value=hidden[2]!=''?hidden[2]:"26000-60000";
			document.form1.Deductible.value=hidden[3]!=''?hidden[3]:"101-250";

			document.form1.DepartureDay.value = hidden[5]!=''?hidden[5]: stref.getDate();
			document.form1.DepartureMonth.value = hidden[6]!=''?hidden[6]: stref.getMonth() + 1;
			document.form1.DepartureYear.value = hidden[7]!=''?hidden[7]: stref.getFullYear();
			document.form1.ReturnDay.value = hidden[8]!=''?hidden[8]: temp.getDate();
			document.form1.ReturnMonth.value = hidden[9]!=''?hidden[9]: temp.getMonth() + 1;
			document.form1.ReturnYear.value = hidden[10]!=''?hidden[10]: temp.getFullYear();
			
			if(hidden[11]!=''&&hidden[12]!=''&&hidden[8]==''&&hidden[9]==''&&hidden[10]=='')
			{
				setdropDowns(hidden[11],hidden[12]); 
			}
			document.form1.monthsOfCoverage.value = hidden[11]!=''?hidden[11]:1;
			document.form1.daysOfCoverage.value = hidden[12]!=''?hidden[12]:0;
			document.form1.NumberOfChildren.value= hidden[4]!=''?hidden[4]:0;
			showtextbox(document.form1.NumberOfChildren.value);
			restorevalues(document.form1.NumberOfChildren.value);

			document.form1.CountryofCitizenship.value=hidden[15]!=''?hidden[15]:"2";
			if(hidden[15]=="1")
				 {	questiondisplay('USA');
					document.form1.CountryforCoverage.value = hidden[16]!=''?hidden[16]:"4";
					document.form1.MailingAddress.value = hidden[13]!=''?hidden[13]:"AL";
					document.form1.Residence.value = hidden[14]!=''?hidden[14]:"AL";
				}
			else {	questiondisplay('NONUSA');
					document.form1.CountryforCoverage.value = hidden[16]!=''?hidden[16]:"1";
					document.form1.MailingAddress.value = hidden[13]!=''?hidden[13]:"2";
					document.form1.Residence.value = hidden[14]!=''?hidden[14]:"2";
					document.form1.USarrival.value = hidden[17]!=''?hidden[17]:"5";					
				}
				priorCvgQuestion();
				if (document.form1.PriorCoverage)
				{
					if (hidden[24] == "0")
						document.form1.PriorCoverage[1].checked = true;
					else
						document.form1.PriorCoverage[0].checked = true;
				}
}
else 
	{	
		setDates(stref,temp,1);
		document.form1.NumberOfChildren.value = 0;
		hidetextbox();
	}
}
function resetform()
{
	document.form1.reset();
	temp.setFullYear(stref.getFullYear(),stref.getMonth()+1,stref.getDate()-1);
	setDates(stref,temp,1);
	questiondisplay('NONUSA');
	hidetextbox();
}
function validate()
{
	if (!validVisitorAge())  return false;
 	if (!validSpouseAge()) return false;
	if (!validStartDate(start, stref)) return false;
	if (!validEndDate(endd, start)) return false;
	if (!validMonthsOfCoverage()) return false;
	if (!validDepChildrenAge()) return false;
	
	if(conditionalparameters())
	{
		document.form1.quote.disabled=true;
		document.form1.resetquote.disabled=true;
		document.form1.submit();
	}
}

function arrivaldisplay(s)
{
	if(s.value=="4")
	{
		document.getElementById("q11a").innerHTML='';
		document.getElementById("q11b").innerHTML='';
		document.getElementById("q11c").innerHTML='';
	}
	else
	{
		if (document.form1.CountryofCitizenship.value==2)
		{		
			document.getElementById("q11a").innerHTML='<span class="formblackbold">11.</span>';
			document.getElementById("q11b").innerHTML='<span class="formblackbold">If traveler already in USA, how long has the traveler been in USA </span><div style="padding-top: 2px" class="formsmallfont">Choose your latest entry to USA.</div>';
			document.getElementById("q11c").innerHTML='<select size="1" name="USarrival" onchange="priorCvgQuestion();">' + getUSArrivalHTML() +'</select> ' ;
		}
	}
}

//This function displays the question for prior insurance coverage if the Non US citizen applicant/spouse's age > 65
//and seeks coverage in or including USA and arrived in the US less than 30 days ago.
function priorCvgQuestion()
{
	var age, sage, usarr, ctz, cvg;
	age = document.form1.Age.value;
	sage = document.form1.spouseAge.value;

	if (document.form1.USarrival)
		usarr = document.form1.USarrival.value;
	else
		usarr = -1;
	cvg = document.form1.CountryforCoverage.value;

	if ((age >=65 || sage >= 65) && usarr < 5 && (cvg != 4))
	{		
		if (document.form1.PriorCoverage)
			return;
		var text = "<input type='radio' name='PriorCoverage' checked='checked' value='1'> Yes &nbsp; &nbsp";
		text += "<input type='radio' name='PriorCoverage' value='0'> No";
		document.getElementById("q12a").innerHTML="&nbsp;12.";
		document.getElementById("q12b").innerHTML= "Has the applicant/spouse aged 65 or more been covered by international travel insurance during the last 30 days?";
		document.getElementById("q12c").innerHTML= text;		
	}
	else
	{
		document.getElementById("q12a").innerHTML="";
		document.getElementById("q12b").innerHTML= "";
		document.getElementById("q12c").innerHTML= "";
	}
}

function conditionalparameters()
{
	var a,s,x,d, cmn_msg;
	
	a=parseInt(document.form1.Age.value,10);
	s=parseInt(document.form1.spouseAge.value,10)?s!='':0;
	x=parseInt(document.form1.MaxPolicyLimit.value,10);
	d=parseInt(document.form1.Deductible.value,10);
	
	if(x==0)x=25000;
	if(x==26000)x=60000;
	if(x==61000)x=75000; 
	if(x==76000)x=100000; 
	if(x==101000)x=499000; 	

	cmnmsg = 'Please change your selection and then click "Get Quote"';
	
	ct=document.form1.CountryofCitizenship.value;

	/*if ((a>80||s>80)&&((x >100000&&ct==1)||(x >50000&&ct==2)))
	{
		alert('For travelers who are 80 yrs and above, the maximum coverage amount has to be $25,000 or $50,000 or $100,000(US citizen and deductible $50).\n'+ cmnmsg);
		 document.form1.MaxPolicyLimit.focus();
		 return false;
	} */

	if ((a>79||s>79)&&(x >100000&&ct==1))
	{
		alert('For US citizens who are 80 yrs and above, the maximum coverage amount has to be $26,000 or $60,000 or $100,000 for a non-zero deductible; maximum coverage should be $25,000 or less for a $0 deductible.\n'+ cmnmsg);
		document.form1.MaxPolicyLimit.focus();
		return false;
	}

	if ((a>79||s>79)&&(x >75000&&ct==2))
	{
		alert('For Non US citizens who are 80 yrs and above, the maximum coverage has to be $26,000 or $60,000 or $75,000 for a non-zero deductible; maximum coverage should be $25,000 or less for a $0 deductible.\n'+ cmnmsg);
		 document.form1.MaxPolicyLimit.focus();
		 return false;
	}

	/*if ((a>70||s>70)&&((x >250000&&ct==1)||(x >100000&&ct==2)) )
	{
		alert('For travelers who are 70 yrs and above, the maximum coverage has to be $25,000, $50,000 or $100,000(Non-US citizen) or $250,000(US citizen).\n'+ cmnmsg);
		 document.form1.MaxPolicyLimit.focus(); 
		 return false;
	}*/
	if (((a>69 && a<80)||(s>69 && s<80))&& (x >1000000&&ct==1))
	{
		alert('For US citizens who are 70 yrs and above, the maximum coverage has to be $1,000,000 or less; For deductibles more than $1,000, maximum coverage has to be less than $500,000.\n'+ cmnmsg);
		 document.form1.MaxPolicyLimit.focus(); 
		 return false;
	}
	if (((a>69 && a<80)||(s>69 && s<80))&&(x >100000&&ct==2))
	{
		alert('For Non US citizens who are 70 yrs and above, the maximum coverage amount has to be $26,000, $60,000, $75,000 or $100,000.\n'+ cmnmsg);
		 document.form1.MaxPolicyLimit.focus(); 
		 return false;
	}
	if ((a>79||s>79) && d==0 && x>26000)
	{
		alert('For all travelers who are 80 yrs and above requiring a $0 deductible, maximum policy coverage has to be $25,000.\n'+ cmnmsg);
		 document.form1.Deductible.focus(); 
		 return false;	
	}
	if ((a>79||s>79) && d>2500 && x>26000)
	{
		alert('For all travelers who are 80 yrs and above requiring deductible above 2500, maximum policy coverage has to be $25,000.\n'+ cmnmsg);
		 document.form1.Deductible.focus(); 
		 return false;	
	}
	if ( ((a>69&&a<80)||(s>69&&s<80)) && d==0  && x==25000)
	{
		alert('For all travelers between 70-79 yrs requiring a $0 deductible, maximum policy coverage has to be $26,000 or more.\n'+ cmnmsg);
		document.form1.Deductible.focus();
		return false;
	} 
	if ( ((a>69&&a<80)||(s>69&&s<80)) && d>101 && x==25000)
	{
		alert('For all travelers between 70-79 yrs requiring deductible above 101, maximum policy coverage has to be $26,000 or more.\n'+ cmnmsg);
		document.form1.Deductible.focus();
		return false;
	} 	

	switch(x)
	{
		case 25000:
			if ((a<70 || s<70)&& d>101)
			{
				alert('For travelers younger than 70yrs, the deductible has to be $250 or less.\n'+ cmnmsg);
				document.form1.Deductible.focus(); 
				return false;	
			}
			break;
		case 60000:
			if ((a<80 || s<80)&& d>5000)
			{
				alert('For travelers younger than 80yrs, the deductible has to be $5000 or less.\n'+ cmnmsg);
				document.form1.Deductible.focus(); 
				return false;	
			}
			break;
		case 100000:
	  		if ((a<70 || s<70)&& d>5000)
			{
				alert('For travelers younger than 70yrs, the deductible has to be $5000 or less.\n'+ cmnmsg);
				document.form1.Deductible.focus(); 
				return false;	
			}
			break;

		case 499000:
			if ((a<70)&& d>5000)
			{
				alert('For travelers younger than 70yrs, the deductible has to be $5000 or less.\n'+ cmnmsg);
				document.form1.Deductible.focus(); 
				return false;	
			}	
			break;	
		case 500000:	  	
			if ((a<70) && d > 5000)
			{
				alert('For all travelers younger than 70 yrs, the deductible has to be $5000 or less\n'+ cmnmsg);
				document.form1.Deductible.focus(); 
				return false;	
			}
			break;	
	}
	return true;
}


