// USAGE - if(!boxIsChecked(form['Privacy Agreement'])) errString = errString + '* <replace with your custom error message>.\n';
function boxIsChecked(obj){
	if(obj.type != "checkbox") alert("Use this validation to see if a checkbox was checked.")
	var pass=false;
	if(obj.checked) pass=true;
	return pass;
}

//USAGE - if(!isChecked(form['Best Call Time'])) errString = errString + '* <replace with your custom error message>.\n';
function isChecked(obj) {
	if(obj[0].type != "radio") alert("Use this validation to see if at least one radio button was selected.\n(radio button group name=\""+obj[0].name+"\")")
  var pass = false;
  for(x=0; x<obj.length; x++) {
    if(obj[x].checked) pass = true;
  }
 return pass;
}

//USAGE - if(findCheckedValue(form['Contractual Obligation'], 'YES')){ //do more validations here; }
function findCheckedValue(checkObj, checkVal) {
	if(checkObj[0].type != "radio") alert("Please use this validation when checking the value of a radio button.\n(radio button group name=\""+checkObj[0].name+"\")")
  myCheck = false;
  for(var i = 0; i < checkObj.length; i++){
     if(checkObj[i].checked){
       if(checkObj[i].value == checkVal){
        myCheck = true      
       }
     }
  }
return myCheck
}

//USAGE - if(!isEmail(form.Email.value)) errString = errString + '* <replace with your custom error message>.\n';
function isEmail(txt) {
  var pass = true;
  rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");if(!rx.test(txt)) pass=false;
 return pass;
}

//USAGE - if(!isNumber(form.Zip.value,5)) errString = errString + '* <replace with your custom error message>.\n';
//Can also be used to check for numbers w/o length - if(!isNumber(form.Zip.value)) errString = errString + '* Numbers only.\n';
function isNumber(val, len) {
	var pass = true;

	var v = "0123456789";
	var w = "";
	
	if(val.length == 0){
		pass=false;
	}
	
	for (i=0; i < val.length; i++) {
		x = val.charAt(i);
		if (v.indexOf(x,0) == -1)  {
			pass= false;
			break;
		}
	}

	if(pass) 
	   if(len > 0 && val.length != len) pass = false;
	return pass;
	
}

//USAGE - if(!isNumberRange(formfieldvalue,rangelowend,rangehighend) errString += '* Number range is not valid.\n';
//Exp:. - if(!isNumberRange(form.Account.value,100,500) errString += '* Number range is not valid.\n';
function isNumberRange(val, low, high) {
	var pass = true;

	var v = "0123456789";
	var w = "";
	var valLength = "good"

	for (i=0; i < val.length; i++) {
		x = val.charAt(i);
		if (v.indexOf(x,0) == -1)  {
			pass= false;
			break;
		}
	}

	if(pass)
	   if(val.length < low ) valLength = "low";
	   if(val.length > high ) valLength = "high";
	if(valLength != "good"){ pass = false; }
	return pass;
}

//USAGE - if(!isValidAN(form.Zip.value,5)) errString = errString + '* Zip code is not valid.\n';
//USAGE - if(!isValidAN(form.Zip.value)) errString = errString + '* Canadian zip code with numbers and letters is not valid.\n';
function isValidAN(val, len) {
	var pass = true;

	var v = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var w = "";
	
	if(val.length == 0){
		pass=false;
	}
	
	for (i=0; i < val.length; i++) {
		x = val.charAt(i);
		if (v.indexOf(x,0) == -1)  {
			pass= false;
			break;
		}
	}

	if(pass) 
	   if(len > 0 && val.length != len) pass = false;

	return pass;
}

//USAGE - if(!isValidANRange(form.UPSAcct.value,10,12)) errString = errString + '* UPS Account number is not valid.\n';
function isValidANRange(val, len, maxLen) {
	var pass = true;

	var v = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-";
	var w = "";

	var valLength = "good"

	for (i=0; i < val.length; i++) {
		x = val.charAt(i);
		if (v.indexOf(x,0) == -1)  {
			pass= false;
			break;
		}
	}

	if(pass) 
	   if(val.length < len ) valLength = "low";
	   if(val.length > maxLen ) valLength = "high";
	if(valLength != "good"){ pass = false; }

	return pass;
}

