
//
// Functions used by the bulletin interface
// 


//----------------------------------------------------------------
//Update Node Text/Caption
//----------------------------------------------------------------
var SAVE_NODE_CAPT_URL= "nlstree/savenodecaption.asp";

// On node change
function onNodeChange(id) {

  // id is the ID of the object
  //alert(id);

  // get the node object from the ID
  var nd=tree.getNodeById(id);

  // from this Node object get the parent ID, pr=parent, orgId=id of the object
  var parentID=nd.pr.orgId;

  var c=new NlsXMLHttpHandler();
  c.tId=this.tId;
  c.id=id;
  
  c.anim=new AnimateSave();
  c.anim.tree=this;
  c.anim.nId=id;  
	
	//alert('id is : ' + id);
  // Call the update script
  c.init("get", SAVE_NODE_CAPT_URL+"?nid=" + id + "&caption=" + nd.capt + "&parentID=" + parentID, true);
  c.send(null);

};

//animate tree node while saving.
function AnimateSave() {

  this.tree=null;
  this.nId=null;
  this.count=1;
  
  var me=this;
  
  this.start=function() {
    var animDiv=NlsGetElementById("utilDiv");
    animDiv.innerHTML="Saving...(" + me.count++ + ")";
    animDiv.style.display="block";
  };
  this.stop=function() { 
    var animDiv=NlsGetElementById("utilDiv");
    animDiv.style.display="none";
    me.count=1; 
    return; 
  };
};


//----------------------------------------------------------------
// Delete Node 
//----------------------------------------------------------------
function DeleteNode(id) {

  // id is the ID of the object
  // alert(id);

  var c=new NlsXMLHttpHandler();
  c.tId=this.tId;
  c.id=id;
  
  c.anim=new AnimateSave();
  c.anim.tree=this;
  c.anim.nId=id;  

  // Call the update script
  c.init("get", "nlstree/DeleteNode.asp?nid=" + id , true);    
  c.send(null);

};

//----------------------------------------------------------------
//Update Node Text/Caption
//----------------------------------------------------------------
var MOVE_TREE_NODE_URL="nlstree/movenode.asp";

function saveMovedNode(c, ddss, type) {
  c.setTimeout(CONNECTION_TIMEOUT);
  c.win.location.href=MOVE_TREE_NODE_URL+"?srctid=" + ddss.srcObj.tId + "&srcnid=" + ddss.srcData.orgId + "&dsttid=" + ddss.destObj.tId + "&dstnid=" + ddss.destData.orgId + "&mtype=" + type;
}

function onNodeDropForUpdate(e) {
  //process
  if (!nlsddSession) return;
  if(!nlsddSession.action) return;

  if (nlsddSession.srcObj.tId==nlsddSession.destObj.tId) { //drag drop in a tree
    switch (nlsddSession.action) {
      case NlsDDAction.DD_INSERT:
        
        var ddss=new NlsDDSession(nlsddSession.srcObj, nlsddSession.srcData);
        ddss.destObj=nlsddSession.destObj;
        ddss.destData=nlsddSession.destData;
        
        if (!ddss.srcData || ddss.srcData.length!=1) return;
        if (ddss.srcObj.isSelected(ddss.destData.orgId)) return;
        if (ddss.destData.equals(ddss.srcObj.rt)) return;
        if (ddss.srcData[0].nx && ddss.destData.equals(ddss.srcData[0].nx)) return;

        var c=new NlsXMLHttpHandler();
        c.tId=nlsddSession.srcObj.tId;
        c.id=nlsddSession.srcData[0].orgId;

        c.anim=new AnimateSave();
        c.anim.tree=nlsddSession.srcObj;
        c.anim.nId=nlsddSession.srcData[0].orgId;  

        c.init("get", MOVE_TREE_NODE_URL + "?srctid=" + ddss.srcObj.tId + "&srcnid=" + ddss.srcData[0].orgId + "&dsttid=" + ddss.destObj.tId + "&dstnid=" + ddss.destData.orgId + "&mtype=" + 2, true);    
        c.send(null);
      
        this.tree.moveChild(nlsddSession.srcData, nlsddSession.destData, 2);        
        break;
      case NlsDDAction.DD_APPEND: 
			if (confirm("Are you sure you want to move this section?"))
			{		
				var ddss=new NlsDDSession(nlsddSession.srcObj, nlsddSession.srcData);
				ddss.destObj=nlsddSession.destObj;
				ddss.destData=nlsddSession.destData;

				if (!ddss.srcData || ddss.srcData.length!=1) return;
				if (ddss.srcObj.isSelected(ddss.destData.orgId)) return;
				if (ddss.srcData[0].equals(ddss.srcObj.rt)) return;
				if (ddss.srcData[0].pr.equals(ddss.destData)) return;

				var c=new NlsXMLHttpHandler();
				c.tId=ddss.srcObj.tId;
				c.id=ddss.srcData[0].orgId;

				c.anim=new AnimateSave();
				c.anim.tree=ddss.srcObj;
				c.anim.nId=ddss.srcData[0].orgId;
				
				c.resultReady_Callback=function(tId, id) {
				  ddss.srcObj.moveChild(ddss.srcData, ddss.destData, 1);
				}

				c.init("get", MOVE_TREE_NODE_URL + "?srctid=" + ddss.srcObj.tId + "&srcnid=" + ddss.srcData[0].orgId + "&dsttid=" + ddss.destObj.tId + "&dstnid=" + ddss.destData.orgId + "&mtype=" + 1, true);    
				c.send(null);
			}
        break;
    }
  }
}

