<!--
//--------------------------------------------------------------
// Author: Dewey C. Layman
// Date: 3/18/1999
// File: UtilityFunctions.js
//--------------------------------------------------------------

   //-----------------------------------------------------------
   // Trim() will return the variable passed without 
   // the leading and trailing spaces 
   //-----------------------------------------------------------
   function Trim(val)
   {
      return (LTrim(RTrim(val)));
   }

   //-----------------------------------------------------------
   // LTrim() will return the variable passed without 
   // the leading spaces 
   //-----------------------------------------------------------
   function LTrim(val)
   {
      var i;

      for (i = 0; i < val.length; i++) {
         if (val.charAt(i) != ' ') {
            break;
         }
      }
      return (val.substring(i, val.length));
   }

   //-----------------------------------------------------------
   // RTrim() will return the variable passed without 
   // the trailing spaces 
   //-----------------------------------------------------------
   function RTrim(val)
   {
      var i;

      for (i = val.length; i >= 0; i--) {
         if (val.charAt(i - 1) != ' ') {
            break;
         }
      }
      return (val.substring(0, i));
   }

   //-----------------------------------------------------------
   // isEmpty() returns true if the variable passed is empty
   // after the Trim() function, and returns false is the 
   // passed variable is not empty after the Trim() function.
   //-----------------------------------------------------------
   function isEmpty(val)
   {
      return (val == '')?true:false;
   }
   
   //-----------------------------------------------------------
   // isInteger() returns true if the variables passed is an 
   // integer or false if the variable is not a integer
   //-----------------------------------------------------------
   function isInteger(val)
   {
      var integerList = '0123456789';
      var isInteger = false;

      for (var i = 0; i < val.length; i++) {
         isInteger = false;
         for (var j = 0; j < integerList.length; j++) {
            if (integerList.charAt(j) == val.charAt(i)) {
               isInteger = true;
               break;
            } 
         } 
         if (!isInteger) {
            break;
         } 
      } 
      return isInteger;
   }      

   //-----------------------------------------------------------
   // isDouble() returns true if the variables passed is an 
   // double or returns false if the variable is not a double
   //-----------------------------------------------------------
   function isDouble(val)
   {
      var decimalCtr = 0;
      var leftVal = '', rightVal = '';
      
      if (!isEmpty(val)) {
         if (val.charAt(0) == ".") {
            val = "0" + val;
         }
      }
      for (var i = 0; i < val.length; i++) {
         if (val.charAt(i) == '.') {
            decimalCtr++;
         }
         else if (decimalCtr == 0) {
            leftVal += val.charAt(i);
         }
         else if (decimalCtr == 1) {
            rightVal += val.charAt(i);
         }
      }
      if (decimalCtr == 0 && !isInteger(leftVal)) {
         return false;
      }
      if (isEmpty(leftVal) || isEmpty(rightVal)) {
         return false;
      }
      else if ((leftVal.length > 0 && !isInteger(leftVal)) || 
         (rightVal.length > 0 && !isInteger(rightVal))) {
            return false;
      }
      else {
         return true;
      }
   }

   //-----------------------------------------------------------
   // isAlphabetic() returns true if the variables passed is  
   // alphabetic or returns false if the variable is not 
   // alphabetic
   //-----------------------------------------------------------
   function isAlphabetic(val, exceptions)   
   {
      if (exceptions == null) {
         exceptions = "";
      }
      var upperCaseList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + exceptions;
      var lowerCaseList = "abcdefghijklmnopqrstuvwxyz";
      var isUpperCase = false, isLowerCase = false;

      for (var i = 0; i < val.length; i++) {
         isUpperCase = isLowerCase = false;
         for (var j = 0; j < lowerCaseList.length; j++) {
            if (val.charAt(i) == lowerCaseList.charAt(j)) {
               isLowerCase = true;
               break;
            }
         }
         if (!isLowerCase) {
            for (var j = 0; j < upperCaseList.length; j++) {
               if (val.charAt(i) == upperCaseList.charAt(j)) {
                  isUpperCase = true;
                  break;
               }
            }
            if (!isUpperCase) {
               break;
            }
         }
      }
      return (isLowerCase || isUpperCase)?true:false;
   }

   //-----------------------------------------------------------
   // isAlphaNumeric() returns true if the variables passed is  
   // alpha/numeric or returns false if the variable is not 
   // alpha/numeric
   //-----------------------------------------------------------
   function isAlphaNumeric(val, exceptions)
   {
      var isAlphaNumeric = false;
      
      for (var i = 0; i < val.length; i++) {
         isAlphaNumeric = false;
         if (!isInteger(val.charAt(i))) {
            if (!isAlphabetic(val.charAt(i), exceptions)) {
               break;
            }
            else {
               isAlphaNumeric = true;
            }
         }
         else {
            isAlphaNumeric = true;
         }
      }
      return isAlphaNumeric;
   }
   
   //-----------------------------------------------------------
   // isDate() returns true if the variables passed is in a 
   // valid date format or returns false if the variable is 
   // not in a valid date format
   //-----------------------------------------------------------
   function isDate(val) 
   {
      if (val.length != 10) {
         return false;
      }
      else if (val.charAt(2) != '/' || val.charAt(5) != '/' ||
         !isInteger(val.substring(0, 2)) || 
         !isInteger(val.substring(3, 5)) || 
         !isInteger(val.substring(6, 10))) {
            return false;
      }
      else
         return true;
   }
   
   //-----------------------------------------------------------
   // isLeapYear() returns true if the variable passed is a 
   // leap year or returns false if the variable is 
   // not in a leap year; requires 4 digit year
   //-----------------------------------------------------------
   function isLeapYear(val) 
   {
      var year = '';
      
      if (val.length >= 4) {
         year = val.substring(val.length - 4, val.length);
         if (isInteger(year)) {
            return ((parseInt(year) % 4 == 0)?true:false);
         }
      }
      return false;
   }

   //-----------------------------------------------------------
   // isEmail() returns true if the variables passed is in a 
   // valid e-mail address format or returns false if the 
   // variable is not in a valid e-mail address format
   //-----------------------------------------------------------
   function isEmail(val) 
   {
      if (isEmpty(val) || val.indexOf("@") == -1 || val.indexOf(".") == -1 || 
         (val.lastIndexOf(".") < val.indexOf("@")) ||
         (val.lastIndexOf(".") == (val.indexOf("@") + 1)) ||
         (val.lastIndexOf(".") == (val.length - 1)) ||
         (val.indexOf("@") == 0)) {
         return false;
      }
      else {
         return true;
      }
   }
   
   //-----------------------------------------------------------
   // isImageFile() returns true if the file name passed does 
   // not have a ".jpg" or ".gif" file extension, and returns
   // true if is does have a ".jpg" or ".gif" file extension.
   //-----------------------------------------------------------
   function isImageFile(val)
   {
      if (val.length < 5)
         return false;
         
      var imgExt = val.substring(val.length - 4, val.length).toUpperCase();
      
      if (imgExt != ".JPG" && imgExt != ".GIF")
         return false;
      else
         return true;
   }
   
   //-----------------------------------------------------------
   // isHtmlFile() returns true if the file name passed does 
   // not have a ".htm" or ".html" file extension, and returns
   // true if is does have a ".htm" or ".html" file extension.
   //-----------------------------------------------------------
   function isHtmlFile(val)
   {
      if (val.length < 5)
         return false;

      var htmExt = val.substring(val.length - 4, val.length).toUpperCase();
      var htmlExt = val.substring(val.length - 5, val.length).toUpperCase();

      if (htmExt != ".HTM" && htmlExt != ".HTML")
         return false;
      else
         return true;
   }
   
   //-----------------------------------------------------------
   // getFileName() will return the file name only when given 
   // full path of the file.
   //-----------------------------------------------------------
   function getFileName(val)
   {
      if (isEmpty(val))
         return '';
      else if(val.indexOf("/") == -1)
         return val;
         
      var i;

      for (i = val.length; i >= 0; i--) {
         if (val.charAt(i - 1) == '/' || 
            val.charAt(i - 1) == '\\') {
               break;
         }
      }
      return (val.substring(i, val.length));
   }
   

   //-----------------------------------------------------------
   // sortArrayAsc() returns the array passed in ascending 
   // order.
   //-----------------------------------------------------------
   function sortArrayAsc(oldArray)
   {
      var limit = oldArray.length;
      var newArray = new Array(limit);
      var element = null;

      for (var i = 0; i < limit; i++) {
         element = oldArray[i];
         for (var j = 0; j < limit; j++) {
            if (newArray[j] == '' || newArray[j] == null) {
               newArray[j] = element;
               break;
            }
            else if (newArray[j] > element) {
               for (var k = limit; k > j - 1; k--)
                  newArray[k] = newArray[k - 1];
               newArray[j] = element;
               break;
            }
         }
      }
      return newArray;
   }

   //-----------------------------------------------------------
   // sortArrayDesc() returns the array passed in descending 
   // order.
   //-----------------------------------------------------------
   function sortArrayDesc(oldArray)
   {
      var limit = oldArray.length;
      var newArray = new Array(limit);
      var element = null;

      for (var i = 0; i < limit; i++) {
         element = oldArray[i];
         for (var j = 0; j < limit; j++) {
            if (newArray[j] == '' || newArray[j] == null) {
               newArray[j] = element;
               break;
            }
            else if (newArray[j] < element) {
               for (var k = limit; k > j - 1; k--)
                  newArray[k] = newArray[k - 1];
               newArray[j] = element;
               break;
            }
         }
      }
      return newArray;
   }
      
   //-----------------------------------------------------------
   // FormatNumber() returns the formatted number passed to 
   // the precision value passed.  If the number is not valid 
   // then null will be returned.
   //-----------------------------------------------------------
   function FormatNumber(val, precision)
   {
      var decimalCtr = 0;
      var validZero = false;
      var prefix = '', suffix = '', extra = '';

      for (var i = 0; i < val.length; i++) {
         if (val.charAt(i) == '.') {
            decimalCtr++;
         }
         else if (decimalCtr == 0) {
            if (val.charAt(i) != '0' || validZero) {
               prefix += val.charAt(i);
               if (!validZero) {
                  validZero = true;
               }
            }
         }
         else if (decimalCtr == 1 && suffix.length < precision) {
            suffix += val.charAt(i);
         }
         else {
            extra += val.charAt(i);
         }
      }
      if (prefix == '' && suffix == '') {
         return null;
      }
      else if ((prefix.length > 0 && !isInteger(prefix)) || 
         (suffix.length > 0 && !isInteger(suffix)) || 
         (extra.length > 0 && !isInteger(extra)) ||
         decimalCtr > 1) {
            return null;
      }
      else if ((suffix + suffix == 0) && prefix.length == 0) {
         return null;
      }
      else {   
         if (suffix.length < precision) {
            for (var j = suffix.length; j < precision; j++) {
               suffix += '0';
            }
         }
         if (prefix.length == 0) {
            prefix = '0';
         }
         if (precision == 0) {
            return (prefix);
         }
         else {
            return (prefix + '.' + suffix);
         }
      }
   }
   
   //-----------------------------------------------------------
   // setUrlParameter() will replace ' ' characters 
   // with the '+' character
   //-----------------------------------------------------------
   function setUrlParameter(val)
   {
      var param = '';
      
      for (var i = 0; i < val.length; i++) {
         if (val.charAt(i) == ' ') {
            param += '+';
         }
         else {
            param += val.charAt(i);
         }
      }
      return param;
   }
   
   //-----------------------------------------------------------
   // checkDate() checks if the date value passed is
   // a valid date; alerting the appropriate message
   // if it is not a valid date.
   //-----------------------------------------------------------
   function checkDate(obj)
   {
      // date value of the object passed
      var date = obj.value
      // date parsed into month, day, & year variables 
      // using the '/' as a delimiter      
      var month = '', day = '', year = '';
      // date delimiter counter variable
      var slashCtr = 0;
      // boolean variable set whether date,
      // without slashes is numeric
      var integer = true;

      if (isEmpty(date)) {
         obj.focus();
         obj.select();
         alert('Date Required\nFormat: MM/DD/YYYY');
      } 
      // END checking empty date
      else if (date.length <= 7) {
         obj.focus();
         obj.select();
         alert('Invalid Date\nFormat: MM/DD/YYYY');
      } 
      // END checking for invalid date length
      else {
         for (var i = 0; i < date.length; i++) {
            if (date.charAt(i) != '/') {
               number = false;
               if (!isInteger(date.charAt(i))) {
                  integer = false;
                  break;
               } 
               // END if..then; break out of for loop if 
               // date isn't numeric
               if (slashCtr == 0) {
                  month += date.charAt(i);
               }
               else if (slashCtr == 1) {
                  day += date.charAt(i);
               }
               else {
                  year += date.charAt(i);
               }
            } 
            // END if..then checking for '\' in date
            else {
               slashCtr++;
            } 
            // END if..then; increment slashCtr if a '/' is found
         } 
         // END for loop
         if (day.length == 1) {
            day = "0" + day.toString()
         }
         // append a zero to day if day length is 1
         if (month.length == 1) {
            month = "0" + month.toString()
         }
         // append a zero to month if month length is 1
         if (!integer) {
            obj.focus();
            obj.select();
            alert('No Characters Allowed in Date\nFormat: MM/DD/YYYY');
         } 
         // END if; alert if date is not numeric
         else if (isEmpty(day) || isEmpty(year)) {
            obj.focus();
            obj.select();
            alert('Invalid Date\nFormat: MM/DD/YYYY');
         } 
         // END else if; alert if day or year is empty
         else if (year.length != 4) {
            obj.focus();
            obj.select();
            alert('Need 4 Digit Year\nFormat: MM/DD/YYYY');
         } 
         // END else if; alert if year is not 4 digits in length
         else { 
            if (month < 1 || month > 12 || day < 1) {
               obj.focus();
               obj.select();
               alert('Invalid Date\nFormat: MM/DD/YYYY');
            } 
            // END if; alert if month is less than 1 or 
            // greater than 12 or the day is less than 1
            else if ((month == 1 || month == 3 || month == 5 ||
               month == 7 || month == 8 || month == 10 || month == 12) && day > 31) {
                  obj.focus();
                  obj.select();
                  alert('Only 31 Days in Month\nFormat: MM/DD/YYYY');
            } 
            // END else if; alert if the day is greater than 31 days 
            else if ((month == 4 || month == 6 || month == 9 ||
               month == 11) && day > 31) {
                  obj.focus();
                  obj.select();
                  alert('Only 30 Days in Month\nFormat: MM/DD/YYYY');
            } 
            // END else if; alert if the day is greater than 30 days 
            else if ((month == 2) && day > 29) {
               if (isLeapYear(year)) {
                  obj.focus();
                  obj.select();
                  alert('Only 29 Days in Month\nFormat: MM/DD/YYYY');
               } 
               // END if; alert if the day is greater than 29 days 
               // when a leap year
               else {
                  obj.focus();
                  obj.select();
                  alert('Only 28 Days in Month\nFormat: MM/DD/YYYY');
               } 
               // END if..then; alert if the day is greater than 28 days 
               // when not a leap year
            } 
            // END if..then checking for whether or not year is a leap year
            else if ((month == 2) && day > 28 && !isLeapYear(year)) {
               if (day == 29) {
                  obj.focus();
                  obj.select();
                  alert('Not a Leap Year\nFormat: MM/DD/YYYY');
               } 
               // END if; alert if the day equals 29 and 
               // the year is not a leap year 
            } 
            // END else if checking whether day entered is 29 
            else {
               obj.value = month + "/" + day + "/" + year
               return true;
            } 
            // END if..then; return true if date validation is OK
         } 
         // END if..then
      } 
      // END if..then validating the date 
      return false;
      // return false if date validation is not OK
   } 
   // END checkDate() function
//-->