var menuForm;
var selectedItemClass;
var selectedItem;
var menuDisabled;
var newSearch;

var displayItemObject;

function disableMenu() {
	menuDisabled = true;
}

function enableMenu() {
	menuDisabled = false;
}

function handleSearchOnClick(commandCode) {
	if(!menuDisabled || newSearch) {
		var bookingNumber = document.getElementById('bookingNumber');
		var confirmationNumber = document.getElementById('confirmationNumber');
		if(confirmationNumber) {//retail customer: must have booking & confirmation numbers
			if(isEmpty(bookingNumber.value) || isEmpty(confirmationNumber.value) || !isBookingNumber(bookingNumber.value) || !isConfirmation(confirmationNumber.value)) {
				if(isEmpty(bookingNumber.value) || isEmpty(confirmationNumber.value)) {
					alert('Please enter both Booking and Confirmation numbers.');
				} else {
					if (!isBookingNumber(bookingNumber.value)) {
						alert('Please enter a valid Booking number (100000-999999).');
					} else {
						if (!isConfirmation(confirmationNumber.value)) {
							alert('Please enter a valid Confirmation number (alphanumeric, 7 digits).');
						}
					}
				}
				disableMenu();
				newSearch = true;
				return false;
			}
		} else {//agent: must have booking number
			if(isEmpty(bookingNumber.value) || !isBookingNumber(bookingNumber.value)) {
				if(isEmpty(bookingNumber.value)) {
					alert('Please enter a Booking number.');
				} else {
					if (!isBookingNumber(bookingNumber.value)) {
						alert('Please enter a valid Booking number (100000-999999).');
					}
				}
				disableMenu();
				newSearch = true;
				return false;
			}
		}
		window.parent.topFrame.disableSubmitters();
		menuForm.command.value=commandCode;
		menuForm.action = 'booking';
		menuForm.target = '_self';
		menuForm.submit();
	}
}

function initializeMenu(activeElementIds, itemSelectClass, itemSelected) {
	menuForm = document.menuForm;
	enableMenu();
	selectedItem = document.getElementById(itemSelected);
	selectedItemClass = itemSelectClass;
	
	if(selectedItem && selectedItemClass) {
		appendStyleClass(selectedItem, selectedItemClass);
	}

	for(var elementIndex=0; elementIndex<activeElementIds.length; ++elementIndex) {
		var element = document.getElementById(activeElementIds[elementIndex]);
		if(element) {
			element.style.display='';
		}
	}
}

function handleItemOnClick(itemChild, commandCode) {
	var item = itemChild.parentNode;
	if(item && selectedItem && item.id==selectedItem.id) {
		return;
	} else if(!menuDisabled) {
		window.parent.topFrame.disableSubmitters();
    
    if( menuForm.show_commission ) {
      var showCommission = false;  
		  if( element ) {
		    showCommission = ! (element.style.display == 'none');
		  }
    
      menuForm.show_commission = showCommission;
    }
    
		menuForm.command.value=commandCode;
		menuForm.action = 'booking';
		menuForm.target = '_self';
		menuForm.submit();
	} else {
		return;
	}
}

function openPaymentWindow() {
	window.open("", "paymentWindow","height=900,width=800,status=no,titlebar=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
}

function refreshItinDisplay() {
	// alert('Return from modal\nDisplay Command = ' + displayCommand);
	handleItemOnClick(displayItemObject.itemChild, displayItemObject.commandCode);
}

function openModalPaymentWindow(itemChild, displayCommandCode) {
	displayItemObject = new Object();
	displayItemObject.itemChild = itemChild;
	displayItemObject.commandCode = displayCommandCode;
	
	openSimDialog("", 800, 1000, refreshItinDisplay);
}

function handlePaymentOnClick(itemChild, commandCode, displayCommandCode) {
	if(!menuDisabled) {
		//openModalPaymentWindow(itemChild, displayCommandCode);
		
		if(menuForm.action.indexOf('.payment')<0) {
			menuForm.action = 'book.payment?secure=true&command='+commandCode;
		}
		// menuForm.target = 'paymentWindow';
		//menuForm.target = dialogWin.name;
		menuForm.command.value = commandCode;
		window.parent.location = menuForm.action;
		//menuForm.submit();
	}
}

function handlePrintOnClick(commandCode) {
	 if(!menuDisabled) handlePrintCommand(commandCode);
}

function handleVoucherPrintOnClick(bookingNumber) {
	 if(!menuDisabled) handleVoucherPrintCommand(bookingNumber);
	 window.parent.topFrame.disableSubmitters();
}

function handleOnMouseOver(elementChild, className) {
	var element = elementChild.parentNode;
	if(element && selectedItem && element.id==selectedItem.id) {
	} else if(!menuDisabled) {
		appendStyleClass(element, className);
		removeStyleClass(element, 'unclickable');
		appendStyleClass(element, 'clickable');
	}
}

function handleOnMouseOut(elementChild, className) {
	var element = elementChild.parentNode;
	if(element && selectedItem && element.id==selectedItem.id) {
	} else if(!menuDisabled) {
		removeStyleClass(element, className);
		removeStyleClass(element, 'clickable');
		appendStyleClass(element, 'unclickable');
	}
}

function handleSearchOnFocus(element) {
  var startForm = document.startForm;
  if(!menuDisabled) {
    clearPage();
    if(!startForm) {
      disableMenu();
    }
    newSearch = true;
  }
}

