// adding region changes / oliver schnarchendorf
// -- SSO Path: updated per Portal DEV
// -- RouteControl: speedselection and resultsrunagain now point to region selection.
// -- printWrongBrowser: implemented 'bad browser page' and browser detection and hooked it up to the detection of IE 8.

	// NEEDS to be false in a production environment
	var debug = false;

	// IE 8 browser version check
	var _agt=navigator.userAgent.toLowerCase();
	var _appVer = navigator.appVersion.toLowerCase();
	is_ie8 = _agt.indexOf("trident/4.0") != -1;

	// PATHS
	var _apppath = 'http://www2.verizon.net/micro/speedtest/java';
	var _imagepath = 'http://www2.verizon.net/micro/speedtest/java/images';
	var _ssopath = 'https://www.verizon.net/ssowebapp/VOLPortalLogin?TARGET=';
	var _helppath = 'http://netservices.verizon.net/portal/msa/bookmark?helpitem&case=';
	var _select_server = '';
	
	// GLOBAL variables
	var _speed = '';
	var _service_class;
	var _dl_class;
	var _ul_class;
	var _local_test_flag = 'false';
	var _debugging_flag = 'true';
	var _package_speeds = new Array(
		"5/2", 
		"5/5", 
		"10/2", 
		"15/2", 
		"15/5", 
		"15/15", 
		"20/5", 
		"20/15", 
		"20/20", 
		"25/15", 
		"30/5", 
		"30/15", 
		"35/10", 
		"35/20", 
		"50/5", 
		"50/10", 
		"50/20"
	);

	// REGION MAP variables.
	var map_region_all = new Image();
	var map_region_west = new Image();
	var map_region_central = new Image();
	var map_region_east = new Image();
	var map_region_selected = 'all';
	
	map_region_all.src = _imagepath + '/region/usa_map.gif';
	map_region_west.src = _imagepath + '/region/usa_map_region_west.gif';
	map_region_central.src = _imagepath + '/region/usa_map_region_central.gif';
	map_region_east.src = _imagepath + '/region/usa_map_region_east.gif';
			
	// Array for results returned by the Java Applet.
	var testResults = new Array(22);

	var STATUS = 0;
	var SHOW_TEST_ERROR = 1;
	var SHOW_TEST_RESULTS = 2;
	var SPEED_UPLOAD = 3;
	var SPEED_DOWNLOAD = 4;
	var JWEB100_ANALYSIS = 5;
	var SND_LIMITED_PCT = 6;
	var RCV_LIMITED_PCT = 7;
	var NET_LIMITED_PCT = 8;
	var SVC_CLASS_DOWNLOAD = 9;
	var SVC_CLASS_UPLOAD = 10;
	var SERVICE_CLASS = 11;
	var BAD_CABLE = 12;
	var NETWORK_CONGESTION = 13;
	var OPTIMIZATION_NEEDED = 14;
	var HALF_DUPLEX = 15;
	var JWEB100_USER_TEXT = 16;
	var CLIENT_RCV_WINDOW_SIZE = 17;
	var PACKET_LOSS_PCT = 18;
	var DUPLEX_MISMATCH = 19;
	var WIRELESS_LINK = 20;
	var SELECT_SERVER = 21;

//
////////////////////////////////////////////////////////////////////////////////
// General Functions
////////////////////////////////////////////////////////////////////////////////
//

	function isMac()
	{
		var agt=navigator.userAgent.toLowerCase();
		return (agt.indexOf("mac")!=-1);
	}
	
	function isIE()
	{
		var appVer = navigator.appVersion.toLowerCase();
		var iePos  = appVer.indexOf('msie');
		return (iePos!=-1);
	}
	
	function getCookie( name, keyname )
	{
		var nameEQ = name + "=";
		var keynameEQ = keyname + "=";
		
		var cookies = document.cookie.split(';');
		
		for(var i = 0; i < cookies.length; i++)
		{
			var cookie = cookies[i];
			
			while (cookie.charAt(0) == ' ') 
			{
				cookie = cookie.substring(1, cookie.length);
			}
			
			if (cookie.indexOf(nameEQ) == 0) 
			{
				var decoded_cookie = unescape(cookie.substring(nameEQ.length,cookie.length));
				var dc_kv_pairs = decoded_cookie.split('&');
				for (var j = 0; j < dc_kv_pairs.length; j++)
				{
					if (dc_kv_pairs[j].indexOf(keynameEQ) == 0)
					{
						return dc_kv_pairs[j].substring(keynameEQ.length, dc_kv_pairs[j].length);
					}
				}
			}
		}
		return null;
	}
	
	function setCookie( name, value, days )
	{
		if( days )
		{
			var date = new Date();
			date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
			var expires = "; expires=" + date.toGMTString();
		}
		else 
		{
			var expires = "";
		}
		document.cookie = name + "=" + escape( value ) + expires + "; path=/";
	}
	
	function removeAlpha( theString )
	{
		return theString.replace(/\D/g, "");
	}

	function zeroTestResults ()
	{
		for (i=0; i > 21; i++)
		{
			testResults[i] = '';
		}
		testResults[SPEED_UPLOAD] = '0.0';
		testResults[SPEED_DOWNLOAD] = '0.0';
		testResults[JWEB100_ANALYSIS] = 'Test results are missing.';
		testResults[SND_LIMITED_PCT] = '0.0';
		testResults[RCV_LIMITED_PCT] = '0.0';
		testResults[NET_LIMITED_PCT] = '0.0';
		testResults[SVC_CLASS_DOWNLOAD] = _dl_class;
		testResults[SVC_CLASS_UPLOAD] = _ul_class;
		testResults[SERVICE_CLASS] = _service_class;
		testResults[SELECT_SERVER] = _select_server;
	}

	function switchContent( theContent )
	{
		document.getElementById("st_content").innerHTML = theContent;
	}

	function readSpeedFromCookie()
	{
		var temp;
		var temp2;
		var speed_inrange = false;
		var speeds_allowed = _package_speeds;

		var linespeed = getCookie( 'user_profile', 'linespeed' );

		if (linespeed != undefined && 
				linespeed != '' &&
				linespeed != null)
		{
			for (var i = 0; i < speeds_allowed.length; i++)
			{
				if (linespeed.toUpperCase() == speeds_allowed[i].toUpperCase())
				{
					speed_inrange = true;
				}
			}
			
			temp = linespeed.split('/');
			
			if (temp.length == 2 && speed_inrange)
			{
				_speed = removeAlpha( temp[0] ) +	'_' +	removeAlpha( temp[1] );
				_service_class = _speed;
				temp2 = _service_class.split('_');
				_dl_class = temp2[0];
				_ul_class = temp2[1];
			}
		}
		
		if (debug)
		{
			alert(
				"_speed: " + _speed + "\n" +
				"_service_class: " + _service_class + "\n" +
				"_dl_class: " + _dl_class + "\n" +
				"_ul_class: " + _ul_class + "\n"
				);
		}
	}


//
////////////////////////////////////////////////////////////////////////////////
// Applet/Object that includes Jweb100 Java Application
////////////////////////////////////////////////////////////////////////////////
//
	function printWrongBrowser ()
	{
		var wrongBrowser = 
		'<div style="margin: 10px; padding: 10px; border: 2px solid #ff0000; font-weight: bold;">' +
		'The application you are trying to access does currently NOT support your Browser.<br />' +
		'</div><br /><center><a href="http://www2.verizon.net/help/"><img src="http://www2.verizon.net/help/fios_settings/include/images/buttons/back_to_fios_help_topics.gif" alt="Back To FiOS Help Topics" width="154" height="18" border="0" /></a><br /></center>';
		return wrongBrowser;
	}
		
