//
// EditValidate.js
//
// Client side library used to validate add/edit fields.
//


//-------------------------------------------------------------------
//
// This flag value is used to control whether we popup an alert
// box informing the user of an error in the data. Once we popup
// an error, we set the flag to false so we don't end up in an
// infinite loop as the focus changes.

var bAlertFlag = true;
var reDate = /(0?[1-9]|1[012])[/](0?[1-9]|[12][0-9]|3[01])[/]([0-9]{2}|[0-9]{4})$/;

// Check for leap year
function CheckLeap(yy) {

  // if not divisible by 4 then not a leap year  
  if ((yy % 4) != 0) return false;
  else if ((yy % 100) == 0) return false;	 // if a century year, then must be divisible by 400 also
  else if((yy % 400) == 0) return true;
  else return true;
}

// This validates a 4 digit year date.
function ValDate(sBox) {
   var szDate,
       mm,
       dd,
       yy;
   var bLeap = false;
   if (typeof(sBox) != "object") return true;
   
   szDate = new String(sBox.value);
   
   if (szDate.length == 0) {
      bAlertFlag = true;
      return true;
      }
      
   if (reDate.test(szDate) == false) {
      bAlertFlag = false;
      var msg = "Dates must be valid and entered in the\r\nform mm/dd/yyyy such as 01/23/2000.\r\n\r\n";
      msg += szDate + " is not a valid date.";
      alert(msg);
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      return false;
      }
   
   // Parse the string up into the components.
   mm = szDate.substring(0, szDate.indexOf('/'));
   dd = szDate.substring(szDate.indexOf('/')+1, szDate.lastIndexOf('/'));
   yy = szDate.substring(szDate.lastIndexOf('/')+1, 255);
   
   mm = parseInt(mm, 10);
   dd = parseInt(dd, 10);
   yy = parseInt(yy, 10);


   // Adjust date ranges
   if (isNaN(yy)) {
      bAlertFlag = false;
      alert("The year must be 4 digits (such as 2000).");
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      return false;
      }
   if (yy < 50) {
      yy += 2000;
      }
   else if (yy < 100) {
      yy += 1900;
      }
   // Check for leap year
   bLeap = CheckLeap(yy);

   switch (mm) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12: {
         if (dd < 1) {
           bAlertFlag = false;
           alert("The day may not be less than 1.");
           if (sBox.disabled == false) {sBox.focus();}
           if (sBox.disabled == false) {sBox.select();}
           return false;
           }
         if (dd > 31) {
           bAlertFlag = false;
           alert("The day may not be more than 31.");
           if (sBox.disabled == false) {sBox.focus();}
           if (sBox.disabled == false) {sBox.select();}
           return false;
           }
         break;
         }
      case 2: {
         if (dd < 1) {
           bAlertFlag = false;
           alert("The day may not be less than 1.");
           if (sBox.disabled == false) {sBox.focus();}
           if (sBox.disabled == false) {sBox.select();}
           return false;
           }
         if ((dd > 28) && (bLeap == false)) {
           bAlertFlag = false;
           alert("February has only 28 days.");
           if (sBox.disabled == false) {sBox.focus();}
           if (sBox.disabled == false) {sBox.select();}
           return false;
           }
         if ((dd > 29) && (bLeap == true)) {
           bAlertFlag = false;
           alert("February has only 29 days.");
           if (sBox.disabled == false) {sBox.focus();}
           if (sBox.disabled == false) {sBox.select();}
           return false;
           }
         break;
         }
      case 4:
      case 6:
      case 9:
      case 11: {
         if (dd < 1) {
           bAlertFlag = false;
           alert("The day may not be less than 1.");
           if (sBox.disabled == false) {sBox.focus();}
           if (sBox.disabled == false) {sBox.select();}
           return false;
           }
         if (dd > 30) {
           bAlertFlag = false;
           alert("The day may not be more than 30.");
           if (sBox.disabled == false) {sBox.focus();}
           if (sBox.disabled == false) {sBox.select();}
           return false;
           }
         break;
         }
      default: {
         bAlertFlag = false;
         alert("The month must be between 1 and 12.");
         if (sBox.disabled == false) {sBox.focus();}
         if (sBox.disabled == false) {sBox.select();}
         return false;
         break;
         }
      }
   
   if ((yy < 1000) && (bAlertFlag == true)) {
      bAlertFlag = false;
      alert("The year must be 4 digits (such as 2000).");
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      return false;
      }
   //datetime in SQL Server has a minimum date of 1/1/1753
   if ((yy > 2100 || yy < 1753) && (bAlertFlag == true)) {
      bAlertFlag = false;
      alert("The year is out of range.");
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      return false;
      }
      
   // So it must be ok
   bAlertFlag = true;
   //sBox.value = mm + '/' + dd + '/' + yy;
   if (sBox.disabled == false) {sBox.value = mm + '/' + dd + '/' + yy;}
   return true;
}

