'-----------------------------------------------------------------
' Copyright (c) Network Associates Corporation
' Free Scan Web page VB script
'-----------------------------------------------------------------
' Author: Ramy Eldesoky
' Created: 10/14/2002
'-----------------------------------------------------------------

option explicit

'---- Scan status values
Const MCVS_ENGINE_LOAD_FAILED = 1
Const MCVS_SCAN_FAILED        = 2
Const MCVS_INVALID_SCAN_PARAM = 3
Const MCVS_SCAN_PENDING       = 4
Const MCVS_SCAN_COMPLETED     =	5
Const SCAN_STATUS_READY       =	5
Const MCVS_SCAN_INPROGRESS    = 6
Const MCVS_STOPSCAN_INPROGRESS= 7
Const MCVS_INVALID_PATH		  = 8

'----- Scan Type Enum
Const MCFSTYPE_FULL		= 0
Const MCFSTYPE_QUICK	= 1
Const MCFSTYPE_SELECT	= 2

'--------- GetSpecialFolderLocation Enum
Const MCFS_MYDOCUMENTS = &H0005
Const MCFS_WINDOWS     = &H0024

Const ACTION_SCANNING		= 0
Const ACTION_VIRUS_FOUND	= 1
Const ACTION_SCAN_CANCELED  = 2
Const ACTION_NO_VIRUS_FOUND	= 3

Const INFO_DISABLE_OTHER_AV = 0
Const INFO_ENABLE_OTHER_AV  = 1
Const INFO_MAX_FILE_REACHED = 2

Const HREF_1 = "<a class=hlink onclick='openBuyNowURL()'>"
Const HREF_2 = "<a class=hlink onclick='openVirusGlossaryURL()'>"
Const HREF_3 = "<a class=hlink onclick='openMoreInfoURL()'>"

dim gScannedFilesCount
dim gFoundVirusesCount
dim strInfectedFile, strInfectedFiles
dim gMaxFilesFound
dim gCurrFactoidIndex
dim gTimerID
dim gScanCancelled
'---------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function openURL(sURL) 
'{
	dim lpage
	set lpage = window.open(sURL,"LPage","")		
	call lpage.focus()
'}
end function
'---------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function openVirusGlossaryURL() 
'{
	openURL strVirusGlossaryURL
'}
end function
'---------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function openBuyNowURL()
'{
	dim sURL
	
	if gFoundVirusesCount > 0 then
		sURL = strBuyNowURL_VirusFound
	elseif gScanCancelled then
		sURL = strBuyNowURL_CancelScan
	else
		sURL = strBuyNowURL_NoVirusFound
	end if
	
	openURL sURL
'}
end function
'---------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function openMoreInfoURL()
'{
	dim sURL
	
	if gFoundVirusesCount > 0 then
		sURL = strMoreInfoURL_VirusFound
	elseif gScanCancelled then
		sURL = strMoreInfoURL_CancelScan
	else
		sURL = strMoreInfoURL_NoVirusFound
	end if
	
	openURL sURL
'}
end function
'---------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function openCampaignURL( nProductID )
'{
	dim sURL
	
  sURL = strCampaignURL & nProductID
	openURL sURL
'}
end function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function CheckActiveX
'{
	dim todayValue, lastCheckDate, latestVersion, currentVersion
	dim obj
	
	CheckActiveX = false

	todayValue = CStr(Year(Now)) & CStr(Month(Now)) & CStr(Day(Now))

	On Error Resume Next

	Set obj = nothing
	set obj = CreateObject("McFreeScan.CoMcFreeScan.1")
	
	If obj is nothing Then
		' Redirect to the server to check if the latest version of the component is already installed
		window.location.replace strFreeScanUpdateServer & "?targeturl=" & Escape(split(document.location.href,"?")(0)) & "&version=0&date=" & todayValue
		exit function
	else
		currentVersion = obj.FileVersion

		If Err.Number Then
			set obj = nothing
			' Redirect to the server to check if the latest version of the component is already installed
			window.location.replace strFreeScanUpdateServer & "?targeturl=" & Escape(split(document.location.href,"?")(0)) & "&version=0&date=" & todayValue
			exit function
		End If

		latestVersion = getParamValue( "version" )

		If currentVersion <> latestVersion Then
			set obj = nothing
			' Redirect to the server to check if the latest version of the component is already installed
			window.location.replace strFreeScanUpdateServer & "?targeturl=" & Escape(split(document.location.href,"?")(0)) & "&version=" & currentVersion & "&date=" & todayValue
			exit function
		End If

		set obj = nothing
	end if

	lastCheckDate = getParamValue("checkdate")
	
	if lastCheckDate <> todayValue then 
		' Redirect to the server to check if the latest version of the component is already installed
		window.location.replace strFreeScanUpdateServer & "?targeturl=" & Escape(split(document.location.href,"?")(0)) & "&version=" & currentVersion & "&date=" & todayValue
		exit function
	end if
	
	CheckActiveX = true ' passed.. ActiveX component is installed and working properly
