/*
<WebFramework>
	<FilePath>/Config/Topic/Scripts/TopicConfig.js</FilePath>
	<Description>This script file handles the configuration of Topics in the WebFramework.</Description>
	<Version Major="5.2" Minor="0" Revision="2" Date="1/3/2007">
		<Revision Date="8/17/2004" Name="Sean Pardue" Major="5" Minor="0" Revision="0">Creation</Revision>
		<Revision Date="9/20/2004" Name="Sean Pardue" Major="5" Minor="0" Revision="1">
			Changed right click functionality to not select and run an item, but to just highlight the item with dashed border.
		</Revision>
		<Revision Date="1/11/2005" Major="5.0" Minor="0" Revision="1">Initial Release</Revision>
		<Revision Date="2/4/2005" Major="5.0" Minor="1" Revision="1" Name="Version Utility">
			Fixed Asset Configuration for setting image path
			Fixed resizing on Help dialog
			Fixed links in Reporting Services documentation
		</Revision>
		<Revision Date="2/14/2005" Major="5.0" Minor="2" Revision="1" Name="Version Utility">
			Fixed problem with deleting Assets
		</Revision>
		<Revision Date="5/24/2005" Major="5.0" Minor="3" Revision="1" Name="Version Utility">Major and Minor revisions automatically set by utility.</Revision>
		<Revision Date="8/4/2005" Major="5.1" Minor="0" Revision="1" Name="Version Utility">Major and Minor revisions automatically set by utility.</Revision>
		<Revision Date="1/4/2006" Major="5.1" Minor="0" Revision="2" Name="Sean Pardue">
			Added Audit Logging
		</Revision>
		<Revision Date="1/3/2007" Major="5.2" Minor="0" Revision="2" Name="Version Utility">Major and Minor revisions automatically set by utility.</Revision>
	</Version>
</WebFramework>
*/
var moPopup = window.createPopup();
var mbCopying=false;
var mbCutting=false;
var moItem=null;

function doEditSelect(eSrc){
	undoEditSelect();
	top.fraTopic.fraAction.undoEditSelect();
	top.fraAsset.undoEditSelect();
	
	if (moLast == null){
		moLast = new Actual();
	}
	moLast.EditTopicID = eSrc.id;
	//eSrc.style.border = '1px dashed #999999';
	eSrc.children[0].Highlighted = true;
}

function undoEditSelect(){
	if (moLast != null){
		if (moLast.EditTopicID != null){
			var oTmp = document.all(moLast.EditTopicID);
			if (oTmp != null) oTmp.children[0].Highlighted = false; //oTmp.style.border='';
			moLast.EditTopicID = null;
		}
	}
}

//Context Menu Functions
function returnFalse(){
	return false;
}
function showMenu(){
	var iHeight = 0;
	var oOld = moPopup.document.all('divMenu')
	if (oOld != null){
		oOld.removeNode(true);
	}
	if (!event.ctrlKey){
		event.returnValue=false;					
		
		if (top.mbSiteConfig == true){
			var eSrc = event.srcElement;
			while (eSrc.tagName != 'TD' && eSrc.parentElement != null){
				eSrc = eSrc.parentElement;
			}
			
			if (eSrc.tagName == 'TD' && parseInt(eSrc.getAttribute('TOPICID')) >= -1 ){
				doEditSelect(eSrc);
				//eSrc.click(); //Select Topic
								
				var oPopBody = moPopup.document.body;
				moPopup.document.createStyleSheet('../CSS/PopupMenu.css');
													
				var oDiv = moPopup.document.createElement('DIV');
				oDiv.id='divMenu';
				oDiv.attachEvent('onclick', menuClick);
				oDiv.attachEvent('onmouseover', doMenuOver);
				oDiv.attachEvent('onmouseout', doMenuOut);
				
				if (parseInt(eSrc.getAttribute('TOPICID')) > 0){
					var oAddB = moPopup.document.createElement('DIV');
					oAddB.innerText='Insert Before...';
					oDiv.appendChild(oAddB);										
				}
				
				var oAddA = moPopup.document.createElement('DIV');
				oAddA.innerText='Insert After...';
				oDiv.appendChild(oAddA);
				
				if (parseInt(eSrc.getAttribute('TOPICID')) > 0){
					var oEdit = moPopup.document.createElement('DIV');
					oEdit.innerText='Edit...';
					oDiv.appendChild(oEdit);					
				
					var oDelete = moPopup.document.createElement('DIV');				
					oDelete.innerText='Delete';
					oDiv.appendChild(oDelete);
					
					var oHR = moPopup.document.createElement('HR');
					oDiv.appendChild(oHR);
				
					var oCopy = moPopup.document.createElement('DIV');
					oCopy.innerText='Copy';				
					oDiv.appendChild(oCopy);
						
					var oCut = moPopup.document.createElement('DIV');
					oCut.innerText='Cut';
					oDiv.appendChild(oCut);																							
				}
				
				if (mbCopying || mbCutting){
					var oHR = moPopup.document.createElement('HR');
					oDiv.appendChild(oHR);												
					
					if (parseInt(eSrc.getAttribute('TOPICID')) > 0){
						var oPasteB = moPopup.document.createElement('DIV');				
						oPasteB.innerText='Paste Before';
						oDiv.appendChild(oPasteB);
					}
					
					var oPasteA = moPopup.document.createElement('DIV');				
					oPasteA.innerText='Paste After';
					oDiv.appendChild(oPasteA);																			
				}
				
				if (oDiv.children.length > 0){
					iHeight = (18 * oDiv.children.length) - ((oDiv.children.length-1) * 2);
					oPopBody.style.backgroundColor='white';
					oPopBody.id='bdyMenu';				
					oPopBody.appendChild(oDiv);
					oPopBody.attachEvent('onselectstart', returnFalse);
					moPopup.show(event.x + 10, event.y, 160, iHeight, document.body);
				}														
			}
		}
		return false;
	}
}

