// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/

var sAppDateSeparator = '-';

function getAnchorPosition(anchorname) {
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	} else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
	} else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	} else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
		}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
		}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
	} else {
		coordinates.x=0; coordinates.y=0; return coordinates;
	}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
}

function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
		} else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	} else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
	} else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
	}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
}

function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}	

function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}

/* SOURCE FILE: PopupWindow.js */
function PopupWindow_getXYPosition(anchorname) {
	var coordinates;
	coordinates = getAnchorPosition(anchorname);
	this.x = coordinates.x;
	this.y = coordinates.y;
}

function PopupWindow_setSize(width,height) {
	this.width = width;
	this.height = height;
}

function PopupWindow_populate(contents) {
	this.contents = contents;
	this.populated = false;
}

function PopupWindow_setUrl(url) {
	this.url = url;
}

function PopupWindow_refresh() {
	if (this.use_gebi) {
		document.getElementById("testdiv1").innerHTML = this.contents;
	} else if (this.use_css) { 
		document.all["testdiv1"].innerHTML = this.contents;
	} else if (this.use_layers) { 
		var d = document.layers["testdiv1"]; 
		d.document.open();
		d.document.writeln(this.contents);
		d.document.close();
	}
}

function PopupWindow_showPopup(anchorname) {
	this.getXYPosition(anchorname);
	this.x += this.offsetX;
	this.y += this.offsetY;
	if (!this.populated && (this.contents != "")) {
		this.populated = true;
		this.refresh();
	}
	if (this.use_gebi) {
		document.getElementById("testdiv1").style.left = this.x + "px";
		document.getElementById("testdiv1").style.top = this.y + "px";
		document.getElementById("testdiv1").style.visibility = "visible";
	} else if (this.use_css) {
		document.all["testdiv1"].style.left = this.x;
		document.all["testdiv1"].style.top = this.y;
		document.all["testdiv1"].style.visibility = "visible";
	} else if (this.use_layers) {
		document.layers["testdiv1"].left = this.x;
		document.layers["testdiv1"].top = this.y;
		document.layers["testdiv1"].visibility = "visible";
	}
}

function PopupWindow_hidePopup() {
	if (this.use_gebi) {
		document.getElementById("testdiv1").style.visibility = "hidden";
	} else if (this.use_css) {
		document.all["testdiv1"].style.visibility = "hidden";
	} else if (this.use_layers) {
		document.layers["testdiv1"].visibility = "hidden";
	}
}

function PopupWindow_isClicked(e) {
	if (this.use_layers) {
		var clickX = e.pageX;
		var clickY = e.pageY;
		var t = document.layers["testdiv1"];
		if ((clickX > t.left) && (clickX < t.left+t.clip.width) && (clickY > t.top) && (clickY < t.top+t.clip.height)) {
			return true;
		}
		else { return false; }
	} else if (document.all) {
		var t = window.event.srcElement;
		while (t.parentElement != null) {
			if (t.id=="testdiv1") {
				return true;
			}
			t = t.parentElement;
		}
		return false;
	} else if (this.use_gebi && e) {
		var t = e.originalTarget;
		while (t.parentNode != null) {
			if (t.id=="testdiv1") {
				return true;
			}
			t = t.parentNode;
		}
		return false;
	}
	return false;
}

function PopupWindow_hideIfNotClicked(e) {
	if (this.autoHideEnabled && !this.isClicked(e)) {
		this.hidePopup();
	}
}

function PopupWindow_autoHide() {
	this.autoHideEnabled = true;
}

function PopupWindow_hidePopupWindows(e) {
	for (var i=0; i<popupWindowObjects.length; i++) {
		if (popupWindowObjects[i] != null) {
			var p = popupWindowObjects[i];
			p.hideIfNotClicked(e);
		}
	}
}

function PopupWindow_attachListener() {
	if (document.layers) {
		document.captureEvents(Event.MOUSEUP);
	}
	window.popupWindowOldEventListener = document.onmouseup;
	if (window.popupWindowOldEventListener != null) {
		document.onmouseup = new Function("window.popupWindowOldEventListener(); PopupWindow_hidePopupWindows();");
	} else {
		document.onmouseup = PopupWindow_hidePopupWindows;
	}
}

