var dayName = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var monthName = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var calendars = new Object();
var timebars = new Object();
var activeTimeBar = null;
var overCal = false;
var overBar = false;
var lastField = '';
var lastStartDate = '';

function Calendar() {
	
	this.id = 'cal-' + Math.round(Math.random() * 100000).toString();
	calendars[this.id] = this;
	this.dates = new Object();
	
	this.currentDate = new Date();	
	
	this.setYear = function(year) {
		this.currentDate.setFullYear(year);
	}
	this.getYear = function() {
		return this.currentDate.getFullYear();
	}
	this.setMonth = function(month) {
		this.currentDate.setMonth(month);
	}
	this.getMonth = function() {
		return this.currentDate.getMonth();
	}
	this.setDate = function(d) {
		this.currentDate.setDate(d);
	}
	
	this.type = 'display';
	this.target = null;
	
	this.set_as_selector = function(textbox) {
		this.type = 'selector';
		this.target = textbox;
	}
	
	this.small_display = function() {
		var str = '';
		str += '<table cellpadding="0" cellspacing="0" width="140" class="calendarSmall" id="' + this.id + '">';
		str += '<tr><td>';
		str += '<table width="100%"><tr>';
		str += '<td width="10%" onclick="calendars[\''+this.id+'\'].small_previous_month();" class="smallArrow">&lt;&lt;</td>';
		str += '<td width="80%" align="center" id="cal-' + this.id + '-month"></td>';
		str += '<td width="10%" onclick="calendars[\''+this.id+'\'].small_next_month();" class="smallArrow">&gt;&gt;</td>';
		str += '</tr></table></td></tr>';
		str += '<tr><td><table width="100%" class="calendarDayNames">';
		str += '<tr><td width="20">S</td><td width="20">M</td><td width="20">T</td><td width="20">W</td><td width="20">T</td><td width="20">F</td><td width="20">S</td></tr></table></td></tr>';
		str += '<tr><td><table width="100%" class="calendarDates" id="cal-' + this.id + '-dates">';
		str +='</table></td></tr></table>';
		
		return(str);
	}
	
	this.small_set_dates = function() {
		if (this.target != null && this.type == 'selector') {
			var dates = this.target.value.split(',');
			this.dates = new Object();
			for (var i = 0; i < dates.length; i++) {
				var date = trim(dates[i]);
				if (date.match(/^\d\d?\/\d\d?\/\d{4}$/)) {
					this.dates[date] = true;
				}
			}
		}
	}
	
	this.small_highlight_dates = function() {
		for (var i in this.dates) {
			if ($('date-' + i) != null) {
				$('date-' + i).className = 'smallActiveDate';
			}
		}
	}
	
	this.small_update = function() {
		var str = '';
		str = monthName[this.getMonth()] + ' ' + this.getYear();
		$('cal-' + this.id + '-month').innerHTML = str;
		
		str = '';
		for (var i = this.firstSunday(); i < this.lastSaturday(); i = this.nextDay(i)) {
			var d = new Date();
			d.setTime(i)
			if (d.getDay() == 0) {
				str += '<tr>';
			}
			str += '<td id="date-' + (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear() + '" width="20" class="smallDate" onclick="calendars[\''+this.id+'\'].small_date_clicked(this);">' + d.getDate() + '</td>';
		}
		
		if (is_ie()) {
			var table = $('cal-' + this.id + '-dates');
			var span = document.createElement('span');
			var table2 = table.cloneNode(false);
			table.parentNode.appendChild(span);
			table.parentNode.appendChild(table2);
			table.parentNode.removeChild(table);
			var html = table2.parentNode.innerHTML.replace(/(<\/?tbody>)|(<\/table>)/gi, '');
			table2.parentNode.removeChild(table2);
			span.innerHTML = html + str + '</table>';
		}
		else {
			$('cal-' + this.id + '-dates').innerHTML = str;
		}
		
		//alert(from_left($(this.id)));
		this.small_set_dates();
		this.small_highlight_dates();
	}
	
	this.small_date_clicked = function(cell) {
		if (this.type = 'selector') {
			for (var i in this.dates) {
				lastStartDate = i;
			}
			this.dates = new Object();
			this.dates[cell.id.substr(5)] = true;
			delete_small_calendar();
			this.small_update_dates();
			check_dates();
		}
	}
	
	this.small_update_dates = function() {
		var temp = new Array();
		for (var i in this.dates) {
			temp.push(i);
		}
		this.target.value = temp.join(', ');
	}
	
	this.small_next_month = function() {
		this.setDate(1);
		this.setMonth(this.getMonth() + 1);
		this.small_update();
	}
	
	this.small_previous_month = function() {
		this.setMonth(this.getMonth() - 1);
		this.small_update();
	}
	
	this.firstSunday = function() {
		var tempDate = new Date();
		tempDate.setFullYear(this.currentDate.getFullYear(), this.currentDate.getMonth(), this.currentDate.getDate());
		tempDate.setFullYear(tempDate.getFullYear(), tempDate.getMonth(), 1);
		tempDate.setFullYear(tempDate.getFullYear(), tempDate.getMonth(), 1 - tempDate.getDay());
		//document.write('first sunday = ' + tempDate + '<br>');
		return tempDate;
	}
	
	this.lastSaturday = function() {
		var tempDate = new Date();
		tempDate.setFullYear(this.currentDate.getFullYear(), this.currentDate.getMonth(), this.currentDate.getDate());
		tempDate.setFullYear(tempDate.getFullYear(), tempDate.getMonth()+1, 0);
		tempDate.setFullYear(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate() + 6 - tempDate.getDay());
		//document.write('last sat = ' + tempDate + '<br>');
		return tempDate;
	}
	
	this.nextDay = function(timestamp) {
		var dateObject = new Date();
		dateObject.setTime(timestamp);
		dateObject.setFullYear(dateObject.getFullYear(), dateObject.getMonth(), dateObject.getDate() + 1);
		//document.write('next day = ' + dateObject + '<br>');
		return dateObject;
	}
	
	this.set_date_from = function(str) {
		var dates = str.split(',');
		var date = trim(dates[0]);
		if (date.match(/^\d\d?\/\d\d?\/\d{4}$/)) {
			var dateParts = date.split('/');
			var month = dateParts[0];
			var day = dateParts[1];
			var year = dateParts[2];
			this.currentDate.setFullYear(year, month-1, day);
		}
	}
	
	this.clear_dates = function() {
		this.dates = new Object();
	}
}

function create_small_calendar(textbox) {
	delete_small_calendar();
	lastField = textbox.id;
	try {
		var div = document.createElement('<div style="background-color:white; border:1px solid black; position:absolute; left:' + from_left(textbox).toString() + 'px; top:' + (bottom_side(textbox) + 1).toString() + 'px;" onmouseover="overCal=true;" onmouseout="overCal=false;"></div>');
	}
	catch (e) {
		var div = document.createElement('div');
		div.setAttribute('style', 'background-color:white; border:1px solid black; position:absolute; left:' + from_left(textbox).toString() + 'px; top:' + (bottom_side(textbox) + 1).toString() + 'px;');
		div.setAttribute('onmouseover', 'overCal=true;');
		div.setAttribute('onmouseout', 'overCal=false;');
	}
	
	document.body.appendChild(div);
		
	var cal = new Calendar();
	cal.set_as_selector(textbox);
	calendars['tempCal'] = cal;
	div.innerHTML = cal.small_display();
	cal.set_date_from(textbox.value);
	cal.small_update();
	cal.small_set_dates();
	cal.small_highlight_dates();
	document.onmousedown = small_not_over;
	
	if (bottom_side(div) > screen_bottom()) {
		div.style.top = from_top(textbox) - div.offsetHeight - 4 + 'px';
	}
	div.style.zIndex = 5;
}

function delete_small_calendar() {
	if (calendars['tempCal'] != null) {
		$(calendars['tempCal'].id).parentNode.parentNode.removeChild($(calendars['tempCal'].id).parentNode);
		delete calendars['tempCal'];
		overCal = false;
		update_stop_date();
	}
}

function small_not_over() {
	if (!overCal) {
		delete_small_calendar();
		document.onmousedown = '';
	}
}

function update_start_date() {
	if (calendars['tempCal'] != null) {
		calendars['tempCal'].small_update();
		calendars['tempCal'].small_set_dates();
		calendars['tempCal'].small_highlight_dates();
	}
}

function update_stop_date() {
	
}

function check_dates() {
	var startDate = trim($('startDate').value);
	var stopDate = trim($('stopDate').value);
	if (is_date(startDate) && is_date(stopDate)) {
		var parts1 = startDate.split('/');
		var parts2 = stopDate.split('/');
		
		startDate = new Date();
		stopDate = new Date();
		
		startDate.setFullYear(parts1[2], parts1[0], parts1[1]);
		stopDate.setFullYear(parts2[2], parts2[0], parts2[1]);
		
		// make stop date at least start date		
		if (stopDate < startDate) {
			if (lastField == 'startDate') {
				$('stopDate').value = $('startDate').value
			}
			else {
				$('startDate').value = $('stopDate').value
			}
		}
	}
}

function is_date(str) {
	if (typeof str == 'string') {
		return str.match(/^\d\d?\/\d\d?\/\d{4}$/);
	}
	else {
		return false;
	}
}