function replace(string, text, by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}



// Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au
// Please acknowledge use of this code by including this header.

// CONSTANTS
var separator = ",";  // use comma as 000's separator
var decpoint = ".";  // use period as decimal point
var percent = "%";
var currency = "$";  // use dollar sign for currency

function formatNumber(number, format, print) {  // use: formatNumber(number, "format")
  if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

  if (number - 0 != number) return null;  // if number is NaN return null
  var useSeparator = format.indexOf(separator) != -1;  // use separators in number
  var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
  var useCurrency = format.indexOf(currency) != -1;  // use currency format
  var isNegative = (number < 0);
  number = Math.abs (number);
  if (usePercent) number *= 100;
  format = strip(format, separator + percent + currency);  // remove key characters
  number = "" + number;  // convert number input to string

   // split input value into LHS and RHS using decpoint as divider
  var dec = number.indexOf(decpoint) != -1;
  var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
  var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

   // split format string into LHS and RHS using decpoint as divider
  dec = format.indexOf(decpoint) != -1;
  var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
  var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

   // adjust decimal places by cropping or adding zeros to LHS of number
  if (srightEnd.length < nrightEnd.length) {
    var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
    nrightEnd = nrightEnd.substring(0, srightEnd.length);
    if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

 // patch provided by Patti Marcoux 1999/08/06
    while (srightEnd.length > nrightEnd.length) {
      nrightEnd = "0" + nrightEnd;
    }

    if (srightEnd.length < nrightEnd.length) {
      nrightEnd = nrightEnd.substring(1);
      nleftEnd = (nleftEnd - 0) + 1;
    }
  } else {
    for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
      if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
      else break;
    }
  }

   // adjust leading zeros
  sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
  while (sleftEnd.length > nleftEnd.length) {
    nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
  }

  if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
  var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
  output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
  if (isNegative) {
    // patch suggested by Tom Denn 25/4/2001
    output = (useCurrency) ? "(" + output + ")" : "-" + output;
  }
  return output;
}

function strip(input, chars) {  // strip all characters in 'chars' from input
  var output = "";  // initialise output string
  for (var i=0; i < input.length; i++)
    if (chars.indexOf(input.charAt(i)) == -1)
      output += input.charAt(i);
  return output;
}

function separate(input, separator) {  // format input using 'separator' to mark 000's
  input = "" + input;
  var output = "";  // initialise output string
  for (var i=0; i < input.length; i++) {
    if (i != 0 && (input.length - i) % 3 == 0) output += separator;
    output += input.charAt(i);
  }
  return output;
}

/****************************** dateformat.js *********************************
<!--
This file retrieved from the JS-Examples archives
http://www.js-examples.com
100s of free ready to use scripts, tutorials, forums.
Author: Scott Connelly - 0
-->
 *
 * Object Extension for Date object
 *   description:
 *          -Stores day and month arrays in Date object 
 *          -Provides a format function to 'pretty' print
 *           the date in custom formats
 *   parameters:
 *          format - accpets any variation of the following list
 *                 yyyy  is a 4-digit year - i.e., 2002   
 *                 yy    is a 2-digit year - i.e., 02
 *                 month is the full month - i.e., September
 *                 mon   is the first three letters of the month - i.e., Sep
 *                 mmm   is the number of the month - i.e., 9
 *                 hh    is hours - i.e., 3
 *                 mm    is minutes (always 2-digit) - i.e., 05
 *                 ss    is seconds (always 2-digit) - i.e., 08
 *                 ddd   is the first three letters of the day - i.e., Wed
 *                 dd    is the numerical day of the month - i.e, 25
 *                 day   is the full day of the week - i.e., Wednesday
 *                 timezone is the the timezone in hours from GMT - i.e., GMT+5
 *                 time24   is the time based on a 24 hour clock - i.e., 18:24   
 *                 time     is the time based on am/pm - i.e., 6:24PM  
 *   example:
 *           myDate = newDate()
 *           myDate.format("day, month dd, yyyy hh:mm:ss timezone")
 *           would return "Wednesday, September 25, 2002 12:14:11 GMT-5"
 *   note: 
 *           If customizing the dateFormat function be aware that the ordering
 *           of the replace calls
 *   author:
 *           Scott Connelly scottsweep@yahoo.com 1/3/2002
 ******************************************************************************/ 