function menuClick(){
	var eSrc=moPopup.document.parentWindow.window.event.srcElement;
	var iTopic = moLast.EditTopicID;//moLast.TopicID;
	
	switch(eSrc.innerText){
		case 'Insert Before...' :
			doAdd(iTopic,'beforeBegin');
			break;
		case 'Insert After...' :
			doAdd(iTopic,'afterEnd');
			break;
		case 'Edit...' :
			doEdit(iTopic);
			break;
		case 'Delete' :
			doDelete(iTopic);
			break;
		case 'Copy' :
			if (moItem != null){
				var oElem = document.all(moItem.ID.toString());
				if (oElem != null && oElem.style.fontStyle=='italic'){
					oElem.style.fontStyle='';
				}
			}			
			if (dicTopics.Exists(iTopic.toString())){
				moItem = dicTopics(iTopic.toString());			
				mbCutting=false;
				mbCopying=true;				
			}
			break;
		case 'Cut' :
			if (moItem != null){
				var oElem = document.all(moItem.ID.toString());
				if (oElem != null && oElem.style.fontStyle=='italic'){
					oElem.style.fontStyle='';					
				}
			}
			if (dicTopics.Exists(iTopic.toString())){
				moItem = dicTopics(iTopic.toString());
				eSrc.style.fontStyle='italic';
				mbCopying=false;		
				mbCutting=true;
			}
			break;
		case 'Paste Before' :			
			//Fall Through
		case 'Paste After' :
			doPaste(iTopic, eSrc.innerText);
			break;
	}		
	moPopup.hide();
	undoEditSelect();	
}

//Callback Function From GetTopic.asp
function RefreshTopics(){	
	var Asset = top.fraAsset.miSelected; //oDivAsset.getAttribute("SELECTEDITEM");
	
	if (Asset != null){
		Asset = Asset.toString();
		if (top.fraAsset.dicAssets.Exists(Asset)){
			var oAssetItem = top.fraAsset.dicAssets(Asset);		
			RefreshTopic(oAssetItem);
		}
	}
}

function CallBack(ReturnValue){	
	moLast.TopicID = ReturnValue; //Select newly created topic
	frmTopicConfig.TopicID.value = ReturnValue;
	frmTopicConfig.txtCallBack.value='false';
	frmTopicConfig.txtHasReturnValue.value='false';
	frmTopicConfig.action='../Config/Topic/GetTopic.asp';
	frmTopicConfig.submit();
}

function doMenuOver(){
	var eSrc = moPopup.document.parentWindow.window.event.srcElement;
	eSrc.className = 'over';				
}
			
function doMenuOut(){
	var eSrc = moPopup.document.parentWindow.window.event.srcElement;
	eSrc.className = '';
}

