/*
* MAmenu.js
* by Mário Araújo, version 1.0
* Copyright (c) Declarativa. All Rights Reserved.
* http://www.declarativa.com
*/
function MAmenu(initializeFunction, sResourcesURL){
	var menu = new MAsubMenu();
	menu.resourcesURL = sResourcesURL;
	menu.main = menu;
	menu.currentItem = null;
	menu.opened = false;
	menu.mouseIsOn = false;
	menu.isMain = true;
	menu._initialized = false;
	menu._initializeMenu = typeof initializeFunction == "function" ? initializeFunction : function(){};

	menu.open = _MAmenu_open;
	menu.close = _MAmenu_close;
	menu._initialize = _MAmenu__initialize;
	menu.destroy = _MAmenu_destroy;
	
	// --- style properties	
	menu.itemColor = "#000000";
	//menu.color = MAmenu.browser.isSafari ? "#fff" : "transparent";
	menu.color = "white";
	menu.overItemColor = "#fff";
	menu.overColor = "#69c";
	menu.width = MAmenu.browser.isMoz4 ? "133px" : "120px";
	menu.borderWidth = "1px";
	menu.borderColor = "#aaa";
	menu.borderStyle = "solid";
	menu.menuBorderColor = "#aaa";
	menu.menuOverBorderColor = "#4C4C4C";
	menu.scrollColor = "#C4C4C4";

	// --- caching images	
	menu.images = { right: new Image(), top: new Image(), bottom: new Image() };
	menu.images.right.src = sResourcesURL+"MAmenu-more-right.gif";
	menu.images.top.src = sResourcesURL+"MAmenu-more-top.gif";
	menu.images.bottom.src = sResourcesURL+"MAmenu-more-bottom.gif";

	if (typeof MAmenus == "undefined") MAmenus = [];
	MAmenus[MAmenus.length] = menu;

	return menu;
}

MAmenu.SCROLL_TIMER = null;
MAmenu.SCROLL_SPEED = 35;
MAmenu.browser = { isMoz4:window.navigator.appVersion.search(/^4\.0.*/)!=-1, isSafari:window.navigator.appVersion.search(/safari/i)!=-1 };
MAmenu._count = 0;

function _MAmenu__initialize(){
	if (!this._initialized){
		this._initializeMenu()
		this._initialized = true;
	}
}

function _MAmenu_open(e){
	this._initialize();
	this.opened = true;
	this.left = e.clientX+window.document.body.scrollLeft;
	this.top = e.clientY+window.document.body.scrollTop;
	this.show();
	return false;
}

function _MAmenu_close(){
	if (this.opened){
		this.hide();
		this.opened = false;
	}
}

function _MAmenu_destroy(){
	window.document.body.removeChild(this.div, true);
	this.div = null;
	this.items = [];
}

/*** MAsubMenu ***/
function MAsubMenu(){
	this.main = null;
	this.parent = null;
	this.parentMenu = null;
	this.isMain = false;
	this.items = [];
	this.div = null;
	this.divScrollTop = null;
	this.divScrollBottom = null;
	
	this.currentItem = null;
	this.currentSubMenu = null;

	this._code = MAmenu._count++;
	this.id = "__MAmenu__"+(this._code);
	eval(this.id+"=this");
	
	this.getMain = _MAsubMenu_getMain;
	this.add = _MAsubMenu_add;
	this.show = _MAsubMenu_show;
	this.hide = _MAsubMenu_hide;
	this.crop = _MAsubMenu_crop;
	this.showScrollButtons = _MAsubMenu_showScrollButtons;
	this.scroll = _MAsubMenu_scroll;
	this.scrollUp = _MAsubMenu_scrollUp;
	this.scrollDown = _MAsubMenu_scrollDown;
	this.stopScroll = _MAsubMenu_stopScroll;
	this.build = _MAsubMenu_build;
	this.load = _MAsubMenu_load;
}

