var jsCalendar = {
	'variable': {
		'month': [
			'January',
			'February',
			'March',
			'April',
			'May',
			'June',
			'July',
			'August',
			'September',
			'October',
			'November',
			'December'
		],
		'dayFull': [
			'Sunday',
			'Monday',
			'Tuesday',
			'Wednesday',
			'Thursday',
			'Friday',
			'Saturday'
		],
		'day': [
			'Su',
			'Mo',
			'Tu',
			'We',
			'Th',
			'Fr',
			'Sa'
		]
	},
	'el': {
		'cal': document.createElement('table'),
		'input': null
	},
	'set': function (el) {
		if (jsCalendar.el.cal.style.display == 'none' || el != jsCalendar.el.input) {
			jsCalendar.popup(el);
		} else {
			jsCalendar.el.cal.style.display = 'none';
		}
	},
	'popup': function (el) {
		jsCalendar.el.input = el;
		jsCalendar.build(el.value);
		el.parentNode.appendChild(jsCalendar.el.cal);
		jsCalendar.el.cal.style.display = '';
	},
	'build': function (d) {
		var tr = document.createElement('tr');
		var td = document.createElement('td');
		var a = document.createElement('a');
		var d = (jsCalendar.isDate(d)?new Date(d):new Date());
		var tbody = jsCalendar.el.cal.appendChild(document.createElement('tbody'));
		jsCalendar.el.cal.setAttribute('class','event-content-calendar event-content-calendar-popup');
		jsCalendar.el.cal.setAttribute('className','event-content-calendar event-content-calendar-popup');
		jsCalendar.el.cal.style.width = '215px';
		//jsCalendar.el.cal.innerHTML = '';
		while (jsCalendar.el.cal.hasChildNodes()) {
			jsCalendar.el.cal.removeChild(jsCalendar.el.cal.firstChild);
		};
		jsCalendar.el.cal.appendChild(tbody);
		tr.setAttribute('class','event-content-calendar-header');
		tr.setAttribute('className','event-content-calendar-header');
		a.innerHTML = '&laquo;';
		a.setAttribute('href','#');
		a.onclick = function() {
			jsCalendar.build(d.setFullYear(d.getFullYear(),d.getMonth()-1,1));
			return false;
		};
		td.setAttribute('class','event-content-calendar-header-left');
		td.setAttribute('className','event-content-calendar-header-left');
		td.appendChild(a);
		tr.appendChild(td);
		td = document.createElement('td');
		td.setAttribute('colSpan',jsCalendar.variable.day.length-3);
		td.setAttribute('class','event-content-calendar-header-middle');
		td.setAttribute('className','event-content-calendar-header-middle');
		td.appendChild(document.createTextNode(jsCalendar.variable.month[d.getMonth()] + ' ' + d.getFullYear()));
		tr.appendChild(td);
		td = document.createElement('td');
		a = document.createElement('a');
		a.innerHTML = '&raquo;';
		a.setAttribute('href','#');
		a.onclick = function() {
			jsCalendar.build(d.setFullYear(d.getFullYear(),d.getMonth()+1,1));
			return false;
		};
		td.setAttribute('class','event-content-calendar-header-right');
		td.setAttribute('className','event-content-calendar-header-right');
		td.appendChild(a);
		tr.appendChild(td);
		td = document.createElement('td');
		a = document.createElement('a');
		var img = document.createElement('img');
		img.setAttribute('src','/icon/cross2-white.png');
		img.setAttribute('border',0);
		img.setAttribute('width',12);
		img.setAttribute('height',12);
		img.style.margin = '3px 3px 3px 0';
		img.style.padding = '1px';
		td.style.padding = '0';
		a.setAttribute('href','#');
		a.onclick = function() {
			jsCalendar.el.cal.style.display = 'none';
			return false;
		};
		td.setAttribute('class','event-content-calendar-header-right');
		td.setAttribute('className','event-content-calendar-header-right');
		a.appendChild(img);
		td.appendChild(a);
		tr.appendChild(td);
		tbody.appendChild(tr);
		// Row 2 (Su Mo Tu ... Sa)
		tr = document.createElement('tr');
		tr.setAttribute('class','event-content-calendar-weekday');
		tr.setAttribute('className','event-content-calendar-weekday');
		for (var i=0;i<jsCalendar.variable.day.length;i++) {
			td = document.createElement('td');
			td.appendChild(document.createTextNode(jsCalendar.variable.day[i]));
			tr.appendChild(td);
		}
		tbody.appendChild(tr);
		//alert(jsCalendar.variable.month[d.getMonth()]);
		// Row 3-N (Calendar Days)
		var d2 = new Date(d.getFullYear(),d.getMonth(),1);
		var d3 = new Date(d2.getFullYear(),d2.getMonth(),d2.getDate()-d2.getDay());
		for (var i=0;i<6;i++) {
			tr = document.createElement('tr');
			tr.setAttribute('class','event-content-calendar-date');
			tr.setAttribute('className','event-content-calendar-date');
			for (var j=0;j<jsCalendar.variable.day.length;j++) {
				var d4 = new Date(d3.getFullYear(),d3.getMonth(),d3.getDate()+j+(i*7));
				td = document.createElement('td');
				//thalert((new Date())-d4);
				if (d4.getMonth() != d2.getMonth()) {
					td.setAttribute('class','shadow');
					td.setAttribute('className','shadow');
				} else if (d4.getDate() == d.getDate() && d4.getMonth() == d.getMonth()) {
					if (d4.getDate() == new Date().getDate() && d4.getMonth() == new Date().getMonth()) {
						td.setAttribute('class','today highlight');
						td.setAttribute('className','today highlight');
					} else {
						td.setAttribute('class','highlight');
						td.setAttribute('className','highlight');
					}
				} else if (d4.getDate() == new Date().getDate() && d4.getMonth() == new Date().getMonth()) {
					td.setAttribute('className','today');
				} 
				a = document.createElement('a');
				a.appendChild(document.createTextNode(d4.getDate()));
				a.setAttribute('date',d4.getMonth()+1 + '/' + (d4.getDate()) + '/' + d4.getFullYear())
				a.setAttribute('href','#');
				a.onclick = function(e) {
					var el = this;
					jsCalendar.el.input.value = el.getAttribute('date');
					jsCalendar.el.cal.style.display = 'none';
					return false;
				};
				td.appendChild(a);
				tr.appendChild(td);
			}
			tbody.appendChild(tr);
		}
	},
	'isDate': function (d) {
		return (!isNaN(new Date (d).getYear()));
	}
};
