function FormatPhoneNumber(number_string, format_string) {

num_of_x = 0;

for (i = 0; i < format_string.length; i++) {

   if (format_string.charAt(i) == 'x') {

     num_of_x++;

   }

}

if (number_string.length != num_of_x) {

   return number_string;

}

else {

   formatted_string = "";

   pos = 0;

   for (i = 0; i < format_string.length; i++) {

     if (format_string.charAt(i) == 'x') {

       formatted_string += number_string.charAt(pos);

       pos++;

     }

     else {

       formatted_string += format_string.charAt(i);

     }

   }

   return formatted_string;

}

}
