function showCalendar3(calTitle,calTarget,calInit,bDoDuration) {
	//alert(calTitle + ' ' + calTarget + ' ' + calInit + ' ' + bDoDuration);
	//alert(calTitle + ' ' + document.calTarget.value + ' ' + document.calInit.value + ' ' + bDoDuration);
	if(bDoDuration==null) { bDoDuration = true; }
	if(calInit!=null) {
		calendarWin=window.open("/includes/calendar.asp?initDate=" + eval("document." + calInit + "_d.value") + "/" + eval("document." + calInit + "_m.value") + "/" + eval("document." + calInit + "_y.value") + "&title=" + escape(calTitle) + "&field=" + escape(calTarget)+"&doduration="+bDoDuration,"calendarWin","width=204,height=202,left=" + ((screen.width-210)/2) + ",top=" + ((screen.height-240)/2) + ",status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no");
	} else { 
    	calendarWin=window.open("/includes/calendar.asp?initDate=" + eval("document." + calTarget + "_d.value") + "/" + eval("document." + calTarget + "_m.value") + "/" + eval("document." + calTarget + "_y.value") + "&title=" + escape(calTitle) + "&field=" + escape(calTarget)+"&doduration="+bDoDuration,"calendarWin","width=204,height=202,left=" + ((screen.width-210)/2) + ",top=" + ((screen.height-240)/2) + ",status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no");
	}
	if(!calendarWin.opener) calendarWin.opener=self;
	if (window.focus){
		calendarWin.focus()
	}
}

function updateCalendarsFlights(fId,bDoDuration)
{
	//Local variables
	yFr=fId.startdate_y; mFr=fId.startdate_m; dFr=fId.startdate_d;
	yTo=fId.enddate_y; mTo=fId.enddate_m; dTo=fId.enddate_d;
	//Check depart date
	maxDaysInFrMonth=maxMonthDays[mFr.selectedIndex + (yFr.selectedIndex * 12)];
	if(dFr.selectedIndex >= maxDaysInFrMonth) { dFr.selectedIndex = maxDaysInFrMonth - 1 }
	if(bDoDuration==true)
	{
		//Alter durations
		if(dFr.selectedIndex + 8 > maxDaysInFrMonth) { dToNew = (dFr.selectedIndex + 7) - maxDaysInFrMonth; mToNew = mFr.selectedIndex + 1; }
		else { dToNew = dFr.selectedIndex + 7; mToNew = mFr.selectedIndex; }
		if(dFr.selectedIndex > dToNew && mFr.selectedIndex == 11 && yFr.selectedIndex == 1) { dToNew = 30; mToNew = 11; yToNew = 1; dFr.selectedIndex = 23; mFr.selectedIndex = 11; yFr.selectedIndex = 1; }
		else if(mFr.selectedIndex == 11 && dFr.selectedIndex > dToNew) { mToNew = 0; yToNew = 1; }
		else { yToNew = yFr.selectedIndex; }
		dTo.selectedIndex = dToNew; mTo.selectedIndex = mToNew; yTo.selectedIndex = yToNew;
	}
	else
	{
		//Check return date
		maxDaysInToMonth=maxMonthDays[mTo.selectedIndex + (yTo.selectedIndex * 12)];
		if(dTo.selectedIndex >= maxDaysInToMonth) { dTo.selectedIndex = maxDaysInToMonth - 1 }
	}
}


function getFormElementByName(source, elName) {
	var i = 0, total, element;
	total = source.elements.length;
	
	while ((!element)  && (i < total)) {
		tmpEl = source.elements[i];
		if (tmpEl.name == elName) {
			element = tmpEl;
		}
		i++;
	}
	return element;
}




function checkDOBs(source) {
	//alert(source.name);
	// Check all dates of birth
	if ((source.name == 'searchForm') || (source.name == 'searchformFlights')) {
		name = source.name;
	} else {
	//alert('here');
		name = source.form.name;
	}
	
	if (name == 'searchformFlights') {
		prefix = 'cf';
	} else {
		prefix = 'ph';
	}
	
//	for (i=1;i<9;i++) { 
//		checkDOB(prefix, i); 
//		if (i<5) {
//			checkIDOB(prefix, i);
//		}
//	}		
}

