var debug = false;
function Fade(id, opacity)
{
	element = document.getElementById(id).style;
	element.opacity = (opacity / 100); 
	element.MozOpacity = (opacity / 100); 
	element.KhtmlOpacity = (opacity / 100); 
	element.filter = "alpha(opacity=" + opacity + ")";
}

function BuildResultTable()
{
	if ( ResultTableData.length )
	{
		// Loop over each entry in the ResultTableData array
		for( var i=0; i<ResultTableData.length; i++ )
		{
			if ( !bDisplayPatched || ( bDisplayPatched && ResultTableData[i][7] == 0 ) ) {
				tableAddedRow(i, ResultTableData[i][0], ResultTableData[i][1], ResultTableData[i][2], ResultTableData[i][7], ResultTableData[i][5], ResultTableData[i][9], ResultTableData[i][8], ResultTableData[i][3], ResultTableData[i][6]);
			}
		}
	}

	// Recalculate faded bg height
	if ( fadedBackground == true )
	{
		FadeBackground();
	}

	return 1;
}

function toggleDetails(id, inputImage, inputName, inputVersion, inputStatus, inputColor, inputProductType, inputUpdate, inputVersionSecure, inputPath)
{
	object = document.getElementById(id);

	if ( toggleLinkContainer[id] == true )
	{
		// Fade in
		for ( i=100 ; i>0 ; i-- )
		{
			setTimeout("Fade(" + id + ", " + i + ");", fadeDelay * (100-i) );
		}

		setTimeout("document.getElementById(" + id + ").innerHTML = ''; document.getElementById(" + id + ").style.padding = 0; document.getElementById('cell_' + " + id + ").style.height = 0; toggleLink(" + id + ");", fadeDelay * 100);
	}
	else
	{
		// Content
		object.style.padding = 10;

		// Generate output
		{
			// Insecure version
			if ( inputStatus == 0 )
			{
				// Product not up-to-date
				object.innerHTML = '<div style="float: left; clear: left; padding-right: 10px; padding-bottom: 10px; width: 32px; height: 32px;"><img src="/gfx/32/' + inputImage + '" border="0"></div><b>' + sThisInstallation + inputName + sIsInsecure + '</b><br><br>';

				// Version insecure or missing MS patches
				if ( inputProductType == '0' )
				{
					object.innerHTML += sTheDetected + "<b>" + inputVersion + "</b>" + sHoweverLatestPatched + "<b>" + inputVersionSecure + '</b>.';
				}
				else
				{
					object.innerHTML += sNotAllMSPatches;
				}

				// Update instructions
				object.innerHTML += "<br><br><b>" + sUpdateInstructions + "</b><br>" + inputUpdate;
			}
			else
			{
				// Product not up-to-date
				object.innerHTML = '<div style="float: left; clear: left; padding-right: 10px; padding-bottom: 10px; width: 32px; height: 32px;"><img src="/gfx/32/' + inputImage + '" border="0"></div><b>' + inputName + '</b>' + sIsUpToDate;

				// Version secure or no missing MS patches
				if ( inputProductType == '0' )
				{
					object.innerHTML += sTheDetected + "<b>" + inputVersion + "</b>" + sWhichEitherCorresponds;
				}
				else
				{
					object.innerHTML += sAllMSPatches;
				}
			}

			// Path
			object.innerHTML += ( inputPath != "N/A" ? '<br><br><b>' + sInstalledOnYourIn + '</b><br>' + inputPath : '' );
		}

		// Expand container cell (IE)
		document.getElementById("cell_" + id).style.height = object.offsetHeight + 15;

		// Dont display (opacity = 0)
		Fade(id, 0);

		// Display it
		object.style.visibility = "visible";

		// Fade in
		for ( i=0 ; i<101 ; i++ )
		{
			setTimeout("Fade(" + id + ", " + i + ");", fadeDelay * i);
		}

		toggleLink(id);
	}
}

function toggleLink(id)
{
	object = document.getElementById("img_" + id);

	if ( toggleLinkContainer[id] == true )
	{
		// Make it a plus link
		object.src = "/gfx/plus.gif";

		// Store status
		toggleLinkContainer[id] = false;
	}
	else
	{
		// Make it a minus link
		object.src = "/gfx/minus.gif";

		// Store status
		toggleLinkContainer[id] = true;
	}
}

