var da = (document.all) ? 1 : 0;
var pr = (window.print) ? 1 : 0;
var mac = (navigator.userAgent.indexOf("Mac") != -1); 
function printPage() 
{
	if (pr) // NS4, IE5
		window.print()
	else if (da && !mac) // IE4 (Windows)
		vbPrintPage()
	else // other browsers
		alert("Sorry, your browser doesn't support this feature. Please press Control-P on a PC or Command-P on a Mac to print this page.");  
	return false;
}
if (da && !pr && !mac) with (document) {
  writeln('<OBJECT ID="WB" WIDTH="0" HEIGHT="0" CLASSID="clsid:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>');
  writeln('<' + 'SCRIPT LANGUAGE="VBScript">');  
  writeln('Sub window_onunload');
  writeln('  On Error Resume Next');  
  writeln('  Set WB = nothing');
  writeln('End Sub');  
  writeln('Sub vbPrintPage');
  writeln('  OLECMDID_PRINT = 6');
  writeln('  OLECMDEXECOPT_DONTPROMPTUSER = 2');
  writeln('  OLECMDEXECOPT_PROMPTUSER = 1');  
  writeln('  On Error Resume Next');
  writeln('  WB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER');
  writeln('End Sub');  
  writeln('<' + '/SCRIPT>');
  }

function openWindow(imageName,imageWidth,imageHeight,alt) {
	newWindow = window.open("","newWindow","width="+imageWidth+",height="+imageHeight);
	newWindow.document.open();
	newWindow.document.write('<HTML><TITLE>'+alt+'</TITLE><BODY bgcolor="#FFFFFF" LEFTMARGIN="0" TOPMARGIN="0" MARGINHEIGHT="0" MARGINWIDTH="0" onBlur="self.close()">'); 
	newWindow.document.write('<IMG SRC='+imageName+' WIDTH='+imageWidth+' HEIGHT='+imageHeight+' ALT='+alt+'>'); 
	newWindow.document.write('</BODY></HTML>');
	newWindow.document.close();
	newWindow.focus();
} 

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("##")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf('?'))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validateenrollmentForm()
{
	var isgood = true;
	var radio_choice = false;
	
if (isgood)	
	if (!cal_prs_date2(document.forms[0].TripBeginDate.value))
		{
			document.forms[0].TripBeginDate.value = "";
			isgood = false;
		}

if (isgood)	
	if (!cal_prs_date2(document.forms[0].TripEndDate.value))
		{
			document.forms[0].TripEndDate.value = "";
			isgood = false;
		}
		
if (isgood)
{
	var now = new Date();
	now = new Date(now.toDateString());
	var dend = new Date(document.forms[0].TripEndDate.value);
	var dbegin = new Date(document.forms[0].TripBeginDate.value);
	if ( dbegin > dend  )
	{
		alert('The trip begin date cannot be after the trip end date.');
		isgood = false;
	}
	if(isgood)
		if ( dbegin < now  )
		{
			alert('Begin date can not be in the past.');
			isgood = false;
		}
	
	now.setYear( now.getFullYear() + 1 );
	if (isgood)
		if (dend >= now)
		{
			alert('The trip end date must be within the next year.');
			isgood = false;
		}
}

if (isgood)
	if(document.forms[0].travelfrom.selectedIndex == 0)
		{
			alert('You must select a country of citizenship');
			isgood = false;
		}

if (isgood)	
	if(document.forms[0].typeofcoverage.selectedIndex == 0)
		{
			alert('You must select a type of coverage');
			isgood = false;
		}

if (isgood)	
{
	radio_choice = false;
	for (counter = 0; counter < document.forms[0].DestinationIncludesUS.length; counter++)
	{
		if (document.forms[0].DestinationIncludesUS[counter].checked)
			radio_choice = true; 
	}
	if( !radio_choice )
	{
			alert('Please select an option for "Travel Includes US"');
			isgood = false;
	}
}

if (isgood)	
{
	radio_choice = false;
	for (counter = 0; counter < document.forms[0].Student.length; counter++)
	{
		if (document.forms[0].Student[counter].checked)
			radio_choice = true; 
	}
	
	if( !radio_choice )
		{
				alert('Please select an option for "Study Abroad?"');
				isgood = false;
		}
}
		
if (isgood)	
	if( document.forms[0].travelfrom.value == 'United States' &&
	   	document.forms[0].DestinationIncludesUS[0].checked &&
		document.forms[0].typeofcoverage.value == 1)
		{
			alert('No medical insurance plan with evacuation coverage is available for residents of the US traveling within the US.');
			isgood = false;
		}

if (isgood)
	if( document.forms[0].travelfrom.value == 'United States' &&
	   	document.forms[0].DestinationIncludesUS[0].checked &&
		document.forms[0].typeofcoverage.value == 2 &&
		document.forms[0].Student.checked)
		{
			alert('No medical insurance plan with evacuation coverage is available for residents of the US traveling within the US.');
			isgood = false;
		}

	if (isgood)
	{
		document.forms[0].submit();
	}
}