function _MAsubMenu_getMain(){
	var menu = this.parent.parent;
	while (menu.main != menu) menu = menu.parent;
	return menu;
}

function _MAsubMenu_add(item){
	item.parent = this;
	if (item.subMenu != null){
		item.subMenu.parent = item;
		item.subMenu.parentMenu = this;
	}
	this.items[this.items.length] = item;
	return item;
}

function _MAsubMenu_show(){
	var ch, cw, st, sl; var topPosition, leftPosition, newLeft; var dh, dw;
	if (this.div == null){
		if (this.main == null) this.main = this.getMain();
		if (this.items.length == 0){
			this.load();
			return true;
		} else {
			this.build();
		}
	}
	st = window.document.body.scrollTop; sl = window.document.body.scrollLeft;
	if (this.isMain){
		topPosition = this.top;
		leftPosition = this.left;
	} else {
		topPosition = _parseInt(this.parentMenu.div.style.top)+_parseInt(this.parent.div.offsetTop)+5;
		leftPosition = _parseInt(this.parentMenu.div.style.left)+_parseInt(this.parent.div.offsetWidth)-5;
		// --- menu was loaded but my parent is not the currentItem
		if (this.parentMenu.currentItem != this.parent) return false;
	}
	this.div.style.height = this.originalHeight+"px";
	if (window.document.body.clientHeight == window.document.body.offsetHeight){
		//ch = window.innerHeight; cw = window.innerWidth+sl;
		ch = window.document.body.clientHeight; cw = window.document.body.clientWidth+sl;
	} else {
		ch = window.document.body.clientHeight; cw = window.document.body.clientWidth+sl;
	}
	dh = _parseInt(this.div.style.height)+3; dw = _parseInt(this.div.style.width);

	if (dh > ch){
		topPosition = st;
	} else if (topPosition - st + dh > ch){
		topPosition = ch-dh+st;
		if (topPosition < 0) topPosition = 0;
	}
	if (dw < cw){
		if (leftPosition + dw > cw){
			if (this.parent != null){
				newLeft = leftPosition-2*_parseInt(this.div.style.width)+(MAmenu.browser.isMoz4 ? 4 : 2)*3;
			} else {
				newLeft = cw-_parseInt(this.div.style.width);
			}
			if (newLeft >= 0) leftPosition = newLeft;
		}
	}
	this.div.style.top = topPosition+"px";
	this.div.style.left = leftPosition+"px";
	if (dh > ch){
		this.crop(ch-(MAmenu.browser.isMoz4 ? 0 : 3));
		this.showScrollButtons(null, true);
	}
	this.div.style.display = "block";
	this.div.style.visibility = "visible";
	if (!this.isMain) this.parentMenu.currentSubMenu = this;
	window.document.body.style.cursor = "";
}

function _MAsubMenu_hide(){
	var firstTop; var i, n;
	if (this.currentSubMenu != null)
		this.currentSubMenu.hide();
	if (this.currentItem != null)
		this.currentItem.setDecoration("normal");
	this.div.style.visibility = "hidden";
	this.div.style.display = "none";
	if (this.parent != null)
		this.parentMenu.currentSubMenu = null;
	this.currentItem = null;
	this.showScrollButtons(false, false);
	firstTop = _parseInt(this.items[0].div.style.top)*-1;
	if (firstTop > 0){
		n = this.items.length;
		for (i = 0; i < n; i++){
			this.items[i].div.style.top = _parseInt(this.items[i].div.style.top)+firstTop;
		}
	}
}

function _MAsubMenu_crop(height){
	this.div.style.height = height+"px";
}

