u.calendar = function(window, sCalendarID, sCMPBase, CatID, sURL) {
	this.w = window;
	this.d = window.document;
	this.cal = u.el.gel(sCalendarID,this.d);
	if (!this.cal) return;
	this.CMPBase = sCMPBase;
	this.catID = CatID;
	this.url = sURL.replace(/\&amp\;/gi, "&");
	
	this.aryDates = new Array('6/24/2011','6/25/2011');

	var dt = new Date();
	this.year = dt.getFullYear();
	this.month = dt.getMonth() + 1;
	this.day = dt.getDate();
	this.today = new Date(this.year, this.month - 1, this.day, 0, 0, 0, 0);
	this.curmonth = 0;
	this.curyear = 0;

	this.days = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	this.months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	this.monthdays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	this.years = [];
	var y = this.today.getFullYear() + 1;
	for (var i = y - 61; i <= y; ++i) this.years[this.years.length] = i;

	this.tbl = u.el.create("table", { tbl: true, cls: "caltable" }, this.d, this.cal);
	var tr = u.el.create("tr", {}, this.d, this.tbl);
	var td = u.el.create("td", { colSpan: "7" }, this.d, tr);
	this.mklink("calnow", this.setToday, "Click here to set today's date", "Today", td);
	this.mklink("calprev", this.setPrev, "Click here to go to the previous month", "&lt;&lt;", td);
	this.monthselect = this.mkselect(this.months, null, { cls: "calmonths", "onchange": { e: this.chgMonth, b: this} }, td);
	this.yearselect = this.mkselect(this.years, null, { cls: "calyears", "onchange": { e: this.chgYear, b: this} }, td);
	this.mklink("calnext", this.setNext, "Click here to go to the next month", "&gt;&gt;", td);
	this.mklink("calclose", this.hide, "Click here to close the calendar", "X", td);

	var tr = u.el.create("tr", {}, this.d, this.tbl);
	for (i = 0; i < 7; ++i) u.el.create("td", { cls: "calhdr", inner: this.days[i] }, this.d, tr);

	this.setVars(this.today);
};
u.calendar.prototype = {
	fmt: function(dt) {
		return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
	},
	find: function(dt) {
		var sDate = this.fmt(dt);
		for (var i = 0; i < this.aryDates.length; i++) {
			if (this.aryDates[i] == sDate) return true;
		}
		return false;
	},
	mklink: function(sClass, fn, sTitle, sHTML, parent) {
		return u.el.create("div", { cls: sClass, onclick: { e: fn, b: this }, title: sTitle, inner: sHTML }, this.d, parent);
	},
	mkselect: function(ary, selection, options, parent) {
		var sel = u.el.create("select", options, this.d, parent);
		for (var i = 0; i < ary.length; ++i) {
			var opt = new Option();
			opt.value = opt.text = ary[i];
			opt.selected = (opt.value == selection);
			sel.options[i] = opt;
		}
		return sel;
	},
	select: function(osel, selection) {
		for (var i = 0, opts = osel.options; i < opts.length; ++i) opts[i].selected = (opts[i].value == selection);
	},
	getDates: function(dt) {
		var sDates = u.xhr.raw(this.CMPBase + "Scripts/CalendarDates.asp?v=" + this.fmt(dt) + "&c=" + this.catID);
		var ary = sDates.split("<Row>");
		var aryD = [];
		var j;
		this.aryDates = [];
		for (var i = 1; i < ary.length; i++) {
			j = ary[i].indexOf("</Row>");
			aryD = ary[i].substr(0,j).split("^;");
			this.aryDates[i-1] = aryD[0];
		}
	},
	setVars: function(dt) {
		this.year = dt.getFullYear();
		this.month = dt.getMonth() + 1;
		this.day = dt.getDate();
		if (this.curmonth != this.month || this.curyear != this.year) {
			this.curmonth = this.month;
			this.curyear = this.year;
			this.getDates(dt);
		}
		this.draw();
	},
	setPrev: function(e) {
		this.day = 1;
		this.month -= 1;
		if (this.month == 0) {
			this.month = 12;
			this.year -= 1;
		}
		var dt = new Date(this.year, this.month - 1, this.day, 0, 0, 0, 0);
		this.setVars(dt);
	},
	setNext: function(e) {
		this.day = 1;
		this.month += 1;
		if (this.month == 13) {
			this.month = 1;
			this.year += 1;
		}
		var dt = new Date(this.year, this.month - 1, this.day, 0, 0, 0, 0);
		this.setVars(dt);
	},
	setToday: function(e) {
		//this.setVars(this.today);
		this.clkDay(e);
	},
	clkDay: function(e) {
		var el = u.evt.el(e);
		this.day = u.mknum(u.el.getAttr(el, "day"));
		var sDate = this.month + "/" + this.day + "/" + this.year;
		this.d.location = this.url + sDate;
	},
	chgMonth: function(e) {
		this.month = this.monthselect.selectedIndex + 1;
		this.draw();
	},
	chgYear: function(e) {
		this.year = this.yearselect.options[this.yearselect.selectedIndex].value;
		this.draw();
	},
	draw: function() {
		this.max = this.monthdays[this.month - 1];
		if (this.month == 2 && this.year % 4 == 0 && (this.year % 100 != 0 || this.year % 400 == 0)) this.max = 29;
		var dt = new Date(this.year, this.month - 1, 1, 0, 0, 0, 0);

		var i;
		for (i = 2; i < this.tbl.childNodes.length; ) u.el.rmv(this.tbl.childNodes[i]);

		this.select(this.monthselect, this.months[this.month - 1]);
		this.select(this.yearselect, this.year);

		i = 1;
		var j, dow, c;
		while (i <= this.max) {
			var tr = u.el.create("tr", {}, this.d, this.tbl);
			dt.setDate(i);
			dow = dt.getDay();
			for (j = 0; j < 7; ++j) {
				if (dow > 0 || i > this.max) {
					u.el.create("td", { cls: "calempty", inner: "&nbsp;" }, this.d, tr);
					--dow;
				} else {
					c = this.find(dt) ? "calday" : "calempty";
					if (dt.toString() == this.today.toString()) c += " caltoday";
					var td = u.el.create("td", { day: i, cls: c, onclick: {e: this.clkDay, b: this}, title: "Click here to select this day", inner: i }, this.d, tr);
					dt.setDate(++i);
				}
			}
		}
	}
};

