var limitDate = new Date();		
var allowAMTStartDateChange = false;// set true when user press Cancel for AMT message in instantquote page


limitDate.setMonth(limitDate.getMonth() + 13);	

var d  = limitDate.getDate();
var day = (d < 10) ? '0' + d : d;
var m = limitDate.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = limitDate.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
var limitDateText = day + "/" + month + "/" + year;

function dissableBuyButton(name){
	document.getElementById(name).disabled = true;
}

function dissableChkBoxes(){
return;

	document.getElementById('bc_option_checkbox_cc').disabled = true;
	document.getElementById('bc_option_checkbox_pl').disabled = true;
	
	
	document.getElementById('st_option_checkbox_ew').disabled = true;
	document.getElementById('st_option_checkbox_ws').disabled = true;
	document.getElementById('st_option_checkbox_ge').disabled = true;
	document.getElementById('st_option_checkbox_pb').disabled = true;
	document.getElementById('st_option_checkbox_wc').disabled = true;
	document.getElementById('st_option_checkbox_si').disabled = true;
	
	document.getElementById('abc_option_checkbox_cc').disabled = true;
	document.getElementById('abc_option_checkbox_pl').disabled = true;
	
	document.getElementById('at_option_checkbox_ew').disabled = true;
	document.getElementById('at_option_checkbox_ww').disabled = true;
	document.getElementById('at_option_checkbox_dd').disabled = true;
	document.getElementById('at_option_checkbox_ge').disabled = true;
	document.getElementById('at_option_checkbox_pb').disabled = true;
	document.getElementById('at_option_checkbox_wc').disabled = true;
	document.getElementById('at_option_checkbox_si').disabled = true;

}

function uktoukCheck(){
	var objForm = document.frmStep1;
	var landingType = objForm.landingType.value;
	var departLocation = objForm.depart_location.value;
	var returnLocation = objForm.return_location.value;
	var uktouk = false;	if (((departLocation == 'uk')||(departLocation == 'uk1')||(departLocation == 'uk2')||(departLocation == 'uk3'))&&(returnLocation == 'uk')) uktouk = true;


	var days = objForm.days.value;
	if ((landingType=='normal')||(landingType=='over65')||(landingType=='wintersports')){
		if ((days == '')&&(uktouk)) document.getElementById('uktoukMessage').style.display='';
		else if ((days < 4)&&(uktouk)) document.getElementById('uktoukMessage').style.display='';
		else document.getElementById('uktoukMessage').style.display='none';
	}
}
function longStaySetDate(){

	var landing = document.frmStep1.landingType.value;
	if (landing!='longstay') return;
	var objForm = document.frmStep1;
	var objDays = objForm.days;
	var objReturnDate = objForm.return_date;
	var objDepartDate = objForm.depart_date;
	var strReturnDate = objReturnDate.value;
	var strDepartDate = objDepartDate.value;
		
	
	if (strDepartDate == '') return false;
	if (strReturnDate == '') return false;
	
	
	var intOneDay = (1000 * 60 * 60 * 24);
	var objRegExpDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	if (objRegExpDate.test(strDepartDate) == false) {
		return false;
	}
	
	if (objRegExpDate.test(strReturnDate) == false) {
		return false;
	}	
	
	
	arrDepartDate = strDepartDate.split('/');
	arrReturnDate = strReturnDate.split('/');
	
	objDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]);
	objReturnDate = new Date(arrReturnDate[2], arrReturnDate[1] - 1, arrReturnDate[0]);

	
	var intDaysDifference = Math.round( (objReturnDate - objDepartDate) / intOneDay );
	intDaysDifference = intDaysDifference + 1;


	
	objDays.selectedIndex = 0;
	if (intDaysDifference > 93 )  	objDays.selectedIndex = 1;
	if (intDaysDifference > 123 )  	objDays.selectedIndex = 2;
	if (intDaysDifference > 153 )  	objDays.selectedIndex = 3;
	if (intDaysDifference > 184 ) 	objDays.selectedIndex = 4;
	if (intDaysDifference > 215 ) 	objDays.selectedIndex = 5;
	if (intDaysDifference > 245 ) 	objDays.selectedIndex = 6;
	if (intDaysDifference > 276 ) 	objDays.selectedIndex = 7;
	if (intDaysDifference > 306 ) 	objDays.selectedIndex = 8;
	if (intDaysDifference > 337 ) 	objDays.selectedIndex = 9;
	if (intDaysDifference > 366 ) 	objDays.selectedIndex = 10;
	if (intDaysDifference > 397 ) 	objDays.selectedIndex = 11;
	if (intDaysDifference > 428 ) 	objDays.selectedIndex = 12;
	if (intDaysDifference > 458 ) 	objDays.selectedIndex = 13;
	if (intDaysDifference > 489 ) 	objDays.selectedIndex = 14;
	if (intDaysDifference > 550 ) 	objDays.selectedIndex = 15;
	
	daysChange(objDays);

}