//-------------------------------------------------------------------
//
// Given a date, verify it is not more than iDays from the today.
// Note that iDays may be positive or negative. If iDays is zero,
// then the date must be today.
//
function CheckDateRange(szNewDate, iDays, szStartDate, iDaysForward) {
   var dDate,
       dToday,
       iDate,
       iToday,
       iMills;
   
   szDate = new String(szNewDate);
   if (reDate.test(szDate) == false) {
      return false;
      }
   
   // Parse the string up into the components.
   mm = szDate.substring(0, szDate.indexOf('/'));
   dd = szDate.substring(szDate.indexOf('/')+1, szDate.lastIndexOf('/'));
   yy = szDate.substring(szDate.lastIndexOf('/')+1, 255);
   
   mm = parseInt(mm, 10);
   dd = parseInt(dd, 10);
   yy = parseInt(yy, 10);
   
   // Now check numeric ranges
   if (mm < 1 || mm > 12) {
      return false;
      }
   if (dd < 1 || dd > 31) {
      return false;
      }
   if (yy < 1000) {
      return false;
      }

   dDate = new Date(yy,(mm-1),dd, 0, 0, 0);
   iDate = dDate.getTime();

   //if (typeof(szStartDate) != "undefined") {
   if ((typeof(szStartDate) != "undefined") && (szStartDate != "")) {
     dToday = new Date(szStartDate);
     }
   else {
     dToday = new Date();
     }
     
   //dToday.setHours(0);
   //dToday.setMinutes(0);
   //dToday.setSeconds(0);
   
   dToday = new Date(dToday.getFullYear(), (dToday.getMonth()), dToday.getDate(), 0, 0, 0);
   iToday = dToday.getTime();
   
   
   // Now we have each time in milliseconds, check the difference
   // against our target value.
   iDays = parseInt(iDays, 10);
   iMills = iDays * 24 * 60 * 60 * 1000;
   
   if (typeof(iDaysForward) == "undefined") {
		if (iDays < 0) { // No more than iDays in the past from Today
		   iMills *= -1;
		   if (iToday - iDate <= iMills) {
		      return true;
		      }
		   else {
		      return false;
		      }
		   }
		else if (iDays > 0) { // Date must be in the future from today no more than iDays
		   if (iDate - iToday <= iMills) {
		      return true;
		      }
		   else {
		      return false;
		      }
		   }
		else { // Dates must be equal
		   if (iDate == iToday) {
		      return true;
		      }
		   else {
		      return false;
		      }
		   }
	}
	else {
		var iDaysBack = iDays;
		var iMillsBack = iMills;
   
		iDaysForward = parseInt(iDaysForward, 10);
		var iMillsForward = iDaysForward * 24 * 60 * 60 * 1000;
		   
		iMillsBack *= -1;

		if (iDate <= iToday) {
			if (iToday - iDate <= iMillsBack) {
				return true;
			}
			else {
				return false;
			}
		}

		if (iDate - iToday <= iMillsForward) {
			return true;
		}
		else {
			return false;
		} 	
	}
}

//-------------------------------------------------------------------
//

function ValNumber(sBox, szLabel, iMin, iMax) {
   var i,
       szVal,
       bBad = false,
       bPoint = false;

   if (typeof(sBox) == "undefined") {
      bAlertFlag = true;
      return true;
      }
   szVal = new String(sBox.value);
   if(szVal.match(/^\s+$/)){
   	bAlertFlag = true;
   	return true;
   }
   pointCheck = new String(sBox.value);
   pointCheck = pointCheck.substring(pointCheck.indexOf('.')+1);
   if ( pointCheck.indexOf('.') != -1 ) {
      bPoint = true;
      }
   
   for (i = 0; i < szVal.length; i++) {
      if ((szVal.charCodeAt(i) < 45) || (szVal.charCodeAt(i) > 57) || (szVal.charCodeAt(i) == 47)) {
         bBad = true;
         break;
         }
      }
      
   if (bPoint == true) {
      bAlertFlag = false;  // Turn off the alert cycle.
      if (typeof(szLabel) != "undefined") {
         alert("      **** Number Field ****\n\n" +
               "Only one point allowed in the field:\n\n" +
               szLabel);
         }
      else {
         alert("      **** Number Field ****\n\n" +
               "Only one point allowed in this field.");
         }
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      return false;
      }
      
   if ((bBad == true) && (bAlertFlag == true)) {
      bAlertFlag = false;  // Turn off the alert cycle.
      if (typeof(szLabel) != "undefined") {
         alert("      **** Number Field ****\n\n" +
               "Only numbers allowed in the field:\n\n" +
               szLabel);
         }
      else {
         alert("      **** Number Field ****\n\n" +
               "Only numbers allowed in this field.");
         }
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      return false;
      }
   iVal = sBox.value;
   iVal = parseFloat(iVal,10);
   if ((isNaN(iVal) == true) && (sBox.value != "")) {
      bAlertFlag = false;  // Turn off the alert cycle.
      if (typeof(szLabel) != "undefined") {
         alert("      **** Invalid Number ****\n\n" +
               "The value for the field " + szLabel + " is invalid.");
         }
      else {
         alert("      **** Invalid Number ****\n\n" +
               "The value for the field is invalid.\n");
         }
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      return false;
      }
   iMax = parseFloat(iMax,10);
   iMin = parseFloat(iMin,10);
   if (isNaN(iMin) == true) {
      bAlertFlag = true;
      return true;
      }
   
   if (isNaN(iMax) == false) {
      bAlertFlag = false;  // Turn off the alert cycle.
      if ((iVal < iMin) || (iVal > iMax)) {
         if (typeof(szLabel) != "undefined") {
            alert("      **** Number Out of Range ****\n\n" +
                  "The value for the field " + szLabel + " is out of range.\n\n" +
                  "The valid range is from " + iMin + " to " + iMax + ".");
            }
         else {
            alert("      **** Number Out of Range ****\n\n" +
                  "The value for the field is out of range.\n\n" +
                  "The valid range is from " + iMin + " to " + iMax + ".");
            }
		 if (sBox.disabled == false) {sBox.focus();}
		 if (sBox.disabled == false) {sBox.select();}
         return false;
         }
      }
   else {
      if (iVal < iMin) {
         bAlertFlag = false;  // Turn off the alert cycle.
         if (typeof(szLabel) != "undefined") {
            alert("      **** Number Out of Range ****\n\n" +
                  "The value for the field " + szLabel + " is out of range.\n\n" +
                  "The lowest value allowed is " + iMin + ".");
            }
         else {
            alert("      **** Number Out of Range ****\n\n" +
                  "The value for the field is out of range.\n\n" +
                  "The lowest value allowed is " + iMin + ".");
            }
		 if (sBox.disabled == false) {sBox.focus();}
		 if (sBox.disabled == false) {sBox.select();}
         return false;
         }
      }
      
   bAlertFlag = true;
   return true;
}

