var timerID = 0;
var timerID2 = 0;
var KeepOptions = false;
var NodeLinkID = 0;
var MenuActivated = false;
var ConnectionString = "";
var option_bg = '#C0C0C0';
var alt_option_bg = '#FFFF00';
var option_sel = '#000080';
var option_sel_txt = '#FFFFFF';
var option_unsel_txt = '#000000';
var link_unsel_txt = '#0000FF';
var focustimer = 0;
var continuefocus = true;
var gotfocus = false;
var SetTab = false;
var SetTabTo = "";
var TabTime = 5; //used to put a slight delay in tabbing since some browsers are faster then others

//function is used when a keypress is done
//we are only interested in tab and enter keys
function register(e)
{
	if (!e) e = window.event;
	
	//Tab Key is Pressed
	if (e['keyCode'] == 9 && e.shiftKey == true) {
		if (SetTab && SetTabBack != "") {
			setFocusFieldBack(SetTabBack);
			SetTab = false;
			SetTabBack = "";
		}
	}
	else if (e['keyCode'] == 9) { 
		if (SetTab && SetTabTo != "") {
			setFocusField(SetTabTo);
			SetTab = false;
			SetTabTo = "";
		}
	}

	//Enter Key is Pressed
	if (e['keyCode'] == 13) {
		checklist();
	}
}

//Continue to set the focus on the focusitem till the continuefocus equals false
function SetStartFocus(focusitem) {
	clearTimeout(focustimer);
	if (continuefocus) {
		document.getElementById(focusitem).focus();
		focustimer = setTimeout("SetStartFocus('" + focusitem + "');",1000);
	}
}

//Check if the gotfocus is true, if it is then stop the continuefocus
function checkfocus() {
	if (gotfocus) {
		continuefocus = false;
	}
}

//Activate when user enters a keypressevent to set where the Tab will go
function KeyPressEvent(focusitemnext, focusitemback) {	
	SetTab = true;
	SetTabTo = focusitemnext;
	SetTabBack = focusitemback;
}

//Depending on what the user set the Next FocusField to it may send it to somewhere else
function setFocusField(focusitem)
{
	var obj;
	
	if (focusitem == "ShowDomain") {
		if (document.getElementById("ShowDomain").value == "  Enter Field  ")
			setTimeout('document.getElementById("ShowDomain2").focus();',TabTime);
		else
			setTimeout('document.getElementById("ShowDomain1").focus();',TabTime);
	}
	else if(focusitem == "ShowAuth") {
		if (document.getElementById("ShowAuth").value == "  Enter Field  ")
			setTimeout('document.getElementById("ShowAuth2").focus();',TabTime);
		else
			setTimeout('document.getElementById("ShowAuth1").focus();',TabTime);
	}
	else if(focusitem == "ShowDomain_") {
		obj = document.getElementById("ShowDomain");
		if (obj)
			setTimeout('document.getElementById("ShowDomain").focus();',TabTime);
		else {
			setTimeout('document.getElementById("ShowDomain1").focus();',TabTime);
		}
	}
	else if(focusitem == "ShowAuth_") {
		obj = document.getElementById("ShowAuth");
		if (obj)
			setTimeout('document.getElementById("ShowAuth").focus();',TabTime);
		else {
			setTimeout('document.getElementById("ShowAuth1").focus();',TabTime);
		}
	}
	else
		setTimeout('document.getElementById("' + focusitem + '").focus();',TabTime);

}

