<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="com.eg.EgConMonHelper"%>
<%@ page import="com.eg.EgInstallInfo"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Arrays"%>
<%@ page import="com.egurkha.util.StringUtils"%>
<%
	String tools = request.getParameter("tools");
	String validate_tools = (String)session.getAttribute("validate_tools");
	if(validate_tools != null && validate_tools.equals("true"))
	{
		session.removeAttribute("validate_tools");
		tools = (String)session.getAttribute("tools");
	}
	String pwd = request.getParameter("pwd");
	String highLightNode="";
	String authenticated = (String)session.getAttribute("authenticated");

	String treeViewSrc = (String)session.getAttribute("treeViewSrc");
	session.removeAttribute("treeViewSrc");

	String tool = null;
	String encode_tool =null;
	String is_valid_tool = "false";

	//Directly accessing optional tools with password or coming from login page after authentication
	if (tools != null && !tools.equalsIgnoreCase("null"))
	{
		session.setAttribute("tools",tools);
		ArrayList optionalTools = new ArrayList();
		optionalTools = StringUtils.egTokenizer(tools,",");
		ArrayList result = validateTools(optionalTools);
		if (((Boolean)result.get(0)).booleanValue())
		{
			is_valid_tool = "true";
			encode_tool = com.eg.EgURLEncoder.encode(egurkha.EgUIUtils.encodeSecurityChar(tools),"UTF-8");
			tool = (String)optionalTools.get(0);
			if (tool != null)
			{
				if ((pwd != null && pwd.equals(EgConMonHelper.executePwd)) || (authenticated != null && authenticated.equalsIgnoreCase("true")))
				{
					session.setAttribute("reqFromForTk",tool);
				}
			}
		}
		else
		{
			session.setAttribute("reqFromForTk","ErrorPage");
			session.setAttribute("tk_errorMsg",(String)result.get(1));
		}
	}

	String reqFrom = (String)session.getAttribute("reqFromForTk");
	session.removeAttribute("reqFromForTk");

	if (reqFrom == null || reqFrom.equalsIgnoreCase("null"))
	{
		if (treeViewSrc == null || treeViewSrc.equalsIgnoreCase("null"))
		{
			if (pwd == null || !pwd.equals(EgConMonHelper.executePwd))
			{
				if (tool != null)
				{
					response.sendRedirect(request.getContextPath()+"/toolkitlogin?comeFrom="+tool+"&tools="+tools);
				}
				else
				{
					response.sendRedirect(request.getContextPath()+"/toolkitlogin");
				}
			}
			treeViewSrc = "/final/admin/EgToolKitHomePage.jsp";
			session.setAttribute("authenticated","true");
		}
	}
	else
	{
		if (reqFrom.equals("HomePage"))
		{
			treeViewSrc = "/final/admin/EgToolKitHomePage.jsp";
		}
		else if(reqFrom.equals("ErrorPage"))
		{
			treeViewSrc = "/final/admin/EgToolKitHomePage.jsp?isError=true";
		}
		else if (reqFrom.equals("ConGlance"))
		{
			treeViewSrc = "/final/admin/EgConMon.jsp?comeFrom=ConnMon&sessionInUse=true";
		}
		else if (reqFrom.equals("ConDetails"))
		{
			treeViewSrc = "/final/admin/EgConMon.jsp?details=true&comeFrom=ConnDet&sessionInUse=true";
		}
		else if (reqFrom.equals("ViewData"))
		{
			treeViewSrc = "/final/admin/EgMon2.jsp?comeFrom=View&sessionInUse=true";
		}
		else if (reqFrom.equals("DBUtility"))
		{
			treeViewSrc = "/final/admin/EgDbUtility.jsp?comeFrom=DBUtil&sessionInUse=true";
		}
		else if (reqFrom.equals("DbCleanup"))
		{
			treeViewSrc = "/final/admin/EgDbCleanup.jsp?comeFrom=Cleanup&sessionInUse=true";
		}
		else if (reqFrom.equals("StackTrace"))
		{
			treeViewSrc = "/final/misc/EgStack.jsp?comeFrom=Stack&sessionInUse=true";
		}
		else if (reqFrom.equals("Usage"))
		{
			String license_file = EgInstallInfo.isSuperManager ? "EgSMLicenseTrack.jsp":"EgLicenseTrack.jsp";
			treeViewSrc = "/final/admin/"+license_file+"?comeFrom=License&sessionInUse=true";
		}
		else if (reqFrom.equals("decrypt"))
		{
			treeViewSrc = "/final/admin/EgLicenseReport.jsp?comeFrom=DecryptUtil&sessionInUse=true";
		}
		else if (reqFrom.equals("cscdecrypt"))
		{
			treeViewSrc = "/final/admin/EgCSCLicenseReport.jsp?comeFrom=Csc&sessionInUse=true";
		}
		else if (reqFrom.equals("ConfigFiles"))
		{
			treeViewSrc = "/final/admin/EgConfigFile.jsp?comeFrom=Config&sessionInUse=true";
		}
		else if (reqFrom.equals("ManagerLogs"))
		{
			treeViewSrc = "/final/misc/EgManagerLogView.jsp?comeFrom=ManagerLogs&sessionInUse=true";
		}
		else if (reqFrom.equals("AgentLogs"))
		{
			treeViewSrc = "/final/misc/EgAgentLogView.jsp?comeFrom=AgentLogs&sessionInUse=true";
		}
		else if (reqFrom.equals("AgentCluster"))
		{
			treeViewSrc = "/final/admin/ReportingAgents.jsp?showAgents=true&comeFrom=AgentInfo&sessionInUse=true";
		}
		else if (reqFrom.equals("Configuration"))
		{
			treeViewSrc = "/final/config/ecSchedule.jsp?comeFrom=Schedule&sessionInUse=true";
		}
		else if (reqFrom.equals("Reporter"))
		{
			treeViewSrc = "/final/reporter/Schedule.jsp?comeFrom=Reporter&sessionInUse=true";
		}
		else if (reqFrom.equals("Jdkversion"))
		{
			treeViewSrc = "/final/admin/jdkversion.jsp?comeFrom=Jdk&sessionInUse=true";
		}
		else if (reqFrom.equals("TomcatInfo"))
		{
			treeViewSrc = "/final/admin/TomcatInfo.jsp?comeFrom=TomcatInfo&sessionInUse=true";
		}
		else if (reqFrom.equals("vdiuc"))
		{
			treeViewSrc = "/final/admin/UserCountReport.jsp?comeFrom=VDI&sessionInUse=true";
		}
		else if (reqFrom.equals("aggsupport"))
		{
			treeViewSrc = "/final/misc/defaultAggTests.jsp?comeFrom=Agg&sessionInUse=true";
		}
		else if (reqFrom.equals("UpdateManualConfig"))
		{
			treeViewSrc = "/final/admin/updateManConfig.jsp?comeFrom=UpdateManualConfig&sessionInUse=true";
		}
		else if (reqFrom.equals("MetricsCatalog"))
		{
			treeViewSrc = "/final/admin/metricsCatalog.jsp?comeFrom=MetricsCatalog&sessionInUse=true";
		}
		else if (reqFrom.equals("ViewTopServers"))
		{
			treeViewSrc = "/final/admin/EgViewTopServers.jsp?comeFrom=ViewTopServers&sessionInUse=true";
		}
		else if (reqFrom.equals("ConfigFileDownload"))
		{
			treeViewSrc = "/final/admin/EgConfigFileDownload.jsp?comeFrom=ConfigFileDownload&sessionInUse=true";
		}
		else if (reqFrom.equals("ServiceNames"))
		{
			treeViewSrc = "/final/admin/EgComponentEntityMapping.jsp?comeFrom=ServiceNames&sessionInUse=true";
		}
		else if (reqFrom.equals("decrypttext"))
		{
			treeViewSrc = "/final/admin/EgDecryptText.jsp?comeFrom=decrypttext&sessionInUse=true";
		}
		else if(reqFrom.equals("RestartManager"))
		{
			treeViewSrc ="/final/admin/ManagerRestart.jsp?comeFrom=RestartManager&sessionInUse=true";
		}
		else if (reqFrom.equals("DuplicateIPCheck"))
		{
			treeViewSrc = "../monitor/FindDuplicateIPsOfAgents.jsp?sessionInUse=true&comeFrom=DuplicateIPCheck";
		}
		else if (reqFrom.equals("smpwd"))
		{
			treeViewSrc = "/final/admin/EgChangeSmPwd.jsp?comeFrom=smpwd&sessionInUse=true";
		}
		else if (reqFrom.equals("UploadDiagnosis"))
		{
			treeViewSrc = "/final/admin/EgUploadDiagnosisStatistics.jsp?comeFrom=UploadDiagnosis&sessionInUse=true";
		}
		session.setAttribute("authenticated","true");
	}
