Number.prototype.toNearest = function(num) { // num is an exponent of 10
   return Math.round(this/num)*num;
}

function stringReplaceAll( strOld, strNew, strString )
{
   return strString.replace(/\,/g, '');
}


//Send this a number, and this function will return the number
//to two significant decimal places.
function floor(number)
{
   return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

//Send this function a string and if it needs comman and zeroes appropriately
//it will send them back with commas and decimals
function commasDecimals(fixit)
{
   var tempString = "";

   //MAKE SURE THAT THE LAST 3 DIGITS = ".00"
   if ((fixit.indexOf('.') == (fixit.length - 2))&&(fixit.length != 1)) {
      fixit += "0";
   } else if ((fixit.indexOf('.') == (fixit.length - 1))&&(fixit.length != 1)) {
      fixit += "00";
   } else if (fixit.indexOf('.') == -1 ) {
      fixit += ".00";
   }

   //Now, Add commas
   if (fixit.substring(fixit.indexOf('.'),0).length > 3) {
      //Add first comma, based on length of number from decimal
      tempString = fixit.substring(fixit.indexOf('.')-3,0) + "," + fixit.substring(fixit.length,fixit.indexOf('.')-3);
      fixit = tempString;

      //Now add more commas while necessary, based on length of number from first visible comma in the string
      while (fixit.indexOf(",") > 3) {
         tempString = fixit.substring(fixit.indexOf(',')-3,0) + "," + fixit.substring(fixit.length,fixit.indexOf(',')-3);
         fixit = tempString;
      }
   }

   //remove potential error "-," for negative numbers a multiple of 3 digits in length
   if ((fixit.indexOf('-') == 0)&&(fixit.indexOf(',') == 1)) {
      tempString = "-" + (fixit.substring(fixit.length,fixit.indexOf(',')+1));
      fixit = tempString;
   }

   //Fix requested by Jennifer Kohl - remove everything decimal and after - this is what is done in the application
   tempString = fixit.substring(0,fixit.indexOf('.'));
   fixit = tempString;

   return fixit;
}


function isBlank(data)
{
   if (data.length == 0)
      return 1;

   for (i = 0; i < (data.length); i++) {
      if (data.charAt(i) != " ")
         return 0;
   }

   return 1;
}

function isNumOnly(data)
{
   var i = new Number(data.length);
   var j = new Number(0);
   var k = new Number();

   for (j=0; j<i; j++) {
      if ((isNaN(data.charAt(j)) == true)) {
         if( data.charAt(j) != '.' )
            return 0;
      }
   }
   return 1;
}

function isInvalidCharacter(data)
{
   for (i=0; i<data.length; i++) {
      if ((data.charAt(i)=="~")||(data.charAt(i)=="!")||(data.charAt(i)=="@")||(data.charAt(i)=="#")
          ||(data.charAt(i)=="$")||(data.charAt(i)=="%")||(data.charAt(i)=="^")||(data.charAt(i)=="&")
          ||(data.charAt(i)=="*")||(data.charAt(i)=="(")||(data.charAt(i)==")")||(data.charAt(i)=="_")
          ||(data.charAt(i)=="+")||(data.charAt(i)=="=")||(data.charAt(i)=="{")||(data.charAt(i)=="[")
          ||(data.charAt(i)=="|")||(data.charAt(i)=="<")||(data.charAt(i)==",")||(data.charAt(i)==">")
          ||(data.charAt(i)=="}")||(data.charAt(i)=="]")||(data.charAt(i)=="?")||(data.charAt(i)=="/")) {
         return 1;
      }
   }
   return 0;
}

function isAlphaOnly(data, characterTest, spaceTest)
{
   i = new Number(data.length);
   j = new Number(0);

   if (characterTest == 0) {
      if (isInvalidCharacter(data) == 1)
         return 0;
   }

   if (spaceTest == 0) {
      for (j=0; j<i; j++) {
         if (data.charAt(j) == " ")
            return 0;
      }
   }

   for (j=0; j<i; j++) {
      if (isNaN(data.charAt(j)) == false)
         return 0;
   }
   if ((j==i)&&(i!=0))
      return 1;
}

function isExactLength(data, num)
{

   var howLong = new Number(data.length);
   var rightLength = new Number(num);

   return((howLong < rightLength)||(howLong > rightLength));
}

function isMinAndMaxLength(data, min, max)
{
   var minimum = new Number(min);
   var maximum = new Number(max);

   return((data.length < minimum) || (data.length > maximum));
}

function isBetweenMinAndMax(data, min, max)
{
   return((data < min)||(data > max));
}

function setSearchRanges(iMinPrice, iMaxPrice)
{
   try{
   
      var pMinPrice = document.getElementById('calculatorsearchminprice');
      var pMaxPrice = document.getElementById('calculatorsearchmaxprice');
      var pPriceDesc = document.getElementById('calculatorsearchpricedescription');
      var strMsg = '';
      if( pMinPrice && pMaxPrice && pPriceDesc ) {
         if( iMinPrice < 1 ) iMinPrice = '';
         if( iMaxPrice < 1 ) iMaxPrice = '';
         pMinPrice.value = iMinPrice;
         pMaxPrice.value = iMaxPrice;
         strMsg = ' between $' + AddCommas(iMinPrice) + ' and $' + AddCommas(iMaxPrice) + ' ';
      }
      pPriceDesc.innerHTML = strMsg;
   }catch (e){
      //Do nothing
   }
}

