<!DOCTYPE html>

<%@ include file = "EgTrackerA.jsp" %>
<%@ page import = "com.eg.EgSpecsInfo" %>
<%@ page import = "com.eg.EgExternalInfo" %>
<%@ page import = "com.eg.EgServicesInfo"%>
<%@ page import = "com.eg.EgDbInfo"%>
<%@ page import = "com.eg.EgRoleInfo"%>
<%@ page import = "com.eg.EgUuidInfo" %>
<%@ taglib prefix = "egui" uri = "../WEB-INF/eg-taglib.tld" %>
<%@ page language = "java" contentType = "text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
%>

<jsp:useBean id="trans" scope="page" class="egurkha.TransactionBean"/>

<%
	trans.createFile("eg_external.ini");

	String assignAgent = request.getParameter("eagent");
	String fromAssignRem = request.getParameter("fromAssignRem");
	String fromDeleteRem = request.getParameter("fromDeleteRem");
	String fromPage = request.getParameter("from");
	String agentState = request.getParameter("agentState");

	EgRoleInfo egRoleInfo = EgRoleInfo.getInstance(session.getId(),(String)session.getAttribute("sess_userid"));
	boolean isLimitedAdmin = !egRoleInfo.hasAllComponentAccess();

	EgExternalInfo extInfo = new EgExternalInfo("manager");
	String[] rmtAgents = extInfo.getAllRemoteAgents();
	if (isLimitedAdmin)
	{
		com.eg.EgUserElementsInfo userElmentsObj = new com.eg.EgUserElementsInfo();
		ArrayList agntsList = userElmentsObj.getExternalAgentsForUser(_user.getLoginID());
		rmtAgents = (String[])agntsList.toArray(new String[0]);
	}
	List rmtList = Arrays.asList(rmtAgents);
	String strrmtList = rmtList.toString();
	strrmtList = strrmtList.substring(1,strrmtList.length()-1);

	String strIntAgents = "";
	String[] internalAgentsHosts = extInfo.getInternalAgentHosts();
	if(internalAgentsHosts != null)
	{
		List intAgentsList = Arrays.asList(internalAgentsHosts);
		strIntAgents = intAgentsList.toString();
		strIntAgents = strIntAgents.substring(1,strIntAgents.length()-1);
	}

	String[] allRemoteComponents = extInfo.getRemoteComponents();
	String strRmtCompsList = "";
	if(allRemoteComponents != null)
	{
		List rmtCompsList = Arrays.asList(allRemoteComponents);
		strRmtCompsList = rmtCompsList.toString();
		strRmtCompsList = strRmtCompsList.substring(1,strRmtCompsList.length()-1);
	}

	EgSpecsInfo specs = new EgSpecsInfo("manager");
	String RmtIpAgent = specs.getEGServer();

	EgServicesInfo servicesInfo = new EgServicesInfo("manager");
	boolean isAllowHostName = servicesInfo.isAllowHostNames();
	boolean isDouble=EgDbInfo.doubleByteEnabled;
	
	IniFile mgrmiscIni = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_mgrmisc.ini");
	boolean hideDwnldAgent = true;
	String enableDownloadAgent = mgrmiscIni.getValue("MISC_ARGS","enableRemoteAgentDownload");
	if (enableDownloadAgent != null && (enableDownloadAgent.equalsIgnoreCase("true") || enableDownloadAgent.equalsIgnoreCase("yes")))
	{
		hideDwnldAgent = false;
	}

	//IniFile clsIni = new IniFile (EgInstallInfo.getInstallDir()+"/manager/config/eg_ls_portal.ini");
	//String cloudModel = clsIni.getValue("LS_TRIAL_SETTINGS","cloudModel");
	//if (cloudModel != null && cloudModel.equalsIgnoreCase("phoenix"))
	//{
		//hideDwnldAgent = true;
	//}

	// for limited components
	String fromWizard = request.getParameter("fromWizard");
	if(com.egurkha.util.StringUtils.isNull(fromWizard))
	{
		fromWizard = "";
	}

	boolean isMspModel = EgUuidInfo.isMspModel();
%>