//
////////////////////////////////////////////////////////////////////////////////
// Applet/Object that includes Jweb100 Java Application
////////////////////////////////////////////////////////////////////////////////
//

	function printJavaEmbed ( speedServer, debugFlag, testFlag, isIE )
	{
		var appletMarkup = 
			'<!-- JWeb100 applet parms:' +
			'    DEBUGGING: enables output to java console if value = "true"' +
			'    LOCAL_TEST: produces faux results after 5 second sleep if value = "true"' +
			'    RUN_NOW: will launch the testing thread immediately upon init if value = "true"' +
			'-->' +
			'<applet ' +
			'	id="APPLET_JWEB100" ' +
			'	archive="JWeb100.jar" ' +
			'	width="1" ' +
			'	height="1" ' +
			'	code="JWeb100.class" ' +
			'	codebase="http://' + speedServer + '/SpeedTester/" ' +
			'	MAYSCRIPT>' +
			'  <param ' +
			'		name="TESTPORT" '+
			'		value="2257">' +
			'  <param ' +
			'		name="DEBUGGING" ' +
			'		value="' + debugFlag + '">' +
			'  <param ' +
			'		name="LOCAL_TEST" ' +
			'		value="' + testFlag + '">' +
			'  <param ' +
			'		name="RUN_NOW" ' +
			'		value="true">' +
			'</applet>';	

		var objectMarkup = 
			'<!-- JWeb100 applet parms:' +
			'    DEBUGGING: enables output to java console if value = "true"' +
			'    LOCAL_TEST: produces faux results after 5 second sleep if value = "true"' +
			'    RUN_NOW: will launch the testing thread immediately upon init if value = "true"' +
			'-->' +
			'<OBJECT ' +
			'           id = "APPLET_JWEB100" ' +
			'           name = "APPLET_JWEB100" ' +
		 	'           classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ' +
		 	'           codebase = "http://java.sun.com/update/1.4.2/jinstall-1_4-windows-i586.cab#Version=1,2,0,0" ' +
		 	'           WIDTH = "1" HEIGHT = "1" > ' +
			'								<param name="TESTPORT" value="2257"> ' +
		 	'               <PARAM NAME="CODE" VALUE="JWeb100.class" > ' +
		 	'               <PARAM NAME="CODEBASE" VALUE="http://' + speedServer + '/SpeedTester/" > ' +
		 	'               <PARAM NAME="ARCHIVE" VALUE="JWeb100.jar" > ' +
		 	'               <PARAM NAME="MAYSCRIPT" VALUE="true"> ' +
		 	'               <PARAM NAME="type" VALUE="application/x-java-applet;version=1.4"> ' +
		 	'               <PARAM NAME="scriptable" VALUE="false"> ' +
		 	'               <PARAM NAME="DEBUGGING" VALUE="' + debugFlag + '"> ' +
		 	'               <PARAM NAME="LOCAL_TEST" VALUE="' + testFlag + '"> ' +
		 	'               <PARAM NAME="RUN_NOW" VALUE="true">' +
			'</OBJECT>';
			
		if (isIE)
		{
			return objectMarkup;
		}
		else
		{
			return appletMarkup;
		}
	}

