function datetimeControl(name,date_control,date_format,control_type,inline)
{
  //Properties
  this.name = name;
  this.control = eval(date_control);
  this.format = date_format;
  this.showDate = control_type=="DateTime" || control_type=="DateTimeToday" || control_type=="DateToday" || control_type=="Date";
  this.showTime = control_type=="DateTime" || control_type=="DateTimeToday" || control_type=="TimeToday" || control_type=="TimeNow" || control_type=="Time";
  this.showButtonToday = control_type=="DateToday" || control_type=="DateTimeToday";
  this.showButtonNow = control_type=="TimeNow";
  this.showButtonOK = control_type=="TimeNow" || control_type=="Time";
  this.inline = inline;

  this.selectedDate = new Date();
  this.selectedDay = 1;
  this.currentMonth = this.selectedDate.getMonth();
  this.currentYear = this.selectedDate.getFullYear();
  this.currentHours = this.selectedDate.getHours();
  this.currentMinutes = this.selectedDate.getMinutes();
  this.currentSeconds = this.selectedDate.getSeconds();
  this.firstWeekDayIndex = 0;  
  this.eventday = new Array();
  this.eventmonth = new Array();
  this.eventyear = new Array(); 
  
  this.beginMonthHTML = "";
  this.endMonthHTML = "";

  this.divWindow = null;
  this.divMonth = null;
  this.ctlMonth = null;
  this.ctlYear = null;
  this.ctlHours = null;
  this.ctlMinutes = null;
  this.ctlSeconds = null;
  
  //Methods
  this.setInitialDate = datetimeControl_setInitialDate;
  this.show = datetimeControl_show;
  this.setToday = datetimeControl_setToday;
  this.setYear = datetimeControl_setYear;
  this.setPreviousYear = datetimeControl_setPreviousYear;
  this.setPreviousMonth = datetimeControl_setPreviousMonth;
  this.setMonth = datetimeControl_setMonth;
  this.setNextMonth = datetimeControl_setNextMonth;
  this.setNextYear = datetimeControl_setNextYear;
  this.setTime = datetimeControl_setTime;
  this.setupTimeSpin = datetimeControl_setupTimeSpin;
  this.returnDate = datetimeControl_returnDate;
  
  this.buildWindow = datetimeControl_buildWindow;
  this.buildTop = datetimeControl_buildTop;
  this.buildBottom = datetimeControl_buildBottom;
  this.rebuildBottom = datetimeControl_rebuildBottom;
  this.setEventday = datetimeControl_setEventday;

  this.buildWindow();
}

// Set the initial DateTime Control date to today or to the existing value in dateField
function datetimeControl_setInitialDate(inDate) 
{
  var date = this.selectedDate.parseDate(inDate, this.selectedDate.parseDateFormat(this.format));

  //alert(inDate+"\n"+this.format+"\n"+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());

  if (date) date = date.checkDateRange();

  // If the incoming date is invalid, use the current date
  if (isNaN(date) || !date) 
    date = new Date();

  
  this.selectedDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
  this.selectedDay  = date.getDate();

  this.currentHours = date.getHours();
  this.currentMinutes = date.getMinutes();
  this.currentSeconds = date.getSeconds();

  // Set day value to 1... to avoid javascript date calculation anomalies
  // (if the month changes to feb and the day is 30, the month would change to march
  //  and the day would change to 2.  setting the day to 1 will prevent that)
  date.setDate(1);

  this.currentMonth = date.getMonth();
  this.currentYear = date.getFullYear();
}

//Show or hide DateTimeControl
function datetimeControl_show() 
{
  var ctl_top = this.control.offsetTop;
  var ctl_left = this.control.offsetLeft;
  var parent = this.control.offsetParent;
  while(parent)
  {
    ctl_top += parent.offsetTop;
    ctl_left += parent.offsetLeft;
    parent = parent.offsetParent;
  }
	if (!this.inline)
	{
		this.divWindow.style.position = "absolute";
		this.divWindow.style.top = ctl_top+this.control.clientHeight+8;
		this.divWindow.style.left = ctl_left-35;
	}

  if (this.divWindow.style.display=="none" || !this.divWindow.style.display || this.inline)
  {
    this.setInitialDate(this.control.value);
    this.ctlYear.value = this.selectedDate.getFullYear();
    this.ctlMonth.value = this.selectedDate.getMonth();
    this.ctlHours.value = this.selectedDate.addZero(this.selectedDate.getHours(),2);
    this.ctlMinutes.value = this.selectedDate.addZero(this.selectedDate.getMinutes(),2);
    this.ctlSeconds.value = this.selectedDate.addZero(this.selectedDate.getSeconds(),2);
    this.rebuildBottom();
    this.divWindow.style.display = "inline";
  } else if (!this.inline) this.divWindow.style.display="none";
}

