//Copywrite 1999, Kent E. Gunnison
//var form1 = document.forms["form1"];
var speed2 = 0;
var totalLength = 0;
var astring = "";
var astring2 = "" ;
var a = 0;
var b = "";
var s = " ";
var timerID = null;
var timerRunning = false;
var errors = 0;
var baderrors = 0;
var awfulerrors = 0;
var click = 0;
var a1 = "";
var b1 = "";
var buildstring = ""
var overwith = 0;
var timekeeper = 0;
var speed = 0;
var num = 61;
var abc = 0;
var testNumber = 1;
// Carry ON
function NextRandomNumber() {
	var hi = this.seed / this.Q;
	var lo = this.seed % this.Q;
	var test = this.A * lo - this.R * hi;
	if (test > 0)
		this.seed = test;
	else
		this.seed = test + this.M;
	return (this.seed * this.oneOverM);
}
//Original from a text book
function RandomNumberGenerator() {
	var d = new Date();
	this.seed = 2345678901 +
		(d.getSeconds() * 0xFFFFFF) +
		(d.getMinutes() * 0xFFFF);
	this.A = 48271;
	this.M = 2147483647;
	this.Q = this.M / this.A;
	this.R = this.M % this.A;
	this.oneOverM = 1.0 / this.M;
	this.next = NextRandomNumber;
	return this;
}
//parse new #
var rand = new RandomNumberGenerator();
function random() {
	a = Math.round(num * rand.next());
	form1.answer.value = a;
}
//Your Test String
function gen() {
	speed2 = 56;
	var astr = makeCharsetString()
	var ar = ''
	t = 0;
	unT = 0;
	for (j = 1; j <= 59; j++) {
		a = Math.round(num * rand.next());
		ar = astr.charAt(a)
		t = t + 1;
			if (t > 5) {
				t = 0
				ar = " "
			}
			if (j == 1 && a < 52 ) {
					ar = ar.toUpperCase()
			}
			if (j > 35 && t == 1 && a < 52 && unT == 0 ) {
					ar = ar.toUpperCase()
					unT = 1;
			}
				astring = astring + ar ;
	}
	totalLength = astring.length;
	form1.answer.value = astring;
form1.tester.focus()
}
// Word generator **********************************
var verb = new Word ("throw out", "have", "get", "eat", "glue", "feed", "fly", "drive", "shine", "read", "drop", "paint", "throw", "wash", "catch", "bury", "click on", "rip", "kick", "dry", "burn", "zip", "release", "rent", "give up", "ignore", "break", "don't hurt", "don't steal", "put away", "fix", "sell", "un-pack", "pack", "hide", "find", "oil", "remember");
var article = new Word ("a", "the", "my", "your", "their");
var adjective = new Word ("nice", "pretty", "smelly", "hairy", "yellow", "old", "new", "crazy", "smart", "big", "little", "small", "expensive", "cheap", "putrid", "cold", "hot", "funny", "bizarre", "zany", "exclusive", "extra-large", "extra", "buzzing", "excited", "yummy", "quiet", "quick", "qued", "fast", "dried", "shrunken", "skinny", "huge", "antique", "green");
var singularNoun = new Word ("dog", "cat", "shoe", "disk", "computer", "truck", "school", "auto", "plane", "floor", "ribbon", "mouse", "store", "garage", "house", "curtain", "wall", "key", "hammer", "bicycle", "camera", "paper", "sock", "string", "CD", "book", "monitor", "comb", "table", "wallet", "carrot", "tree");
var pluralNoun = new Word ("dogs", "cats", "shoes", "disks", "computers", "trucks", "schools", "autos", "planes", "floors", "ribbons", "mice", "stores", "garages", "houses", "curtains", "walls", "keys", "hammers", "bicycles", "cameras", "papers", "socks", "strings", "CDs", "books", "monitors", "combs", "tables", "wallets", "carrots", "trees");
//Read CH
function isVowel (ch) {
	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') 
		return true;
		return false;
}
//Is English Grammer OK?
function phrase (adjs) {
	var plural = (rand.next() >= .5);
	var vb = verb.toString();
	var first = vb.charAt(0);
	var vb = first.toUpperCase() + vb.substring(1,vb.length);
	var art = article.toString();
	var adj = new Object();
	for (var i = 0; i < adjs; i++)
		adj[i] = adjective.toString();
	var nn = (plural) ? pluralNoun.toString() : singularNoun.toString();
	if (plural && art == "a")
		art = "some";
	if (art == "a" ) {
		if (adjs > 0)
			first = adj[0].charAt(0);
		else
			first = adj[0].charAt(0);
		if (isVowel(first))
			art = "an";
}
//Test for correct form
var ph = vb + " " + art + " ";
for (i = 0; i < adjs; i++)
	ph += adj[i] + " ";
	ph += nn;
	return ph;
}
//Editor
function Word() {
	var argv = Word.arguments;
	var argc = argv.length;
	this.list = new Object();
	for (var i = 0; i < argc; i++)
		this.list[i] = argv[i];
	this.count = argc;
	this.toString = WordString;
	return this;
}
//Keep this part
function WordString() {
	var i = Math.round((this.count - 1) * rand.next());
	return this.list[i];
}
//End with this
function phraseTest() {
		speed2 = 56;
		astring = phrase(2) + ". " + phrase(2) + "."
		form1.answer.value = astring;
		totalLength = astring.length;
	form1.tester.focus();
	startTimer();
}
// New String
function makeCharsetString() {
	var astr
	astr='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789";:?.,!/'
	return astr
}
//Testing now
function beginTest() {
	testNumber ++;
	form1.answer.value = "";
	form1.tester.value = "";
	form1.ticker.value = "";
	form1.yourtotal.value = "";
	form1.err.value = "";
	errors = 0;
	overwith = 0;
	astring = "";
	astring2 = "";
	a1 = "";	
	b1 = "";
	timekeeper = 0;
	if ( document.form1.num_yn.selectedIndex == 0 ) {		// was 3
		phraseTest();
	} else {
		gen();
	}
	form1.calc.value = "BEGIN Test Number " + testNumber;
	startTimer();
}
//Get ready
function checkProgress() {
		astring2 = form1.tester.value;
	if ( astring2.length >= (astring.length) ) {
	//	form1.calc.focus();
		stopTimer();
		checkWork();
		ending();
	}
	if ( overwith == 0 ) {
		timerID = setTimeout("checkProgress()", 50);
		timerRunning = true;
		timekeeper++
//	} else {
//		checkWork();
	}
}
//unclock
function stopTimer(){
	//stop the clock
	if(timerRunning) {
		clearTimeout(timerID)
		timerRunning = false
	}
} 
//New clock
function startTimer(){
     // Stop the clock (in case it's running), then make it go.
    stopTimer();
    if (overwith == 0) {
		checkProgress();
	}
}
//Next step
function checkWork() {
	errors = 0;
	baderrors = 0;
	awfulerrors = 0;
	click = 0;
	for (j = 0; j <= totalLength; j++ ) {
		a1 = astring2.charAt(j);
		b1 = astring.charAt(j);
			if ( a1 != b1 ) {
				errors = errors + 1;
			}
	}
// if ( errors > 0 ) {
	for (j = 0; j <= totalLength; j++ ) {
		a1 = astring2.charAt(j);
			if ( a1 == astring.charAt(j + 1) 
				&& astring.charAt(j) != astring.charAt(j + 1)) {
				baderrors = baderrors + 1;
			}
			if	( a1 == astring.charAt(j + 2) 
				&& astring.charAt(j + 1) != astring.charAt(j + 2)) {
				baderrors = baderrors + 1;
			}
			if ( a1 == astring.charAt(j - 2) 
				&& astring.charAt(j - 1) != astring.charAt(j - 2)) {
				baderrors = baderrors + 1;
			}
			if ( a1 == astring.charAt(j - 1)
				&& astring.charAt(j) != astring.charAt(j - 1)) {
				baderrors = baderrors + 1;
			}
			click++;
	}	
//	}
	if (errors == 0) return;
//	alert("errors = " + errors );
//	alert("baderrors = " + baderrors);
//	alert("Click = " + click);
	if ( errors != 0 ) awfulerrors = errors;
	errors = Math.abs( errors - baderrors );
	if ( errors == 0 ) errors = awfulerrors;
//	alert("errors = " + errors );
}