//
////////////////////////////////////////////////////////////////////////////////
// Speed Test Results page functions and content
////////////////////////////////////////////////////////////////////////////////
//
	function speedLimit( testResults )
	{
		return	(testResults[SPEED_UPLOAD] < (testResults[SVC_CLASS_UPLOAD] / 5) ||
						testResults[SPEED_DOWNLOAD] < (testResults[SVC_CLASS_DOWNLOAD] / 5));
	}
	
	var pxLength = 150;
	var downSpeeds = new Array(5, 10, 15, 20, 25, 30, 35, 50);
	var upSpeeds = new Array(2, 5, 10, 15, 20);
	var ARROWUP = 0;
	var ARROWDOWN = 1;
	var ARROWNONE = 2;
	
	function printBox( state, boxContent )
	{
		var leftBoxImgs = new Array( 
			'<img src="' + _imagepath + '/boxLeftArrowUp.gif" alt="" width="34" height="65" border="0" /><br />',
			'<img src="' + _imagepath + '/boxLeftArrowDown.gif" alt="" width="34" height="65" border="0" /><br />',
			'<img src="' + _imagepath + '/boxLeft.gif" alt="" width="14" height="65" border="0" /><br />'
			);
		var leftBoxImg = '';
		if (state == ARROWUP || state == ARROWDOWN || state == ARROWNONE)
		{
			leftBoxImg = leftBoxImgs[state];
		}
		else
		{
			leftBoxImg = leftBoxImgs[ARROWNONE];
		}
		
		var boxMarkup =
			'<table cellspacing="0" cellpadding="0" border="0">' +
			'	<tr>' +
			'		<td>' +
			leftBoxImg +
			'		</td>' +
			'		<td style="vertical-align: middle; background-image: url(' + _imagepath + '/boxMid.gif);" height="65" valign="top">' +
			'			<div style="font-size: smaller;	text-align: center;">' + boxContent + '</div>' +
			'		</td>' +
			'		<td>' +
			'			<img src="' + _imagepath + '/boxRight.gif" alt="" width="14" height="65" border="0" /><br />' +
			'		</td>' +
			'	</tr>' +
			'</table>';
		return boxMarkup;
	}
	
	function printBoxes( download, upload, package )
	{
		var statusText = ''
		var downText = 'Your <span style="color: #cc0000; font-weight: bold;">download</span><br /> speed is:<br /><span style="color: #309618;	font-weight: bold;">' + download + ' Mbps</span>';
		var upText = 'Your <span style="color: #cc0000; font-weight: bold;">upload</span><br /> speed is:<br /><span style="color: #309618;	font-weight: bold;">' + upload + ' Mbps</span>';
		var packageText = 'Your current Verizon<br />FiOS Internet Speed<br />package is up to ' + package;
		var noPackageText = '<a href="' + _ssopath + top.location.href + '" target="_top">Let us check</a> your<br />current Verizon FiOS<br />Internet speed package.';
		
		if (package == undefined || package == '')
		{
			statusText = noPackageText;
		}
		else
		{
			statusText = packageText;
		}
		
		var boxesMarkup =
			'<b>We have finished your connection speed test using the ' + map_region_selected + ' Region server.</b><br />' +
			'<br />' +
			'<table cellspacing="0" cellpadding="0" border="0">' +
			'	<tr>' +
			'		<td valign="top" width="33%">' +
			'			' + printBox(ARROWDOWN, downText) +
			'		</td>' +
			'		<td valign="top" width="33%">' +
			'		  ' + printBox(ARROWUP, upText) +
			'		</td>' +
			'		<td valign="top" width="33%">' +
			'			' + printBox(ARROWNONE, statusText) +
			'		</td>' +
			'	</tr>' +
			'</table>';
		return boxesMarkup;
	}
	
	function printHeader( headline, colOneHeader, colTwoHeader )
	{
		var headerMarkup = 
			'<table border="0" cellpadding="0" cellspacing="0" width="100%">' +
			'	<tr>' +
			'		<td colspan="3" style="padding: 20px 0 3px 10px;	color: #cc0000;	font-weight: bold; font-size: large;">' +
			'			' + headline +
			'		</td>' +
			'	</tr>' +
			'	<tr>' +
			'		<td width="150" style="padding: 1px 0 1px 0;	margin: 1px 0 1px 0; border-top: dashed 1px #cccccc; border-bottom: dashed 1px #cccccc;">' +
			'			<div style="padding: 3px 0 3px 10px; background-color: #ffdddd; font-weight: bold;">' + colOneHeader + '</div>' +
			'		</td>' +
			'		<td width="350" style="padding: 1px 0 1px 0;	margin: 1px 0 1px 0; border-top: dashed 1px #cccccc; border-bottom: dashed 1px #cccccc;">' +
			'			<div style="padding: 3px 0 3px 10px; background-color: #ffdddd; font-weight: bold;">' + colTwoHeader + '</div>' +
			'		</td>' +
			'		<td style="padding: 1px 0 1px 0;	margin: 1px 0 1px 0; border-top: dashed 1px #cccccc; border-bottom: dashed 1px #cccccc;">' +
			'			<div style="padding: 3px 0 3px 10px; background-color: #ffdddd; font-weight: bold;">&nbsp;</div>' +
			'		</td>' +
			'	</tr>' +
			'</table>';
		return headerMarkup + '\n';
	}

	function printResultsMessage( testResults )
	{
		var test_okay = false;
		var message = '';
		var testMessage = '';
		var passImg = _imagepath + '/info/green_checkmark.gif';
		var passAlt = 'Pass';
		var failImg = _imagepath + '/info/red_x.gif';
		var failAlt = 'Fail';

		var testMessageImg = failImg
		var testMessageAlt = failAlt
		var testMessageStyle = 'padding: 5px; border: 1px solid #cc0000; background: #ffcccc;';

		if (testResults == undefined)
		{
			return 'An error was encountered and no test results have been returned. Please try your test again later.<br />';;
		}
		
		if (testResults[SHOW_TEST_RESULTS])
		{
			message = 	
				'We have finished your connection speed test. You can review the ' +
				'<a href="#" onclick="routeControl(\'resultsinfo\'); return false;">\'speed information\'</a> ' +
				'or <a href="#" onclick="routeControl(\'resultsanalysis\'); return false;">\'analysis\'</a> for more connection speed test information.<br />';
		}
		else
		{
			message = '<span style="color: #ff0000;">' + testResults[SHOW_TEST_ERROR] + '</span>';
		}
		
		if (testResults[CLIENT_RCV_WINDOW_SIZE] < 300000)
		{
			if (isMac())
			{
				helpcase = "27244";
			}
			else
			{
				helpcase = "not_optimized";
			}
			testMessage = 
				'Your PC has not been optimized for the premium experience offered by FiOS. ' +
				'<a href="' + _helppath + helpcase + 
				'" target="_top">Learn more about FiOS Optimizer</a> and run this test again.';
		}
		else if ( speedLimit( testResults ) && testResults[PACKET_LOSS_PCT] >= 0.03 && testResults[PACKET_LOSS_PCT] <= 0.06)
		{
			helpcase = "packet_loss";
			testMessage = 
				'We are sorry, but your test is being affected by a temporary network problem. ' +
				'<a href="' + _helppath + helpcase + 
				'" target="_top">Learn more about network issues</a> and test your PC speed later.';
		}
		else if ( speedLimit( testResults ) && testResults[PACKET_LOSS_PCT] >= 0.06)
		{
			testMessage = 
				'There appears to be a problem affecting your speed due to serious packet loss. ' +
				'Please reboot your router and PC, and try the test again.  If the problem continues, call technical support. ' +
				'Consumers please call 1-888-553-1555. Business Customers please call 1-888-244-4440.';
		}
		else if ( speedLimit( testResults ) && testResults[SND_LIMITED_PCT] >= 0.10)
		{
			testMessage = 'We’re sorry, our test server is experiencing a problem. Please try your test again later.';
		}
		else if ( speedLimit( testResults ) && parseInt(testResults[BAD_CABLE]) != 0)
		{
			helpcase = "check_ethernet";
			testMessage = 
				'The test indicates that you may have a bad Ethernet cable. ' +
				'Please <a href="' + _helppath + helpcase + 
				'" target="_top">check the troubleshooting guide</a> for more information.';
		}
		else if ( speedLimit( testResults ) &&
							(	parseInt(testResults[DUPLEX_MISMATCH]) != 0 || 
							  parseInt(testResults[HALF_DUPLEX]) != 0 || 
								parseInt(testResults[WIRELESS_LINK]) != 0 ) )
		{
			helpcase = "nic_config";
			testMessage = 
				'To accurately measure the speed of your connection, you must use an Ethernet connected PC. ' +
				'If you receive this testMessage, you are either testing from a wireless connection, ' +
				'or your PC’s Ethernet adapter may be configured incorrectly.  Please try your test from a wired connection, or ' +
				'<a href="' + _helppath + helpcase + 
				'" target="_top">learn how to reconfigure your adapter</a> for proper operation.';
		}
		else if (!testResults[SHOW_TEST_RESULTS])
		{
			var failed_to_connect = testResults[SHOW_TEST_ERROR].indexOf('Failed to connect');
			if (failed_to_connect > -1)
			{
				testMessage = 'An error was encountered and no test results have been returned.';
			}
			else
			{
				testMessage = testResults[SHOW_TEST_ERROR] + '<br />Consumers please call 1-888-553-1555. Business Customers please call 1-888-244-4440.';
			}
		}
		else
		{
			testMessage = "Congratulations!  This test result indicates your service is operating normally.";
			test_okay = true;
		}

		if( test_okay )
		{
			testMessageImg = passImg;
			testMessageAlt = passAlt;
			testMessageStyle = 'padding: 5px; border: 1px solid #00cc00; background: #ccffcc;';
		}
		
		var resultsMessageMarkup =
			'<div>' + message + '</div>' +
			'<br />' +
			'<table cellpadding="0" cellspacing="0" border="0">' +
			'	<tr>' +
			'		<td>' +
			'			<img src="' + testMessageImg + '" alt="' + testMessageAlt + '" width="19" height="16" border="0" style="padding: 5px;" />' +
			'		</td>' +
			'		<td>' +
			'			<div style="' + testMessageStyle + '">' + testMessage + '</div>' +
			'		</td>' +
			'	</tr>' +
			'</table>';
	
		if( debug )
		{
			alert("" +
				"CLIENT_RCV_WINDOW_SIZE: " 	+ testResults[CLIENT_RCV_WINDOW_SIZE] + "\n" +
				"PACKET_LOSS_PCT: " 				+ testResults[PACKET_LOSS_PCT] + "\n" +
				"SVC_CLASS_DOWNLOAD: " 			+ testResults[SVC_CLASS_DOWNLOAD] + "\n" +
				"SVC_CLASS_UPLOAD: " 				+ testResults[SVC_CLASS_UPLOAD] + "\n" +
				"SPEED_UPLOAD: " 						+ testResults[SPEED_UPLOAD] + "\n" +
				"SPEED_DOWNLOAD: " 					+ testResults[SPEED_DOWNLOAD] + "\n" +
				"HALF_DUPLEX: " 						+ testResults[HALF_DUPLEX] + "\n" +
				"DUPLEX_MISMATCH: " 				+ testResults[DUPLEX_MISMATCH] + "\n" +
				"WIRELESS_LINK: " 					+ testResults[WIRELESS_LINK] + "\n" +
				"SND_LIMITED_PCT: " 				+ testResults[SND_LIMITED_PCT] + "\n" +
				"RCV_LIMITED_PCT: " 				+ testResults[RCV_LIMITED_PCT] + "\n" +
				"NET_LIMITED_PCT: " 				+ testResults[NET_LIMITED_PCT] + "\n" +
				"BAD_CABLE: " 							+ testResults[BAD_CABLE] + "\n");
		}
		
		return resultsMessageMarkup;
	}
	

	function printFooter( package, testResults )
	{
		var footerMarkup = '';
		var resultsMessage = printResultsMessage (testResults);
		var footerPackageMarkup = 
			'<br />' +
			'<span style="color: #cc0000;">*</span> Indicates your current Verizon Fios speed package. <a href="http://www22.verizon.com/content/consumerfios/packages+and+prices/packages+and+prices.htm">Want to go faster?</a><br />' +
			'<br />' +
			resultsMessage + 
			'<br />' +
			'<a href="javascript: routeControl(\'resultsrunagain\');"><img src="' + _imagepath + '/btnRunTheTestAgain.gif" alt="Run the test again &gt;" width="122" height="18" border="0" /></a>';
		var footerNoPackageMarkup =
			'<br />' +
			'<a href="http://www22.verizon.com/content/consumerfios/packages+and+prices/packages+and+prices.htm">Want to go faster?</a><br />' +
			'<br />' +
			resultsMessage +
			'<br />' +
			'If you would like to compare your FiOS Internet Speed Test Results to your FiOS Internet Speed package, please run the test again.<br />' +
			'<br />' +
			'<a href="javascript: routeControl(\'resultsrunagain\');"><img src="' + _imagepath + '/btnRunTheTestAgain.gif" alt="Run the test again &gt;" width="122" height="18" border="0" /></a>';
		
		if (!testResults[SHOW_TEST_RESULTS])
		{
			footerMarkup = '<br />' + resultsMessage + '<br /><a href="javascript: routeControl(\'resultsrunagain\');"><img src="' + _imagepath + '/btnRunTheTestAgain.gif" alt="Run the test again &gt;" width="122" height="18" border="0" /></a>';
		}
		else if (package == undefined || package == '')
		{
			footerMarkup = footerNoPackageMarkup;
		}
		else
		{
			footerMarkup = footerPackageMarkup;
		}
		return footerMarkup;
	}
	
	function printBar( speed, barcolor )
	{
		if (speed <= 0) { speed = 1; };
		if (speed > 100) { speed = 50; };
		var percentile = (pxLength / 100 * (speed * 2));
		barMarkup = 
			'<table cellspacing="0" cellpadding="0" border="0">' +
			'	<tr>' +
			'		<td width="1">' +
			'			<img src="' + _imagepath + '/'+ barcolor + 'PipeCap.gif" alt="" width="1" height="13" border="0" /><br />' +
			'		</td>' +
			'		<td style="background-image: url(' + _imagepath + '/bkg'+ barcolor + 'Pipe.gif);" width="' + percentile + '" height="13">' +
			'		</td>' +
			'		<td width="1">' +
			'			<img src="' + _imagepath + '/'+ barcolor + 'PipeCap.gif" alt="" width="1" height="13" border="0" /><br />' +
			'		</td>' +
			'	</tr>' +
			'</table>';
		return barMarkup;
	}

	function printResultsRow( speed, barcolor, desc, packageSpeed )
	{
		var rowDesc = '';
		var rowLabelStyle = 'color: #aaaaaa;';
		
		if (desc != '')
		{
			rowDesc =  
				'<div style="color: #306918;	font-weight: bold;">' +
				desc +
				'</div>';
			rowLabelStyle = 'color: #306918;	font-weight: bold;';
		}
		else
		{
			rowDesc = 'Up to ' + speed + ' Mbps';
			if (packageSpeed != undefined && 
				  packageSpeed != 0 && 
					packageSpeed == speed)
			{
				rowDesc += '<span style="color: #cc0000;">*</span>';
			}
		}
		resultsRowMarkup = 
			'<table cellspacing="0" cellpadding="0" border="0">' +
			'	<tr>' +
			'		<td style="padding: 3px 0 1px 10px;" width="150">' +
			rowDesc +
			'		</td>' +
			'		<td>' +
			'			<table cellspacing="0" cellpadding="0" border="0">' +
			'				<tr>' +
			'					<td>' +
			printBar( speed, barcolor ) + 
			'					</td>' +
			'					<td style="padding: 0 0 0 10px; ' + rowLabelStyle + '">' +
			'						' + speed + ' Mbps' +
			'					</td>' +
			'				</tr>' +
			'			</table>' +
			'		</td>' +
			'	</tr>' +
			'</table>';
		return resultsRowMarkup;
	}

	function printUserSpeed( userSpeed, packageSpeed )
	{
		return '<div style="color: #306918;	font-weight: bold;">' +
			printResultsRow( userSpeed, 'green', 'Your Speed Result', packageSpeed ) +
			'</div>';
	}

	function printSpeeds( speeds, userSpeed, packageSpeed )
	{	
		var results = '';
		var userSpeedPrinted = false;
		for (speed in speeds)
		{
			if (userSpeed < speeds[speed] && !userSpeedPrinted)
			{
				results += printUserSpeed( userSpeed, packageSpeed );
				userSpeedPrinted = true;
			}
			results += printResultsRow( speeds[speed], 'grey', '', packageSpeed );
		}
		if (!userSpeedPrinted)
		{
			results += printUserSpeed( userSpeed, packageSpeed );
		}
		return results;
	}
	
	function printResults( testResults )
	{
		var download = testResults[SPEED_DOWNLOAD]
		var upload = testResults[SPEED_UPLOAD]
		var packageDown = testResults[SVC_CLASS_DOWNLOAD]
		var packageUp = testResults[SVC_CLASS_UPLOAD]
		
		var packageStr = '';
		if (packageDown == undefined) { packageDown = ''; }
		if (packageUp == undefined) { packageUp = ''; }

		if (packageDown != '' && packageUp != '')
		{
			packageStr = packageDown + '/' + packageUp;
		}
		
		var resultsMarkup = 
			printBoxes(download, upload, packageStr) + 
			printHeader('Download speed test comparison', 'Connection Type', 'Connection Speed') +
			printSpeeds(downSpeeds, download, packageDown) +
			printHeader('Upload speed test comparison', 'Connection Type', 'Connection Speed') +
			printSpeeds(upSpeeds, upload, packageUp) +
			printFooter( packageStr, testResults );
		return resultsMarkup;
	}