function _MAsubMenu_showScrollButtons(top, bottom){
	var main = this.main;
	if (top != null){
		if (top == true){
			if (this.divScrollTop == null){
				var div = _createDiv(); div.style.top = "0px";
				div.onmouseover = _MAsubMenu_onmouseover_scrollUp;
				div.onmouseout = _MAsubMenu_onmouseout_stopScroll;
				this.div.appendChild(div);
				var img = _creteImg(main.images.top.src);
				div.appendChild(img);
				this.divScrollTop = div;
			}
			this.divScrollTop.style.display = "block";
			this.divScrollTop.style.visibility = "visible";
		} else if (this.divScrollTop != null){
			this.divScrollTop.style.visibility = "hidden";
			this.divScrollTop.style.display = "none";
		}
	}
	if (bottom != null){
		if (bottom == true){
			if (this.divScrollBottom == null){
				var div = _createDiv();
				div.onmouseover = _MAsubMenu_onmouseover_scrollDown;
				div.onmouseout = _MAsubMenu_onmouseout_stopScroll;
				this.div.appendChild(div);
				var img = _creteImg(main.images.bottom.src);
				div.appendChild(img);
				this.divScrollBottom = div;
			}
			this.divScrollBottom.style.top = (_parseInt(this.div.style.height)-(MAmenu.browser.isMoz4 ? 12 : 9))+"px";
			this.divScrollBottom.style.display = "block";
			this.divScrollBottom.style.visibility = "visible";
		} else if (this.divScrollBottom != null){
			this.divScrollBottom.style.visibility = "hidden";
			this.divScrollBottom.style.display = "none";
		}
	}
	
	function _createDiv(){
		var div = window.document.createElement("DIV");
		div.style.fontSize = "0px";
		div.style.position = "absolute";
		div.style.left = "0px";
		div.style.width = "100%";
		div.style.height = "10px";
		div.style.backgroundColor = main.scrollColor;
		div.style.visibility = "hidden";
		return div;
	}
	
	function _creteImg(image){
		var img = window.document.createElement("IMG");
		img.src = image;
		img.style.position = "absolute";
		img.style.left = "50%"; img.style.top = "2px";
		img.style.marginLeft = "-7px";
		return img;
	}
}

function _MAsubMenu_scrollUp(){
	var delta = 1
	var i; var buttonTop, buttonBottom; var newPos;
	var n = this.items.length;
	var inc = 6*delta;
	var firstItemTop = _parseInt(this.items[0].div.offsetTop);
	var dif = firstItemTop+inc;
	if (dif > 0){
		inc = firstItemTop*-1-1;
		this.stopScroll();
	}
	for (i = 0; i < n; i++){
		newPos = _parseInt(this.items[i].div.style.top)+inc;
		this.items[i].div.style.top = newPos+"px";
	}
	if (dif > 0){
		buttonTop = false;
	} else {
		if (this.divScrollTop != null && this.divScrollTop.style.visibility == "visible"){
			buttonTop = null;
		} else {
			buttonTop = true;
		}
	}
	this.showScrollButtons(buttonTop, true);
}

function _MAsubMenu_scrollDown(){
	var delta = -1
	var i; var buttonTop, buttonBottom; var newPos;
	var n = this.items.length;
	var inc = 6*delta;
	var menuHeight = _parseInt(this.div.style.height);
	var lastItemTop = _parseInt(this.items[n-1].div.offsetTop);
	var lastItemHeight = _parseInt(this.items[n-1].div.offsetHeight);
	var lastItemBottom = lastItemTop+lastItemHeight;
	var dif = menuHeight-(lastItemBottom+inc);
	if (dif > 0){
		inc -= lastItemBottom+inc-menuHeight;
		this.stopScroll();
	}
	for (i = 0; i < n; i++){
		newPos = _parseInt(this.items[i].div.style.top)+inc;
		this.items[i].div.style.top = newPos+"px";
	}
	if (dif > 0){
		buttonBottom = false;
	} else {
		if (this.divScrollBottom != null && this.divScrollBottom.style.visibility == "visible"){
			buttonBottom = null;
		} else {
			buttonBottom = true;
		}
	}
	this.showScrollButtons(true, buttonBottom);
}