function checkDOB(prefix, dob) {
//alert(prefix +' '+ dob)
	if (prefix == 'ph') { theForm = document.searchForm; }
	else if (prefix == 'cf') { theForm = document.searchformFlights; }
	
	alert(theForm.name);
	
	// Checks that the child date of births are valid; if not then amend them so the are
	// Must be 2 years from the return date (minus flexibility) and less than 17 years from departure date (minus flex)
	//var target = new Date(document.getElementById('startdate_y').value, document.getElementById('startdate_m').value-1, document.getElementById('startdate_d').value);
	// Need to account for flexibility
	//var duration = document.getElementById('duration').value * 1000 * 60 * 60 * 24;
	
	alert(getFormElementByName(theForm, 'flexibility').value);
	
	var flex = getFormElementByName(theForm, 'flexibility').value * 1000 * 60 * 60 * 24;
	if (getFormElementByName(theForm, 'duration')) {
		var target = new Date(getFormElementByName(theForm, 'startdate_y').value, getFormElementByName(theForm, 'startdate_m').value-1, getFormElementByName(theForm, 'startdate_d').value);
		var duration = getFormElementByName(theForm, 'duration').value * 1000 * 60 * 60 * 24;
	} else {	// There is no duration - calculate it
		var target = new Date(getFormElementByName(theForm, 'enddate_y').value, getFormElementByName(theForm, 'enddate_m').value-1, getFormElementByName(theForm, 'enddate_d').value);
		var duration = 0;
	}
	//var flex = document.getElementById('flexibility').value * 1000 * 60 * 60 * 24;
	
	target = Date.parse(target) + duration + flex;
	
	var earliest = target - ((365 * 24 * 60 * 60 * 1000)*2);	// 1 years * 24 h *  60 m * 60 s * 1000 = 2 years in milliseconds
	var latest = target - ((365 * 24 * 60 * 60 * 1000)*17);	// 1 years * 24 h *  60 m * 60 s * 1000 = 17 years in milliseconds
	
	//var dateOfBirth = new Date(document.getElementById('dob'+dob+'_year').value, document.getElementById('dob'+dob+'_mnth').value - 1, document.getElementById('dob'+dob+'_day').value);
//	var dateOfBirth = new Date(getFormElementByName(theForm, 'dob'+dob+'_year').value, getFormElementByName(theForm, 'dob'+dob+'_mnth').value - 1, getFormElementByName(theForm, 'dob'+dob+'_day').value);
//	if (dateOfBirth < latest) {
//		latest = new Date(latest);
//		dy = latest.getDate();
//		if (dy < 10) dy = '0' + dy;
//		mn = latest.getMonth()+1;
//		if (mn < 10) mn = '0' + mn;
//		/*document.getElementById('dob'+dob+'_day').value = dy;
//		document.getElementById('dob'+dob+'_mnth').value = mn;
//		document.getElementById('dob'+dob+'_year').value = latest.getFullYear();*/
//		getFormElementByName(theForm, 'dob'+dob+'_day').value = dy;
//		getFormElementByName(theForm, 'dob'+dob+'_mnth').value = mn;
//		getFormElementByName(theForm, 'dob'+dob+'_year').value = latest.getFullYear();
//	}
//	if (dateOfBirth > earliest) {
//		// Child is too young; adjust
//		earliest = new Date(earliest);
//		dy = earliest.getDate();
//		if (dy < 10) dy = '0' + dy;
//		mn = earliest.getMonth()+1;
//		if (mn < 10) mn = '0' + mn;
//		/*document.getElementById('dob'+dob+'_day').value = dy;
//		document.getElementById('dob'+dob+'_mnth').value = mn;
//		document.getElementById('dob'+dob+'_year').value = earliest.getFullYear();*/
//		getFormElementByName(theForm, 'dob'+dob+'_day').value = dy;
//		getFormElementByName(theForm, 'dob'+dob+'_mnth').value = mn;
//		getFormElementByName(theForm, 'dob'+dob+'_year').value = earliest.getFullYear();
//	}
	
//	var agefull = (target - dateOfBirth) / 1000 / 60 / 60 / 24 / 365;
//	var age = parseInt(agefull);
//	if (age > agefull) {
//			age--;
//	}

//	/*dateOfBirth = new String(document.getElementById('dob'+dob+'_year').value + document.getElementById('dob'+dob+'_mnth').value + document.getElementById('dob'+dob+'_day').value);
//	document.getElementById('dob'+dob).value = dateOfBirth;


//	var divText = '&nbsp;(' + age + ')';
//	document.getElementById('dob'+dob+'div').innerHTML = divText;	*/
//	dateOfBirth = new String(getFormElementByName(theForm, 'dob'+dob+'_year').value + getFormElementByName(theForm, 'dob'+dob+'_mnth').value + getFormElementByName(theForm, 'dob'+dob+'_day').value);
//	getFormElementByName(theForm,'dob'+dob).value = dateOfBirth;
	
//	var divText = '&nbsp;(' + age + ')';
//	document.getElementById(prefix + 'dob'+dob+'div').innerHTML = divText;	
}

