﻿function daysInMonth(month, year)
{
	if (month == 1)
		return 31;
	else if (month == 2)
	{
		if ((year % 4) == 0)
			return 29;
		else
			return 28;
	}
	else if (month == 3)
		return 31;
	else if (month == 4)
		return 30;
	else if (month == 5)
		return 31;
	else if (month == 6)
		return 30;
	else if (month == 7)
		return 31;
	else if (month == 8)
		return 31;
	else if (month == 9)
		return 30;
	else if (month == 10)
		return 31;
	else if (month == 11)
		return 30;
	else if (month == 12)
		return 31;
	else
		return 0;
}

function monthToInt(month)
{
	month = month.toLowerCase();
	if (month == 'jan')
		return 1;
	else if (month == 'feb')
		return 2;
	else if (month == 'mar')
		return 3;
	else if (month == 'apr')
		return 4;
	else if (month == 'may')
		return 5;
	else if (month == 'jun')
		return 6;
	else if (month == 'jul')
		return 7;
	else if (month == 'aug')
		return 8;
	else if (month == 'sep')
		return 9;
	else if (month == 'oct')
		return 10;
	else if (month == 'nov')
		return 11;
	else if (month == 'dec')
		return 12;
	else
		return 0;
}

function intToMonth(month)
{
	if (month == 1)
	  return 'Jan';
	else if (month == 2)
	  return 'Feb';
	else if (month == 3)
		return 'Mar';
	else if (month == 4)
		return 'Apr';
	else if (month == 5)
		return 'May';
	else if (month == 6)
		return 'Jun';
	else if (month == 7)
		return 'Jul';
	else if (month == 8)
		return 'Aug';
	else if (month == 9)
		return 'Sep';
	else if (month == 10)
		return 'Oct';
	else if (month == 11)
		return 'Nov';
	else if (month == 12)
		return 'Dec';
	else
		return 0;
}

function populateCalendar(outputDate)
{
	var calendarOutput = new String('<table cellpadding="0" cellspacing="0" border="0" class="miniCalendar">');
	calendarOutput += '<tr><td class="miniCalHead">Mo</td><td class="miniCalHead">Tu</td><td class="miniCalHead">We</td>' +
	  '<td class="miniCalHead">Th</td><td class="miniCalHead">Fr</td><td class="miniCalHead">Sa</td><td class="miniCalHead">Su</td></tr>';
	
	while (curCalDate >= outputDate)
	{
	  calendarOutput += '<tr>';
	  var i = 0;
	  while (i < 7)
	  {
	    calendarOutput += '<td class="miniCal">';
	    if (curCalDate.getMonth() == outputDate.getMonth())
	      calendarOutput += '<a href="javascript:returnCalVal(\'' + outputDate.getDate() + '/' + (outputDate.getMonth() + 1) + '/' + outputDate.getFullYear() + '\');">' + outputDate.getDate() + '</a>';
	    else
	      calendarOutput += '&nbsp;';
	    	    
   	  calendarOutput += '</td>';

	    i++;
	    outputDate.setDate(outputDate.getDate() + 1);
	  }
	  calendarOutput += '</tr>';
	}
	
	calendarOutput += '</table>';
	return calendarOutput;
}

function monthOption(month, outputMonth)
{
  var output = new String('<option');
  if (curCalDate.getMonth() + 1 == outputMonth)
    output += ' selected';
  output += '>' + month + '</option>';
  
  return output;
}