//
////////////////////////////////////////////////////////////////////////////////
// Speed Test Start page functions and content
////////////////////////////////////////////////////////////////////////////////
//
	function printStartPage ()
	{
		var startPageMarkup = 
			'<h2>How <i>fast</i> is your<br /> ' +
			'<span style="font-size: larger;">FiOS connection?</span></h2> ' +
			'Verizon FiOS provides state-of-the-art connection speeds with its fiber optic ' +
			'technology. As a FiOS customer, you can measure the FiOS advantage by ' +
			'testing the upload and download speeds of your connection.<br /> ' +
			'<br /> ' +
			'The speed test measures the amount of data that is uploaded and downloaded ' +
			'in a set amount of time. In other words, it shows how fast data moves  ' +
			'through your FiOS fiber optic connection and demonstrates the superior ' +
			'speeds the Verizon FiOS provides.<br /> ' +
			'<br /> ' +
			'<a href="#" onclick="routeControl(\'start\'); return false;"><img src="' +	_apppath +	'/images/cover/check_my_speed_button.gif" alt="Check My Speed &gt;" width="164" height="28" border="0" /></a><br /> ' +
			'<br /> ' +
			'<b>How does the Verizon Online Speed Test work?</b><br /> ' +
			'The FiOS Speed Test sends bursts of random data for 10 seconds and ' +
			'calculates the upload and download speed of your connection by measuring ' +
			'how many bits of data were transferred during the 10-second period.<br /> ' +
			'<br />';
		return startPageMarkup;
	}
	
//
////////////////////////////////////////////////////////////////////////////////
// Speed Test Disclaimer page functions and content
////////////////////////////////////////////////////////////////////////////////
//
	function printDisclaimer ()
	{
		var disclaimerMarkup = 
			'<b>Disclaimer</b><br /><br /> ' +
			'Actual throughput speed will vary based on factors such as the condition of wiring inside a specific ' +
			'location, computer configuration, WiFi Adapters, network or internet congestion, and the server ' +
			'speed of the Web sites accessed, among other things. Speed and uninterrupted use of the ' +
			'service are not guaranteed.<br /> ' +
			'<br /> ' +
			'Click <b>Continue</b> to start the FiOS Speed Test.<br /> ' +
			'<br /> ' +
			'<a href="#" onclick="routeControl(\'disclaimer\'); return false;"><img src="'+ _imagepath + '/disclaimer/continue_button.gif" alt="Continue &gt;" width="85" height="18" border="0" /></a><br /> ' +
			'<br />';
		return disclaimerMarkup;
	}

//
////////////////////////////////////////////////////////////////////////////////
// Speed Test Select Your Region page functions and content
////////////////////////////////////////////////////////////////////////////////
//
	var req;

	function loadXMLDoc(url) {
		req = false;
	

	    if (window.XMLHttpRequest && !(window.ActiveXObject)) 
	    { // branch for native XMLHttpRequest object
	    	try 
	    	{
				req = new XMLHttpRequest();
	        } 
	        catch(e) 
	        {
				req = false;
	        }
	    } 
	    else if (window.ActiveXObject) 
	    { // branch for IE/Windows ActiveX version
	       	try 
	       	{
	        	req = new ActiveXObject("Msxml2.XMLHTTP");
	      	} 
	      	catch(e) 
	      	{
	        	try 
	        	{
	          		req = new ActiveXObject("Microsoft.XMLHTTP");
	        	} 
	        	catch(e) 
	        	{
	          		req = false;
	        	}
			}
	    }
		if(req) 
		{
			req.onreadystatechange = processReqChange;
			req.open("GET", _apppath + "/fetch_speedtest_server.asp?server=" + url, true);
			req.send("");
		}
	}

	function processReqChange() 
	{
	    // only if req shows "loaded"
	    if (req.readyState == 4) 
	    {
	        // only if "OK"
	        if (req.status == 200) 
	        {
	        	// set speed test server
	            _select_server = req.responseText;
	            
	            if (_select_server == '')
	            {
	            	alert('Couldn\'t retrieve the speed test server for the selected region.\n\nPlease try again later.');
	            	routeControl('disclaimer');
	            }
	            routeControl('region');	
	        }
	        else 
	        {
	            alert('Couldn\'t retrieve the speed test server for the selected region.\n\nPlease try again later.\n\nError: ' + req.statusText);
	            routeControl('disclaimer');
	        }
	    }
	}
	function getMapRegionSrc (selected)
	{
		var srcString = 'map_region_' + selected.toLowerCase() + '.src';
		return eval(srcString)
	}
	
	function setMapRegion (selected)
	{
		var srcString = 'map_region_' + selected.toLowerCase() + '.src';
		map_region_selected = selected;
		
		// change map
		document.getElementById('usamap').src = eval(srcString);
		
		var radioRegions = document.getElementsByName('region');
		
		// set radio button
		for (i = 0; i < radioRegions.length; i++)
		{
			if (radioRegions[i].value == selected)
			{
				radioRegions[i].checked = true;
			}
		}
	}

	function checkRegion ()
	{
		var radioRegions = document.getElementsByName('region');
		var regionSelected = false;
		
		// check for region
		for (i = 0; i < radioRegions.length; i++)
		{
			if (radioRegions[i].checked == true)
			{
				regionSelected = true;
			}
		}
		if (!regionSelected 
			|| map_region_selected == '')
		{
			alert ('Please select the region where you live.');
			return false;
		}
		else
		{
			switch(map_region_selected.toLowerCase())
			{
				case 'west':
					loadXMLDoc('http://la.speedtest.verizon.net/ServerIPFetcher/ServerName');
					break;
				case 'central':
					loadXMLDoc('http://speedtest.verizon.net/ServerIPFetcher/ServerName');
					break;
				case 'east':
					loadXMLDoc('http://res.speedtest.verizon.net/ServerIPFetcher/ServerName');
					break;
				default:
	            	alert('Couldn\'t retrieve the speed test server for the "' + map_region_selected + '" speed test region.\n\n Please try again later.\n');
	            	routeControl('disclaimer');
			}		
		}

	}
	
	function printSelectRegion ()
	{
		var selectRegionMarkup =
			'<p>To perform a speedtest please select the region where you live:</p> ' +
			'<img id="usamap" name="usamap" src="'+ _imagepath + '/region/usa_map.gif" width="400" height="276" style="border: 1px solid #000000;" usemap="#regions" /> ' +
			'<map name="regions"> ' +
			'	<area  ' +
			'		shape="poly"  ' +
			'		coords="82,13,89,35,86,45,91,46,95,59,107,58,103,92,115,94,104,179,91,176,64,161,48,157,26,132,20,116,14,75,20,52,33,19,32,5,48,5"  ' +
			'		href="javascript: setMapRegion(\'West\');"  ' +
			'		onclick="setMapRegion(\'West\'); return false;"  ' +
			'		onmouseover="usamap.src = map_region_west.src;" ' +
			'		onmouseout="usamap.src = getMapRegionSrc(map_region_selected);" ' +
			'		alt="West Region" /> ' +
			'	<area  ' +
			'		shape="poly"  ' +
			'		coords="86,14,158,26,212,28,243,33,230,44,226,66,237,72,239,82,245,88,238,108,246,119,247,123,256,135,249,154,303,150,319,149,337,152,341,240,102,231,120,92,106,88,111,56,97,53,94,42,90,42,93,34"  ' +
			'		href="javascript: setMapRegion(\'Central\');"  ' +
			'		onclick="setMapRegion(\'Central\'); return false;"  ' +
			'		onmouseover="usamap.src = map_region_central.src;" ' +
			'		onmouseout="usamap.src = getMapRegionSrc(map_region_selected);" ' +
			'	 	alt="Central Region" /> ' +
			'	<area  ' +
			'		shape="poly" ' +
			'		coords="382,12,394,34,355,140,343,151,331,143,312,142,297,147,255,149,260,134,251,120,250,114,242,106,244,95,250,90,250,86,243,81,240,68,230,61,232,45,252,43,258,35"  ' +
			'		href="javascript: setMapRegion(\'East\');"  ' +
			'		onclick="setMapRegion(\'East\'); return false;"  ' +
			'		onmouseover="usamap.src = map_region_east.src;" ' +
			'		onmouseout="usamap.src = getMapRegionSrc(map_region_selected);" ' +
			'		alt="East Region" /> ' +
			'</map><br /> ' +
			'<p> ' +
			'<input type="radio" name="region" value="West" onclick="setMapRegion(this.value);" />West region speed test server<br /> ' +
			'<input type="radio" name="region" value="Central" onclick="setMapRegion(this.value);" />Central region speed test server<br /> ' +
			'<input type="radio" name="region" value="East" onclick="setMapRegion(this.value);" />East region speed test server<br /> ' +
			'</p> ' +
			'<a href="#" onclick="checkRegion();"><img src="'+ _imagepath + '/region/btn_continue.gif" width="68" height="18" border="0" alt="CONTINUE &gt;" /></a><br /><br /> ';
		return selectRegionMarkup;
	}

