//***************** 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 chara = input.charAt(i);
			if(!(isNaN(chara) || chara == " ")) numbers += chara;
		}

		var output = ""; //assign numbers here

		//assign numbers to chosen format
		var n = 0;
		i = 0;
		while(i < format.length && n < numbers.length) {
			chara = format.charAt(i);
			if(chara == "#") {
				output += numbers.charAt(n++)
			} else {
				output += chara;
			}
			i++;
		}

		//give alert if length is less than 8.
		if(numbers.length < 10) {
			alert("The telephone number must be 10 digits");
		}

		form.value = output; //output to form
	}
}


function phoneFilter2(form) { // phone filter 2 permits text FOLLOWING the phone number, such as "ext 1234"

	var input = form.value;
	var format ="(###) ###-###############";
	if(input.length > 0) { //do not perform if empty input

		var numbers = ""; //store all the numbers here

		//process to remove typical phone formatting characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]\-]/  // anything preceded by a backslash is illegal
		var numbercount = 0;  // initialize
 		for(var i = 0; i < input.length; i++) {
	 		var chara = input.charAt(i);
	 		if (!(chara.match(illegalChars))) { // if chara is not an illegal character
	 			if (! ((isNaN(chara)) || (chara==" ")) ) {  // if chara is a number and not a space
					if (numbercount>=1){  // if this isn't the first number
						numbercount ++;  // chara is a number, increment count for later error check
	 					numbers += chara;  // add it to the output
					}
					else{  // if numbercount <1 (meaning if this is the first number
						if (!(chara=="1")){  // and the first character is not a leading "1" (which we don't want)
							numbercount ++;  // chara is a number, increment count for later error check
	 						numbers += chara;  // add it to the output
						}
					}		
				}
				else{  // if chara is not a number or is a space...
	 				if (i>=10){  // if we've passed ten characters, anything... 
	 					numbers += chara;  // ...(i.e.: ext 123), is okay.  Add it to the output
	 				} 
	 			}  // end of else chara is not a number or a space
	 		}  // end of illegal characters if 
		}	// end of "for" (0 to input.length) 	
		var output = ""; //assign numbers here

		//assign numbers to chosen format
		var n = 0;
		i = 0;
		while(i < format.length && n < numbers.length) {
			chara = format.charAt(i);
			if(chara == "#") {
				output += numbers.charAt(n++)
			} else {
				output += chara;
			}
			i++;
		}

		//give alert if length is less than 10.
		if(numbercount < 10) {  
			alert("The telephone number must be 10 digits");
		}

		form.value = output; //output to form
	}
}



function zipFilter(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 chara = input.charAt(i);
			if(!(isNaN(chara) || chara == " ")) numbers += chara;
		}

		var output = ""; //assign numbers here

		//assign numbers to chosen format
		var n = 0;
		i = 0;
		while(i < format.length && n < numbers.length) {
			chara = format.charAt(i);
			if(chara == "#") {
				output += numbers.charAt(n++)
			} else {
				output += chara;
			}
			i++;
		}

		//give alert if length is not 5 or 9.
		if(!(numbers.length == 9 || numbers.length == 5)) {
			alert("The zip code must be 5 or 9 digits");
		}

		form.value = output; //output to form
	}
}

function dateFilter(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 chara = input.charAt(i);
			if(!(isNaN(chara) || chara == " ")) numbers += chara;
		}

		var output = ""; //assign numbers here

		//assign numbers to chosen format
		var n = 0;
		i = 0;
		while(i < format.length && n < numbers.length) {
			chara = format.charAt(i);
			if(chara == "#") {
				output += numbers.charAt(n++)
			} else {
				output += chara;
			}
			i++;
		}

		//give alert if length is less than 8.
		if(numbers.length < 8) {
			alert("The date number must be 8 digits formatted ##/##/#### - mo/da/year");
			output = "";  // if it's bogus delete it
	
		}

		form.value = output; //output to form
	}
}
function passwordLength(form) {
	var Password = form.value;
	if(Password.length <6) { //six characters minumum
		alert("Password Must be 6 or more characters!");
		form.value = "";
		}
}


function passwordConfirm(form) {

	var Password, ConfPassword, Result;
	Password = document.Contact.contact_password.value;
	ConfPassword = document.Contact.contact_password2.value;
	Result = document.Contact.txtResult;
	if(Password != ConfPassword){
		alert("The passwords do not match");
		document.Contact.contact_password.value = "";
		document.Contact.contact_password2.value = "";
		document.Contact.contact_password.select();
		}
}




function alphaFilter(form) {
	var input = form.value;

	if(input.length > 0) { //do not perform if empty input

		var letters = ""; //store all the letters here

		//process to remove non-numbers and spaces
		for(var i = 0; i < input.length; i++) {
			var chara = input.charAt(i);
			var test = escape(chara); // escape returns %-something unless alpha or numberic
			if((test.charAt(0) != "%") || (chara == " ")){ // if first character of variable "test" is not %, 
			if((isNaN(chara)) || (chara == " ")) letters += chara;  // and if "chara" is not a number, add to "letters"
			}	
		}

		//give alert if length is less than 2.
		if(letters.length < 2) {
			alert("This field must be 2 or more characters");
			form.select();
		}
		form.value = letters; //output to form
	}
}


