/*
 *	TITLE:	listdeploy.js
 *	VER:	1.0
 *  AUTHOR:	David Pattison
 *	DATE:	10/03/2004
 */

/* 
 * Global variables
 */
var canUserReadAndWrite = false;
var xmlDeleteSelection  = null;	//reference to the XML of the selected deployment marked for deletion

/*
 * Send request to server to delete deployment
 */
function doApplyDelete(xmlRef)
{
	// doConfirmDeleter returns null if the list is empty
	var xmlSelDeploy = ConfirmDelete(xmlRef, 'deployment');
	var xmlDoc;
	
	if( xmlSelDeploy != null )
	{
		try
		{
			// Construct xml doc to transport request
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.loadXML("<delete/>");
			xmlDoc.createProcessingInstruction("xml", "version='1.0'");
            var xmlDirNode = xmlSelDeploy.firstChild.cloneNode(true);
			xmlDoc.documentElement.appendChild(xmlDirNode);
			
            // store the XML reference of the selection to delete on the callback
			xmlDeleteSelection = xmlSelDeploy;
            
			// Commit changes - POST them to server
			doPostXml(xmlDoc, true, ApplyDeleteCallback); // async completion
		}
		catch(err)
		{
			LocaleAlertMessage("doApplyDelete()", err);
		}
	} 
}

/* 
 * call-back function for Deleting a deployment on the webserver
 * Upon successful deletion the current page is reloaded and 
 * the status is updated
 */
function ApplyDeleteCallback(xmlReply)
{
	var xmlRoot    = xmlReply.documentElement;
	var xmlDeleted = (xmlRoot==null) ? null :
                     xmlRoot.selectSingleNode("deleted");
	if(xmlDeleted != null)
	{
		// update the local list of deployments
		if(xmlDeleteSelection == null || xmlDeleteSelection.firstChild == null)
		{
			// the local XML file remains inconsistent. Reload the page
			location.reload();
		}
		var strDelSelName = xmlDeleteSelection.firstChild.nodeTypedValue;
		var strSrcFile    = xmlDeleted.nodeTypedValue;
		if(strDelSelName == strSrcFile)
		{
			xmlDeleteSelection.parentNode.removeChild(xmlDeleteSelection);
			InsertMessage("DEPDEL", strSrcFile);
			xmlDeleteSelection = null;
		}
		else
		{
			// the local XML file remains inconsistent. Reload the page
			location.reload();
		}
	}
	// ELSE - Completed with errors
	// Error messages are written directly on async reply
}

/*
 * load SCADA page with selected deployment
 */
function doExecuteDeploy(xmlRef, bIsDC) 
{
	var strSelName = new String();
	var strProjName = new String();
	var strClientCAB = new String();
	var strURL = new String();
	
	try 
	{
		xmlSelDeploy = GetSelObject(xmlRef, 'deployment');
		if( xmlSelDeploy != null )
		{
			strSelName = xmlSelDeploy.selectSingleNode("dir").nodeTypedValue;
			strProjName = xmlSelDeploy.selectSingleNode("projname").nodeTypedValue;
			strClientCAB = xmlSelDeploy.selectSingleNode("clientpath").nodeTypedValue;
			// clientpath could be empty only if the deployment is created by citect
			if (strClientCAB == "")
			{
				alert(GetLocaleMessage("INVALIDCLIENTCTRL"));
			}
			else
			{
				strURL = "scada.htm?DeployDir=" + strSelName + "&ProjectName=" + strProjName + "&ClientCAB=" + strClientCAB;
			
				if( bIsDC )
				{
					strURL += "&AccessMode=RW";
				}
			
				//window.open(strURL, "NewWin", "scrollbars=1, resizeable=1");
				location.href=strURL;
			}
		}
		else
		{
			alert(GetLocaleMessage("DEPNULL", "run"));
			InsertMessage("DEPNULL", "run");
		}
	}
	catch (err) 
	{
		LocaleAlertMessage("doExecuteDeploy()", err);
	}
}

/*
 * load Add/Edit page with selected deployment
 * Selected deployment is null for a new request
 */
function doAddEditDeploy(xmlRef) 
{
	var strSelName = new String();
	var strURL = new String();
	
	try 
	{
		xmlSelDeploy = GetSelObject(xmlRef, 'deployment');
		if( xmlSelDeploy == null )
		{
			strURL = "addeditdeploy.htm?";
		}
		else
		{
			strSelName   = xmlSelDeploy.firstChild.nodeTypedValue;
			strURL = "addeditdeploy.htm?DeployDir=" + strSelName;
		}
		
		if( strLang != null )
		{
			strURL+= "&lang="+strLang;
		}
		
		//window.open(strURL, "NewWin", "scrollbars=1, resizeable=1");
		document.location.href=strURL;
	}
	catch (err) 
	{
		LocaleAlertMessage("doAddEditDeploy()", err);
	}
}

