var PSEVENT_Error          =  7;
var PSEVENT_ScannerReady   =  8;
var PSEVENT_ScanComplete   =  9;
var PSEVENT_ScanAborted    = 10;
var PSEVENT_RebootRequired = 11;
var PSEVENT_PSUninstalled  = 12;
var PSEVENT_PestFound      = 13;
var PSEVENT_Progress       = 14;
var PSEVENT_Status         = 15;
var PSEVENT_PopupReady     = 16;
var PSEVENT_PrintDocReady  = 17;


var scanInProgress = false;
var scannerReady = false;
var printReady = false;
var pendingMsg = "";
var pendingMsgId = 0;
var xmlReport = null;
var printXSLT = null;

var pestCnt = 0;
var riskCount = new Array( 0, 0, 0, 0 );
var riskImages = new Array( "risktc.jpg", "risklow.jpg", "riskmid.jpg", "riskhi.jpg" ); 
var riskCounters = new Array( 4 );
var pests = new Array();

var MAX_PROGRESS = 4;

function pestscan::OnPSEvent( eventId, data, request )
{
	switch (eventId)
	{
		case PSEVENT_Status :
			delayedStatus( data );
		break;
		case PSEVENT_Error :
		break;
		case PSEVENT_ScannerReady :
			scannerReady = true;
			startBtn.className = "startNormal"
			caption1.innerText = strReadyToStart;
			caption2.innerText = strClickStart;
			introInstall.style.display = "none";
			introScan.style.display = "inline";			
			statusText( "" );
		break;
		case PSEVENT_ScanComplete :
			showProgress( MAX_PROGRESS );
			scanComplete();
		break;
		case PSEVENT_ScanAborted :
			scanComplete();
		break;
		case PSEVENT_RebootRequired :
		break;
		case PSEVENT_PestFound :
			reportPest( data );
		break;
		case PSEVENT_Progress :
			showProgress( new Number(data) );
		break;
		case PSEVENT_PSUninstalled :
		break;
		case PSEVENT_PopupReady :
		break;
		case PSEVENT_PrintDocReady :
			if (printXSLT != null && xmlReport != null)
			{
				data.clear();
				var content = xmlReport.transformNode( printXSLT );
				data.write( content );
				data.close();
			}
		break;
	}
}

function onLoad()
{
	if (activeXinstalled())
	{
		dxobj.InitPS();
		riskCounters[3] = riskHighCnt;
		riskCounters[2] = riskMidCnt;
		riskCounters[1] = riskLowCnt;
		riskCounters[0] = riskTCCnt;
		showProgress( 0 );
	}
	else
	{
		window.setTimeout( "checkForInstall()", 1000 );
	}
}

function checkForInstall()
{
	if (activeXinstalled())	
		window.navigate( "default.aspx" );
	else
		window.setTimeout( "checkForInstall()", 1000 );
}

function activeXinstalled()
{
	try
	{	  
		var obj = new ActiveXObject( "pestscanx.PSFormX" );
		return (obj != null)
	}
	catch(e)
	{
		return false;
	}
}	 

function showProgress( value )
{
	progressBar.style.width = 182*(value/MAX_PROGRESS);
}


function findElement( from, tagName )
{
	if (from.tagName == tagName)
		return from
	else
		if (from.parentElement != null)
			return findElement( from.parentElement, tagName )
		else
			return null;
}

function onShowStatus()
{
	pendingMsgId = 0;
	statusMsg.innerHTML = "<nobr>" + pendingMsg + "</nobr>"
}

function delayedStatus( msg )
{
	if (pendingMsgId != 0)
		window.clearTimeout( pendingMsgId );
	pendingMsg = msg;
	pendingMsgId = window.setTimeout( "onShowStatus()", 10 );
}

function statusText( msg )
{
	if (pendingMsgId != 0)
		window.clearTimeout( pendingMsgId );
	statusMsg.innerHTML = "<nobr>" + msg + "</nobr>"
}

function onStartMouseOver()
{
	if (scannerReady)
	{
		var td = findElement( event.srcElement, "TD" );
		if (td != null)
			if (!scanInProgress)
				td.className = "startHot";
			else
				td.className = "stopHot";
	}
}

function onStartMouseOut()
{
	if (scannerReady)
	{
		var td = findElement( event.srcElement, "TD" );
		if (td != null)
			if (!scanInProgress)
				td.className = "startNormal";
			else
				td.className = "stopNormal";
	}
}