function alphaUpperFilter(form) {
	var input = form.value;

	if(input.length > 0) { //do not perform if empty input

		var letters = ""; //store all the letters here

		//process to remove non-numbers and spaces
		for(var i = 0; i < input.length; i++) {
			var chara = input.charAt(i);
			var test = escape(chara); // escape returns %-something unless alpha or numberic
			if((test.charAt(0) != "%") || (chara == " ")){ // if first character of variable "test" is not %, 
			if((isNaN(chara)) || (chara == " ")) letters += chara.toUpperCase();  // and if "chara" is not a number, add to "letters"
			}	
		}

		//give alert if length is less than 2.
		if(letters.length < 2) {
			alert("This field must be 2 or more characters");
			form.select();
		}
		form.value = letters; //output to form
	}
}


function alphaQuickCaps(form) {  // changes letters to upper case on keypress
	var input = form.value;

	if(input.length > 0) { //do not perform if empty input

		var letters = ""; //store all the letters here

		//process to remove non-numbers and spaces
		for(var i = 0; i < input.length; i++) {
			var chara = input.charAt(i);
			var test = escape(chara); // escape returns %-something unless alpha or numberic
			if((test.charAt(0) != "%") || (chara == " ")){ // if first character of variable "test" is not %, 
			if((isNaN(chara)) || (chara == " ")) letters += chara.toUpperCase();  // and if "chara" is not a number, add to "letters"
			}	
		}

		form.value = letters; //output to form
	}
}





function alphaLowerFilter(form) {
	var input = form.value;

	if(input.length > 0) { //do not perform if empty input

		var letters = ""; //store all the letters here

		//process to remove non-numbers and spaces
		for(var i = 0; i < input.length; i++) {
			var chara = input.charAt(i);
			var test = escape(chara); // escape returns %-something unless alpha or numberic
			if((test.charAt(0) != "%") || (chara == " ")){ // if first character of variable "test" is not %, 
			if((isNaN(chara)) || (chara == " ")) letters += chara.toLowerCase();  // and if "chara" is not a number, add to "letters"
			}	
		}

		//give alert if length is less than 2.
		if(letters.length < 2) {
			alert("This field must be 2 or more characters");
			form.select();
		}
		form.value = letters; //output to form
	}
}



function fixedFilter(form, fixed)

{
	var i;
	var val = form.value;

	// make a string - to preserve the zeros at the end
	val = val + '';
	
	var decimalPos = val.indexOf('.');
	if (decimalPos == -1)
	{
		val += '.';
		for (i=0; i<fixed; i++)
		{
			val += '0';
		}
	}
	else
	{
		var actualDecimals = (val.length - 1) - decimalPos;
		var difference = fixed - actualDecimals;
		for (i=0; i<difference; i++)
		{
			val += '0';
		}
	}
	
	form.value = val;
}



function numericFilter(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 chara = input.charAt(i);
			if(!(isNaN(chara) || chara == " ")) numbers += chara;
		}

		var output = ""; //assign numbers here

		//assign numbers to chosen format
		var n = 0;
		i = 0;
		while(i < format.length && n < numbers.length) {
			chara = format.charAt(i);
			if(chara == "#") {
				output += numbers.charAt(n++)
			} else {
				output += chara;
			}
			i++;
		}

		form.value = output; //output to form
	}
}



function emailFilter (form) {

form.value=form.value.toLowerCase();

var emailStr = form.value;

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}



function placeFocus() {
if (document.forms.length > 0) {
var field = document.forms[0];
for (var i = 0; i < field.length; i++) {
if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
document.forms[0].elements[i].focus();
break;
         }
      }
   }
}

function bwidth(){
	if (document.Frequencies.equipment.selectedIndex<7){
		document.Frequencies.bandwidth_units[0].click();			 
		}
	else {
		document.Frequencies.bandwidth_units[1].click();
		}
}


function agile(){
	if (document.Frequencies.frequency_agile.checked){
		// document.Frequencies.frequency_agile_lower.value=0;			 
		document.Frequencies.frequency_agile_lower.readOnly=0;			 
		document.Frequencies.frequency_agile_upper.readOnly=0;			 
		document.Frequencies.frequency_agile_span.readOnly=0;			 
		document.Frequencies.frequency_agile_lower.focus();			 
		}
	else {
		document.Frequencies.frequency_agile_lower.value="";			 
		document.Frequencies.frequency_agile_lower.readOnly=1;
		document.Frequencies.frequency_agile_upper.value="";			 
		document.Frequencies.frequency_agile_upper.readOnly=1;
		document.Frequencies.frequency_agile_span.value="";			 
		document.Frequencies.frequency_agile_span.readOnly=1;
		document.Frequencies.tpo.focus();			 
		}
}