function wrongReturnDateCheck(){
	var objForm = document.frmStep1;
	var landingType = objForm.landingType.value;
	

	var objReturnDate = objForm.return_date;
	var objDepartDate = objForm.depart_date;
	var strReturnDate = objReturnDate.value;
	var strDepartDate = objDepartDate.value;
		
	
	if (strDepartDate == '') return false;
	if (strReturnDate == '') return false;
	
	
	var intOneDay = (1000 * 60 * 60 * 24);
	var objRegExpDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	if (objRegExpDate.test(strDepartDate) == false) {
		return false;
	}
	
	if (objRegExpDate.test(strReturnDate) == false) {
		return false;
	}	
	
	
	arrDepartDate = strDepartDate.split('/');
	arrReturnDate = strReturnDate.split('/');
	
	objDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]);
	objReturnDate = new Date(arrReturnDate[2], arrReturnDate[1] - 1, arrReturnDate[0]);

	
	var intDaysDifference = Math.round( (objReturnDate - objDepartDate) / intOneDay );
	intDaysDifference = intDaysDifference + 1;
	if ((landingType=='normal')||(landingType=='over65')||(landingType=='wintersports')){
		if (intDaysDifference < 1) 	document.getElementById('wrongReturnMessage').style.display='';
		else  	document.getElementById('wrongReturnMessage').style.display='none';

	}
		
	

}


