function maxArray( arr ) {
	// this function calculates the maximum value in the associative array
	var max = 0;
	for(var i in arr){
  	    if (arr[i] > max)
  	    		max = arr[i];
  	}
  	return max;
}

function minArray( arr ) {
	// this function calculates the minimum value in the associative array
	var min;
	var count = 0;
	for(var i in arr){
		// initialize minimum value with first value of associative array
		if (count==0)
			min = arr[i];
		if (arr[i] < min)
  	    		min = arr[i];
  	    	count++;
  	}
  	return min;
}

function drawTagCloud(map, maxStyle) {
	// this function draws the tag cloud using the associative javascript array generated by the Notes view
	// calculate the maximum entries in the cloud. if not found, no tags are found
	var maxEntries = maxArray(map);
	if (maxEntries==0) {
		document.write('No tags found');
	}
	
	//	calculate the minimum entries in the cloud
	var minEntries = minArray(map);
	
	// calculate the range
	var range = maxEntries - minEntries;
	if (range <= 0)
		range = 1; 
	
	// loop through the tag map to draw each tag.
	for(var tag in map){
  		drawTag(tag, map[tag], minEntries, maxEntries, maxStyle);
  	}
}

function drawTag(tag, count, min, max, maxStyle) {

tag2 = replaceSubstring(tag, " ", "+")

url = '/clients/ulc/www/CMS2Content.nsf/search?OpenAgent&query=' + escape(tag2);

  // this function renders a tag line. first determine
  // the size tag to use for this tag count
  sizeTag = Math.round((((maxStyle-1)/(max-min))*count) +(1*max-maxStyle*min)/(max-min));
  
  // now write the link to the page
  document.write('<a href="' + url + '" class="tag' + sizeTag + '">' + tag + '</a> ');
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function