function _MAsubMenu_scroll(delta){
	var i; var buttonTop, buttonBottom; var newPos;
	var n = this.items.length;
	var inc = 6*delta;
	var menuHeight = _parseInt(this.div.style.height)+2;
	var lastItemTop = _parseInt(this.items[n-1].div.style.top);
	var lastItemHeight = _parseInt(this.items[n-1].div.style.height);
	var lastItemBottom = lastItemTop+lastItemHeight;
	var dif = menuHeight-(lastItemBottom+inc);
	if (dif > 0){
		inc = dif*delta;
		this.stopScroll();
	}
	for (i = 0; i < n; i++){
		newPos = _parseInt(this.items[i].div.style.top)+inc;
		this.items[i].div.style.top = newPos+"px";
	}
	if (dif > 0){
		buttonBottom = false;
	} else {
		if (this.divScrollBottom != null && this.divScrollBottom.style.visibility == "visible"){
			buttonBottom = null;
		} else {
			buttonBottom = true;
		}
	}
	this.showScrollButtons(true, buttonBottom);
}

function _MAsubMenu_build(){
	var i; var div; var text; var a;
	// --- creating items container
	this.div = window.document.createElement("DIV");
	window.document.body.appendChild(this.div);
	this.div.menu = this;
	this.div.style.paddingTop = this.main.borderWidth;
	this.div.style.backgroundColor = this.main.color;
	this.div.style.border = "1px solid "+this.main.menuBorderColor;
	this.div.style.position = "absolute";
	this.div.style.visibility = "hidden";
	this.div.style.overflow = "hidden";
	this.div.onmouseover = _MAsubMenu_onmouseover;
	this.div.onmouseout = _MAsubMenu_onmouseout;
	// --- create each item
	for (i = 0; i < this.items.length; i++) this.items[i].build();
	this.div.style.width = (_getElWidth(this.div) - 2)+"px";
	this.div.style.height = (_getElHeight(this.div) + 12)+"px";
	this.originalHeight = _parseInt(this.div.style.height);
	// --- opacity
	div = window.document.createElement("DIV");
	this.div.appendChild(div);
	div.style.backgroundColor = "#fff";
	div.style.position = "absolute";
	div.style.left = div.style.top = "0px";
	div.style.width = div.style.height = "100%";
	div.style.filter = "alpha(opacity=90)"; div.style.opacity = 0.90;
	div.style.zIndex = -1;
	
	var newDiv = window.document.createElement("div");
	newDiv.align = "right";
	newDiv.style.marginRight = "2px";
	newDiv.style.marginTop = "2px";
	newDiv.style.marginBottom = "2px";
	var closeImg = window.document.createElement("img");
	closeImg.src = "publico/includes/MAmenu/MAmenu-more-close.png";
	closeImg.onclick = function (){ this.parentNode.parentNode.menu.close(); };
	newDiv.appendChild(closeImg);
	this.div.appendChild(newDiv);
}