function checkData() {

	var objForm = document.frmStep1;
	var landingType = objForm.landingType.value;
	var isBackPacker;
	if (landingType == 'backpacker') isBackPacker = true;
	else isBackPacker = false;
    document.frmStep1.return_date.disabled = false;
	
	
	var objDepartLocation = objForm.depart_location;
	var objReturnLocation = objForm.return_location;
	
	var strDepartDate = objForm.depart_date.value;
	var strReturnDate = objForm.return_date.value;
	
	var objUnder2 		= objForm.under2;
	var objAged2to17 	= objForm.aged2to17;
	var objAged18to65 	= objForm.aged18to65;
	var objAged66to70 	= objForm.aged66to70;
	var objAged71to75 	= objForm.aged71to75;
	var objAged76to80 	= objForm.aged76to80;
	var objAged81to85 	= objForm.aged81to85;
	
	var objCouple = objForm.couple;
	var objSingleParentFamily = objForm.single_parent_family;
	var objFamily = objForm.family;
	var objPeople = objForm.people;
	
//	var objOfflineFname = objForm.offlineFname;
//	var objOfflineAddress1 = objForm.offlineAddress1;
//	var objOfflineEmail = objForm.offlineEmail;
	
	var strParty = 'single';
	
	
	var intUnder2 		= parseInt(objUnder2.value);
	var intAged2to17 	= parseInt(objAged2to17.value);
	var intAged18to65 	= parseInt(objAged18to65.value);
	var intAged66to70 	= parseInt(objAged66to70.value);
	var intAged71to75 	= parseInt(objAged71to75.value);
	var intAged76to80	= parseInt(objAged76to80.value);
	var intAged81to85	= parseInt(objAged81to85.value);
	
	if (intAged81to85>0) {
	 alert('80+ prices are currently unavailable until 1st October due to changing underwriters. We are sorry for any inconvenience caused. Please re-visit the site from 1st October 2009 onwards, alternatively for more information please call Customer Services - 0845 508 9886.');
	 objAged81to85.value=0;
	 intAged81to85=0;
	 }
	
	
	
	var intAdults 	= intAged18to65 + intAged66to70 + intAged71to75 + intAged76to80 + intAged81to85;
	var intBabies	= intUnder2;
	var intChildren = intAged2to17;
	objPeople.value = intAdults + intChildren + intBabies;
	
//	var strOfflineFname = objOfflineFname.value;
//	var strOfflineAddress1 = objOfflineAddress1.value;
//	var strOfflineEmail = objOfflineEmail.value;
	
	var objToday = new Date();
	var intOneDay = (1000 * 60 * 60 * 24);	
	var objDateDiff;
	
	//Check for customer information
//	if((strOfflineFname == '') || (strOfflineAddress1 == '') || (strOfflineEmail == '')){
//		alert('Please fill costomer information');
//		objOfflineFname.focus();
//		return false;
//	}	
	
	if (objDepartLocation.options[objDepartLocation.selectedIndex].value == '') {
		alert('Please select a departure location');
		objDepartLocation.focus();
		return false;
	}
	
	if (objReturnLocation.options[objReturnLocation.selectedIndex].value == '') {
		alert('Please select a return from location');
		objReturnLocation.focus();
		return false;
	}	

	
	
	//Check the departure and return dates;
	var objRegExpDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	if (objRegExpDate.test(strDepartDate) == false) {
		alert('Please enter a valid departure date in the format "dd/mm/yyyy"');
		return false;
	}
	
	if (objRegExpDate.test(strReturnDate) == false) {
		alert('Please enter a valid return date in the format "dd/mm/yyyy"');
		return false;
	}
	
	
	arrDepartDate = strDepartDate.split('/');
	arrReturnDate = strReturnDate.split('/');
	
	objDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]);

	objDateDiff = Math.ceil((objDepartDate.getTime() - objToday.getTime()) / intOneDay)
	if (objDateDiff < 1) {
		alert('Please enter a departure date in the future');
		return false;
	}
	
		 
	
	
	objReturnDate = new Date(arrReturnDate[2], arrReturnDate[1] - 1, arrReturnDate[0]);
	
	objDateDiff = Math.ceil((objReturnDate.getTime() + objReturnDate.getTimezoneOffset() - objDepartDate.getTime() - objDepartDate.getTimezoneOffset()) / intOneDay)
	if (objDateDiff < 0) {
		alert('Please enter a return date greater than the departure date');
		return false;
	}
	
	if ((landingType == 'normal')||(landingType == 'over65')||(landingType == 'wintersports'))if (!objForm.chkAnnual.checked){
		if (((objDateDiff+1) > 100)||((intAged76to80 > 0)&&((objDateDiff+1) > 31))){
			alert('Please note: Cover is available subject to a maximum of 100 days per trip, or 31 days for those aged 76 - 80. (See Key Facts for further details or select Long Stay / BackPacker)');
			return false;
		}
	}	
	if(intAged81to85 > 0){
		var threeMonthsFromToday = serverDate.getTime() + intOneDay * 93 ;
		if (objDepartDate.getTime() > threeMonthsFromToday){
			alert('Please note: For clients aged 81-85 any trip must commence within 93 days');
			return false;
		}
		if (objForm.chkAnnual.checked){
			alert('Please note: Our Annual Multi Trip policy is currently unavailable for clients aged 81-85. We do however allow single trips of up to 31 days');
			return false;
		}
		if ((objDateDiff+1) > 31){
			alert('Please note: The maximum trip duration is 31 days for clients aged 76-85');
			return false;
		}
		
	}

	
	
	
	//Check the party and number of people in each age range
	var objCouple = objForm.couple;
	var objSingleParentFamily = objForm.single_parent_family;
	var objFamily = objForm.family;
	
	if (objCouple.checked) {
		strParty = 'couple';
	} else if (objSingleParentFamily.checked) {
		strParty = 'single parent family';
	} else if (objFamily.checked) {
		strParty = 'family';
	}

	
	if (strParty == 'single') {
	
		if ((intAdults == 0) && (intChildren+intBabies == 0)) {
			alert('Please select the number of people under each age range that you wish to insure.')
			return false;
		}
		
		if ((intUnder2 > 0) && (intAged2to17 == 0) && (intAdults == 0)) {
			alert('Childen under 2 must travel with an accompanying adult.')
			return false;
		}
		
	}	
	
	if (isBackPacker) if (objForm.aged18to65.value<2) if (objForm.companion.checked){
			alert("For the companion discount to apply there must be two or more persons in your party");
			return false;
	
	}
	if (strParty == 'couple') {
		if ((intAdults != 2) || (intChildren != 0)) {
			alert('Please re-select the number of adults travelling – this should equal two.')
			return false;
		}
	}
	
	if (strParty == 'family') {
		if ((intAged18to65 != 2) || (intChildren == 0) ||((intAged66to70+intAged71to75+intAged76to80+intAged81to85) !=0)) {
			alert('Our Family discount covers 2 adults aged 18-65 plus any number of children. Where ALL children are under 2, you may select ‘Couple discount’ as children under 2 are insured free of charge.');
			return false;
		}
	}
	
	
	if (strParty == 'single parent family') {
		if (intAged18to65 < 1) {
			alert('You have selected \'single parent family\' which covers one adult and your dependant children under 18.')
			return false;
		}
		if ((intAged18to65 > 1) || ((intAged66to70 + intAged71to75 + intAged76to80 + intAged81to85)>0)) {
			alert('Only 1 adult aged 18-65 should be selected')
			return false;
		}
		if ((intChildren == 0)) {
			alert('Please select the number of children aged 2 - 17')
			return false;
		}
	}	
	
	if (landingType == 'longstay'){
		var destination = document.frmStep1.return_location.value;

		 if ((destination =='europe')&&(document.frmStep1.days.value > 366) &&((intAged66to70 + intAged71to75 + intAged76to80 + intAged81to85)>0)){
		 		alert('Sorry, you have exceeded the maximum duration for this age-group (See Key Facts for details)')
				return false;
		 
		 }
		 if ((destination =='world')&&((intAged66to70 + intAged71to75 + intAged76to80 + intAged81to85)>0)&&(document.frmStep1.days.value > 276)){
		 		alert('Sorry, you have exceeded the maximum duration for this age-group (See Key Facts for details)')
				return false;
		 
		 }
		 if ((destination =='world')&&((intAged71to75 + intAged76to80 + intAged81to85)>0)&&(document.frmStep1.days.value > 184)){
		 		alert('Sorry, you have exceeded the maximum duration for this age-group (See Key Facts for details)')
				return false;
		 
		 }
		 if ((destination =='usa')&&((intAged66to70 + intAged71to75 + intAged76to80 + intAged81to85)>0)&&(document.frmStep1.days.value > 184)){
		 		alert('Sorry, you have exceeded the maximum duration for this age-group (See Key Facts for details)')
				return false;
		 
		 }
		 if ((destination =='usa')&&((intAged71to75 + intAged76to80 + intAged81to85)>0)&&(document.frmStep1.days.value > 123)){
		 		alert('Sorry, you have exceeded the maximum duration for this age-group (See Key Facts for details)')
				return false;
		 
		 }
	}	
	
	

	if (document.getElementById('startDateMessage').style.display == '') {
			 	alert('Sorry, you are unable to book for a departure date greater than '+ limitDateText +'.');
				return false;
	
	}	
	if (document.getElementById('uktoukMessage').style.display == '') {
			 	alert('Please note, trips made within the UK are subject to a minimum of 3 nights pre-booked accommodation');
				return false;
	
	}
	if (okToSubmit == false) return;	
	objForm.submit();
	okToSubmit = false;
	

}