//
////////////////////////////////////////////////////////////////////////////////
// Speed Test Select Your Speed page functions and content
////////////////////////////////////////////////////////////////////////////////
//
	function setSelectedSpeed()
	{
		var speed = document.getElementById("speed").options[document.getElementById("speed").selectedIndex].value;
		
		if (speed == undefined || speed == '')
		{
			alert("Please select a FiOS package speed!");
		}
		else
		{
			_speed = speed;
			_service_class = _speed;
			var temp2 = _service_class.split('_');
			_dl_class = temp2[0];
			_ul_class = temp2[1];
			routeControl('speedselection');
		}
	}

	function printSpeedOptions()
	{
		var speedOptions = '';
		PACKAGE_SPEED_DOWN = 0;
		PACKAGE_SPEED_UP = 1;
		for (package in _package_speeds)
		{
			var speeds = _package_speeds[package].split("/");
			speedOptions += '	<option value="' + 
				speeds[PACKAGE_SPEED_DOWN] + '_' + speeds[PACKAGE_SPEED_UP] + 
				'">Up to ' + speeds[PACKAGE_SPEED_DOWN] + ' Mbps / ' + speeds[PACKAGE_SPEED_UP] + ' Mbps</option>';
		}
		return speedOptions;
	}

	function printSelectSpeed()
	{
		var selectSpeedMarkup =
			'<b>Select Your Speed</b><br /><br />' +
			'This will allow you to analyze your network performance, both uploading ' +
			'and downloading. (Please note, this test may take up to 2 minutes to ' +
			'complete.)<br />' +
			'<br />' +
			'Please indicate your FiOS package maximum speed (download/upload) or allow us to <a href="' + _ssopath + top.location.href + '" target="_top">check it for you</a>.<br />' +
			'<br />' +
			'<form name="speeds" id="speeds">' +
			'<select name="speed" id="speed">' +
			'	<option value="">Select your speed</option>' +
			printSpeedOptions() +
			'</select><br />' +
			'<br />' +
			'<a href="#" onclick="setSelectedSpeed();"><img src="' + _imagepath + '/speeds/next_button.gif" alt="Next" width="45" height="18" border="0" /></a><br />' +
			'<br />' +
			'Not all speeds are available in all areas<br />' +
			'</form>';
		return selectSpeedMarkup;
	}


