/*********************************************************************************************
 * Algorithm used to convert a hexadecimal color string to RGB (Red, Green, Blue) triplet    *
 * values. Make sure the input hexadecimal values are in the range of 00...FF.               *
 *                                                                                           *
 * Example code for obtaining equivalent RGB (Red, Green, Blue) values of a color from a     *
 * hexadecimal string.                                                                       *
 *                                                                                           *
 *   rowBgColor = document.getElementById("idClientRow"+suffix).style.backgroundColor;       *
 *                                                                                           *
 *   if(rowBgColor != "")                                                                    *
 *      rowBgColor  = hexToRGB(rowBgColor);                                                  *
 *                                                                                           *
 * Regular expressions are delimited by forward slashes (/).                                 *
 *                                                                                           *
 *           Regular Expression (RegExp) Meta Characters and Character Modifiers.            *
 *                                                                                           *
 * Character             Matches                             Example                         * 
 * ---------  -----------------------------  ----------------------------------------        *
 *     +      one or more preceding term     /ah+x/ matches ""ahx" or "ahhhhx"               * 
 *     *      zero or more preceding term    /ah*x/ matches "aax", "ahx", or "ahhhhx"        * 
 *     \      Escape changes meaning of the  See the following.                              * 
 *            next character                                                                 * 
 *     \b     Word boundary                  /\bto/ matches "to" in "today"                  * 
 *     \s     Single white space             /cross\sbrowser/ matches "cross browser"        * 
 *   [...]    One of set                     /th[eo]se/ matches "those" or "these"           * 
 *   {n,}     n or more preceding term       /\d{2,} matches "456" but not "4"               * 
 *     |      or                             /John|Sara/ matches "John" in "Call John        *
 *                                            and Bob." or "Sara" in "Also get Sara."        * 
 *     ^      At beginning of line           /^Hi/ matches "Hi" in "Hi Mark"                 * 
 *     $      At the end of a line           /Mark$/ matches "Mark" in "and Mark"            * 
 *                                                                                           *
 *                             Regular Expression (RegExp)Flags                              *
 *                                                                                           *
 * These flags modify the whole expression. Use "g" for global matching and "i" for case     *
 * insensitive matches. The global flag ("g") allows multiple replacements in the case of    *
 * the replace method.                                                                       * 
 *                                                                                           *
 * RegExp                                                                                    *                                            
 *  Flag   Flag Meaning                              Description                             *
 * ------  -------------  ------------------------------------------------------------------ * 
 *   g     Global Search  The RegExp searches for a pattern throughout the string, replacing *
 *                        all occurance or creating an array of all matches.                 * 
 *   i     Ignore Case    The regular expression becomes case insensitive.                   * 
 *********************************************************************************************/
function hexToRGB(string)
  {
   string +='';				// Forced to string.

   if(string.indexOf('rgb') == 0)
     {
      return string;			// Return input RGB (Red, Green, Blue) triplet as output.
     }		

   string = string.replace(/#/g, '');
   string = string.replace(/^\s*rgb/i, ' ');
   string = string.replace(/[,\(\)]/g, ' ');
   string = string.replace(/\s{2,}/g, ' ');
   string = string.replace(/^[\s\b]+|[\s\b]+$/g, '');
   string = string.replace(/\s+/g, '');

   var output = [];

   // Input is a hexadecimal color.
   var three     = (string.length <= 3);
   var increment = (!three || string.length == 2)?2:1;

   for(var s = 0; s < string.length; s+=increment)
      {
       output[++output.length - 1] = (!three)?

       parseInt(string.substring(s, s + 2), 16):
       parseInt(string.substring(s, s + 1) + string.substring(s, s + 1), 16);
      }

   return ( 'rgb(' + output.join(',') + ')' );
  }

