//***************** AUTHOR INFORMATION *************************
//* Phone/number filtering script made by,                     *
//* Øyvind Hansen, oyhansen@yahoo.no, Norway                   *
//* 14.04.2003                                                 *
//**************************************************************

// TESTED IN:

// Opera:	OK
// IE:		OK
// NS:		?
// Mozilla	?

//****************** SCRIPT INFO v1.1 **************************
//* Form validation of a phone number. Remove all illegal      *
//* characters and assign to a given format                    *
//**************************************************************

//********************* FIX TO v1.1 ****************************
//* Now using a 'while', instead of a 'for'-loop.              *
//* Removed some 'waste code' and added an empty input check   *
//**************************************************************

//************************* OTHER INFO *************************
//* This is made for norwegian phone numbers which are of      *
//* length 8 and starts with country code +47                  *
//* Edit to fit your needs                                     *
//*                                                            *
//**> Free for use, but include author information header    <**
//* Please report any bugs & please rate it!                   *
//*                                                            *
//* http://www.hotscripts.com/Detailed/21683.html              *
//**************************************************************


function phoneFilter(form, format) {

	var input = form.value;

	if(input.length > 0) { //do not perform if empty input

		var numbers = ""; //store all the numbers here

		//process to remove non-numbers and spaces
		for(var i = 0; i < input.length; i++) {
			var char = input.charAt(i);
			if(!(isNaN(char) || char == " ")) numbers += char;
		}

		//remove country code, if any
		//if(numbers.substring(0, 2) == "47") numbers = numbers.substring(2, numbers.length);

		var output = ""; //assign numbers here

		//assign numbers to chosen format
		var n = 0, i = 0;
		while(i < format.length && n < numbers.length) {
			var char = format.charAt(i);
			if(char == "#") {
				output += numbers.charAt(n++)
			} else {
				output += char;
			}
			i++;
		}

		//give alert if length is less than 8.
		if(numbers.length < 10) {
			alert("The number must be of length 10");
			form.select();
		}

		form.value = output; //output to form
	}
}