//
////////////////////////////////////////////////////////////////////////////////
// Speed Test Speedtest page functions and content
////////////////////////////////////////////////////////////////////////////////
//

	// -------------------------------------------------
	// NOTE: getPhase, getStatus, and getMessage are
	// called by the showTest... functions
	// -------------------------------------------------
	function getPhase(theMessage) {
		if (theMessage.length == 2 ||
				(theMessage.length > 2 && theMessage.substring(2,3) == ":")) {
			var theCode = theMessage.substring(0, 1);
			if (!isNaN(theCode)) {
				return theCode;
			}
		}
		return -1;
	}
	
	function getStatus(theMessage) {
		if (theMessage.length == 2 ||
				(theMessage.length > 2 && theMessage.substring(2,3) == ":")) {
			var theCode = theMessage.substring(1,2);
			if (!isNaN(theCode)) {
				return theCode;
			}
		}
		return -1;
	}
	
	function getMessage(theMessage) {
		// If there is a number-number-colon sequence, skip it.
		if (theMessage.length >= 3 &&
				!isNaN(theMessage.substring(0,2)) &&
				theMessage.substring(2,3) == ":") {
			return theMessage.substring(3,theMessage.length + 1);
		}
		return theMessage;
	}

	function getData() {
		var g_JWeb100 = document.getElementById("APPLET_JWEB100");

	    testResults[SPEED_UPLOAD] = parseInt(g_JWeb100.SpeedUpload) / 1000;
	    testResults[SPEED_DOWNLOAD] = parseInt(g_JWeb100.SpeedDownload) / 1000;
	    testResults[JWEB100_ANALYSIS] = g_JWeb100.ResultsIntermediate.toString();
	    testResults[SND_LIMITED_PCT] = g_JWeb100.SndLimitedPer;
	    testResults[RCV_LIMITED_PCT] = g_JWeb100.RecLimitedPer;
	    testResults[NET_LIMITED_PCT] = g_JWeb100.NetLimitedPer;
	    testResults[BAD_CABLE] = g_JWeb100.BadCable;
		testResults[NETWORK_CONGESTION] = g_JWeb100.NetworkCongestion;
	    testResults[OPTIMIZATION_NEEDED] = g_JWeb100.NeedToRunOptimizer;
	    testResults[HALF_DUPLEX] = g_JWeb100.HalfDuplex;
	    testResults[CLIENT_RCV_WINDOW_SIZE] = g_JWeb100.ClientRcvWin;
	    testResults[PACKET_LOSS_PCT] = g_JWeb100.PacketLoss;
	    testResults[DUPLEX_MISMATCH] = g_JWeb100.DuplexMismatch;
	    testResults[WIRELESS_LINK] = g_JWeb100.WirelessLink;
	    
	    // fix to deal with jweb100_analysis not being returned by server.
	    if (!testResults[JWEB100_ANALYSIS])
	    {
			testResults[JWEB100_ANALYSIS] = "No analysis information has been returned by the Speedtest server.";
	    }
				
	    testResults[JWEB100_USER_TEXT] = 
	        "Congratulations!  This test result indicates your service is operating normally.";
	    if (g_JWeb100.BadCable == 1) {
		    testResults[JWEB100_USER_TEXT] = 
	            "The test is indicating that you may have a bad Ethernet cable. " + 
				"<a href='http://help.verizon.net/'>Click here</a> " +
	            "for troubleshooting information.";
	    }
	    if (g_JWeb100.HalfDuplex == 1) {
	        bErr = 1;
	        eText += "<br />- PC network card configured for half duplex.";
	    }
	    
	    testResults[SHOW_TEST_RESULTS] = true;
	}

	// -------------------------------------------------
	// NOTE: showTestStatus is called -DIRECTLY- by
	// the JWeb100 applet.
	// -------------------------------------------------
	function showTestStatus(theStatus) {
		// These are displayed in the status table fields
		var aStatusText = new Array(6);
		aStatusText[0] = "Not Started";
		aStatusText[1] = "In Progress";
		aStatusText[2] = "Complete";
		aStatusText[3] = "Error";
		aStatusText[4] = "Failed";
		aStatusText[5] = "Failed";
		var nStatus = -1;
		var nPhase = -1;
	
		// These will be -1 if there is no phase/status encoding
		nStatus = getStatus(theStatus);
		nPhase = getPhase(theStatus);
		
		if (nPhase > 0)
		{
			document.getElementById("status" + nPhase ).innerHTML = aStatusText[nStatus];
			document.getElementById("status" + nPhase ).style.fontWeight = 'bold';
			document.getElementById("step" + nPhase ).style.fontWeight = 'bold';
		}
	  if (nPhase == 3 && nStatus > 1) 
		{
			document.getElementById("status" + 4).innerHTML = 'In Progress';
			document.getElementById("status" + 4 ).style.fontWeight = 'bold';
			document.getElementById("step" + 4 ).style.fontWeight = 'bold';
	  }
		testResults[STATUS] = theStatus;
	}

	// -------------------------------------------------
	// NOTE: showTestResults is called -DIRECTLY- by
	// the JWeb100 applet.
	// -------------------------------------------------
	function showTestResults() {
		// added 3 second delay for IE8/JDK issues
		setTimeout("showTestResultsDelayed()", 3000);
	}


	// -------------------------------------------------
	// NOTE: showTestError is called -DIRECTLY- by
	// the JWeb100 applet.
	// -------------------------------------------------
	function showTestError() {
		// added 3 second delay for IE8/JDK issues
		setTimeout("showTestErrorDelayed()", 3000);
	}

	function showTestResultsDelayed() {
		getData();	    
		routeControl( 'speedtest' );
	}
	
	function showTestErrorDelayed(theError) 
	{
	  showTestStatus(theError);
	  if (getStatus(theError) != 3) 
		{
			var errMsg;
			errMsg = getMessage(theError);
			if (getStatus(theError) == 4) { //transient
			    errMsg += "  Please restart the test.  If the problem persists, contact customer support.";
			} else {
			    errMsg += "  Please contact customer support for assistance.";
			}
		}
		testResults[SHOW_TEST_ERROR] = errMsg;
		testResults[SHOW_TEST_RESULTS] = false;
		routeControl( 'speedtest' );
	}
	
	
	function printSpeedtestCSS()
	{
		var speedtestCSSMarkup =
			'<style type="text/css">' +
			'	#monitor { ' +
			'		height: 93px;' +
			'		width: 267px;' +
			'		margin: 0 auto;' +
			'	}' +
			'	#monitor-top { ' +
			'		background: #fff url(' + _imagepath + '/run/top_monitors.gif) no-repeat;' +
			'		height: 33px;' +
			'		width: 267px;' +
			'	}' +
			'	#monitor-middle {' +
			'		height: 32px;' +
			'		width: 267px;' +
			'	}' +
			'	#mon-middle-left {' +
			'		background: #fff url(' + _imagepath + '/run/left_monitors.gif) no-repeat;' +
			'		height: 32px;' +
			'		width: 74px;' +
			'		float: left;' +
			'	}' +
			'	#mon-middle-center {' +
			'		background: #fff url(' + _imagepath + '/run/animated.gif) no-repeat;' +
			'		height: 32px;' +
			'		width: 120px;' +
			'		float: left;' +
			'	}' +
			'	#mon-middle-right {' +
			'		background: #fff url(' + _imagepath + '/run/right_monitors.gif)  no-repeat;' +
			'		height: 32px;' +
			'		width: 73px;' +
			'		float: left;' +
			'	}' +
			'	#monitor-bottom {' +
			'		background: #fff url(' + _imagepath + '/run/bottom_monitors.gif) no-repeat;' +
			'		height: 29px;' +
			'		width: 267px;' +
			'	}' +
			'	#tracker {' +
			'		width: 306px;' +
			'		margin: 0 auto 20px auto;' +
			'	}' +
			'	#tracker-label,' +
			'	#tracker-connection,' +
			'	#tracker-upload,' +
			'	#tracker-download,' +
			'	#tracker-analyze {' +
			'		width: 306px;' +
			'	}' +
			'	#step-label,' +
			'	#status-label {' +
			'		padding: 1px 0px 1px 7px;' +
			'		background-color: rgb(64, 132, 183); ' +
			'		color: rgb(255, 255, 255);' +
			'		font-weight: bold;' +
			'	}' +
			'	#step-label {' +
			'		background-image: url(' + _imagepath + '/run/blue_bar_corner.gif);' +
			'		background-repeat: no-repeat;' +
			'		width: 190px;' +
			'		float: left;' +
			'	}' +
			'	#status-label {' +
			'		width: 100px;' +
			'		float: right;' +
			'	}' +
			'	#step1, #status1, ' +
			'	#step2, #status2,' +
			'	#step3, #status3,' +
			'	#step4, #status4 {' +
			'		background-color: rgb(238, 238, 238); ' +
			'		color: rgb(0, 0, 0);' +
			'	}' +
			'	#step1,' +
			'	#step2,' +
			'	#step3,' +
			'	#step4 {' +
			'		padding: 1px 0px 1px 7px;' +
			'		margin: 0 0 1px 0;' +
			'		width: 190px;' +
			'		float: left;' +
			'	}' +
			'	#status1,' +
			'	#status2,' +
			'	#status3,' +
			'	#status4 {' +
			'		padding: 1px 0px 1px 7px;' +
			'		margin: 0 0 1px 0;' +
			'		width: 100px;' +
			'		float: right;' +
			'	}' +
			'	#step4 {' +
			'		background-image: url(' + _imagepath + '/run/bottom_left_gray.gif);' +
			'		background-repeat: no-repeat;' +
			'		background-position: bottom left;' +
			'	}' +
			'	#status4 {' +
			'		background-image: url(' + _imagepath + '/run/bottom_right_gray.gif);' +
			'		background-repeat: no-repeat;' +
			'		background-position: bottom right;' +
			'	}' +
			'	#legend {' +
			'		position: relative;' +
			'		background-color: #f4f4f4;' +
			'		border: 1px solid #cccccc;' +
			'		padding: 10px;' +
			'		width: 180px;' +
			'	}' +
			'	.corner {' +
			'		position: absolute;' +
			'		background-image: url(' + _imagepath + '/info/sprite_legend_nored.gif);' +
			'		background-color: #f5f5f5;' +
			'		width: 10px;' +
			'		height: 10px;' +
			'	}' +
			'	.topLeft {' +
			'		top: -1px;' +
			'		left: -1px;' +
			'		background-position: 0px 0px;' +
			'	}' +
			'	.topRight {' +
			'		top: -1px;' +
			'		right: -1px;' +
			'		background-position: -10px 0px;' +
			'	}' +
			'	.bottomLeft {' +
			'		bottom: -1px;' +
			'		left: -1px;' +
			'		background-position: 0px -10px;' +
			'	}' +
			'	.bottomRight {' +
			'		bottom: -1px;' +
			'		right: -1px;' +
			'		background-position: -10px -10px;' +
			'	}' +
			'</style>';
			return speedtestCSSMarkup;
	}

	function printSpeedTest ()
	{
		var speedtestMarkup = 
			'<b>Speed Test for ' + map_region_selected + ' Region</b><br />' +
			'<br />' +
			'This will allow you to analyze your network performance, both uploading and downloading. (Please note, this test may take up to 2 minutes to complete.)<br />' +
			'<br />' +
			'<div id="monitor">' +
			'	<div id="monitor-top">' +
			'	</div>' +
			'	<div id="monitor-middle">' +
			'		<div id="mon-middle-left">' +
			'		</div>' +
			'		<div id="mon-middle-center">' +
			'		</div>' +
			'		<div id="mon-middle-right">' +
			'		</div>' +
			'	</div>' +
			'	<div id="monitor-bottom">' +
			'	</div>' +
			'</div>' +
			'<br />' +
			'<div id="tracker">' +
			'	<div id="tracker-label">' +
			'		<div id="step-label">' +
			'			Step' +
			'		</div>' +
			'		<div id="status-label">' +
			'			Status' +
			'		</div>' +
			'	</div>' +
			'	<div id="tracker-connection">' +
			'		<div id="step1">' +
			'			Checking Connection' +
			'		</div>' +
			'		<div id="status1">' +
			'			 Not Started...' +
			'		</div>' +
			'	</div>' +
			'	<div id="tracker-upload">' +
			'		<div id="step2">' +
			'			Upload Test (10 seconds)' +
			'		</div>' +
			'		<div id="status2">' +
			'			Not Started...' +
			'		</div>' +
			'	</div>' +
			'	<div id="tracker-download">' +
			'		<div id="step3">' +
			'			 Download Test (10 seconds)' +
			'		</div>' +
			'		<div id="status3">' +
			'			Not Started...' +
			'		</div>' +
			'	</div>' +
			'	<div id="tracker-analyze">' +
			'		<div id="step4">' +
			'			Analyzing' +
			'		</div>' +
			'		<div id="status4">' +
			'			Not Started...' +
			'		</div>' +
			'	</div>' +
			'</div><br /><br />';
			return speedtestMarkup;
	}
	
