	var stopwords_en = new Array("I","a","about","an","are","as","at","be","by","com","for","from","how","in","is","it","of","on","or","that","the","this","to","was","what","when","where","who","will","with","the");


	function doit() {
		var input=document.getElementsByName('input')[0];
		var output=document.getElementsByName('output')[0];
		var optionPreserveCase=document.getElementsByName('preservecase')[0];
                var optionDeleteStopwords=document.getElementsByName('deletestopwords')[0];

		output.value='working...';
		
		// periods and hyphens are possible word separators - replace them with a space
		var a=input.value.replace(/[\.\-,]/g,' ');
		output.value+='.';

		// Lowercase the input if checked
		if(!optionPreserveCase.checked) a=a.toLowerCase();
                output.value+='.';

		// Replace multiple whitepace with single space
                a=a.replace(/\s+/g,' ');
                output.value+='.';

                // remove stopwords
		if(optionDeleteStopwords.checked) {
	                for(var i=0;i<=stopwords_en.length;i++){
				var re = new RegExp(' ' + stopwords_en[i] + ' ', 'gi');				
				a=a.replace(re,' ');
	        	        output.value+='.';
			}
		}

		// Split the input on spaces
		var b=a.split(' ');
                output.value+='.';

			

		// Sort the resulting array
		b.sort();
                output.value+='.';
		var lastkw='';
		var frequency=0;
		var d=[];

		// for each entry in the input array
		for(var i=0;i<=b.length;i++) {
			frequency++;
			
			// if we have a repeat of the last keyword then just increment the frequency count
			if(b[i]==lastkw) continue;

			// we have a new word, finish on the old word and start the new one
			if(lastkw!='') d[lastkw]=frequency;
			lastkw=b[i];
			frequency=0;
		}
                output.value+='.';
	
		// Sort the output array by frequency
		var e = sortAssoc(d);
                output.value+='.';
		
		// assemble the result set
		var rs='';
		for (var key in e) {
			rs=rs+key+',' +e[key] + '\n';
		}
                output.value+='.';

		// apply the result set to the output box in one go (waaaay faster than one line at a time)
		output.value=rs;
	}	
	function clearall() {
                document.getElementsByName('input')[0].value="";
                document.getElementsByName('output')[0].value="";
	}

	function sortAssoc(aInput) {
		var aTemp = [];
		for (var sKey in aInput) aTemp.push([sKey, aInput[sKey]]);
		aTemp.sort(function () {return arguments[0][1] - arguments[1][1]});
		var aOutput = [];
		for (var nIndex = aTemp.length-1; nIndex >=0; nIndex--)
		aOutput[aTemp[nIndex][0]] = aTemp[nIndex][1];
		return aOutput;
	}
