//pir => parseint +  isInt (NaN) + replace comma
function pir(val)
{
return parseInt(isInt(repStr(val,",","")));
}

function isInt (str)
{
	var i = parseInt (str);

	if (isNaN (i))
		return 0;

	i = i . toString ();
	if (i != str)
		return 0;

	return str;
}


function repStr(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function



function formatNumber(fieldName)
{
var value = document.getElementById(fieldName).value;
value = repStr(value,",","")
var valueLen = value.length;
//alert(valueLen);
switch (valueLen)
{
case 4:
document.getElementById(fieldName).value = value.substring(0,1) + ',' + value.substring(1, valueLen);
break;
case 5: 
document.getElementById(fieldName).value = value.substring(0,2) + ',' + value.substring(2, valueLen);
break;
case 6:
document.getElementById(fieldName).value = value.substring(0,3) + ',' + value.substring(3, valueLen);
break;
case 7:
document.getElementById(fieldName).value = value.substring(0,1) + ',' + value.substring(1, 4) + ',' + value.substring(4, valueLen);
break;
case 8:
document.getElementById(fieldName).value = value.substring(0,2) + ',' + value.substring(2, 5) + ',' + value.substring(5, valueLen);
break;
case 9:
document.getElementById(fieldName).value = value.substring(0,1) + ',' + value.substring(1, 3) + ',' + value.substring(3, 6)+ ',' + value.substring(6, valueLen);;
break;
case 1:
case 2:
case 3:
document.getElementById(fieldName).value = value;

}

}