// Pre-build portions of the DateTime Control (for performance reasons)
function datetimeControl_buildWindow()
{

  this.firstWeekDayIndex = 0;
  var newWeekdayArray = new Array(7);
  var newWeekdayList = new Array(7);

  var swdays = this.selectedDate.listShortWeekdays;
  var wdays = this.selectedDate.listWeekdays;
  var fwday = this.selectedDate.firstWeekDay;
  for (var i=0; i<swdays.length; i++)
    if (swdays[i]==fwday) this.firstWeekDayIndex = i;

  for (var i=this.firstWeekDayIndex; i<swdays.length; i++)
  {
      newWeekdayArray[i-this.firstWeekDayIndex] = swdays[i];
      newWeekdayList[i-this.firstWeekDayIndex] = wdays[i];
  }

  for (var i=0; i<this.firstWeekDayIndex; i++)
  {
      newWeekdayArray[7-this.firstWeekDayIndex-i] = swdays[i];
      newWeekdayList[7-this.firstWeekDayIndex-i] = wdays[i];
  }

  var weekdays = "<tr>";

  for (i in newWeekdayArray)
  {
      weekdays += "<td class=\"dayCaption\" align=\"center\">&nbsp;" + newWeekdayArray[i] + "&nbsp;</td>";
  }

  weekdays += "</tr>";


  var showDate = this.showDate?"":" style=\"display:none\"";
  var showToday = " style='display:none'";
  var showTodayTitle = "";
  if (this.showButtonToday)
  {
    showToday = "";
    showTodayTitle = "Today";
  }
  if (this.showButtonNow)
  {
    showToday = "";
    showTodayTitle = "Now";
  }
  var showOK = this.showButtonOK?"":"style='display:none'";
  var showTime = this.showTime?"":"style='display:none'";

  var windowHTML = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr"+showDate+"><td>"+this.buildTop()+"</td></tr><tr"+showDate+"><td align=\"center\" valign=\"top\">"+
      "<div id=\""+this.name+"_month_window\" class=\"MonthWindow\"></div></td></tr>";

 
  windowHTML += "<tr><td><table cellspacing=\"1\" cellpadding=\"0\" border=\"0\">"+
      "<td " + showTime + ">" + 
      "<input class=\"timeBox\" type=\"text\" size=\"2\" maxlength=\"2\" id=\""+this.name+"_hours\" value=\""+this.currentHours+"\" onchange=\"javascript:"+this.name+".setTime('Hour',this.value)\" onfocus=\"javascript:"+this.name+".setupTimeSpin(0);\">" +
      "</td><td " + showTime + ">:</td>" +
      "<td " + showTime + ">" + 
      "<input class=\"timeBox\" type=\"text\" size=\"2\" maxlength=\"2\" id=\""+this.name+"_minutes\" value=\""+this.currentMinutes+"\" onchange=\"javascript:"+this.name+".setTime('Min',this.value)\" onfocus=\"javascript:"+this.name+".setupTimeSpin(1);\">" +
      "</td><td " + showTime + ">:</td>" +
      "<td " + showTime + ">" + 
      "<input class=\"timeBox\" type=\"text\" size=\"2\" maxlength=\"2\" id=\""+this.name+"_seconds\" value=\""+this.currentSeconds+"\" onchange=\"javascript:"+this.name+".setTime('Sec',this.value)\" onfocus=\"javascript:"+this.name+".setupTimeSpin(2);\">" +
      "</td>" +
      "<td width=\"100%\" " + showTime + ">" + 
      "<div id=\"" + this.name + "_spin_time\" for=\"" + this.name + "_seconds\" value=\"" + this.currentSeconds + "\" max=\"59\" min=\"0\" step=\"1\"></div></nobr>" +
      "</td><td " + showOK + ">" +
      "<input type=\"button\" value=\"OK\" onClick=\"javascript:"+this.name+".returnDate("+this.name+".selectedDate.getDate())\"></td><td " + showToday + ">" +
      "<input type=\"button\" value=\""+showTodayTitle+"\" onClick=\"javascript:"+this.name+".setToday()\"></td></table></td></tr></table>";

  this.beginMonthHTML = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"MonthTable\">" + weekdays;
  this.endMonthHTML += "</table>";


  this.divWindow = eval("document.all."+this.name+"_window");
  if (!this.divWindow)
  {
    this.divWindow = document.createElement("div");
    this.divWindow.id = this.name+"_window";
    this.divWindow.className = "CalendarWindow";
    document.body.appendChild(this.divWindow);
  }
  this.divWindow.innerHTML = windowHTML;

  this.divMonth = eval("document.all."+this.name+"_month_window");
  this.ctlMonth = eval("document.all."+this.name+"_month");
  this.ctlYear = eval("document.all."+this.name+"_year");
  var spinControl = eval(this.name+"_spin");
  createSpinControl(spinControl);
  /*var timespinControl = eval(this.name+"_spin_time");
  createSpinControl(timespinControl);*/
  this.setupTimeSpin(0);
  this.ctlHours = eval("document.all."+this.name+"_hours");
  this.ctlMinutes = eval("document.all."+this.name+"_minutes");
  this.ctlSeconds = eval("document.all."+this.name+"_seconds");
}