function onStartClick()
{
	if (scannerReady)
	{
		Hourglass.className = "SpanShow";
		Scorecard.className = "SpanHide";
		introScan.style.display = "none";
		main.style.display = "inline";
		outerCBrow.style.display = "none";
		scanInProgress = !scanInProgress;
		if (scanInProgress)
		{
			xmlReport = new ActiveXObject( "MSXML.DOMDocument" );
			xmlReport.async = false;
			xmlReport.loadXML( 
				'<report><scores>' +
				'<summary-total>0</summary-total>' +
				'<summary risk="0">0</summary>' +
				'<summary risk="1">0</summary>' +
				'<summary risk="2">0</summary>' +
				'<summary risk="3">0</summary>' +
				'</scores></report>' );
			riskCount[0] = 0;
			riskCount[1] = 0;
			riskCount[2] = 0;
			riskCount[3] = 0;
			riskCounters[0].innerHTML = "0";
			riskCounters[1].innerHTML = "0";
			riskCounters[2].innerHTML = "0";
			riskCounters[3].innerHTML = "0";
			pestCnt = 0;
			pestCount.innerHTML = "0";
			printReady = false;
			printBtn.className = "printDisabled";
			CB.disabled = true;
			startLabel.innerText = strStop;
			startBtn.className = "stopHot";
			dxobj.sendReport = CB.checked;
			dxobj.Start();
			caption1.innerText = strScanUnderWay;
			caption2.className = "caption2";
			caption2.innerText = strStandBy;
			while (reportTable.children.length > 0)
				reportTable.removeChild( reportTable.children[0] );
			pests = new Array();
		}
		else
		{
			startLabel.innerText = "...";
			dxobj.Stop();
			startLabel.innerText = strStart;
			startBtn.className = "startHot";
			statusText( "" );
			caption1.innerText = strReadyToStart;
			// caption2.innerText = "Click the START button to begin.";
		}
		onStartMouseOver();
	}
}

function onPrintMouseOver()
{
	if (printReady)
	{
		var td = findElement( event.srcElement, "TD" );
		if (td != null)
			if (!scanInProgress)
				td.className = "printHot";
			else
				td.className = "printHot";
	}
}

function onPrintMouseOut()
{
	if (printReady)
	{
		var td = findElement( event.srcElement, "TD" );
		if (td != null)
			if (!scanInProgress)
				td.className = "printNormal";
			else
				td.className = "printNormal";
	}
}

function onPrintClick()
{
	if (printReady)
	{
		printXSLT = new ActiveXObject( "MSXML.DOMDocument" );
		printXSLT.async = false;
		if (printXSLT.load( "print.xslt" ))
		{
			dxobj.createPrintDocument( "about:blank" );
		}
	}
}

function onSendInfoClick()
{
	if (event.srcElement == outerCB)
		CB.checked = outerCB.checked;
}

function toggleLocations()
{
	var tr = findElement( event.srcElement, "TR" );
	if (tr != null)
	{
		var pestId = tr.id;
		var row = document.getElementById( "pestInfo_" + pestId );
		var img = document.getElementById( "expand_" + pestId );
		if (row != null && img != null)
			if (row.style.display == "none")
			{
				row.style.display = "inline";
				img.src = "images/minus.jpg";
				var node = xmlReport.selectSingleNode( "//pest[@pestId='" + pestId + "']" );
				if (node != null)
					node.setAttribute( "display", "inline" );
			}
			else
			{
				row.style.display = "none";
				img.src = "images/plus.jpg";
				var node = xmlReport.selectSingleNode( "//pest[@pestId='" + pestId + "']" );
				if (node != null)
					node.setAttribute( "display", "none" );
			}
	}
}

function expandAll( expand )
{
	for (var i = 0; i < pests.length; i++)
	{
		var pestId = pests[i];
		var row = document.getElementById( "pestInfo_" + pestId );
		var img = document.getElementById( "expand_" + pestId );
		if (row != null && img != null)
			if (expand)
			{
				row.style.display = "inline";
				img.src = "images/minus.jpg";
				var node = xmlReport.selectSingleNode( "//pest[@pestId='" + pestId + "']" );
				if (node != null)
					node.setAttribute( "display", "inline" );
			}
			else
			{
				row.style.display = "none";
				img.src = "images/plus.jpg";
				var node = xmlReport.selectSingleNode( "//pest[@pestId='" + pestId + "']" );
				if (node != null)
					node.setAttribute( "display", "none" );
			}
	}
}