function PopupWindow() {
	if (!window.popupWindowIndex) { window.popupWindowIndex = 0; }
	if (!window.popupWindowObjects) { window.popupWindowObjects = new Array(); }
	if (!window.listenerAttached) {
		window.listenerAttached = true;
		PopupWindow_attachListener();
	}
	this.index = popupWindowIndex++;
	popupWindowObjects[this.index] = this;
	this.popupWindow = null;
	this.width=0;
	this.height=0;
	this.populated = false;
	this.visible = false;
	this.autoHideEnabled = false;
	this.contents = "";
	this.url="";
	this.windowProperties="toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable,alwaysRaised,dependent,titlebar=no";
	this.use_gebi = false;
	this.use_css = false;
	this.use_layers = false;
	if (document.getElementById) { this.use_gebi = true; }
	else if (document.all) { this.use_css = true; }
	else if (document.layers) { this.use_layers = true; }
	this.offsetX = 0;
	this.offsetY = 0;
	this.getXYPosition = PopupWindow_getXYPosition;
	this.populate = PopupWindow_populate;
	this.setUrl = PopupWindow_setUrl;
	this.refresh = PopupWindow_refresh;
	this.showPopup = PopupWindow_showPopup;
	this.hidePopup = PopupWindow_hidePopup;
	this.setSize = PopupWindow_setSize;
	this.isClicked = PopupWindow_isClicked;
	this.autoHide = PopupWindow_autoHide;
	this.hideIfNotClicked = PopupWindow_hideIfNotClicked;
	this.bCombined = false;
}

/* SOURCE FILE: CalendarPopup.js */
function CalendarPopup() {
	var c;
	if (arguments.length>0) {
		c = new PopupWindow(arguments[0]);
	} else {
		c = new PopupWindow();
		c.setSize(150,175);
	}
	
	c.offsetX = 82;
	c.offsetY = 30;
	c.autoHide();
	c.monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	c.monthAbbreviations = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	c.dayHeaders = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
	c.returnFunction = "CP_tmpReturnFunction";
	c.disabledDatesExpression = "";
	c.currentDate = null;
	window.CP_dateFormat = "mm/dd/yyyy";
	window.CP_dateField = "";
	c.copyMonthNamesToWindow = CP_copyMonthNamesToWindow;
	c.addDisabledDates = CP_addDisabledDates;
	c.showCalendar = CP_showCalendar;
	c.hideCalendar = CP_hideCalendar;
	c.getStyles = CP_getStyles;
	c.refreshCalendar = CP_refreshCalendar;
	c.getCalendar = CP_getCalendar;
	c.select = CP_select;
	c.copyMonthNamesToWindow();
	c.fieldCombined = false;
	return c;
}

function CP_copyMonthNamesToWindow() {
	if (typeof(window.MONTH_NAMES)!="undefined" && window.MONTH_NAMES!=null) {
		window.MONTH_NAMES = new Array();
		for (var i=0; i<this.monthNames.length; i++) {
			window.MONTH_NAMES[window.MONTH_NAMES.length] = this.monthNames[i];
		}
		for (var i=0; i<this.monthAbbreviations.length; i++) {
			window.MONTH_NAMES[window.MONTH_NAMES.length] = this.monthAbbreviations[i];
		}
	}
}

function CP_tmpReturnFunction(y,m,d,bCombined) {
	var dt = new Date(y,m-1,d,0,0,0);
	var tmpDateStr = formatDate(dt,window.CP_dateFormat);
	document.getElementById(window.CP_dateField + "_DAY").value = tmpDateStr.substr(0,2);
	if (bCombined == false) {
		document.getElementById(window.CP_dateField + "_MONTH").value = tmpDateStr.substr(3,3);
		document.getElementById(window.CP_dateField + "_YEAR").value = tmpDateStr.substr(7,4);
	} else {
		document.getElementById(window.CP_dateField + "_MONTH").value = tmpDateStr.substr(3);
	}
	if (typeof(document.getElementById(window.CP_dateField + "_DAY").onchange) == "function") document.getElementById(window.CP_dateField + "_DAY").onchange();
}

function CP_addDisabledDates(start, end) {
	if (arguments.length==1) { end=start; }
	if (start==null && end==null) { return; }
	if (this.disabledDatesExpression!="") { this.disabledDatesExpression+= "||"; }
	if (start!=null) { start = parseDate(start); start=""+start.getFullYear()+LZ(start.getMonth()+1)+LZ(start.getDate());}
	if (end!=null) { end=parseDate(end); end=""+end.getFullYear()+LZ(end.getMonth()+1)+LZ(end.getDate());}
	if (start==null) { this.disabledDatesExpression+="(ds<="+end+")"; }
	else if (end  ==null) { this.disabledDatesExpression+="(ds>="+start+")"; }
	else { this.disabledDatesExpression+="(ds>="+start+"&&ds<="+end+")"; }
}
	
