// CalendarEvent
function EventDay(idDay, idMonth, idYear, idProduct, qtProduct)
{
    this.idDay = idDay;
    this.idMonth = idMonth;
    this.idYear = idYear;  
    this.idProduct = idProduct; 
    this.qtProduct = qtProduct;
}

function CalendarEvent(calendarHeaderId, calendarContentId, idActDay, idActMonth, idActYear)
{
	this.eventDaysList = new Array(365);
	this.eventDays = 0;
	this.calendarHeaderId = calendarHeaderId;
    this.calendarContentId = calendarContentId;
    this.idMonth = null;
    this.idYear = null;
    this.idActDay = idActDay; 
    this.idActMonth = idActMonth; 
    this.idActYear = idActYear;
}

CalendarEvent.prototype.add = function(idDay, idMonth, idYear, idProduct, qtProduct)
{
	this.eventDaysList[this.eventDays++] = new EventDay(idDay, idMonth, idYear, idProduct, qtProduct);
}

CalendarEvent.prototype.nextMonth = function()
{
	this.idMonth++;
	if (this.idMonth > 11)
	{
		this.idMonth = 0;
		this.idYear++;
	}
	this.render();
}

CalendarEvent.prototype.prevMonth = function()
{
	this.idMonth--;
	if (this.idMonth < 0)
	{
		this.idMonth = 11;
		this.idYear--;
	}
	this.render();
}

CalendarEvent.prototype.render = function()
{
	var content = "";
	var calendarContentObj = getById(this.calendarContentId);
	if (calendarContentObj == null)
		return;

	var calendarHeaderObj = getById(this.calendarHeaderId);

	// Reset the content
	calendarContentObj.innerHTML = "";

	// Now
	var date = new Date();

	// Setup the first date
	if (this.idMonth == null || this.idYear == null)
	{
		if (this.eventDays > 0)
		{
//			eventDay = this.eventDaysList[0];
			this.idMonth = this.idActMonth;
			this.idYear = this.idActYear;
		}
		else
		{
			this.idMonth = date.getMonth();
			this.idYear = date.getUTCFullYear();
		}
	}

	// Set the first day of the month
	date.setDate(1);
	date.setMonth(this.idMonth);
	date.setYear(this.idYear);
	
	// Fill the header
	if (calendarHeaderObj != null)
		calendarHeaderObj.innerHTML = labels.get("idMonth_" + this.idMonth) + " " + this.idYear;  

	// Fill initial the gap of the week
	var dow = date.getDay() - 1;
	if (dow < 0)
		dow = 6;
	date.setDate(-dow + 1);

	// Fill the calendar
	for (i = 0; i < (7 * 6); i++)
	{
		foundProduct = false;
		actDay = date.getDate();
		actMonth = date.getMonth();
		actYear = date.getUTCFullYear();
		
		for (e = 0; e < this.eventDays; e++)
		{
			eventDay = this.eventDaysList[e];
			
			if (eventDay.idDay == actDay &&
				eventDay.idMonth == actMonth &&
				eventDay.idYear == actYear)
			{
				foundProduct = true;
				break;
			}
		} 
		
		if (foundProduct)
		{
			if (this.idActDay == eventDay.idDay &&
				this.idActMonth == eventDay.idMonth && 
				this.idActYear == eventDay.idYear) 
				content += "<div class=\"cellDAYSel\">";
			else
				content += "<div class=\"cellDAYAva\">";			
		}
		else  if (date.getMonth() == this.idMonth)
			content += "<div class=\"cellDAY\">";
		else
			content += "<div class=\"cellDAYG\">";
		if (foundProduct)
			content += "<a href=\"javascript:chooseEventDate(" + eventDay.idProduct + ");\">" + date.getDate() + "</a>";
		else
			content += date.getDate();
		content += "</div>";
		date.setDate(date.getDate() + 1);
	}

	calendarContentObj.innerHTML = content;
}


function chooseEventDate(idProduct)
{
	var obj = getById("idProduct");
	for (i = 0; i < obj.options.length; i++)
	{
		if (idProduct == obj.options[i].value)
		{
			obj.selectedIndex = i;
			obj.form.submit();
			break;
		}
	}
//	alert(obj.options.length);
//	alert(obj.options.length);
/*
function fn(ele) {
  var selected = ele.options[ele.selectedIndex].value;
  alert(selected)
}
*/
}