function doAdd(iTopic, sLocation){
	var aItems=(new VBArray(dicTopics.Items())).toArray();
	var args=new Array();
			
	args[0]='Create New Topic';	
	args[1] = new Array(-1,'', false);
						
	var ret=window.showModalDialog('../Config/Topic/Dialog.asp',args,'dialogWidth:220px;dialogHeight:50px;help:no;status:no;resizable:no;');
	if (ret != null){
		var iAsset = 'null';
		if (ret[0] > 0) iAsset = ret[0];
		
		var SQL = "Set nocount on;";	
		if (dicTopics.Exists(iTopic.toString())){
			var oItem = dicTopics(iTopic.toString());
			var iOrder = oItem.Order;
			
			if (sLocation == 'beforeBegin'){
				for (var i=0;i<aItems.length;i++){
					if (aItems[i].Order >= iOrder){
						SQL += "Update fwTopic Set DisplayOrder = DisplayOrder + 1 Where ID = " + aItems[i].ID + ";";
					}
				}
			}else{
				for (var i=0;i<aItems.length;i++){
					if (aItems[i].Order > iOrder){
						SQL += "Update fwTopic Set DisplayOrder = DisplayOrder + 1 Where ID = " + aItems[i].ID + ";";
					}
				}
				iOrder++; //Insert After				
			}
			
			var iConfigFlag = 0;
			if (ret[2] == true){
				iConfigFlag = 1;
			}
			
			SQL += "Insert fwTopic (Asset_ID, Name, DisplayOrder, ConfigFlag) Values (" +
				iAsset + ",'" + ret[1].replace(/'/g, "''") + "'," + iOrder + "," + iConfigFlag + ");";
					
			SQL += "Select Scope_Identity() as NewIdentity;";
			frmTopicConfig.txtCallBack.value='true';
			frmTopicConfig.txtHasReturnValue.value='true';
			frmTopicConfig.sql.value = SQL;
			frmTopicConfig.txtAuditDescription.value = 'Added Topic [' +  ret[1] + ']';
			frmTopicConfig.action='ExecuteSQL.asp';		
			frmTopicConfig.submit();
		}
	}	
}

