//var ElementObject = new Object();
//ElementObject.ObjectCode
//ElementObject.RecordID


///////////////////////////////////////////////////////////////////////////////////////////////////////////
function ToggleElementActions(SourceControl, ElementObject, bShow){
	if(SourceControl.SelectedItems.length == 1){
		ToggleContextNavigationActions(ElementObject, bShow);
		//ToggleSubNavigationActions(ElementObject, bShow);
		EnableToolbarButtons(document.all.Calendar, ElementObject, bShow);
	}else{
		ToggleContextNavigationActions(ElementObject, false);
		EnableToolbarButtons(document.all.Calendar, ElementObject, false);
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function ToggleContextNavigationActions(ElementObject, bShow){	
	if(bShow){		
		var oCurrentContextMenu = eval("window."+ ElementObject.ContextMenuName);
		if(typeof(oCurrentContextMenu) != 'undefined'){
			UpdateContextActions(oCurrentContextMenu, ElementObject, bShow);
		}						
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function ToggleSubNavigationActions(ElementObject, bShow){

	//var oSelectedRow = oRow;
	//var nSelectedRowId = (typeof oSelectedRow.RowItemId == 'undefined') ? 0 : oSelectedRow.RowItemId;
	var nSelectedRowId = ElementObject.RecordID;
	var oRegularExpression = new RegExp("!1");
	var ContextActionsTable = document.getElementById(ElementObject.ObjectCode +"_ContextActionsTable");
	if(ContextActionsTable == null) return;
	//if(oSelectedRow == null) return;
	
	var ContextActionLinks = document.getElementById(ElementObject.ObjectCode +"_ContextActionItems");
	if(ContextActionLinks == null) return;
	var ContextActionsSeparator = document.getElementById(ElementObject.ObjectCode +"_ContextActionsSeparator");
		
	var i;
	for(i = 0; i < ContextActionsTable.m_arItems.length; i++){
		if(ContextActionsTable.m_arItems[i].RecordDependent == "1" && ContextActionsTable.m_arItems[i].Active == "1"){
			if(typeof ContextActionsTable.m_arItems[i].EnableConditionHandler != 'undefined' && ContextActionsTable.m_arItems[i].EnableConditionHandler != ""){
			//!!!
				bEnabled = eval(ContextActionsTable.m_arItems[i].EnableConditionHandler + "(ContextActionsTable.m_arItems[i], oSelectedRow, "+ bShow +");");
			}else{
				bEnabled = false;
			}
			if(bEnabled){
				ContextActionsTable.m_arItems[i].Disable(false);
				ContextActionsTable.m_arItems[i].TemplateRedirectUrl = ContextActionsTable.m_arItems[i].RedirectUrl;
				ContextActionsTable.m_arItems[i].RedirectUrl = ContextActionsTable.m_arItems[i].RedirectUrl.replace(oRegularExpression, nSelectedRowId);
			}else{
				ContextActionsTable.m_arItems[i].Disable(true);
				if(ContextActionsTable.m_arItems[i].TemplateRedirectUrl) ContextActionsTable.m_arItems[i].RedirectUrl = ContextActionsTable.m_arItems[i].TemplateRedirectUrl;
			}
		}
	}
	if(bShow == true && typeof(window.m_arSelectedRows) != "undefined" && window.m_arSelectedRows.length > 0){
		if(typeof(window.oLeftMenuContextActions) != 'undefined'){
			/// ?!!?!?!?!?!?!??!?!?!? need fix, vzima 2ra kolona po default !!!?!?!??!?!!!!!!!!!!!!!!
			window.oLeftMenuContextActions.m_oTitle.innerText = (oSelectedRow.cells[1].firstChild == null || typeof(oSelectedRow.cells[1].firstChild) == 'undefined') ? '' : oSelectedRow.cells[1].firstChild.innerText;
			UpdateContextActions(window.oLeftMenuContextActions, ElementObject);
		}

		if(ContextActionsSeparator == null){
			ContextActionsTable.nextSibling.id = oSelectedRow.ApplicationObjectCode +"_ContextActionsSeparator";
			ContextActionsSeparator = document.getElementById(oSelectedRow.ApplicationObjectCode +"_ContextActionsSeparator");
		}
		ContextActionsTable.style.display = '';
		if(ContextActionsSeparator != null) ContextActionsSeparator.style.display = '';

	}else{
		if(ContextActionsTable != null) ContextActionsTable.style.display = 'none';
		if(ContextActionsSeparator != null) ContextActionsSeparator.style.display = 'none';
	}
}
///////////////////////////////////////////////////////////////////////////////////
function EnableToolbarButtons(Control, ElementObject, bShow){
	var oRegularExpression = new RegExp("!1");
	var i, j;
	if(Control == null || typeof(Control.m_arToolBars) == 'undefined') return;	
	for(i = 0; i < Control.m_arToolBars.length; i++){
		var ToolBar = Control.m_arToolBars[i];
		if(!ToolBar.m_arItems) continue;
		for(j = 0; j < ToolBar.m_arItems.length; j++){
			var ToolbarButton = ToolBar.m_arItems[j];
			var bValidConditionHandler = false;
			if(ToolbarButton != undefined && ToolbarButton.RecordDependent == "1" && ToolbarButton.Active == "1"){
				if(typeof ToolbarButton.EnableConditionHandler != 'undefined' && ToolbarButton.EnableConditionHandler != ""){
					bEnabled = eval(ToolbarButton.EnableConditionHandler + "(this, ElementObject, "+ bShow +");");
				}else{
					bEnabled = false;
				}
				if(!bEnabled){
					ToolbarButton.Enabled = false;
					if(ToolbarButton.TemplateRedirectUrl) ToolbarButton.RedirectUrl = ToolbarButton.TemplateRedirectUrl;
				}else{
					ToolbarButton.Enabled = true;
					if(typeof(ToolbarButton.RedirectUrl) != 'undefined'){
						ToolbarButton.TemplateRedirectUrl = ToolbarButton.RedirectUrl;
						ToolbarButton.RedirectUrl = ToolbarButton.RedirectUrl.replace(oRegularExpression, ElementObject.RecordID);
					}
				}
			}
		}
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function UpdateContextActions(oMenu, ElementObject, bShow){
	//alert("update");
	var oRegularExpression = new RegExp("!1");
	for(i = 0; i < oMenu.m_arItems.length; i++){
		var oItem = oMenu.m_arItems[i];
		if(typeof oItem.EnableConditionHandler != 'undefined' && oItem.EnableConditionHandler != ""){
			bEnabled = eval(oItem.EnableConditionHandler + "(oItem, ElementObject, bShow);");
		}else{
			bEnabled = false;
		}
		if(typeof(oItem.Enable) != "undefined"){
			if(bEnabled){
				oItem.Enable(true);
			}else{
				oItem.Enable(false);
			}
		}		
		if(typeof(oItem.RedirectUrl) != 'undefined'){	
			if(typeof(oItem.OriginalRedirectUrl) == 'undefined') oItem.OriginalRedirectUrl = oItem.RedirectUrl;
			oItem.RedirectUrl = oItem.OriginalRedirectUrl.replace(oRegularExpression, ElementObject.RecordID);
			nSaveCounter = 0;
			while(nSaveCounter < 10 && oItem.RedirectUrl.match(oRegularExpression)){
				oItem.RedirectUrl = oItem.RedirectUrl.replace(oRegularExpression, ElementObject.RecordID);
				nSaveCounter++;
			}
		}
	}
}