<html>
<head>
	<style type="text/css">
		.add {
			background-image:url('/resources/themes/images/default/grid/refresh.gif') !important;
			width:80px!important;
			height:80px!important;
			margin-right: auto !important;
			margin-left: auto !important;
		}
	</style>
	<meta charset="utf-8">
	<title>REMOTE AGENTS</title>

	<!--Skin color--------->
	<egui:skin ui = "admin"/>

	<!---Components, icons, skin and Core ext script--->
	<script type="text/javascript" src="../extjs/ext-all.js"></script>

	<!-- Fancy Scroll-->
	<script src="/final/jquery/jquery.js"></script>
	<script src="/final/styles/jscrollpane/jquery.jscrollpane.js" ></script>
	<script src="/final/styles/jscrollpane/jquery.mousewheel.js" ></script>
	<script src="/final/styles/jscrollpane/mwheelIntent.js" ></script>
	<link rel="stylesheet" href="/final/styles/jscrollpane/jquery.jscrollpane.css"/>

	<script language="JavaScript" src="eg_scripts/validate.js"></script>
	<script language="JavaScript1.2" src="../scripts/<%=script_src%>"></script>
	<script language="JavaScript1.2" src="eg_scripts/CustomGridSorting.js"></script>
	<script language="JavaScript" src="/final/scripts/utilities/EncodeDecodeParams.js"></script>

	<script language = "JavaScript1.2">
		var csrfToken = '<%=strCsrfToken%>';
		var hideDwnldAgent = <%=hideDwnldAgent%>;
		var isMspModel='<%=isMspModel%>';
		var checkdots = '<egui:intnl text="admin.external.Hostname should not start or end with a dot"/>';
		var downAgent = '<egui:intnl text="admin.title.Download Agent"/>';
		var checkSingleEdit = '<egui:intnl text="admin.external.Host IP and Nick Name cannot be changed at the same time"/>';
		var checkconsecutivedots = '<egui:intnl text="admin.external.Cannot have Period symbol in consecutive positions"/>';
		var checkBlankIP = '<egui:intnl text="admin.external.Please enter host IP"/>';
		var checkBlankIPName = '<egui:intnl text="admin.external.Please enter host IP/host name"/>';
		var checkBlankHostName = '<egui:intnl text="admin.external.Please enter nick name"/>';
		var hostNameIsIP = '<egui:intnl text="admin.server.Please enter the same IP address if you are entering IP address for the nick name field"/>';
		var hostNameByOtherAgent = '<egui:intnl text="admin.remote.This host/nick name is used by another remote agent"/>';
		var hostNameByOtherComp = '<egui:intnl text="admin.remote.The nickname you are trying to add is already mapped to a component."/>';
		var checkDBL = '<egui:intnl text="admin.external.Host name is not allowed in DBL(Chinese,Korean,Japanese),since manager is SingleByte"/>';
		var intAgentMappingAlert = '<egui:intnl text="admin.common.The nickname you are trying to assign to this agent is already mapped to a component set with internal agent assignment option"/>';
		var rmtPageTitle = '<egui:intnl text="admin.title.Remote Agent Configuration"/>';
		var rmtPageDesc = '<egui:intnl description="EgRemoteAgents.jsp"/>';
		var rmtAddPageTitle = '<egui:intnl text="admin.title.NEW REMOTE AGENT"/>';
		var rmtModPageTitle = '<egui:intnl text="admin.title.MODIFY REMOTE AGENT"/>';
		var rmtAddModPageDesc = '<egui:intnl description="EgNewRemoteAgents.jsp"/>';
		var assignRmtPageTitle = '<egui:intnl text="admin.title.Assign - Remote Agents"/>';
		var assignRmtPageDesc = '<egui:intnl description="EgConfigRemoteAgents.jsp"/>';
		var deleteRmtPageTitle = '<egui:intnl text="admin.title.DELETE REMOTE AGENTS"/>';
		var deleteRmtPageDesc = '<egui:intnl description="DeleteRemoteAgents.jsp"/>';

		var bckLabel = '<egui:intnl text="admin.common.Back"/>';
		var rmtTitleLbl = '<egui:intnl text="admin.common.Remote agents which are not associated to any hosts"/>';
		var lbl_AddAgnt = '<egui:intnl text="admin.common.Add New Agent"/>';
		var lbl_AssHost = '<egui:intnl text="admin.common.Associate Hosts"/>';
		var lbl_DelAgnt = '<egui:intnl text="admin.common.Delete Agents"/>';
		var lbl_DelToolAgnt = '<egui:intnl text="admin.common.Delete remote agents which are not associated to any hosts"/>';
		var lbl_VM = '<egui:intnl text="admin.common.VM Statistics"/>';
		var lbl_AgntHst = '<egui:intnl text="admin.server.Nick name"/>';
		var lbl_AgntIP = '<egui:intnl text="admin.server.Host IP/Name"/>';
		var ipMsg = '<egui:intnl text="admin.server.Specify the IP address or the fully qualified host name of the target"/>';
		if (isMspModel != null && isMspModel == 'true')
		{
			lbl_AgntIP = '<egui:intnl text="admin.discovery.Host name"/>';
			ipMsg = '<egui:intnl text="admin.server.Specify the DNS resolvable fully qualified host name of the target"/>';
		}
		var lbl_ChngIP = '<egui:intnl text="admin.common.Change IP"/>';
		var lbl_Del = '<egui:intnl text="admin.common.Delete"/>';
		var NoRmtDesc = '<egui:intnl text="admin.common.No remote agents present in the system"/>';
		var NoRmtFound = '<egui:intnl text="admin.common.No remote agent(s) found for your current search"/>';
		var sameNameAlert1 = '<egui:intnl text="admin.common.An external agent already exists with this old nick name"/>';
		var sameNameAlert2 = '<egui:intnl text="admin.common.By choosing YES, this operation will change both the external and remote agents"/>';
		var sameNameAlert3 = '<egui:intnl text="admin.common.By choosing NO, you can discard this operation"/>';

		var deleteSingleRmtAgntMsg = "<egui:intnl text='admin.external.Are you sure you want to delete the remote agent'/>, ";
		var deleteMsg = '<egui:intnl text = "admin.remote.Are you sure you want to delete the selected remote agent(s)"/>?';

		var remAgentLbl = '<egui:intnl text = "admin.remote.HOSTS MANAGED BY THIS AGENT"/>';
		var otherRemAgentLbl = '<egui:intnl text = "admin.remote.HOSTS MANAGED BY"/>';

		var label_Modify = '<egui:intnl text="admin.common.Modify"/>';
		var label_Delete = '<egui:intnl text="admin.common.Delete"/>';
		var label_AssAgent = '<egui:intnl text="admin.agents.Associate hosts"/>';
		var label_delSel = '<egui:intnl text="admin.common.Delete selected"/>';
		
		var btnText = '';
		var oldHostIp = '';
		var oldNickName = '';
		var RmtIpAgent = '<%=RmtIpAgent%>';
		var isAllowHostName = '<%=isAllowHostName%>';
		var isDouble = '<%=isDouble%>';
		var rmtListToStr = '<%=strrmtList%>';
		var rmtListStr = rmtListToStr.split(",");
		var intAgentsListToStr = '<%=strIntAgents%>';
		var intAgentsListStr = intAgentsListToStr.split(",");
		var remAgentCompListToStr = '<%=strRmtCompsList%>';
		var remAgentCompListArr = remAgentCompListToStr.split(",");
		var extListStr = new Array();
		var fromPage = '<%=fromPage%>';

		var appRmtCnt = '';
		var assignAgent = '<%=assignAgent%>';
		var fromAssignRem = '<%=fromAssignRem%>';
		var fromDeleteRem = '<%=fromDeleteRem%>';
		var isLimitedAdmin = '<%=isLimitedAdmin%>';
		var agentState = '<%=agentState%>';

		var publicJson = '';
		var loading = false;
		var firstTime = true;
		var vmComponentWindow = '';
		var noRmtAgtSelected ='<egui:intnl text="admin.agents.Please choose the agent to view the VM details" />';
		var noVmDetailsMsg ='<egui:intnl text="admin.agents.No data available" />';
		var selectOneMsg ='<egui:intnl text="admin.agents.Please choose a remote agent"/>';
		var alreadyLoading ='<egui:intnl text="admin.agents.Already Loading a file"/>';

		var pageName1 = '<egui:intnl text="admin.agents.VM Component view logs"/>';
		var pageName2 = '<egui:intnl text="admin.common.VM Statistics"/>';
		var label1 = '<egui:intnl text="admin.agents.Agent"/>';
		var label2 = '<egui:intnl text="admin.agents.Refresh"/>';
		var label3 = '<egui:intnl text="admin.agents.Back"/>';
		var label4 = '<egui:intnl text="admin.agents.VM Statistics is being fetched"/>';

		var probsErrorMessage = '<egui:intnl text="admin.error.Problem encountered while loading the user interface"/>';
		var requestTimeOut = '<egui:intnl text="admin.error.Response timed out"/>';
		var errorTitle = '<egui:intnl text="admin.user.Error"/>';
		var noRmtSelected = '<egui:intnl text="admin.common.No remote agent(s) selected"/>';
		var deleteSelected = '<egui:intnl text="admin.group.Are you sure you want to delete the selected remote agent(s)? This change is permanent and this cannot be recovered"/>';

		var selectValidFile = '<egui:intnl text="admin.server.Please upload a valid CSV file"/>';
		var selectCompsListFile = '<egui:intnl text="admin.server.Please upload a valid CSV file"/>';
		var addOrDeleteRmtAgentInBulkTitle = '<egui:intnl text="admin.server.Add/Delete Remote Agents in Bulk"/>';
		var addRmtAgentInBulkTitle = '<egui:intnl text="admin.server.Add Remote Agents in Bulk"/>';
		var deleteRmtAgentInBulkTitle = '<egui:intnl text="admin.server.Delete Remote Agents in Bulk"/>';

		// wizard design starts...
		var fromWizard = '<%=fromWizard%>';
		var homeTitle = '<div>'+rmtPageTitle+' <img src="/final/images/spacer.gif" id="homeTitle" class="iconInfoCircle" style="vertical-align:text-top;position:relative;top:2px"/></div>';
		var assignTitle = '<div>'+assignRmtPageTitle+' <img src="/final/images/spacer.gif" id="assignTitle" class="iconInfoCircle" style="vertical-align:text-top;position:relative;top:2px"/></div>';
		var vmTitle = '<div>'+pageName2+' <img src="/final/images/spacer.gif" id="vmTitle" class="iconInfoCircle" style="vertical-align:text-top;position:relative;top:2px"/></div>';

		var bulkAddOrDeleteTitle = '<egui:intnl text="admin.server.Bulk Agents Add/Delete"/>';
		var selActionLabel = '<egui:intnl text="admin.common.Select action"/>';
		var addLabel = '<egui:intnl text="admin.common.Add"/>';
		var deleteLabel = '<egui:intnl text="admin.common.Delete"/>';
		var downloadlinkLabel = '<egui:intnl text="admin.common.Download sample CSV"/>';
		var uploadbtnLabel = '<egui:intnl text="admin.common.Upload"/>';
		var browsebtnLabel = '<egui:intnl text="admin.common.Browse"/>';
		var ChooseFileLabel = '<egui:intnl text="admin.common.Choose file"/>';
		var nickShareMsg1 = '<egui:intnl text = "admin.server.This nickname is already mapped to an organization"/>';
		var nickShareMsg2 = '<egui:intnl text = "admin.server.Associating a new remote agent to this nick will automatically associate the new remote agent to the organization too"/>';
		var nickShareMsg3 = '<egui:intnl text = "admin.server.Do you want to proceed further?"/>';
		// wizard design ends..

		Ext.onReady(function(){
			Ext.Ajax.extraParams = {"com.eg.CSRF_NONCE" : csrfToken};
			Ext.data.Connection.prototype.extraParams = {"com.eg.CSRF_NONCE" : csrfToken};
			Ext.data.ScriptTagProxy.prototype.extraParams = {"com.eg.CSRF_NONCE" : csrfToken};

			Ext.Ajax.on('requestexception', function (conn, response, options) {
				var errorStr = response.responseText;
				if (errorStr != undefined && errorStr != 'undefined' && errorStr.indexOf("customerror") != -1)
				{
					if (errorStr.indexOf("user-session-invalid") != -1)
					{
						callWindow(1);
					}
					else if (errorStr.indexOf("user-deleted") != -1)
					{
						callWindow(2);
					}
					else if (errorStr.indexOf("user-timedout") != -1)
					{
						callWindow(3);
					}
				}
				else
				{
					if (response.status == 302 || (response.timedout != undefined && response.timedout == true))
					{
						Ext.Msg.show({
							title : errorTitle,
							msg : requestTimeOut +'.',
							buttons : Ext.Msg.OK,
							icon : 'iconError'
						});
					}
					else
					{
						console.log(response.responseText);
						Ext.Msg.show({
							title : errorTitle,
							msg : probsErrorMessage +'.',
							buttons : Ext.Msg.OK,
							icon : 'iconError'
						});
					}
				}
			});
		});

	</script>
	<!-- Application folder -->
	<script type="text/javascript" src="RemoteAgent/app/app.js"></script>
</head>

<body>
<div id="loadmask" class="x-mask-msg x-layer x-mask-msg-default" style="z-index: 19001;left:45%;top:35%;">
	<div class="x-mask-loading" style="position:relative">
		Loading...
	</div>
</div>
</body>

</html>



<%
	if (com.egurkha.util.StringUtils.isNull(fromWizard))
	{
		if(request.getQueryString()!=null)
		{
			session.setAttribute("adminLastAccessedURL","/final/admin/EgRemoteAgents.jsp?"+request.getQueryString());
		}
		else
		{
			session.setAttribute("adminLastAccessedURL","/final/admin/EgRemoteAgents.jsp");
		}
	}
%>