function chkOfflineData(){

	var objForm = document.frmStep1;
	var objOfflineFname = objForm.offlineFname;
	var objOfflineAddress1 = objForm.TxtContactAddress1;
	var objOfflineEmail = objForm.offlineEmail;
	
	var strOfflineFname = objOfflineFname.value;
	var strOfflineAddress1 = objOfflineAddress1.value;
	var strOfflineEmail = objOfflineEmail.value;
	
	//Check for customer information
	if((strOfflineFname == '') || (strOfflineAddress1 == '') || (strOfflineEmail == '')){
		alert('Please fill customer information.');
		objOfflineFname.focus();
		return false;
	}
	
	var trimmedOfflineEmail = strOfflineEmail.replace(/^\s+|\s+$/g, '') ;
	objOfflineEmail.value = trimmedOfflineEmail;
	var objRegExpEmail = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/;
	
	if (objRegExpEmail.test(trimmedOfflineEmail) == false) {
		alert('The email address you have supplied does not appear to be valid.');
		objOfflineEmail.focus();
		return false;
	}		
	
	checkData();
	
}

function extrasChanged(objField) {

	
	if (document.frmStep1.st_option_checkbox_ge.checked == true) {
		
		var golfPeopleCount = document.frmStep1.st_golfPeople.value;
		if (golfPeopleCount =='') {
			document.frmStep1.st_golfPeople.style.display='';
			document.getElementById('golfMessage').style.display='';
			document.frmStep1.st_golfPeople.focus();
			return;
		}
		else {
			document.getElementById('golfMessage').style.display='none';
		}
	}
	
	if (document.frmStep1.st_option_checkbox_si.checked == true) {

		
		var silLimit = document.frmStep1.st_singleItemLimit.value;
		if (silLimit =='') {
	
			document.getElementById('silMessage').style.display='';
			document.frmStep1.st_singleItemLimit.style.display='';
			document.frmStep1.st_singleItemLimit.focus();
			return;
		}
		else {
			document.getElementById('silMessage').style.display='none';
		}
		
		document.getElementById('st_options_tempRow_si_desc').style.display='';
		var silDesc = document.frmStep1.st_single_item_desc.value;
		if (silDesc =='') {
			document.getElementById('silDescMessage').style.display='';
			document.frmStep1.st_single_item_desc.focus();

			return;
		}
		else {
			document.getElementById('silDescMessage').style.display='none';
		}
	
	}

	
	//put at_ fields inside try catch, some quote pages does not have these fields
	try{
		if (document.frmStep1.at_option_checkbox_ww.checked == true) {
			
			document.frmStep1.return_location.selectedIndex = 5;
		
		}
		
		if (document.frmStep1.at_option_checkbox_ge.checked == true) {
		
			var golfPeopleCount = document.frmStep1.at_golfPeople.value;
			if (golfPeopleCount =='') {
				document.frmStep1.at_golfPeople.style.display='';
				document.getElementById('golfMessage').style.display='';
				document.frmStep1.at_golfPeople.focus();
				return;
			}
			else {
				document.getElementById('golfMessage').style.display='none';
			}
		}
	
		if (document.frmStep1.at_option_checkbox_si.checked == true) {

		
			var silLimit = document.frmStep1.at_singleItemLimit.value;
			if (silLimit =='') {
	
				document.getElementById('silMessage').style.display='';
				document.frmStep1.at_singleItemLimit.style.display='';
				document.frmStep1.at_singleItemLimit.focus();
				return;
			}
			else {
				document.getElementById('silMessage').style.display='none';
			}
		
			document.getElementById('at_options_tempRow_si_desc').style.display='';
			var silDesc = document.frmStep1.at_single_item_desc.value;
			if (silDesc =='') {
				document.getElementById('silDescMessage').style.display='';
				document.frmStep1.at_single_item_desc.focus();

				return;
			}
			else {
				document.getElementById('silDescMessage').style.display='none';
			}

		}
	}catch(e){
	
	}
	
	checkData();
	

}

