/*=========================================================================================================================
| NORTH AMERICA PHONE NUMBER FORMATTING SCRIPT
|  
| Author:    Jeremy Caney, Ignia LLC (Jeremy@ignia.com)
| Client     Ignia
| Project    Library
|
| Purpose :  This script will automatically reformat a phone number field to follow North America phone number formatting
|            conventions.  This code should NOT be used for international phone number collection since the format varies
|            on a per country basis.
|
>=========================================================================================================================
| Revisions  Date        Author          Comments
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|            12.10.02    Jeremy Caney    Initial version created.
\------------------------------------------------------------------------------------------------------------------------*/

/*=========================================================================================================================
| DECLARE GLOBAL VARIABLES
\------------------------------------------------------------------------------------------------------------------------*/

  var strFormatPhoneOriginal;

/*=========================================================================================================================
| FUNCTION: FormatPhone()
>=========================================================================================================================
| Core function call; validates the phone number and alters the format
\------------------------------------------------------------------------------------------------------------------------*/

  function FormatPhone(oPhoneField) {

  //Declare Variables
    var p   = oPhoneField.value;
    var pp  = "";
    var pos = 0;

  //If Value Hasn't Changed, Exit
    if (p == strFormatPhoneOriginal){
      return(null);
      }
    else {
      strFormatPhoneOriginal = p;
      }

  //Strip Phone Characters
  //Note: This will only replace a single character (unlike VBS), so must run twice to handle repeated characters
    if(p.length > 0) {
      p = p.replace("(","");
      p = p.replace(")","");
      p = p.replace("-","");
      p = p.replace("x","");
      p = p.replace(" ","");
      p = p.replace(" ","");
      }
    
  //Handle Area Code
    if(p.length >= 3) {
      pos = 3;
      pp  = pp + "(" + p.substring(0,pos) + ") ";
      }
    
  //Handle Prefix
    if(p.length >= 6) {
      pos = 6;
      pp = pp + "" + p.substring(3,pos) + "-";
      }

  //Handle Number
    if(p.length >= 10) {
      pos = 10;
      pp = pp + "" + p.substring(6,pos);
      }

  //Handle Extensions
    if(p.length > 10) {
      pos = p.length;
      pp = pp + " x" + p.substring(10, pos);
      }
      
  //Unhandled Characters
    if (pos < p.length) {
      pp = pp + p.substring(pos, p.length)
      }

  //Return Formatted Phone Number
    oPhoneField.value = pp;
    
    }
