////////////////////////// verifies that non-empty string s contains only digits and at most one decimal point  ///////////////////////////////////////////////////

function isValidMoneyChars(s) {

   var i = 0;
   var c = '';
   var numOfDecimals = 0;
   var validMoneyChars = "0123456789.";

   for (i = 0; i < s.length; i++) { 
  
      c = s.charAt(i);

      if (validMoneyChars.indexOf(c) == -1) {
         return false;
      	} else {
      		if (c == '.') {
         	numOfDecimals++;
			}
		}
   }

   if (numOfDecimals <= 1) {
      return true;
	}

   return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////// removes commas, whitespace, and dollar signs from a non-empty string s  //////////////////////////////////////////////////////////////////

function stripExtraneousChars(s, keepDecimal, noZero) {
   var i = 0;
   var returnString = "";
   var c = '';
   var charsToKeep = "0123456789";
   
   if (keepDecimal) charsToKeep = charsToKeep + "."

	

  		 // Strip beginning 0's.

   		 while (s.charAt(i) == '-1' || charsToKeep.indexOf(i) == -1)
     	 i++
   
  		  for (i; i < s.length; i++) {   
     	  c = s.charAt(i);

    	 if (charsToKeep.indexOf(c) >= 0) returnString += c
			


	}




   return returnString;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



//////////////////////////// Validate a money field.  (Commas are removed when this function is done.)///////////////////////////////////////////////////////////////////

function checkMoney(theField, nameOfField) {

   var normalizedMoney = 0;


   // Remove all commas.
   var normalizedMoney = stripExtraneousChars(theField.value, true) ;

   // Verify that it only contains digits and at most one decimal point.

   if (!isValidMoneyChars(normalizedMoney) )  {
      warnInvalid(theField, "Invalid format -\nPlease enter " +  nameOfField + "\nin the format 9999999.99.");
      return false
   }

   theField.value = normalizedMoney
   
   return true
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




/////////////////// Add commas while the user is typing.  This function needs to be called  //////////////////////////////////////////////////////////////////////
///// every time a key is pressed.																																										////
///// !!!!!This does not work in Netscape because Netscape will put the cursor																									////
//////////////// !!!!!back at the beginning of the text box after the user hits each key.   ////////////////////////////////////////////////////////////////////////

function addCommas(theField, useDecimal, noZero) {

  if (navigator.appName.toLowerCase().indexOf("microsoft") > -1) {

  	if ( (event != null && (event.keyCode > 31 || event.keyCode == 8) ) || event == null) {
      var str = stripExtraneousChars(theField.value, false, noZero);
      var i = 0;
      
      if (str.length > 2) {

        if (useDecimal) {
          // Add decimal.
          i = str.length - 2
          str = str.substring(0, i) + "." + str.substring(i);
        }else{
          i = str.length;
        }
        
        // Add commas.
        i = i - 3
        while (i > 0)  {
           str = str.substring(0, i) + "," + str.substring(i)
           i = i - 3
        }
      }
      
      theField.value = str;

    }
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////