
// Variables
var m_aVistaBasic = new Array();
var m_aVistaPrem = 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("VistaHome");
	this.location.href = "/vistaready/" + str + "?error=noie";
}

// Activate controls
function activateControls()
{
	var str = getCookie("VistaHome");

	// 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 = "/vistaready/" + 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 = "/vistaready/" + str;
		
		return false;
	}

	
	return true;
}



// Test result
function testResult(strTest, strVista, nRes)
{	
	// Get items
	var cImage = document.getElementById(strTest + "_" + strVista);
	var cRes = document.getElementById(strTest + "_" + strVista + "_res");
	
	// Check for nasty errors
	if (cImage && cRes)
	{
		if (nRes == 0)
		{
			cImage.src = "/images/vistaready/no.gif";
			cRes.innerHTML = "<font color=\"red\" ><b>Failed!</b></font>";
		}
		else
		{
			cImage.src = "/images/vistaready/yes.gif";
			cRes.innerHTML = "<font color=\"green\" ><b>Pass!</b></font>";
		}
	}
}

// Final result
function finalResult(strVista, nRes)
{
	// Get items
	var cImage = document.getElementById("result_" + strVista);
	var cRes = document.getElementById("result_" + strVista + "_res");
	
	// Check for nasty errors
	if (cImage && cRes)
	{
		if (nRes == 0)
		{
			cImage.src = "/images/vistaready/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 Vista.</p>";
		}
		else
		{ 
			if (strVista == "basic")
			{
				cImage.src = "/images/vistaready/success.gif";
				cRes.innerHTML = "<font color=\"#D0A806\" style=\"font-size: 140%\" ><b>Success!</b></font><p>This PC meets the minimum Vista requirements but the recommended specs are required for decent Vista performance.</p><a target='_blank' href='/offsite.asp?{gwvista1|vistatext}316946.php?mtr=DCYAOAAHB&seg=hm'>Buy Vista Now!</a>";
			}
			else
			{
				cImage.src = "/images/vistaready/success.gif";
				cRes.innerHTML = "<font color=\"#D0A806\" style=\"font-size: 140%\" ><b>Success!</b></font><p>Congratulations, this PC will run Microsoft Vista!</p><p></p><p></p><a target='_blank' href='/offsite.asp?{gwvista1|vistatext}316946.php?mtr=DCYAOAAHB&seg=hm'>Buy Vista 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;
}

// Run test
function onRunTest()
{
	// Reset variables
	// -- Vista basic
	m_aVistaBasic["os"] = 0;
	m_aVistaBasic["mem"] = 0;
	m_aVistaBasic["cpu"] = 0;
	m_aVistaBasic["disk"] = 0;
	m_aVistaBasic["screen"] = 0;
	m_aVistaBasic["cd"] = 0;
	m_aVistaBasic["videomem"] = 0;
	// -- Vista premium
	m_aVistaPrem["os"] = 0;
	m_aVistaPrem["mem"] = 0;
	m_aVistaPrem["cpu"] = 0;
	m_aVistaPrem["disk"] = 0;
	m_aVistaPrem["screen"] = 0;
	m_aVistaPrem["cd"] = 0;
	m_aVistaPrem["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 strVistaCheck = new String(sys.OSVersion);
	if (strVistaCheck.toUpperCase().match("VISTA") != null)
	{
		alert("Congratulations, you are already running Vista!");
		this.location.href = "http://www.pcpitstop.com/vistaready/";
	}
	
	// Check os
	// -- Get OS
	var strOS = new String(sys.OSVersion);
	// -- Save PS
	m_aYourSys["os"] = strOS;
	// -- Check level
	if (strOS.toUpperCase().match("XP") != null)
	{
		m_aVistaBasic["os"] = 1;
		m_aVistaPrem["os"] = 1;
	}
	// -- Results
	yourResult("os", "Your system is running " + m_aYourSys["os"] + ".");
	testResult("os", "basic", m_aVistaBasic["os"]);
	testResult("os", "prem", m_aVistaPrem["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 >= 512)
	{
		m_aVistaBasic["mem"] = 1;
	}
	if (nRam >= 1024)
	{
		m_aVistaPrem["mem"] = 1;
	}
	// -- Results
	yourResult("mem", "Your system has " + formatBytes(nRam * 1024 * 1024) + " of memory.");
	testResult("mem", "basic", m_aVistaBasic["mem"]);
	testResult("mem", "prem", m_aVistaPrem["mem"]);
	
	// Check CPU
	var nCPU = cpu.ClockFrequency;
	// -- Save CPU
	m_aYourSys["cpu"] = nCPU;
	// -- Check level
	if (nCPU >= 800)
	{
		m_aVistaBasic["cpu"] = 1;
	}
	if (nCPU >= 1000)
	{
		m_aVistaPrem["cpu"] = 1;
	}
	// -- Results
	yourResult("cpu", "Your system has a " + formatNumber(nCPU, 0, false, false, true) + "MHz processor.");
	testResult("cpu", "basic", m_aVistaBasic["cpu"]);
	testResult("cpu", "prem", m_aVistaPrem["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 >= 15 && nDisk / 1024 >= 20)
			{
				m_aVistaBasic["disk"] = 1;
				nRes = 1;
			}
			if (nFreeDisk / 1024 >= 15 && nDisk / 1024 >= 40)
			{
				m_aVistaPrem["disk"] = 1;
				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_aVistaBasic["disk"]);
		testResult("disk", "prem", m_aVistaPrem["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 Vista 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_aVistaBasic["screen"] = 1;
		m_aVistaPrem["screen"] = 1;
	}
	// -- Results
	yourResult("screen", "Your system has a screen resolution of " + m_aYourSys["screen"] + ".");
	testResult("screen", "basic", m_aVistaBasic["screen"]);
	testResult("screen", "prem", m_aVistaPrem["screen"]);
	
	// Check CD/DVD drive
	for (var i = 0; CDDrive.enumCD(i); i++) 
	{
		// -- DVD
		if (CDDrive.CDType & 0x3018)
		{
			m_aVistaPrem["cd"] = 1;
			m_aVistaBasic["cd"] = 1;
		}
		
		// -- CD
		if (CDDrive.CDType & 0x0300)
		{
			m_aVistaBasic["cd"] = 1;
		}
	}
	// -- Results
	if (m_aVistaBasic["cd"] == 1 && m_aVistaPrem["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_aVistaBasic["cd"] == 1 && m_aVistaPrem["cd"] == 0)
	{
		m_aYourSys["cd"] = "CD-ROM drive";
		yourResult("cd", "Your system has a CD-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_aVistaBasic["cd"]);
	testResult("cd", "prem", m_aVistaPrem["cd"]);
	
	// Video memory
	m_aYourSys["videomem"] = video.VidMemory * 16;
	m_aVistaBasic["videomem"] = 1;
	if (m_aYourSys["videomem"] >= 128)
	{
		m_aVistaPrem["videomem"] = 1;
	}
	yourResult("videomem", "Your system has " + m_aYourSys["videomem"] + "MB of video memory.");
	testResult("videomem", "basic", m_aVistaBasic["videomem"]);
	testResult("videomem", "prem", m_aVistaPrem["videomem"]);
	
	// Test results
	// -- Basic
	var nRes = 1;
	for (strKey in m_aVistaBasic)
	{
		if (m_aVistaBasic[strKey] == 0)
		{
			nRes = 0;
			break;
		}
	}
	finalResult("basic", nRes);
	// -- Premium
	nRes = 1;
	for (strKey in m_aVistaPrem)
	{
		if (m_aVistaPrem[strKey] == 0)
		{
			nRes = 0;
			break;
		}
	}
	finalResult("prem", nRes);
}