/**
 * Library of javascript functions
 *
 * @version 1.1
 */

// detect browser type
var dom = (document.getElementById) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;

// submit the form
function submitForm(theformname, theaction) {
	if (document.forms[theformname]) {
		document.forms[theformname].act.value = theaction;
		document.forms[theformname].submit();
	}
}

// open the link in a new window
// return false if the popup is shown, true otherwise
function openWindow(oLink, name, width, height, params) {
	var href = '';
	if (oLink.getAttribute) url = oLink.getAttribute('href');
	if (href=='') href = oLink.href;
	var all_params = '';
	all_params += 'width=' + width + ',height=' + height;
	if (params && params!='') all_params += ','+params;
	// all is ready, open the popup and focus on it
	var oPopup = window.open(href, (name && name!='' ? name : 'hdlpPopup'), all_params);
	if (oPopup) oPopup.focus();
	return (oPopup?false:true);
}

// open the link in a new window
function openNews(oLink) {
	return openWindow(oLink, 'hdlpPopupNews', 670, 670, 'directories=no,location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes');
}

// open the link in a new window
function openOptions(oLink) {
	return openWindow(oLink, 'hdlpPopupOptions', 670, 670, 'directories=no,location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes');
}

// open the link in a new window
function openPress(oLink) {
	return openWindow(oLink, 'hdlpPopupPress', 670, 670, 'directories=no,location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes');
}

// open the link in a new window
function openSendToFriend(oLink) {
	return openWindow(oLink, 'hdlpPopupSendToFriend', 500, 550, 'directories=no,location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes');
}

// open the link in the opener window
function showLinkInOpener(href) {
	if (window.opener) {
		window.opener.document.location.href = href;
		window.opener.focus();
	}
}

// show element
function show(id) {
	if (ns4) {
		if (document.layers[id]) {
			document.layers[id].visibility = "show";
			document.layers[id].display = "block";
		}
	} else if (ie4) {
		if (document.all[id]) {
			document.all[id].style.visibility = "visible";
			document.all[id].style.display = "block";
		}
	} else if (dom) {
		if (document.getElementById(id)) {
			document.getElementById(id).style.visibility = "visible";
			document.getElementById(id).style.display = "block";
		}
	}
}

// hide element
function hide(id) {
	if (ns4) {
		if (document.layers[id]) {
			document.layers[id].visibility = "hide";
			document.layers[id].display = "none";
		}
	} else if (ie4) {
		if (document.all[id]) {
			document.all[id].style.visibility = "hidden";
			document.all[id].style.display = "none";
		}
	} else if (dom) {
		if (document.getElementById(id)) {
			document.getElementById(id).style.visibility = "hidden";
			document.getElementById(id).style.display = "none";
		}
	}
}

// hide multiple div, beginning with 'pre' as prefix and a number
function hideMulti(pre, start, end) {
	var s, i;
	for (i=start; i<=end; i++) {
		s = pre+''+i;
		hide(s);
	}
}

// show a div if it is visible, hide it if it is hidden
function toggle(id) {
	if (ns4) {
		if (document.layers[id]) {
			if (document.layers[id].visibility == "hide" || document.layers[id].display == "none")
				show(id);
			else
				hide(id);
		}
	} else if (ie4) {
		if (document.all[id]) {
			if (document.all[id].style.visibility == "hidden" || document.all[id].style.display == "none")
				show(id);
			else
				hide(id);
		}
	} else if (dom) {
		if (document.getElementById(id)) {
			if (document.getElementById(id).style.visibility == "hidden" || document.getElementById(id).style.display == "none")
				show(id);
			else
				hide(id);
		}
	}
}


// trim the left and right spaces of a string
function trim(str) {
	return str.replace(/^\s+|\s+$/, '');
};

// returns true if the string is empty
function isEmpty(str) {
	return (str == null) || (str.length == 0);
}

