//   This JavaScript capitalizes the first letter of all 
//   words and makes rest of the letters lowercase. 
//   It also removes extra space between words. 
//
//   Two values need to be specified in the call, the form 
//   name and the field name to be processed.  Do that by 
//   adding the following code to the submit button input tag: 
//
//   onclick="return CapitalizeNames('formname', 'fieldname')" 
//
//   replacing 'formname' and 'fieldname' with actual Form Name 
//   and Field Name in single quotes. 

function CapitalizeNames(formname, fieldname)
{
var ValueString = new String();
eval('ValueString=document.' + formname + '.' + fieldname + '.value');
ValueString = ValueString.replace(/ +/g,' ');
var names = ValueString.split(' ');
for(var i = 0; i < names.length; i++)
   {
      if(names[i].length > 1)
         {
            names[i] = names[i].toLowerCase();
            letters = names[i].split('');
            letters[0] = letters[0].toUpperCase();
//   Properly handle names with an apostrophe (e.g. O'Grady) 
            if(letters[1] == "'")
               {
                  if(letters[2])
                     { letters[2] = letters[2].toUpperCase() }
//                Check for names with "Mc" after the apostrophe (e.g."O'McDonald") 
                  if((letters[2] == "M") && (letters[3] == "c"))
                     {
                        if(letters[4])
                           { letters[4] = letters[4].toUpperCase() }
                     }
//                Check for names with "Mac" after the apostrophe (e.g."O'MacDugal") 
                  if((letters[2] == "M") && (letters[3] == "a") && (letters[4] == "c"))
                     {
                        if(letters[5])
                           { letters[5] = letters[5].toUpperCase() }
                     }
               }
//   Properly handle names with "Mc" 
            if((letters[0] == "M") && (letters[1] == "c"))
               {
                  if(letters[2])
                     { letters[2] = letters[2].toUpperCase() }
               }
//   Properly handle names with "Mac" 
            if((letters[0] == "M") && (letters[1] == "a") && (letters[2] == "c"))
               {
                  if(letters[3])
                     { letters[3] = letters[3].toUpperCase() }
               }
//   For hyphenated names 
            for(var n = 0; n < letters.length; n++)
            {
               if(letters[n] == "-")
               {
                  if(letters[n+1])
                     { letters[n+1] = letters[n+1].toUpperCase() }
//                Check for names with an apostrophe after the hyphen (e.g."-O'Grady") 
                  if(letters[n+2] == "'")
                     {
                        if(letters[n+3])
                           { letters[n+3] = letters[n+3].toUpperCase() }
                     }
//                Check for names with "Mc" after the hyphen (e.g."-McDonald") 
                  if((letters[n+1] == "M") && (letters[n+2] == "c"))
                     {
                        if(letters[n+3])
                           { letters[n+3] = letters[n+3].toUpperCase() }
                     }
//                Check for names with "Mac" after the hyphen (e.g."-MacDugal") 
                  if((letters[n+1] == "M") && (letters[n+2] == "a") && (letters[n+3] == "c"))
                     {
                        if(letters[n+4])
                           { letters[n+4] = letters[n+4].toUpperCase() }
                     }
               }
            }
            names[i] = letters.join('');
         }
            else { names[i] = names[i].toUpperCase() }
   }
ValueString = names.join(' ');
eval('document.' + formname + '.'+ fieldname + '.value=ValueString');
return true;
}