function ValInteger(sBox, szLabel, iMin, iMax) {
   var i,
       szVal,
       bBad = false;

   if (typeof(sBox) == "undefined") {
      bAlertFlag = true;
      return true;
      }
   szVal = new String(sBox.value);
   
   for (i = 0; i < szVal.length; i++) {
      if ((szVal.charCodeAt(i) < 48) || (szVal.charCodeAt(i) > 57)) {
         bBad = true;
         break;
         }
      }
      
   if ((bBad == true) && (bAlertFlag == true)) {
      bAlertFlag = false;  // Turn off the alert cycle.
      if (typeof(szLabel) != "undefined") {
         alert("      **** Integer Number Field ****\n\n" +
               "Only integer numbers allowed in the field:\n\n" +
               szLabel);
         }
      else {
         alert("      **** Integer Number Field ****\n\n" +
               "Only integer numbers allowed in this field.");
         }
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      return false;
      }
   iVal = sBox.value;
   iVal = parseInt(iVal,10);
   iMax = parseInt(iMax,10);
   iMin = parseFloat(iMin,10);
   if (isNaN(iMin) == true) {
      bAlertFlag = true;
      return true;
      }
      
   if (isNaN(iMax) == false) {
      if ((iVal < iMin) || (iVal > iMax)) {
         if (typeof(szLabel) != "undefined") {
            alert("      **** Number Out of Range ****\n\n" +
                  "The value for the field " + szLabel + " is out of range.\n\n" +
                  "The valid range is from " + iMin + " to " + iMax + ".");
            }
         else {
            alert("      **** Number Out of Range ****\n\n" +
                  "The value for the field is out of range.\n\n" +
                  "The valid range is from " + iMin + " to " + iMax + ".");
            }
		 if (sBox.disabled == false) {sBox.focus();}
		 if (sBox.disabled == false) {sBox.select();}
         return false;
         }
      }
   else {
      if (iVal < iMin) {
         if (typeof(szLabel) != "undefined") {
            alert("      **** Number Out of Range ****\n\n" +
                  "The value for the field " + szLabel + " is out of range.\n\n" +
                  "The lowest value allowed is " + iMin + ".");
            }
         else {
            alert("      **** Number Out of Range ****\n\n" +
                  "The value for the field is out of range.\n\n" +
                  "The lowest value allowed is " + iMin + ".");
            }
		 if (sBox.disabled == false) {sBox.focus();}
		 if (sBox.disabled == false) {sBox.select();}
         return false;
         }
      }
      
   bAlertFlag = true;
   return true;
}

function ValDimension(sBox, iMax, szLabel) {
   var i,
       szVal,
       bBad = false;

   if (typeof(sBox) == "undefined") {
      bAlertFlag = true;
      return true;
      }
   szVal = new String(sBox.value);
   szVal = szVal.toLowerCase();
   
   for (i = 0; i < szVal.length; i++) {
      if ( ((szVal.charCodeAt(i) < 48)  || (szVal.charCodeAt(i) > 57))   &&
           (szVal.charCodeAt(i) != 120) && (szVal.charCodeAt(i) != 46)   &&
           (szVal.charCodeAt(i) != 32)                                      ) {
         bBad = true;
         break;
         }
      }

   if ((bBad == true) && (bAlertFlag == true)) {
      bAlertFlag = false;  // Turn off the alert cycle.
      if (typeof(szLabel) != "undefined") {
         alert("        **** Dimension Field ****\n\n" +
               "Only numbers and 'x' allowed in the field:\n\n" + 
               szLabel);
         }
      else {
         alert("        **** Dimension Field ****\n\n" +
               "Only numbers and 'x' allowed in this field.");
         }
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      return false;
      }
      
   // Max length for a dim field.
   if ((szVal.length > iMax) && (bAlertFlag == true)) {
      bAlertFlag = false;  // Turn off the alert cycle.
      if (typeof(szLabel) != "undefined") {
         alert("        **** Dimension Field ****\n\n" +
               "Only " + iMax + " characters may be entering in the field:\n\n" +
               szLabel);
         }
      else {
         alert("        **** Dimension Field ****\n\n" +
               "Only " + iMax + " characters may be entering in this field.");
         }
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      return false;
      }
         
   bAlertFlag = true;
   return true;
}

function CheckListContains(stringToMatch,arrayToCheck)
{
	var stringToMatch = (stringToMatch === undefined)?"":stringToMatch;
	var arrayToCheck = (arrayToCheck === undefined)?new Array():arrayToCheck;
	if (stringToMatch.length == 0)
		return false;
	else if (arrayToCheck.length == 0)
		return false;
	else
	{
		var foundMatch = false;
		for (var i = 0; i < arrayToCheck.length; i++)
		{
			if (stringToMatch.toLowerCase()==arrayToCheck[i].toLowerCase())
				foundMatch = true;
		}
		return foundMatch;
	}
}

function PrepareDisallowedCharacterString(disallowedCharacters)
{
	var disallowedCharacters = (disallowedCharacters === undefined)?"":disallowedCharacters;
	var sDisallowed = new String();
	var aDisallowed = new Array();
	// scrub the sDisallowedTextAreaCharacters value for regex metacharacters
	for (var i = 0; i < disallowedCharacters.length; i++)
	{
		var tDisallowed = new String(disallowedCharacters.charAt(i));
		tDisallowed = tDisallowed.replace("\\","\\\\");
		tDisallowed = tDisallowed.replace("^","\\^");
		tDisallowed = tDisallowed.replace("$","\\$");
		tDisallowed = tDisallowed.replace(".","\\.");
		tDisallowed = tDisallowed.replace("|","\\|");
		tDisallowed = tDisallowed.replace("{","\\{");
		tDisallowed = tDisallowed.replace("}","\\}");
		tDisallowed = tDisallowed.replace("(","\\(");
		tDisallowed = tDisallowed.replace(")","\\)");
		tDisallowed = tDisallowed.replace("[","\\[");
		tDisallowed = tDisallowed.replace("]","\\]");
		tDisallowed = tDisallowed.replace("*","\\*");
		tDisallowed = tDisallowed.replace("+","\\+");
		tDisallowed = tDisallowed.replace("?","\\?");
		aDisallowed[aDisallowed.length] = tDisallowed;	
	}
	disallowedCharacters = aDisallowed.join("|");
	return disallowedCharacters;
}

