function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  
	if(!d) d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
  		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
  	if(!(x=d[n])&&d.all) 
		x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) 
		x=d.forms[i][n];
  	for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
		x=MM_findObj(n,d.layers[i].document);
  	if(!x && d.getElementById) 
		x=d.getElementById(n); return x;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; 
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) 
  	x.src=x.oSrc;
}


function MM_changeProp(objName,x,theProp,theValue) { //v6.0
	var obj = MM_findObj(objName);
	if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    	if (theValue == true || theValue == false)
			eval("obj."+theProp+"="+theValue);
    	else eval("obj."+theProp+"='"+theValue+"'");
  	}
}


function interestedInDates(selectDay1, selectDay2, selectDay3, selectDay4) {
	if (selectDay1.selectedIndex == 0 && selectDay2.selectedIndex==0 && selectDay3.selectedIndex==0 && selectDay4.selectedIndex ==0)  {
		return false;
	} 
	return true;
}

function populateDates (something, somethingElse) {}

function wrongDates(startDateSelect, endDateSelect, startMonthSelect, endMonthSelect) {
	if (startDateSelect.selectedIndex == 0 && startMonthSelect.selectedIndex !=0) return true;
	if (startDateSelect.selectedIndex != 0 && startMonthSelect.selectedIndex ==0) return true;
	if (endDateSelect.selectedIndex == 0 && endMonthSelect.selectedIndex !=0) return true;
	if (endDateSelect.selectedIndex != 0 && endMonthSelect.selectedIndex ==0) return true;
	return false;
}

function checkAndSubmit(aForm) { 
	var theForm = MM_findObj(aForm);
	var startDateSelect = theForm.start_date;
	var endDateSelect = theForm.end_date;
	var startMonthSelect = theForm.start_month;
	var endMonthSelect = theForm.end_month;

	dates = interestedInDates(startDateSelect, endDateSelect, startMonthSelect, endMonthSelect);
	
	if (!dates) {
		theForm.submit();
	} else if (wrongDates(startDateSelect, endDateSelect, startMonthSelect, endMonthSelect)){
		alert ("Please choose a day and a month for departure and return");
	} else {
  		var startAsDate, endAsDate;
  		
  		if (startMonthSelect.selectedIndex != 0) {
			var startDateValue = startDateSelect.options[startDateSelect.selectedIndex].text;
			var startMonthValue = startMonthSelect.options[startMonthSelect.selectedIndex].text;
			var startDate = startDateValue+" "+startMonthValue;
			startAsDate = new Date(startDate);  		
  		}
  		
  		if (endMonthSelect.selectedIndex != 0) {
  			var endMonthValue = endMonthSelect.options[endMonthSelect.selectedIndex].text;
			var endDateValue = endDateSelect.options[endDateSelect.selectedIndex].text;
			var endDate = endDateValue+" "+endMonthValue;
			endAsDate = new Date(endDate);
		}

  		
  		var durnSelect = theForm.duration;
  		var durnValue = durnSelect.options[durnSelect.selectedIndex].value;
		today = new Date();  		

  		
  		if (endMonthSelect.selectedIndex == 0) {
  			//work out end dates from start date and duration
  			var end = startAsDate.getTime() + (durnValue * 1000 * 60 * 60 * 24);
  			endAsDate = new Date(end);
  		}
		
		if (startMonthSelect.selectedIndex == 0) {
			//work out start dates from end date and duration and make sure not in past.
		  	startAsDate = new Date(endAsDate - (durnValue * 1000 * 60 * 60 * 24));
		  	diffBetweenStartAndNow = startAsDate-today;
			if (diffBetweenStartAndNow < 0) {
				alert ("It is too late for you to go for that number of days and return on the chosen date.");
				return;
			}
  		}
		
  
  		var timediff = endAsDate-startAsDate;
  		var days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
  
  		var timediffStartNow = startAsDate - today;
  		var timediffStartNowDays = Math.floor(timediffStartNow / (1000 * 60 * 60 * 24));
  		
  		var timediffEndNow = endAsDate - today;
  		var timediffEndNowDays = Math.floor(timediffEndNow / (1000 * 60 * 60 * 24));
  
  		if (timediffStartNowDays < -1) alert ("The start date you have given has already passed.");

  		else if (timediffEndNowDays > -1) {
		//selected end date...
			if (days < 0) alert ("The holiday end date you have given is before the start date you have given.");
			else if (days < durnValue) {
				alert ("The holiday start and end date you have given don't give enough time for you to go for "+durnValue+" nights.");
			}
			else {
				theForm.submit();
			}
		}
  
  		else {
			theForm.submit();
		}
   	}
}

function MM_swapImage() { //v3.0
  
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3) {
  	var x = MM_findObj(a[i]);
	
   	if (x!=null) {
         document.MM_sr[j++]=x; 
		 if(!x.oSrc) x.oSrc=x.src; 
		 x.src=a[i+2];
	} else {
		
	}
}
}

function showMenu (nameofthemenu) {
	var menu = MM_findObj(nameofthemenu);
	menu.style.visibility = 'visible';
}

function hideMenu (nameofthemenu) {
	var menu = MM_findObj(nameofthemenu);
	menu.style.visibility = 'hidden';
}

function showDropdownMenu() {
	var hidDiv = MM_findObj('destDropdown');
	hidDiv.style.visibility='visible';
}

function hideAfterABit(e) {
	//window.setTimeout(hide, 500);  //nice to have but need to cancel timeout if something else happening
	hide();
}
function hide() {
	var hidDiv = MM_findObj('destDropdown');
	hidDiv.style.visibility='hidden';
}

function checkMouseLeave (element, evt) {
	if (element.contains && evt.toElement) {
		return !element.contains(evt.toElement);
	}
	else if (evt.relatedTarget) {
		return !containsDOM(element, evt.relatedTarget);
	}
}

function containsDOM (container, containee) {
	var isParent = false;
	do {
		if ((isParent = container == containee))
		break;
		containee = containee.parentNode;
	}
	while (containee != null);
	return isParent;
}

function init() {
//for template.  Overrride
}