/*
Calendar Script
dollars.is - azizul hakimi bin ahmad - 2009
*/

/*
simple structure
<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
	<th>S</th>
	<th>M</th>
	<th>T</th>
	<th>W</th>
	<th>T</th>
	<th>F</th>
	<th>S</th>
</tr>
</thead>
<tbody>
<tr>
	<td class="first-col"><a href="#">1</a></td>
	<td><a href="#" class="selected">2</a></td>
	<td><a href="#" class="today">3</a></td>
	<td><a href="#">4</a></td>
	<td><a href="#">5</a></td>
	<td><a href="#">6</a></td>
	<td><a href="#">7</a></td>
</tr>
</tbody>
</table>
*/

function showPrevCal(_containerid, _monthid, _yearid, _targetid)
{	
	var _monthContainer = document.getElementById(_monthid);
	var _yearContainer = document.getElementById(_yearid);
	_month = _monthContainer.options[_monthContainer.selectedIndex].value;
	_year = _yearContainer.options[_yearContainer.selectedIndex].value;
	var _calCurrentDate = new Date();
	_calCurrentDate.setFullYear(_calCurrentDate.getFullYear(),_calCurrentDate.getMonth(),1,0,0,0);
	
	_month--;
	if(_month  < 0) 
	{	
		_month = 11;
		_year--;
	}
	
	var _calNewDate = new Date();
	_calNewDate.setFullYear(_year,_month,1,0,0,0);	
		
	if(_calCurrentDate <= _calNewDate)
	{
		renderCalendar(_containerid, _month, _year, _targetid);	
		_monthContainer.selectedIndex = _month;
		if(_month == 11)
			_yearContainer.selectedIndex--;
	}
	return false;
}

function showNextCal(_containerid, _monthid, _yearid, _targetid)
{	
	var _monthContainer = document.getElementById(_monthid);
	var _yearContainer = document.getElementById(_yearid);
	_month = _monthContainer.options[_monthContainer.selectedIndex].value;
	_year = _yearContainer.options[_yearContainer.selectedIndex].value;
	_month++;
	if(_month > 11) 
	{	
		_month = 0;
		_year++;
	}
	
	renderCalendar(_containerid, _month, _year, _targetid);	
	_monthContainer.selectedIndex = _month;
	if(_month == 0)
		_yearContainer.selectedIndex++;	
	return false;
}

function showCal(_containerid, _monthid, _yearid, _targetid, _activedate)
{	
	var _monthContainer = document.getElementById(_monthid);
	var _yearContainer = document.getElementById(_yearid);
	var _month = _monthContainer.options[_monthContainer.selectedIndex].value;
	var _year = _yearContainer.options[_yearContainer.selectedIndex].value;
	renderCalendar(_containerid, _month, _year, _targetid, _activedate);		
	return false;
}

function renderCalendar(_containerid,_month,_year, _targetid, _activedate)
{	
	var _currentDate = new Date();
	
	var _container = document.getElementById(_containerid);	
	var _calCurrentDate = new Date();
	_calCurrentDate.setFullYear(_year,_month,1);
	var _next_month = parseInt(_month) + 1;	
	var _next_year = parseInt(_year);	
	if(_next_month  > 11) 
	{	
		_next_month = 0;
		_next_year++;
	}
	var _calNextMonth = new Date();
	_calNextMonth.setFullYear(_next_year,_next_month,1);		
	
	var _calendar = "";
	var _day = 0;
	var _row = 0;
	
	while(_calCurrentDate < 	_calNextMonth)
	{
		_day++;
		if(_calendar == "")
		{
			
			//fill in the starting empty cols
			_calendar += "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n<thead><tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr></thead><tbody><tr>\n";
			_row++;
			for(var i=0; i<_calCurrentDate.getDay();i++)
			{
				if(i==0)
				{
					_calendar += "<td class=\"first-col\">&nbsp;</td>";
				}
				else
				{
					_calendar += "<td>&nbsp;</td>\n";
				}
			}

			if(_calCurrentDate.getDay() == 0)
			{
					_calendar += "<td class=\"first-col\">";
			}
			else
			{
				_calendar += "<td>";
			}			
		}
		else
		{
			if(_calCurrentDate.getDay() == 0)
			{
				_row++;
				_calendar += "</tr>\n<tr>\n<td class=\"first-col\">";
			}
			else
			{
				_calendar += "<td>";
			}
		}
		
		if(_calCurrentDate >= _currentDate)
		{
			var _selected = "";
			if(_activedate!= null)
			{
				if(parseInt(_day) == parseInt(_activedate.getDate()))
					_selected = " class=\"selected\" ";				
			}
			
			_calendar += "<a href=\"#\" id=\"" + _containerid + "cal_day_" + _day + "\" onclick=\"calUpdateTarget('" + _targetid + "','" + _day + "." + _month + "." + _year +"','" + _containerid + "cal_day_" + _day + "','" + _containerid + "'); return false;\"" + _selected + ">" + _day + "</a>";
			
		}
		else
		{
			_calendar += "<a href=\"#\" onclick=\"return false;\" style=\"color:#aaa;cursor:default;\">" + _day + "</a>";
		}
		_calendar += "</td>\n";
		
		_calCurrentDate.setDate(_calCurrentDate.getDate()+1);
	}
	
	while(_calCurrentDate.getDay() != 0)
	{
		_calendar += "<td>&nbsp;</td>\n";
		_calCurrentDate.setDate(_calCurrentDate.getDate()+1);
	}
	_calendar += "</tr>";
	
	while(_row < 6)
	{
		_calendar += "<tr><td class=\"first-col\"><a href=\"#\">&nbsp;</a></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
		_row++;
	}
	_calendar += "</tbody></table>";
	
	_container.innerHTML = _calendar;
	return false;
}

function calUpdateTarget(_targetid, _value, _caldayid, _prefix)
{	
	document.getElementById(_targetid).value = _value;
		
	for(var i=1; i<=31; i++)
	{	
		if(_prefix + "cal_day_" + i == _caldayid)
		{
			document.getElementById(_prefix + "cal_day_" + i ).className = 'selected';
		}
		else
		{
			if(document.getElementById(_prefix + "cal_day_" + i ))
				document.getElementById(_prefix + "cal_day_" + i ).className = '';
		}
	}
	calculate();
	return false;
}