function generateContent(displayObj, returnObj)
{
  displayObj.style.display = 'block';

	var outputDate = new Date();
	outputDate.setFullYear(outputDate.getFullYear(), outputDate.getMonth(), 1);
  
	// set date to already selected date if it exists
	if (returnObj.value.length > 0)
	{
		// Expect date to be in the format of d-mm-yyyy
		var tmp = returnObj.value.split('/');
		if (tmp.length == 3)
			outputDate.setFullYear(tmp[2], monthToInt(tmp[1]) - 1, 1);		
	}
	else
		outputDate.setFullYear(outputDate.getFullYear() - 1, outputDate.getMonth(), 1);

	curCalDate = new Date(outputDate);
	curCalDate.setDate(curCalDate.getDate() + (daysInMonth(curCalDate.getMonth() + 1, curCalDate.getFullYear()) - 1));

	// adjust the date to the monday of the week the 1st of the month is
	if (outputDate.getDay() == 0)
		outputDate.setDate(outputDate.getDate() - 6);
	else if (outputDate.getDay() > 1)
		outputDate.setDate(outputDate.getDate() - outputDate.getDay() + 1);

  var output = new String('');
  output += '<table cellpadding="0" cellspacing="0" border="0" class="miniCalendarHeader"><tr>';
  output += '<td class="calMonth"><select class="calMonth" onchange="gotoMonth(this)">';

  output += monthOption('January', 1);
  output += monthOption('February', 2);
  output += monthOption('March', 3);
  output += monthOption('April', 4);
  output += monthOption('May', 5);
  output += monthOption('June', 6);
  output += monthOption('July', 7);
  output += monthOption('August', 8);
  output += monthOption('September', 9);
  output += monthOption('October', 10);
  output += monthOption('November', 11);
  output += monthOption('December', 12);
  output += '</select></td>';
  
  var year = (new Date()).getFullYear();
  year = year;
  output += '<td class="calYear"><select class="calYear" onchange="gotoYear(this)">';

  var cntr = 100;
  while (cntr-- > 1)
  {   
    output += '<option';
    if (curCalDate.getFullYear() == (year - cntr))
      output += ' selected';
    output += '>';
    
    output += (year - cntr) + '</option>';
  }
  output += '</select></td>';
  
  output += '<td class="calClose"><a href="javascript:closeCal()"><img src="close.jpg" /></a></td>';
  
  output += '</tr></table>';
  
  output += '<div id="calendar">';
  output += populateCalendar(outputDate);
  output += '</div>';
		
	calendarObj = displayObj;
	textBoxObj = returnObj;
	displayObj.innerHTML = output;
}

var calendarObj = null;
var textBoxObj = null;
var curCalDate = null;

function closeCal()
{
  calendarObj.style.display = 'none';
  calendarObj.innerHTML = '';
}

function returnCalVal(value)
{
  textBoxObj.value = value;
  closeCal();
}

function gotoMonth(cmbMonth)
{
  curCalDate.setFullYear(curCalDate.getFullYear(), cmbMonth.selectedIndex, daysInMonth(cmbMonth.selectedIndex + 1, curCalDate.getFullYear()));

	// adjust the date to the monday of the week the 1st of the month is
	var outputDate = new Date(curCalDate);
	outputDate.setFullYear(curCalDate.getFullYear(), curCalDate.getMonth(), 1);

	if (outputDate.getDay() == 0)
		outputDate.setDate(outputDate.getDate() - 6);
	else if (outputDate.getDay() > 1)
		outputDate.setDate(outputDate.getDate() - outputDate.getDay() + 1);
		
  document.getElementById('calendar').innerHTML = populateCalendar(outputDate);
}

function gotoYear(cmbYear)
{
  curCalDate.setFullYear(cmbYear.options[cmbYear.selectedIndex].text, curCalDate.getMonth(), daysInMonth(curCalDate.getMonth() + 1, cmbYear.options[cmbYear.selectedIndex].text));

	// adjust the date to the monday of the week the 1st of the month is
	var outputDate = new Date(curCalDate);
	outputDate.setFullYear(curCalDate.getFullYear(), curCalDate.getMonth(), 1);

	if (outputDate.getDay() == 0)
		outputDate.setDate(outputDate.getDate() - 6);
	else if (outputDate.getDay() > 1)
		outputDate.setDate(outputDate.getDate() - outputDate.getDay() + 1);
		
  document.getElementById('calendar').innerHTML = populateCalendar(outputDate);
}