//***************** 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              *
//**************************************************************

//KenG added the length parameter, so that you can have the alert go
//when you want it to.
function phoneFilter(form, format, length) {

	var input = form.value;

	if(input && 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 c = input.charAt(i);
			if(!(isNaN(c) || c == " ")) numbers += c;
		}

		//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 c = format.charAt(i);
			if(c == "#") {
				output += numbers.charAt(n++)
			} else {
				output += c;
			}
			i++;
		}

		//give alert if length is less than what is passed in.
		//Instead of checking against 8, I have it check against
		//the length passed in.
		if ( length != null && length != 'unknown')
		{
			if(numbers.length < length) {	//KenG
				alert("The length of the number must be: " + length);	//KenG
				form.focus();
			}
		}

		form.value = output; //output to form
	}
}

function removePhoneFilter(form, format)
{
	var i = 0;
	var data = form.value;
	var output = "";
	
	while (i < format.length)
	{
		if ( format.charAt(i) == "#" )
		{
			output += data.charAt(i);
		}
		i++;
	}

	return output;
}
