// 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){
	if (!document.getElementById) {
		hideMenu();
	    return;
	}
  	// get the menu content
  	var 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');
	var w = banner.style.width;
	var x = removePx(w);
	x  = (window.innerWidth - x)/2 + mw;
	var h = banner.clientHeight;
	h = (h+ht)+'px';
	clearHideMenu();
	// find the menu object
	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;
	menuobj.thestyle.left =  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";
}

function dynamicHide(e){
	menuobj = document.getElementById("popmenu");
	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  {
		while(source_el.id != "popmenu") {
			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++;
	}
}
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;
}
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);
}
function changeMonth(dobjname, mobjname, yobjname) {
	var obj = document.getElementById(mobjname);
	var mon = getDropDownValue(obj);
	obj = document.getElementById(yobjname);
	var yr = getDropDownValue(obj);
	var i = 0;
	while (i < 12 && months[i] != mon) i++;
	setDays(dobjname, i, yr);
}

function timer(){
	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; 
	if (ie4) document.all.todaysdate.value = hours + ':' + mins + ' on ' + day + '-' + (month+1) + '-' +year;
	else document.getElementById('todaysdate').value = hours + ':' + mins + ' on ' + day + '-' + (month+1) + '-' +year;  
	// set the options on the arrive year select
	var obj = 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);
	// set the arrive day list
	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
	if ((ie4 && document.all.depart_day) || (!ie4 && document.getElementById('depart_day'))) d_day = ie4 ? document.all.depart_day : document.getElementById('depart_day');
	else return;
	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
	nights = ie4 ? document.all.nights : document.getElementById('nights');
	nights.options.selectedIndex = 0;
}

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('arrive_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';
}