//Depending on what the user set the Back FocusField to it may send it to somewhere else
function setFocusFieldBack(focusitem)
{
	var obj;
	
	if (focusitem == "ShowDomain") {
		obj = document.getElementById("ShowDomain");
		if (obj) {
			if (document.getElementById("ShowDomain").value == "  Enter Field  ")
				setTimeout('document.getElementById("ShowDomain2").focus();',TabTime);
			else
				setTimeout('document.getElementById("ShowDomain1").focus();',TabTime);
		}
		else
			setTimeout('document.getElementById("ShowDomain1").focus();',TabTime);
	}
	else if(focusitem == "ShowAuth") {
		obj = document.getElementById("ShowAuth");
		if (obj) {
			if (document.getElementById("ShowAuth").value == "  Enter Field  ")
				setTimeout('document.getElementById("ShowAuth2").focus();',TabTime);
			else
				setTimeout('document.getElementById("ShowAuth1").focus();',TabTime);
		}
		else
			setTimeout('document.getElementById("ShowAuth1").focus();',TabTime);
	}
	else if(focusitem == "ShowDomain_") {
		obj = document.getElementById("ShowDomain");
		if (obj)
			setTimeout('document.getElementById("ShowDomain").focus();',TabTime);
		else
			setTimeout('document.getElementById("password").focus();',TabTime);
	}
	else if(focusitem == "ShowAuth_") {
		obj = document.getElementById("ShowAuth");
		if (obj)
			setTimeout('document.getElementById("ShowAuth").focus();',TabTime);
		else {
			obj = document.getElementById("ShowDomain");
			if (obj) {
				if (document.getElementById("ShowDomain").value == "  Enter Field  ")
					setTimeout('document.getElementById("ShowDomain2").focus();',TabTime);
				else
					setTimeout('document.getElementById("ShowDomain1").focus();',TabTime);
			}
			else
				setTimeout('document.getElementById("ShowDomain1").focus();',TabTime);
		}
	}
	else
		setTimeout('document.getElementById("' + focusitem + '").focus();',TabTime);
}

if (NSbrowser) {
    // Checks to see if its a netscape browser
    //if it is activates the mouse events
	window.captureEvents(Event.MOUSEMOVE);
	//if it is activates the keypress events
	window.captureEvents(Event.KEYPRESS);
}

// Sets the properties of the options box
function GlobalOptionSelect(option)
{
	ResetOptions();
	document.getElementById(option).style.background = option_sel;
	document.getElementById(option).style.color = option_sel_txt;
}

// Sets the properties of the options box
function GlobalOptionUNSelect(option)
{
	ResetOptions();
	if ((option == "ShowAuth" || option == "ShowDomain") && (document.getElementById(option).value == "  Enter Field  "))
		document.getElementById(option).style.background = alt_option_bg;
	else
		document.getElementById(option).style.background = option_bg;
	
	document.getElementById(option).style.color = option_unsel_txt;
}

// Sets the properties of the options box
function GlobalNodeSelect(option)
{
	if (NodeLinkID != 0) {
		KeepOptions = false;
		GlobalNodeUNSelect(NodeLinkID);
	}
	ResetOptions();
	NodeLinkID = option;
	document.getElementById(option).style.background = option_sel;
	document.getElementById(option).style.color = option_sel_txt;
}

// Sets the properties of the options box
function GlobalNodeUNSelect(option)
{
	if (!KeepOptions)
	{
		ResetOptions();
		document.getElementById(option).style.background = option_sel_txt;
		document.getElementById(option).style.color = link_unsel_txt;
	}
	
}
// Sets the properties of the Menu
function GlobalMenuSelect(option)
{
	ResetOptions();
	if (MenuActivated) {
		document.getElementById(option).style.border = 'thin inset transparent';
		AccessMenu(option);
	}
	else
		document.getElementById(option).style.border = 'thin outset transparent';
}

// Sets the properties of the Menu
function GlobalMenuUNSelect(option)
{
	ResetOptions();
	document.getElementById(option).style.border = 'none';
}

// Sets the properties of the Menu
function GlobalMenuSubSelect(option)
{
	ResetOptions();
	document.getElementById(option).style.border = 'thin inset transparent';
}

