//
// 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-9]{0,2}\/{1}[0-9]{0,2}\/{1}[0-9]{2,4}/;

// Check for leap year
function CheckLeap(yy) {

  // if not divisible by 4 then not a leap year  
  if ((yy % 4) != 0) return false;
  
  // if a century year, then must be divisible by 400 also
  if ( ((yy % 100) == 0) && ((yy % 400) != 0) ) return false;
  
  // All that's left is a leap year
  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 == true)) {
      bAlertFlag = false;
      alert("Dates must be entered in the form mm/dd/yyyy such as 01/23/2000.");
      sBox.focus();
      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).");
      sBox.focus();
      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.");
           sBox.focus();
           sBox.select();
           return false;
           }
         if (dd > 31) {
           bAlertFlag = false;
           alert("The day may not be more than 31.");
           sBox.focus();
           sBox.select();
           return false;
           }
         break;
         }
      case 2: {
         if (dd < 1) {
           bAlertFlag = false;
           alert("The day may not be less than 1.");
           sBox.focus();
           sBox.select();
           return false;
           }
         if ((dd > 28) && (bLeap == false)) {
           bAlertFlag = false;
           alert("February has only 28 days.");
           sBox.focus();
           sBox.select();
           return false;
           }
         if ((dd > 29) && (bLeap == true)) {
           bAlertFlag = false;
           alert("February has only 29 days.");
           sBox.focus();
           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.");
           sBox.focus();
           sBox.select();
           return false;
           }
         if (dd > 30) {
           bAlertFlag = false;
           alert("The day may not be more than 30.");
           sBox.focus();
           sBox.select();
           return false;
           }
         break;
         }
      default: {
         bAlertFlag = false;
         alert("The month must be between 1 and 12.");
         sBox.focus();
         sBox.select();
         return false;
         break;
         }
      }
   
   if ((yy < 1000) && (bAlertFlag == true)) {
      bAlertFlag = false;
      alert("The year must be 4 digits (such as 2000).");
      sBox.focus();
      sBox.select();
      return false;
      }
   if ((yy > 2100) && (bAlertFlag == true)) {
      bAlertFlag = false;
      alert("The year is out of range.");
      sBox.focus();
      sBox.select();
      return false;
      }
      
   // So it must be ok
   bAlertFlag = true;
   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);
   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.");
         }
      sBox.focus();
      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.");
         }
      sBox.focus();
      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");
         }
      sBox.focus();
      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 + ".");
            }
         sBox.focus();
         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 + ".");
            }
         sBox.focus();
         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) < 47) || (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 (no commas) are allowed in the field:\n\n" +
               szLabel);
         }
      else {
         alert("      **** Integer Number Field ****\n\n" +
               "Please enter only numbers in this box. (no commas)");
         }
      sBox.focus();
      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 + ".");
            }
         sBox.focus();
         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 + ".");
            }
         sBox.focus();
         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) < 47)  || (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.");
         }
      sBox.focus();
      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.");
         }
      sBox.focus();
      sBox.select();
      return false;
      }
         
   bAlertFlag = true;
   return true;
}

function ValChar(sBox, iMax, szLabel) {
   var szVal;

   if (typeof(sBox) == "undefined") {
      bAlertFlag = true;
      return true;
      }
   szVal = new String(sBox.value);

   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.");
         }
      sBox.focus();
      return false;
      }
   if ((szVal.search(/\<\s*(\S+)(\s[^\>]*)?\>[\s\S]*\<\s*\/{1}\s*(\S+)\>/gi) >= 0) && (bAlertFlag == true)) {
	  bAlertFlag = false;  // Turn off the alert cycle.
	  alert("HTML Tags are not permitted in date entry fields.");
      sBox.focus();
      return false;	  
   }
   bAlertFlag = true;
   return true;
}

//-------------------------------------------------------------------

// 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" +
         "Please enter only numbers in this box. (no commas)");
   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

   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) {
      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;
}

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.");
      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.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) {
      sBox.focus();
      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.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) {
      sBox.focus();
      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) {
      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.focus();
      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.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.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 == "")) {
      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)) {
      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) {
      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)) {
      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();
      return false;
      }

   return true;
}


function MaxInput( sBoxmin, sBoxmax ){ 
   if(sBoxmax.value == '' ) sBoxmax.value = sBoxmin.value;
}




//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