function CP_hideCalendar() {
	if (arguments.length > 0) { window.popupWindowObjects[arguments[0]].hidePopup(); }
	else { this.hidePopup(); }
}

function CP_refreshCalendar(index) {
	var calObject = window.popupWindowObjects[index];
	if (arguments.length>1) { 
		calObject.populate(calObject.getCalendar(arguments[1],arguments[2],arguments[3],arguments[4],arguments[5]));
	} else {
		calObject.populate(calObject.getCalendar());
	}
	calObject.refresh();
}

function CP_showCalendar(anchorname) {
	if (arguments.length>1) {
		if (arguments[1]==null||arguments[1]=="") {
			this.currentDate=new Date();
		} else {
			this.currentDate=new Date(parseDate(arguments[1]));
		}
	}
	this.populate(this.getCalendar());
	this.showPopup(anchorname);
}

function CP_select(linkname, format, dateField, bCombined) {
	if (typeof(bCombined) == "undefined") bCombined = false;
	this.fieldCombined = bCombined;
	if (bCombined == false) {
		var selectedDate = getDateFromFormat(document.getElementById(dateField + "_DAY").value + sAppDateSeparator + document.getElementById(dateField + "_MONTH").value + sAppDateSeparator + document.getElementById(dateField + "_YEAR").value, format);		
	} else {
		var selectedDate = getDateFromFormat(document.getElementById(dateField + "_DAY").value + sAppDateSeparator + document.getElementById(dateField + "_MONTH").value, format);
	}
	this.currentDate=null;
	if (selectedDate == 0)
		this.currentDate = null;
	else
		this.currentDate = new Date(selectedDate);
	window.CP_dateFormat = format;
	window.CP_dateField = dateField;
	this.showCalendar(linkname);
}

function CP_getStyles() {
	var result = "";
	result += "<STYLE>\n";
	result += "TD.cal,TD.calday,TD.calmonth,TD.caltoday,A.textlink,.disabledtextlink { font-family:arial; font-size: 8pt; }\n";
	result += "TD.calday{border:solid thin #C0C0C0;border-width:0 0 1 0;}\n";
	result += "TD.calmonth{text-align:right;}\n";
	result += "TD.caltoday{text-align:right;color:white;background-color:#C0C0C0;border-width:1;border:solid thing #800000;}\n";
	result += "TD.textlink{border:solid thin #C0C0C0; border-width:1 0 0 0;}\n";
	result += "A.textlink{height:20px;color:black;}\n";
	result += ".disabledtextlink{height:20px;color:#808080;}\n";
	result += "A.cal{text-decoration:none;color:#000000;}\n";
	result += "A.calthismonth{text-decoration:none;color:#000000;}\n";
	result += "A.calothermonth{text-decoration:none; color:#808080;}\n";
	result += ".calnotclickable{color:#808080;}\n";
	result += ".disabled{color:#D0D0D0;text-decoration:line-through;}\n";
	result += "</STYLE>\n";
	return result;
}

