var cpu = sys.cpuObject;
var disk = sys.diskObject;
var memory = sys.memoryObject;
var video = sys.videoObject;
var CDDrive = sys.cddriveObject;
var DH = sys.diskhealthObject;
var SHORT_DELAY = 1, LONG_DELAY = 200;
var testObj,step;
var TEST_TIMEOUT_SEC = 120; // timeout in seconds for an individual function of a test
var LONGER_TIMEOUT_SEC = 240; // timeout for long running functions (defined in the test object for that file)
sys.TraceLevel = -2; //negative enables ODS output for DebugView, MH 1/09

// Variables
var m_aWin7Basic = new Array();
var m_aWin7Prem = new Array();
var m_aYourSys = new Array();

// Get cookie
function getCookie(strName)
{
	var strPrefix = strName + "=";
	var nBegin = document.cookie.indexOf("; " + strPrefix);
	if (nBegin == -1)
	{
		nBegin = document.cookie.indexOf(strPrefix);
		if (nBegin != 0)
		{
			return null;
		}
	}
	else
	{
		nBegin += 2;
	}
	var nEnd = document.cookie.indexOf(";", nBegin);
	if (nEnd == -1)
	{
		nEnd = document.cookie.length;
	}

	return unescape(document.cookie.substring(nBegin + strPrefix.length, nEnd));
}

// Check system requirements
if (!document.all || navigator.appVersion.indexOf("Mac") != -1) 
{
	alert("We apologize, but this test requires IE5+ running Microsoft Windows.");
	var str = getCookie("Windows7Home");
	this.location.href = "/windows7ready/" + str + "?error=noie";
}

// Activate controls
function activateControls()
{
	var str = getCookie("Windows7Home");

	// Check to make sure control is installed
	if (!document.sys || !sys.ControlBuild || sys.ControlBuild < CONTROLBUILD) 
	{
		alert("You don't appear to have the diagnostic utility installed. Please go back and try to run the test again.");
		
	
		this.location.href = "/windows7ready/" + str;
		
		return false;
	}
	if (!sys.Enable2(0)) 
	{
		alert("Unable to run the diagnostic utility, permission declined. Please go back and try to run the test again.");
		this.location.href = "/windows7ready/" + str;
		
		return false;
	}

	
	return true;
}



// Test result
function testResult(strTest, strWin7, nRes)
{	
	// Get items
	var cImage = document.getElementById(strTest + "_" + strWin7);
	var cRes = document.getElementById(strTest + "_" + strWin7 + "_res");
	
	// Check for nasty errors
	if (cImage && cRes)
	{
		if (nRes == 0)
		{
			cImage.src = "/images/windows7ready/no.gif";
			cRes.innerHTML = "<font color=\"red\" ><b>Failed!</b></font>";
		}
		else if(nRes==2)
		{
			cImage.src = "/images/windows7ready/caution.gif";
			cRes.innerHTML = "<font color=\"#bf8700\" ><b>Warning!</b></font>";
		}
		else
		{
			cImage.src = "/images/windows7ready/yes.gif";
			cRes.innerHTML = "<font color=\"green\" ><b>Pass!</b></font>";
		}
	}
}