// Choose which action to do base on user selection
function OptionAction(option)
{
	if (option == "Video1")
		document.DSWebviewApplet.Appliance(ConnectionString,"Launch",0);
	else if (option == "Video2")
		document.DSWebviewApplet.Appliance(ConnectionString,"Launch",5);
	else if (option == "ViewUsers")
		document.DSWebviewApplet.Appliance(ConnectionString,"ShowUsers",0);
	else if (option == "PowerState1")
		document.DSWebviewApplet.Appliance(ConnectionString,"PowerStatus",0);
	else if (option == "Telnet1")
		document.DSWebviewApplet.Appliance(ConnectionString,"Telnet",0);
	else if (option == "Disconnet")
		document.DSWebviewApplet.Appliance(ConnectionString,"DisconnectUser",0);
	else if (option == "Reboot") {
		var reboot = confirm("Are you sure you want to reboot the item?")
		if (reboot == true)
			document.DSWebviewApplet.Appliance(ConnectionString,"Reboot",0);
	}
	else if (option == "Appliance1")
		document.DSWebviewApplet.Appliance(ConnectionString,"FirmwareVer",0);
	else if (option == "EVR15001")
		document.DSWebviewApplet.Appliance(ConnectionString,"EVR1500",0);
	else if (option == "GenericAppliance1")
		document.DSWebviewApplet.Appliance(ConnectionString,"GenericAppliance",0);
	else if (option == "File1") {
		if (enablestatus == 1)
			document.exittologin.action = "dswebinterface.asp?view=3";
		else if (enablestatus == 2)
			document.exittologin.action = "dswebinterface.asp?view=4";
		else if (enablestatus == 3)
			document.exittologin.action = "dswebinterface.asp?view=1";
		else if (enablestatus == 4)
			document.exittologin.action = "dswebinterface.asp?view=2";
		else {
			HideOptions();
			document.exittologin.action = "dswebinterface.asp?logout=1";
		}
		document.exittologin.submit();
		cacheOn();
	}
	else if (option == "File2") {
		HideOptions();
		document.exittologin.action = "dswebinterface.asp?logout=1";
		document.exittologin.submit();
	}
	else if (option == "View1") {
		if (enablestatus == 3 || enablestatus == 4)
			document.exittologin.action = "dswebinterface.asp?view=4";
		else
			document.exittologin.action = "dswebinterface.asp?view=2";
		
		cacheOn();
		document.exittologin.submit();
	}
	else if (option == "View2") {
		if (enablestatus == 3 || enablestatus == 4)
			document.exittologin.action = "dswebinterface.asp?view=3";
		else
			document.exittologin.action = "dswebinterface.asp?view=1";
		
		cacheOn();
		document.exittologin.submit();
	}
	else if (option == "View3") {
		document.exittologin.action = window.location;
		document.exittologin.submit();
		cacheOn();
	}
	else if (option == "Help1") {
		window.open('Help/en/contents.html');
		void('');
	}
	else if (option == "Help2")
		openversionwindow();
	else
		alert('Unknown Command!');
	
	//Unselected it
	GlobalOptionUNSelect(option);
	
	AutoClose();
}

function openversionwindow()
{
	window.open("about.asp","Version","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=320, height=120, top=" + ((document.body.clientHeight-110)/2) + ", left=" + ((document.body.clientWidth-320)/2))
}

// Resets the timer on options if user is cycling thru items
function ResetOptions()
{
	clearTimeout(timerID);
	timerID = setTimeout("AutoClose();",1500);
	
	if (MenuActivated) {
		clearTimeout(timerID2);
		timerID2 = setTimeout("MenuActivated = false;",1500);
	}
}