/*
 * Invoke detail dropdown, change icon to match
 */
function doExpandClick(tblExpand) 
{
	try
	{
		var nRecNumber = GetSelByNumber();
		var strSrc = new String(event.srcElement.src);
		var rowExpand=tblExpand[nRecNumber];
		
		// toggle icon
		if (strSrc.indexOf("minus") > 0) 
		{
			strNewSrc = strSrc.replace("minus", "plus");
		} 
		else 
		{
			strNewSrc = strSrc.replace("plus", "minus");
		}
		event.srcElement.src = strNewSrc

		// display/hide details
		if( rowExpand == null )
		{
			rowExpand=tblExpand;
		}
		if (rowExpand.style.display == "none") 
		{
			rowExpand.style.display = "";
		} 
		else 
		{
			rowExpand.style.display = "none";
		}
	}
	catch (err) 
	{
		LocaleAlertMessage("doExpandClick()", err);
	}
}

/*
 * Add specified deployment config to main xml doc
 */
function UpdateList(objPath, xmlRef)
{
	try
	{
		var strXmlPath = new String();
	 	strXmlPath=objPath.href+"citectscada.xml";
	 	
		var xmlNewRec=xmlClone(strXmlPath, xmlRef);
		
		// Override XML for dir name with actual directory
		// this allows copy/paste of directories.
		if( xmlNewRec != null)
		{
                    var strPath = objPath.innerText;
                    var ss = strPath.split("/");
                    xmlNewRec.firstChild.nodeTypedValue = ss[0];
		}
 	}
	catch(err)
	{
		LocaleAlertMessage("UpdateList()", err);
	}
}	 

/*
 * Hidden IFrame has browsed ./deploy folder
 * Each subdirectory appears as a link on this page
 * Parse each link
 */
function ListLinks(xmlRef)
{
	try
	{
		var theDoc = deploy.document;
		// scan each anchor
		for (ii=0; ii<theDoc.links.length; ii++)
		{
			UpdateList(theDoc.links[ii], xmlRef);
		}
	}
	catch(err)
	{
		// skip erroneous folders
		LocaleAlertMessage("ListLinks()", err);
	}
}

/*
 * Hide Administrator/DC buttons
 */
function SetUser(xmlRef)
{
	try
	{
		var xmlRoot=xmlRef.documentElement;
		
		if( xmlRoot != null )
		{
			var strUser=xmlRoot.selectSingleNode("status/from");
			var defaultImages = document.images;
			
			for (ii=0; ii<defaultImages.length; ii++)
			{
				var thisImage = defaultImages[ii];
				
				switch(strUser.nodeTypedValue)
				{
				case "LOGINADMIN":
					if(thisImage.id=="CLICK")
					{
						thisImage.style.display="";
						thisImage.style.cursor="hand";
					}
					if(thisImage.id=="ADDDEP" || thisImage.id=="EDITDEP" ||
						thisImage.id=="DELDEP" || thisImage.id=="SCADADC")
					{
						thisImage.style.display="";
					}
					break;
		
				case "LOGINDC":
					if(thisImage.id=="SCADADC")
					{
						thisImage.style.display="";
					}
					break;
					
				case "LOGINMC":
				default:
					break;
				}
			}
		}
	}
	catch(err)
	{
		LocaleAlertMessage("SetUser()", err);
	}
}



/*
 * Check RW accessmode
 */
function CanUserRW(xmlRef)
{
	try
	{
		var xmlRoot=xmlRef.documentElement;
		
		if( xmlRoot != null )
		{
			var strUser=xmlRoot.selectSingleNode("status/from");
		
			switch(strUser.nodeTypedValue)
			{
                            case "LOGINADMIN":
                            case "LOGINDC":
                                canUserReadAndWrite = true;
				break;
				
                            case "LOGINMC":
                            default:
				break;
			}
		}
	}
	catch(err)
	{
		LocaleAlertMessage("CanUserRW()", err);
	}
}

/*
 * Catch pages loading without SSL
 */
function SSLOnly()
{
	try
	{
		if( location.protocol == "http:" )
		{
			location.href=location.href.replace("http", "https");
		}
	}
	catch(err)
	{
		LocaleAlertMessage("SSLOnly()", err);
	}
}

/*
 * Check whether the current user has write access to the /deploy folder
 * if not, remove admin functions.
 */
function doTestUser(fPostFn)
{
	var xmlDoc;
	try
	{
		// Construct xml doc to transport request
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.loadXML("<user/>");
		xmlDoc.createProcessingInstruction("xml", "version='1.0'");
			
		// Commit changes - POST them to server
		// pass the additional function to post processing
		doPostXml(xmlDoc, false, fPostFn);
	}
	catch(err)
	{
		LocaleAlertMessage("doTestUser()", err);
	}
	
	return false;
}