function ValChar(sBox, iMax, szLabel) 
{
	var szVal;
	if (typeof(sBox) == "undefined") 
	{
		bAlertFlag = true;
		return true;
	}
	if (typeof(szLabel)=="undefined") szLabel = sBox.name;
	szVal = new String(sBox.value);
	if (szVal.length >= 1)
	{
		// replace \c with \ c to prevent client side javascript character escape violations
		var regex = /\\c/g;
		if (szVal.search(regex)>=0)
		{
			szVal = szVal.replace(regex," c");
			sBox.value = szVal;
		}
		// checks for the occurence of unsupported characters in text.
		// the sDisallowedTextAreaCharacters variable must be defined in /Edit/Scripts/CustomRules.js to support this feature
		// ex. var sDisallowedTextAreaCharacters = "|&";
		// the sDisallowedCharactersIncludeList variable can be added to /Edit/Scripts/CustomRules.js and can contain a comma-
		// delimited list of field to include in special character scrubbing.
		// the sDisallowedCharactersExcludeList variable can be added to /Edit/Scripts/CustomRules.js and can contain a comma-
		// delimited list of field to exclude in special character scrubbing.
		sDisallowedCharactersIncludeList = new String((typeof(sDisallowedCharactersIncludeList)!="undefined")?sDisallowedCharactersIncludeList:"");
		sDisallowedCharactersExcludeList = new String((typeof(sDisallowedCharactersExcludeList)!="undefined")?sDisallowedCharactersExcludeList:"");
		var sTempDisallowedTextAreaCharacters = new String((typeof(sDisallowedTextAreaCharacters)!="undefined")?sDisallowedTextAreaCharacters:"");
		var sOrigDisallowedTextAreaCharacters = sTempDisallowedTextAreaCharacters;
		var aDisallowedCharactersIncludeList = new Array();
		aDisallowedCharactersIncludeList = sDisallowedCharactersIncludeList.split(",");
		var aDisallowedCharactersExcludeList = new Array();
		aDisallowedCharactersExcludeList = sDisallowedCharactersExcludeList.split(",");
		var bScrubbableField = true;
		if (aDisallowedCharactersIncludeList.length > 0 && CheckListContains(szLabel,aDisallowedCharactersIncludeList)==false)
			bScrubbableField = false;
		if (aDisallowedCharactersExcludeList.length > 0 && CheckListContains(szLabel,aDisallowedCharactersExcludeList)==true)
			bScrubbableField = false;
		if (sOrigDisallowedTextAreaCharacters.length > 0 && bScrubbableField == true)
		{
			sTempDisallowedTextAreaCharacters = PrepareDisallowedCharacterString(sTempDisallowedTextAreaCharacters);
			if (sTempDisallowedTextAreaCharacters.length > 0)
			{
				var sDisallowedRegex = "/" + sTempDisallowedTextAreaCharacters + "/g";
				var iDisallowed = szVal.search(sDisallowedRegex);
				if (iDisallowed > -1)
				{
					bAlertFlag = false;
					alert("You have entered one or more invalid characters for field:\n\n" + szLabel + "\n\nInvalid characters: " + sOrigDisallowedTextAreaCharacters);
					if (sBox.disabled == false) {sBox.focus();}
					return false;
				}
			}
		}
	}

	if ((szVal.length > iMax) && (bAlertFlag == true)) 
	{
		bAlertFlag = false;  // Turn off the alert cycle.
		if (typeof(szLabel) != "undefined") 
		{
			alert("You have exceeded the field limit of " + iMax + " characters for field:\n\n" + szLabel);
		}
		else
		{
			alert("You have exceeded the field limit of " + iMax + " characters.");
		}      
		if (sBox.disabled == false) {sBox.focus();}
		return false;
	}
	
	if (szVal.length >= 1)
	{
		// was checking for opening and closing html tags, but not opening only,
		// which allowed for some html tags to sneak through ~ mbs ~ 09/24/02
		if (((szVal.search(/\<\s*(\S+)(\s[^\>]*)?\>[\s\S]*\<\s*\/{1}\s*(\S+)\>/gi) >= 0) ||
			 (szVal.search(/\<\s*(\S+)(\s[^\>]*)?\>/gi) >= 0)) && (bAlertFlag == true)) 
		{
			bAlertFlag = false;  // Turn off the alert cycle.
			alert("HTML Tags are not permitted in data entry fields.");
			if (sBox.disabled == false) {sBox.focus();}
			return false;
		}
	}
	
	bAlertFlag = true;
	return true;
}

