
var strtest="This is a free typing speed test which tests your typing speed. Typing speed is defined in wpm, that is words typed per minute. A word is taken to be five characters long including blank spaces. Typing speed is also defined as strokes typed per minute. This typing speed test will tell you your typing speed in wpm, words per minute, as soon as you finish typing this short paragraph.";
var start=1;	//allows timer to start
var xp=0;	//possition of letter to be typed
var starttyping=0;
var endtyping=0;
var numofletters=0;
var mistakes=0;
var addmistake = 1;
var f1;
var t1;
var e1;
var obj1;
function init(){
	browser();
	if(ns4){
		e1=document.yourerror;
		}
	else if(ie4){
		e1=yourerror;
		}
	else if(ns6){
	f1=document.getElementById("idmain");
	t1=document.getElementById("tentry");
	e1=document.getElementById("yourerror");
		}
	document.forms[0].elements[0].value="";
	hiderror();
	if(linux){
	document.onkeypress = compare;  
	}
	}





function starttest(){
	start=1;
	xp=0;
	starttyping=0;
	endtyping=0;
	numofletters=0;
	document.forms[0].elements[0].value="";
	document.forms[0].elements[0].focus();
	}

//function endtest(){
	//}
function hiderror(){
	if(ns6 || ie4)e1.style.visibility="hidden";
	if(ns4)e1.visibility="hide";
	}	

function showerror(){
	if(ns6 || ie4)e1.style.visibility="visible";
	if(ns4)e1.visibility="show";
	}	


function compare(e){

	var keyChar;
	var k = strtest.length;
	if(start==1){
	start=2;
	var beginning=new Date();
	starttyping=beginning.getTime();
	}
	if(xp < (k-1))
	{
	if(ns4)keyChar = String.fromCharCode(e.which);
	if(ns6)keyChar = String.fromCharCode(e.which);
	if(ie4)keyChar = String.fromCharCode(e.keyCode);

	var ch1;
	if(ie4)ch1=strtest.charAt(xp);
	else ch1=strtest[xp];

	if(ch1==keyChar)
	{
	addmistake = 1;
	hiderror();
	xp+=1;
	numofletters+=1;
	return true;
	}
	else
	{

var j1="";
var j2="";
var j3="";

j1="<B><font color=\"black\" size=\"+1\"><tt>&nbsp;error<\/tt><\/font><font color=\"red\" size=\"+2\"><I>!<\/I><\/font><font color=\"black\" size=\"+1\"><tt>&nbsp;type: <\/TT><\/font><\/B>";


    if( strtest.charCodeAt(xp) == 32 ){
j2="<font color=\"black\" size=\"+1\"><b><tt> space bar&nbsp;<\/tt><\/b><\/font>";
    }
    else
    {
j3="<font color=\"black\" size=\"+2\"><b><tt>"+ch1+"&nbsp;<\/tt><\/b><\/font>";

    }
finalerror='<center>'+j1+j2+j3+'<\/center>';
if(ns4){
	e1.document.open();
	e1.document.write(finalerror);
	e1.document.close();
	
	}
else e1.innerHTML='<center>'+j1+j2+j3+'<\/center>';

showerror();
if(addmistake == 1)mistakes+=1;
addmistake=2;
return false;
	}
	}
	else
	{
	
	excercise_results();
	}
	}

function stopkey(e){
	var ch;
		if(ie4){
	ch=e.keyCode;
	if(ch==8){
	xp=xp-1;
	return;
	}
	}	
	}


function excercise_results(){
	start=1;	//reset time-count initilizer
	var endtime=new Date();	
	endtyping=endtime.getTime();
	if(numofletters>0){
	var words=numofletters/5;
	var seconds=(endtyping-starttyping)/1000;
	var wpm=(words*60)/seconds;
	wpm=Math.round(wpm);
alert('your typing rate is :  '+wpm+' wpm\n mistakes made : '+mistakes);

	cleartest();
	}
	}

function cleartest(){
	xp=0;
	start=1;	//reset time-count initilizer
	numofletters=0;
	mistakes=0;
	addmistake=1;
	}