function reportPest( data )
{
	var pestId = new String( data.getAttribute( "pestid" ) );
	var risk = new Number( data.getAttribute( "risk" ) );
	var name = new String( data.getAttribute( "name" ) );
	var category = new String( data.getAttribute( "category" ) );
	if (category == "Tracking Cookie")
		risk = 0;
	var location = data.selectSingleNode( "//location" );
	if (location != null)
		location = new String( location.text );
	else
		location = "";
	var pestRow = document.getElementById( pestId );
	if (pestRow == null)
	{
		// update counters
		pestCnt++;
		pestCount.innerHTML = '<span style="color: red">' + pestCnt + '</span>'

		riskCount[risk]++;
		riskCounters[risk].innerHTML = riskCount[risk];
		
		// add pest to XML report and pest array
		pests[pests.length] = pestId;
		var pestNode = xmlReport.createElement( "pest" );
		xmlReport.documentElement.appendChild( pestNode );
		pestNode.setAttribute( "pestId", pestId );
		pestNode.setAttribute( "name", name );
		pestNode.setAttribute( "category", category );
		pestNode.setAttribute( "risk", risk );
		pestNode.setAttribute( "display", "none" );
		pestNode.setAttribute( "riskImg", riskImages[risk] );
		var summaryNode = xmlReport.selectSingleNode( "//summary[@risk='" + risk + "']" );
		summaryNode.text = riskCount[risk];
		summaryNode = xmlReport.selectSingleNode( "//summary-total" );
		summaryNode.text = pestCnt;

		// insert row for pest in report		
		pestRow = document.createElement( "TR" );
		pestRow.id = pestId;
		pestRow.onclick = "toggleLocations()";
		reportTable.appendChild( pestRow );
		

		// add plus sign			
		var td = document.createElement( "TD" );
		td.className = "reportImgCell";
		td.innerHTML = '<img border="0" id="expand_' + pestId + '" class="expandIcon" src="images/plus.jpg" width="9" height="9" onclick="toggleLocations()"/>';
		pestRow.appendChild( td );

		// add risk icon
		var td = document.createElement( "TD" );
		td.className = "reportRiskCell";
		td.innerHTML = '<img border="0" src="images/' + riskImages[risk] + '" width="6" height="6"/>';
		pestRow.appendChild( td );

		// add pest name
		var td = document.createElement( "TD" );
		td.className = "reportCell";
		td.innerHTML = name;
		pestRow.appendChild( td );
		
		// add pest category
		var td = document.createElement( "TD" );
		td.className = "reportCell";
		td.innerHTML = category;
		pestRow.appendChild( td );
		
		// add pest location info
		var locRow = document.createElement( "TR" );
		locRow.id = "pestInfo_" + pestId;
		locRow.style.display = "none";
		reportTable.appendChild( locRow );
		var td = document.createElement( "TD" );
		td.className = "pestInfoFrame";
		td.colSpan = 14;
		td.style.overflowX = "hidden";
		td.style.textOverflow = "ellipsis";
		td.innerHTML =
			'<div class="pestInfoHeader" style="display: block; width: 100%; overflow-x: hidden; background-image: url(images/infoback.jpg);">' + 
				'<b>' + category + ' "' + name + '"</b> ' + strFoundIn + ':' +
			'</div>' +
			'<div id="pestLoc_' + pestId + '" style="overflow-x: hidden; border-style: solid; border-width: 0px; border-color: red;">' + 
			'</div>' +
			'<div style="text-align: right; margin: 7px; border-style: solid; border-width: 0px; border-color: red;">' + 
				'<span pestId="' + pestId + '" class="moreLink" onClick="showPestInfo()" >' + strMore + '</span>' +
			'</div>';
		locRow.appendChild( td );
	}
	var pestNode = xmlReport.selectSingleNode( "//pest[@pestId='" + pestId + "']" );
	if (pestNode != null)
	{
		var locNode = xmlReport.createElement( "location" );
		var data = xmlReport.createCDATASection( location );
		locNode.appendChild( data );
		pestNode.appendChild( locNode );
	}
	var locations = document.getElementById( "pestLoc_" + pestId );
	var div = document.createElement( "DIV" );
	div.innerHTML = '<li style="margin-left: 30px; margin-right: -50px">' + location + '</li>';
	locations.appendChild( div );
}

function showPestInfo()
{
	var pestId = event.srcElement.pestId;
	var url = "http://www.pestpatrol.com/spywarecenter/pest.aspx?id=" + pestId;
	dxobj.popup( url, 780, 500, "" );
}

function scanComplete()
{
	Hourglass.className = "SpanHide";
	BuyNowLink.className = "SpanHide";
	BuyNowBox.className = "SpanShow";
	Scorecard.className = "SpanShow";
	startLabel.innerText = strStart;
	startBtn.className = "startNormal";
	caption1.innerText = strScanComplete;
	if (pestCnt > 0)
	{
		caption2.className = "caption2SpywareFound";
		caption2.innerText = strSpywareDetected;
	}
	else
	{
		caption2.className = "caption2";
		caption2.innerText = strClickStart;
	}
	scanInProgress = false;
	scannerReady = true;
	statusText( "" );
	showProgress( 0 );
	CB.disabled = false;
	printReady = true;
	printBtn.className = "printNormal";
}

function ClickQuestion1()
{
	if(spQuest1.className== "SpanShow"){
		spQuest1.className = "SpanHide";
	}else{
		spQuest1.className = "SpanShow";
		spQuest2.className = "SpanHide";
		spQuest3.className = "SpanHide";
	}
}
function ClickQuestion2()
{
	if(spQuest2.className== "SpanShow"){
		spQuest2.className = "SpanHide";
	}else{
		spQuest2.className = "SpanShow";
		spQuest3.className = "SpanHide";
		spQuest1.className = "SpanHide";
	}
}
function ClickQuestion3()
{
	if(spQuest3.className== "SpanShow"){
		spQuest3.className = "SpanHide";
	}else{
		spQuest3.className = "SpanShow";
		spQuest1.className = "SpanHide";
		spQuest2.className = "SpanHide";
	}
}
function CloseQuestionAll()
{
		spQuest3.className = "SpanHide";
		spQuest1.className = "SpanHide";
		spQuest2.className = "SpanHide";			
}
	