function ValCharHTMLTags(sBox, iMax, szLabel) 
{
	var szVal;
	if (typeof(sBox) == "undefined") 
	{
		bAlertFlag = true;
		return true;
	}
	if (typeof(szLabel)=="undefined") szLabel = sBox.name;
	szVal = new String(sBox.value);
	if (szVal.length >= 1)
	{
		// replace \c with \ c to prevent client side javascript character escape violations
		var regex = /\\c/g;
		if (szVal.search(regex)>=0)
		{
			szVal = szVal.replace(regex,"\\ c");
			sBox.value = szVal;
		}
		// checks for the occurence of unsupported characters in text.
		// the sDisallowedTextAreaCharacters variable must be defined in /Edit/Scripts/CustomRules.js to support this feature
		// ex. var sDisallowedTextAreaCharacters = "|&";
		// the sDisallowedCharactersIncludeList variable can be added to /Edit/Scripts/CustomRules.js and can contain a comma-
		// delimited list of field to include in special character scrubbing.
		// the sDisallowedCharactersExcludeList variable can be added to /Edit/Scripts/CustomRules.js and can contain a comma-
		// delimited list of field to exclude in special character scrubbing.
		sDisallowedCharactersIncludeList = new String((typeof(sDisallowedCharactersIncludeList)!="undefined")?sDisallowedCharactersIncludeList:"");
		sDisallowedCharactersExcludeList = new String((typeof(sDisallowedCharactersExcludeList)!="undefined")?sDisallowedCharactersExcludeList:"");
		var sTempDisallowedTextAreaCharacters = new String((typeof(sDisallowedTextAreaCharacters)!="undefined")?sDisallowedTextAreaCharacters:"");
		var sOrigDisallowedTextAreaCharacters = sTempDisallowedTextAreaCharacters;
		var aDisallowedCharactersIncludeList = new Array();
		aDisallowedCharactersIncludeList = sDisallowedCharactersIncludeList.split(",");
		var aDisallowedCharactersExcludeList = new Array();
		aDisallowedCharactersExcludeList = sDisallowedCharactersExcludeList.split(",");
		var bScrubbableField = true;
		if (sDisallowedCharactersIncludeList.length > 0 && CheckListContains(szLabel,aDisallowedCharactersIncludeList)==false)
			bScrubbableField = false;
		if (sDisallowedCharactersExcludeList.length > 0 && CheckListContains(szLabel,aDisallowedCharactersExcludeList)==true)
			bScrubbableField = false;
		if (sOrigDisallowedTextAreaCharacters.length > 0 && bScrubbableField == true)
		{
			sTempDisallowedTextAreaCharacters = PrepareDisallowedCharacterString(sTempDisallowedTextAreaCharacters);
			if (sTempDisallowedTextAreaCharacters.length > 0)
			{
				var sDisallowedRegex = "/" + sTempDisallowedTextAreaCharacters + "/g";
				var iDisallowed = szVal.search(sDisallowedRegex);
				if (iDisallowed > -1)
				{
					bAlertFlag = false;
					alert("You have entered one or more invalid characters for field:\n\n" + szLabel + "\n\nInvalid characters: " + sOrigDisallowedTextAreaCharacters);
					if (sBox.disabled == false) {sBox.focus();}
					return false;
				}
			}
		}
	}
	if ((szVal.length > iMax) && (bAlertFlag == true)) 
	{
		bAlertFlag = false;  // Turn off the alert cycle.
		if (typeof(szLabel) != "undefined") 
		{
			alert("You have exceeded the field limit of " + iMax + " characters for field:\n\n" + szLabel);
		}
		else
		{
			alert("You have exceeded the field limit of " + iMax + " characters.");
		}
		if (sBox.disabled == false) {sBox.focus();}
		return false;
	}
	if (szVal.length >= 1)
	{
		var index = 0;
		var lastindex = 0;
		var sztmp = new String("");
		if (((szVal.search(/\<\s*(\S+)(\s[^\>]*)?\>[\s\S]*\<\s*\/{1}\s*(\S+)\>/gi) >= 0) ||
			 (szVal.search(/\<\s*(\S+)(\s[^\>]*)?\>/gi) >= 0)) && (bAlertFlag == true)) 
		{
			var szTmp = new String(szVal);
			do
			{
				index = szTmp.search(/\<\s*(\S+)(\s[^\>]*)[\s\S]*\<\s*\/{1}\s*(\S+)/gi);
				if(index==-1)
					index = szTmp.search(/\<\s*(\S+)(\s[^\>]*)?\>/gi);
				//strip tag
				lastindex = szTmp.search(/\>/gi);
				sztmp = szTmp.substring(index+1,lastindex);
				sztmp = sztmp.toLowerCase();
				//Check to see if it's i, b, or font color
				if((sztmp!="b")&&(sztmp!="i")&&(sztmp.search(/font color/gi)<0)&&(sztmp!="/b")&&(sztmp!="/i")&&(sztmp!="/font"))
				{
					alert("Any HTML Tags except for <font color>,<i> and <b> are not permitted in data entry fields.");
					if (sBox.disabled == false) {sBox.focus();}
					return false;
				}
				szTmp = szTmp.substring(lastindex+1);
			}
			while(((szTmp.search(/\<\s*(\S+)(\s[^\>]*)?[\s\S]*\<\s*\/{1}\s*(\S+)/gi) >= 0) || (szTmp.search(/\<\s*(\S+)(\s[^\>]*)?\>/gi) >= 0)))
		}
	}
	bAlertFlag = true;
	return true;
}
function ValPhone(sBox, iMax, szLabel) {
	var szVal;
	
    if (typeof(sBox) == "undefined") {
		bAlertFlag = true;
		return(true);
    }
      	
	var bValChar = ValChar(sBox, iMax, szLabel);
		
	if (bValChar) {
		szVal = new String(sBox.value);
		szVal = szVal.replace(/\s\S/g,'');
		if ((szVal.length > 0) && ((szVal.length != 12) || (szVal.search(/\d{3}-\d{3}-\d{4}/) != 0))) {
			if ((szVal.length == 10) && (szVal.search(/\d{10}/) == 0)) {
				sBox.value = szVal.substr(0,3) + "-" + szVal.substr(3,3) + "-" + szVal.substr(6,4);			
			} else {
				bAlertFlag = false;
				bValChar = false;
				alert("Phone numbers must be numeric and supplied in the \"xxx-xxx-xxxx\" format.");
				if (sBox.disabled == false) {sBox.focus();}
			}	
		}
	}	
	return(bValChar);
}

//-------------------------------------------------------------------

// flag for browser type.
var bNetscape = ((navigator.appName == "Netscape") ? true : false);