// Create the top DateTime Control part
function datetimeControl_buildTop() 
{
  var options_list = "";
  for (var i=0; i<this.selectedDate.listMonths.length; i++)
  {
    var selected = this.currentMonth==i?" selected":"";
    options_list += "<option value=\"" + i + "\" " + selected + ">" + this.selectedDate.listMonths[i] + "</option>";
  }
  var topHTML =
      "<table cellpadding=\"0\" cellspacing=\"4\" border=\"0\" width=\"100%\">" +
      "<tr>" +
      "<td>" +
      "<select id=\"" + this.name + "_month\" class=\"MonthInput\" onchange=\"javascript:" + this.name + ".setMonth(this.value)\"> " + options_list + "</select>" +
      "</td>" +
      "<td nobr width=\"100%\" align=\"right\">" +      
      "<input id=\"" + this.name + "_year\" size=2 class=\"YearInput\" maxlength=\"4\" value=\"" + this.currentYear + "\" onchange=\"javascript:" + this.name + ".setYear(this.value)\">" +
      "</td>" +
      "<td>" +
      "<div id=\"" + this.name + "_spin\" for=\"" + this.name + "_year\" value=\"" + this.currentYear + "\" max=\"9999\" min=\"1753\" step=\"1\"></div>" +
      "</td>" +
      "</tr>" +
      "</table>";
  return topHTML;
}