// Final result
function finalResult(strWin7, nRes)
{
	// Get items
	var cImage = document.getElementById("result_" + strWin7);
	var cRes = document.getElementById("result_" + strWin7 + "_res");
	
	// Check for nasty errors
	if (cImage && cRes)
	{
		if (nRes == 0)
		{
			cImage.src = "/images/windows7ready/fail.gif";
			cRes.innerHTML = "<font color=\"#FF3D0A\" style=\"font-size: 140%\" ><b>Failed!</b></font><p>We recommend upgrading or replacing this PC before upgrading to Microsoft Windows 7.</p>";
		}
		else
		{ 
			if (strWin7 == "basic")
			{
				cImage.src = "/images/windows7ready/success.gif";
				cRes.innerHTML = "<font color=\"#D0A806\" style=\"font-size: 140%\" ><b>Success!</b></font><p>This PC meets the minimum Windows 7 requirements but the recommended specs are required for decent Windows 7 performance.</p><a target='_blank' href='/offsite.asp?http://www.microsoft.com/windows/windows-7/download.aspx'>Download Windows 7 Now!</a>";
			}
			else
			{
				cImage.src = "/images/windows7ready/success.gif";
				cRes.innerHTML = "<font color=\"#D0A806\"  style=\"font-size: 140%\" ><b>Success!</b></font><p>Congratulations, this PC will run Microsoft Windows 7!</p><p></p><p></p><a target='_blank' href='/offsite.asp?http://www.microsoft.com/windows/windows-7/download.aspx'>Download Windows 7 Now!</a>";
			}
		}
	}
}

// Your system result
function yourResult(strTest, strNote)
{
	// Get items
	var cRes = document.getElementById(strTest + "_your");
	
	// Check for nasty errors
	if (cRes)
	{
		cRes.innerHTML = strNote;
	}
}

// Decimal format
function formatNumber(num, decimalNum, bolLeadingZero, bolParens, bolCommas)
{ 
        if (isNaN(parseInt(num))) return "NaN";

		var tmpNum = num;
		var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
		// Adjust number so only the specified number of numbers after
		// the decimal point are shown.
		tmpNum *= Math.pow(10,decimalNum);
		tmpNum = Math.round(Math.abs(tmpNum))
		tmpNum /= Math.pow(10,decimalNum);
		tmpNum *= iSign;					// Readjust for sign
	
	
		// Create a string object to do our formatting on
		var tmpNumStr = new String(tmpNum);

		// See if we need to strip out the leading zero or not.
		if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
			if (num > 0)
				tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
			else
				tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
			
		// See if we need to put in the commas
		if (bolCommas && (num >= 1000 || num <= -1000)) {
			var iStart = tmpNumStr.indexOf(".");
			if (iStart < 0)
				iStart = tmpNumStr.length;

			iStart -= 3;
			while (iStart >= 1) {
				tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
				iStart -= 3;
			}		
		}

		// See if we need to use parenthesis
		if (bolParens && num < 0)
			tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

		return tmpNumStr;		// Return our formatted string!
}

// Format bytes
function formatBytes(lBytes)
{
	 // Declare variables
	var strReturn = new String("");
	var lCalc = 0;
	var strSuffix = new String("");

	if (lBytes >= Math.pow(2, 40))
	{
	        lCalc = lBytes / Math.pow(1024, 4);
	        strSuffix = new String("TB");
	}
	else if (lBytes >= Math.pow(2, 30))
	{
		
	        lCalc = lBytes / Math.pow(1024, 3);
	        strSuffix = new String("GB");
	}
	else if (lBytes >= Math.pow(2, 20))
	{
	        lCalc = lBytes / Math.pow(1024, 2);
	        strSuffix = new String("MB");
	}
	else if (lBytes >= Math.pow(2, 10))
	{
		lCalc = lBytes / Math.pow(1024, 1);
	        strSuffix = new String("KB");
	}
	else
	{
	        lCalc = lBytes;
	        strSuffix = new String("Byte");
	}
	
	return formatNumber(lCalc, 2, false, false, true) + strSuffix;
}

// Format hertz
function formatHertz(lHertz)
{
	 // Declare variables
	var strReturn = new String("");
	var lCalc = 0;
	var strSuffix = new String("");


	if (lHertz >= 1000)
	{
		lCalc = lHertz / Math.pow(1000, 1);
	    strSuffix = new String(" GHz");
	}
	else if (lHertz < 1000)
	{
	        lCalc = lHertz;
	        strSuffix = new String("MHz");
	}
	
	return formatNumber(lCalc, 2, false, false, true) + strSuffix;
}