//----------------------------------------------------------------
// When a node is clicked
//----------------------------------------------------------------

function handleOnClick(e, id) {
	
	var firstChar = id.substring(0,1);
	if (firstChar=='D')
	{
		//nls_setCookie('CD', id, 20);
		//alert("I am a document clicked " + id + ' first = ' + firstChar);
		//window.open()
		//alert(tree.getNodeById(id).url);
	}
	else
	{
		//alert("I am a folder clicked " + id);
	}
}

//----------------------------------------------------------------
// When a move up is clicked
//----------------------------------------------------------------

function moveUp(selNodes) {
  //move before previous node of selected node.
  var prev=(selNodes[0]?selNodes[0].pv:null);
  if (!prev) return;
  // Set the 1st and 2nd sequences which we use when calling the asp script
  var Seq1=prev.orgId;
  var Seq2=selNodes[0].orgId;

  // alert('move up - switch ' + prev.orgId + ' (' + prev.capt + ') with ' + selNodes[0].orgId + ' (' + selNodes[0].capt + ')');
  SequenceChange(prev.orgId, selNodes[0].orgId);
  tree.ctx_moveChild(selNodes, prev, 2);
  tree.selectNodeById(selNodes[0].orgId);
  
}

//----------------------------------------------------------------
// When a move down is clicked
//----------------------------------------------------------------

function moveDown(selNodes) {
  //move after next node of selected node.
  var next=(selNodes[0]?selNodes[0].nx:null);
  if (!next) return;

  // alert('move down - switch  ' + next.orgId + ' (' + next.capt + ') with ' + selNodes[0].orgId + ' (' + selNodes[0].capt + ')');
  SequenceChange(next.orgId, selNodes[0].orgId);

  tree.ctx_moveChild(selNodes, next, 3);
  tree.selectNodeById(selNodes[0].orgId);
}

//----------------------------------------------------------------
// Change Sequence
//----------------------------------------------------------------

var SEQUENCE_CHANGE_URL="nlstree/SwitchSequence.asp";

function SequenceChange(id1, id2) {
  var c=new NlsXMLHttpHandler();
  c.anim=new AnimateSave();
  c.anim.tree=this;
  c.init("get", SEQUENCE_CHANGE_URL+"?id1=" + id1 + "&id2=" + id2, true);    
  c.send(null);
};

/******************************************
* auto resize ifame
********************************************/
function autofitIframe(id)
{
	if(document.getElementById) 
	{
		if(parent.document.getElementById(id))
		{
			parent.document.getElementById(id).style.height=this.document.body.scrollHeight+"px";
			if (this.document.body.offsetHeight)
			{
			parent.document.getElementById(id).style.height=this.document.body.offsetHeight+"px";
			}
		}
	}
}

/******************************************
* reload iframe
********************************************/

function refreshIframe(id)
{
	if(document.getElementById) 
	{
		if(parent.document.getElementById(id))
		{
			//parent.document.getElementById(id).document.location.reload();
			parent.document.getElementById(id).src = parent.document.getElementById(id).src;
		}
	}
}

/******************************************
* Open Popup Window
********************************************/

function openPopupWindow(windowURL, windowName, xLeft, yTop, xWidth, xHeight)
{
	var win=window.open(windowURL, windowName, "left="+xLeft+",top="+yTop+",width="+xWidth+",height="+xHeight+",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
	win.focus();  
}

function closePopupWindow()
{
	window.opener.focus();
  	window.close();
}

function closeme() 
		{
			// refreshes calling window
			opener.location.href = opener.location;

			var ua = window.navigator.appVersion;
			var msie = ua.indexOf ( "MSIE ");

			if(msie > 0)
			{
				window.open('close.html', '_self');
			}
			else
			{
				// Firefox
				top.window.close();
				window.self.close(opener=0);
			}
			
		}