function backpackerExtrasChanged(objField) {

	var baseStPrice = parseFloat(document.frmStep1.st_base_quote_value.value);
	var newPrice = baseStPrice;
	
	if (document.frmStep1.st_option_checkbox_ws.checked == true) {
		
		var winterSportsMonths = document.frmStep1.winterSportsMonths.value;
		if (winterSportsMonths  =='') {
			document.frmStep1.winterSportsMonths.style.display='';
			document.getElementById('winterSportsMessage').style.display='';
			document.frmStep1.winterSportsMonths.focus();
		}
		else {
			document.getElementById('winterSportsMessage').style.display='none';
			checkData();
			
		}

	
	}
	else  checkData();

}



function partyChange(objParty) {
	
	var objForm = document.frmStep1;
	

	if (objParty.checked) {
	
		if (objParty.name != 'couple') objForm.couple.checked = false;
		if (objParty.name != 'single_parent_family') objForm.single_parent_family.checked = false;
		if (objParty.name != 'family') objForm.family.checked = false;	
	
	}
	

}

function backpackerPartyChange(objParty) {
	
	var objForm = document.frmStep1;
	var peopleCount = objForm.aged18to65.value;

	

	if (objParty.checked) {
		if (peopleCount < 2) {
			alert("For the companion discount to apply there must be two or more persons in your party");
			objForm.companion.checked = false;
			return;

		}

	
	}
	

}


function buyPrice(strTrip,serverDate) {
	
	if ((strTrip == 'bc')||(strTrip == 'abc')) document.frmStep1.basic.value = 'Y';
	else document.frmStep1.basic.value = 'N';
		

	if (strTrip == 'st'||strTrip == 'bc') {//single trip OR single trip basic
		
		document.frmStep1.single_trip.value = 'Y';
		if (document.frmStep1.st_option_checkbox_si.checked){
			if (document.frmStep1.st_single_item_desc ==''){
				alert('Please enter single item description');
				document.frmStep1.st_single_item_desc.focus();
				return;
			}
		
		}
	} else {//annual trip 
		document.frmStep1.single_trip.value = 'N';
		if (document.frmStep1.at_option_checkbox_si.checked){
			if (document.frmStep1.at_single_item_desc ==''){
				alert('Please enter single item description');
				document.frmStep1.at_single_item_desc.focus();
				return;
			}
		
		}
		if (!allowAMTStartDateChange){// prompt only once
			
			ok = confirm('PLEASE NOTE, the maximum trip duration for persons over 65 is 35 days. If at the date of this application you are aged 65 or under then the maximum duration is 50 days, which can be increased to 100 days upon payment of an additional premium. It is possible to start your Annual Multi Trip policy up to 93 days in advance however you will not be covered in the event of cancellation* until this date. We have therefore defaulted your policy to begin tomorrow. Click OK to continue or Cancel to amend your start date.\n\n*where applicable');
			document.frmStep1.chkAnnual.checked = true;
			annualTripChange(serverDate);// initialize departure date to tomorow
			if (!ok){// user wants to change departure date
				allowAMTStartDateChange = true;// will stop promting again after user amends date and press "next"
				
				document.frmStep1.depart_date.focus();
				return;
	
			}
			
		}
					
		var strDepartDate = document.frmStep1.depart_date.value;
			
		if (strDepartDate == '') return false;
		
	
		var intOneDay = (1000 * 60 * 60 * 24);
		var objRegExpDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

		if (objRegExpDate.test(strDepartDate) == false) {
			alert('Please enter a valid departure date in the format "dd/mm/yyyy"');
			document.frmStep1.depart_date.focus();
			return false;
		}
	
	
		arrDepartDate = strDepartDate.split('/');
	
	
		objDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]);
		
		var threeMonthsFromToday = serverDate.getTime() + intOneDay * 93 ;
		
		if (objDepartDate.getTime()>threeMonthsFromToday ){
			alert('Departure date for Annual trip should be within 3 months, please correct departure date');
			document.frmStep1.depart_date.focus();
			return false;
		
		}
		if (objDepartDate.getTime() < serverDate.getTime()) {
			alert('Please enter a departure date in the future');
			return false;
		}
	

	}// end annual trip checks
	
	var oldAction = document.frmStep1.action;
	document.frmStep1.action = 'step2.php';
	document.frmStep1.submit();
	document.frmStep1.action = oldAction;

}