// Run test
function onRunTest(){
	// Reset variables
	// -- Vista basic
	m_aWin7Basic["os"] = 0;
	m_aWin7Basic["mem"] = 0;
	m_aWin7Basic["cpu"] = 0;
	m_aWin7Basic["disk"] = 0;
	m_aWin7Basic["screen"] = 0;
	m_aWin7Basic["cd"] = 0;
	m_aWin7Basic["videomem"] = 0;
	// -- Vista premium
	m_aWin7Prem["os"] = 0;
	m_aWin7Prem["mem"] = 0;
	m_aWin7Prem["cpu"] = 0;
	m_aWin7Prem["disk"] = 0;
	m_aWin7Prem["screen"] = 0;
	m_aWin7Prem["cd"] = 0;
	m_aWin7Prem["videomem"] = 0;
	// -- Your system
	m_aYourSys["os"] = "";
	m_aYourSys["mem"] = "";
	m_aYourSys["cpu"] = "";
	m_aYourSys["disk"] = "";
	m_aYourSys["screen"] = "";
	m_aYourSys["cd"] = "";
	m_aYourSys["videomem"] = "";
	
	// Activate controls
	if (!activateControls()) {
		return;
	}
	
	// Vista check
	var strWin7Check = new String(sys.OSVersion);
	if (strWin7Check.toUpperCase().match("WINDOWS 7") != null) {
		alert("Congratulations, you are already running Windows 7!");
		this.location.href = "/windows7ready/";
	}
	
	// Check os
	// -- Get OS
	var strOS = new String(sys.OSVersion);
	// -- Save PS
	m_aYourSys["os"] = strOS;
	// -- Check level
	if (strOS.toUpperCase().match("VISTA") != null) {
		m_aWin7Basic["os"] = 1;
		m_aWin7Prem["os"] = 1;
	}
	else 
		if (strOS.toUpperCase().match("XP") != null) {
			m_aWin7Basic["os"] = 2;
			m_aWin7Prem["os"] = 2;
		}
	// -- Results
	yourResult("os", "Your system is running " + m_aYourSys["os"] + ".");
	testResult("os", "basic", m_aWin7Basic["os"]);
	testResult("os", "prem", m_aWin7Prem["os"]);
	
	// Check RAM
	// -- Get RAM
	var nRam = memory.RAM;
	var nRamInstalled = memory.RAMInstalled;
	nRam = Math.max(nRam, nRamInstalled);
	// -- Save RAM
	m_aYourSys["mem"] = nRam;
	// -- Check level
	if (nRam >= 1000) {
		m_aWin7Basic["mem"] = 1;
	}
	if (nRam >= 2048) {
		m_aWin7Prem["mem"] = 1;
	}
	else 
		if (nRam < 2048 && nRam >= 1000) {
			m_aWin7Prem["mem"] = 2;
		}
	// -- Results
	yourResult("mem", "Your system has " + formatBytes(nRam * 1024 * 1024) + " of memory.");
	testResult("mem", "basic", m_aWin7Basic["mem"]);
	testResult("mem", "prem", m_aWin7Prem["mem"]);
	
	// Check CPU
	var nCPU = cpu.ClockFrequency;
	// -- Save CPU
	m_aYourSys["cpu"] = nCPU;
	// -- Check level
	if (nCPU >= 1000) {
		m_aWin7Basic["cpu"] = 1;
	}
	if (nCPU >= 1600) {
		m_aWin7Prem["cpu"] = 1;
	}
	else 
		if (nCPU < 1600 && nCPU > 1000) {
			m_aWin7Prem["cpu"] = 2;
		}
	// -- Results
	yourResult("cpu", "Your system has a " + formatHertz(nCPU) + " processor.");
	//yourResult("cpu", "Your system has a " + formatNumber(nCPU, 0, false, false, true) + " MHz processor.");
	testResult("cpu", "basic", m_aWin7Basic["cpu"]);
	testResult("cpu", "prem", m_aWin7Prem["cpu"]);
	
	// Check disk space
	var nRes = 0;
	var strDrive = "C".charCodeAt(0);
	var j = 0;
	for (var i = 0; i < 24; i++) {
		disk.DrivePath = String.fromCharCode(strDrive + i) + ":\\";
		if (disk.DriveType != "No root directory") {
			// -- Get drive space
			var nFreeDisk = disk.Free;
			var nDisk = disk.Size;
			
			// -- Check level
			if (nFreeDisk / 1024 >= 16 && nDisk / 1024 >= 20) {
				m_aWin7Basic["disk"] = 1;
				nRes = 1;
			}
			if (nFreeDisk / 1024 >= 16 && nDisk / 1024 >= 40) {
				m_aWin7Prem["disk"] = 1;
				nRes = 1;
			}
			else 
				if (nFreeDisk / 1024 >= 16 && nDisk / 1024 < 40) {
					m_aWin7Prem["disk"] = 2;
					nRes = 1;
				}
		}
		
		// -- Do we have a result
		if (nRes == 1) {
			break;
		}
	}
	// -- Do we have a result
	if (nRes == 1) {
		m_aYourSys["disk"] = nFreeDisk;
		m_aYourSys["diskspace"] = nDisk;
		
		// -- -- Results
		yourResult("disk", "Your hard disk '" + disk.DrivePath + "' has a " + formatBytes(nFreeDisk * 1024 * 1024) + " free out of " + formatBytes(nDisk * 1024 * 1024) + " hard disk space.");
		testResult("disk", "basic", m_aWin7Basic["disk"]);
		testResult("disk", "prem", m_aWin7Prem["disk"]);
	}
	else {
		m_aYourSys["disk"] = nFreeDisk;
		m_aYourSys["diskspace"] = nDisk;
		
		// -- -- Results
		yourResult("disk", "We were unable to find a hard disk on your machine that has the amount of free space that Windows 7 requires.");
		testResult("disk", "basic", 0);
		testResult("disk", "prem", 0);
	}
	
	// Check screen resolution
	var nHorz = video.VidHoriz;
	var nVert = video.VidVert;
	// -- Save screen resolution
	m_aYourSys["screen"] = nHorz + "x" + nVert;
	// -- Check level
	if (nHorz >= 800 && nVert >= 600) {
		m_aWin7Basic["screen"] = 1;
		
	}
	if (nHorz >= 1024 && nVert >= 768) {
		m_aWin7Prem["screen"] = 1;
		
	}
	else 
		if (nHorz < 1024 || nVert < 768) {
			m_aWin7Prem["screen"] = 2;
			
		}
	
	// -- Results
	yourResult("screen", "Your system has a screen resolution of " + m_aYourSys["screen"] + ".");
	testResult("screen", "basic", m_aWin7Basic["screen"]);
	testResult("screen", "prem", m_aWin7Prem["screen"]);
	/*
	// Check CD/DVD drive
	var drivetype = new Array();
	var cdtype = new Array();
	//alert('CD drive exists, 1 true or 0 false: '+CDDrive.enumCD(cddrives.index));
	for (var i = 0; CDDrive.enumCD(i); i++) {
		
		if ( CDDrive.CDType & 0x0100 ){
			Type = "CD-R";
			m_aWin7Basic["cd"] = 1;
			drivetype[i]=CDDrive.CDType & 0x0100;
		}
		if (CDDrive.CDType & 0x0200) {
			Type = "CD-RW";
			m_aWin7Basic["cd"] = 1;
			drivetype[i]=CDDrive.CDType & 0x0200;
		}
		if (CDDrive.CDType & 0x2000) {
			Type = "DVD-RAM";
			m_aWin7Prem["cd"] = 1;
			m_aWin7Basic["cd"] = 1;
			drivetype[i]=CDDrive.CDType & 0x2000;
		}
		if (CDDrive.CDType & 0x1000){ 
			Type = "DVD-RW";
			m_aWin7Prem["cd"] = 1;
			m_aWin7Basic["cd"] = 1;
			drivetype[i]=CDDrive.CDType & 0x1000;
		}
		if ( CDDrive.CDType & 0x0018 ){
			Type = "DVD-ROM";
			m_aWin7Prem["cd"] = 1;
			m_aWin7Basic["cd"] = 1;
			drivetype[i]=CDDrive.CDType & 0x0018;
		}
		alert('In CDDrive loop-- cd drive type: '+drivetype[i]);
				// -- DVD
		/dvdtype[i]=CDDrive.CDType & 0x3018;
		if (CDDrive.CDType & 0x3018)
		{
			m_aWin7Prem["cd"] = 1;
			m_aWin7Basic["cd"] = 1;
		}
		
		// -- CD
		cdtype[i]=CDDrive.CDType & 0x0300;
		if (CDDrive.CDType & 0x0300)
		{
			m_aWin7Basic["cd"] = 1;
		}
		//alert('In CDDrive loop-- cd drive type: '+cdtype[i]+' dvdtype: '+dvdtype[i]);
	}
	
	*/
	// -- Results
	//force pass
	m_aWin7Prem["cd"] = 1;
	m_aWin7Basic["cd"] = 1;
	//end force pass
	if (m_aWin7Basic["cd"] == 1 && m_aWin7Prem["cd"] == 1)
	{
		m_aYourSys["cd"] = "CD-ROM and DVD-ROM drive";
		yourResult("cd", "Your system has a CD-ROM and DVD-ROM drive.");
	}
	else if (m_aWin7Basic["cd"] == 1 && m_aWin7Prem["cd"] == 0)
	{
		m_aYourSys["cd"] = "CD-ROM drive";
		yourResult("cd", "Your system has a CD-ROM drive.");
	}
	else if(m_aWin7Basic["cd"] || m_aWin7Prem["cd"])
		{
		m_aYourSys["cd"] = "CD-ROM or DVD-ROM drive";
		yourResult("cd", "Your system has a CD-ROM drive or DVD-ROM Drive.");
	}
	else
	{
		m_aYourSys["cd"] = "No CD-ROM and DVD-ROM drive";
		yourResult("cd", "Your system does not has a CD-ROM drive.");
	}
	
	testResult("cd", "basic", m_aWin7Basic["cd"]);
	testResult("cd", "prem", m_aWin7Prem["cd"]);
	

	
	// Video memory
	m_aYourSys["videomem"] = video.VidMemory * 16;
	m_aWin7Basic["videomem"] = 1;
	if (m_aYourSys["videomem"] >= 128)
	{
		//m_aWin7Basic["videomem"] = 1;
		m_aWin7Prem["videomem"] = 1;
	}
	//if (m_aYourSys["videomem"] >= 256)
	//{
	//	m_aWin7Prem["videomem"] = 1;
	//}
	//else if(m_aYourSys["videomem"] < 256 && m_aYourSys["videomem"] >= 128)
	else if(m_aYourSys["videomem"] < 128)
	{
		//m_aWin7Basic["videomem"] = 2;
		m_aWin7Prem["videomem"] = 2;
	}
	yourResult("videomem", "Your system has " + m_aYourSys["videomem"] + "MB of video memory.");
	testResult("videomem", "basic", m_aWin7Basic["videomem"]);
	testResult("videomem", "prem", m_aWin7Prem["videomem"]);
	
	// Test results
	// -- Basic
	var nRes = 1;
	for (strKey in m_aWin7Basic)
	{
		if (m_aWin7Basic[strKey] == 0)
		{
			nRes = 0;
			break;
		}
	}
	finalResult("basic", nRes);
	// -- Premium
	nRes = 1;
	for (strKey in m_aWin7Prem)
	{
		if (m_aWin7Prem[strKey] == 0)
		{
			nRes = 0;
			break;
		}
	}
	finalResult("prem", nRes);
}