'}
end function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function objMcFreeScan_OnScanningMemory
'{
	'LogEvent "Scanning Memory..."
'}
End Function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function objMcFreeScan_OnMemoryScanCompleted(bInfected, strVirusName, bContinue)
'{
	bContinue = true

	if bInfected then 
		bContinue = false
	end if
'}
End Function

'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function objMcFreeScan_OnScanningBoot(sDriveName)
	'msgbox "Scannin boot sector"
end function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function objMcFreeScan_OnVirusFound (sFileName, sVirusName)	
'{
  '// Make sure we bail out the moment we find MAX_INFECTED_FILES
	if gFoundVirusesCount > MAX_INFECTED_FILES then
		Exit Function
	end if

	gFoundVirusesCount = gFoundVirusesCount + 1
	
	if gFoundVirusesCount = MAX_INFECTED_FILES then
		window.setTimeout "ForceStopScan()", 1
	end if
	
	strInfectedFile = "<tr><td class=normal valign=top>" & trimFilePath(sFileName,50) & "</td><td class=normal valign=top><a class=hlink onclick='onClickVirusName(""" & sVirusName & """)'>" & sVirusName  & "</a></td></tr>"
	strInfectedFiles = strInfectedFiles & strInfectedFile

	txtInfected.InnerHTML = "<b>" & strLabelInfectedFiles & ":</b> " & gFoundVirusesCount
	txtLog.InnerHTML = StartTextLog() & strInfectedFiles & EndTextLog()

	callToAction (ACTION_VIRUS_FOUND)
'}
end Function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function objMcFreeScan_OnScanningFile(bstrFileName)	
'{
	gScannedFilesCount = gScannedFilesCount + 1
	txtFiles.InnerHTML = "<b>" & strLabelScannedFiles & ":</b> " & gScannedFilesCount
	txtInformation.InnerHTML = "<b>" & strLabelScanning & ":</b> " & GetHtmlFriendlyPath (trimFilePath(bstrFileName,100))
'}
end Function		
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function objMcFreeScan_OnScanCancelled()
'{
	if gTimerID then
		window.clearTimeout gTimerID
		gTimerID = 0
	end if

	gScanCancelled = true

	If gFoundVirusesCount = 0 Then
		callToAction (ACTION_SCAN_CANCELED)
	End If

	if 	gMaxFilesFound then
		showAVInfoMessage INFO_MAX_FILE_REACHED,false
	else
		showAVInfoMessage INFO_ENABLE_OTHER_AV,false
	end if
	
	if gMaxFilesFound = true then
		txtInformation.InnerHTML = "<b>" & strInformation & ":</b> " & strScanCompleted 'sMsgMaxInfectedFilesFound
	else
		txtInformation.InnerHTML = "<b>" & strInformation & ":</b> " & strScanCanceled 
	end if

	document.images("progress").src ="images/progress1.gif"
	idScantype.disabled = false
	idButtonScan.src = "images/scanbutton.gif"
'}
end Function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function objMcFreeScan_OnScanCompleted(status)
'{
	If gFoundVirusesCount > 0 Then
		callToAction(ACTION_VIRUS_FOUND)
	else
		callToAction(ACTION_NO_VIRUS_FOUND)
	End If

	showAVInfoMessage INFO_ENABLE_OTHER_AV,false

	txtInformation.innerHTML = "<b>" & strInformation & ":</b> " & strScanCompleted

	document.images("progress").src ="images/progress1.gif"

'	txtButton.innerHTML = "<img src=images/scanbutton.gif width=90 height=21 border=0>"
	idButtonScan.src = "images/scanbutton.gif"

	idScantype.disabled = false
'}
end Function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function ForceStopScan
'{
	gMaxFilesFound = true
	call stopScan(false)
	'msgbox sMsgMaxInfectedFilesFound, vbOKonly, strPageTitle
'}
end function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function stopScan(bStopCallback)
'{
	on error resume next
	call objMcFreeScan.StopScan2 (bStopCallback)

	'if bStopCallback then
		'document.images("progress").src ="images/progress1.gif"
		'idScantype.disabled = false
		'idButtonScan.src = "images/scanbutton.gif"
	'end if
	
	gCurrFactoidIndex =0
	
'}
end Function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function callToAction (status)
'{
	dim strDetailsMsg, txtCallToAction, strFilesInfected
	
	Select case status 
	'{
	case ACTION_VIRUS_FOUND

		If gFoundVirusesCount = 1 Then
			strFilesInfected = strFile
		Else
			strFilesInfected = strFiles
		End If

		txtCallToAction  = Replace (strAlertVirusesDetected, varFiles, strFilesInfected)
		txtCallToAction  = Replace (txtCallToAction, varVirusCount, gFoundVirusesCount)

	case ACTION_SCAN_CANCELED

		txtCallToAction  = strAlertScanCancelled

	case ACTION_NO_VIRUS_FOUND
		txtCallToAction  = strAlertNoVirusFound
	'}	
	End select
	
	txtCallToAction = InsertHREFs (txtCallToAction)
	txtCallToAction = Replace (txtCallToAction, "<p>", "<div style='height:10px;font-size:1pt'></div>")

	idActionMessage.style.display = ""
	idActionMessage.InnerHTML = txtCallToAction

'}
End Function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function RotateAVInfoMessage (num)
'{
	showAVInfoMessage num,true
	
	if IsScanRunning() then
		gTimerID = window.setTimeout ("RotateAVInfoMessage(-1)", gRotatingFactsSpeed) ' -1 is sent so that the main message will not be changed
	end if
'}
End function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
Function showAVInfoMessage (num, rotator_call)
'{
	dim txtAVInfoMessage, strDetailsMsg
	
	if num >= LBound(strAVInfoMessage) and num <= UBound(strAVInfoMessage) then
		txtAVInfoMessage = strAVInfoMessage(num) ' new message is required
	else
		txtAVInfoMessage = strFactoids(0) ' same message or just rotation 
	end if

	if IsScanRunning() then
	'{
		if strFactoids(0) = txtAVInfoMessage then
		'{ same main message, just rotate
			if rotator_call then ' don't rotate uless called from the rotator thread
			'{
				txtAVInfoMessage = strFactoids(gCurrFactoidIndex) ' read the current rotated message to display

				if gCurrFactoidIndex <> 0 then txtAVInfoMessage = "<font color=black>" & txtAVInfoMessage & "</font>"

				if gCurrFactoidIndex = UBound(strFactoids) then gCurrFactoidIndex = 1 else	gCurrFactoidIndex = gCurrFactoidIndex + 1
			'}
			end if
		'}
		else
		'{ new message, update the main message and restart the rotation
			strFactoids(0) = txtAVInfoMessage ' save the new message at the header message in the rotation list

			'Go back to the main message
			'gCurrFactoidIndex = 0
		'}
		end if

		'if rotator_call = false then exit function ' interruption messages during rotation would not show up immediately, it will wait until next rotator call
	'}
	else
	'{
		txtAVInfoMessage = InsertHREFs (txtAVInfoMessage)
	'}
	end if
	

	idAVInfoMessage.InnerHTML = txtAVInfoMessage
	idAVInfoMessage.style.display = ""
'}
End function
'-----------------------------------------------------------------
'
'-----------------------------------------------------------------
function startScan()
'{
	dim sLocation
	
	gMaxFilesFound = false

	gScannedFilesCount = 0
	gFoundVirusesCount = 0
	gScanCancelled = false

	txtLog.innerHTML      = InitTextLog()

	strInfectedFile       = ""
	strInfectedFiles      = ""
	idActionMessage.style.display = "none"
	idAVInfoMessage.style.display = "none"
	
	txtInfected.innerHTML = "<b>" & strLabelInfectedFiles & ":</b> 0"
	'txtButton.innerHTML   = "<img src=images/cancelbutton.gif width=90 height=21 border=0>"
	idButtonScan.src = "images/cancelbutton.gif"


	document.images("progress").src ="images/progressanimation_small.gif"

	idScantype.disabled = true

	gCurrFactoidIndex=1
	
	call RotateAVInfoMessage (-1)
	
	select case idScantype.selectedIndex
	'{
	case 0: ' c: drive
		sLocation = "C:"

	case 1: ' My Documents
		sLocation = objMcFreeScan.GetSpecialFolderLocation(	MCFS_MYDOCUMENTS )

	case 2: ' Windows Files
		sLocation = objMcFreeScan.GetSpecialFolderLocation(	MCFS_WINDOWS )
	'}
	end select
	
	objMcFreeScan.ScanType = MCFSTYPE_SELECT

	objMcFreeScan.ScanParam = sLocation + "\"
	
	'msgbox objMcFreeScan.FileVersion
	
	objMcFreeScan.Scan	
'}
end function

'----------------------------------------------------------------------------
' Insert <WBR> after the path delimiters in order for the browser to wrap 
' the path in the locations of the delimiters
'----------------------------------------------------------------------------
function GetHtmlFriendlyPath (szFilePath)
'{
	GetHtmlFriendlyPath = Replace ( szFilePath , "\", "\<WBR>" )
	GetHtmlFriendlyPath = Replace ( GetHtmlFriendlyPath , "&", "&amp;<WBR>" ) ' for the Internet Cached files that contains query strings at the end of the file name
'}
end function
'----------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function InitTextLog
	InitTextLog = StartTextLog() + EndTextLog() + "&nbsp;"
end function
'----------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function StartTextLog

StartTextLog="<table cellpadding=1 cellspacing=0 border=0 width=100%>" & _
			"<tr><td class=listheader width=70%>" & strHeaderFileName & "</td><td class=listheader width=30%>" & strHeaderVirusName & "</td></tr>"

end function

'----------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function endTextLog
	endTextLog="</table>"
end function
'---------------------------------------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function onClickVirusName(sVirusName)
'{
	frmVirInfo.searchstring.Value = sVirusName
	frmVirInfo.submit
'}
end function
'---------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
Function getParamValue(paramName)
'{
	dim param, allParams, paramPair, queryString, hrefPair

	getParamValue = ""

	hrefPair = Split(document.location.href, "?")
	
	if UBound(hrefPair) = 0 then exit function ' no query found in the href
	
	queryString = hrefPair(1)
	
	allParams = Split(queryString, "&")
	
	for each param in allParams
	'{
		paramPair = Split (param, "=")
		if paramPair(0) = paramName then
		'{
			if UBound(paramPair) > 0 then
				getParamValue = paramPair(1)
			else
				getParamValue = "true"
			end if
		'}
		end if
	'}
	next
'}
End function
'---------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
Function InsertHREFs (sText)
'{
	dim sRetText
	
	sRetText = Replace (sText,    varHREF_1, HREF_1)
	sRetText = Replace (sRetText, varHREF_2, HREF_2)
	sRetText = Replace (sRetText, varHREF_3, HREF_3)
	
	InsertHREFs = sRetText
'}
end Function
'---------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function HelpPopUp() 
'{
	dim mywnd

	set mywnd = window.open( "McFreeScanHelp.asp", "Help", "toolbar=0,status=0,width=400,height=400,scrollbars=1,resizable=1" )

	mywnd.focus()
'}
end function
'---------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function IsScanRunning()
'{
	IsScanRunning = idScanType.disabled
'}
end function
'---------------------------------------------------------------------------
'
'----------------------------------------------------------------------------
function btnScan_onclick() 
'{
	if IsScanRunning() then 
		call stopScan(false)
	else 
		call startScan()
	end if
'}
end function
