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 countCountries(){
	
	var countryCount = 1;
	while (document.getElementById('country_list'+countryCount)!=null){

			countryCount++;		
	}			
	return (countryCount-1);

}

function dissableBuyButton(name){
	document.getElementById(name).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 > 519 ) 	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='';
			/*objReturnDate.value = strDepartDate.value;
			returnDateChange();*/
		}
		else  	document.getElementById('wrongReturnMessage').style.display='none';
	}
}


function checkData( submit ) {
	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 = document.getElementById("return_location"); //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;
	//axa
	var objAged18to30 	= objForm.aged18to30;
	var objAged31to55 	= objForm.aged31to55;
	var objAged56to65 	= objForm.aged56to65;
	
	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 	=  0;
	var intAged18to30 	=  0;
	var intAged31to55 	=  0;
	var intAged56to65 	=  0;
	if( objAged18to65 )
		intAged18to65 = parseInt(objAged18to65.value);
	else{
		//axa
		if( objAged18to30 )
			intAged18to30 = parseInt(objAged18to30.value);
		
		if( objAged31to55 )
			intAged31to55 = parseInt(objAged31to55.value);
		
		if( objAged56to65 )
			intAged56to65 = parseInt(objAged56to65.value);
		intAged18to65 = intAged18to30 + intAged31to55 + intAged56to65;
	}
	var intAged66to70 	= parseInt(objAged66to70.value);
	var intAged71to75 	= parseInt(objAged71to75.value);
	var intAged76to80	= parseInt(objAged76to80.value);
	var intAged81to85	= parseInt(objAged81to85.value);
	
	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(); IE FIX
		return false;
	}	*/

	//var amtSelected = objForm.chkAnnual.checked;
	var tripTypeAnnual = document.getElementById('tripTypeAnnual').checked;
	
	
	if( !tripTypeAnnual /*!amtSelected*/ ){
	//check country select
				countryCount = countCountries();
				
			
				for (c=0; c<countryCount; c++){
					id = (c+1);
					selection = document.getElementById("country_list"+id).value;
						if (selection == '' || selection == null) {
							alert('Please select country');
							document.getElementById("country_list"+id).focus();
							return false;
						}	

				}
	} else {
		if( document.getElementById("amt_region_list").value == "" ){
			alert('Please select where you are traveling to see option 2');
			document.getElementById("amt_region_list").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() * 60 * 1000 - objDepartDate.getTime() + objDepartDate.getTimezoneOffset() * 60 * 1000) / intOneDay)
	if (objDateDiff < 0) {
		alert('Please enter a return date greater than the departure date');
		return false;
	}
	
	if( 'INSME' == 'STS01' || 'INSME' == 'EXPAT' || 'INSME' == 'INSME' ) {
		// if there's more than 1 person aged 71-75, and destination is area 4 or > (world or usa),  
		/*if( document.getElementById("days").value && parseInt( document.getElementById("days").value ) && parseInt( document.getElementById("days").value ) > 123 &&  
			parseInt(document.getElementById("aged71to75").value) && parseInt(document.getElementById("aged71to75").value) > 0	) {
			
			var maxWeightCountry = getMaxWeightCountry(); 
			if( maxWeightCountry == 'world' || maxWeightCountry == 'usa' ) {
				alert("Please note: maximum travel duration is 4 months for clients aged 71-75 travelling to these destinations.");
				return false;
			}
			
		}*/
		var maxweight = getMaxWeightCountry();
		var days = objDateDiff + 1;
		
		if( jGet('aged66to70').value > 0 ) {
			if( jGet('tripTypeSingle').checked || jGet('tripTypeWinter').checked ) {
				//no limitation
			} else if( jGet('tripTypeAnnual').checked ) {
				//no limitation
			} else if( jGet('tripTypeLongstay').checked ) {
				if( maxweight == 'europe' && days > 366 ) {
					alert("Please note: maximum travel duration is 12 months for travellers over 65 travelling to Europe.");
					return false;
				} 
				if( maxweight == 'world'  && days > 276 ) {
					alert("Please note: maximum travel duration is 9 months for travellers over 65 travelling Worldwide.");
					return false;
				}
				if( maxweight == 'usa'    && days > 184 ) {
					alert("Please note: maximum travel duration is 6 months for travellers over 65 travelling Worldwide/USA.");
					return false;
				}
			}
		}
		if( jGet('aged71to75').value > 0 ) {
			if( jGet('tripTypeSingle').checked || jGet('tripTypeWinter').checked ) {
				//no limit
			} else if( jGet('tripTypeAnnual').checked ) {
				//no limit
			} else if( jGet('tripTypeLongstay').checked ) {
				if( maxweight == 'europe' && days > 366 ) {
					alert("Please note: maximum travel duration is 12 months for travellers over 70 travelling to Europe.");
					return false;
				} 
				if( maxweight == 'world'  && days > 184 ) {
					alert("Please note: maximum travel duration is 6 months for travellers over 70 travelling Worldwide.");
					return false;
				}
				if( maxweight == 'usa'    && days > 123 ) {
					alert("Please note: maximum travel duration is 4 months for travellers over 70 travelling Worldwide/USA.");
					return false;
				}
			}
		}
		if( jGet('aged76to80').value > 0 ) {
			if( jGet('tripTypeSingle').checked || jGet('tripTypeWinter').checked ) {
				if( days > 31 ) {
					alert("Please note: maximum travel duration is 31 days for travellers over 75.");
					return false;
				}
			} else if( jGet('tripTypeAnnual').checked ) {
				//no limit //35 days max travel
			} else if( jGet('tripTypeLongstay').checked ) {
				alert( "Please note: Long Stay is currently not available for travellers over 75.");
				return false;
			}
		}
		if( jGet('aged81to85').value > 0 ) {
			if( jGet('tripTypeSingle').checked || jGet('tripTypeWinter').checked ) {	
				if( days > 31 ) {
					alert("Please note: maximum travel duration is 31 days for travellers over 80.");
					return false;
				}
			} else if( jGet('tripTypeAnnual').checked ) {
				alert( "Please note: AMT is currently not available for travellers over 80.");
				return false;
			} else if( jGet('tripTypeLongstay').checked ) {
				alert( "Please note: Long Stay is currently not available for travellers over 80.");
				return false;
			}
		}
	}
	if( ! 'INSME' == 'TIMUK' ) {
		
		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 = new Date().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 ( tripTypeAnnual /*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 || document.getElementById("partyTypeCouple");
	var objSingleParentFamily = objForm.single_parent_family || document.getElementById("partyTypeSpf");
	var objFamily = objForm.family || document.getElementById("partyTypeFamily"); 
	
	if (objCouple.checked) {
		strParty = 'couple';
	} else if (objSingleParentFamily.checked) {
		strParty = 'single parent family';
	} else if (objFamily.checked) {
		strParty = 'family';
	}
	
	var isNewDesign = parseFloat('1.1') && parseFloat('1.1') >= 1.1  ? true : false;
	if( isNewDesign ) {
		if( document.getElementById("partyTypeIndividual").checked && intAdults + intChildren + intBabies != 1 ){
			alert("Individual is selected under Point 3, but there are " + (intAdults + intChildren + intBabies) + " travellers selected under Point4.\n" +
					"Please select the correct type of travellers, or select exactly one traveller.");
			return false;
		}
		if( document.getElementById("partyTypeGroup").checked && intAdults + intChildren + intBabies <= 1 ){
			alert("Group is selected under Point 3, but there is " + (intAdults + intChildren + intBabies) + " travellers selected under Point4.\n" +
					"Please select the correct type of travellers, or select more than one traveller.");
			return false;
		}
	}
	

	
	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 one adult aged 18-65 should be selected')
			return false;
		}
		if( intChildren == 0 && intBabies == 0 ){
			alert('Please select the number of children');
			return false;
		}
	}	
	
	if( ! 'INSME' == 'TIMUK' ) {
		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( 'INSME' == 'STS01' || 'INSME' == 'EXPAT' || 'INSME' == 'INSME' ) {
		var toUk = false;
		for(var sel = 1; jGet('country_list' + sel); sel++ ){
			if( jGet('country_list' + sel).value == 'UNITED KINGDOM' )
				toUk = true;
		} 
		
		if( jGet('depart_location').value == 'uk1' && toUk && objDateDiff < 3   ) {
		 	alert('Please note, trips made within the UK are subject to a minimum of 3 nights pre-booked accommodation');
			return false;
		}
	}
	
	
	//AXA age limits check
	if( 'INSME' == 'TIMUK' ) {
		if( document.getElementById('tripTypeAnnual').checked ) {
			//AMT:
			//if( intAged76to80 && document.getElementById("amt_region_list").value == "WORLD_HR"	) {
			//	alert( "Sorry, Worldwide annual cover is not available for travellers over 75.")
			//	return false;
			//}
		} else {
			var days = parseInt( document.getElementById( "days" ).value );
			//SINGLE:
			var maxWeightCountry = getMaxWeightCountry();
			if( maxWeightCountry == 'AXA_AREA2' || maxWeightCountry == 'EUROPE_LR') {
				//80-85: max 17 days
				if( intAged81to85 > 0 && days > 90 ) {
					alert( "Sorry, these destinations are only available for max. 90 days for travellers over 80.");
					return false;
				}
			} else if ( maxWeightCountry == 'AXA_AREA3' || maxWeightCountry == 'EUROPE_HR' ) {
				//80-85: max 17 days
				if( intAged81to85 > 0 && days > 90 ) {
					alert( "Sorry, these destinations are only available for max. 90 days for travellers over 80.");
					return false;
				}
				//75-79: max 31 days
				if( intAged76to80 > 0 && days > 90 ) {
					alert( "Sorry, these destinations are only available for max. 90 days for travellers over 75.");
					return false;
				}
		
			} else if ( maxWeightCountry == 'AXA_AREA4' || maxWeightCountry == 'WORLD_LR') {

				//over 75 max 90 days
				if (days > 90 ){
					if( intAged76to80 > 0 || intAged81to85 > 0 ){
						alert( "Sorry, these destinations are only available for max. 90 days for travellers over 75.");
						return false;
					}
				}

		
			} else if ( maxWeightCountry == 'AXA_AREA5' || maxWeightCountry == 'WORLD_HR') {
		
				//over 70: max 90 days
				if (days > 90){
					if( intAged71to75 > 0 || intAged76to80 > 0 || intAged81to85 > 0) {
						alert( "Sorry, these destinations are only available for max. 90 days for travellers over 70.");
						return false;
					}
				}

			}
		}
		
	}
	if( isNewDesign ) {
		if( document.getElementById("webQuoteForm").style.display == 'none' ) {
			//call center fields check
			var ot = document.getElementById("organiserTitle");
			if( !ot.value ) {
				alert("Please provide a valid title!"); 
				ot.focus(); 
				return false; 
			}
			
			var ofn = document.getElementById("organiserFirstName");
			if( !ofn.value ) {
				alert("Please provide a valid first name!"); ofn.focus(); return false; }
			
			var oln = document.getElementById("organiserLastName");
			if( !oln.value ) {
				alert("Please provide a valid last name!"); oln.focus(); return false; }
			
			var reDob = /^(\d){1,2}\/(\d){1,2}\/(\d{4})$/
			var odob = document.getElementById("organiserDateOfBirth");
			if( !odob.value || !reDob.test( odob.value ) ) {
				alert("Please provide a valid date of birth!\n Use the format mm/dd/yyyy, e.g.: 21/02/1953."); odob.focus(); return false; }
			var before18years = new Date( new Date().getFullYear() - 18, new Date().getMonth(), new Date().getDate() );
			if( !(
					new Date( parseInt(odob.value.split('/')[2]), parseInt(odob.value.split('/')[1]) - 1, parseInt(odob.value.split('/')[0]) ).getTime() 
					< before18years.getTime()
			) ) {
				alert("The travel organiser should be at least 18 years old!"); return false;
			}
			
			var oe = document.getElementById("organiserEmail");
			var objRegExpEmail = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/;
			if( oe.value && objRegExpEmail.test(oe.value) == false ) {
				alert("The supplied email address does not seem to be valid. Please correct it."); return false;
			}
			var oce = document.getElementById("organiserConfirmEmail");
			if( oe.value != oce.value ) {
				alert("The email and the email confirmation fields are not matching. Please correct it.");return false;
			}
			
			var opc = document.getElementById('organiserPostcode');
			if( opc.value == '' || opc.value.length < 5) {
				alert("Please provide a valid postcode and press the 'Find UK Address' button"); opc.focus(); return false; }
			
			var ohp = document.getElementById("organiserHomePhone");
			if( !ohp.value ) {
				alert("Please provide a valid home phone!"); ohp.focus(); return false; }
			
			if( document.getElementById("organiserAddressFields").style.display == 'none' ) {
				alert("Please press the 'Find UK Address' button, and select valid address details");
				return false;
			} else {
				var oa1 = document.getElementById("organiserAddress1");
				if( !oa1.value ) {
					alert("Please provide a valid address!"); oa1.focus(); return false; }
				
				var oc = document.getElementById("organiserCity");
				if( !oc.value ) {
					alert("Please provide a valid town / city!"); oc.focus(); return false; }
				
				var ocn = document.getElementById("organiserCountry");
				if( !ocn.value ) {
					alert("Please provide a valid country!"); ocn.focus(); return false; }
			}
		} else {
			//web fields check
			var owe = document.getElementById("organiserEmailWeb");
			var objRegExpEmail = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/;
			if( owe.value && objRegExpEmail.test(owe.value) == false ) {
				alert("Please supply a valid email address."); 
				owe.focus();
				return false;
			}
			
		}
	}
	
	
		
	if('INSME' == 'EXPAT') {
		if( submit )
			objForm.submit();
	}else{
		jQuery('#MessageWrapper').show();return false;
	}
	

}

function chkOfflineData( submit){

	var objForm = document.frmStep1;
	var objOfflineFname = objForm.offlineFname;
	var objOfflineAddress1 = objForm.TxtContactAddress1;
	var objOfflineEmail = objForm.offlineEmail;
	var objOfflineTelephone = objForm.offlineTelephone;
	
	var strOfflineFname = objOfflineFname.value;
	var strOfflineAddress1 = objOfflineAddress1.value;
	var strOfflineEmail = objOfflineEmail.value;
	var strOfflineTelephone = objOfflineTelephone.value;
	
	//Check for customer information
	if((strOfflineFname == '') || (strOfflineAddress1 == '') || (strOfflineEmail == '') || (strOfflineTelephone == '')){
		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( submit );
	
}

function extrasChanged(objField) {

	
	if (document.frmExtras.st_option_checkbox_ge.checked == true) {
		
		var golfPeopleCount = document.frmExtras.st_golfPeople.value;
		if (golfPeopleCount =='') {
			document.frmExtras.st_golfPeople.style.display='';
			document.getElementById('golfMessage').style.display='';
			document.frmExtras.st_golfPeople.focus();
			return;
		}
		else {
			document.getElementById('golfMessage').style.display='none';
		}
	}
	
	if (document.frmExtras.st_option_checkbox_si.checked == true) {

		
		var silLimit = document.frmExtras.st_singleItemLimit.value;
		if (silLimit =='') {
	
			document.getElementById('silMessage').style.display='';
			document.frmExtras.st_singleItemLimit.style.display='';
			document.frmExtras.st_singleItemLimit.focus();
			return;
		}
		else {
			document.getElementById('silMessage').style.display='none';
		}
		
		document.getElementById('st_options_tempRow_si_desc').style.display='';
		var silDesc = document.frmExtras.st_single_item_desc.value;
		if (silDesc =='') {
			document.getElementById('silDescMessage').style.display='';
			document.frmExtras.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.frmExtras.at_option_checkbox_ww && document.frmExtras.at_option_checkbox_ww.checked == true) {
			
			document.frmStep1.return_location.selectedIndex = 5;
		
		}
		
		if (document.frmExtras.at_option_checkbox_ge.checked == true) {
		
			var golfPeopleCount = document.frmExtras.at_golfPeople.value;
			if (golfPeopleCount =='') {
				document.frmExtras.at_golfPeople.style.display='';
				document.getElementById('golfMessage').style.display='';
				document.frmExtras.at_golfPeople.focus();
				return;
			}
			else {
				document.getElementById('golfMessage').style.display='none';
			}
		}
	
		if (document.frmExtras.at_option_checkbox_si.checked == true) {

		
			var silLimit = document.frmExtras.at_singleItemLimit.value;
			if (silLimit =='') {
	
				document.getElementById('silMessage').style.display='';
				document.frmExtras.at_singleItemLimit.style.display='';
				document.frmExtras.at_singleItemLimit.focus();
				return;
			}
			else {
				document.getElementById('silMessage').style.display='none';
			}
		
			document.getElementById('at_options_tempRow_si_desc').style.display='';
			var silDesc = document.frmExtras.at_single_item_desc.value;
			if (silDesc =='') {
				document.getElementById('silDescMessage').style.display='';
				document.frmExtras.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.frmExtras.st_option_checkbox_si.checked){
			if (document.frmExtras.st_single_item_desc ==''){
				alert('Please enter single item description');
				document.frmExtras.st_single_item_desc.focus();
				return;
			}
		
		}
	} else {//annual trip 
		document.frmStep1.single_trip.value = 'N';
		if (document.frmExtras.at_option_checkbox_si.checked){
			if (document.frmExtras.at_single_item_desc ==''){
				alert('Please enter single item description');
				document.frmExtras.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
	return true;

}
*/



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);
	var intOneHour = (1000 * 60 * 60 + 1);
	
	if (objDays.value == '365') {
		if( objAnnual )
			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;
			}*/
    		//var oneMonthFromToday = objToday.getTime() + (intOneDay * 31) ;
    		var threeMonthsFromTodayforTIM = objToday.getTime() + (intOneDay * 90) ; // TIM needs 90 days from AMT instead of 93
    		if( objDepartDate2.getTime() > threeMonthsFromTodayforTIM && 'INSME'=='TIMUK' ){
    			//AXA: 1 MONTH
    			alert('Departure date for Annual trip should be within 90 days, please correct departure date');
				dtmDepartDate=new Date(threeMonthsFromTodayforTIM);// 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;
    		} else 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 {
		if( objAnnual )
			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)) + intOneHour);
	
	
	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 intOneHour = (1000 * 60 * 60 + 1); //dst fix 
	
	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;
		}*/
    	//var oneMonthFromToday = objToday.getTime() + (intOneDay * 31) ;
    	var threeMonthsFromTodayforTIM = objToday.getTime() + (intOneDay * 90) ;
		if( objDepartDate2.getTime() > threeMonthsFromTodayforTIM && 'INSME'=='TIMUK' ){
			//AXA: 1 MONTH
			alert('Departure date for Annual trip should be within 90 days, please correct departure date');
			dtmDepartDate=new Date(threeMonthsFromTodayforTIM);// 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;
			return false;
		} else 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;
		}
		
	}
	if (objRegExpDate.test(strDepartDate) == false) {	
		return false;
	}
	arrDepartDate = strDepartDate.split('/');
	dtmDepartDate = new Date(arrDepartDate[2], arrDepartDate[1] - 1, arrDepartDate[0]);
	var duration = objDays.value;
	if( duration == 365 ) {
		var startDateFrags = objDepartDate.value.split('/');
		var endDateFrags = objReturnDate.value.split('/');
		var startDate = new Date( 
				parseInt(startDateFrags[2],10), 
				parseInt(startDateFrags[1],10) - 1,
				parseInt(startDateFrags[0],10));
		var endDate = new Date(
				parseInt(endDateFrags[2],10),
				parseInt(endDateFrags[1],10) - 1,
				parseInt(endDateFrags[0],10));
		//if start or end date is not selected yet:
		if( startDate == "Invalid Date")
			startDate = new Date( endDate.getTime() - 365 * 24 * 60 * 60 * 1000);
		if( endDate == "Invalid Date")
			endDate = new Date( startDate.getTime() + 365 * 24 * 60 * 60 * 1000 );
		//if start year is leap year and start date < leap day and end date > leap day, 
		//OR end year is leap year and start date < leap day and end date > leap day
		if( ( new Date(startDate.getFullYear(),1,29).getDate() == 29 
				&& startDate.getTime() < new Date(startDate.getFullYear(),1,29).getTime() 
				&& endDate.getTime() > new Date(startDate.getFullYear(),1,29).getTime() )
				|| ( new Date(endDate.getFullYear(),1,29).getDate() == 29 
				&& startDate.getTime() < new Date(endDate.getFullYear(),1,29).getTime()
				&& endDate.getTime() > new Date(endDate.getFullYear(),1,29).getTime() )) {
			duration = 366;
		}
	}
				
	dtmReturnDate = new Date(Date.parse(dtmDepartDate) + (intOneDay * (duration - 1)) + intOneHour );
	
	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 objForm = document.getElementById("frmStep1");
	var landingType = jGet("landingType").value;
	if (landingType == 'backpacker'){
		backpackerReturnDateChange();
		return;
	}
	 
	var objDays = jGet("days");
	var objReturnDate = jGet("return_date");
	var objDepartDate = jGet("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 intOneHour = (1000 * 60 * 60 + 1);
	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], 0,0,0);
	objReturnDate = new Date(arrReturnDate[2], arrReturnDate[1] - 1, arrReturnDate[0],0,0,0);
	
	var intDaysDifference = Math.round( (objReturnDate - objDepartDate + intOneHour) / intOneDay );
	intDaysDifference = intDaysDifference + 1;
	if( ( jGet('tripTypeSingle').checked || jGet('tripTypeWinter').checked ) && intDaysDifference > 100 ) {
		var hundredDaysFromDepart = new Date(arrDepartDate[2], arrDepartDate[1] - 1, parseInt(arrDepartDate[0]) + 99, 0,0,0);
		var day = "0" + hundredDaysFromDepart.getDate(); 
		day = day.substr(day.length - 2);  
		var month = "0" + (hundredDaysFromDepart.getMonth() + 1);
		month = month.substr( month.length - 2);
		jGet("return_date").value = 
			day + "/" + 
			month + "/" + 
			hundredDaysFromDepart.getFullYear(); 
		intDaysDifference = 100;
	}
	
	var landing = objForm.landingType.value;
	//if ((landing!='longstay')&&(landing!='backpacker')) if (intDaysDifference > 0 && intDaysDifference <= 100) {
	if( jQuery('#tripTypeSingle').is(':checked') && intDaysDifference > 0 && intDaysDifference <= 100 ) {
		objDays.value = intDaysDifference;
	} else if ( jQuery('#tripTypeAnnual').is(':checked') ) {	
		var duration = 365;
		var startDateFrags = objDepartDate.value.split('/');
		var endDateFrags = objReturnDate.value.split('/');
		var startDate = new Date( 
				parseInt(startDateFrags[2],10), 
				parseInt(startDateFrags[1],10) - 1,
				parseInt(startDateFrags[0],10));
		var endDate = new Date(
				parseInt(endDateFrags[2],10),
				parseInt(endDateFrags[1],10) - 1,
				parseInt(endDateFrags[0],10));
		//if start or end date is not selected yet:
		if( startDate == "Invalid Date")
			startDate = new Date( endDate.getTime() - 365 * 24 * 60 * 60 * 1000);
		if( endDate == "Invalid Date")
			endDate = new Date( startDate.getTime() + 365 * 24 * 60 * 60 * 1000 );
		
		//if start year is leap year and start date < leap day and end date > leap day, 
		//OR end year is leap year and start date < leap day and end date > leap day
		if( ( new Date(startDate.getFullYear(),1,29).getDate() == 29 
				&& startDate.getTime() < new Date(startDate.getFullYear(),1,29).getTime() 
				&& endDate.getTime() > new Date(startDate.getFullYear(),1,29).getTime() )
				|| ( new Date(endDate.getFullYear(),1,29).getDate() == 29 
				&& startDate.getTime() < new Date(endDate.getFullYear(),1,29).getTime()
				&& endDate.getTime() > new Date(endDate.getFullYear(),1,29).getTime() )) {
			duration = 366;
		}
		
		startDate = new Date(endDate.getTime() - duration * 24 * 60 * 60 * 1000 );
		objDepartDate.value = padWithZero(startDate.getDate()) + "/" + padWithZero(startDate.getMonth() + 1) + "/" + starDate.getFullYear();
		
	}
	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;
	}*/
	
	if( jGet("tripTypeAnnual").checked ) {
		objDays.selectedIndex = objDays.length - 1;// set number of days select drop down to 365 
		daysChange(objDays);
	} else {
		objDays.selectedIndex = 0;
	}
	

	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 addCountrySelect(){
	lastCountryNum = countCountries();
	nextCountryNum = lastCountryNum  + 1;
	country1 = document.getElementById('country_list1');
	clone = country1.cloneNode(true);
	clone.id = 'country_list'+nextCountryNum;
	clone.name = 'country_list'+nextCountryNum;
	clone.onclick = function(){setDestination();};

	containerDiv = document.getElementById('containerDiv');
	containerDiv.appendChild(document.createElement('br'));
	containerDiv.appendChild(clone);
}