function _MAsubMenu_load(){
	var sRequest; var formFields, formValues; var sForm;
	var menu = this;

	if (this.main.WAM.source == "WAMAdmin"){
		formFields = ["txtMenuType", "txtUser", "txtListSchema", "txtListName", "txtEditTableSchema", "txtEditTableName", "txtSchema", "txtTable", "txtJoin", "txtMenuItemID"];
		formValues = [this.main.WAM.source+this.main.WAM.context, this.main.WAM.user, this.main.WAM.context == "LIST" ? WAM.list.schema : WAM.row.schema, this.main.WAM.context == "LIST" ? WAM.list.wamList.substr(WAM.list.schema.length+1) : WAM.row.table, '', '', this.parent.WAM.tableSchema, this.parent.WAM.tableName, this.__getJoin(), this.id];
	} else {
		formFields = ["txtMenuType", "txtUser", "txtListSchema", "txtListName", "txtEditTableSchema", "txtEditTableName", "txtSchema", "txtTable", "txtJoin", "txtMenuItemID"];
		formValues = [this.main.WAM.source, this.main.WAM.user, this.main.WAM.listSchema, this.main.WAM.listName, this.main.WAM.editTableSchema, this.main.WAM.editTableName, this.parent.WAM.tableSchema, this.parent.WAM.tableName, this.__getJoin(), this.id];
	}

	sForm = "<form name='MAloadForm' action='"+this.main.WAM.url+"' method='post'>";
	for (var i = 0; i < formFields.length; i++) sForm += "<input type='text' name='"+formFields[i]+"' value='"+formValues[i]+"'>";
	sForm += "</form>";
	
	var oASPExec = new ASPExecute();
	//oASPExec.setDebug(true);
	oASPExec.setForm(sForm, "MAloadForm");
	window.document.body.style.cursor = "wait";
	oASPExec.execute();
}

function _MAsubMenu_onmouseover_scrollUp(){
	MAmenu.SCROLL_TIMER = window.setInterval("eval('"+this.parentNode.menu.id+".scrollUp()')", MAmenu.SCROLL_SPEED);
}

function _MAsubMenu_onmouseover_scrollDown(){
	MAmenu.SCROLL_TIMER = window.setInterval("eval('"+this.parentNode.menu.id+".scrollDown()')", MAmenu.SCROLL_SPEED);
}

function _MAsubMenu_onmouseout_stopScroll(){
	window.clearInterval(MAmenu.SCROLL_TIMER);
}

function _MAsubMenu_stopScroll(){
	window.clearInterval(MAmenu.SCROLL_TIMER);
}

function _MAsubMenu_onmouseover(){
	this.menu.main.mouseIsOn = true;
	this.style.borderColor = this.menu.main.menuOverBorderColor;
}

function _MAsubMenu_onmouseout(){
	this.menu.main.mouseIsOn = false;
	this.style.borderColor = this.menu.main.menuBorderColor;
}

/*** MAmenuItem ***/
function MAmenuItem(caption, title, action, subMenu){
	this.parent = null;
	this.caption = caption;
	this.title = typeof title == "undefined" ? null : title;
	this.action = typeof action == "undefined" ? null : action;
	this.subMenu = typeof subMenu == "undefined" ? null : subMenu;
	this.div = null;

	this.build = _MAmenuItem_build;
	this.setDecoration = _MAmenuItem_setDecoration;
}

function _MAmenuItem_build(){
	var img; var div;
	var main = this.parent.main;

	// --- creating item container
	this.div = window.document.createElement("DIV");
	this.div.menuItem = this;
	this.div.style.position = "relative";
	this.div.style.marginTop = "-"+main.borderWidth;
	this.div.style.width = main.width;
	this.div.style.borderWidth = main.borderWidth;
	this.div.style.borderColor = main.borderColor;
	this.div.style.borderStyle = main.borderStyle;
	this.div.style.backgroundColor = main.color;
	this.div.style.padding = "3px";
	this.div.style.paddingRight = "10px";
	this.div.style.fontFamily = "arial";
	this.div.style.fontSize = "12px";
	this.div.style.color = main.itemColor;
	this.div.style.cursor = "pointer";
	this.div.unselectable = true;
	this.div.onclick = _MAmenuItem_onclick;
	this.div.onmouseover = _MAmenuItem_onmouseover;
	this.div.onmouseout = _MAmenuItem_onmouseout;
	// --- it has submenu?
	if (this.subMenu != null){
		div = window.document.createElement("DIV");
		this.div.appendChild(div);
		div.style.position = "relative";
		div.style.cssFloat = "right"; div.style.styleFloat = "right";
		div.style.marginRight = MAmenu.browser.isMoz4 ? "-3px" : "-5px";
		div.style.top = "50%";
		div.style.marginTop = MAmenu.browser.isMoz4 ? "-8px" : "-6px";
		img = window.document.createElement("IMG");
		div.appendChild(img);
		img.src = main.images.right.src;
	}
	this.div.appendChild(window.document.createTextNode(this.caption));
	if (this.title != null && this.title != "") this.div.setAttribute("title", this.title);
	// --- add to the items container
	this.parent.div.appendChild(this.div);
	this.div.style.height = _getElHeight(this.div)+"px";
}

