// common validation functions
// 06/15/05 BPB - created
// 01-03-06 BPB - added JSValidateEmail
// 12-18-06 BPB - JSValidateEmail now allows a 4-character domain name (e.g.: .aero)
// 07-05-07 BPB - added JSRadioValue
// 12-06-07 BPB - allow \n in phone numbers so Bill can add line breaks
// 2008-02-06 BPB - allow \n in email addresses so Bill can send to multiple users
// 2009-04-07 BPB - telephone numbers must have at least 10 digits

function JSRadioValue(oRadio) {
	// Returns the value of the currently selected radio button in a radio group.
	// Returns null if nothing is selected.
	var Option = null;
	var i;
	for (i=oRadio.length-1; i > -1; i--) {
		if (oRadio[i].checked) {
			//alert("option=" + i + ", value==" + oRadio[i].value);
			return(oRadio[i].value);
		}
	}
	//alert("not found");
	return(null);
}

function JSValidatePhoneNumber(strCheck) {
	// Validates "strCheck" as a phone number.
	// Phone numbers must have at least 7 digits.
	// Returns true if OK, false if not.
	var regBad = /[^\w\n \-\+\(\)\/\\]/;
	var mat = strCheck.search(regBad);
	//var stat = (mat < 0) ? "OK" : "BAD " + strCheck[mat];
	//alert('strCheck=' + strCheck + ', regBad=' + regBad + ',mat=' + mat + "\n\n" + stat);
	if (mat < 0) {
		// no bad chars - make sure there are at least 7 digits
		var regDigits = /[\d]/g;
		mat = strCheck.match(regDigits);
		if (mat == null) {
			//alert('no digits!');
			return(false);		// no digits!
		} else {
			var ndigits = mat.length;
			//alert('ndigits=' + ndigits);
			return (ndigits >= 7);
		}
	} else {
		return (false);
	}
}


function JSValidateEmail(strEmail) {
	// From IsEmail
	// Check valid email
	// strEmail is the string to check.
	// Called with: onChange="return IsEmail(this.value)" within Email form element
	// Must have a "@" and a "." to be valid.
	// Must have at least 1 character before "@"
	// Must have at least 1 character after "@" and before "."
	// Must have at least 2 characters after "."
	// An empty field is considered invalid.
	var strCheck,i,ii,j,k,kk,jj,len;
	if (strEmail.length > 0) {
		// split multiple addresses at (literal) \n string, not the special newline character!
		var arrAddrs = strEmail.split('\\n');
		var num = arrAddrs.length;
		//alert(num + " addresses found.");
		for (var n=0; n<num; n++) {
			strCheck = arrAddrs[n];
			//alert('checking ' + strCheck);
			i=strCheck.indexOf("@");
			ii=strCheck.indexOf("@",i+1);
			j=strCheck.indexOf(".",i);
			k=strCheck.indexOf(",");
			kk=strCheck.indexOf(" ");
			jj=strCheck.lastIndexOf(".")+1;
			len=strCheck.length;
			//alert("i,ii,j,k,kk,jj,len=" + i + ', ' + ii + ', ' + j + ', ' + k + ', ' + kk + ', ' + jj + ', ' + len);
			if ((i>0) && (j>(i+1)) && (k==-1) && (ii==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=4)) {
				//alert(strCheck + ' is valid.');
				continue;
			} else {
				//alert(strCheck + ' is NOT valid.');
				return (false);	// this address is invalid; quit
			}
		}
		//alert('All addresses are valid!');
		return(true);	// all addresses are valid
	}
	return (false);
}

function JSValidateZipCode(strZip) {
	//JavaScript US ZIP Codes regex
	regZip = '^[0-9]{5}(?:-[0-9]{4})?$';
	mat = strZip.match(regZip);
	//alert('mat=' + mat);
	return (mat != null);
}