//Store the date info in the Date object
Date.prototype.Months = ["January", "February", "March", 
                         "April", "May", "June", "July", 
                         "August", "September", "October", 
                         "November", "December"];
Date.prototype.Days = ["Sunday", "Monday", "Tuesday", 
                       "Wednesday", "Thursday", 
                       "Friday", "Saturday"];
Date.prototype.format = dateFormat;

function dateFormat(format) {
   var dateString = format;

   //yyyy  is a 4-digit year - i.e., 2002  
   dateString = dateString.replace( new RegExp("yyyy", "gi"), this.getYear() );
   //yy    is a 2-digit year - i.e., 02
   dateString = dateString.replace( new RegExp("yy", "gi"), new String( this.getYear() ).substring(2,4) );
   //month is the full month - i.e., September
   dateString = dateString.replace( new RegExp("month", "gi"), this.Months[this.getMonth()] );
   //mon   is the first three letters of the month - i.e., Sep
   dateString = dateString.replace( new RegExp("mon", "gi"), new String( this.Months[this.getMonth()] ).substring(0,3) );
   //mmm   is the number of the month - i.e., 9
   dateString = dateString.replace( new RegExp("mmm", "gi"), (this.getMonth() + 1) );   
   //hh    is hours - i.e., 3
   dateString = dateString.replace( new RegExp("hh", "gi"), this.getHours() );
   //mm    is minutes (always 2-digit) - i.e., 05
   var mm = new String( this.getMinutes() );
   if (mm.length == 1) mm = "0" + mm; //pad if single digit
   dateString = dateString.replace( new RegExp("mm", "gi"), mm );
   //ss    is seconds (always 2-digit) - i.e., 08
   var ss = new String( this.getSeconds() );
   if (ss.length == 1) ss = "0" + mm; //pad if single digit
   dateString = dateString.replace( new RegExp("ss", "gi"), ss ); 
   //ddd   is the first three letters of the day - i.e., Wed
   dateString = dateString.replace( new RegExp("ddd", "gi"), new String( this.Days[this.getDay()] ).substring(0,3) );
   //dd    is the numerical day of the month - i.e, 25
   dateString = dateString.replace( new RegExp("dd", "gi"), this.getDate() );
   //day   is the full day of the week - i.e., Wednesday
   dateString = dateString.replace( new RegExp("day", "gi"), this.Days[this.getDay()] );

   //timezone is the the timezone in hours from GMT - i.e., GMT+5
   tz = this.getTimezoneOffset();
   timezone = "";
   if (tz < 0)
      timezone = "GMT-" +  tz / 60;
   else if (tz == 0)
      timezone = "GMT";
   else
      timezone = "GMT+" + tz / 60;
   dateString = dateString.replace( new RegExp("timezone", "gi"), timezone );
   
   //time24   is the time based on a 24 hour clock - i.e., 18:24   
   var minutes = new String( this.getMinutes() );
   if (minutes.length == 1) minutes = "0" + minutes; //pad if single digit
   var seconds = new String( this.getSeconds() );
   if (seconds.length == 1) seconds = "0" + seconds; //pad if single digit

   var time24 = new String( this.getHours() + ":" + minutes );
   dateString = dateString.replace( new RegExp("time24", "gi"), time24 );
   
   //time     is the time based on am/pm - i.e., 6:24PM
   var time;
   var ampm;
   var hour = this.getHours();
   if ( hour < 12) {
      if (hour == 0) hour = 12;
         ampm = "AM"
   } else {
      if (hour !=12)
         hour = hour - 12;
      ampm = "PM";   
   }
   time = new String(hour + ":" + minutes + ":" + seconds + " " + ampm);     
   dateString = dateString.replace( new RegExp("time", "gi"), time );

   return dateString;   
}

//*********************** end of dateformat.js *********************************

