function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function updateClock ( )
{
  var currentTime = new Date ( );

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );
  var currentDay = currentTime.getDate ( );
  var currentMonth = currentTime.getMonth ( ) + 1;
  var currentMonthLong = currentTime.getMonth ( ) + 1;
  var currentYear = currentTime.getFullYear ( );
	var population = 1159376826;

	var month_string;
  
	switch(currentMonth)
	{
		case 1:
			month_string = "January"
		break;    
		case 2:
			month_string = "February"
		break;    
		case 3:
			month_string = "March"
		break;    
		case 4:
			month_string = "April"
		break;    
		case 5:
			month_string = "May"
		break;    
		case 6:
			month_string = "June"
		break;    
		case 7:
			month_string = "July"
		break;    
		case 8:
			month_sting = "August"
		break;    
		case 9:
			month_string = "September"
		break;    
		case 10:
			month_string = "October"
		break;    
		case 11:
			month_string = "November"
		break;    
		case 12:
			month_string = "December"
		break;    
		default:
			month_string = "January"
	}
	
	// Set the two dates
  var datetime_set =new Date(2009, 1, 18, 12, 0, 0); //YYYY-M-D-h:m:s // Note: Month is 0-11 in JavaScript
  //Get 1 day in milliseconds
  var one_second=1000;

  //Calculate difference btw the two dates, and convert to days
  var datediff = Math.floor((currentTime.getTime()-datetime_set.getTime())/(one_second));
	
	var births_per_second = 0.5832669763594020
	var pop_total = Math.floor(population + (datediff*births_per_second));

  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
  currentDay = ( currentDay < 10 ? "0" : "" ) + currentDay;
  currentMonth = ( currentMonth < 10 ? "0" : "" ) + currentMonth;
  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  //currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Compose the string for display
  var currentDateString = currentDay + " " + month_string + " " + currentYear;
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds;

  var pop_below_2dollars = addCommas(Math.floor(pop_total*0.39));
  var pop_below_pov_line = addCommas(Math.floor(pop_total*0.26));
  var pop_with_HIV_AIDS = addCommas(Math.floor(pop_total*0.036));
  var pop_under_18 = addCommas(Math.floor(pop_total*0.40));
  pop_total = addCommas(Math.floor(pop_total));
		
	// Update the time display
  document.getElementById("date").innerHTML               = "<div class='feed_date'>" + currentDateString + "</div>";
  document.getElementById("clock").innerHTML              = "<div class='feed_time'>" + currentTimeString + "</div>";
  document.getElementById("pop_total").innerHTML          = "<div class='feed_names'>Population:</div><div class='feed_numbers'>" + pop_total + "</div>";
  document.getElementById("pop_below_2dollars").innerHTML = "<div class='feed_names'>People living on $2 or less:</div><div class='feed_numbers'>" + pop_below_2dollars + "</div>";
  document.getElementById("pop_below_pov_line").innerHTML = "<div class='feed_names'>People living below the national poverty line: </div><div class='feed_numbers'>" + pop_below_pov_line + "</div>";
  document.getElementById("pop_with_hiv_aids").innerHTML  = "<div class='feed_names'>People living with HIV/AIDS: </div><div class='feed_numbers'>" + pop_with_HIV_AIDS + "</div>";
  document.getElementById("pop_under_18").innerHTML       = "<div class='feed_names'>Population under the age of 18: </div><div class='feed_numbers'>" + pop_under_18 + "</div>";

}
