//------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
//
// ValUtils.js
//
//   General JavaScript validation utility functions.
//
//  History:
//    08/10/2001 Created initial new version of file -- Hershel Robinson
//    ??/??/2002 Procedures added
//    04/13/2007  GW  Procedures added and updated
//    01/15/2008  GW  Added checkboxs and radio buttons
//
///////////////////////////////////////////////////////////////////////////////
function removeSpaces(sbString) {
    var temp = "";
    sbString = '' + sbString;
    splitstring = sbString.split(" ");
    for(i = 0; i < splitstring.length; i++)
        temp += splitstring[i];
    return temp;
}
function checkZero(theField,sbType,sbName) {
  if ((removeSpaces(theField.value)).length == 0) {
    alert("Please enter a "+sbType+" in the \""+sbName+"\" field.");
    theField.focus();
    return (false);
  }
  else
    return true;
}
regexp_ssn = /^(\d){3}-?(\d){2}-?(\d){4}$/;
function checkSSN(theField,sbName) {
    var checkStr = theField.value;
    if (!checkZero(theField,'value',sbName)) {
      return (false);
    }
    if (!checkStr.match(regexp_ssn)) {
      alert("You have entered an invalid Social Security number.\n"+
            "Please use the format 111223333 or 111-22-3333 in the \""+sbName+"\".");
      theField.focus();
      return (false);
    }
    return true;
}
////////////////////
// ValidateNumber
//   validates that the value of theBox is null or a positive integer.
// inputs:
//    theBox An input type=text object
//    Name   The pretty name of the field
//    type   An OPTIONAL field.  If type exists then this function will call
//           checkZero to see if the field has any data.  If not, it will ask
//           the user to enter a /type/ into the field called /Name/
///////////////////////
regexp_wholenum = /^\d*$/;
function ValidateNumber(theBox, sbName, sbType) {
  // first check if the object exists
  if (!theBox) return true;
  if (sbType && !checkZero(theBox,sbType,sbName)) return false;
  var checkStr = theBox.value;
  if (!checkStr.match(regexp_wholenum)) {
    alert("Please enter only digits in the " + sbName + " field.");
    theBox.focus();
    return (false);
  }
  return (true);
}
////
// ValidateNumberPt
//   validates that the value of theBox is null or a floating-point number.
////
regexp_numpt = /^[\d\.]*$/;
function ValidateNumberPt(theBox, sbName) {
  // first check if the object exists
  if (!theBox) return true;
  var checkStr = theBox.value;
  if (!checkStr.match(regexp_numpt)) {
    alert("Please enter only digits or '.' in the " + sbName + " field.");
    theBox.focus();
    return (false);
  }
  if (isNaN(checkStr)) {
    alert("Please enter a valid number in the " + sbName + " field.");
    theBox.focus();
    return (false);
  }
  return true;
}
////
// ValidateNegNumber
//   validates that the value of theBox is null or an integer.
////
regexp_wholenum = /^[\d\-]*$/;
function ValidateNegNumber(theBox, sbName) {
  // first check if the object exists
  if (!theBox) return true;
  var checkStr = theBox.value;
  if (!checkStr.match(regexp_wholenum)) {
    alert("Please enter only digits in the " + sbName + " field.");
    theBox.focus();
    return (false);
  }
  return (true);
}
function IsMissing(theBox, sbName) {
  if (theBox.value == "")
  {
    alert("Please enter a value for the " + sbName + " field.");
    theBox.focus();
    return true;
  }
  return false;
}
////
// ValidateNumberFormat
//   validates that the value of theBox is null or a positive integer.
////
function ValidateNumberFormat(theBox, sbName, IntPlaces, DecPlaces) {
  // first check if the object exists
  if (!theBox) return true;
  // build match pattern
  var pattern = "^\\d{0," + IntPlaces + "}\\.?\\d{0," + DecPlaces + "}$";
  regexp_numformat = new RegExp(pattern,"");
  var checkStr = theBox.value;
  if (!checkStr.match(regexp_numformat)) {
    alert("Please enter numbers with upto " + IntPlaces + " digits and " + DecPlaces + " decimals in the " + sbName + " field.");
    theBox.focus();
    return (false);
  }
  if (theBox.value == ".") theBox.value = "";
  return (true);
}

