

function checkNA() {
	// If WW Inc NA is selected
	if ( document.getElementById('destination').options[document.getElementById('destination').selectedIndex].value == 'Worldwide inc USA & Canada' ) {

		if ( 	document.getElementById('departureDateDay').options[document.getElementById('departureDateDay').selectedIndex].value &&
				document.getElementById('departureDateMonth').options[document.getElementById('departureDateMonth').selectedIndex].value &&
				document.getElementById('departureDateYear').options[document.getElementById('departureDateYear').selectedIndex].value &&
				document.getElementById('returnDateDay').options[document.getElementById('returnDateDay').selectedIndex].value &&
				document.getElementById('returnDateMonth').options[document.getElementById('returnDateMonth').selectedIndex].value &&
				document.getElementById('returnDateYear').options[document.getElementById('returnDateYear').selectedIndex].value
		) {


			var startDate = new Date(	parseInt(document.getElementById('departureDateYear').options[document.getElementById('departureDateYear').selectedIndex].value, 10),
										parseInt(document.getElementById('departureDateMonth').options[document.getElementById('departureDateMonth').selectedIndex].value - 1, 10),
										parseInt(document.getElementById('departureDateDay').options[document.getElementById('departureDateDay').selectedIndex].value, 10),
										0,
										0,
										0,
										0);
			var endDate = new Date(		parseInt(document.getElementById('returnDateYear').options[document.getElementById('returnDateYear').selectedIndex].value, 10),
										parseInt(document.getElementById('returnDateMonth').options[document.getElementById('returnDateMonth').selectedIndex].value - 1, 10),
										parseInt(document.getElementById('returnDateDay').options[document.getElementById('returnDateDay').selectedIndex].value, 10),
										0,
										0,
										0,
										0);

			// Show NA Question if difference > 62 days
			var difference = 0;
			difference = ((endDate.getTime() - startDate.getTime()) / (1000*60*60*24)) + 1;

			if ( difference > 62 ) {
				document.getElementById('naQuestion').style.display = '';
			} else {
				document.getElementById('naQuestion').style.display = 'none';
			}


			// Show NA Dates if 'travelling outside of NA'
			if ( document.getElementById('checkNAYes').checked ) {
				document.getElementById('naDetails').style.display = '';
				document.getElementById('naStartSection').style.display = '';
				document.getElementById('naEndSection').style.display = '';

			} else {
				document.getElementById('naDetails').style.display = 'none';
				document.getElementById('naStartSection').style.display = 'none';
				document.getElementById('naEndSection').style.display = 'none';
			}
		} else {
			document.getElementById('naQuestion').style.display = 'none';
			document.getElementById('naDetails').style.display = 'none';
			document.getElementById('naStartSection').style.display = 'none';
			document.getElementById('naEndSection').style.display = 'none';
		}
	} else {
		document.getElementById('naQuestion').style.display = 'none';
		document.getElementById('naDetails').style.display = 'none';
		document.getElementById('naStartSection').style.display = 'none';
		document.getElementById('naEndSection').style.display = 'none';
	}
}

function checkInsurable() {
	// If we've chosen USA/Canada...
	if ( document.getElementById('destination').options[document.getElementById('destination').selectedIndex].value == 'Worldwide inc USA & Canada' ) {

		// Calculate the trip duration
		var startDate = new Date(
			parseInt(document.getElementById('departureDateYear').options[document.getElementById('departureDateYear').selectedIndex].value, 10),
			parseInt(document.getElementById('departureDateMonth').options[document.getElementById('departureDateMonth').selectedIndex].value - 1, 10),
			parseInt(document.getElementById('departureDateDay').options[document.getElementById('departureDateDay').selectedIndex].value, 10),
			0, 0, 0, 0
		);

		var endDate = new Date(
			parseInt(document.getElementById('returnDateYear').options[document.getElementById('returnDateYear').selectedIndex].value, 10),
			parseInt(document.getElementById('returnDateMonth').options[document.getElementById('returnDateMonth').selectedIndex].value - 1, 10),
			parseInt(document.getElementById('returnDateDay').options[document.getElementById('returnDateDay').selectedIndex].value, 10),
			0, 0, 0, 0
		);

		var num_days = ((endDate.getTime() - startDate.getTime()) / (1000*60*60*24)) + 1;

		if (num_days > 364) {
			alert('Following recent healthcare reforms in the US we cannot insure travellers who are planning to stay in the US for 365 days or more.');
		}
	}
}


function toggleThaiArea(show) {
	document.getElementById('thai_area').style.display = (show) ? '' : 'none';
}

/**
 * find the position of an element on the page
 * to use for floating divs...
 */
function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

/**
 * enable the next click to close any popups that we have open
 */
function setBodyClickHandler()
{
	document.onclick = function (event) {
		closePopups();
	}
}

/**
 * close any open popups...
 */
function closePopups()
{
	for (key in openPopups)
	{
		openPopups[key].className = "popup";
	}
	document.onclick = function(){};
	openPopups = new Array();
}

var openPopups = new Array();
/**
 * show a popup window...
 */
function showPopup(anchor, popupName)
{
	var popup = document.getElementById(popupName);
	if (openPopups.length > 0)
	{
		closePopups();
	}
	popup.className = "popupOver"
	position = findPos(anchor);
	//alert(position);
	popup.style.left = (popupName == 'popupwhere') ? (position[0]-30)+"px" : (position[0]+15)+"px";
	popup.style.top = position[1]+"px";
	openPopups[openPopups.length] = popup;
	setTimeout("setBodyClickHandler()", 1);
}

function toggleMoreInfo()
{
	var moreInfo = document.getElementById("moreinfo");
	if (moreInfo.style.display == "block")
	{
		moreInfo.style.display = "none";
	}
	else
	{
		moreInfo.style.display = "block";
	}
}