// Only let numbers in the price field
function CheckInteger(e) {
   // Grab the keystroke entered
   if (bNetscape == true) {
      kStroke = e.which;
      }
   else {
      kStroke = e.keyCode;
      }
//alert("kStroke = " + kStroke);
   if ((kStroke > 47) && (kStroke < 58)) {
      return true;
      }
   if (kStroke == 8) return true; // Backspace
   if (kStroke == 9) return true; // Tab

   alert("      **** Integer Number Field ****\n\n" +
         "Only integer numbers allowed in this field.");
   return false;
}

function CheckNumber(e) {
   // Grab the keystroke entered
   if (bNetscape == true) {
      kStroke = e.which;
      }
   else {
      kStroke = e.keyCode;
      }

   if ((kStroke > 47) && (kStroke < 58)) {
      return true;
      }
   if (kStroke == 8) return true;  // Backspace
   if (kStroke == 46) return true; // period
   if (kStroke == 45) return true; // dash
   if (kStroke == 9) return true;  // Tab

   alert("      **** Number Field ****\n\n" +
         "Only numbers allowed in this field.");
   return false;
}

function CheckDate(e) {
   // Grab the keystroke entered
   if (bNetscape == true) {
      kStroke = e.which;
      }
   else {
      kStroke = e.keyCode;
      }

   if ((kStroke > 47) && (kStroke < 58)) {
      return true;
      }
   if (kStroke == 8) return true;  // Backspace
   if (kStroke == 47) return true; // forward slash
   if (kStroke == 9) return true;  // Tab

   alert("             **** Date Field ****\n\n" +
         "Only numbers and forward slash allowed in this field.");
   return false;
}


function CheckDimension(e, vField, iMax) {

   if (vField.value.length+1 > iMax) {
      alert("        **** Dimension Field ****\n\n" +
            "Only " + iMax + " characters may be entering in this field."); 
      return false;
      }
      
   // Grab the keystroke entered
   if (bNetscape == true) {
      kStroke = e.which;
      }
   else {
      kStroke = e.keyCode;
      }

   if ((kStroke > 47) && (kStroke < 58)) {
      return true;
      }
   if (kStroke == 8) return true;   // Backspace
   if (kStroke == 46) return true;  // Period
   if (kStroke == 88) return true;  // Letter 'X'
   if (kStroke == 120) return true; // Letter 'x'
   if (kStroke == 9) return true;   // Tab

   alert("        **** Dimension Field ****\n\n" +
         "Only numbers and 'x' allowed in this field.");
   return false;
}

function CheckChar(e, vField, iNum) 
{
	// Grab the keystroke entered
	if (bNetscape == true) 
		kStroke = e.which;
	else
		kStroke = e.keyCode;
	if (kStroke == 8) return true;   // Backspace
	if (kStroke == 9) return true;   // Tab
	if (kStroke == 127) return true; // Delete
	// checks for the occurence of unsupported characters in text.
	// the sDisallowedTextAreaCharacters variable must be defined in /Edit/Scripts/CustomRules.js to support this feature
	// ex. var sDisallowedTextAreaCharacters = "|&";
	// the sDisallowedCharactersIncludeList variable can be added to /Edit/Scripts/CustomRules.js and can contain a comma-
	// delimited list of field to include in special character scrubbing.
	// the sDisallowedCharactersExcludeList variable can be added to /Edit/Scripts/CustomRules.js and can contain a comma-
	// delimited list of field to exclude in special character scrubbing.
	sDisallowedCharactersIncludeList = new String((typeof(sDisallowedCharactersIncludeList)!="undefined")?sDisallowedCharactersIncludeList:"");
	sDisallowedCharactersExcludeList = new String((typeof(sDisallowedCharactersExcludeList)!="undefined")?sDisallowedCharactersExcludeList:"");
	var sTempDisallowedTextAreaCharacters = new String((typeof(sDisallowedTextAreaCharacters)!="undefined")?sDisallowedTextAreaCharacters:"");
	var aDisallowedCharactersIncludeList = new Array();
	aDisallowedCharactersIncludeList = sDisallowedCharactersIncludeList.split(",");
	var aDisallowedCharactersExcludeList = new Array();
	aDisallowedCharactersExcludeList = sDisallowedCharactersExcludeList.split(",");
	var vFieldName = vField.name;
	var bScrubbableField = true;
	if (sDisallowedCharactersIncludeList.length > 0 && CheckListContains(vFieldName,aDisallowedCharactersIncludeList)==false)
		bScrubbableField = false;
	if (sDisallowedCharactersExcludeList.length > 0 && CheckListContains(vFieldName,aDisallowedCharactersExcludeList)==true)
		bScrubbableField = false;
	if (sTempDisallowedTextAreaCharacters.length > 0 && bScrubbableField == true)
	{
		var bCharacterAllowed = true;
		for (var i = 0; i < sTempDisallowedTextAreaCharacters.length; i++)
		{
			if (bCharacterAllowed==true)
				bCharacterAllowed = !(sTempDisallowedTextAreaCharacters.charAt(i)==String.fromCharCode(kStroke));
			if (bCharacterAllowed==false)
			{
				alert("The character \"" + String.fromCharCode(kStroke) + "\" is not allowed.");
				return false;
			}
		}
	}
	if (vField.value.length < iNum) 
	{
		return true;
	}
	alert("You have exceeded the field limit of " + iNum + " characters.");
	return false;
}

