//
// 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}/;

// This validates a 4 digit year date.
function ValDate(sBox) {
   var szDate,
       mm,
       dd,
       yy;

   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);
   
   // Now check numeric ranges
   if (((mm < 1 || mm > 12) && (bAlertFlag == true)) || (isNaN(mm))){
      bAlertFlag = false;
      alert("The month must be between 1 and 12.");
      sBox.focus();
      sBox.select();
      return false;
      }
   if (((dd < 1 || dd > 31) && (bAlertFlag == true)) || (isNaN(dd))) {
      bAlertFlag = false;
      alert("The day must be between 1 and 31.");
      sBox.focus();
      sBox.select();
      return false;
      }
   // 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;
      }
   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) {
   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();
   
   dToday = new Date();
   dToday.setHours(0);
   dToday.setMinutes(0);
   dToday.setSeconds(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 (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;
         }
      }
   
}

//-------------------------------------------------------------------
//

function ValNumber(sBox) {
   var i,
       szVal,
       bBad = false,
       bPoint = false;

   szVal = new String(sBox.value);
   pointCheck = new String(sBox.value);
   pointCheck = pointCheck.substring(pointCheck.indexOf('.')+1,pointCheck.length);
   
   if ( pointCheck.indexOf('.') != -1 )
        bPoint = true;
   
   for (i = 0; i < szVal.length; i++) {
      if ((szVal.charCodeAt(i) < 46) || (szVal.charCodeAt(i) > 57)) {
         bBad = true;
         break;
         }
      }
      
   if (bPoint == true) {
       bAlertFlag = false;  // Turn off the alert cycle.
      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.
      alert("      **** Number Field ****\n\n" +
            "Only numbers allowed in this field.");
      sBox.focus();
      sBox.select();
      return false;
      }
      
   bAlertFlag = true;
   return true;
}

function ValInteger(sBox) {
   var i,
       szVal,
       bBad = false;

   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.
      alert("      **** Integer Number Field ****\n\n" +
            "Only integer numbers allowed in this field.");
      sBox.focus();
      sBox.select();
      return false;
      }
      
   bAlertFlag = true;
   return true;
}

function ValDimension(sBox, iMax) {
   var i,
       szVal,
       bBad = false;

   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)                                    ) {
         bBad = true;
         break;
         }
      }

   if ((bBad == true) && (bAlertFlag == true)) {
      bAlertFlag = false;  // Turn off the alert cycle.
      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.
      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) {
   var szVal,

   szVal = new String(sBox.value);

   if ((szVal.length > iMax) && (bAlertFlag == true)) {
      bAlertFlag = false;  // Turn off the alert cycle.
      alert("You have exceeded the field limit of " + iMax + " characters.");
      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" +
         "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 == 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 == 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 (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) {
//   sBox = eval(sBox);

   var i,
       iCount;
   
   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
   
   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) {
   var iVal;
   
   if (ValNumber(sBox) == false) {  // Invalid entry
      return false;
      }
   // Entry is valid, make sure one is there
   if (sBox.value.length == 0) {
      sBox.focus();
      alert("You must fill in a value.");
      return false;
      }
   iVal = parseFloat(sBox.value, 10);
   if (iVal < iMin) {
      sBox.focus();
      sBox.select();
      alert("You must enter a minimum value of " + iMin + " for this field.");
      return false;
      }

   return true;
}

function VerifyInteger(sBox, iMin) {
   var iVal;
   
   if (ValNumber(sBox) == false) {  // Invalid entry
      return false;
      }
   // Entry is valid, make sure one is there
   if (sBox.value.length == 0) {
      sBox.focus();
      alert("You must fill in a value.");
      return false;
      }
   iVal = parseInt(sBox.value, 10);
   if (iVal < iMin) {
      sBox.focus();
      sBox.select();
      alert("You must enter a minimum value of " + iMin + " for this field.");
      return false;
      }

   return true;
}

function VerifyDimension(sBox, iMax) {
   if (ValDimension(sBox) == false) {  // Invalid entry
      return false;
      }
   // Entry is valid, make sure one is there
   if (sBox.value.length == 0) {
      sBox.focus();
      sBox.select();
      alert("You must fill in a value.");
      return false;
      }

   return true;
}

function VerifyChar(sBox, iMax) {
   if (ValChar(sBox) == false) {  // Invalid entry
      return false;
      }
   // Entry is valid, make sure one is there
   if (sBox.value.length == 0) {
      sBox.focus();
      alert("You must fill in a value.");
      return false;
      }

   return true;
}

function VerifyDate(sBox, iMax) {
   if (ValDate(sBox) == false) {  // Invalid entry
      return false;
      }
   // Entry is valid, make sure one is there
   if (sBox.value.length == 0) {
      sBox.focus();
      alert("You must fill in a date.");
      return false;
      }

   return true;
}

function VerifyNumSelected(sBox, iMax) {
   var iNum;
   
   iNum = CheckNumSelected(sBox, iMax);
   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();
      alert("You must select a valid choice from the list.");
      return false;
      }      
      
   if ((iNum == 0) && (iMax > 1)) {
      sBox.focus();
      alert("You must select at least one value from the list.");
      return false;
      }
   else if (iNum == 0) {
      sBox.focus();
      alert("You must select one value from the list.");
      return false;
      }

   return true;
}

function VerifyIntegerRange(sBox, iMin, iMax) {
   var iVal;
   
   if (ValNumber(sBox) == false)  // Invalid entry
      return false;
    
   iVal = parseInt(sBox.value, 10);
   
   if ((iVal < iMin) || (iVal > iMax)) {
      sBox.focus();
      sBox.select();
      alert("You must enter a value between " + iMin + " and " + iMax + ".");
      return false;
      }

   return true;
}

//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