//
////////////////////////////////////////////////////////////////////////////////
// Speed Test Information page functions and content
////////////////////////////////////////////////////////////////////////////////
//

	function printInformation( testResults )
	{
		var TEST_SERVER = 0;
		var CLIENT_PC = 1;
		var NETWORK = 2;
		var xo = new Array(3);
		
		var message;
		var helpcase = "";

		var passImg = _apppath + "/images/info/green_checkmark.gif";
		var failImg = _apppath + "/images/info/red_x.gif";

		var passAlt = 'Pass';
		var failAlt = 'Fail';		
		
		var testserverStateImg = failImg;
		var testserverStateAlt = failAlt;
		var customerpcStateImg = failImg;
		var customerpcStateAlt = failAlt;
		var networkStateImg = failImg;
		var networkStateAlt = failAlt;

		for (i = 0; i < 3; i++)
		{
			xo[i] = 0;
		}
		
		if (testResults[CLIENT_RCV_WINDOW_SIZE] < 300000)
		{
			if (isMac())
			{
				helpcase = "case=27244";
			}
			else
			{
				helpcase = "case=not_optimized";
			}
			message = 
				'Your PC has not been optimized for the premium experience offered by FiOS. ' +
				'<a href="' + _helppath + helpcase + 
				'" target="_top">Learn more about FiOS Optimizer</a> and run this test again.'
			xo[TEST_SERVER] = 1;
			xo[NETWORK] = 1;
		}
		else if ( speedLimit( testResults ) && testResults[PACKET_LOSS_PCT] >= 0.03 && testResults[PACKET_LOSS_PCT] <= 0.06)
		{
			helpcase = "packet_loss";
			message = 
				'We are sorry, but your test is being affected by a temporary network problem. ' +
				'<a href="' + _helppath + helpcase + 
				'" target="_top">Learn more about network issues</a> and test your PC speed later.';
			xo[TEST_SERVER] = 1;
			xo[CLIENT_PC] = 1;

		}
		else if ( speedLimit( testResults ) && testResults[PACKET_LOSS_PCT] >= 0.06)
		{
			message = 
				'There appears to be a problem affecting your speed due to serious packet loss. ' +
				'Please reboot your router and PC, and try the test again.  If the problem continues, call technical support. ' +
				'Consumers please call 1-888-553-1555. Business Customers please call 1-888-244-4440.';
			xo[TEST_SERVER] = 1;
		}
		else if ( speedLimit( testResults ) && testResults[SND_LIMITED_PCT] >= 0.10)
		{
			message = 'We’re sorry, our test server is experiencing a problem. Please try your test again later.';
			xo[CLIENT_PC] = 1;
			xo[NETWORK] = 1;
		}
		else if ( speedLimit( testResults ) && parseInt(testResults[BAD_CABLE]) != 0 )
		{
			helpcase = "check_ethernet";
			message = 
				'The test indicates that you may have a bad Ethernet cable. ' +
				'Please <a href="' + _helppath + helpcase + 
				'" target="_top">check the troubleshooting guide</a> for more information.';
			xo[TEST_SERVER] = 1;
			xo[NETWORK] = 1;
		}
		else if ( speedLimit( testResults ) &&
							( parseInt(testResults[DUPLEX_MISMATCH]) != 0 || 
								parseInt(testResults[HALF_DUPLEX]) != 0 || 
						 		parseInt(testResults[WIRELESS_LINK]) != 0 ) )
		{
			helpcase = "nic_config";
			message = 
				'To accurately measure the speed of your connection, you must use an Ethernet connected PC. ' +
				'If you receive this message, you are either testing from a wireless connection, ' +
				'or your PC’s Ethernet adapter may be configured incorrectly.  Please try your test from a wired connection, or ' +
				'<a href="' + _helppath + helpcase + 
				'" target="_top">learn how to reconfigure your adapter</a> for proper operation.';
			xo[TEST_SERVER] = 1;
			xo[NETWORK] = 1;
		}
		else if (!testResults[SHOW_TEST_RESULTS])
		{
			var failed_to_connect = testResults[SHOW_TEST_ERROR].indexOf('Failed to connect');
			if (failed_to_connect > -1)
			{
				testMessage = 'An error was encountered and no test results have been returned.';
			}
			else
			{
				testMessage = testResults[SHOW_TEST_ERROR] + '<br />Consumers please call 1-888-553-1555. Business Customers please call 1-888-244-4440.';
			}
		}
		else
		{
			message = "Congratulations!  This test result indicates your service is operating normally.";

			for (i = 0; i < 3; i++)
			{
				xo[i] = 1;
			}
		}
		
		
		if (xo[TEST_SERVER] == 1)
		{
			testserverStateImg = passImg;
			testserverStateAlt = passAlt;
		}
		
		if (xo[CLIENT_PC] == 1)
		{
			customerpcStateImg = passImg;
			customerpcStateAlt = passAlt;
		}
		
		if (xo[NETWORK] == 1)
		{
			networkStateImg = passImg;
			networkStateAlt = passAlt;
		}
	
		var infoMarkup = 
			'<b>Speed Information</b><br /><br />' +
			'<div id="info">' + message + '</div>' +
			'<br />' +
			'<table cellpadding="5" cellspacing="0" border="0" width="100%">' +
			'	<tr>' +
			'		<td colspan="2">' +
			'			<hr width="100%" />' +
			'		</td>' +
			'	</tr>' +
			'	<tr>' +
			'		<td width="40">' +
			'			<img id="testserver" name="testserver" src="' + testserverStateImg + '" alt="' + testserverStateAlt + '" width="16" height="15" border="0" /><br />' +
			'		</td>' +
			'		<td>' +
			'			<strong>Test Server</strong>' +
			'		</td>' +
			'	</tr>' +
			'	<tr>' +
			'		<td colspan="2">' +
			'			<hr width="100%" />' +
			'		</td>' +
			'	</tr>' +
			'	<tr>' +
			'		<td>' +
			'			<img id="customerpc" name="customerpc" src="' + customerpcStateImg + '" alt="' + customerpcStateAlt + '" width="16" height="15" border="0" /><br />' +
			'		</td>' +
			'		<td>' +
			'			<strong>Customer PC</strong>' +
			'		</td>' +
			'	</tr>' +
			'	<tr>' +
			'		<td colspan="2">' +
			'			<hr width="100%" />' +
			'		</td>' +
			'	</tr>' +
			'	<tr>' +
			'		<td>' +
			'			<img id="network" name="network" src="' + networkStateImg + '" alt="' + networkStateAlt + '" width="16" height="15" border="0" /><br />' +
			'		</td>' +
			'		<td>' +
			'			<strong>Network</strong>' +
			'		</td>' +
			'	</tr>' +
			'	<tr>' +
			'		<td colspan="2">' +
			'			<hr width="100%" />' +
			'		</td>' +
			'	</tr>' +
			'</table>' +
			'<br />' +
			'<div id="legend">' +
			'	<img src="' + _imagepath + '/info/green_checkmark.gif" alt="" width="19" height="16" border="0" /> = OK&nbsp;&nbsp;&nbsp;' +
			'	<img src="' + _imagepath + '/info/red_x.gif" alt="" width="16" height="15" border="0" /> = Problem' +
			'	<div class="corner topLeft"></div>' +
			'	<div class="corner topRight"></div>' +
			'	<div class="corner bottomLeft"></div>' +
			'	<div class="corner bottomRight"></div>' +
			'</div>' +
			'<br />' +
			'<table cellspacing="0" cellpadding="10" border="0">' +
			'	<tr>' +
			'		<td>' +
			'			<a href="#" onclick="routeControl(\'inforesults\'); return false;"><img src="' + _imagepath + '/info/back_to_results_button.gif" alt="&lt; Back to Results" width="108" height="18" border="0" /></a><br />' +
			'		</td>' +
			'		<td>' +
			'			See your speed test results.<br />' +
			'		</td>' +
			'	</tr>' +
			'	<tr>' +
			'		<td>' +
			'			<a href="#" onclick="routeControl(\'infoanalysis\'); return false;"><img src="' + _imagepath + '/info/test_analysis_button.gif" alt="Test Analysis &gt;" width="92" height="18" border="0" /></a><br />' +
			'		</td>' +
			'		<td>' +
			'			See detailed technical information.<br />' +
			'		</td>' +
			'	</tr>' +
			'</table>' +
			'<br /><br />';
		return infoMarkup;
	}

//
////////////////////////////////////////////////////////////////////////////////
// Speed Test Analysis page functions and content
////////////////////////////////////////////////////////////////////////////////
//

	function printAnalysis ( analysis )
	{
		var analysisMarkup = 
			'<b>Speed Test Analysis Information</b><br /><br />' +
			'<div id="analysis">' + analysis + '</div>' +
			'<br />' +
			'<table cellspacing="0" cellpadding="10" border="0">' +
			'	<tr>' +
			'		<td>' +
			'			<a href="#" onclick="routeControl(\'analysisresults\'); return false;"><img src="' + _imagepath + '/info/back_to_results_button.gif" alt="&lt; Back to Results" width="108" height="18" border="0" /></a><br />' +
			'		</td>' +
			'		<td>' +
			'			See your speed test results.<br />' +
			'		</td>' +
			'	</tr>' +
			'	<tr>' +
			'		<td>' +
			'			<a href="#" onclick="routeControl(\'analysisinfo\');; return false;"><img src="' + _imagepath + '/analysis/speed_info_button.gif" alt="Speed Information" width="125" height="18" border="0" /></a><br />' +
			'		</td>' +
			'		<td>' +
			'			See detailed technical information.<br />' +
			'		</td>' +
			'	</tr>' +
			'</table>' +
			'<br /><br />';
		return analysisMarkup;
	}
	
