function capWordsAndNoExtraSpaces(userText) {

  /* Find any periods not followed by a space and insert a space */

  userText = userText.replace(/\.(\w+)/g, ". $1");

  var wordArray = userText.replace("  "," ").replace("  ", " ").split(" ");

  var modifieduserText="";



  for (var j = 0; j < wordArray.length; j++) {

    modifieduserText = modifieduserText+wordArray[j].substring(0,1).toUpperCase();
 
    modifieduserText = modifieduserText+wordArray[j].substring(1).toLowerCase();

    modifieduserText = modifieduserText+" ";

  }


  /* Find hyphenated words, and capitalize the word following the hyphen */

  var hyphenMatch = /-([a-z]+)/;

  while(hyphenMatch.test(modifieduserText)){

    modifieduserText.match(hyphenMatch);

    var posthyphenSubstr = RegExp.$1.substring(0,1).toUpperCase();

	posthyphenSubstr = posthyphenSubstr+RegExp.$1.substring(1);

    modifieduserText = modifieduserText.replace(hyphenMatch, "-"+posthyphenSubstr);

  }



  /* Find the ordinal type name modifiers and capitalize them. II, IV, etc. */

  var Ordinals = /(\si+\s*|\siv\s*|\sv\s*)$/i;

  if (Ordinals.test(modifieduserText)){

  modifieduserText = modifieduserText.replace(Ordinals, RegExp.$1.toUpperCase());

  }



  /* Find Mc* or Mac* and capitalize the letter after */

  var mcMatch = /\s(Mc\w+)/;

  var macMatch = /\s(Mac\w+)/;

  if (mcMatch.test(modifieduserText)) {

      modifieduserText.match(mcMatch);

      var PostMcSubstr = RegExp.$1.substring(2,3).toUpperCase();

      PostMcSubstr = PostMcSubstr+RegExp.$1.substring(3);

      modifieduserText = modifieduserText.replace(mcMatch, " Mc"+PostMcSubstr);

  }

  else if (macMatch.test(modifieduserText)) {

      modifieduserText.match(macMatch);

      var PostMacSubstr = RegExp.$1.substring(3,4).toUpperCase();

      PostMacSubstr = PostMacSubstr+RegExp.$1.substring(4);

      modifieduserText = modifieduserText.replace(macMatch, " Mac"+PostMacSubstr);

  }

/* strip trailing spaces */
	var value = modifieduserText;
	startposn=0;
 	value=value.substring(startposn,value.length);
	endposn=(value.length)-1;
	while(value.charAt(endposn)==" ") {
		endposn--;
	}
	value=value.substring(0,endposn+1);
	
  return value;
}