// Displays Options Box with defined properties
function ShowOptions(x,y,type)
{
	var op_number = 0;
	var CheckPS = "";
	var CheckSU = "";
	var CheckReboot = "";
	var showVU = false;
	
	// Display the number of menu or options
	if (type == "View") {
		// Check to see if its the View options if so mark the correct option with the current display
		if (enablestatus == 1 || enablestatus == 3)
		{
			document.getElementById(type + 2).innerHTML = "<img BORDER='0' SRC='active.gif'> Server List";
			document.getElementById(type + 1).innerHTML = "<img BORDER='0' SRC='notactive.gif'> Topology";
		}
		else
		{
			document.getElementById(type + 2).innerHTML = "<img BORDER='0' SRC='notactive.gif'> Server List";
			document.getElementById(type + 1).innerHTML = "<img BORDER='0' SRC='active.gif'> Topology";
		}
		op_number = 3;
	}
	else if (type == "File"){
		// Check to see if its the View options if so mark the correct option with the current display
		if (enablestatus > 2) {
			document.getElementById(type + 1).innerHTML = "<img BORDER='0' SRC='active.gif'> Enable Port Status";
		}
		else {
			document.getElementById(type + 1).innerHTML = "<img BORDER='0' SRC='notactive.gif'> Enable Port Status";
		}

		op_number = 2;
	}
	else if (type == "Help")
		op_number = 2;
	else {
		//Its not a menu so do option display
		CheckSU = type.substring(0, 1);  //Can be 0,1
		CheckPS = type.substring(1, 2);  //Can be 0,1,2
		type = type.substring(2, type.length)
		CheckReboot = type.substring(0, 6);
		
		if (CheckReboot.indexOf("Reboot") >= 0)
			type = type.substring(6, type.length)
		
		//Check if node is something that can show users
		if (type == "TelnetX" || type == "Video")
			showVU = true;
		
		//Check if the user has rights to see the user
		if ((CheckSU == "1") && (CheckPS == "0"))
			showVU = false;
		
		if (type == "TelnetX")
			type = "Telnet";
		
		if (type == "Video")
			op_number = 2;
		else
			op_number = 1;
		
	}

	//Show the Reboot on Appliances if Users are admins
	if (CheckReboot.indexOf("Reboot") >= 0) {
		document.getElementById("Reboot").style.visibility = 'visible';
		document.getElementById("Reboot").style.top = y;
		document.getElementById("Reboot").style.left = x - 10;
		y += 20
	}

	for(i = 1; i <= op_number; i++) {
		document.getElementById(type + i).style.visibility = 'visible';
		document.getElementById(type + i).style.top = y;
		document.getElementById(type + i).style.left = x - 10;
		y += 20
	}

	//Show the View Connected Users to the right ports
	if (showVU) {
		document.getElementById("ViewUsers").style.visibility = 'visible';
		document.getElementById("ViewUsers").style.top = y;
		document.getElementById("ViewUsers").style.left = x - 10;
		y += 20
		//Show Disconnect to people who are allowed (which are the same ones that can view users)
		document.getElementById("Disconnet").style.visibility = 'visible';
		document.getElementById("Disconnet").style.top = y;
		document.getElementById("Disconnet").style.left = x - 10;
		y += 20
	}

	//Show Power State to people who are allowed
	if (CheckPS == "2" && type != "PowerState") {
		document.getElementById("PowerState1").style.visibility = 'visible';
		document.getElementById("PowerState1").style.top = y;
		document.getElementById("PowerState1").style.left = x - 10;
		y += 20
	}	

	
	// Once the box gets displayed set the timer to keep box open for period of time
	KeepOptions = true;
	timerID = setTimeout("AutoClose();",1500);
}

// Displays the correct text field
function ShowList(type)
{
	// Display the correct field the user wants to use\
	// Changes Auth Server Field
	document.getElementById(type).style.color = option_unsel_txt;
	
	if (type == "ShowAuth") {
		// Check to see if its the View options if so mark the correct option with the current display
		if (document.getElementById(type).value == "  Select List  ")
		{
			document.getElementById(type).value = "  Enter Field  ";
			document.getElementById(type).style.background = alt_option_bg;
			document.getElementById(type + 1).style.visibility = 'hidden';
			document.getElementById(type + 2).style.visibility = 'visible';
		}
		else
		{
			document.getElementById(type).value = "  Select List  ";
			document.getElementById(type).style.background = option_bg;
			document.getElementById(type + 1).style.visibility = 'visible';
			document.getElementById(type + 2).style.visibility = 'hidden';
		}
	}
	else {
		// Changes Domain Field
		if (document.getElementById(type).value == "  Select List  ")
		{
			document.getElementById(type).value = "  Enter Field  ";
			document.getElementById(type).style.background = alt_option_bg;
			document.getElementById(type + 1).style.visibility = 'hidden';
			document.getElementById(type + 2).style.visibility = 'visible';
		}
		else
		{
			document.getElementById(type).value = "  Select List  ";
			document.getElementById(type).style.background = option_bg;
			document.getElementById(type + 1).style.visibility = 'visible';
			document.getElementById(type + 2).style.visibility = 'hidden';
		}
	}
}

