function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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_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_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

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)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function submitNewsletter() {
	document.form_nl.action = "javascript:MM_openBrWindow('newsletter.php?m_email=" + document.form_nl.m_email.value + "','newsletter','scrollbars=yes,width=380,height=320')";
	document.form_nl.submit();
}





function openwin(page, name, w, h) {
	window.open(page, name, "toolbar=0,status=0,scrollbars=0,width="+w+",height="+h);
}
function refreshopener() {
	if (window.opener) {
		window.opener.location.reload(true); // JS 1.2
	}
	return;
}
// limit a field to the number of chars
function limitChars(thefield, n) {
	if (thefield.value.length > n) {
		alert("Warning! This field is limited to "+n+" characters.");
		thefield.value = thefield.value.substr(0,n);
	}
}


// 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);
}


function y2k(number) { return (number < 1000) ? number + 1900 : number; }

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;
}

function checkDate() {
	var d = document.formReservation.day.value;
	var m = document.formReservation.month.value; // month is in plain english
	var y = document.formReservation.year.value;
	var days = daysElapsed(new Date(d+" "+m+" "+y), new Date());
	if (days < 2) {
		alert("Unfortunately we cannot take reservations online within 48 hours of the time requested.\n Check the date.");
		return false;
	} else {
		return true;
	}
}

function checkTime() {
	var h_min = 7; var m_min = 30; // change these 4 constants to modify the time boundaries
	var h_max = 9; var m_max = 30; 
	var h = document.formReservation.hour.value;
	var m = document.formReservation.minute.value;
	if (h == "" || m == "") {
		// Don't warn; the formmail will warn about the missing fields.
		return true;
	}
	h = parseInt(h); // to avoid the + operator on strings (see h*60+m below)
	m = parseInt(m);
	if (((h*60+m) < (h_min*60+m_min)) || ((h*60+m) > (h_max*60+m_max))) {
		alert("Unfortunately we can only take reservations online between "+h_min+":"+m_min+" and "+h_max+":"+m_max+".");
		return false;
	} else {
		return true;
	}
}

function checkMandatory() {
	// get the required fields and split them to check them
	var i, s, rf, a_rf, retval;
	rf = document.formReservation.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 (document.formReservation[a_rf[i]].type == 'text' || 
			document.formReservation[a_rf[i]].type == 'textarea' ||
			document.formReservation[a_rf[i]].type == 'radio' ||
			document.formReservation[a_rf[i]].type == 'checkbox' ||
			document.formReservation[a_rf[i]].type == 'select-one') {
			
			s = '';
			if (document.formReservation[a_rf[i]].type == "select-one") {
				var si = document.formReservation[a_rf[i]].selectedIndex;
                if (si >= 0) {
                    s = document.formReservation[a_rf[i]].options[si].value;
                }
			} else if (document.formReservation[a_rf[i]].type == 'radio') {
				if (document.formReservation[a_rf[i]].checked) {
					s = document.formReservation[a_rf[i]].value;
				}
			} else if (document.formReservation[a_rf[i]].type == 'checkbox') {
				if (document.formReservation[a_rf[i]].checked) {
					s = '1';
				}
			} else {
				s = document.formReservation[a_rf[i]].value;
			}
			s = trim(s);
			if (isEmpty(s)) {
				document.formReservation[a_rf[i]].value = s;
				document.formReservation[a_rf[i]].focus();
				retval = false;
			}
			
		} else if ((document.formReservation[a_rf[i]].length > 0) && 
				(document.formReservation[a_rf[i]][0].type == 'radio' || 
				document.formReservation[a_rf[i]][0].type == 'checkbox')) {
			
			var loop, isChecked=-1;
            for (loop=0;loop < document.formReservation[a_rf[i]].length;loop++) {
                if (document.formReservation[a_rf[i]][loop].checked) {
                    isChecked=loop;
                    break; // only one needs to be checked
                }
            }
            if (isChecked < 0) {
                document.formReservation[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 checkFields() {
	return checkMandatory() && checkDate() && checkTime();
}


/* 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 = 'top=10,left=10,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;
}
