function JAVAparseServerResponse(raw) {
	// Split by new lines
	raw = raw + ' ';
	if ( raw.length > 1 ) {
		var lines = raw.split(/\r?\n/);
		var mytest = 0;
		var entries = '';
		var color = '';

		// Loop over each entry
		for ( mytest in lines ) {
			if ( lines[mytest].length > 1 ) {
				// Split by tabs
				entries = lines[mytest].split(/\t/);

				// Applications Counter
				ApplicationsCounter++;
				document.getElementById('ApplicationsDetected').innerHTML = ApplicationsCounter + ( ApplicationsCounter != 1 ? sDetectedProgramsPlural : sDetectedProgramsSingular );

				// Colors and counters
				if ( entries[0] == 0 ) {
					color = 'red';

					// Insecure counter
					InsecureCounter++;
					document.getElementById('InsecureDetected').innerHTML = InsecureCounter + ( InsecureCounter != 1 ? sInsecureVersionsPlural : sInsecureVersionsSingular );
				} else {
					// Secure counter
					SecureCounter++;
					document.getElementById('SecureDetected').innerHTML = SecureCounter + ( SecureCounter != 1 ? sPatchedVersionsPlural : sPatchedVersionsSingular );
				}

				ResultTableData[ResultTableDataCounter++] = new Array(entries[7],entries[6],entries[3],entries[4],entries[5],( entries[0] == 0 ? 'red' : ( entries[0] == 3 ? 'black' : 'green' ) ), entries[2], entries[0], entries[8], entries[9]);
			}
		}
	}

	// Rebuild output table
	BuildResultTable();

	return -1;
}

function JAVAcheckScan()
{
	var SecuniaSoftwareInspector = document.getElementById('SecuniaSoftwareInspector');

	// Update Status
	var tmpStr = SecuniaSoftwareInspector.getStatus() + '';
	if ( tmpStr.length > 55 ) {
		tmpStr = tmpStr.substring(0,25) + '...' + tmpStr.substring((tmpStr.length - 25),tmpStr.length);
	}
	document.getElementById('outputStatus').innerHTML = tmpStr;

        // Reminder notify
        if ( !SecuniaSoftwareInspector.isRunning() && tmpStr == 'Detection completed successfully' ) {
                // "no insecure programs" and "bDisplayPatched is true", output success message
//                if ( InsecureCounter == 0 && bDisplayPatched ) {
//                        alert( sCongratulations );
//                }

                // Display reminder
                if ( bDisplayReminder ) {
                        PlaceContentLayer(reminderServiceTitle, reminderServiceContent);
                        ShowLayer('ContentLayer', true, 450);
                        document.getElementById('email').focus();
                }
        }

	// Check if running
	if ( SecuniaSoftwareInspector.isRunning() )
	{
		setTimeout('JAVAcheckScan()', 50);
		document.getElementById('DetectionRunning').innerHTML = SecuniaSoftwareInspector.getRunTime();
	} else {
		// Update buttons
		EnableStartButton();
		DisableStopButton();
		detectionDone = true;

		// Set process icon as being "done"
		document.getElementById('processIcon').src = "/gfx/process_done.gif"
	}

	// Check for errors
	tmpStr = SecuniaSoftwareInspector.getErrors();
	if ( tmpStr != 'empty' ) {
		// Trim errors string
		tmpStr = tmpStr.trim();

		// Split string by newline
		var tmpStrSplit = tmpStr.split("\n");

		// Move to error container
		ErrorsCounter = tmpStrSplit.length;
		ErrorsEncountered = tmpStr;

		// Insert link for viewing the errors
		document.getElementById('ErrorsDetected').innerHTML = '<a href="#" onClick="alert(ErrorsEncountered);">' + ErrorsCounter + ( ErrorsCounter == 1 ? sErrorsDetectedSingular : sErrorsDetectedPlural ) + '</a>';
	}

	// Check to see if there is any output available
	var raw = SecuniaSoftwareInspector.getOutput();

	if( raw == "empty" ) {
		return;
	} else {
		JAVAparseServerResponse(raw);
	}
}

function JAVAstartScan()
{
	// Reset Variables
	{
		// Scan State
		detectionDone = false;
		ScanAborted = false;

		// Detection Counters
		document.getElementById('ApplicationsDetected').innerHTML = sScanStatsPrograms;
		document.getElementById('InsecureDetected').innerHTML = sScanStatsInsecure;
		document.getElementById('SecureDetected').innerHTML = sScanStatsPatched;
		ApplicationsCounter = 0;
		InsecureCounter = 0;
		SecureCounter = 0;

		// Error reporting
		ErrorsEncountered = '';
		ErrorsCounter = 0;
		document.getElementById('ErrorsDetected').innerHTML = sScanNoErrors;
	}

	// Mark as been started
	detectionStarted = true;

	// Clean up the table
	for ( i=0 ; i<ResultTableData.length ; i++ ) {
		if ( document.getElementById("row_" + i) ) {
			document.getElementById("ResultTable").removeChild(document.getElementById("row_" + i));
			document.getElementById("ResultTable").removeChild(document.getElementById("row_t_" + i));
		}
	}
	ResultTableData = new Array();
	tableContaining = new Array();
	ResultTableDataCounter = 0;

	// Update buttons
	DisableStartButton();
	EnableStopButton();

	// Start the scan
	document.getElementById('SecuniaSoftwareInspector').startScan(FullScan, bWithWindowsUpdate);

	// Initialise Check Scan Status
	JAVAcheckScan();
}

function JAVAstopScan()
{
	// Inspection aborted	
	ScanAborted = true;
	DisableStopButton();

	// Send request to applet
	document.getElementById('SecuniaSoftwareInspector').stopScan();

	// Alert
	alert(sScanAborted);

	// Re-enabled start button
        EnableStartButton();
}

function JAVAloadFeedback()
{
	try
	{
		document.getElementById('SecuniaSoftwareInspector').getStatus();
	}
	catch( err )
	{
		document.getElementById('outputStatus').innerHTML = 'Loading Java Applet... Try ' + ++iLoadTryCounter + ' of 50...';

		// Check how many times this has failed
		if ( loadFailedCount++ >= 50 )
		{
			// Problems loading the applet?
			document.getElementById('outputStatus').innerHTML = sLoadProblemHeadline;

			if ( confirm(sLoadProblem) )
			{
				EnableStartButton();
				return 1;
			}
			return -1;
		}

		// Check again
		setTimeout('JAVAloadFeedback()', 500);
		return -1;
	}

	// Enable Start Button
	EnableStartButton();

	document.getElementById('outputStatus').innerHTML = sLoadSuccess;
}