function checkMax(theField,fieldName,maxVal,allowBlank) {
  // use this check in case the page doesn't have certain fields
  if (!(theField)) return true;

  var chkVal = theField.value;
  if (allowBlank && chkVal.length == 0) return true;
  //if (!ValidateNumber(theField,fieldName)) return false;
  if (chkVal > maxVal || chkVal < 0 || chkVal.length == 0) {
    alert("Please enter a value greater than or equal to 0 "+
          "and less than or equal to "+maxVal+" in the \""+fieldName+"\" field.");
    theField.focus();
    return false;
  }
  return true;
}

function checkMin(theField,fieldName,minVal,allowBlank) {
  // use this check in case the page doesn't have certain fields
  if (!(theField)) return true;

  var chkVal = theField.value;
  if (allowBlank && chkVal.length == 0) return true;
  //if (!ValidateNumber(theField,fieldName)) return false;
  if (chkVal < minVal || chkVal.length == 0)  {
    alert("Please enter a value greater than or equal to "+minVal+
          " in the \""+fieldName+"\" field.");
    theField.focus();
    return (false);
  }
  return true;
}

function checkZip(theField,fieldName,required) {
  // use this check in case the page doesn't have certain fields
	if (!(theField)) return true;

	if (required) {
		if (IsMissing(theField,fieldName)) {return false;}
	} else {
		if (theField.value.length == 0) {return true;}
	}
	var zipRegEx = /^(\d){5}(-(\d){4})?$/;
	// check Zip code
	var checkStr = theField.value;
	if (!checkStr.match(zipRegEx)) {
		alert("Please enter a valid zipcode in the \"" + fieldName + "\" field.");
		theField.focus();
		return false;
	}
  return true;
}

function checkSelection(theField,fieldName) {
  // use this check in case the page doesn't have certain fields
	if (!(theField)) return true;

	// This assumes that the first entry is a propmpt and therefore not valid
	if (theField.length > 1 && theField.selectedIndex == 0) {
	    alert("Please select a \"" + fieldName + "\".");
	    theField.focus();
	    return false;
	  }
  return true;
}

function NotSelected(theField,fieldName) {
  // use this check in case the page doesn't have certain fields
	if (!(theField)) return false;

	// This assumes that the first entry is a propmpt and therefore not valid
	if (theField.length > 1 && theField.selectedIndex == 0) {
	    alert("Please select a \"" + fieldName + "\".");
	    theField.focus();
	    return true;
	  }
  return false;
}

function checkEMail(theField,fieldName,allowBlank) {
	// use this check in case the page doesn't have certain fields
	if (!(theField)) return true;

	var checkStr = theField.value;
	if (checkStr.length == 0) {
		if (allowBlank) {return true;}
		else {
			alert("Please enter an E-Mail address in the \"" + fieldName + "\" field.");
			theField.focus();
			return false;
		}
	}
	if (!IsEmailAddress(checkStr)) {
			alert("Please enter a valid E-Mail address in the \"" + fieldName + "\" field.");
			theField.focus();
			return false;
	}

	return true;
}

function RadioChecked(buttonGroup,fieldName,here) {
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) return true;
      }
   } else {
      if (buttonGroup.checked) return true;
   }
   // if we get to this point, no radio button is selected
   alert("Please select a \"" + fieldName + "\".");
   here.focus();
   return false;
}

function CheckBoxOK(frm,buttonGroup,fieldName,count) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var icnt = count;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
	x=document.getElementsByName(buttonGroup[i]);
        if (x[0].checked) icnt--;
      }
   } else { // There is only one check box (it's not an array)
	x=document.getElementsByName(buttonGroup);
	if (x.checked) return true;
   }
   if (icnt != 0) {
	alert("Please select " + count + " \"" + fieldName + "\" choices.");
//   buttonGroup.focus();
	return false;
   }
   return true;
}

function NotCheckChecked(theField,fieldName) {
	// use this check in case the page doesn't have certain fields
	if (!(theField)) return faalse;
	if (!theField.checked) {
		alert("Please select \"" + fieldName + "\".");
		theField.focus();
		return true;
	}
	return false;
}