// Does a final check on the login form to see which values to use for domain and primaryauth
function checklist()
{
	if (document.getElementById("ShowAuth1").style.visibility == 'visible')
		document.getElementById("ShowAuth3").value = 'primaryauth';
	else
		document.getElementById("ShowAuth3").value = 'alt_primaryauth';
	
	if (document.getElementById("ShowDomain1").style.visibility == 'visible')
		document.getElementById("ShowDomain3").value = 'domain';
	else
		document.getElementById("ShowDomain3").value = 'alt_domain';
	
	document.loginform.submit();
	cacheOn();
}

// Used to immediately close Options box
function AutoClose()
{
	KeepOptions = false;
	HideOptions();
}


function AccessMenu(element)
{
	// Closes any other option box
	AutoClose();
	
	// Find the element position
	var obj = document.getElementById(element);
    var x_value = 0;
    var y_value = 0;
    while (obj) {
        x_value += obj.offsetLeft;
        y_value += obj.offsetTop;
        obj = obj.offsetParent;
    }
    
	x_value += 10;
	y_value += 21;
	
	// Show the submenus
	ShowOptions(x_value,y_value,element);
}

function MenuClick(element)
{
	// Change the click menu
	if (MenuActivated) {
		MenuActivated = false;
		// Closes any other option box
		AutoClose();
	}
	else {
		MenuActivated = true;
		timerID2 = setTimeout("MenuActivated = false;",1500);
	}
	
	GlobalMenuSelect(element);
}

// Count down till display options box when mouse is over node
function AccessOptions(e,nodestring,type)
{
	xOffset = document.body.scrollLeft;
	yOffset = document.body.scrollTop;
	
	if (IEbrowser) {
		x_value = window.event.clientX + xOffset;
		y_value = window.event.clientY + yOffset;
	}
	else {
		x_value = e.pageX;
		y_value = e.pageY;
	}

	// Closes any other option box
	AutoClose();
	
	// Saves the node connection string for the options box
	ConnectionString = nodestring;

	// Calls the option box to open
	timerID = setTimeout("ShowOptions(" + x_value + "," + y_value + ",'" + type + "');",1000);
}

// Hides the option box, verifies that the box isnt open yet, if open it will conditionally stay open
function HideOptions()
{
	if (!KeepOptions)
	{
		clearTimeout(timerID);
		ConnectionString = "";
		document.getElementById('Video1').style.visibility = 'hidden';
		document.getElementById('Video2').style.visibility = 'hidden';
		document.getElementById('ViewUsers').style.visibility = 'hidden';
		document.getElementById('PowerState1').style.visibility = 'hidden';
		document.getElementById('Telnet1').style.visibility = 'hidden';
		document.getElementById('Appliance1').style.visibility = 'hidden';
		document.getElementById('Reboot').style.visibility = 'hidden';
		document.getElementById('Disconnet').style.visibility = 'hidden';
		document.getElementById('EVR15001').style.visibility = 'hidden';
		document.getElementById('GenericAppliance1').style.visibility = 'hidden';
		document.getElementById('File1').style.visibility = 'hidden';
		document.getElementById('File2').style.visibility = 'hidden';
		document.getElementById('View1').style.visibility = 'hidden';
		document.getElementById('View2').style.visibility = 'hidden';
		document.getElementById('View3').style.visibility = 'hidden';
		document.getElementById('Help1').style.visibility = 'hidden';
		document.getElementById('Help2').style.visibility = 'hidden';
	}
}
function cacheOn()
{
	cach.visibility = "visible";
}
function cacheOff()
{
	cach.visibility = "hidden";
}