function openhelp()
{
	newWindow = window.open("ppchelp.html","Help","width=420,height=300,resizable=yes,scrollbars=yes");
}
function openpage(page)
{
	newWindow = window.open(page,"MEDEX","width=420,height=300,resizable=yes,scrollbars=yes");
}

function PlanChoiceValidation(thisform)
{
	if (thisform.sel_right.length <= 0){
		alert('You must select at least one destination country.');
		return false;
	}
	
	ctr = 0
	while (ctr < thisform.sel_right.length){
		if (thisform.sel_right.options[ctr].text == thisform.travelfrom.options[thisform.travelfrom.selectedIndex].text){
			alert("You're travel may not include your country of residence");
			return false;
		}			
	ctr = ctr + 1;
	}
	
	ctr = 1;
	travelerCtr = 0;
	travelerInSeventyBand = false;
	travelerInEightyBand = false;
	
	while (ctr <= 10){
		thistraveler = eval('thisform.travelers_'+ctr+'.value');		
		if (isNaN(thistraveler)){
			alert("You have specified an invalid age for traveler "+ctr+".  Please specify a numeric age");
			eval('thisform.travelers_'+ctr+'.value=""');	
			return false;		
	  	}else{

			if (thistraveler > 0){
				travelerCtr = travelerCtr + 1;
				
				if (thistraveler >= 80){
					travelerInEightyBand = true;
				}else if (thistraveler >= 70){
					travelerInSeventyBand = true;
				}
			}
	  	}
		ctr = ctr+1;
	}
	if (travelerCtr == 0){
		alert("You must enter at least one traveler's age");
		return false;
	}
	if (travelerInEightyBand){
		alert('Because one or more of your travelers is over 79, the maximum medical benefit available for that traveler is $10,000.  Any other travelers on this policy will have a maximum benefit of $50,000');
		thisform.medicalLimit.selectedIndex = 0;
	}
	if (travelerInSeventyBand){
		alert('Because one or more of your travelers is over between 70 and 79, the maximum medical benefit available for that traveler is $50,000.  Any other travelers on this policy will also have a maximum benefit of $50,000');
		thisform.medicalLimit.selectedIndex = 0;
	}
	if (!setdepartdate()){
		return false;
	}
	if (!setreturndate()){
		return false;
	}
	
	departdate = new Date(thisform.departdate.value);
	returndate = new Date(thisform.returndate.value);
	
	if (departdate >= returndate){
		alert('You must have an expiration date that is after your effective date');
		return false;
	}
	
	dayct = daysElapsed(returndate,departdate)
	
	if (dayct < 6){
		alert('Travmed Choice has a minimum coverage period of 7 days, please adjust your expiration date');
		return false;
	}
	if (dayct >= 364){
		alert('Travmed Choice has a maximum coverage period of 365 days, please adjust your expiration date');
		return false;
	}

	thisform.submit();
	return true;
}

function daysElapsed(date1,date2) {
    var difference = Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0) - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
   return difference/1000/60/60/24;
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function validateAge(field){
		if (isNaN(field.value)){
			alert("All ages must be in a numeric format");
			field.value="";
			field.focus();
		}
}

function doTooltip(e, msg) {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.show(e, msg);
}

function hideTip() {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.hide();
}