%>
<html>
<head>
<meta http-equiv="pragma" content="no-cache" />
<title>
</title>
<LINK href="/final/admin/eg_styles/adminstyle_blue.css" type="text/css" rel="stylesheet">
<script language="javascript" src="/final/admin/eg_scripts/DiagnosisTree.js"></script>
<script language="JavaScript1.2" src="/final/jquery/jquery.js"></script>
<script language="JavaScript">
function noBack()
{
	history.forward();
}
setTimeout(noBack(),0);
window.onunload = function(){null};

function closeMenu()
{
	var menu=document.getElementById("menu");
	menu.style.display='none';
}

function iframeOnLoad()
{
	try
	{
		var viewIframe= document.getElementById("MonitorView").contentWindow.document.body;
		var treeIframe=document.getElementById("MonitorTree").contentWindow.document.body;
		if(treeIframe!=null)
		{
			treeIframe.onclick=closeMenu;
		}
		if(viewIframe!=null)
		{
			viewIframe.onclick = closeMenu;
		}
		divResize();
	}
	catch(Exception)
	{}
}

function iframeOnLoadTree()
{
	var mainIframe = document.getElementById("MonitorTree");
	var treeUrlObj = document.getElementById("treeUrl");
	if (mainIframe != null && mainIframe != undefined && treeUrlObj!=null )
	{
		mainIframe.src = treeUrlObj.value;
	}

	var MonitorView = document.getElementById("MonitorView");
	var infraViewUrl = document.getElementById("infraViewUrl");
	if (MonitorView != null && MonitorView != undefined && infraViewUrl!=null )
	{
		MonitorView.src = infraViewUrl.value;
	}
}