function daysChange(objDays) {

	if (objDays.value == '') return false;

	var objForm = document.frmStep1;
	var objReturnDate = objForm.return_date;
	var objAnnual = objForm.chkAnnual;
	var strDepartDate = objForm.depart_date.value;
	var intOneDay = (1000 * 60 * 60 * 24);
	
	if (objDays.value == '365') {
		objAnnual.checked = true;
		 // if it's a annual trip
    		var objToday = new Date(); // We get the day  TODO: cannot rely on client date setting use serverDate instead
    		var arrDepartDate = strDepartDate.split('/'); // we get a vector with the day, the month and the year
    		var objDepartDate2 = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]); // we create a date from the vector
    		var threeMonthsFromToday = objToday.getTime() + (intOneDay * 93) ; // we get the value for the limit date    
			//alert('Departure date for Annual trip should be within 3 months, please correct departure date');
			if (objDepartDate2.getTime()>threeMonthsFromToday ){ // we test if the DepartDate is biger than the limit
				alert('Departure date for Annual trip should be within 3 months, please correct departure date');
				dtmDepartDate=new Date(threeMonthsFromToday);// we create a new date wich is the limit
				strReturnDate = dtmDepartDate.getDate() + '/' + (dtmDepartDate.getMonth() + 1) + '/' + dtmDepartDate.getFullYear() + ''; // we create a string from this date
				document.frmStep1.depart_date.value = strReturnDate; // we set the value
				document.frmStep1.depart_date.focus(); // and give the focus to the field
				dtmReturnDate = new Date(Date.parse(dtmDepartDate) + (intOneDay * (objDays.value - 1)));	
				strReturnDate = dtmReturnDate.getDate() + '/' + (dtmReturnDate.getMonth() + 1) + '/' + dtmReturnDate.getFullYear() + '';
				document.frmStep1.return_date.value = strReturnDate;
				//document.frmStep1.return_date.disabled = true; TODO check this IE send filed as null, gives error in step3
				//alert('PLEASE NOTE, the maximum trip duration for persons over 65 is 35 days. If at the date of this application you are aged 65 or under then the maximum duration is 50 days, which can be increased to 100 days upon payment of an additional premium. It is possible to start your Annual Multi Trip policy up to 93 days in advance however you will not be covered in the event of cancellation until this date. ');
				return false;
			}
			else{
			//document.frmStep1.return_date.disabled = true; TODO check
			//alert('PLEASE NOTE, the maximum trip duration for persons over 65 is 35 days. If at the date of this application you are aged 65 or under then the maximum duration is 50 days, which can be increased to 100 days upon payment of an additional premium. It is possible to start your Annual Multi Trip policy up to 93 days in advance however you will not be covered in the event of cancellation until this date. ');
			}
	}
	else {
		objAnnual.checked = false;	
		document.frmStep1.return_date.disabled = false;
	}

	
	var intOneDay = (1000 * 60 * 60 * 24);
	var objRegExpDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	if (objRegExpDate.test(strDepartDate) == false) {
		return false;
	}
	
	arrDepartDate = strDepartDate.split('/');
	objDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]);
	dtmReturnDate = new Date(Date.parse(objDepartDate) + (intOneDay * (objDays.value - 1)));
	
	
	strReturnDate = dtmReturnDate.getDate() + '/' + (dtmReturnDate.getMonth() + 1) + '/' + dtmReturnDate.getFullYear() + '';
	objReturnDate.value = strReturnDate;
	uktoukCheck();
	wrongReturnDateCheck();

}