// Create the bottom DateTime Control part 
function datetimeControl_buildBottom() 
{
  // Check to see if year is a leap year
  function isLeapYear (Year)
  {
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0))
      return true;
    else 
      return false;
  }

  // Get number of days in month
  function getDaysInMonth(Month,Year)
  {
    var days;
    var month = Month+1;
    var year  = Year;

    if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
    {
      days=31;
    }
    else if (month==4 || month==6 || month==9 || month==11) 
    {
      days=30;
    }
    else if (month==2)  
    {
      if (isLeapYear(year))
      {
        days=29;
      }
      else 
      {
        days=28;
      }
    }
    return (days);
  }

  var datetimeDocument = "";

  var blankCell = "<td align=center class=\"workday\">&nbsp;</td>"; 

  month   = this.currentMonth;
  year    = this.currentYear;
  var date = new Date();


  var selectedDay = this.selectedDay;

  var i   = 0;

  var days = getDaysInMonth(this.currentMonth, this.currentYear);

  if (selectedDay > days) 
    selectedDay = days;

  var firstOfMonth = new Date (year, month, 1);

  var startingPos  = firstOfMonth.getDay() - this.firstWeekDayIndex;
  if (startingPos<0) startingPos += 7;
  days += startingPos;

  datetimeDocument += "<tr>";

  var columnCount = 0;

  for (i = 0; i < startingPos; i++) 
  {
    datetimeDocument += blankCell;
    columnCount++;
  }

  var currentDay = 0;
  var dayType    = "weekday";
  var dayBackground = "";

  for (i = startingPos; i < days; i++) 
  {
    var paddingChar = "";
    dayBackground = "";

    if (i-startingPos+1 < 10) 
      padding = "";
    else 
      padding = "";

    currentDay = i-startingPos+1;

    var currentDate = this.selectedDate;		

    if (currentDay == selectedDay && month==currentDate.getMonth() && year==currentDate.getFullYear())
    {
      dayType = "selectedDay";
      dayBackground = "selectedDay";
	  click = "";
	  href = "javascript:"+this.name+".returnDate(" + currentDay + ")";
    }
    else if (currentDay == date.getDate() && month==date.getMonth() && year==date.getFullYear())
    {
      dayType = "today";
      dayBackground = "today";
	  click = "";
	  href = "javascript:"+this.name+".returnDate(" + currentDay + ")";
    }else if (currentDay == this.eventday[currentDay] && month==this.eventmonth[currentDay] && year==this.eventyear[currentDay]){
      dayType = "eventday";
      dayBackground = "eventday";	  
	  click = "onclick=\"javascript:"+this.name+".returnDate(" + currentDay + ")\"";

	  curday = currentDay < 10 ? "0"+currentDay : currentDay;
	  curmonth = (this.currentMonth+1) < 10 ? "0"+(this.currentMonth+1) : (this.currentMonth+1);

	  href="events.php#" + this.currentYear + "-" + curmonth + "-" + curday;
	}else 
    {
      dayType = "weekDay";
	  click = "";
	  href = "javascript:"+this.name+".returnDate(" + currentDay + ")";
    }


    if ((columnCount + this.firstWeekDayIndex) % 7 == 0 || (columnCount + this.firstWeekDayIndex) % 7 == 6)
    {
      datetimeDocument += "<td align=\"center\" class=\""+(dayBackground?dayBackground:"weekend")+"\">" +
                "<a class=\"" + dayType + "\" href=\"" + href + "\" " + click + ">" + padding + currentDay + paddingChar + "</a></td>";
    }
    else 
    {
      datetimeDocument += "<td align=\"center\" class=\""+(dayBackground?dayBackground:"workday")+"\">" +
                "<a class=\"" + dayType + "\" href=\"" + href + "\" " + click + ">" + padding + currentDay + paddingChar + "</a></td>";
    }
    columnCount++;

    if (columnCount % 7 == 0) 
      datetimeDocument += "</tr><tr>";
  }

  for (i=days; i<(startingPos<0?42+startingPos:42); i++)
  {
    datetimeDocument += blankCell;
    columnCount++;

    if (columnCount % 7 == 0) 
    {
      datetimeDocument += "</tr>";
      if (i<(startingPos<0?41+startingPos:41)) datetimeDocument += "<tr>";
    }
  }

  return datetimeDocument;
}

// Rebuild the bottom of DateTime Control
function datetimeControl_rebuildBottom()
{
  var monthHTML = this.beginMonthHTML + this.buildBottom() + this.endMonthHTML;
  this.divMonth.innerHTML = monthHTML;
}

// Select the DateTime Control to today's date
function datetimeControl_setToday()
{
  var date = new Date();

  this.currentMonth = date.getMonth();
  this.currentYear = date.getFullYear();
  this.currentHours = date.getHours();
  this.currentMinutes = date.getMinutes();
  this.currentSeconds = date.getSeconds();

  this.returnDate(date.getDate());
}

//Set the DateTime Control to eventday's date

function datetimeControl_setEventday(Eventday, Eventmonth, Eventyear)
{		
	this.eventday[Eventday] = Eventday;	
	this.eventmonth[Eventday] = Eventmonth-1;
	this.eventyear[Eventday] = Eventyear;
}

// Set the global date to the selected year and redraw the DateTime Control
function datetimeControl_setYear(Year)
{
  var date = new Date();
  if (isNaN(parseInt(Year)) || !Year) 
    Year = this.ctlYear.value = date.getFullYear();
  date.setFullYear(parseInt(Year));
  date = date.checkDateRange();
  year = date.getFullYear();
  this.currentYear = year;
  this.rebuildBottom();
}

// Set the global date to the previous year and redraw the DateTime Control
function datetimeControl_setPreviousYear()
{
  var year = this.currentYear - 1;
  var date = new Date();
  date.setFullYear(year);
  date = date.checkDateRange();
  year = date.getFullYear();
  this.currentYear = year;
  this.rebuildBottom();
}

