// show drop-down menu - params are event and menuno
var menuobj;
var opera = navigator.userAgent.indexOf('Opera')>=0 ? true : false;
var ie4 = document.all;
var ns6 = document.getElementById&&!document.all;
var ns4 = document.layers&&!document.getElementById;

function removePx(s) {
	var p = s.indexOf("px");
	return eval(s.replace(/px/, ""));
}
function showMenu(e, menuno, menuindex, type){
	if (!document.getElementById) {
		hideMenu();
	    return;
	}
  	// get the menu content
  	var divobj;
	if (type) divobj = document.getElementById("sidemenu_"+menuno);
	else divobj = document.getElementById("menu_"+menuno);
	if (!divobj) {
		// then no drop-down menu for this cell
		hideMenu();
		return;
	}
  	var which = divobj.innerHTML;
	// get the position of the element which was mouseover-ed
	var target = e.target;
	if (target.firstChild.nodeName != 'A') target = target.parentNode;
	// get the width of the menu <td>
	var mw = target.clientWidth;
	var ht = target.clientHeight+10;
	mw = mw*menuindex;
	// use the banner to get the positions
	var banner = document.getElementById('thebanner');
	if (!type) {
		var w = banner.style.width;
		var x = removePx(w);
		x  = (window.innerWidth - x)/2 + mw;
		var h = banner.clientHeight;
		h = (h+ht);
	}
	else {
		var h = banner.clientHeight;
		var w = banner.style.width;
		var x = removePx(w);
		if (mw == 0) x = (window.innerWidth - x) / 2 - 10 + target.clientWidth;
		else x  =  (window.innerWidth - x)/2 + mw / menuindex - 10;
		h = h + (ht - 10)*menuindex + 45;
	}
	clearHideMenu();
	// find the menu object
	if (type) menuobj = document.getElementById("spopmenu");
	else menuobj = document.getElementById("popmenu");
	if (!menuobj) return;
	menuobj.innerHTML=which;
	menuobj.contentwidth = menuobj.offsetWidth;
	menuobj.contentheight =  menuobj.offsetHeight;
	menuobj.thestyle = menuobj.style;
	menuobj.thestyle.top =  h + 'px';
	menuobj.thestyle.left =  Math.round(x) + 'px';
	menuobj.thestyle.width = target.offsetWidth + 'px';
	menuobj.contentwidth = target.offsetWidth;
	menuobj.style.visibility = "visible";
	menuobj.target = target;
	return false;
}

function hideMenu(){
	menuobj = document.getElementById("popmenu");
	if (menuobj) menuobj.thestyle = menuobj.style;
	if (menuobj) menuobj.thestyle.visibility = "hidden";
	menuobj = document.getElementById("spopmenu");
	if (menuobj) menuobj.thestyle = menuobj.style;
	if (menuobj) menuobj.thestyle.visibility = "hidden";
}

function dynamicHide(e){
	menuobj = document.getElementById("popmenu");
	if (!menuobj) return;
	if (e.currentTarget!= e.relatedTarget && !contains(e.currentTarget, e.relatedTarget)) {
		hideMenu();
	}
	menuobj = document.getElementById("spopmenu");
	if (!menuobj) return;
	if (e.currentTarget!= e.relatedTarget && !contains(e.currentTarget, e.relatedTarget)) {
		hideMenu();
	}
}

function delayHideMenu(){
	window.delayhide = setTimeout("hideMenu()",500);
}

function clearHideMenu() {
	if (window.delayhide)  clearTimeout(window.delayhide);
}

function highlightMenu(e,state) {
	var source_el;
	source_el = e.target;
	if (source_el.nodeName == 'A') source_el = source_el.parentNode;
	source_el.background = eval(state);
	if (source_el.className == "menu") {
		source_el.style.background=eval(state);
		//source_el.id=(state=="on")? "mouseoverstyle" : "mouseoutstyle";
	}
	else if (source_el.className == "sidemenu") {
		source_el.style.background=eval(state);
		//source_el.id=(state=="son")? "mouseoverstyle" : "mouseoutstyle";
	}
	else  {
		while(source_el.id != "popmenu" && source_el.id != "spopmenu") {
			source_el = document.getElementById ? source_el.parentNode : source_el.parentElement
			if (source_el.className=="menuitems") {
				source_el.style.background=eval(state);
				//source_el.id=(state=="on")? "mouseoverstyle" : "mouseoutstyle";
			}
		}
	}
	clearHideMenu();
}