function tableAddedRow(id, inputImage, inputName, inputVersion, inputStatus, inputColor, inputProductType, inputUpdate, inputVersionSecure, inputPath)
{
	// Check if data is already appended
	if ( tableContaining[id] )
	{
		return -1;
	}
	tableContaining[id] = true;

	// First ROW
	{
		// Create Elements
		{
			// TR
			trElement = document.createElement("TR");
			trElement.setAttribute("id", "row_" + id);
			trElement.setAttribute("onMouseClick", "toggleDetails(" + id + ", " + inputImage + ", " + inputName + ", " + inputVersion + ", " + inputStatus + ", " + inputColor + ", " + inputProductType + ", " + inputUpdate + ", " + inputVersionSecure + ", " + inputPath + ");");
			trElement.onclick = function() { toggleDetails(id, inputImage, inputName, inputVersion, inputStatus, inputColor, inputProductType, inputUpdate, inputVersionSecure, inputPath); }
			trElement.style.cursor = "pointer";
			trElement.style.color = ( inputStatus == 0 ? inputColor : "" );
	
			// TDs
			tdElement_1 = document.createElement("TD");
			tdElement_1.setAttribute("height", 20);
			tdElement_1.setAttribute("id", id + "_link");
			tdElement_2 = document.createElement("TD");
			tdElement_2.setAttribute("width", "3%");
			tdElement_3 = document.createElement("TD");
			tdElement_4 = document.createElement("TD");
			tdElement_5 = document.createElement("TD");
	
			// Content
			tdText_1 = document.createElement("IMG");
			tdText_1.setAttribute("id", "img_"+id);
			tdText_1.setAttribute("src", "/gfx/plus.gif");
			tdText_2 = document.createElement("IMG");
			tdText_2.setAttribute("src", "/gfx/16/" + inputImage);
			tdText_2.setAttribute("border", "0");
			tdText_2.setAttribute("alt", inputName);
			tdText_2.setAttribute("height", 16);
			tdText_2.setAttribute("width", 16);
			tdText_3 = document.createTextNode(" " + inputName);
			tdText_4 = document.createTextNode(inputVersion);
			tdText_5 = document.createElement("IMG");
			tdText_5.setAttribute("src", "/gfx/" + ( inputStatus == 0 ? "un" : "" ) + "patched_16.gif");
			tdText_5.setAttribute("border", "0");
			tdText_5.setAttribute("alt", inputName);
			tdText_5.setAttribute("height", 16);
			tdText_5.setAttribute("width", 16);
		}
	
		// Append Content to TR
		{
			tdElement_1.appendChild(tdText_1);
			tdElement_2.appendChild(tdText_2);
			tdElement_3.appendChild(tdText_3);
			tdElement_4.appendChild(tdText_4);
			tdElement_5.appendChild(tdText_5);
		}
	
		// Append TDs to TR
		{
			trElement.appendChild(tdElement_1);
			trElement.appendChild(tdElement_2);
			trElement.appendChild(tdElement_3);
			trElement.appendChild(tdElement_4);
			trElement.appendChild(tdElement_5);
		}
	
		// Append TR to TBODY
		document.getElementById("ResultTable").appendChild(trElement);
	}

	// Content Row
	{
		// TR
		trElement = document.createElement("TR");
		trElement.setAttribute("id", "row_t_" + id);

		// TDs
		tdElement_1 = document.createElement("TD");
		tdElement_2 = document.createElement("TD");
		tdElement_2.setAttribute("colspan", "4");
		tdElement_2.colSpan = 4;
		tdElement_2.setAttribute("id", "cell_" + id);
		tdElement_2.setAttribute("valign", "top");
		tdElement_2.vAlign = "top";

		// Content
		tdText_1 = document.createTextNode("");
		tdText_2 = document.createElement("DIV");
		tdText_2.setAttribute("id", id);
		tdText_2.setAttribute("class", "DetectionDetails");
		tdText_2.attributes["class"].value = "DetectionDetails";

		// Append Content to TR
		tdElement_1.appendChild(tdText_1);
		tdElement_2.appendChild(tdText_2);

		// Append TDs to TR
		trElement.appendChild(tdElement_1);
		trElement.appendChild(tdElement_2);

		// Append TR to TBODY
		document.getElementById("ResultTable").appendChild(trElement);
	}

	// If 'insecure' entry, then expand by default
	if ( inputStatus == 0 )
	{
		toggleDetails(id, inputImage, inputName, inputVersion, inputStatus, inputColor, inputProductType, inputUpdate, inputVersionSecure, inputPath);
	}
}

// Function for fading background when displaying layers
function FadeBackground()
{
	// Width and Height
	{
		var wW, wH, xS, yS;

		if ( window.innerHeight && window.scrollMaxY )
		{
			xS = document.body.scrollWidth;
			yS = window.innerHeight + window.scrollMaxY;
		}
		else if ( document.body.scrollHeight > document.body.offsetHeight )
		{
			xS = document.body.scrollWidth;
			yS = document.body.scrollHeight;
		}

		if ( self.innerHeight )
		{
			wW = self.innerWidth;
			wH = self.innerHeight;
		}
		else if ( document.documentElement && document.documentElement.clientHeight )
		{
			wW = document.documentElement.clientWidth;
			wH = document.documentElement.clientHeight;
		}

		if( yS < wH )
		{
			rHeight = wH;
		}
		else
		{
			rHeight = yS;
		}

		if ( xS < wW)
		{
			rWidth = wW;
		}
		else
		{
			rWidth = xS;
		}
	}

	// Store in global variable that bg is faded
	fadedBackground = true;

	var fadeBackground = document.getElementById('fadebg');
	fadeBackground.style.height = ( rHeight ? rHeight + 'px' : '100%' );
	fadeBackground.style.width = ( rWidth ? rWidth + 'px' : '100%' );
	fadeBackground.style.display = 'block';
}