// Validate keystrokes given to a password field. We don't let the
// characters in which we can't pass in a url.
function CheckPass(e, vField, iNum) {
   // Grab the keystroke entered
   if (bNetscape == true) {
      kStroke = e.which;
      }
   else {
      kStroke = e.keyCode;
     }
     
/*
   if (kStroke != 47){
	// to determine the e.keyCode of any key  :) jkk
		alert(kStroke);
		return false;
	}
*/


//per Triad 16054  jkk
//this will not affect current passwords, but will prevent the
//use of spaces when they change it

   if (kStroke == 32) {
      alert('No spaces are allowed in passwords.');
      return false;
      }
   if (kStroke == 39) {
      alert('The apostrophe is not allowed in passwords.');
      return false;
      }

   if (kStroke == 47) {
      alert('The "/" symbol is not allowed in passwords.');
      return false;
      }
   if (kStroke == 58) {
      alert('The ":" symbol is not allowed in passwords.');
      return false;
      }
   if (kStroke == 64) {
      alert('The "@" symbol is not allowed in passwords.');
      return false;
      }
      
   if (kStroke == 8) return true;   // Backspace

   if (vField.value.length < iNum) {
      return true;
      }
   alert("You have exceeded the field limit of " + iNum + " characters.");
   return false;
}

// Validate keystrokes given to a filename field. Also applies to 
// names for CustomSearches and CustomReports.  We don't let the
// characters in which we can't pass in a url.
// This function is plagerized from Paul H's CheckPass() above -- chd 4/22/2003
function CheckFileName(e, vField, iNum) {
   // Grab the keystroke entered
   if (bNetscape == true) {
      kStroke = e.which;
      }
   else {
      kStroke = e.keyCode;
      }
      
      
   if (kStroke == 47) {
      alert('The "/" symbol is not allowed.');
      return false;
      }
   if (kStroke == 58) {
      alert('The ":" symbol is not allowed.');
      return false;
      }
   if (kStroke == 64) {
      alert('The "@" symbol is not allowed.');
      return false;
      }
      
   ///if (kStroke == 8) return true;   // Backspace

   return true;
}

// Validate keystrokes given to CustomSearches and CustomReports.  
function CheckSearchName(e) {
   // Grab the keystroke entered
   if (bNetscape == true) {
      kStroke = e.which;
      }
   else {
      kStroke = e.keyCode;
      }     
   if (kStroke == 47) {
      alert('The "/" symbol is not allowed.');
      return false;
      }
   if (kStroke == 64) {
      alert('The "@" symbol is not allowed.');
      return false;
      }
   return true;
}

function CheckNumSelected(sBox, iMax) {
   var i,
       iCount,
       bNoValue = false;
   
   if (typeof(sBox) != "object") return 0;
   iCount = 0;
   for (i = 0; i < sBox.options.length; i++) {
      if (sBox.options[i].selected == true) iCount++;
      if (iCount > iMax) {
         sBox.options[i].selected = false;
         }
      }
   if (iCount > iMax) {
      alert("You may only select " + iMax + " items from this listbox.");
      if (sBox.disabled == false) {sBox.focus();}
      return -1;
      }
      
   return iCount;
}

// Restrict characters to mlsnumbers only
function CheckMls(e) {
   // Grab the keystroke entered
   if (bNetscape == true) {
      kStroke = e.which;
      }
   else {
      kStroke = e.keyCode;
      }

   if ((kStroke > 47) && (kStroke < 58)) {
      return true;
      }
   if (kStroke == 8) return true; // Backspace
   if (kStroke == 44) return true; // comma
   if (kStroke == 32) return true; // comma
   if (kStroke == 127) return true; // delete
   
   alert("Only numbers and commas allowed in this field.");
   return false;
}



// Don't let them type anything in this field
function NoKeys() {
   return false;
}

//-------------------------------------------------------------------
//
// Final Validation Routines. Run just before the form is submitted.
//


function VerifyNumber(sBox, iMin, szLabel) {
   var iVal;

   if (ValNumber(sBox,szLabel) == false) {  // Invalid entry
      return false;
      }
   
   if (typeof(sBox) == "undefined") return true;
   
   // Entry is valid, make sure one is there
   if ((sBox.value.length == 0) || (sBox.value.match(/^\s+$/))){ // ((sBox.value.length == 1) && (sBox.value == " "))){
      if (sBox.disabled == false) {sBox.focus();}
      if (typeof(szLabel) != "undefined") {
         alert("You must fill in a value for the field:\n\n" + szLabel);
         }
      else {
         alert("You must fill in a value.");
         }
      return false;
      }
   iVal = parseFloat(sBox.value, 10);
   if (iVal < iMin) {
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      if (typeof(szLabel) != "undefined") {
         alert("You must enter a minimum value of " + iMin + " for the field:\n\n" + szLabel);
         }
      else {
         alert("You must enter a minimum value of " + iMin + " for this field.");
         }
      return false;
      }

   return true;
}

function VerifyInteger(sBox, iMin, szLabel) {
   var iVal;
   
   if (ValNumber(sBox,szLabel) == false) {  // Invalid entry
      return false;
      }
   
   if (typeof(sBox) == "undefined") return true;
   
   // Entry is valid, make sure one is there
   if ((sBox.value.length == 0) || (sBox.value.match(/^\s+$/))){ //((sBox.value.length == 1) && (sBox.value == " "))){
      if (sBox.disabled == false) {sBox.focus();}
      if (typeof(szLabel) != "undefined") {
         alert("You must fill in a value for the field:\n\n" + szLabel);
         }
      else {
         alert("You must fill in a value.");
         }
      return false;
      }
   iVal = parseInt(sBox.value, 10);
   if (iVal < iMin) {
      if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      if (typeof(szLabel) != "undefined") {
         alert("You must enter a minimum value of " + iMin + " for the field:\n\n" + szLabel);
         }
      else {
         alert("You must enter a minimum value of " + iMin + " for this field.");
         }
      return false;
      }

   return true;
}

function VerifyPick(sBox, sBoxButton, szLabel) {
   var iVal;

   if (typeof(sBox) == "undefined") {
      bAlertFlag = true;
      return true;
      }
   if (typeof(sBoxButton) == "undefined") {
      bAlertFlag = true;
      return true;
      }

   // Entry is valid, make sure one is there
   if ((sBox.value.length == 0) || (sBox.value.match(/^\s+$/))){ //((sBox.value.length == 1) && (sBox.value == " "))){
	  if (sBoxButton.disabled == false) {sBoxButton.focus();}
      if (typeof(szLabel) != "undefined") {
         alert("You must fill in a value for the field:\n\n" + szLabel);
         }
      else {
         alert("You must fill in a value.");
         }
      return false;
      }

   return true;
}