// returns true if the string is a valid email
function isEmail(str) {
	if (isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

// return the year as 4 number
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

// return the number of days between two dates
function daysElapsed(date1,date2) {
	var difference =
		Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
	  - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
	return difference/1000/60/60/24;
}

// check if the date is at least X days after the curent date (month is in plain english)
function checkDate(d,m,y,numdays) {
	var days = daysElapsed(new Date(d+" "+m+" "+y), new Date());
	if (days < numdays) {
		alert("Unfortunately we cannot take reservations online within "+numdays+" days of the request.");
		return false;
	} else {
		return true;
	}
}

// check all mandatory fields of the form
// the mandatory fields are in a hidden field named "require"
function checkMandatory(theform) {
	// get the required fields and split them to check them
	var i, s, rf, a_rf, retval;
	rf = theform["require"].value;
	a_rf = rf.split(",");
	retval = true;
	
	// loop the fields to check them
	i=0;
	while (i < a_rf.length && retval) {
		// get type of field
		if (theform[a_rf[i]].type == 'text' || 
			theform[a_rf[i]].type == 'textarea' ||
			theform[a_rf[i]].type == 'radio' ||
			theform[a_rf[i]].type == 'checkbox' ||
			theform[a_rf[i]].type == 'select-one') {
			
			s = '';
			if (theform[a_rf[i]].type == "select-one") {
				var si = theform[a_rf[i]].selectedIndex;
				if (si >= 0) {
					s = theform[a_rf[i]].options[si].value;
				}
			} else if (theform[a_rf[i]].type == 'radio') {
				if (theform[a_rf[i]].checked) {
					s = theform[a_rf[i]].value;
				}
			} else if (theform[a_rf[i]].type == 'checkbox') {
				if (theform[a_rf[i]].checked) {
					s = '1';
				}
			} else {
				s = theform[a_rf[i]].value;
			}
			s = trim(s);
			if (isEmpty(s) || (a_rf[i]=="email" && !isEmail(s))) {
				theform[a_rf[i]].value = s;
				theform[a_rf[i]].focus();
				retval = false;
			}
			
		} else if ((theform[a_rf[i]].length > 0) && 
				(theform[a_rf[i]][0].type == 'radio' || 
				theform[a_rf[i]][0].type == 'checkbox')) {
			
			var loop, isChecked=-1;
			for (loop=0;loop < theform[a_rf[i]].length;loop++) {
				if (theform[a_rf[i]][loop].checked) {
					isChecked=loop;
					break; // only one needs to be checked
				}
			}
			if (isChecked < 0) {
				theform[a_rf[i]][0].focus();
				retval = false;
			}
			
		}
		i++;
	}
	
	// warn the user
	if (!retval)
		alert("We cannot process your reservation if you don't fill all mandatory fields."); // '
	
	return retval;
	
}

function checkReservation(theform) {
	if (theform) {
		var d = theform["arrivalday"].value;
		var m = theform["arrivalmonth"].value;
		var y = theform["arrivalyear"].value;
		var n = theform["numdays"].value;
		
		return checkMandatory(theform) && checkDate(d,m,y,n);
	} else {
		return false;
	}
}

function checkNewsletter(theform) {
	if (theform) {
		if (!isEmail(theform['email'].value)) {
			alert("The email is not correct");
			return false;
		} else
			return true;
	} else {
		return false;
	}
}

/* OTHER FUNCTIONS (PLEASE DON'T USE THEM) */
/* popup */

var sUserAgent = navigator.userAgent.toLowerCase();
var isIE = document.all?true:false;
var isNS4 = document.layers?true:false;
var isOp = (sUserAgent.indexOf('opera')!=-1)?true:false;
var isMoz = (sUserAgent.indexOf('mozilla/5')!=-1 && sUserAgent.indexOf('opera')==-1 && sUserAgent.indexOf('msie')==-1)?true:false;

// ****************************************************************
// Example use:
//   <a href="/foo.htm" onclick="return pop(this,'survey');" title="Survey opens a new window.">Take our survey!</a>
//   <a href="/foo.htm" onclick="return pop(this,'','width=200,height=200');" title="Opens a new window.">Foo</a>
//   <a href="/foo.htm" onclick="return pop(this);" title="Opens a new window.">Foo</a>
// ****************************************************************
function pop(oAnchor,sWindow,sProps){
 var sUrl = '';
 if(oAnchor.getAttribute) sUrl = oAnchor.getAttribute('href');
 if(sUrl=='' && isIE) sUrl = window.event.srcElement.getAttribute('href');
 if(sUrl=='') sUrl = oAnchor.href;
 var sWindowName = sWindow?sWindow:'_blank';
 if(!sProps) sProps = 'width=640,height=480,scrollbars,resizable,toolbar,status,menubar,location';
 if(sUrl) var oPopup = window.open(sUrl,sWindowName,sProps);
 if(oPopup && !isOp) oPopup.focus();
 return (oPopup)?false:true;
}
function redirect(sTarget,oAnchor,bCloseSelf){
 var sUrl = '';
 if(oAnchor.getAttribute) sUrl = oAnchor.getAttribute('href');
 if(sUrl=='' && isIE) sUrl = window.event.srcElement.getAttribute('href');
 if(sUrl=='') sUrl = oAnchor.href;
 if(sTarget && sUrl) {
  eval(sTarget+".location='"+sUrl+"'");
  eval(sTarget+".focus()'");
 }
 if(bCloseSelf) self.close();
 return false; // false invalidates the real href attribute
}
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}
