/**
 * @author Walter Wimberly
 * @copyright	2007
 * @version	.9
 */
var dateobj = null;

function DisplayCalendar(dt, target, targetTxt, objName, formatStyle, hideOnSelect, monthLength, dayLength) {
	this.targetText = targetTxt;
	this.targetDiv = document.getElementById(target);
	if(!(document.getElementById(target))) {
		document.writeln('<div class="wdCalendarContainer" id="' + target + '"></div>');
		this.targetDiv = document.getElementById(target);
	}
	this.displayDate = dt ? dt : new Date();
	this.objName = objName ? objName : null;
	this.formatStyle = formatStyle ? formatStyle : 'mm/dd/yyyy';
	this.hideOnSelect = hideOnSelect ? hideOnSelect : true;
	this.monthLength = monthLength ? monthLength : 30;
	this.dayLength = dayLength ? dayLength : 3;
	
}

DisplayCalendar.prototype = {
	
	updateDate: function(newDate) {
		this.displayDate = newDate;
	},
	
	addMonth: function() {
		this.updateMonth(1);
	},
	
	subMonth: function() {
		this.updateMonth(-1);
	},
	
	updateMonth: function(modifier) {
		this.displayDate.setMonth(this.displayDate.getMonth() + modifier);
		this.showCalendar();
	},
	
	addYear: function() {
		this.updateYear(1);
	},
	
	subYear: function() {
		this.updateYear(-1);
	},
	
	updateYear: function(modifier) {
		this.displayDate.setYear(this.displayDate.getFullYear() + modifier);
		this.showCalendar();
	},
	
	
	createTD: function(num, today, tmpDate) {
		var str = '';
		str += '<td';
		if(today.getFullYear() == tmpDate.getFullYear() && 
			today.getMonth() == tmpDate.getMonth() &&
			today.getDate() == tmpDate.getDate()) {
				str += ' class="today"';
		} else if( tmpDate.getDay() === 0 || tmpDate.getDay() == 6) {
			str += ' class="weekend"';
		}
		str += '>';
		if(this.targetText !== null) {
			str += '<a href="#" onclick="javascript:sendDate(\'' + 
				this.targetText + '\', new Date(' + tmpDate.getFullYear() + 
				', ' + tmpDate.getMonth() + ', ' + tmpDate.getDate() + 
				'), \''+ this.formatStyle + '\'';
			if( this.objName ) {
				str += ', \'' + this.objName + '\'';
			}
			str += ');';
			if(this.hideOnSelect) {
				str += ' hideCalendar(this);';
			}
			str +=	'">';
			str += num;
			str += '</a>';
		} else {
			str += num;
		}
		str += '</td>';
		return str;
	},
	
	showCalendar: function(passedDate) {
		var tmpDate = new Date(this.displayDate);
		var today = passedDate ? passedDate :new Date(this.displayDate);
		
		var initialMonth = this.displayDate.getMonth();
		
		var str = '';
		
		str += '<table class="wdCalendar" cellspacing="0">';
		
		str += this.getCalHeader();
		str += '<tr>';
		
		tmpDate.setDate(1);
		day = tmpDate.getDay();
		if(day > 0) {
			str += '<td';
			str += ' colspan="' + day + '"';
			str += '>&nbsp;';
			str += '</td>';
		}
		
		i = 1;
		for(; day < 7; day++) {
			str += this.createTD(i, today, tmpDate);
			i++;
			tmpDate.setDate(tmpDate.getDate() + 1);
		}
		str += '</tr>';
		
		tmpDate.setDate(i);
		j = 0;
		do {
			if(j % 7 === 0) {
				str += '<tr>';
				j = 0;
			}
			str += this.createTD(i, today, tmpDate);
			i++;
			j++;
			if(j % 7 === 0) {
				str += '</tr>';
			}
			tmpDate.setDate(tmpDate.getDate() + 1);
		} while(tmpDate.getMonth() == initialMonth);
	
		j = 7 - j % 7;
		
		if (j > 1 && j < 7) {
			if(j == 1) {
				str += '<td>&nbsp;</td>';
			} else {
				str += '<td colspan="' + j +'">&nbsp;</td>';
			}
			
			str += '</tr>';
		}
		str += '</table>';
		
		
		this.targetDiv.innerHTML = str;
		this.targetDiv.style.display = "block";
	},

	
	getCalHeader: function() {
		var t = '';
		
		
		// display month and year
		t += "<thead>";
		t += "<tr>";
		t += '<th colspan="7" class="wdCalHeader">' + monthNames[this.displayDate.getMonth()].substr(0, this.monthLength) + "&nbsp;" + 
				this.displayDate.getFullYear() + '</th>'; 
		t += "</tr>";
		
		
		// display the modifers...
		if(this.objName) {
			t += '<tr class="wdCalNavRow">';
			t += '<td colspan="3" class="wdCalNavLeft">';
			t += '<a href="javascript:'+ this.objName +'.subYear();">&lt;&lt;</a>&nbsp;&nbsp;';
			t += '<a href="javascript:'+ this.objName +'.subMonth();">&lt;</a>';
			t += '</td>';
			t += '<td>&nbsp;</td>';
			t += '<td colspan="3" class="wdCalNavRight">&nbsp;';
			t += '<a href="javascript:'+ this.objName +'.addMonth();">&gt;</a>&nbsp;&nbsp;';
			t += '<a href="javascript:'+ this.objName +'.addYear();">&gt;&gt;</a>';
			t += '</td>';
			t += "</tr>";
		}
		
		// put in the days of the week
		t += '<tr class="wdWeekDayHeader">';
		for(i = 0; i < 7; i++) {
			t += '<th class="wdWeekDayHeader">' + dayNames[i].substr(0, this.dayLength) + "</th>";
		}
		t += "</tr>";
		t += "</thead>";
		return t;
	}
	
};

