/*
 * Trevor Hayes
 * Shopcity.com
 * June 27, 2008
 */

// Global variables
var Today = new Date();
var WeekDays = new Array('Sun','Mon','Tues','Wed','Thur','Fri','Sat');
var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var MonthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

function Calendar(id, limit)
{
	/*this.element = document.getElementById(id);
	this.head = this.element.getElementById('head');
	this.grid = this.element.getElementsByTagName('td');
	this.form_elements = this.element.getElementById('form_elements');*/
	
	// Moo
	this.element = $(id);
	//this.head = this.element.getElementById('head');
	this.head = this.element.getElement('th[id=head]');
	this.grid = this.element.getElements('td');
	//this.form_elements = this.element.getElementById('form_elements');
	this.form_elements = this.element.getElement('span[id=form_elements]');
	
	this.month = Today.getMonth();
	this.year = Today.getFullYear();
	
	this.datelist = new DateList(limit);
	this.markSet = false;
	
	this.update();
}

Calendar.prototype.setName = function(str)
{
	this.name = str;
}

Calendar.prototype.mouseOver = function(e)
{
	if(window.ie)
	{
		$(e).addClass('hover');
	}
}

Calendar.prototype.mouseOut = function(e)
{
	
	if(window.ie)
	{
		$(e).removeClass('hover');
	}
}

Calendar.prototype.getFirstDay = function()
{
	var d = new Date(this.year, this.month, 1);
	return d.getDay();
}

Calendar.prototype.toggle = function(e)
{
	if(e.day != null)
	{
		if(!this.datelist.isSet(this.month, e.day, this.year))
		{
			if(this.datelist.add(this.month, e.day, this.year))
			{
				e.className = "selected";
			}
		}
		else
		{
			this.datelist.remove(this.month, e.day, this.year);
			e.className = "";
		}
	}
}

Calendar.prototype.preSubmit = function()
{
	var e = this.form_elements;
	var i = 1;
	
	// Clear any elements in the area.
	e.innerHTML = "";
	this.datelist.forSelected(function(m, d, y){
		// It is important to convert the month into a non-zero based form.
		e.innerHTML += "<input name=\"date" + ((i < 10) ? "0"+i : i) + "\" type=\"hidden\" value=\"" + ((parseInt(m)+1) + "/" + d + "/" +y) + "\">";
		i++;
	});
	
	for(; i <= 10; i++)
	{
		e.innerHTML += "<input name=\"date" + ((i < 10) ? "0"+i : i) + "\" type=\"hidden\" value=\"\">";
	}
}

Calendar.prototype.markDate = function(mmddyyyy)
{
	if(mmddyyyy != "")
	{
		var section = mmddyyyy.split("/");
		this.datelist.add(parseInt(section[0])-1, section[1], section[2]);
		
		// Show the first cronological date.
		if(this.markSet)
		{
			if(section[0]-1 < this.month)
			{
				this.month = section[0]-1;
			}
			
			if(section[2] < this.year)
			{
				this.year = section[2];
				this.month = section[0]-1;
			}
		}
		else
		{
			this.markSet = true;
			this.month = section[0]-1;
			this.year = section[2];
		}
	}
}

Calendar.prototype.update = function()
{
	// Update the header
	this.head.innerHTML = MonthNames[this.month] + " " + this.year; 
	
	var first_square = this.getFirstDay()+7;
	var days = this.getDayCount();
	var end = this.grid.length;
	
	// Clear the start of the calendar
	for(var i = 7; i < first_square; i++)
	{
		this.grid[i].className = "empty";
		this.grid[i].innerHTML = "&nbsp;";
		this.grid[i].day = null;
	}
	
	// Number the days in the month
	for(var day = 1; day <= days; day++)
	{
		if(this.datelist.isSet(this.month, day, this.year))
		{
			this.grid[i].className = "selected";
		}
		else
		{
			this.grid[i].className = "";
		}
		
		this.grid[i].innerHTML = day;
		this.grid[i].day = day;
		i++;
	}
	
	// Clear the end of the calendar
	for(; i < end; i++)
	{
		this.grid[i].className = "empty";
		this.grid[i].innerHTML = "&nbsp;";
		this.grid[i].day = null;
	}
}

Calendar.prototype.nextMonth = function()
{
	this.month++;
	if(this.month > 11)
	{
		this.month = 0;
		this.year++;
	}
	this.update();
}

Calendar.prototype.prevMonth = function()
{
	this.month--;
	if(this.month < 0)
	{
		this.month = 11;
		this.year--;
	}
	this.update();
}

// Returns the number of days in a month (handles leap-years)
Calendar.prototype.getDayCount = function()
{
   return ((this.month == 1) && ((this.year % 400 == 0) || ((this.year % 4 == 0) && (this.year % 100 != 0)))) ? 29 : MonthDays[this.month];
}