// ************************************************************
// The following code implements the onchange() event for the phone number text fields
// The functions are:
//
//     1) isDigit(string)
//     2) makeNumeric(string)
//     3) addHyphens(string)
// ************************************************************



  // This function tests a string to see if it is equal to a numeric character
  function isDigit(strNumber){
    if (  (strNumber == "0")    ||
      (strNumber == "1")    ||
      (strNumber == "2")    ||
      (strNumber == "3")    ||
      (strNumber == "4")    ||
      (strNumber == "5")    ||
      (strNumber == "6")    ||
      (strNumber == "7")    ||
      (strNumber == "8")    ||
      (strNumber == "9")    ) {
      
      return true
      
    } else {
    
       return false
       
     }
  }


  // This function converts any string into a new string containing only
  // numeric characters (0-9)
  //    Precondition:   argument, myString, must be a string of characters
  //    Postcondition: returns newString, which contains only numeric characters.
  function makeNumeric(myString) {
    var newString = ""   // This stores the new string

    // This loop iterates through the old string
    for (var i = 0; i < myString.length; i++) {
      next = myString.charAt(i) // Read a character
      
      if (isDigit(next)) {      // If the character is a number, store it in the new string
        newString += next
      }
    } 
    
    return newString
  }



  // This function adds hyphens to a string in the format of a phone number
  // (ie. "0123456789" is changed to "012-345-6789")
  //    Precondition:   argument, myString, must be a string of characters
  //    Postcondition: returns newString, which contains two hyphens.   
  function addHyphens(myString) {
    var strHyphen = "-" // This stores a hyphen for later insertion
    var newString = ""   // This stores the new string

    // This loop iterates through the old string
      for (var i = 1; i <= 10; i++) {
      
      if ((i == 4)||(i == 7))  {    // If the 4th or 7th positions are being read, insert a hyphen
        newString += strHyphen
      }
      
      newString += myString.charAt(i - 1)
    }
    
    return newString
  }    

    
    

  // Finds and replaces a target character in the string
  // Returns the new string
  function replaceTargetChar(myString, targetChar, replacementChar) {
    var newString = ""   // This stores the new string

    // This loop iterates through the old string
    for (var i = 0; i < myString.length; i++) {
      next = myString.charAt(i) // Read a character
      
      if (next == targetChar) {      // If the character is the targetChar, replace it with the replacementChar
        next = replacementChar
      } else {
      }
      newString += next
    } 
    return newString
  }


  // This function is called from the text field's onchange() event-handler.
  // It replaces any invalid characters in the text field
  function onChangeText(myName)
  {
    var myValue = document.forms[0][myName].value
        
    if (myValue != "") {
      myValue = replaceTargetChar(myValue, "'", "\"")    // Replace all single quote characters (') with double quote characters (")
          document.forms[0][myName].value = myValue
        }
  }
  
  // This function is called from the Home_Phone textfield's onchange() event-handler. It calles two
  // functions which remove any non-numeric characters from the Home_Phone string and then formats
  // the string with the proper parentheses and hyphens.
  function onChangePhone(myName)
  {
    var myValue = document.forms[0][myName].value
        
    if (myValue != "") {
      myValue = makeNumeric(myValue)
          myValue = addHyphens(myValue)
          document.forms[0][myName].value = myValue
        }
  }

  
  // This function is called from the Home_Phone textfield's onchange() event-handler. It calles two
  // functions which remove any non-numeric characters from the Home_Phone string and then formats
  // the string with the proper parentheses and hyphens.
  function onChangePhoneExt(myName)
  {
    var myValue = document.forms[0][myName].value
        
    if (myValue != "") {
      myValue = makeNumeric(myValue)
          document.forms[0][myName].value = myValue
        }
  }
  
  
  // This function is called from the text field's onchange() event-handler.
  // It replaces any invalid characters in the text field
  function onChangeEmail(myName)
  {
    var myValue = document.forms[0][myName].value
        
    if (myValue != "") {
      myValue = replaceTargetChar(myValue, "'", "\"")    // Replace all single quote characters (') with double quote characters (")
          document.forms[0][myName].value = myValue
        }
  }

  
  