// JavaScript Document

<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

<!-- Original: Michael Tartaglia <stonedstan@hotmail.com> -->
<!-- Web Site:  http://www.geocities.com/SiliconValley/Horizon/5235 -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
msg = new Array("People who are considered creative have developed an ability to be innovative, to generate original ideas, and to seek solutions in non-traditional ways.  Creativity lies within every person, yet only a few people will cultivate this skill on a regular basis.  In today’s society, creativity often sets apart the successful manager from the struggling employee, the productive volunteer group from the failing company, or the honours degree student from the average student.  Fresh ideas and creative thinking are a critical part of success on the job, at home, or at school.  In order to cultivate creativity, there are six common obstacles that we must recognize and overcome in order to reach our full creative potential.\n\nThe first stumbling block to creative thinking is the need for order.  While planning is often truly constructive, rigidly sticking to a program can minimize your chances of finding a new solution.  This is particularly true when you are working on a simple or familiar problem.")

function beginIt() {
	randNum = Math.floor((Math.random() * 1)) % 4
	msgType = msg[randNum]
	day = new Date();
	startType = day.getTime();
	document.theForm.given.value = msgType
	document.theForm.typed.focus();
	document.theForm.typed.select();
}

function cheat() {
	alert("You can not change that!");
	document.theForm.typed.focus();
}

function stopIt() {

alert("Thank you for completing the test, click OK to calculate your results.  Please be patient this may take a few moments.");

word=document.theForm.typed.value.split(' ').length;

dayTwo = new Date();
endType = dayTwo.getTime();
totalTime = ((endType - startType) / 1000)
totalErrors = levenshteinenator(document.theForm);
spd = Math.round((word/totalTime) * 60)
	if (document.theForm.typed.value == document.theForm.given.value) {
		alert("\nYou typed a " + word + " word sentence in " 
		+ totalTime + " seconds, a speed of about " + spd + " words per minute!");

		document.form2.wpm_show.value=spd;
		document.form2.wpm.value=spd;
		document.form2.timetaken.value=totalTime;
		document.form2.timetaken2.value=totalTime;
		document.form2.words.value=word;
		document.form2.words2.value=word;
		document.form2.errors.value=totalErrors;
		document.form2.errors2.value=totalErrors;

		}
		else {
			alert("You made " + totalErrors + " error(s), but in " + totalTime + " seconds you typed " + word + " words at a speed of " + spd + " words per minute.")
			
		document.form2.wpm_show.value=spd;
		document.form2.wpm.value=spd;
		document.form2.timetaken.value=totalTime;
		document.form2.timetaken2.value=totalTime;
		document.form2.words.value=word;
		document.form2.words2.value=word;
		document.form2.errors.value=totalErrors;
		document.form2.errors2.value=totalErrors;
   		}
   		
   		
}


/*

Copyright (c) 2006. All Rights reserved.

If you use this script, please email me and let me know, thanks!

Andrew Hedges
andrew (at) hedges (dot) name

If you want to hire me to write JavaScript for you, see my resume.

http://andrew.hedges.name/resume/

*/

// calculate the Levenshtein distance between a and b, fob = form object, passed to the function
levenshteinenator = function(fob) {
	var cost;
	
	// get values
	var a = fob['given'].value;
	var m = a.length;
	
	var b = fob['typed'].value;
	var n = b.length;
	
	// make sure a.length >= b.length to use O(min(n,m)) space, whatever that is
	if (m < n) {
		var c=a;a=b;b=c;
		var o=m;m=n;n=o;
	}
	
	var r = new Array();
	r[0] = new Array();
	for (var c = 0; c < n+1; c++) {
		r[0][c] = c;
	}
	
	for (var i = 1; i < m+1; i++) {
		r[i] = new Array();
		r[i][0] = i;
		for (var j = 1; j < n+1; j++) {
			cost = (a.charAt(i-1) == b.charAt(j-1))? 0: 1;
			r[i][j] = minimator(r[i-1][j]+1,r[i][j-1]+1,r[i-1][j-1]+cost);
		}
	}
	
	return r[m][n];
}

// return the smallest of the three values passed in
minimator = function(x,y,z) {
	if (x < y && x < z) return x;
	if (y < x && y < z) return y;
	return z;
}
//-->