function doEdit(iTopic){
	if (dicTopics.Exists(iTopic.toString())){
		var oItem=dicTopics(iTopic.toString());
		var args=new Array();
			
		args[0]='Edit ' + oItem.Name + ' Topic';
		args[1]=new Array(oItem.AssetID, oItem.Name, oItem.ConfigFlag);
		var ret=window.showModalDialog('../Config/Topic/Dialog.asp',args,'dialogWidth:220px;dialogHeight:50px;help:no;status:no;resizable:no;');
		if (ret != null){
			var iAsset = 'null';
			var iType=null;
			if (ret[0] > 0) iAsset = ret[0];			
						var SQL = "Set nocount on;";		
			
			var iConfigFlag = 0;
			if (ret[2] == true){
				iConfigFlag = 1;
			}
				
			SQL += "Update fwTopic Set Asset_ID = " + iAsset + 
				",Name = '" + ret[1].replace(/'/g, "''") +								
				"',ConfigFlag = " + iConfigFlag +
				" Where ID = " + oItem.ID + ";";
						
			SQL += "Select " + oItem.ID + " as EditIdentity;";
			
			frmTopicConfig.txtCallBack.value='true';
			frmTopicConfig.txtHasReturnValue.value='true';
			frmTopicConfig.sql.value = SQL;
			frmTopicConfig.txtAuditDescription.value = 'Changed Topic [' +  ret[1] + ']';
			frmTopicConfig.action='ExecuteSQL.asp';		
			frmTopicConfig.submit();
		}
	}
}

function doDelete(iTopic){
	var aItems=(new VBArray(dicTopics.Items())).toArray();
	
	if (dicTopics.Exists(iTopic.toString())){
		var oItem=dicTopics(iTopic.toString());												
		var iOrder = oItem.Order;
		
		var ret=window.confirm('Are you sure you want to delete Topic [' + oItem.Name + 
			']? THIS CANNOT BE UNDONE!');			
		if (ret){
			var SQL = "Set nocount on;";
			for (var i=0;i<aItems.length;i++){
				if (aItems[i].Order > iOrder){
					SQL += "Update fwTopic Set DisplayOrder = DisplayOrder - 1 Where ID = " + aItems[i].ID + ";";
				}
			}
			SQL += "Delete From fwTopic Where ID =" + oItem.ID + ";";
			SQL += "Select 0 as DeleteIdentity;";
			
			dicTopics.Remove(oItem.ID.toString());						
						
			frmTopicConfig.txtCallBack.value='true';
			frmTopicConfig.txtHasReturnValue.value='true';
			frmTopicConfig.sql.value = SQL;
			frmTopicConfig.txtAuditDescription.value = 'Deleted Topic [' +  oItem.Name  + ']';
			frmTopicConfig.action='ExecuteSQL.asp';					
			frmTopicConfig.submit();												
		}
	}
}

function doPaste(iTopic, sType){
	if (mbCutting){
		doPasteCut(iTopic, sType);
	}else if(mbCopying){
		doPasteCopy(iTopic, sType);
	}
	ResizeTopic();
}

function doPasteCut(iTopic, sType){
	if (dicTopics.Exists(iTopic.toString())){
		var oItem = dicTopics(iTopic.toString()); //Destination Item
		
		var oParent = document.all(oItem.ID.toString()); //Destination TD
		var oElem = document.all(moItem.ID.toString());  //Source Element
		var oNewElem = oElem.cloneNode(true); //Make a copy of the element
		
		var SQL = 'set nocount on;';
		
		switch(sType){
			case 'Paste Before' :				
				SQL += "Update fwTopic Set DisplayOrder = DisplayOrder + 1" +
					" Where DisplayOrder >= " + oItem.Order + ";";
				
				SQL += "Update fwTopic Set DisplayOrder=" + oItem.Order + 
					" Where ID = " + moItem.ID + ";";
				
				//oParent.insertAdjacentElement('beforeBegin', oNewElem);
				break;
			case 'Paste After' :				
				SQL += "Update fwTopic Set DisplayOrder = DisplayOrder + 1" +
					" Where DisplayOrder > " + oItem.Order + ";";		
				
				SQL += "Update fwTopic Set DisplayOrder=" + (oItem.Order+1) + 
					" Where ID = " + moItem.ID + ";";
				
				//oParent.insertAdjacentElement('afterEnd', oNewElem);
				break;
		}
		
		SQL += "Select " + moItem.ID + " as CutIdentity;";		
		
		if (oElem != null){
			oElem.removeNode(true);
		}	
	
		mbCutting=false;
		mbCopying=false;
				
		frmTopicConfig.txtCallBack.value='true';
		frmTopicConfig.txtHasReturnValue.value='true';
		frmTopicConfig.sql.value = SQL;
		frmTopicConfig.txtAuditDescription.value = 'Moved Topic [' +  moItem.Name  + ']';
		frmTopicConfig.action='ExecuteSQL.asp';
		frmTopicConfig.submit();
	}
}

function doPasteCopy(iTopic, sType){
	if (dicTopics.Exists(iTopic.toString())){
		var oItem = dicTopics(iTopic.toString()); //Destination Item
		
		var oParent = document.all(oItem.ID.toString()); //Destination TD
		var oElem = document.all(moItem.ID.toString());  //Source Element
		var oNewElem = oElem.cloneNode(true); //Make a copy of the element
		
		var SQL = 'set nocount on;';
		
		switch(sType){
			case 'Paste Before' :				
				SQL += "Update fwTopic Set DisplayOrder = DisplayOrder + 1" +
					" Where DisplayOrder >= " + oItem.Order + ";";
				
				SQL += "exec p_fwCopyTopic " + moItem.ID + "," + oItem.Order + ";";
				
				//oParent.insertAdjacentElement('beforeBegin', oNewElem);
				break;
			case 'Paste After' :				
				SQL += "Update fwTopic Set DisplayOrder = DisplayOrder + 1" +
					" Where DisplayOrder > " + oItem.Order + ";";		
				
				SQL += "exec p_fwCopyTopic " + moItem.ID + "," + (oItem.Order+1) + ";";
				
				//oParent.insertAdjacentElement('afterEnd', oNewElem);
				break;
		}
		
		SQL += "Select -1 as CopyIdentity;";				
	
		mbCutting=false;
		mbCopying=false;
				
		frmTopicConfig.txtCallBack.value='true';
		frmTopicConfig.txtHasReturnValue.value='true';
		frmTopicConfig.sql.value = SQL;
		frmTopicConfig.txtAuditDescription.value = 'Copied Topic [' +  moItem.Name  + ']';
		frmTopicConfig.action='ExecuteSQL.asp';
		frmTopicConfig.submit();
	}
}
