var oldV = 1;
function pon(v) {

	for (i=1; i<=3; i++) {
		if (v != i) { document.getElementById('t' + i).style.display = 'none'; }
		else { document.getElementById('t' + i).style.display = 'block'; }
		resetHelp();
	}
	for (i=1; i<=3; i++) {
		if (v != i) { document.getElementById('id' + i).className='pantoff'; }
		else { document.getElementById('id' + i).className='panton'; }
	}
	if (!refresh) {
		with (document.search) {
			if(oldV==1) { schNumNights2.selectedIndex = schNumNights1.selectedIndex;
				schNumNights3.selectedIndex = schNumNights1.selectedIndex; }
			if(oldV==2) { schNumNights1.selectedIndex = schNumNights2.selectedIndex;
				schNumNights3.selectedIndex = schNumNights2.selectedIndex; }
			if(oldV==3) { schNumNights1.selectedIndex = schNumNights3.selectedIndex;
				schNumNights2.selectedIndex = schNumNights3.selectedIndex; }			   
		}
	}
	refresh = false;
	eval('document.search.active' + oldV).value = '';
	eval('document.search.active' + v).value = '1';
	
	oldV = v;
}

var refresh = false;
setTimeout('setDatePanel()',400);

function setDatePanel() {
	refresh = true;
	on = 1;
	if (document.search.active2.value == 1) on=2;
	if (document.search.active3.value == 1) on=3;
	if (on!= 1) { pon(on); } else { refresh=false; }
}

function makeDate(d) {
	dy = d.substring(0,d.indexOf('/'));
	m = d.substring(d.indexOf('/')+1,d.lastIndexOf('/'));
	y = d.substr(d.lastIndexOf('/')+1);
	dt = '';
	if (dy == '' || m == '' || y == '') {
		//--
	} else if (dy > 31 || m <= 0 || m > 12) {
		//--
	} else {
		if (y.length == 2) y = '20' + y;
		dt = new Date(y, m-1, dy);
		nw = new Date();
		if (dt < nw) {
			dt = '';
		}
	}
	return dt;
}
function submitSearch() {
	// Setup temporary values
	loc = document.search.schLocation.value;
	people = document.search.schNumPeople.value;
	infants = document.search.schInfants.value;
	cot = document.search.schCotRequired.checked;
	
	act1 = document.search.active1.value!='1'?false:true;
	act2 = document.search.active2.value!='1'?false:true;
	act3 = document.search.active3.value!='1'?false:true;
	start = '';
	month = '';
	fromd = '';
	tod = '';
	dateerror = false;
	mondiff = 0;
	if (act2) {
		start = document.search.schStartDate.value;
		if (start=='dd/mm/yyyy') start = '';
		nights = document.search.schNumNights1.value;
		startdt = makeDate(start);
		if(startdt == '') dateerror = true;
	} else if (act1) {
		month = document.search.schMonth.value;
		nights = document.search.schNumNights2.value;
	} else {
		fromd = document.search.schSearchFrom.value;
		if (fromd=='dd/mm/yyyy') fromd = '';
		tod = document.search.schSearchTo.value;
		if (tod=='dd/mm/yyyy') tod = '';
		nights = document.search.schNumNights3.value;

		if (fromd != '' && tod != '') {
			fromdt = makeDate(fromd);
			todt = makeDate(tod);
			if (fromdt != '' && todt != '') {
				todtnights = new Date();
				todtnights.setFullYear(todt.getFullYear());
				todtnights.setMonth(todt.getMonth());
				todtnights.setDate(todt.getDate()*1-nights*1);
			}
			if (fromdt == '' || todt == '')	{
				dateerror = true;
			} else if (todtnights < fromdt) {
				dateerror = true;
			} else {
				fdf = (fromdt.getFullYear()*12) + fromdt.getMonth();
				tdf = (todt.getFullYear()*12) + todt.getMonth();
				mondiff = tdf - fdf;
			}
		}
	}
	//prices = document.search.schPriceRange.value;
	prices = '';

	er = '';
	// Check for Party errors
	if (act3 && fromd == '' && tod != '') {
		er = 'You have not entered a \'Search From\' date.<br><br>Please enter a date and try again.';
	} else if (act3 && fromd != '' && tod == '') {
		er = 'You have not entered a \'Search To\' date.<br><br>Please enter a date and try again.';
	} else if (loc=='' && start=='' && month=='' && (fromd=='' || tod=='') && nights=='' && (people*1+infants*1<8)) {
		er = 'You must enter some key search information.<br><br>Select at least a location or your desired holiday dates.';
	} else if(people=='' && infants != '') {
		er = 'You have selected ' + infants + ' infants, but have not specified a total party size.<br><br>Please enter the number of people and try again.';
	} else if (cot && infants == '') {
		er = 'You have selected that you want us to supply a cot, but have also stated that your party has no infants.<br><br>Please enter the number of infants and try again.';
	} else if (act2 && nights != '' && start == '') {
		er = 'You have selected a number of nights, but have not entered a start date.<br><br>Please enter a start date and try again.';
	} else if (act1 && nights != '' && month == '') {
		er = 'You have selected a number of nights, but have not selected a month (and year).<br><br>Please select a month and try again.';
	} else if (act3 && nights != '' && tod == '' && fromd == '') {
		er = 'You have selected a number of nights, but have not given a date range.<br><br>Please enter a date range and try again.';
	} else if (prices != '' && ((act2 && start == '') || (act1 && month == '') || (act3 && (fromd == '' && tod == '')))) {
		er = 'You must select a date range if you wish to search on prices.<br><br>Please select your holiday dates and try again.';
	} else if (act2 && dateerror) {
		er = 'The start date you entered is not valid.<br><br>Please re-enter your date and try again.';
	} else if (act3 && dateerror) {
		er = 'One or more of your dates are not valid.<br><br>Please re-enter your dates and try again.';
	} else if (act3 && (mondiff < 0 || mondiff > 2)) {
		er = 'You have selected a date range, but you can only search three consecutive months inclusively as a maximum.<br><br>Please reduce your range.';
	}
	//document.submitb.style.cursor = 'wait';
	if (er != '') {
		error = true;
		document.getElementById('help').style.border = '1px solid #FF0000';
		document.getElementById('help').innerHTML = '' + er;
		setTimeout('resetSubmitCursor()',600);
	} else {
		error = true;
		document.getElementById('help').innerHTML = '';
		document.search.submit();
	}
}
function resetSubmitCursor() {
	//document.submitb.style.cursor = 'pointer'; 
	//document.submitb.style.cursor = 'hand'; 
	document.getElementById('submitb').style.cursor = 'pointer'; 
	document.getElementById('submitb').style.cursor = 'hand'; 
}
function dfoc(ob) {
	if(ob.value=='dd/mm/yyyy') { ob.value=''; ob.style.backgroundImage = 'url(images/im_datebg.gif)';} 
}
function dblur(ob) {
	if(ob.value=='') { ob.value='dd/mm/yyyy'; } 
}
function dkey(ob) {
	if(ob.value=='') {
		ob.style.backgroundImage = 'url(images/im_datebg.gif)';
	} else {
		ob.style.backgroundImage = '';
	}
}
function setNightsOp1(ob) {
	switch(ob.name) {
		case 'schStartDate': v = 'schNumNights1'; break;
		case 'schSearchTo':
		case 'schSearchFrom': v = 'schNumNights3'; break;
		default: v = 'schNumNights2';
	}
	o = eval('document.search.' + v + '');
	if (ob.value!='') {
		o.options[0].text = '<select>';
	} else {
		o.options[0].text = '<any>';
	}
}