function closeDetailsDiv()
{
	var detailsDiv=document.getElementById("detailsDiv");
	if(detailsDiv!=null)
	{
		detailsDiv.style.display="none";
	}
}

var $j = jQuery.noConflict();

$j(document).ready(function(){
	$j("#expand").click(function(){
		$j(".treeDivMain").toggle();
		divResize();
			var treeDivMainObj=$j(".treeDivMain").css("display");
			if(treeDivMainObj!=null && treeDivMainObj=='none')
			{
				$j(".split_right").attr({src:"/final/admin/eg_images/blue/split_right.gif"});
			}
			else if(treeDivMainObj!=null && treeDivMainObj=='block')
			{
				$j(".split_right").attr({src:"/final/admin/eg_images/blue/split_left.gif"});
			}
	});
	iframeOnLoadTree();
});

//var winHeightFsc = 0;
//var winHeightNsc = 0;
function divResize(){

	var winWidth = window.innerWidth;
	var winHeight = window.innerHeight;

	/*var winWidth = $j(window).width();
	var winHeight = $j(window).height();

	if(!window.screenTop && !window.screenY && winHeightNsc == 0)
	{
		winHeightNsc = winHeight;
	}
	else if(window.screenTop && window.screenY && winHeightFsc == 0)
	{
		winHeightFsc = winHeight;
	}

	if(!window.screenTop && !window.screenY) // normal screen
	{
		winHeight = winHeightNsc;
	}
	else if(window.screenTop && window.screenY) // full screen
	{
		winHeight = winHeightFsc;
	}*/

	$j(".mainDiv").width(winWidth);
	$j(".mainDiv").height(winHeight);

	var treeDivMainObj=$j(".treeDivMain").css("display");
	var infraViewDivObj=$j(".infraViewDiv").css("display");

	if (infraViewDivObj!=null && infraViewDivObj=='block' && treeDivMainObj!=null && treeDivMainObj=='block')
	{
		$j(".treeDivMain").width((winWidth*(20/100))-10);
		$j(".treeDivMain").height(winHeight-11);
		$j(".lrImageDiv").height(winHeight-18);

		$j(".treeDivInner").width((winWidth*(20/100))-12);
		$j(".treeDivInner").height(winHeight-35);

		$j(".infraViewDiv").width((winWidth*(80/100))-10);
		$j(".infraViewDiv").height(winHeight-13);

		$j(".infraViewDivIframe").width((winWidth*(80/100))-10);
		$j(".infraViewDivIframe").height(winHeight-13);

		$j(".mainDiv").css("padding", "6px");

		$j("#dividerImgChange").removeClass('dividerRight').addClass('dividerLeft');
	}
	else if (infraViewDivObj!=null && infraViewDivObj=='block' && treeDivMainObj!=null && treeDivMainObj=='none')
	{
		$j(".infraViewDiv").width(winWidth-15);
		$j(".infraViewDiv").height(winHeight-13);

		$j(".infraViewDivIframe").width(winWidth-15);
		$j(".infraViewDivIframe").height(winHeight-13);

		$j(".mainDiv").css("padding", "6px 6px 6px 0px");

		$j(".lrImageDiv").height(winHeight-18);
		$j("#dividerImgChange").removeClass('dividerLeft').addClass('dividerRight');
	}
}