function backpackerDaysChange(objDays) {

	if (objDays.value == '') return false;

	var objForm = document.frmStep1;

	var objReturnDate = objForm.return_date;

	var strDepartDate = objForm.depart_date.value;
	


	
	var intOneDay = (1000 * 60 * 60 * 24);
	var objRegExpDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	if (objRegExpDate.test(strDepartDate) == false) {
		return false;
	}
	
	arrDepartDate = strDepartDate.split('/');
	objDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]);
	dtmReturnDate = new Date(Date.parse(objDepartDate) + (intOneDay * (objDays.value - 1)));
	
	
	strReturnDate = dtmReturnDate.getDate() + '/' + (dtmReturnDate.getMonth() + 1) + '/' + dtmReturnDate.getFullYear() + '';
	objReturnDate.value = strReturnDate;

}


function departDateChange() {
	var objForm = document.frmStep1;
	var objDays = objForm.days;
	var objReturnDate = objForm.return_date;
	var objDepartDate = objForm.depart_date;
	var strDepartDate = objDepartDate.value;
	if (strDepartDate == '') return false;
	var intOneDay = (1000 * 60 * 60 * 24);
	var objRegExpDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	if(objDays.value == '365')	{ // if it's a annual trip
    	var objToday = new Date(); // We get the day date TODO:cannot rely on client date setting, use serverDate instead
    	var arrDepartDate = strDepartDate.split('/'); // we get a vector with the day, the month and the year
    	var objDepartDate2 = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]); // we create a date from the vector
    	var threeMonthsFromToday = objToday.getTime() + (intOneDay * 93) ; // we get the value for the limit date    
		//alert('Departure date for Annual trip should be within 3 months, please correct departure date');
		if (objDepartDate2.getTime()>threeMonthsFromToday ){ // we test if the DepartDate is biger than the limit
			alert('Departure date for Annual trip should be within 3 months, please correct departure date');
			dtmDepartDate=new Date(threeMonthsFromToday);// we create a new date wich is the limit
			strReturnDate = dtmDepartDate.getDate() + '/' + (dtmDepartDate.getMonth() + 1) + '/' + dtmDepartDate.getFullYear() + ''; // we create a string from this date
			document.frmStep1.depart_date.value = strReturnDate; // we set the value
			document.frmStep1.depart_date.focus(); // and give the focus to the field
			dtmReturnDate = new Date(Date.parse(dtmDepartDate) + (intOneDay * 364));	
			strReturnDate = dtmReturnDate.getDate() + '/' + (dtmReturnDate.getMonth() + 1) + '/' + dtmReturnDate.getFullYear() + '';
			document.frmStep1.return_date.value = strReturnDate;
			return false;
		}
		
	}
	if (objRegExpDate.test(strDepartDate) == false) {	
		return false;
	}
	arrDepartDate = strDepartDate.split('/');
	dtmDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]);
	dtmReturnDate = new Date(Date.parse(dtmDepartDate) + (intOneDay * (objDays.value - 1)));
	if (dtmDepartDate.getTime()>=limitDate.getTime()){
		var ediv = document.getElementById('startDateMessage');
		ediv.removeChild(ediv.childNodes[0]);
		ediv.style.display='';
		ediv.innerHTML = '<table border="1"><tr><td>	Sorry, you are unable to book for a departure date greater than '+ limitDateText +'.</td></tr></table>';
		return false;
	
	}else document.getElementById('startDateMessage').style.display='none';

	if (objDays.options[objDays.selectedIndex].value == '')  return false;
	strReturnDate = dtmReturnDate.getDate() + '/' + (dtmReturnDate.getMonth() + 1) + '/' + dtmReturnDate.getFullYear() + '';
	objReturnDate.value = strReturnDate;
	uktoukCheck();
	wrongReturnDateCheck();
	longStaySetDate();

}

