var ms = 0;
var ton = 0;
var then;
var now;

function showcomment(text) {
	document.getElementById('comment').style.display = 'block';
	document.getElementById('comment').innerHTML = text;
	document.getElementById('comment').style.left = (cX+10) + "px";
	document.getElementById('comment').style.top = (cY+30) + "px";
}

function hidecomment() {
	document.getElementById('comment').style.display = 'none';
}

function UpdateCursorPosition(e){ cX = e.pageX; cY = e.pageY;}
function UpdateCursorPositionDocAll(e){ cX = event.clientX; cY = event.clientY;}
if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }


	function startstop() {
		if (ton == 0) {
			ton = 1;
			then = new Date();
			then.setTime(then.getTime() - ms);
		} else {
			ton = 0;
			now = new Date();
			ms = now.getTime() - then.getTime();
			s = roundNumber(ms / 1000, 1);
			document.getElementById('thetimer').innerHTML = s;
		}
	}

	function display() {
		setTimeout("display()", 100);
		if (ton == 1) {
			now = new Date();
			ms = now.getTime() - then.getTime();
			s = roundNumber(ms / 1000, 1);
			document.getElementById('thetimer').innerHTML = s;
		}
	}

	function roundNumber(num, dec) {
		var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
		return result;
	}

	function getValueFromApplet(){

		if (document.QoSTest.isReady() == true) {

			if (document.submittingform.isp.value == '' || document.submittingform.advertised.value == '' || document.submittingform.modem.value == '') {
				document.getElementById('status').innerHTML='Test completed. Please fill in your details below and click \'Submit\'.';
				alert('Test completed. Please fill in your details below and click \'Submit\'.');
				document.getElementById('allfields').style.visibility = 'visible';
				return false;
			} else if (document.submittingform.userrating.value == '') {
				document.getElementById('status').innerHTML='Please rate your connection to continue.';
				alert('Please rate your connection and click \'submit\' to continue.');
				document.getElementById('allfields').style.visibility = 'visible';
				return false;
			} else {
				document.submittingform.results.value = document.QoSTest.getResults();
				return true;
			}
		} else {

			document.getElementById('status').innerHTML='Please wait while the test is running.';

			if (document.submittingform.isp.value == '' || document.submittingform.advertised.value == '' || document.submittingform.modem.value == '') {
				document.getElementById('allfields').innerHTML = '<span class="checker">All fields are required. </span>';
				document.getElementById('allfields').style.visibility = 'visible';
			
			} else if (document.submittingform.userrating.value == '') {
				document.getElementById('allfields').style.visibility = 'visible';
				document.getElementById('allfields').innerHTML = '<span class="checker">Please rate your connection. </span>';
				
			} else {
				document.getElementById('allfields').style.visibility = 'hidden';
			}

			return false;
		}
	}

	function grabApplet() {
		if (document.QoSTest.isReady() == true) {
			startstop();
			if (document.submittingform.isp.value == '' || document.submittingform.advertised.value == '' || document.submittingform.modem.value == '') {
				document.getElementById('status').innerHTML='Test completed. Please fill in your details below and click \'Submit\'.';
				alert('Test completed. Please fill in your details below and click \'Submit\'.');
				document.getElementById('allfields').style.visibility = 'visible';
			} else if (document.submittingform.userrating.value == '') {
				document.getElementById('status').innerHTML='Please rate your connection to continue.';
				alert('Please rate your connection and click \'submit\' to continue.');
				document.getElementById('allfields').style.visibility = 'visible';
			} else {
				getValueFromApplet();
				document.submittingform.submit();
			}
		} else {
			setTimeout('grabApplet()', 2000);
			if (ton==0 && document.QoSTest.isRunning()) {
				display();
				startstop();
			}
		}
	}