// Set the global date to the next year and redraw the DateTime Control
function datetimeControl_setNextYear()
{
  var year  = this.currentYear + 1;
  var date = new Date();
  date.setFullYear(year);
  date = date.checkDateRange();
  year = date.getFullYear();
  this.currentYear = year;
	this.rebuildBottom();
}

// Set the global date to the selected month and redraw the DateTime Control
function datetimeControl_setMonth(Month)
{
  this.currentMonth = parseInt(Month);
  this.rebuildBottom();
}

// Set the global date to the previous month and redraw the DateTime Control
function datetimeControl_setPreviousMonth()
{
  var year  = this.currentYear;
  var month = this.currentMonth;

  if (month == 0)
  {
    month = 11;
    if (year > 1000)
    {
      year--;
      var date = new Date();
      date.setFullYear(year);
      date = date.checkDateRange();
      year = date.getFullYear();
      this.currentYear = year;
    }
  }else 
  {
    month--;
  }
  this.currentMonth = month;
  this.rebuildBottom();
}

// Set the global date to the next month and redraw the DateTime Control
function datetimeControl_setNextMonth()
{
  var year = this.currentYear;
  var month = this.currentMonth;

  if (month == 11)
  {
    month = 0;
    year++;
    var date = new Date();
    date.setFullYear(year);
    date = date.checkDateRange();
    year = date.getFullYear();
    this.currentYear = year;
  } else
  {
    month++;
  }
  this.currentMonth = month;
  this.rebuildBottom();
}

// Set the global date to the new time and redraw the DateTime Control
function datetimeControl_setTime(part, value)
{
  var time_value = parseInt(value);
  if (isNaN(time_value))
  {
    if (part=="Hour")
      this.ctlHours.value = this.selectedDate.addZero(this.currentHours,2);
    if (part=="Min")
      this.ctlMinutes.value = this.selectedDate.addZero(this.currentMinutes,2);
    if (part=="Sec")
      this.ctlSeconds.value = this.selectedDate.addZero(this.currentSeconds,2);
    return false;
  }
  if (part=="Hour")
    this.currentHours = time_value;
  if (part=="Min")
    this.currentMinutes = time_value;
  if (part=="Sec")
    this.currentSeconds = time_value;
  if (part=="Hour")
  {
    this.ctlHours.value = this.selectedDate.addZero(this.currentHours,2);
    this.ctlHours.select();
  }if (part=="Min")
  {
    this.ctlMinutes.value = this.selectedDate.addZero(this.currentMinutes,2);
    this.ctlMinutes.select();
  }
  if (part=="Sec")
  {
    this.ctlSeconds.value = this.selectedDate.addZero(this.currentSeconds,2);
    this.ctlSeconds.select();
  }
  return true;
}

// Setup time spin buttons for hours/minutes/seconds
function datetimeControl_setupTimeSpin(index)
{
  var timespinControl = eval(this.name+"_spin_time");
  if (index==0)
  {
    timespinControl.setAttribute("for", this.name+"_hours");
    timespinControl.setAttribute("min", 0);
    timespinControl.setAttribute("max", 23);
    timespinControl.setAttribute("value", this.currentHours);
  }else if (index==1)
  {
    timespinControl.setAttribute("for", this.name+"_minutes");
    timespinControl.setAttribute("min", 0);
    timespinControl.setAttribute("max", 59);
    timespinControl.setAttribute("value", this.currentMinutes);
  }else
  {
    timespinControl.setAttribute("for", this.name+"_seconds");
    timespinControl.setAttribute("min", 0);
    timespinControl.setAttribute("max", 59);
    timespinControl.setAttribute("value", this.currentSeconds);
  }
  createSpinControl(timespinControl);
}

// Set field value to the date selected and close the DateTime Control window
function datetimeControl_returnDate(inDay)
{
  var date = new Date(parseInt(this.currentYear), parseInt(this.currentMonth), 1, parseInt(this.currentHours), parseInt(this.currentMinutes), parseInt(this.currentSeconds));
  date.setDate(inDay);

  if (date) date = date.checkDateRange();

  var outDate = date.formatDate(date, (new Date()).parseDateFormat(this.format));

  this.control.value = String(outDate).replace(/\s*$/, "");    

  if (this.control.type!="hidden") this.control.focus();

  if (!this.inline)
    this.divWindow.style.display = "none";
  else
    this.show();
}