//USAGE - if(!isSelected(form.State.selectedIndex,0)) errString = errString + '* <replace with your custom error message>.\n';
function isSelected(selectRequired,selectDesired){
var pass = true;
	if(selectRequired == selectDesired){
	pass = false;
	}
	return pass;
}

//USAGE - if(isBlank(form.Account.value)) errString = errString + '* <replace with your custom error message>.\n'; 
function isBlank(txt) {
   var pass = true;
   val = trimString(txt);

   if(val.length > 0) pass = false;
   return pass;
}

function trimString(strString)
{
	while (strString.charAt(0) == " ")
	{
		strString = strString.substring(1, strString.length);
	}
	
	while (strString.charAt(strString.length - 1) == " ")
	{
		strString = strString.substring(0, strString.length - 1);
	}
	
	return strString;
}


//USAGE - errString = errString + checkPhone(form.Phone1.value,form.Phone2.value,form.Phone3.value,'', form['Telephone']);
function checkPhone(areacode, prefix, suffix, extension, formattedphone) {
  var thiserr = '';

    if(!isNumber(areacode,3)) thiserr = thiserr + '* Phone Number Area Code is not valid.\n';
    if(!isNumber(prefix,3)) thiserr = thiserr + '* Phone Number Prefix is not valid.\n';
    if(!isNumber(suffix,4)) thiserr = thiserr + '* Phone Number Suffix is not valid.\n';
    if(extension.length > 0) 	{
	if(!isNumber(extension,0)) thiserr = thiserr + '* Phone Number Extension is not valid.\n';
	formattedphone.value = '(' + areacode + ') ' + prefix + ' - ' + suffix + ' ext: ' + extension;
    } else
	formattedphone.value = '(' + areacode + ') ' + prefix + ' - ' + suffix;

   return thiserr;
}

//USAGE - if(!checkPhoneField(form['Contact Phone'].value,form['Contact Phone'])) errString = errString + '* <replace with your custom error message>.\n';
function checkPhoneField(phone,formattedphone){
var phone1 = /^\(\d{3}\)([\-\.\s]?)\d{3}([\-\.\s]?)\d{4}$/; // format (nnn)nnn[. -]nnnn 
var phone2 = /^\d{3}([\-\s\.]?)\d{3}([\-\.\s]?)\d{4}$/;     // format nnn[. -]nnn[. -]nnnn
var digits = '0123456789'
var thisPhone = false;

   if(phone1.test(phone) || phone2.test(phone) ){
   thisPhone = '';
	  for(var d = 0; d < phone.length; d++){
	     if(digits.indexOf(phone.charAt(d),0) != -1){
		 thisPhone += phone.charAt(d);
		 }
	  }
	  var ac = thisPhone.substring(0,3);
	  var npa = thisPhone.substring(3,6);
	  var nxx = thisPhone.substring(6,10);
      var temp = "(" + ac + ") " + npa + "-" + nxx;
	formattedphone.value = temp;
   }
return thisPhone;
}


// USAGE - <input name="Phone1" type="text" class="text" id="Phone1" size="3" maxlength="3" onkeyup="nextField(this,this.form,3,Phone2)">
//       - <input name="Phone2" type="text" class="text" id="Phone2" size="3" maxlength="3" onkeyup="nextField(this,this.form,3,Phone3)">
//       - <input name="Phone3" type="text" class="text" id="Phone3" size="4" maxlength="4">
function nextField(obj,frm,l,fieldName){
if (obj.value.length==l && fieldName.value.length==0) {
	   fieldName.focus();
	}
}

//USAGE - <textarea name="Comments" cols="57" rows="4" wrap="virtual" class="text" id="Comments" onFocus="if(this.value=='My Default Text Value')this.value=''" onBlur="if(this.value=='')this.value=this.defaultValue" onKeyDown="textCounter(this,this.form.txtCount,220)" onKeyUp="textCounter(this,this.form.txtCount,220)"></textarea>
function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}