function hideCalendar(link) {
	link.parentNode.parentNode.parentNode.parentNode.parentNode.style.display = "none";
}

function sendDate(target, date, formatting, objName) {
	document.getElementById(target).value = new DateFormater( date ).customFormatMSStyle(formatting);
	
	if(objName && objName.updateDate) {
		objName.updateDate(date);
	}
}

function initializeCalendar(configuration) {
	var lst = document.getElementsByTagName("input");
	var attLst = null;
	var txtId = '';
	var imgSrc = imgSrc ? configuration.image : "/images/cal.gif";
	
	// determin the configuration
	var dt = dt ? configurtion.date : "new Date()";
	var fs = configuration && configuration.format ? configuration.format: 'mm/dd/yyyy';
	var ml  = configuration && configuration.monthLength ? configuration.monthLength : 'null';
	var dl = configuration && configuration.dayLength ? configuration.dayLength : 'null';
	
	
	for(i = 0; i < lst.length; i++) {
		attLst = lst[i].attributes;
		if(lst[i].getAttribute('calendar') ) {
			
			txtId = lst[i].getAttribute('id');
			
			var configStr = dt + ", '" + txtId + "CalendarContainer" + "','"  
				+ txtId + "', '" + txtId + "obj', '" + fs + "', '', " + ml + ", " + dl;

			str = "<img src=\"" + imgSrc + "\" id=\"img4" + txtId +"\"" +
				"onclick=\"{ " + txtId + "obj = new DisplayCalendar(" + configStr + "); " + txtId + "obj.showCalendar();}\" />";
			// str += "<img src=\"" + imgSrc + "\" id=\"img4" + txtId +"\"" +
			
			var txtBox = lst[i].cloneNode(false);
			var calCont = document.createElement("span");
			calCont.setAttribute("class", "wdCalendarContainer");
			calCont.setAttribute("id", txtId + "CalendarContainer");
			
			var container = document.createElement("span");
			container.appendChild(txtBox);
			container.appendChild(calCont);
			container.innerHTML += str;
			lst[i].parentNode.replaceChild(container, lst[i]);
			
			var elem = document.getElementById(txtId + "CalendarContainer");
			elem.className = "wdCalendarContainer";
			
		}
	}
	
}