function backpackerReturnDateChange() {


	var objForm = document.frmStep1;
	var objDays = objForm.days;
	var objReturnDate = objForm.return_date;
	var objDepartDate = objForm.depart_date;
	var strReturnDate = objReturnDate.value;
	var strDepartDate = objDepartDate.value;
		
	
	if (strDepartDate == '') return false;
	if (strReturnDate == '') return false;
	
	
	var intOneDay = (1000 * 60 * 60 * 24);
	var objRegExpDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	if (objRegExpDate.test(strDepartDate) == false) {
		return false;
	}
	
	if (objRegExpDate.test(strReturnDate) == false) {
		return false;
	}	
	
	
	arrDepartDate = strDepartDate.split('/');
	arrReturnDate = strReturnDate.split('/');
	
	objDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]);
	objReturnDate = new Date(arrReturnDate[2], arrReturnDate[1] - 1, arrReturnDate[0]);

	
	var intDaysDifference = Math.round( (objReturnDate - objDepartDate) / intOneDay );
	intDaysDifference = intDaysDifference + 1;


	
	if (intDaysDifference > 366) {
		alert("Sorry, the maximum trip duration for this type of policy is 12 months, See Key Facts for details");
		return false;
	}
	objDays.selectedIndex = 0;
	if (intDaysDifference > 0 )   objDays.selectedIndex = 1;
	if (intDaysDifference > 31 )  objDays.selectedIndex = 2;
	if (intDaysDifference > 62 )  objDays.selectedIndex = 3;
	if (intDaysDifference > 93 )  objDays.selectedIndex = 4;
	if (intDaysDifference > 123 ) objDays.selectedIndex = 5;
	if (intDaysDifference > 153 ) objDays.selectedIndex = 6;
	if (intDaysDifference > 184 ) objDays.selectedIndex = 7;
	if (intDaysDifference > 215 ) objDays.selectedIndex = 8;
	if (intDaysDifference > 245 ) objDays.selectedIndex = 9;
	if (intDaysDifference > 276 ) objDays.selectedIndex = 10;
	if (intDaysDifference > 306 ) objDays.selectedIndex = 11;
	if (intDaysDifference > 337 ) objDays.selectedIndex = 12;
		

}

function returnDateChange() {


	var objForm = document.frmStep1;
	var landingType = objForm.landingType.value;
	if (landingType == 'backpacker'){
		backpackerReturnDateChange();
		return;
	}
	 
	var objDays = objForm.days;
	var objReturnDate = objForm.return_date;
	var objDepartDate = objForm.depart_date;
	var strReturnDate = objReturnDate.value;
	var strDepartDate = objDepartDate.value;
		
	
	if (strDepartDate == '') return false;
	if (strReturnDate == '') return false;
	
	
	var intOneDay = (1000 * 60 * 60 * 24);
	var objRegExpDate = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	if (objRegExpDate.test(strDepartDate) == false) {
		return false;
	}
	
	if (objRegExpDate.test(strReturnDate) == false) {
		return false;
	}	
	
	
	arrDepartDate = strDepartDate.split('/');
	arrReturnDate = strReturnDate.split('/');
	
	objDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]);
	objReturnDate = new Date(arrReturnDate[2], arrReturnDate[1] - 1, arrReturnDate[0]);

	
	var intDaysDifference = Math.round( (objReturnDate - objDepartDate) / intOneDay );
	intDaysDifference = intDaysDifference + 1;
	
	var landing = objForm.landingType.value;
	if ((landing!='longstay')&&(landing!='backpacker')) if (intDaysDifference > 0 && intDaysDifference <= 100) {
		objDays.selectedIndex = intDaysDifference;
	}
	uktoukCheck();
	wrongReturnDateCheck();
	longStaySetDate();
}



function annualTripChange(serverDate) {
// this function is called every time the AMT check box is set/unset
// if AMT, then set start date and finish date one year apart
	var intOneDay = (1000 * 60 * 60 * 24);
	tomorow = new Date(serverDate.getTime()+intOneDay );
	yearAfterTomorrow = new Date(serverDate.getTime()+365*intOneDay);

	var objForm = document.frmStep1;
	var objAnnual = objForm.chkAnnual;
	var objDays = objForm.days;
	
	if (objAnnual.checked == true) {
		objDays.selectedIndex = objDays.length - 1;// set number of days select drop down to 365 
		daysChange(objDays);
	} else {
		objDays.selectedIndex = 0;
		document.frmStep1.return_date.disabled = false;
	}
	

	var objReturnDate = objForm.return_date;
	var objDepartDate = objForm.depart_date;

		
	strDepartDate = tomorow.getDate() + '/' + (tomorow.getMonth() + 1) + '/' + tomorow.getFullYear() + '';

	strReturnDate = yearAfterTomorrow.getDate() + '/' + (yearAfterTomorrow.getMonth() + 1) + '/' + yearAfterTomorrow.getFullYear() + '';
	
	objDepartDate.value = strDepartDate;
	objReturnDate.value = strReturnDate;
	
	uktoukCheck();
	wrongReturnDateCheck();

}

function overEightyChange() {

var objForm = document.frmStep1;
var objAged81to85 	= objForm.aged81to85;
var intAged81to85	= parseInt(objAged81to85.value);

    if (intAged81to85>0) {
	 alert('80+ prices are currently unavailable until 1st October due to changing underwriters. We are sorry for any inconvenience caused. Please re-visit the site from 1st October 2009 onwards, alternatively for more information please call Customer Services - 0845 508 9886.');
	 objAged81to85.value=0;
	 intAged81to85=0;
	 }
}