function _MAmenuItem_setDecoration(sType){
	var style = this.div.style; var main = this.parent.main;
	switch(sType){
		case "highlight":
			style.backgroundColor = main.overColor;
			style.color = main.overItemColor;
			break;
		case "normal":
			style.backgroundColor = main.color;
			style.color = main.itemColor;
			break;
	}
}

function _MAmenuItem_onclick(){
	var menuItem = this.menuItem;
	var script = /^javascript/i;
	if (menuItem.action != null && menuItem.action != ""){
		if (script.test(menuItem.action)) eval(menuItem.action)
		else window.document.location.href = menuItem.action;
	}
	event.cancelBubble = true;
	return false;
}

function _MAmenuItem_onmouseover(){
	var menuItem = this.menuItem;
	if (menuItem.parent.currentItem != menuItem){
		if (menuItem.parent.currentItem != null)
			 menuItem.parent.currentItem.setDecoration("normal");
		menuItem.parent.currentItem = menuItem;
		menuItem.setDecoration("highlight");
		if (menuItem.parent.currentSubMenu != null){
			menuItem.parent.currentSubMenu.hide();
		}
		if (menuItem.subMenu != null){
			menuItem.subMenu.show();
		}
	}
}

function _MAmenuItem_onmouseout(){
}

function MAmenuCloseAll(){
	if (typeof MAmenus != "undefined") for (var menu in MAmenus) if (!MAmenus[menu].mouseIsOn) MAmenus[menu].close();
}

function _getElHeight(el){
	if (MAmenu.browser.isMoz4) return el.offsetHeight;
	else return el.offsetHeight-_parseInt(el.style.borderTopWidth)-_parseInt(el.style.borderBottomWidth)-_parseInt(el.style.paddingTop)-_parseInt(el.style.paddingBottom);
}

function _getElWidth(el){
	if (MAmenu.browser.isMoz4) return el.offsetWidth;
	else return el.offsetWidth-_parseInt(el.style.borderLeftWidth)-_parseInt(el.style.borderRightWidth)-_parseInt(el.style.paddingRight)-_parseInt(el.style.paddingLeft);
}

function _parseInt(val){
	if (val == "") return 0;
	return parseInt(val);
}

