// Data Validation Scripts
// (c) 2001-2002 Frank Alviani

//--- available functions ---
// function upper1st (aFld)
// function checkName (ob, minLen, maxLen, fldName)
// function minLenCheck (ob, minLen, fldName)
// function popupCheck(menuOb, menuName)
// function checkEmail(emailOb)
// function chkPhone (phoneOb, fldName)
// function chkRadioArray (radArrayOb, fldName)

// removes leading and trailing blanks, and apostrophes
function strTrim(ob) {
    if (ob==null) { return false; }
    if (ob.value.length == 0) { return false; }

	var parmType = typeof ob.value;

	if (parmType == "string")
	{
		var newStr = ob.value, done = false, pos;

    newStr.replace (/^\s*/, "");	// delete leading whitespace
		newStr.replace (/\s*$/, "");	// delete trailing whitespace
		while (!done)
		{
			pos = newStr.indexOf("'");
			if (pos != -1)
			{
				newStr = newStr.substring(0, pos) + newStr.substring(pos+1, newStr.length);
			}
			else
				done = true;
		}

		ob.value = newStr;
		return true;
	}
	else
	{
	  return false;
	}
}

function minLenCheck (ob, minLen, fldName) {
 if (strTrim(ob))
    {
		var mLen = ob.value.length;
	
		if (mLen < minLen) {
			gOutMsg = gOutMsg + "You must enter at least " + minLen + " letters in the \"" + fldName + "\" field.\n";
			return false;
		}
		return true;
	}
	else
    {
    gOutMsg = gOutMsg + fldName + " field was empty\n";
		return false;
    }
} // minLenCheck

function upper1st (aFld)
{
  if (strTrim(aFld))
	{
		var re = /^([a-zA-Z])([a-zA-Z]*)$/
		var part = re.exec(aFld.value);
		if (part) {	aFld.value = part[1].toUpperCase() + part[2].toLowerCase();	}
	}
  else
    {
		gOutMsg = gOutMsg + fldName + " field was empty\n";
		return false;
    }
}

function checkPosInt(ob, fldName) {
  if (strTrim(ob))
	{
		var obVal = parseInt(ob.value);
		var isOK;
	
//	alert("checkPosInt value = " + obVal);
//	return false;

		isOK = (obVal > 0);
		if (!isOK) {
			gOutMsg = gOutMsg + "You must enter a whole number > 0 in the \"" + fldName + "\" field.\n";
		}
		return isOK;
	}
	else
    {
		gOutMsg = gOutMsg + fldName + " field was empty\n";
		return false;
    }
}

function checkNonNegInt(ob, fldName) {
  if (strTrim(ob))
	{
		var obVal = parseInt(ob.value);
		var isOK;
	
		isOK = (obVal >= 0);
		if (!isOK) {
			gOutMsg = gOutMsg + "You must enter a whole number >= 0 in the \"" + fldName + "\" field.\n";
		}
		return isOK;
	}
	else
    {
		gOutMsg = gOutMsg + fldName + " field was empty\n";
		return false;
    }
}

// allows alpha, space, apostrophe, and hyphen
function checkName (ob, minLen, maxLen, fldName) {
  if (strTrim(ob))
	{
		var mLen = ob.value.length;
		var pat = /^([a-zA-Z \'\-]+)$/;
		
		if (mLen < minLen) {
			gOutMsg = gOutMsg + "You must enter at least " + minLen + " letters in the \"" + fldName + "\" field.\n";
			return false;
		}
		
		var part = pat.exec(ob.value);
		if (!part) {
			gOutMsg = gOutMsg + "Only letters, spaces, apostrophes, and hyphens are allowed in the \"" + fldName + "\" field.\n";
			return false;
		}
	
		if (mLen > maxLen) {
			var tStr = ob.value.substring(0, maxLen);	// clip
			ob.value = tStr;
		}
		return true;
	}
	else
    {
		gOutMsg = gOutMsg + fldName + " field was empty\n";
		return false;
    }
}

function popupCheck(menuOb, menuName) {
  if (menuOb==null) { return false; }
  var aChoice = menuOb.selectedIndex;
  var val = menuOb.options[aChoice].value;

  if (val == "0") {
    gOutMsg = gOutMsg + "You must make a choice from the \"" + menuName + "\" menu.\n";
	  return false;
  }
  return true;
}  // popupCheck

// Ensure email address has reasonably valid syntax - returns true if valid
function checkEmail(emailOb, fldName) {
  if (strTrim(emailOb))
	{
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var isOK = re.test(emailOb.value);
		if (!isOK) {
			gOutMsg = gOutMsg + "You must enter a valid e-mail address.\n"
		}
		return isOK;
	}
	else
  {
    gOutMsg = gOutMsg + fldName + " field was empty\n";
		return false;
  }
} // checkEmail

// phone validation
function chkPhone (phoneOb, fldName) {
  if (strTrim(phoneOb))
	{
		if (phoneOb.value.length < 10) {  // too short to be valid
			gOutMsg = gOutMsg + "You must enter a valid phone number (including area code) in the \"" + fldName + "\" field.\n";
			return false;
		}
	
		var re = /^\(?(\d{3})\)?[ \-]?(\d{3})[ \-]?(\d{4})$/
		var part = re.exec(phoneOb.value);
	
		if (part) {
			phoneOb.value = "(" + part[1] + ") " + part[2] + "-" + part[3];
			return true;
		} else {
			gOutMsg = gOutMsg + "You must enter a valid phone number (including area code) in the \"" + fldName + "\" field.\n";
			return false;
		}
	}
	else
    {
        gOutMsg = gOutMsg + fldName + " field was empty\n";
		return false;
    }
} // chkPhone

//at least 1 must be selected
function chkRadioArray (radArrayOb, fldName) {
  if (radArrayOb == null) { return false; }
  var arrLen = radArrayOb.length;
  var ix = 0;
  var choice = -1;

  for (ix = 0; ix < arrLen; ix++) {
    if (radArrayOb[ix].checked) { choice = ix; }
  }

  if (choice == -1) {
    gOutMsg = gOutMsg + "You must choose 1 of the " + fldName + " radio buttons.\n"
	return false;
  }

  return true;
} //chkRadioArray

function getRadioVal (radArrayOb) {
  if (radArrayOb == null) { return null; }
  var arrLen = radArrayOb.length;
  var ix = 0;
  var choice = -1;

  for (ix = 0; ix < arrLen; ix++) {
    if (radArrayOb[ix].checked) { choice = ix; }
  }

  if (choice != -1) {
		return radArrayOb[choice].value;
  } else {
		return null;
	}
}