// All functions defined in this file assume that the Attachments wiper object
// is already instantiated and assigned to the whaleCacheClean variable - 
// either using HTML ( <OBJECT id=whaleCacheClean ...) or using javascript ( whaleCacheClean = new ActiveXObject("...") )

// The functions used to initialize the Attachment Wiper are no longer called from the login page,
// and cannot be activated directly (only the Endpoint Detection module calls them!).

// The 2 functions below may be used to trigger the Attachments wiper
// cache cleanup functionality.
// ActivateCacheClean() redirects browser to the logoutURL
// ActivateCacheCleanDontSurf() leaves the browser on its current location

//after opening a modal dialog on IE5 there is a problem with location.replace - therefore in this case using location.href=
function ActivateCacheClean(logoutURL, isReplaceProblem)
{
    try 
	{		
		if (whlClientMode == 2)
		{
			if(typeof(whaleCacheClean) == "object")
			{
				whaleCacheClean.SetSessionTime(window.location.hostname, 0);
			}
		}
		else if (whlClientMode == 1)
		{
			SetKeepAlive(0);
		}
		
		if(typeof(logoutURL) == "string")
		{
			if (window.parent.document.images)
			{
				window.parent.document.location.replace(logoutURL);
				
				//in case location.replace failed
				if(isReplaceProblem && isIE5())
				{
					window.parent.document.location.href = logoutURL;
				}
			}
			else
			{
				window.parent.document.location.href = logoutURL;
			}
		}
		else
		{
			alert("Invalid logout URL string");
		}
	}
    catch(e)
	{
		//alert("Exception in ActivateCacheClean(): " + e.description + ", Number " + e.number);
	}
}

function ActivateCacheCleanDontSurf()
{
    //function is called only from LogoffMsg.asp in case of ActiveX. No need to handle applet case here.
    try 
	{
		if(typeof(whaleCacheClean) == "object")
		{
			whaleCacheClean.SetSessionTime(window.location.hostname, 0);
		}
	}
    catch(e)
    {
		//alert("Exception in ActivateCacheClean(): " + e.description + ", Number " + e.number);
	}
}

//SetTimeoutForCacheClean(Timeout) schedules the cache cleanup to start no later than in Timeout seconds
function SetTimeoutForCacheClean(timeout)
{
	//Make sure whlClientMode is defined for scheduled cleanup
	if (typeof(whlClientMode) == "undefined")
		whlClientMode = 2;

	if (whlClientMode == 2)
	{
		try 
		{
			if(typeof(whaleCacheClean) == "object")
			{
				whaleCacheClean.SetSessionTime(window.top.location.hostname, timeout);
			}
		}
		catch(e)
		{
			//alert("Exception in SetTimeoutForCacheClean(): " + e.description + ", Number " + e.number);
		}
	}
	else if (whlClientMode == 1)
	{
		SetKeepAlive(timeout)
	}	
}

// CleanHistoryOnExit defines the Attachment wiper's behavior regarding the 
// Browser History, Web Address Autocomplete and Forms Autocomplete
// flags may be either 0 or combination (OR) of the following values:
// 1 - clear the browser history
// 2 - clear the web address autocomplete (the address bar drop-down box) and IntelliForms
// 4 - clear forms autocomplete (the drop-down boxes in the INPUT html elements) and cached passwords
// 8 - clear all fields that are saved by Forms AutoComplete, except for Cached Password, which are cleared by flag 4
function CleanHistoryOnExit(flags)
{
    try 
	{
		if(typeof(whaleCacheClean) == "object")
		{
			var rc = whaleCacheClean.CanClearHistory;
			if(rc)whaleCacheClean.ClearHistoryOnExit = flags & rc;
			else whaleCacheClean.ClearHistoryOnExit = (flags & 2); //we always can clean autocomplete addresses
		}
	}
    catch(e)
	{
		//alert("Exception in CleanHistoryOnExit(): " + e.description + ", Number " + e.number);
	}
}

