/***************************************************************
************************* DateSelector *************************
***************************************************************/

DateSelector = function(name, containerName, startDate){
	this.name = name; // reference to self
	this.startDate  = (typeof startDate != "undefined") ? startDate : new Date("January 1, 1000");
	this.month = null;
	this.year = null;
	this.today  = null;
	this.selectedDates = new Array(); // will hold selected dates
	
	this.months = {"1":"January", "2":"February", "3":"March", "4":"April", "5":"May", "6":"June", "7":"July", "8":"August", "9":"September", "10":"October", "11":"November", "12":"December"}
	this.weekdays = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	
	this.setMonthAndYear(); // set default values
	this.showResetLink = true;
	
	if ((typeof containerName != "undefined") && (containerName !="")){
		// load dateSelector into specified container (div)	
		this.node = document.getElementById(containerName);
	}
	else {
		// create new div node and append to body
		var docBody = document.getElementsByTagName("body").item(0); 
		this.node = document.createElement("div");
		this.node.id = "dateSelector";
		docBody.appendChild(this.node);
	}	
	
}

DateSelector.prototype.setMonthAndYear = function(month,year){
	var today = new Date();
	this.month = (month!=null && month>0 && month<13) ? month : today.getMonth()+1;
	this.year  = (year!=null) ? year : today.getFullYear();
}

DateSelector.prototype.setToday = function(today){
	this.today = today;
}

DateSelector.prototype.show = function(month,year){
	this.setMonthAndYear(month,year);
	this.node.innerHTML = this.getCalendarHtml();
	this.highlightSelectedDays();
	this.node.style.display = "block";
}

DateSelector.prototype.hide = function(){
	this.node.style.display = "none";
}

DateSelector.prototype.toggle = function(){
	if (this.node.style.display == "none") this.show();
	else this.hide();
}

DateSelector.prototype.reset = function(){
	this.selectedDates = new Array();
	this.show();
}

DateSelector.prototype.addDate = function(date){
	this.selectedDates.push(date);
}

DateSelector.prototype.removeDate = function(date){
	var remainingDates = new Array();
	for (var i=0; i<this.selectedDates.length; i++){
		if (this.selectedDates[i] != date) remainingDates.push(this.selectedDates[i]);
	}
	this.selectedDates = remainingDates;
}

DateSelector.prototype.highlightSelectedDays = function(){
	for (var i=0; i<this.selectedDates.length; i++){
		if (this.getTwoDigit(this.month) == this.selectedDates[i].substr(4,2) && this.year == this.selectedDates[i].substr(0,4)){
			this.toggleCell("cell" + this.selectedDates[i].substr(6,2),this.selectedDates[i],true);
		}
	}
}

DateSelector.prototype.getDaysInMonth = function(){
	return 32 - new Date(this.year, this.month-1, 32).getDate();
}

DateSelector.prototype.getWeekdayIndexFirstOfMonth = function(){
	var firstOfMonth = new Date(this.year, this.month-1, 1);
	return firstOfMonth.getDay();
}

DateSelector.prototype.getTwoDigit = function(num){
	num = num.toString();
	if (num.length == 2) return num;
	else return "0" + num;
}

DateSelector.prototype.removeLeadingZero = function(num){
	var numStr = num.toString();
	if (numStr.charAt(0) == 0) return numStr.charAt(1);
	else return numStr;
}

DateSelector.prototype.getDateString = function(date, customFunction){
	date = date.toString();
	var year  = date.substr(0,4);
	var month = this.removeLeadingZero(date.substr(4,2));
	var day   = this.removeLeadingZero(date.substr(6,2));
	var date = new Date(year,(month-1),day);
	if (customFunction){
		return eval("date." + customFunction + "()");
	}
	return date.toDateString();
}

DateSelector.prototype.getMonthName = function(){
	var month = this.month.toString();
	if (month.charAt(0) == 0) return this.months[month.charAt(1)];
	else return this.months[month];
}

