// dataValidationUtilities.js


function resetForm(formName)
{
	var ml = document.formName;
	var len = ml.elements.length;
	for (var i = 0; i < len; i++) {
		var e = ml.elements[i];
		if (e.type != "image") {
			e.value="";
		}
	}
}

function trim( str ) {
	var idx = 0;
	var rdx = str.length-1;
	while( (idx<str.length) && (str.charAt(idx) == " ") ) idx++;
	while( (rdx> 0) &&         (str.charAt(rdx) == " ") ) rdx--;
	return str.substring(idx, rdx+1);
}

function checkPhoneNumber( p ) {
	var v = trim(p.value);
	v = trim( v );
	if ( ! validPhoneNumber(v) ) {
		p.value = v;  // might have needed trimming
		p.focus();
		p.select();
		alert(v + " is not a valid telephone number.");
	}
}

function fillPhone(phone, field){
	var phn = phone.value;
	if((phn.length == 3)||(phn.length == 7)){
		phn = phn + '-';
	}
	if((phn.length == 5)||(phn.length == 9)){
		if(phn.substr(phn.length-1) == '-'){
			phn = phn.substr(0, phn.length-1);
		}
	}
	if(phn.length == 13){
		phn = phn.substr(0, phn.length-1);
	}
	document.getElementById(field).value = phn;
}

function checkPassword( p ) {
	pwd = trim( p.value );
	if ( ! validPassword(pwd) ) {
		p.value = '';
		alert(pwd + " is not a valid password.");
		p.focus();
		p.select();
	}
}

function validPhoneNumber( num ) {
	var ac = num.substring(0,3);
	var ex = num.substring(4,7);
	var nm = num.substring(8,12);
	if (num.length != 12) {return false;}
	if ( isNaN(ac) ||
	     isNaN(ex) ||
			 isNaN(nm) ||
			 (num.charAt(3) != "-") ||
			 (num.charAt(7) != "-")) {return false;}
	return true;
}

function validPassword( pwd ) {
	if (pwd.length < 6) {
		return false;
	}
	return true;
}

function checkZipCode( z ) {
	var zc  = trim( z.value );
	if ( ! validZipCode( zc ) ) {
		z.value = zc;
		alert(zc + " is not a valid zip code.");
		z.focus();
		z.select();
	}
}

function validZipCode( zc ) {
	var len = zc.length;
	switch (len) {
		case 5:
			if (isNaN(zc)) return false;
			else return true;
			break;
		case 10:
			var fp = zc.substring(0,5);
			var sp = zc.substring(6);
			if ((zc.charAt(5) != "-") || isNaN(fp) || isNaN(sp)) return false;
			else return true;
			break;
		default:
			return false;
	}//en switch
}