//Is length correct?
function ending() {
	document.all.thead.style.visibility = "visible"
	nice = 0;
	//form1.len.value = totalLength;
	overwith = 1;
	newlength = astring2.length;
	total_words = newlength - errors;
	gwpm = Math.floor(speed2 / (timekeeper / 20) * (Math.round(newlength / 5)));
	speed = Math.floor(speed2 / (timekeeper / 20) * (Math.round(total_words / 5)));
	form1.yourtotal.value = Math.floor(gwpm);	
	form1.ticker.value = Math.floor(speed);
	form1.err.value = errors;
	if ( form1.ticker.value > 60 && form1.err.value < 1 && num > 50 ) {
		astudent();
	}
	if ( form1.err.value < 1 && form1.ticker.value > 29 ) {
		goodstudent();
	}
}
//Section break!
function loadhelp() {
	window.open('kbhelp.htm', 'Note', 'toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=300,height=400');
}
//Offer Help
function changer() {
	var r = 0;
	r = document.form1.num_yn.selectedIndex;
		if ( r == 1 ) {		// was 1
			num = 51;		// was 51
		}
		if (r == 2 ) {		// was 0
			num = 60;		// was 61
		}
		if ( r == 3 ) {		// was 2
			num = 69;		// was 69
		}
}
//NIX everything
function reseter() {
	astring = "";
	astring2 = "";
	form1.answer.value = "";
	form1.tester.value = "";
	form1.ticker.value = "";
	form1.yourtotal.value = "";
	form1.err.value = "";
}
// 1-time warning!
function helpWarning() {
	alert('Be sure to click on the "HELP" / "READ ME"\nbutton before attempting to use the next page.')
	location.href='keyboard.htm'
}
// 9th grade student!
function astudent() {
	window.open('astudent.htm', 'Note','toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,copyhistory=no,width=300,height=100');
}
// 9th grade speed page!
function goodstudent() {
	window.open('goodstudent.htm',
'Note','toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,copyhistory=no,width=300,height=100');
}
// 9-out-of-10 aint bad!
function goodby() {
	if(confirm("If . . . you haven't e-mailed your comments about this page,\nthey would be greatly appreciated at this time."))
	location.href="mailto:kentg@icehouse.net";
}
//Copywrite 1999, Kent E. Gunnsion