function checkIDOB(prefix, dob) {
	if (prefix == 'ph') { theForm = document.searchForm; }
	else if (prefix == 'cf') { theForm = document.searchformFlights; }
	
	// Checks that the child date of births are valid; if not then amend them so the are
	// Must be 2 years from the return date (minus flexibility) and less than 17 years from departure date (minus flex)
	// Need to account for flexibility
	var flex = getFormElementByName(theForm, 'flexibility').value * 1000 * 60 * 60 * 24;
	if (getFormElementByName(theForm, 'duration')) {
		var target = new Date(getFormElementByName(theForm, 'startdate_y').value, getFormElementByName(theForm, 'startdate_m').value-1, getFormElementByName(theForm, 'startdate_d').value);
		var duration = getFormElementByName(theForm, 'duration').value * 1000 * 60 * 60 * 24;
	} else {	// There is no duration - calculate it
		var target = new Date(getFormElementByName(theForm, 'enddate_y').value, getFormElementByName(theForm, 'enddate_m').value-1, getFormElementByName(theForm, 'enddate_d').value);
		var duration = 0;
	}
	
	target = Date.parse(target) + duration + flex;
	
	var earliest = target;
	var latest = target - ((365 * 24 * 60 * 60 * 1000)*2);	// 1 years * 24 h *  60 m * 60 s * 1000 = 2 years in milliseconds
	
	var dateOfBirth = new Date(getFormElementByName(theForm, 'idob'+dob+'_year').value, getFormElementByName(theForm, 'idob'+dob+'_mnth').value - 1, getFormElementByName(theForm, 'idob'+dob+'_day').value);
	if (dateOfBirth < latest) {
		latest = new Date(latest);
		dy = latest.getDate();
		if (dy < 10) dy = '0' + dy;
		mn = latest.getMonth()+1;
		if (mn < 10) mn = '0' + mn;
		getFormElementByName(theForm, 'idob'+dob+'_day').value = dy;
		getFormElementByName(theForm, 'idob'+dob+'_mnth').value = mn;
		getFormElementByName(theForm, 'idob'+dob+'_year').value = latest.getFullYear();
	}
	if (dateOfBirth > earliest) {
		// Child is too young; adjust
		earliest = new Date(earliest);
		dy = earliest.getDate();
		if (dy < 10) dy = '0' + dy;
		mn = earliest.getMonth()+1;
		if (mn < 10) mn = '0' + mn;
		getFormElementByName(theForm, 'idob'+dob+'_day').value = dy;
		getFormElementByName(theForm, 'idob'+dob+'_mnth').value = mn;
		getFormElementByName(theForm, 'idob'+dob+'_year').value = earliest.getFullYear();
	}
	
	var agefull = (target - dateOfBirth) / 1000 / 60 / 60 / 24 / 365;
	var age = parseInt(agefull);
	if (age > agefull) {
		age--;
	}
	if (age < 0) { age = 0; }
	
	dateOfBirth = new String(getFormElementByName(theForm, 'idob'+dob+'_year').value + getFormElementByName(theForm, 'idob'+dob+'_mnth').value + getFormElementByName(theForm, 'idob'+dob+'_day').value);
	getFormElementByName(theForm,'idob'+dob).value = dateOfBirth;
	
	var divText = '&nbsp;(' + age + ')';
	document.getElementById(prefix + 'idob'+dob+'div').innerHTML = divText;	
}