$j(window).resize(function(){
	divResize();
	iframeOnLoad();
});

function getDetailsDivObj()
{
	return document.getElementById("detailsDiv");
}
</SCRIPT>
</head>
<BODY  style="overflow:hidden" class="innerbg" leftmargin="0px" marginwidth="0px" marginheight="0px">
<input type="hidden" name="treeUrl" id="treeUrl" value="/final/admin/EgToolKitUI.jsp?highLightNode=<%=highLightNode%>&reqFrom=<%=reqFrom%>&tools=<%=encode_tool%>&isValidTool=<%=is_valid_tool%>" style="padding:0px;margin:0px"/>
<input type="hidden" name="infraViewUrl" id="infraViewUrl" value="<%=treeViewSrc%>"/>

<div id="menu" style="position:absolute;width:40px;height:20px;visibility:visible;display:none;z-index:1000;">
</div>

<div style="width:900px;height:540px;padding:6px;border:0px solid red;display:inline-block" class="mainDiv">

	<div class="treeDivMain" style="float:left;width:200px;height:500px;border:0px solid green;display:block;position:relative;padding:0px;margin:0px;">
 		<div class="eg_subhead" width="100%" style="display:inline;position:relative;border:0px solid blue">
			<table width="100%" cellspacing="0" cellpadding="0" border="0" height="22">
				<tr>
					<td id="subhead_left" width="7"><img src="/final/admin/eg_images/spacer.gif" border="0" width="7"></td>
					<td width="100%" align="center" id="eg_subheadbg" style="border-left-width:0px;border-right-width:0px"> <strong> TOOLKIT</strong></td>
					<td align="right" valign="middle" id="eg_subheadbg">&nbsp;</td>
					<td id="subhead_right" width="7"><img src="/final/admin/eg_images/spacer.gif" border="0" width="7"></td>
				</tr>
			</table>
		</div>
		
		<div class="linesquare eg_whitebg" style="align:left;padding:0px;margin:0px;">
			<iframe id="MonitorTree" class="treeDivInner" style="width:180px;height:500px;overflow-x:auto;overflow-y:auto;" width="180px" height="500px"  FRAMEBORDER="0" marginheight="0px" scrolling="auto" marginwidth="0px" onload="javascript:iframeOnLoad();"></iframe>
		</div>
	</div>

	<div class="lrImageDiv" style="border:0px solid blue;width:6px;float:left;height:500px">
		<div style="position:relative;top:45%"><img id="expand" src="/final/admin/eg_images/blue/split_left.gif" width="6px" height="50px" border="0"  class="split_right mouse" /></div>
	</div>

	<div class="infraViewDiv linesquare eg_whitebg" style="float:left;width:600px;height:500px;display:block;position:relative;">
		 <iframe id="MonitorView" class="infraViewDivIframe" style="width:600px;height:500px;overflow-x:auto;overflow-y:auto;"  width="600px" height="500px"  FRAMEBORDER="0" marginheight="0px" scrolling="auto" marginwidth="0px" ></iframe>
	</div>
</div>

<!-- This div for highlight the node by process  -->
<div id="treeSelectionDiv" name="treeSelectionDiv" style="display:none"></div>
</BODY>
</HTML>

<%!
	private ArrayList validateTools(ArrayList optionalTools)
	{
		ArrayList list = new ArrayList();
		list.add(new Boolean(true));
		if(optionalTools == null || optionalTools.isEmpty())
		{
			list.set(0,new Boolean(false));
			list.add("Option should not be blank!");
			return list;
		}
		String[] tools = new String[]{"decrypt","cscdecrypt","vdiuc","aggsupport","decrypttext","smpwd"};
		ArrayList sm_invalid_opts = new ArrayList(Arrays.asList(new String[]{"cscdecrypt","vdiuc","aggsupport"}));
		ArrayList valid_options = new ArrayList(Arrays.asList(tools));
		for(int i=0;i<optionalTools.size();i++)
		{
			String tool = (String)optionalTools.get(i);
			if(EgInstallInfo.isSuperManager && sm_invalid_opts.contains(tool))
			{
				list.set(0,new Boolean(false));
				list.add("eG SuperManager toolkit does not support this option.");
				return list;
			}
			else if(!EgInstallInfo.isSuperManager && tool.equals("smpwd"))
			{
				list.set(0,new Boolean(false));
				list.add("eG Manager toolkit does not support this option.");
				return list;
			}
			else if(!valid_options.contains(tool))
			{
				list.set(0,new Boolean(false));
				list.add("Please enter the valid option.");
				return list;
			}
		}
		return list;
	}
%>



