//phone format code
var previous_phone_length = null;
var previous_ssn_length   = null;
var international         = false;
function autoFormat(input,type) {
  //lock out NS4
  if (!document.layers) {
    if (type == 'phone') {
      var add_first_paren   = false;
      var add_second_paren  = false;
      var add_dash          = false;
      var areacode_length   = 3;
      var first_three       = 3;
      var this_input        = null;
      var this_areacode     = null;
      var this_first_three  = null;
      var this_last_four    = null;
      
      this_input            = input.value.replace(/[. ()-\/]/gi,'');
      
      // handle single character entry
      if (input.value.length == 1) {
        //if first input is a "+" or "0" assume its an international number and do not format
        if (input.value == '+' || input.value == '0') { international = true; return true;}
        else if (input.value == '(') { previous_phone_length = 1; return true;}
      } else {
        international = (input.value.substr(0,1) == '+' || input.value.substr(0,1) == '0');
        //dont do anything on backspace
        if (input.value.length >= previous_phone_length && international == false) {
          this_areacode    = this_input.substr(0,3);
          this_first_three  = this_input.substr(3,3);
          this_last_four    = this_input.substr(6,4);
          
          //add '('
          if (this_input.length > 0) { add_first_paren = true; }
          //add ') '
          if (this_areacode.length == areacode_length) { add_second_paren = true; }
          //add '-'
          if (this_first_three.length == first_three) { add_dash = true; }
          
          //add everything, assign to field
          if (add_first_paren) { this_areacode = '(' + this_areacode; }
          if (add_second_paren) { this_areacode += ') '; }
          if (add_dash) { this_first_three += '-'; }
          input.value = this_areacode + this_first_three + this_last_four;
        }
      }
      previous_phone_length = input.value.length;
    } else if (type == 'ssn') {
      //declare variables
      var first_group_length  = 3;
      var second_group_length = 2;
      var third_group_length  = 4;
      var this_input          = null;
      var first_group         = null;
      var second_group        = null;
      var third_group         = null;
      var add_first_dash      = false;
      var add_second_dash     = false;
      
      this_input              = input.value.replace(/[^0-9]/gi,'');
      
      //dont do anything on backspace
      if (input.value.length >= previous_ssn_length) {
        first_group   = this_input.substr(0, first_group_length);
        second_group  = this_input.substr(first_group_length, second_group_length);
        third_group   = this_input.substr(first_group_length + second_group_length, third_group_length);
        
        //add first dash
        if (first_group.length == first_group_length) { add_first_dash = true; }
        //add second dash
        if (second_group.length == second_group_length) { add_second_dash = true; }
        
        //add everything, assign to field
        if (add_first_dash) { first_group += '-'; }
        if (add_second_dash) { second_group += '-'; }
        if (third_group.length > third_group_length) { third_group = third_group.substr(0, third_group_length); }
        
        input.value = first_group + second_group + third_group;
      }
      previous_ssn_length = input.value.length;
    }
  }
}