function VerifyDimension(sBox, iMax, szLabel) {
   if (ValDimension(sBox,szLabel) == false) {  // Invalid entry
      return false;
      }
   // Entry is valid, make sure one is there
   if ((sBox.value.length == 0) || (sBox.value.match(/^\s+$/))){  //((sBox.value.length == 1) && (sBox.value == " "))){
	  if (sBox.disabled == false) {sBox.focus();}
      if (sBox.disabled == false) {sBox.select();}
      if (typeof(szLabel) != "undefined") {
         alert("You must fill in a value for the field:\n\n" + szLabel);
         }
      else {
         alert("You must fill in a value.");
         }
      return false;
      }

   return true;
}

function VerifyChar(sBox, iMax, szLabel) {
   if (ValChar(sBox,szLabel) == false) {  // Invalid entry
      return false;
      }
   
   if (typeof(sBox) == "undefined") return true;
   
   // Entry is valid, make sure one is there
   if ((sBox.value.length == 0) || (sBox.value.match(/^\s+$/))){ //((sBox.value.length == 1) && (sBox.value == " "))){
      if (sBox.disabled == false) {sBox.focus();}
      if (typeof(szLabel) != "undefined") {
         alert("You must fill in a value for the field:\n\n" + szLabel);
         }
      else {
         alert("You must fill in a value.");
         }
      return false;
      }

   return true;
}

function VerifyDate(sBox, iMax, szLabel) {
   if (typeof(sBox) != "object") return true;
   if (ValDate(sBox, szLabel) == false) {  // Invalid entry
      return false;
      }
   // Entry is valid, make sure one is there
   if ((sBox.value.length == 0) || (sBox.value.match(/^\s+$/))){ // ((sBox.value.length == 1) && (sBox.value == " "))){
      if (sBox.disabled == false) {sBox.focus();}
      if (typeof(szLabel) != "undefined") {
         alert("You must fill in a date for the field:\n\n" + szLabel);
         }
      else {
         alert("You must fill in a date.");
         }
      return false;
      }

   return true;
}

function VerifyNumSelected(sBox, iMax, szLabel) {
   var iNum;
   
   if (typeof(sBox) != "object") return true;
   iNum = CheckNumSelected(sBox, iMax, szLabel);
   if (iNum == -1) {  // Invalid entry
      return false;
      }
      
   // For the required fields, they can't not select something valid.
	if ((sBox.options[0].selected == true) && (sBox.options[0].value == "")) {
      if (sBox.disabled == false) {sBox.focus();}
      if (typeof(szLabel) != "undefined") {
         alert("You must select a valid choice from the list for the field:\n\n" + szLabel);
         }
      else {
         alert("You must select a valid choice from the list.");
         }
      return false;
      }      
      
   if ((iNum == 0) && (iMax > 1)) {
      if (sBox.disabled == false) {sBox.focus();}
      if (typeof(szLabel) != "undefined") {
         alert("You must select at least one value from the list for the field:\n\n" + szLabel);
         }
      else {
         alert("You must select at least one value from the list.");
         }
      return false;
      }
   else if (iNum == 0) {
      if (sBox.disabled == false) {sBox.focus();}
      if (typeof(szLabel) != "undefined") {
         alert("You must select one value from the list for the field:\n\n" + szLabel);
         }
      else {
         alert("You must select one value from the list.");
         }
      return false;
      }

   return true;
}

function VerifyIntegerRange(sBox, iMin, iMax, szLabel) {
   var iVal;
   
   if (ValNumber(sBox, szLabel) == false)  // Invalid entry
      return false;
    
   if (typeof(sBox) == "undefined") return true;
   
   iVal = parseInt(sBox.value, 10);
   
   if ((iVal < iMin) || (iVal > iMax)) {
      if (sBox.disabled == false) {sBox.focus();}
      sBox.select();
      if (typeof(szLabel) != "undefined") {
         alert("You must enter a value between " + iMin + " and " + iMax + " for the field:\n\n" + szLabel);
         }
      else {
         alert("You must enter a value between " + iMin + " and " + iMax + ".");
         }
      return false;
      }

   return true;
}

function VerifyRadio(sRadio, szLabel) {
   if (typeof(sRadio) != "object") return true;
   
   var ret = null;

   if (sRadio.length > 0) {
      for(i = 0; i < sRadio.length; i++ ) {
         if (sRadio[i].checked) {
            ret = sRadio[i].value;
            break;
            }
         }
       }
    else if (sRadio.checked) {
      /* special case where there is only one radio button */
      ret = sRadio.value;
      }
      
   if (ret == null) {
      alert("You must select a choice for " + szLabel);
      //sRadio[0].focus();
      if (sRadio[0].disabled == false) {sRadio[0].focus();}
      return false;
      }

   return true;
}


function MaxInput( sBoxmin, sBoxmax ){ 
   if(sBoxmax.value == '' ) sBoxmax.value = sBoxmin.value;
}



function CheckIntegerMany(e) {
   // Grab the keystroke entered
   if (bNetscape == true) {
      kStroke = e.which;
      }
   else {
      kStroke = e.keyCode;
      }
//alert("kStroke = " + kStroke);
   if ((kStroke > 47) && (kStroke < 58)) {
      return true;
      }
   if (kStroke == 8) return true; // Backspace
   if (kStroke == 9) return true; // Tab
   if (kStroke == 44) return true; // comma
   if (kStroke == 32) return true; // comma
   if (kStroke == 45) return true; // hyphen for negative search

   alert("      **** Integer Number Field ****\n\n" +
         "Only integer numbers allowed in this field.");
   return false;
}



//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------

