<!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.IniFile" %>
<%@ 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"%>

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

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

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

	String fromWizard = request.getParameter("fromWizard");
	String assignAgent = request.getParameter("eagent");
	String fromAssignExt = request.getParameter("fromAssignExt");
	String fromDeleteExt = request.getParameter("fromDeleteExt");
	String fromPage = request.getParameter("from");
	String agentState = request.getParameter("agentState");

	if(com.egurkha.util.StringUtils.isNull(fromWizard))
	{
		fromWizard = "";
	}

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

	EgExternalInfo extInfo = new EgExternalInfo("manager");
	String[] rmtAgents = extInfo.getAllRemoteAgents();
	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);
	}	

	EgServicesInfo servicesInfo = new EgServicesInfo("manager");
	boolean isAllowHostName = servicesInfo.isAllowHostNames();
	boolean isDouble=EgDbInfo.doubleByteEnabled;
	boolean isClientEmulation = EgInstallInfo.isClientEmulationEnabled;

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

	IniFile iniObject = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_extmondash.ini",true);
	String externalAgentLocationFlag=iniObject.getValue("External_Agent_Location","AgentLocation");

	List ex_varList = new ArrayList();
	String strex_varList = "";

	String [] ex_var=trans.getVariables("Eg_External_Agents");
	if (isLimitedAdmin)
	{
		com.eg.EgUserElementsInfo userElmentsObj = new com.eg.EgUserElementsInfo();
		ArrayList agntsList = userElmentsObj.getExternalAgentsForUser(_user.getLoginID());
		ex_var = (String[])agntsList.toArray(new String[0]);
	}
	if (ex_var != null && ex_var.length > 0)
	{
		ex_varList = Arrays.asList(ex_var);
		strex_varList = ex_varList.toString();
		strex_varList = strex_varList.substring(1,strex_varList.length()-1);
	}
	
	IniFile mgrmiscIni = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_mgrmisc.ini");
	boolean hideDwnldAgent = true;
	String enableDownloadAgent = mgrmiscIni.getValue("MISC_ARGS","enableExternalAgentDownload");
	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;
	//}

	com.eg.IniFile iniUiObj = new com.eg.IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_ui.ini",true);
	String google_map_key = iniUiObj.getValue("ZONE_MAP","google_map_key");

	boolean isMspModel = EgUuidInfo.isMspModel();
%>