function RemoveFadedBackground()
{
	// Store in global variable that bg is no longer faded
	fadedBackground = false;

	var fadeBackground = document.getElementById('fadebg');
	fadeBackground.style.display = 'none';
}

// Function for enabling "Start" button
function EnableStartButton()
{
	// Image
	document.getElementById('startDetection').src = "/gfx/start_active.png";

	// OnClick
	document.getElementById('startDetection').onclick = function() { JAVAstartScan() };

	// CSS Hover Effect
	document.getElementById('startDetection').style.cursor = "pointer";
	
	// Change "process" icon to inactive
	document.getElementById('processIcon').src = "/gfx/process_inactive.gif"
}

// Function for disabling "Start" button
function DisableStartButton()
{
	// Image
	document.getElementById('startDetection').src = "/gfx/start_inactive.png";

	// OnClick
	document.getElementById('startDetection').onclick = function() {  };

	// CSS Hover Effect
	document.getElementById('startDetection').style.cursor = "default";
	
	// Change "process" icon to active
	document.getElementById('processIcon').src = "/gfx/process.gif"
}

// Function for enabling "Stop" button
function EnableStopButton()
{
	// Image
	document.getElementById('stopDetection').src = "/gfx/stop_active.png";

	// OnClick
	document.getElementById('stopDetection').onclick = function() { JAVAstopScan() };

	// CSS Hover Effect
	document.getElementById('stopDetection').style.cursor = "pointer";
}

// Function for disabling "Stop" button
function DisableStopButton()
{
	// Image
	document.getElementById('stopDetection').src = "/gfx/stop_inactive.png";

	// OnClick
	document.getElementById('stopDetection').onclick = function() {  };

	// CSS Hover Effect
	document.getElementById('stopDetection').style.cursor = "default";
}

// Function for placing content in layer
function PlaceContentLayer(layerTitle, layerContent)
{
	document.getElementById('ContentLayerTitle').innerHTML = layerTitle;
	document.getElementById('ContentLayerContent').innerHTML = layerContent;
}

// Function for showing layer
function ShowLayer(layerName, centerLayer, layerWidth)
{
	// Fade Background
	FadeBackground();

	// Display layer
	document.getElementById(layerName).style.visibility = 'visible';

	// Place in center
	if ( centerLayer == true )
	{
		document.getElementById(layerName).style.left = ( document.body.scrollWidth / 2 ) - (layerWidth / 2);
	}
}

// Function for showing layer
function HideLayer(layerName)
{
	document.getElementById(layerName).style.visibility = 'hidden';

	// Remove Faded Background
	RemoveFadedBackground();
}

// Functino for sending XMLHttpRequest
function SendXMLHttpRequest(inputUrl, inputSuccess, inputFail)
{
	// Initialise XMLHttpRequest object
	req = InitXMLHttpRequest();

	// Open
	req.open('GET', inputUrl, true);

	// Handle response
	req.onreadystatechange = function()
	{
		// Check if state is '4'
		if ( req.readyState == 4 )
		{
			var tmp = req.responseText;

			if ( tmp == 'OK' )
			{
				eval(inputSuccess);

				// Hidi layer
				HideLayer('ContentLayer');
			}
			else
			{
				eval(inputFail);
			}
		}
	}

	// Start
	req.send('');

	// Return
//	return true;
}

// Function for Initialising the XMLHttpRequest
function InitXMLHttpRequest()
{
	try
	{
		request = new XMLHttpRequest();
	} catch (trymicrosoft)
		{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
				request = false;
			}
		}
	}

	return request;
}

// Function for submitting "Reminder Service"
function SubmitReminderService(inputEmail)
{
	// URL
	var inputUrl = "/vulnerability_scanning/online/?task=reminder&xml=1&email=" + escape(inputEmail);

	// Successful Response
	var inputSuccess = "alert('" + sReminderThankYou + "');";

	// Failed Reponse
	var inputFail = "alert('" + sReminderError + "');";

	// Send request
	SendXMLHttpRequest(inputUrl, inputSuccess, inputFail);
}

// Function for aborting a form submission
function AbortFormSubmit(e)
{
	if ( e && e.preventDefault )
	{
		e.preventDefault();
	}

	return false;
}

// Add 'trim' functionality to String object
// Usage: String = String.trim();
String.prototype.trim = function () {
	return this.replace(/^\s+|\s+$/g, '');
}