function GetCacheCleanInstance()
{
	if(typeof(navigator) == "object")
	{
		var posBrowser = navigator.appName.indexOf("Explorer");
		var posMSIE = navigator.appVersion.indexOf("MSIE");
		var posVersion = 0;

		if (posMSIE != -1)
		{
			posVersion = navigator.appVersion.charAt(navigator.appVersion.indexOf("MSIE") + 5);
		}
		if(posBrowser != -1 && posVersion >= 5 && navigator.platform != "Win64")
		{
			try 
			{
				whaleCacheClean = new ActiveXObject("WhlCache.Scramble.6");
			}
			catch(e)
			{
				//alert("Exception in GetCacheCleanInstance(): " + e.description + ", Number " + e.number);
			}
		}
	}
}

function SetAllowClean()
{
    try 
	{
        if(typeof(whaleCacheClean) == "object")
		{
            whaleCacheClean.AllowClean(window.top.location.hostname);
		}
    }
    catch(e)
	{
		//alert("Exception in SetAllowClean(): " + e.description + ", Number " + e.number);
	}
}

//This function check if the browser type is IE5, and handle a problem occured after opening a modalWindow
//(in case a session timeout occured) - it's impossible to use location.replace() on IE5
function isIE5()
{	
	var ieVersion = window.navigator.appVersion;
	var msieIndex = ieVersion.indexOf("MSIE");
	if (parseFloat(msieIndex) != -1)//it's MSIE
	{	
		var beginStr = msieIndex + 5;
		var ieVersionNumber = parseFloat(ieVersion.substr(beginStr, 4));

		if (ieVersionNumber < 5.5)
			return true;
	}
	return false;
}

//Adding applet to page
//In case wipeType is -1 parameter is not added to applet
function DrawAWApplet(activationType,timeOut,wipeType,siteName,bSecure)
{
	if (getCookie("WhlInstall") != "False")	
	{
		var sDomain = location.host;
		var sDomainForInstall = sDomain.replace(/\./gi,"");
		sDomainForInstall = sDomainForInstall.replace(/\:/gi,"");
		var sInstallDir = "Forefront UAG Remote Access Agent/" + sDomainForInstall + "/" + siteName + bSecure;

		var sAppletObj = '<applet id="RemoteAccessAgent" name="Forefront UAG Remote Access Agent" codebase="/InternalSite/applet" code="com.opswat.agent.AgentAppletDriver.class" width=0 height=0 archive="MicrosoftClient.jar,OESISLocal.jar,RuntimeElevator.jar" mayscript="true">';
		sAppletObj += '<param name="ActivationType" value="' + activationType + '">';
		sAppletObj += '<param name="SiteName" value="' + siteName + '">';	
		sAppletObj += '<param name="EPinstallDir" value="' + sInstallDir + '">';
		sAppletObj += '<param name="timeout" value="' + timeOut + '">';
		sAppletObj += '<param name="AWdomain" value="' + sDomain + '">';
		if (wipeType > -1)
			sAppletObj += '<param name="AWWipeType" value="' + wipeType + '">';
		sAppletObj += '<param name="SecureMode" value="' + bSecure+ '">';
		sAppletObj += '<param name="vbInitialDetect" value="' + 'false' + '">';
		sAppletObj += '<param name="vbUseNAP" value="' + 'false' + '">';
		sAppletObj += '<param name="vbLeaveThreadRunning" value="' + 'false' + '">';
		sAppletObj += '</applet>';
		
		document.getElementById("appletSpan").innerHTML = sAppletObj;
	}
}

//Set keep-alive value (seconds) to applet.
//In case applet does not exist it is first created.
function SetKeepAlive(timeout)
{
	if (getCookie("WhlInstall") != "False")	
	{
		var oApplet = document.getElementById("RemoteAccessAgent");
		if (oApplet)
			oApplet.keepAlive(timeout);
		else
		{
			if (document.getElementById("appletSpan") == null)
			{
				var newSpan = document.createElement('span');
				newSpan.setAttribute('id',"appletSpan");
				document.body.appendChild(newSpan);
			}
			//siteName and secure params exist here from logoffParams.asp
			DrawAWApplet("keep-alive",timeout,-1,siteName,secure);
		}
	}
}