<html>
<head>
	<meta charset="utf-8">
	<title>EXTRNAL 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="eg_scripts/eg_map.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 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.external.This host/nick name is used by another external agent"/>';
		var checkDBL = '<egui:intnl text="admin.external.Host name is not allowed in DBL(Chinese,Korean,Japanese),since manager is SingleByte"/>';

		var extPageTitle = '<egui:intnl text="admin.title.External Agent Configuration"/>';
		var downAgent = '<egui:intnl text="admin.title.Download Agent"/>';
		var extPageDesc = '<egui:intnl description="EgExternalAgents.jsp"/>';
		var extAddPageTitle = '<egui:intnl text="admin.title.NEW EXTERNAL AGENT"/>';
		var extModPageTitle = '<egui:intnl text="admin.title.MODIFY EXTERNAL AGENT"/>';
		var extAddModPageDesc = '<egui:intnl description="EgNewExtAgents.jsp"/>';
		var assignExtPageTitle = '<egui:intnl text="admin.title.Assign - External Agents"/>';
		var assignExtPageDesc = '<egui:intnl description="EgConfigExtAgents.jsp"/>';
		var deleteExtPageTitle = '<egui:intnl text="admin.title.DELETE EXTERNAL AGENTS"/>';
		var deleteExtPageDesc = '<egui:intnl description="DeleteExternalAgents.jsp"/>';

		var deleteSingleAgntMsg = "<egui:intnl text='admin.external.Are you sure you want to delete the external agent'/>, ";
		var deleteMsg = '<egui:intnl text="admin.common.Are you sure you want to delete selected external agent(s)"/>?';
		var BckLbl = '<egui:intnl text="admin.common.Back"/>';
		var DeleteLbl = '<egui:intnl text="admin.common.External agents which are not associated to any hosts"/>';
		var zoneLbl = '<egui:intnl text="admin.common.Please enter Zone Location"/>';

		var extAddLbl = '<egui:intnl text="admin.common.Add New Agent"/>';
		var extAssDisLbl = '<egui:intnl text="admin.common.Associate/Dissociate Hosts"/>';
		var extDelLbl = '<egui:intnl text="admin.common.Delete Agents"/>';
		var extDelToolLbl = '<egui:intnl text="admin.common.Delete external agents which are not associated to any hosts"/>';
		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 noExtPrtDesc = '<egui:intnl text="admin.common.No external agents present in the system"/>';
		var noExtFound = '<egui:intnl text="admin.common.No external agent(s) found for your current search"/>';
		var label_Agnt = '<egui:intnl text="admin.server.Nick name"/>';
		var label_HostIP = '<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')
		{
			label_HostIP = '<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 label_ClientEmul = '<egui:intnl text="admin.common.Client emulation"/>';
		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 clienabled1 = '<egui:intnl text="admin.error.Client emulation cannot be enabled for this nick name as this name is associated with a remote agent"/>';
		var sameNameAlert1 = '<egui:intnl text="admin.common.A remote 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 extAgentLbl = '<egui:intnl text = "admin.remote.HOSTS MANAGED BY THIS AGENT"/>';
		var otherExtAgentLbl = '<egui:intnl text = "admin.remote.HOSTS MANAGED BY"/>';

		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 external agent to this nick will automatically associate the new external agent to the organization too"/>';
		var nickShareMsg3 = '<egui:intnl text = "admin.server.Do you want to proceed further?"/>';
		var btnText = '';
		var oldHostIp = '';
		var oldNickName = '';
		var old_emulate = '';
		var ExtIpAgent = '<%=ExtIpAgent%>';
		var rmtListToStr = '<%=strrmtList%>';
		var rmtListStr = rmtListToStr.split(",");
		var intAgentsListToStr = '<%=strIntAgents%>';
		var intAgentsListStr = intAgentsListToStr.split(",");
		var strex_varList = '<%=strex_varList%>';
		var ex_varStr = strex_varList.split(",");
		var fromPage = '<%=fromPage%>';
		var agentState = '<%=agentState%>';

		var extAgentLocationFlag = '<%=externalAgentLocationFlag%>';
		var locationFlag = (extAgentLocationFlag=="true" ||  extAgentLocationFlag=="yes")?false:true;

		var isLimitedAdmin = '<%=isLimitedAdmin%>';
		var isClientEmulation = '<%=isClientEmulation%>';
		var isAllowHostName = '<%=isAllowHostName%>';
		var isDouble = '<%=isDouble%>';
		var appCnt = '';
		var assignAgent = '<%=assignAgent%>';
		var fromAssignExt = '<%=fromAssignExt%>';
		var fromDeleteExt = '<%=fromDeleteExt%>';
		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 noExtSelected = '<egui:intnl text="admin.common.No external agent(s) have been selected"/>';
		var deleteSelected = '<egui:intnl text="admin.group.Are you sure you want to delete the selected external 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 addOrDeleteExtAgentInBulkTitle = '<egui:intnl text="admin.server.Add/Delete External Agents in Bulk"/>';
		var addExtAgentInBulkTitle = '<egui:intnl text="admin.server.Add External Agents in Bulk"/>';
		var deleteExtAgentInBulkTitle = '<egui:intnl text="admin.server.Delete External Agents in Bulk"/>';

		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"/>';

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


		// wizard design ends..
		var google_map_key = '<%=google_map_key%>';
		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="ExternalAgent/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/EgExternalAgents.jsp?"+request.getQueryString());
		}
		else
		{
			session.setAttribute("adminLastAccessedURL","/final/admin/EgExternalAgents.jsp");
		}
	}
%>

