// Variables
var theDate;  // The date choosen

// Writes the code to create a calender button on the webpage
// Make sure you have a: calendar.gif in the same folder as this
// so that the <img> works correctly.
// Usage: <script type="text/javascript>addCal(name);</script>
function addCal()
{
  document.write('<a onClick="showCal(); return true;"><img src="images/calendar.gif" border="0"><\/a>');
}

// Function that runs when the user clicks the calendar image
// It shows the actual Calendar popup
function showCal()
{
  // Center on the screen
  var top = (screen.height/2)-90;
  var left = (screen.width/2)-105;

  // Open
  window.open('calendar.php', 'popcalendar','height=180,width=210,scrollbars=no,resizable=no,menubar=no,toolbar=no,location=no,top='+top+',left='+left);
  return false;
}

// Called by the popup, it returns in the format
// It calls with the name and the new value for that
function calendarPopupClick(value)
{
  // Get the value
  theDate = value;

  // Put in a nice format
  var split = theDate.split("-");
  var year = split[0];
  var monthStr = split[1];
  var day = split[2];
  var month = parseInt(monthStr);
  
  // Weird Javascript Error I experience for numbers 08 and 09
  if (monthStr == "08")
    month = 8;
  if (monthStr == "09")
    month = 9;

  var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  month = months[month-1];
  var niceString = month + " " + day + ", " + year;

  // Set the date textfield
  document.getElementById("date").value = niceString;
}
