//   capFirstNameLastName.js 
//
//   This version handles first and last name as separate fields. 
//
//   This JavaScript capitalizes the first letter of all 
//   words and makes rest of the letters lowercase. 
//   It also removes extra space between words. 
//
//   The values that need to be specified in the call are the form 
//   name and the field names to be processed.  That's done 
//   by adding the following to the submit button input tag: 
//
//   onclick="return CapitalizeNames('formname', 'fieldname1', 'fieldname2')" 
//
//   replacing 'formname' and the fieldnames with actual Form Name 
//   and Field Names in single quotes. 


function CapitalizeNames(formname, fieldname1, fieldname2)
{
var ValueString1 = new String();
var ValueString2 = new String();

eval('ValueString1=document.' + formname + '.' + fieldname1 + '.value');
eval('ValueString2=document.' + formname + '.' + fieldname2 + '.value');

//--------------------------------------------------------
//   Process the first field (fieldname1)  
//--------------------------------------------------------
ValueString1 = ValueString1.replace(/ +/g,' ');
var names = ValueString1.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 AU Webmaster (used in testing) 
            if((names[i] == "AU") || (names[i] == "au"))
               { letters[1] = letters[1].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() }
   }
ValueString1 = names.join(' ');

//--------------------------------------------------------
//   Process the second field (fieldname2)  
//--------------------------------------------------------
ValueString2 = ValueString2.replace(/ +/g,' ');
var names = ValueString2.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 AU Webmaster (used in testing) 
            if((names[i] == "AU") || (names[i] == "au"))
               { letters[1] = letters[1].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() }
   }
ValueString2 = names.join(' ');

//--------------------------------------------------------
//   End of fieldname processing 
//--------------------------------------------------------

eval('document.' + formname + '.' + fieldname1 + '.value=ValueString1');
eval('document.' + formname + '.' + fieldname2 + '.value=ValueString2');
return true;
}