function CP_getCalendar() {
	var now = new Date();
	var nowYear = now.getFullYear();
	var nowMonth = now.getMonth()+1;
	var nowDate = now.getDate();
	var result = '';
	if (this.currentDate==null) { this.currentDate = now;}
	if (arguments.length > 0) { var month = arguments[0];}
	else {var month = this.currentDate.getMonth()+1;}
	if (arguments.length > 1 && arguments[1]>0 && arguments[1]-0==arguments[1]) {var year = arguments[1];}
	else {var year = this.currentDate.getFullYear();}
	var daysinmonth= new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	if (((year%4 == 0)&&(year%100 != 0)) || (year%400 == 0)) {daysinmonth[2] = 29;}
	var current_month = new Date(year,month-1,1);
	var display_year = year;
	var display_month = month;
	var display_date = 1;
	var weekday= current_month.getDay();
	var offset = 0;
	
	offset = (weekday >= 0) ? weekday-0 : 7-0+weekday ;
	if (offset > 0) {
		display_month--;
		if (display_month < 1) { display_month = 12; display_year--; }
		display_date = daysinmonth[display_month]-offset+1;
	}
	var next_month = month+1;
	var next_month_year = year;
	if (next_month > 12) { next_month=1; next_month_year++; }
	var last_month = month-1;
	var last_month_year = year;
	if (last_month < 1) { last_month=12; last_month_year--; }
	var date_class;
	result += "<iframe src=\"about:blank\" scrolling=\"no\" frameborder=\"0\" style=\" width:184px; height:268px; z-index:-1; position: absolute; filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0); \"></iframe>";
	result += "<table border=\"0\" cellspacing=\"2\" cellpadding=\"3\">";
	result += '<tr>\n';
	var refresh = 'CP_refreshCalendar';
	var refreshLink = 'javascript:' + refresh;
	result += '<td class="cpMonthNavigation"><a class="cpMonthNavigation" href="'+refreshLink+'('+this.index+','+last_month+','+last_month_year+');">&lt;&lt;</a></td>\n';
	result += '<td colspan=\"5\" class="cpMonthNavigation"><span class="cpMonthNavigation">'+this.monthNames[month-1]+' '+year+'</span></td>\n';
	result += '<td class="cpMonthNavigation"><a class="cpMonthNavigation" href="'+refreshLink+'('+this.index+','+next_month+','+next_month_year+');">&gt;&gt;</a></td>\n';
	result += '</tr>\n';
	result += '<tr>\n';
	for (var j=0; j<7; j++) {
		result += '<td class="cpDayColumnHeader"><span class="cpDayColumnHeader">'+this.dayHeaders[(0+j)%7]+'</td>\n';
	}
	result += '</tr>\n';
	for (var row=1; row<=6; row++) {
		result += '<tr>\n';
		for (var col=1; col<=7; col++) {
			var disabled=false;
			if (this.disabledDatesExpression!="") {
				var ds=""+display_year+LZ(display_month)+LZ(display_date);
				eval("disabled=("+this.disabledDatesExpression+")");
			}
			var dateClass = "";
			if ((display_month == this.currentDate.getMonth()+1) && (display_date==this.currentDate.getDate()) && (display_year==this.currentDate.getFullYear())) {
				dateClass = "cpCurrentDate";
			} else if (display_month == month) {
				dateClass = "cpCurrentMonthDate";
			} else {
				dateClass = "cpOtherMonthDate";
			}
			if ((display_year < nowYear) || ((display_year == nowYear) && (display_month < nowMonth)) || ((display_year == nowYear) && (display_month == nowMonth) && (display_date < nowDate))) {
				disabled = true;
			}
			if (disabled) {
				result += '	<td class="'+dateClass+'"><span class="'+dateClass+'Disabled">'+display_date+'</span></td>\n';
			} else {
				var selected_date = display_date;
				var selected_month = display_month;
				var selected_year = display_year;
				result += '	<td class="'+dateClass+'"><a href="javascript:'+this.returnFunction+'('+selected_year+','+selected_month+','+selected_date+','+this.fieldCombined+');'+'CP_hideCalendar(\''+this.index+'\');" class="'+dateClass+'">'+display_date+'</a></td>\n';
			}
			display_date++;
			if (display_date > daysinmonth[display_month]) {
				display_date=1;
				display_month++;
			}
			if (display_month > 12) {
				display_month=1;
				display_year++;
			}
		}
		result += '</tr>';
	}
	var current_weekday = now.getDay() - 0;
	if (current_weekday < 0) {
		current_weekday += 7;
	}
	result += '<tr>\n';
	result += '	<td colspan=7 align=center class="cpTodayText">\n';
	if (this.disabledDatesExpression!="") {
		var ds=""+now.getFullYear()+LZ(now.getMonth()+1)+LZ(now.getDate());
		eval("disabled=("+this.disabledDatesExpression+")");
	}
	if (disabled) {
		result += '		<span class="cpTodayTextDisabled">Today</span>\n';
	} else {
		result += '		<a class="cpTodayText" href="javascript:'+this.returnFunction+'(\''+now.getFullYear()+'\',\''+(now.getMonth()+1)+'\',\''+now.getDate()+'\','+this.fieldCombined+');'+'CP_hideCalendar(\''+this.index+'\');">Today</a>\n';
	}
	result += '		<br>\n';
	result += '	</td></tr></table>\n';

	return result;
}