//
////////////////////////////////////////////////////////////////////////////////
// Speed Test Test Data Entry page functions and content
////////////////////////////////////////////////////////////////////////////////
//

	// THE FOLLOWING FUNCTIONS JUST EXIST FOR DIT/SIT TESTING!
	// --- test values for application logic
	function setToPct(elementID) {
		var theFloat = 0.0;
		
		theFloat = parseFloat(document.getElementById(elementID).value);
		if (isNaN(theFloat)) {
			theFloat = 0.0;
		}
		while (theFloat >= 1) {
			theFloat = theFloat / 100;
		}
		document.getElementById(elementID).value = theFloat.toPrecision(3);
	}

	function fixPctValues() {
		setToPct("packetLossPct");
		setToPct("sndLimitedPct");
		setToPct("rcvLimitedPct");
		setToPct("netLimitedPct");
	}
	
	function showTestDriverResults()
	{
		var badCable;
		var halfDuplex;
		var duplexMismatch;
		var wirelessLink;
		
		fixPctValues();
		testResults[SHOW_TEST_RESULTS] = true;
		testResults[SPEED_UPLOAD] = parseInt(document.getElementById("speedUpload").value) / 1000;
		testResults[SPEED_DOWNLOAD] = parseInt(document.getElementById("speedDownload").value) / 1000;
		testResults[SND_LIMITED_PCT] = document.getElementById("sndLimitedPct").value;
		testResults[RCV_LIMITED_PCT] = document.getElementById("rcvLimitedPct").value;
		testResults[NET_LIMITED_PCT] = document.getElementById("netLimitedPct").value;
		testResults[CLIENT_RCV_WINDOW_SIZE] = document.getElementById("clientRcvWinSz").value;
		testResults[PACKET_LOSS_PCT] = document.getElementById("packetLossPct").value;
		testResults[JWEB100_ANALYSIS] = 'Test results are missing.';
		testResults[SVC_CLASS_DOWNLOAD] = document.getElementById("svcClassDownload").value;
		testResults[SVC_CLASS_UPLOAD] = document.getElementById("svcClassUpload").value;
		testResults[SERVICE_CLASS] = testResults[SVC_CLASS_DOWNLOAD] + "_" + testResults[SVC_CLASS_UPLOAD];
		
		if (testResults[SVC_CLASS_DOWNLOAD] == '')
		{
			testResults[SVC_CLASS_DOWNLOAD] = undefined;
		}
		
		if (testResults[SVC_CLASS_UPLOAD] == '')
		{
			testResults[SVC_CLASS_UPLOAD] = undefined;
		}
		
		badCable = document.getElementsByName("badCable")
		for( var i = 0; i < badCable.length; i++ )
		{
			if( badCable[i].checked )
			{
				testResults[BAD_CABLE] = badCable[i].value;
			}
		}
		
		halfDuplex = document.getElementsByName("halfDuplex");
		for( i = 0; i < halfDuplex.length; i++ )
		{
			if( halfDuplex[i].checked )
			{
				testResults[HALF_DUPLEX] = halfDuplex[i].value;
			}
		}
	
		duplexMismatch = document.getElementsByName("duplexMismatch");
		for( i = 0; i < duplexMismatch.length; i++ )
		{
			if( duplexMismatch[i].checked )
			{
				testResults[DUPLEX_MISMATCH] = duplexMismatch[i].value;
			}
		}
		
		wirelessLink = document.getElementsByName("wirelessLink");
		for( i = 0; i < wirelessLink.length; i++ )
		{
			if( wirelessLink[i].checked )
			{
				testResults[WIRELESS_LINK] = wirelessLink[i].value;
			}
		}
		
		if (debug)
		{
			alert("" +
				"CLIENT_RCV_WINDOW_SIZE: " 	+ testResults[CLIENT_RCV_WINDOW_SIZE] + "\n" +
				"PACKET_LOSS_PCT: " 				+ testResults[PACKET_LOSS_PCT] + "\n" +
				"SVC_CLASS_DOWNLOAD: " 			+ testResults[SVC_CLASS_DOWNLOAD] + "\n" +
				"SVC_CLASS_UPLOAD: " 				+ testResults[SVC_CLASS_UPLOAD] + "\n" +
				"SPEED_UPLOAD: " 						+ testResults[SPEED_UPLOAD] + "\n" +
				"SPEED_DOWNLOAD: " 					+ testResults[SPEED_DOWNLOAD] + "\n" +
				"HALF_DUPLEX: " 						+ testResults[HALF_DUPLEX] + "\n" +
				"DUPLEX_MISMATCH: " 				+ testResults[DUPLEX_MISMATCH] + "\n" +
				"WIRELESS_LINK: " 					+ testResults[WIRELESS_LINK] + "\n" +
				"SND_LIMITED_PCT: " 				+ testResults[SND_LIMITED_PCT] + "\n" +
				"RCV_LIMITED_PCT: " 				+ testResults[RCV_LIMITED_PCT] + "\n" +
				"NET_LIMITED_PCT: " 				+ testResults[NET_LIMITED_PCT] + "\n" +
				"BAD_CABLE: " 							+ testResults[BAD_CABLE] + "\n");
		}
		routeControl('testentrynext');
	}

	function printTest ()
	{
		var testMarkup =
			'<form name="testDriver">' +
			'	<strong>Results Test Driver</strong><br />' +
			'	<table border="0" id="table7">' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Receive Window Size</td>' +
			'			<td><input type="text" id="clientRcvWinSz" name="clientRcvWinSz"></td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Packet Loss %</td>' +
			'			<td><input type="text" id="packetLossPct" name="packetLossPct"></td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Download Service Class</td>' +
			'			<td><input type="text" id="svcClassDownload" name="svcClassDownload"></td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Upload Service Class</td>' +
			'			<td><input type="text" id="svcClassUpload" name="svcClassUpload"></td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Download Speed</td>' +
			'			<td><input type="text" id="speedDownload" name="speedDownload"></td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Upload Speed</td>' +
			'			<td><input type="text" id="speedUpload" name="speedUpload"></td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Half Duplex</td>' +
			'			<td><input type="radio" value="1" name="halfDuplex">Yes' +
			'			<input type="radio" value="0" checked name="halfDuplex">No</td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Duplex Mismatch</td>' +
			'			<td><input type="radio" value="1" name="duplexMismatch">Yes' +
			'			<input type="radio" value="0" checked name="duplexMismatch">No</td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Wireless Link </td>' +
			'			<td><input type="radio" value="1" name="wirelessLink">Yes' +
			'			<input type="radio" value="0" checked name="wirelessLink">No</td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Sender Limited %</td>' +
			'			<td><input type="text" id="sndLimitedPct" name="sndLimitedPct"></td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Receiver (PC) Limited %</td>' +
			'			<td><input type="text" id="rcvLimitedPct" name="rcvLimitedPct"></td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Network Limited %</td>' +
			'			<td><input type="text" id="netLimitedPct" name="netLimitedPct"></td>' +
			'		</tr>' +
			'		<tr>' +
			'			<td align="right">&nbsp;</td>' +
			'			<td align="right">Bad Ethernet Cable</td>' +
			'			<td><input type="radio" value="1" name="badCable">Yes' +
			'			<input type="radio" value="0" checked name="badCable">No</td>' +
			'		</tr>' +
			'	</table>' +
			'</form>' +
			'<div align="center"><a href="#" onclick="showTestDriverResults(); return false;"><img src="' + _imagepath + '/speeds/next_button.gif" alt="Next" width="45" height="18" border="0" /></a><br /></div><br />';
		return testMarkup;
	}
	
	//----------------------------------------------------------------------------
	// routeControl is the brain of this application, it kicks of the next action
	// depending on the last user interaction.
	//----------------------------------------------------------------------------
	function routeControl ( lastCommand )
	{
		if (debug) { alert(lastCommand); }
		
		switch( lastCommand )
		{
			case 'start':
				switchContent( printDisclaimer() );
				break;
			case 'disclaimer':
				switchContent( printSelectRegion() );
				break;
			case 'region':
				zeroTestResults();
				// TODO: set TEST SERVER
				switchContent( 
					printSpeedTest() + 
					printJavaEmbed( _select_server, _debugging_flag, _local_test_flag, isIE() ) 
					);
				break;
			case 'speedselection':
				if (_speed != '' || _speed != undefined)
				{
					switchContent( printSelectRegion() );
					setMapRegion(map_region_selected);
				}
				else
				{	
					switchContent( printSelectSpeed() );
				}
				break;
			case 'speedtest':
				switchContent( printResults( testResults ) );
				break;
			case 'results':
				break;
			case 'resultsinfo':
				if (testResults[SVC_CLASS_DOWNLOAD] == undefined && testResults[SVC_CLASS_UPLOAD] == undefined)
				{
					switchContent( printSelectSpeed() );
				}
				else
				{
					switchContent( printInformation( testResults ) );
				}
				break;
			case 'resultsanalysis':
				switchContent( printAnalysis( testResults[JWEB100_ANALYSIS] ) );
				break;
			case 'resultsrunagain':
				if (testResults[SVC_CLASS_DOWNLOAD] == undefined && testResults[SVC_CLASS_UPLOAD] == undefined)
				{
					switchContent( printSelectSpeed() );
				}
				else
				{
					switchContent( printSelectRegion() );
					setMapRegion(map_region_selected);
				}
				break;
			case 'inforesults':
				switchContent( printResults( testResults ) );
				break;
			case 'infoanalysis':
				switchContent( printAnalysis( testResults[JWEB100_ANALYSIS] ) );
				break;
			case 'analysisresults':
				switchContent( printResults( testResults ) );
				break;
			case 'analysisinfo':
				if (testResults[SVC_CLASS_DOWNLOAD] == undefined && testResults[SVC_CLASS_UPLOAD] == undefined)
				{
					switchContent( printSelectSpeed() );
				}
				else
				{
					switchContent( printInformation( testResults ) );
				}
				break;
			case 'testentrynext':
				switchContent( printResults( testResults ) );
				break;
		}
	}	

	document.write(printSpeedtestCSS());
	document.write('<div id="st_content"></div>');
//	if(is_ie8)
//	{
//		if (window.location.search == '?ie8msg=false')
//		{
//			readSpeedFromCookie();
//			switchContent( printStartPage() );
//		}
//		else 
//		{
//			switchContent( printWrongBrowser() );
//		}
//	}
//	else 
	if( window.location.search == '?test=true' )
	{
		switchContent( printTest() );
	}
	else
	{
		readSpeedFromCookie();
		switchContent( printStartPage() );
	}
