// Global settings
/*
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}

// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position
        // reflects the position from the top/left of the screen the
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }

    window.status = "xMousePos=" + xMousePos + ", yMousePos=" + yMousePos + ", xMousePosMax=" + xMousePosMax + ", yMousePosMax=" + yMousePosMax;
}*/

function positionMenu(){
	var noPx = document.childNodes ? 'px' : 0;

	var menuItems = document.getElementsByName('dinnerMenuItem');
	var parentItem = document.getElementById('menuParent');

	var x = findPosX(parentItem);
	var y = findPosY(parentItem);
	var w = 75;

	x -= findPosX(menuItems[0]);	// Need to subtract any offset pixels from any parent objects
	y += parentItem.style.height;	// Align it just below the main menu item

	menuItems[0].style.left = x + noPx;
	menuItems[0].style.top = y + noPx;
	menuItems[0].style.width = w + noPx;
	menuItems[0].style.visibility = "visible";

	for(i=1; i<menuItems.length; i++){
		x = findPosX(menuItems[i-1]) - findPosX(menuItems[i]);
		y = findPosY(menuItems[i-1]) + menuItems[i-1].style.height;
		menuItems[i].style.left = x + noPx;
		menuItems[i].style.top = y + noPx;
		menuItems[i].style.width = w + noPx;
		menuItems[i].style.visibility = "visible";
	}
}

function openMenu(event, menuToShow, parentItem) {
	menuToShow = document.getElementById(menuToShow);
	parentItem = document.getElementById(parentItem);
	menuToShow.style.visibility = "visible";
}

function hideMenu(divID){
	var menuToHideStyle = document.getElementById(divID).style;
	menuToHideStyle.visibility = "hidden";
}

function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}else if (obj.x){
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}else if (obj.y){
		curtop += obj.y;
	}
	return curtop;
}

function showMenu(id){
	if (id=="dinnermenu"){
		document.getElementById("winemenu").style.display='none';
		document.getElementById("dessertmenu").style.display='none';
		document.getElementById("dinnermenu").style.display='block';
	}else if (id=="winemenu"){
		document.getElementById("dinnermenu").style.display='none';
		document.getElementById("dessertmenu").style.display='none';
		document.getElementById("winemenu").style.display='block';
	}else if (id=="dessertmenu"){
		document.getElementById("dinnermenu").style.display='none';
		document.getElementById("winemenu").style.display='none';
		document.getElementById("dessertmenu").style.display='block';
	}
}

// Validation
function checkForm(){
	var val = false;
	if (document.getElementById('fname').value == ""){
		alert("Name is missing! Please fill in all the required information.");
		document.getElementById('fname').focus();
	}else if (!validateEmail(document.getElementById('email'))) {
		document.getElementById('email').focus();
		document.getElementById('email').select();
	}else	if(document.getElementById('btnReservations').checked){
		if(document.getElementById('res_date').value == ""){
			alert("Date is missing! Please fill in all the required information.");
			document.getElementById('res_date').focus();
		}else if (document.getElementById('res_time').value == "") {
			alert("Dining time is missing! Please fill in all the required information.");
			document.getElementById('res_time').focus();
		}else if (document.getElementById('partyno').value == "") {
			alert("We need to know the number in the party! Please fill in all the required information.");
			document.getElementById('res_time').focus();
		}else{
			val = true;
		}

		// Only check data format if everything else passed and date value is not null
		if(val && document.getElementById('res_date').value != ""){
			var dateElement = document.getElementById("res_date");

			if(validateDate(dateElement)){
				var date1= Date.parse(dateElement.value);
				var date2= new Date();

		//sab		dateDiff = (date1-date2)/(24*60*60*1000);

		//sab		if(dateDiff <= 1){
		//sab			val = false;
		                val = true
		//sab			alert("Please give Ristorante Renato at least 2 calendar days notice for reservations.");
		//sab			document.getElementById('res_date').focus();
		//sab			document.getElementById('res_date').select();
		//sab		}else{
		//sab			val = true;
		//sab		}
			}else{
				document.getElementById('res_date').focus();
				document.getElementById('res_date').select()
			}
		}
	}else{
		document.getElementById('res_date').value = "";
		document.getElementById('res_time').value = "";
		document.getElementById('partyno').value = "";
		val = true;
	}

	return val;
}

function validateDate(dateElement) {
	var myRegex = /^([0][0-9]|[1][0-2])\/([0-2][0-9]|[3][0-1])\/[0-9]{4}$/;
	var checkElement = dateElement.value;
	if (checkElement.match(myRegex)){
		var theDay = Math.round(dateElement.value.substr(0,2));
		var theMonth = Math.round(dateElement.value.substr(3,2));
		var theYear = Math.round(dateElement.value.substr(6,4));

		if ((theYear%4 == 0) && (theDay > 29) && (theMonth == 2)) {
			alert ("Not a valid date.");
			return false;
		}
		else if ((theYear%4 != 0) && (theDay > 28) && (theMonth == 2)) {
			alert ("Not a valid date.");
			return false;
		}
		else if ((theDay > 30) && (theMonth == 4 || theMonth == 6 || theMonth == 0 || theMonth == 11)) {
			alert ("Not a valid date.");
			return false;
		}
		else {
			return true;
		}
	}else{
		alert ("Date not in correct dd/mm/year format of the date specified does not exist.");
		return false;
	}
}

function validateEmail(emailElement){
	var myRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	var checkElement = emailElement.value;
	if(checkElement.match(myRegex)){
		return true;
	}else{
		alert("Please enter a valid e-mail address.");
		return false;
	}
}

function ShowHide(el, val){
	document.getElementById(el).style.display = val;
}

function ShowHideRes(val){
	document.getElementById("tr_res_date").style.display = val;
	document.getElementById("tr_res_time").style.display = val;
	document.getElementById("tr_partyno").style.display = val;

	document.getElementById("res_date").value = '';
	document.getElementById("res_time").value = '';
	document.getElementById("partyno").value = '';
}

/*
function openMenu2(event, divID) {
	var menuToShow = document.getElementById(divID);
	var isParent = false;
	var container = menuToShow;
	var containee = event.relatedTarget;

	while (containee != null){
		if ((isParent = container == containee))
			break;
		containee = containee.parentNode;
	}

	if(!isParent) {
		var noPx = document.childNodes ? 'px' : 0;
		var xMousePos = 0, yMousePos = 0;
		var menuToShowStyle;

		if (!event){
			var event = window.event;
		}

		if (document.layers){
				// Netscape...
				xMousePos = event.pageX;
				yMousePos = event.pageY;
		}else if (document.all){
				// Old IE...
				xMousePos = window.event.x + document.body.scrollLeft;
				yMousePos = window.event.y + document.body.scrollTop;
		}else if (document.getElementById){
				// DOM
				xMousePos = event.pageX;
				yMousePos = event.pageY;
		}

		if(menuToShow.style){
			menuToShowStyle = menuToShow.style;
		}

		if(!parseInt(menuToShow.left)){
			menuToShowStyle.left = xMousePos - findPosX(menuToShow) + noPx;
			menuToShowStyle.top = yMousePos - findPosY(menuToShow) + noPx;
			menuToShowStyle.visibility = "visible";
		}
	}
}*/