/*** CUSTOM FUNCTION ***/
MAmenu.itemClick = function (el){
		var menuItem = el.menuItem; var form;
		var column = {};
		// --- Add this column to the WAMList
		if (menuItem.parent.isMain){
			if (menuItem.parent.WAM.source == "WAMListAggregates"){
				// --- Aggregate menu
				menuItem.parent.WAM.form.__Action.value = "aggregateFunction";
				menuItem.parent.WAM.form.txtValue.value = menuItem.WAM.aggregateFunctionName;
				menuItem.parent.main.close();
				WAM.list.refresh();
				return true;
			}
			// --- Column belongs to the list's base table; 1st level
			column.tableSchema = menuItem.parent.main.WAM.editTableSchema;
			column.tableName = menuItem.parent.main.WAM.editTableName;
			column.name = menuItem.WAM.columnName;
			column.join = "";
		} else {
			column.tableSchema = menuItem.parent.WAM.tableSchema;
			column.tableName = menuItem.parent.WAM.tableName;
			column.name = menuItem.WAM.columnName;
			if (menuItem.parent.parentMenu.isMain || menuItem.parent.parentMenu.WAM.isGroup && menuItem.parent.parentMenu.parentMenu.isMain){
				// --- 2nd level join table
				if (menuItem.parent.main.WAM.source == "WAMAdmin" && menuItem.parent.main.WAM.context != "LIST"){
				} else {
					column.join = menuItem.parent.__getJoin();
				}
			} else {
				// --- >2nd level join table
				column.join = menuItem.parent.WAM.join;
			}
		}
		
		if (menuItem.parent.main.WAM.source == "WAMAdmin"){
			// --- WAMAdmin ...
			if (menuItem.parent.main.WAM.context == "ROWDL"){
				// --- ... adding new standalone detail list
				window.document.____WAMmodelAdmin.____admin_constraint.value = "";
				window.document.____WAMmodelAdmin.____admin_column.value = "";
				window.document.____WAMmodelAdmin.____admin_requiredJoin.value = menuItem.WAM.join;
				____WAMAdmin_AddLkColumnOrDetailList("ADDDTLIST");
			} else {
				// --- ...  adding new (row or standalone detail list) lookup column
				if (!menuItem.parent.isMain){
					var x = (menuItem.parent.WAM.join+",").indexOf(",");
					column.constraint = menuItem.parent.WAM.join.substr(0, x);
				} else column.constraint = "";
				window.document.____WAMmodelAdmin.____admin_constraint.value = column.constraint;
				window.document.____WAMmodelAdmin.____admin_column.value = column.name;
				window.document.____WAMmodelAdmin.____admin_requiredJoin.value = column.join;
				____WAMAdmin_AddLkColumnOrDetailList("ADDLKCOL");
			}
		} else {
			with (menuItem.parent.main.WAM.form){
				txtSchema.value = column.tableSchema;
				txtTable.value = column.tableName;
				txtColumn.value = column.name;
				txtRequiredJoin.value = column.join;
				switch (menuItem.parent.main.WAM.source){
					case "WAMList": __Action.value = "addColumn"; WAM.list.refresh(); break;
					case "WAMCriterion": wcAction.value = "addColumn"; WAM.criterion.refresh(); break;
				}
			}
		}
		menuItem.parent.main.close();
	};

MAsubMenu.prototype.__hasTwoOrMoreJoinsToSameMasterTable = function (){
		var nJoins = 0;
		for (var i = 0; i < this.parentMenu.items.length && nJoins < 2; i++){
			if (this.parentMenu.items[i].subMenu != null && typeof this.parentMenu.items[i].WAM != "undefined" &&
				this.parentMenu.items[i].WAM.tableSchema == this.parent.WAM.tableSchema && 
				this.parentMenu.items[i].WAM.tableName == this.parent.WAM.tableName) nJoins++;
		}
		return nJoins > 1;
	};

MAsubMenu.prototype.__getJoin = function (){
		var parentTableSchema; var parentTableName;
		if (this.__hasTwoOrMoreJoinsToSameMasterTable()) return this.parent.WAM.join;
		if (this.parent.WAM.relationshipType == "DETAIL") return this.parent.WAM.join;
		if (this.main.WAM.source == "WAMAdmin") return this.parent.WAM.join;
		// --- Is it a recursive join?
		if (this.parentMenu.isMain == true || this.parentMenu.WAM.isGroup == true){
			parentTableSchema = this.main.WAM.editTableSchema;
			parentTableName = this.main.WAM.editTableName;
		} else {
			parentTableSchema = this.parentMenu.parent.WAM.tableSchema;
			parentTableName = this.parentMenu.parent.WAM.tableName;
		}
		if (parentTableSchema == this.parent.WAM.tableSchema && parentTableName == this.parent.WAM.tableName) return this.parent.WAM.join;
		return "";
	};

MAmenu.pathClick = function(el){
		el.menuItem.myWindow.focus();
		el.menuItem.parent.main.close();
	};