function displayWords() {
	
	var xmldoc = XML.load('rich_ii.xml');	// load the xml
	
	// get and display the title
	var docTitleNode = xmldoc.getElementsByTagName("TITLE");
	docTitle = docTitleNode[0].childNodes[0].nodeValue;
	document.writeln("<h1 class='title'>" + docTitle + "</h1><hr />");
	
	// get the main array with words and count
	var wordCountArray = getWordCount(xmldoc);
	wordCountArray.sort(sortRandom);		// randomize the array
	wordCountArray.sort(sortRandom);		// randomize the array
	wordCountArray.sort(sortRandom);		// randomize the array
	
	// make a copy of the main array and sort it so we can figure out the highest count
	var sortedArray = wordCountArray.slice(0);
	var highestCount = sortedArray.sort(sortWordCount)[sortedArray.length - 1][1];

	
	for (var i=0; i < wordCountArray.length; i++) {
		
		var word = wordCountArray[i][0];
		var count = wordCountArray[i][1];
		
		if (count) {
			
			// algorithm for font size
			var fontSize = 12 + (count/highestCount)*150;
		
			// algorithm for font color
			var fontColor = 
				"rgb(" + 
				Math.round(Math.random()*255) + 
				"," + 
				Math.round(Math.random()*255) + 
				"," + 
				Math.round(Math.random()*255) + 
				")";
			
			// write the word to the page
			document.writeln(
				" <span style='" +						// open the span
				"font-size: " + fontSize + "px;" +		// font size
				"color: " + fontColor + "'>" +			// font color
				word +									// word
				"</span>");								// close the span
		
		}
		
	}
	
};

function sortWordCount(a, b) {
	
	var count1 = a[1];
	var count2 = b[1];
	
	return ((count1 < count2) ? -1 : ((count1 > count2) ? 1 : 0));
	
};

function sortRandom(a, b) {
	
	return (Math.round(Math.random())-0.5);
	
};

function getWordCount(xmldoc) {
	
	var allLines = xmldoc.getElementsByTagName("LINE");		// get all the lines from the play
	
	// we'll maintain 2 arrays. One with the words/counts and one with just the words
	var wordCountArray = new Array(0, 2);		// hold the words and their count
	var listOfWords = new Array();				// list of words
	var highestCount = 0;
	
	
	for (var i=0; i < allLines.length; i++) {
	
		// grab the text from the <LINE> element
		var line = allLines[i].childNodes[0].nodeValue;

		if (line) {
			
			// remove unwanted characters from the string
			line = formatLine(line);
		
			// split the line into an array of each word
			var lineWords = line.split(" ");
			
			for (var x=0; x < lineWords.length; x++) {
				
				var word = lineWords[x].toLowerCase();
				
				// put the new word in the array if it doesn't already exist
				if (listOfWords.indexOf(word) == -1) {
					
					// add the word to the list of words
					listOfWords[listOfWords.length] = word;
					
					// put the word in the multidimensional array
					wordCountArray[wordCountArray.length] = [ word, 1 ]
				
				// increment the count if the word is already in the array
				} else {
					
					// get the position of the word in the array
					var wordPos = listOfWords.indexOf(word);
					
					// increment the word count
					wordCountArray[wordPos][1]++;
					
					if (wordCountArray[wordPos][1] > highestCount) {
						highestCount = wordCountArray[wordPos][1];
					}
					
				}
	
			}
		
		}

	}
	
	return wordCountArray;
	
};

function formatLine(text) {

	text = text.replace(/,/g, "");
	text = text.replace(/\./g, "");
	text = text.replace(/-/g, " ");
	text = text.replace(/"/g, "");
	text = text.replace(/\'s/g, "");
	text = text.replace(/\?/g, "");
	text = text.replace(/\!/g, "");
	text = text.replace(/\:/g, "");
	text = text.replace(/\;/g, "");
	text = text.replace(/\'d/g, "");
	text = text.replace(/\'/g, "");
	
	return text;
	
};