DateSelector.prototype.getPrevMonthLink = function(){
	var prevMonth = null; var year = null;
	if (this.month == 1){ prevMonth = 12; year = this.year-1; } 
	else { prevMonth = this.month-1; year = this.year; }

	var prevDate = new Date(year,prevMonth,1);
      if ( prevDate > this.startDate ){
		return "<a href='javascript:" + this.name + ".show(" + prevMonth + "," + year + ")'>" + "&laquo; Prev" + "</a>";
	}
	else {
		return "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	}
}

DateSelector.prototype.getNextMonthLink = function(){
	var nextMonth = null; var year = null;
	if (this.month == 12){ nextMonth = 1; year = this.year+1; } 
	else { nextMonth = this.month+1; year = this.year; }
	return "<a href='javascript:" + this.name + ".show(" + nextMonth + "," + year + ")'>" + "Next &raquo;" + "</a>";
}

DateSelector.prototype.isToday = function(curDate){
	if (this.today != null && curDate == this.today) return true;
	else return false;
}

DateSelector.prototype.getCalendarHtml = function(){
	var out = "";
	out += "<table border='0' cellpadding='0' cellspacing='0' width='100%' id='monthNav'>\n";
	out += "<tr>\n<td class='monthLink'>" + this.getPrevMonthLink() + "</td>\n";
	out += "<td class='curMonth' nowrap='nowrap'>" + this.getMonthName() + " " + this.year + "</div>\n";
	out += "<td class='monthLink' align='right'>" + this.getNextMonthLink() + "</td>\n";
	out += "</tr>\n</table>\n";
	
	out += "<table border='0' cellpadding='0' cellspacing='1' width='100%' id='monthDays'>\n";
	out += "<thead><tr>\n";
	for (var i=0; i<this.weekdays.length; i++) out += "<td>" + this.weekdays[i] + "</td>\n";
	out += "</tr></thead><tbody>\n";
	
	var curDay = 1;
	var weekdayIndex = this.getWeekdayIndexFirstOfMonth();
	var isFirstWeek  = true;
	
  while (curDay <= this.getDaysInMonth()){
		// draw blank days in first week
  	if (isFirstWeek && weekdayIndex != 0){
			out += "<tr>";
			for (var i=1; i<=weekdayIndex; i++) out += "<td class='blank'>&nbsp;</td>\n";
      isFirstWeek = false;
		} else {
			isFirstWeek = false;
		}

		if (weekdayIndex==0) out += "<tr>\n"; // if weekday is Sunday, open new table row

		var thisDate = new Date(this.year,this.month-1,curDay);
      	if ( thisDate >= this.startDate ){

			out += "<td id='cell" + this.getTwoDigit(curDay) + "' class='day" + (( this.isToday(this.year + this.getTwoDigit(this.month) + this.getTwoDigit(curDay)) )?" today":"") + "'><a href=\"javascript:" + this.name + ".toggleCell('cell" + this.getTwoDigit(curDay) + "','" + this.year + this.getTwoDigit(this.month) + this.getTwoDigit(curDay) + "',false)\" onClick=\"this.blur();\">" + curDay + "</a></td>\n";
		}
		else {
			out += "<td id='cell" + this.getTwoDigit(curDay) + "' class='dayGrey" + (( this.isToday(this.year + this.getTwoDigit(this.month) + this.getTwoDigit(curDay)) )?" today":"") + "'>" + curDay + "</td>\n";
		}

		if (weekdayIndex==6) out += "</tr>\n"; // if weekday is Saturday, close table row

		weekdayIndex++;
    weekdayIndex = weekdayIndex % 7;
    curDay++;
	}
	
	// draw blank days in last week
	while (weekdayIndex < 7 && weekdayIndex != 0){
		out += "<td class='blank'>&nbsp;</td>\n";
		weekdayIndex++;
	}
	out += "</tr></tbody></table>\n";
	
	out += "<table border='0' cellpadding='0' cellspacing='0' width='100%' id='subLinks'>\n";
	out += "<tr><td>";
	if (this.showResetLink) out += "<a href='javascript:" + this.name + ".reset()'>Reset</a>";
	out += "</td><td align='right'><a href='javascript:" + this.name + ".hide()'>Close</a></td></tr>\n";
	out += "</table>\n";
	return out;
}

DateSelector.prototype.toggleCell = function(cellId,dateString,onlyChangeCellColor){
	var cell = document.getElementById(cellId);
	if (cell.className == "day" || cell.className == "day today"){
		cell.className = "selectedDay";
		if (!onlyChangeCellColor) this.addDate(dateString);			
	}
	else {
		cell.className = "day";
		if (!onlyChangeCellColor) this.removeDate(dateString);
	}
	if (!onlyChangeCellColor) this.processSelection();
}

DateSelector.prototype.processSelection = function(){
	// override this method for actual response
	alert("DateSelector.processSelection() hasn't been re-defined as needed.");
}