function contains(a, b) {
	// Determines if 1 element in contained in another
	var c = b;
	while (c && c.parentNode) {
		if ((c = c.parentNode) == a) {
                      return true;
		}
	}
	return false;
}
function popup(myUrl) {
   	var topBars= 'directories=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no,dependent=yes';
   	var myOptions = 'scrollbars=yes,width=640,height=500,resizeable=no,top=10,left=50';
   	var myFeatures = topBars+','+myOptions;
   	var newWin = open(myUrl,'',myFeatures);
     // 	newWin.focus();
}

function showImage(fname, w, h, cap) {
	var obj = document.getElementById('largeimage');
	obj.src = fname;
	obj.style.width = w;
	obj.style.height = h;
	obj.style.alt = cap;
	obj = document.getElementById('background');
	obj.style.visibility = 'visible';
	obj = document.getElementById('foreground');
	obj.style.visibility = 'hidden';
	obj = document.getElementById('backlink');
	obj.style.visibility = 'visible';
	obj = document.getElementById('caption');
	obj.style.visibility = 'visible';
	obj.innerHTML = cap;
	var i = 0;
	var imagewidths = new Array();
	var imageheights = new Array();
	while (obj = document.getElementById('album_image_'+i)) {
		imageheights[i] = obj.style.height;
		obj.style.height = 1;
		imagewidths[i] = obj.style.width;
		obj.style.width = 1;
		i++;
	}
	document.widths = imagewidths;
	document.heights = imageheights;
	document.albumsize = i;
}
function hideImage() {
	obj = document.getElementById('background');
	obj.style.visibility = 'hidden';
	obj = document.getElementById('foreground');
	obj.style.visibility = 'visible';
	var obj = document.getElementById('largeimage');
	obj.style.width = 0;
	obj.style.height = 0;
	obj = document.getElementById('backlink');
	obj.style.visibility = 'hidden';
	obj = document.getElementById('caption');
	obj.style.visibility = 'hidden';
	obj.innerHTML = '';
	var imagewidths = document.widths;
	var imageheights = document.heights;
	var i = 0;
	while (obj = document.getElementById('album_image_'+i)) {
		obj.style.height = imageheights[i];
		obj.style.width = imagewidths[i];
		i++;
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Block of date / time functions for standard enquiry form           ////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// some arrays for dealing with the months        ///
/////////////////////////////////////////////////////////////////////////////
var monlen = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
///////////////////////////////////////////////////////////////////////
// utility fn to get value of a <select> box    ///
///////////////////////////////////////////////////////////////////////
function getDropDownValue(sel) {
	//alert(sel.options[sel.selectedIndex].value);
	return sel.options[sel.selectedIndex].value;
}
////////////////////////////////////////////////////////////////////////
// utilty fn to check if a year is a leap year     ///
///////////////////////////////////////////////////////////////////////
function isLeapYear(y) {
	if (y%4 != 0) return 0;
	if(y%100 == 0 && y%400 !=0) return 0;
	return 1;
}
/////////////////////////////////////////////////////////////////////////////////
// set the drop-down list of days in a select object ///
////////////////////////////////////////////////////////////////////////////////
function setDays(objname, month, year) {
	var obj = document.getElementById(objname);
	obj.options.length = 0;
	var i = 0;
	for (i = 0; i < monlen[month] ; i++) {
		obj.options[i] = new Option(i+1,i+1);
	}
	if (month == 1 && isLeapYear(year)) obj.options[28] = new Option(29,29);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// called when changing the value of a month drop-down select object             ///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function changeMonth(dobjname, mobjname, yobjname) {
	var mobj = document.getElementById(mobjname);
	var mon = getDropDownValue(mobj);
	var yobj = document.getElementById(yobjname);
	var yr = getDropDownValue(yobj);
	var i = 0;
	while (i < 12 && months[i] != mon) i++;
	////////////////////////////////////////////////////////////////////////////////
	// set the corresponding days drop-down select   //
	///////////////////////////////////////////////////////////////////////////////
	setDays(dobjname, i, yr);
	//////////////////////////////////////////////////////////////////////////////
	// do we need to make 'depart-month' the same? //
	//////////////////////////////////////////////////////////////////////////////
	if (mobjname=='arrive_month') {
		var obj = document.getElementById('depart_month');
		if  (obj) {
			obj.options.selectedIndex = i;
			changeMonth('depart_day', 'depart_month','depart_year');
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////
// timer function - should be called on loading the page ///
///////////////////////////////////////////////////////////////////////////////////////
function timer(){
	var a_day, a_mon, a_year, d_day, d_mon, d_year;
	//////////////////////////////////////////////////////////////////////
	// get the current time and date                 ///
	////////////////////////////////////////////////////////////////////
	var now = new Date();
	var year = now.getYear();
	if (navigator.appName=='Netscape' && year < 100) year += 1900;
	if (year < 1000) year += 1900;
	var day = now.getDate();
	var month = now.getMonth();
	//months++;
	var hours = now.getHours();
	var mins = now.getMinutes();
	if (mins < 10) mins = "0"+mins; 
	///////////////////////////////////////////////////////////////////////////
	// put the date/time in the todaysdate field        //
	///////////////////////////////////////////////////////////////////////////
	var todaysdate = ie4 ? document.all.todaysdate : document.getElementById('todaysdate');
	if (todaysdate != null) todaysdate.value = hours + ':' + mins + ' on ' + day + '-' + (month+1) + '-' +year;
	////////////////////////////////////////////////////////////////////////////////////
	// set the options on the arrive year select object     ///
	///////////////////////////////////////////////////////////////////////////////////
	var obj = ie4 ? document.all.arrive_year : document.getElementById('arrive_year');
	obj.options.length = 0;
	obj.options[0] = new Option(year,year,true);
	obj.options[1] = new Option((year+1),(year+1), false);
	////////////////////////////////////////////////////////////////////////////////////////
	// do the same for the depart year object - if it exists   ///
	///////////////////////////////////////////////////////////////////////////////////////
	obj = ie4 ? document.all.arrive_year : document.getElementById('depart_year');
	if (obj) {
		obj.options.length = 0;
		obj.options[0] = new Option(year,year,true);
		obj.options[1] = new Option((year+1),(year+1), false);
	}
	///////////////////////////////////////////////////////////////////////////////
	// set the arrive day/month/year drop-down lists  //
	//////////////////////////////////////////////////////////////////////////////
	setDays('arrive_day', month, year);
	a_day = ie4 ? document.all.arrive_day : document.getElementById('arrive_day');
	a_day.options.selectedIndex = (day-1);
	a_mon = ie4 ? document.all.arrive_month : document.getElementById('arrive_month');
	a_mon.options.selectedIndex = (month);
	a_year = ie4 ? document.all.arrive_year : document.getElementById('arrive_year');
	a_year.options.selectedIndex = 0;
	/////////////////////////////////////////////////////////////////////////////////////////////////////
	// departure settings - these are optional and may not exist       //
	/////////////////////////////////////////////////////////////////////////////////////////////////////
	d_day = ie4 ? document.all.depart_day : document.getElementById('depart_day');
	if (d_day) d_day.options.selectedIndex = (day-1);
	d_mon = ie4 ? document.all.depart_month : document.getElementById('depart_month');
	if (d_mon) d_mon.options.selectedIndex = (month);
	d_year = ie4 ? document.all.depart_year : document.getElementById('depart_year');
	if (d_year) d_year.options.selectedIndex = 0;
	nights = ie4 ? document.all.nights : document.getElementById('nights');
	if (nights) nights.options.selectedIndex = 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// clicking the submit button calls this function, which checks the dates are viable //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkDates() {
	var now = new Date();
	var year = now.getYear();
	if (year<1000) year+=1900;
	var day = now.getDate();
	if (day < 10) day = '0'+day;
	var month = now.getMonth()+1;
	if (month < 10) month = '0'+month;
	var sqldate = year+'-'+ month+'-'+ day;
	var valid = 1;
	var obj = document.getElementById('arrive_year');
	var ayear = getDropDownValue(obj);
	obj = document.getElementById('arrive_month');
	var amonth = getDropDownValue(obj);
	var i = 0;
	while (i < 12 && months[i] != amonth) i++;
	amonth = i;
	if (amonth < 10) amonth = '0'+amonth;
	obj = document.getElementById('a_day');
	var aday = getDropDownValue(obj);	
	if (aday<10) aday = '0'+aday;
	if (aday == 0 || amonth ==0 || ayear == 0) valid = 0;
	var adate =  ayear+'-'+ amonth+'-'+ aday;
	if (adate < sqldate) valid = 0;
	alert(adate);
	if (!valid) alert('You must enter a valid date which is NOT today\'s date');
	else window.document.location='bookingrequest.php';
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// End of block of date / time functions for enquiry form               ////
/////////////////////////////////////////////////////////////////////////////////////////////////////////

