
var regEndSpace = new RegExp();
var regFrontSpace = new RegExp();
regEndSpace.compile(" +$");
regFrontSpace.compile("^ +");

function isNotEmpty(str) { return !isEmpty(str) };
function isEmpty(str) 
{
   if (str == "") return true;
   if (str) return (trimString(str).length == 0);
   return true;
}
function trimString(str) 
{
   str = String(str);
   str =  str.replace(regEndSpace, "");
   return str.replace(regFrontSpace, "");
}
function isNumber(c) 
{
   if (c == "") return false;
   if ((c >= "0") && (c <= "9"))
      return true;
   return false;
}
function isUpperCase(c) 
{
   if (c == "") return false;
   if ((c >= "A") && (c <= "Z"))
      return true;
   return false;
}

function isValidUpin(v)
{
   if (checkforValidUpin(v) == false) {
      alert("UPIN: " + v +"\nA valid UPIN consists of 6 characters, 1st is Upper Case Alpha");
      return false;
   }
   return true;
}

function checkforValidUpin(v)
{
   if (v != "") {
      if ( (v.length != 6) || !isUpperCase(v.charAt(0)) || !isNumber(v.charAt(1)) ||
            !isNumber(v.charAt(2)) || !isNumber(v.charAt(3)) || !isNumber(v.charAt(4)) ||
           !isNumber(v.charAt(5)) ) {
         return false;
      }
   }
   return true;
}


/*
 * Check for valid and duplicate NPI numbers
 */
function CheckNPI(npi)
{
   npi = trimString(npi)
   if (npi == "") 
      return true;
   var m = checkNPIValue(npi)
   if (m != "") {
      alert("NPI: " + m + " ("+npi+") \n");
      return false;
   }
   return true;
}

function checkNPIValue(npi)
{
   if ((npi.length !== 10) && (npi.length != 15))
      return "Number has to be 10 or 15 characters in length." ;

   for (var i=0;i<npi.length;i++) {
      if (!isNumber(npi.charAt(i)))
         return "Invalid digit. Only 0-9 allowed. " ;
   }

   if (!isValidNPI(npi))
      return "Invalid NPI number. ";
   return "";
}

function isValidNPI(npi)
{
   var tmp = 0;  // Current digit of interest
   var sum = 0;  // Hash
   var i;    // Length of NPI
   var j;    // Even-odd bit


   /* the NPI is a 10 digit number, but it could be
    * preceded by the ISO prefix for the USA (80840)
    * when stored as part of an ID card.  The prefix
    * must be accounted for, so the NPI check-digit
    * will be the same with or without prefix.
    * The magic constant for 80840 is 24.
    */
   i = npi.length;
   var eight = npi.substring(0, 5);
   //alert("eight " + eight);
   if ((i == 15) && (eight == "80840"))
      sum = 0;
   else if (i == 10)
      sum = 24;           /* to compensate for the prefix */
   else return false;      /* length must be 10 or 15 bytes */

   /* the algorithm calls for calculating the check-digit
    * from right to left.
    * First, intialize the odd/even counter, taking into account
    * that the rightmost digit is presumed to be the check-sum
    * so in this case the rightmost digit is even instead of
    * being odd
    */
   j = 0;
   /* now scan the NPI from right to left */
   var m = "";
   while (i != 0) {  
      /* only digits are valid for the NPI */
         m += "\nisNumber " + npi.charAt(i - 1) + " -- " + isNumber(npi.charAt(i - 1))
      if (!isNumber(npi.charAt(i - 1))) {
         alert ("i " + i + " - " + m);
         return false;
      }

      /* this conversion works for ASCII and EBCDIC */
      tmp = npi.charAt(i - 1) - '0';
      /* the odd positions are multiplied by 2 */
      if ((j++ % 2) != 0) {   
         if ((tmp <<= 1) > 9) {  /* when the multiplication by 2 
             * results in a two digit number
             * (i.e., greater than 9) then the
             * two digits are added up.  But we
             * know that the left digit must be
             * '1' and the right digit must be
             * x mod 10.  In that case we can
             * just subtract 10 instead of 'mod'
             */
            tmp -= 10;  /* 'tmp mod 10' */
            tmp++;      /* left digit is '1' */
         }
      }
      sum += tmp;
      i--;
   }

   //alert (m);
   /* If the checksum mod 10 is zero then the NPI is valid */
   if ((sum % 10) == 0)
      return true;
   else 
      return false;
}