function setDestination(){
	isBackpacker = false;
	if (document.getElementById('landingType').value == 'backpacker') isBackpacker = true;
	countryCount = countCountries();
	previouslySetValue = null;
	
	
	var objForm = document.frmStep1;
	var amtselected = document.getElementById('tripTypeAnnual').checked;//objForm.chkAnnual.checked;
	//if amt not checked, do this:
		if( !amtselected ){
				for (c=0; c<countryCount; c++){
						id = (c+1);
						index = document.getElementById("country_list"+id).selectedIndex;
						temp  = document.getElementById("country_list"+id).options[index].getAttribute('id');
						
						if ((!isBackpacker) && (temp =='ausnz' )) temp = 'world';
						if ((isBackpacker) && (temp =='usa' )) temp = 'world';
						
						if (previouslySetValue == null) previouslySetValue = temp;
						
					
						if ((temp == 'europe'||temp == 'world'||temp == 'usa'||temp == 'ausnz') && (previouslySetValue =='uk')){
							previouslySetValue  = temp;
						}
						if ((temp == 'world'||temp == 'usa'||temp == 'ausnz') && (previouslySetValue =='europe')){
							previouslySetValue  = temp;
						}
						if ((temp == 'usa'||temp == 'world') && (previouslySetValue =='ausnz')){
							previouslySetValue  = temp;
						}
						if ((temp == 'usa') && (previouslySetValue =='world')){
							previouslySetValue  = temp;
						}
				}
		}else{
			amtsel = document.getElementById("amt_region_list").value;
			if( amtsel == 'europe1' || amtsel == 'europe2' ) {
				previouslySetValue = 'europe';
			}else if ( amtsel == 'world') {
				previouslySetValue = 'world';
			}else if ( amtsel == 'ausnz') {
				previouslySetValue = 'ausnz';
			}else {
				previouslySetValue = 'usa';
			}
			
		}

		document.getElementById("return_location").selectedIndex = 0;
		var numOfOptions = document.getElementById("return_location").options.length;
		for (i=0;i<numOfOptions;i++){

			if (document.getElementById("return_location").options[i].value == previouslySetValue ){
						document.getElementById("return_location").selectedIndex = i;
			}
	
		}

	



	uktoukCheck();
}

function removeCountrySelect(){
	lastCountryNum = countCountries();
	if (lastCountryNum == 1) return;
	lastCountry = document.getElementById('country_list'+lastCountryNum);
	p = lastCountry.parentNode;
	p.removeChild(lastCountry);
	p.removeChild( p.lastChild ); //remove <br />
	setDestination();
}




