function yfcMiniCalPrevMonth() {
	if (this.currField == undefined) return;
	if (this.currMonth > 0)
		this.currMonth--;
	if (this.currMonth == 0)
		document.getElementById('yfcMiniCalPrevMonthDiv').style.visibility = 'hidden';
	document.getElementById('yfcMiniCalNextMonthDiv').style.visibility = 'visible';
	if (this.disableSelectbox) {
		document.getElementById('yfcMiniCalThisMonthTitle').innerHTML = this.months[this.currMonth].dropDownLabel;
	}
	else {
		document.getElementById('yfcMiniCalMonthSelect').selectedIndex = this.currMonth;
	}
	this.showMonth();  
}

function yfcMiniCalNextMonth() {
	if (this.currField == undefined) return;
	if (this.currMonth < this.maxMonth)
		this.currMonth++;
	if (this.currMonth == this.maxMonth-1)
		document.getElementById('yfcMiniCalNextMonthDiv').style.visibility = 'hidden';
	document.getElementById('yfcMiniCalPrevMonthDiv').style.visibility = 'visible';
	if (this.disableSelectbox) {
		document.getElementById('yfcMiniCalThisMonthTitle').innerHTML = this.months[this.currMonth].dropDownLabel;
	}
	else {
		document.getElementById('yfcMiniCalMonthSelect').selectedIndex = this.currMonth;
	}
	this.showMonth();
}

function yfcMiniCalCloseCal() {
	var cal = document.getElementById('yfcMiniCalDiv');
	var iframed = document.getElementById('yfcMiniCalIFrame');
	var iframe = document.getElementById('yfcMiniCalIFrameIn');
	if (!cal || !iframe || !iframed) return;
	cal.style.visibility = 'hidden';
	iframed.style.height = "0px";
	iframed.style.width = "0px";
	iframe.style.height = "0px";
	iframe.style.width = "0px";
	iframed.style.visibility = 'hidden';
	document.getElementById('yfcMiniCalNextMonthDiv').style.visibility = 'hidden';
	document.getElementById('yfcMiniCalPrevMonthDiv').style.visibility = 'hidden';
	this.displayed[this.currField] = 0;
	this.currField = undefined;
    if (this.closeCalHandler != undefined) {
        this.closeCalHandler();
    }
	delete(cal);
}

function yfcMiniCalOpenCal(fldDiv,fld,startFld, xOffset, yOffset) {
    if (!xOffset) {xOffset = 0;};
    if (!yOffset) {yOffset = 24;}; 

	var fldDiv = document.getElementById(fldDiv);
	var cal = document.getElementById('yfcMiniCalDiv');
	var iframeDiv = document.getElementById('yfcMiniCalIFrame');
	var iframe = document.getElementById('yfcMiniCalIFrameIn');
	var field = this.fields[fld];
	
	if (!fldDiv) return;
	if (!cal) return;
	if (!iframeDiv || !iframe) return;
	if (!field) return;
	
	var absoluteLeft = getAbsoluteLeft(fldDiv);
	var absoluteTop  = getAbsoluteTop (fldDiv);
	var left = fldDiv? absoluteLeft + xOffset + 'px' : 0;
	var top  = fldDiv? absoluteTop + yOffset + 'px' : 0;

	cal.style.top  = top;
	cal.style.left = left;
	iframeDiv.style.top = top;
	iframeDiv.style.left = left;
	iframeDiv.style.height = "197px";
	iframeDiv.style.width = "390px";
	iframe.style.height = "197px";
	iframe.style.width = "390px";
	
	cal.style.visibility = 'visible';
	if (navigator.userAgent.indexOf('Opera') == -1) {
		iframeDiv.style.visibility = 'visible';
	}
	
	this.displayed[this.currField] = 0;
	this.currField = fld;
	this.displayed[fld] = 1;
	this.startField = startFld;
	this.showMonth(true);

	if (typeof(horizontalResizeManager)!='undefined') {
		horizontalResizeManager.attach(iframeDiv,fldDiv);
		horizontalResizeManager.attach(cal,fldDiv);
	}
	delete(cal);
}

function getAbsoluteLeft (e) {
	var p = 0;
  while (e.offsetParent) {
 	  p += e.offsetLeft;
    e = e.offsetParent;
  }
  return p;
}

function getAbsoluteTop (e) {
  var p = 0;
  while (e.offsetParent) {
 	  p += e.offsetTop;
    e = e.offsetParent;
  }
  return p;
}

function yfcMiniCalUpdateArrows() {
	var pm = document.getElementById('yfcMiniCalPrevMonthDiv');
	var nm = document.getElementById('yfcMiniCalNextMonthDiv');
	if (this.currMonth == this.maxMonth-1) {
  		nm.style.visibility = 'hidden';
		pm.style.visibility = 'visible';
	}
	else if (this.currMonth == 0) {
  		nm.style.visibility = 'visible';
		pm.style.visibility = 'hidden';
	}
	else {
  		nm.style.visibility = 'visible';
		pm.style.visibility = 'visible';
	}
	delete pm;
	delete nm;
}

function yfcMiniCalShowMonth(jumpToMonth) {
	var formNames = this.currField.split(this.formFieldSeparator);
	var currFieldDate = document.forms[formNames[0]][formNames[1]].value;
	var currFieldDateParse = currFieldDate.split('/');
	if (this.dayMonthDate) {
		var selDay = currFieldDateParse[0];
		var selMon = currFieldDateParse[1];
	} else {
		var selMon = currFieldDateParse[0];
		var selDay = currFieldDateParse[1];
	}
	var selMonForCheck = (selMon && selMon.indexOf("0")==0) ? selMon.substring(1) : selMon;
	var selDayForCheck = (selDay && selDay.indexOf("0")==0) ? selDay.substring(1) : selDay;
	var selYear = currFieldDateParse[2];
	if (!parseInt(selMonForCheck) || !parseInt(selDayForCheck) || !parseInt(selYear)) {
		var todayDate = new Date();
		selMon = todayDate.getMonth();
		if (selMon<10) selMon = "0" + selMon;
		selDay = todayDate.getDate();
		if (selDay<10) selDay = "0" + selDay;
		selYear = todayDate.getFullYear() % 100;
		if (selYear<10) selYear = "0" + selYear;
	}
	var startFieldDate = null;
	var startMon = null;
	var startDay = null;
	var startYear = null;
	if (this.startField) {
		formNames = this.startField.split(this.formFieldSeparator);
		startFieldDate = document.forms[formNames[0]][formNames[1]].value;
		currFieldDateParse = startFieldDate.split('/');
		if (this.dayMonthDate) {
			startDay = currFieldDateParse[0];
			startMon = currFieldDateParse[1];
		} else {
			startMon = currFieldDateParse[0];
			startDay = currFieldDateParse[1];
		}
		startYear = currFieldDateParse[2];
	}
	var nextMonth = this.currMonth + 1;
	if (jumpToMonth) {
		if (selMon.length == 1) selMon = '0' + selMon;
		for (var m = 0; m < this.maxMonth; m++) {
			if (this.months[m].mon != selMon || this.months[m].yr != selYear) continue;
			if (this.disableSelectbox) {
				this.currMonth = m;
				document.getElementById('yfcMiniCalThisMonthTitle').innerHTML = this.months[this.currMonth].dropDownLabel;
			}
			else {
				this.currMonth = ((startMon == selMon || startMon == null) ? m : m-1);
                if(this.currMonth < 0) { this.currMonth = 0; }
				document.getElementById('yfcMiniCalMonthSelect').selectedIndex = this.currMonth;
			}
			nextMonth = this.currMonth+1;
			break;
		}
	}
	nextMonthTitleDiv = document.getElementById('yfcMiniCalNextMonthTitle');
	nextMonthTitleDiv.innerHTML = this.months[nextMonth].dropDownLabel;
	var sdow = this.months[this.currMonth].startDOW;
	var lday = this.months[this.currMonth].lastDay;
	var mon = this.months[this.currMonth].mon;
	var yr = this.months[this.currMonth].yr;
	var today = (this.currMonth == 0)? this.today:0;

	var isSelMonth = (this.months[this.currMonth].mon == selMon);
	var isStartMon = (this.months[this.currMonth].mon == startMon);
	var isSelYear = (this.months[this.currMonth].yr == selYear);
	var isStartYear = (this.months[this.currMonth].yr == startYear);
	var href = '<a href="javascript:void(0);" onClick="yfcMiniCal.updateField(\''+this.currField+'\',\''+mon+'\',\'';
	var md = 1;
	var nextMd = 1;
	for (i = 0; i < 42; i++) {
		dayDiv = document.getElementById('yfcMiniCalDayDiv'+Number(i));
		dayDiv.className = 'yfcMiniCalDay' + 
						( (md >= today || this.disableSelectbox) ? 
							( (md > lday) ? 
								'Next' 
								: ( ((md == startDay) && isStartMon && isStartYear) ?
                                                                        'Start'
									: ( ((md == selDay) && isSelMonth && isSelYear) ? 
										'Sel' 
										: ''
									) 
								)
							)
							: (this.disableSelectbox ? '' : 'Past')
						);
		if (i < sdow || md > lday) {
			dayDiv.innerHTML = '&nbsp;';
		}
		else {
			dayDiv.innerHTML = (md < today && !this.disableSelectbox)? md : href+((md > 9)? md :'0'+md)+'\',\''+yr+'\'); return false;">'+md+'</a>';
			md++;
		}
	}

	sdow = this.months[nextMonth].startDOW;
	lday = this.months[nextMonth].lastDay;
	mon = this.months[nextMonth].mon;
	yr = this.months[nextMonth].yr;
	today = 0;

	isSelMonth = (this.months[nextMonth].mon == selMon);
	isStartMon = (this.months[nextMonth].mon == startMon);
	isSelYear = (this.months[nextMonth].yr == selYear);
	isStartYear = (this.months[nextMonth].yr == startYear);
	href = '<a href="javascript:void(0);" onClick="yfcMiniCal.updateField(\''+this.currField+'\',\''+mon+'\',\'';
	md = 1;
	nextMd = 1;
	for (i = 0; i < 42; i++) {
		dayDiv = document.getElementById('yfcMiniCalDayDivNext'+Number(i));
		dayDiv.className = 'yfcMiniCalDay' +
			( (md >= today || this.disableSelectbox) ?
				( (md > lday) ?
					'Next'
					: ( ((md == startDay) && isStartMon && isStartYear) ?
						'Start'
						: ( ((md == selDay) && isSelMonth && isSelYear) ?
							'Sel'
							: ''
						)
					)
				)
				: (this.disableSelectbox ? '' : 'Past')
			);
		if (i < sdow || md > lday) {
			dayDiv.innerHTML = '&nbsp;';
		}
		else {
			dayDiv.innerHTML = (md < today && !this.disableSelectbox)? md : href+((md > 9)? md :'0'+md)+'\',\''+yr+'\'); return false;">'+
				md+'</a>';
			md++;
		}
	}

	this.updateArrows();
	delete(dayDiv);
}

function yfcMiniCalInitCalendar() {
	var wkDiv = document.getElementById('yfcMiniCalWeekdays');
	var wkDivNext = document.getElementById('yfcMiniCalWeekdaysNext');
	if (!wkDiv || !wkDivNext) return;
  	var day = 0;
	var newDay;
	
	while (day < 42) {
		newDay = document.createElement('div');
		newDay.id = 'yfcMiniCalDayDiv'+Number(day);
		newDay.className = 'yfcMiniCalDay';
		wkDiv.appendChild(newDay);
                newDay = document.createElement('div');
                newDay.id = 'yfcMiniCalDayDivNext'+Number(day);
                newDay.className = 'yfcMiniCalDay';
                wkDivNext.appendChild(newDay);
		day++;
	}
	
	newClr = document.createElement('br');
	newClr.className = 'ygcl';
	wkDiv.appendChild(newClr);
}

function yfcMiniCalField(formName,fldName,depFldName,depInc) {
	this.formName = formName;
	this.fldName = fldName;
	this.depFldName = depFldName;
	this.depInc = (depInc == undefined)? 3 : depInc;
	this.depSet = false;
}

function yfcMiniCalAddField(formName,fldName,depFldName,depInc) {
	this.fields[formName+this.formFieldSeparator+fldName] = new yfcMiniCalField(formName,fldName,depFldName,depInc);
}

function yfcMiniCalUpdateField(fld,m,d,y) {
	if (!this.fields[fld]) return;
	var formName = this.fields[fld].formName;
	var fldName = this.fields[fld].fldName;
	if (this.dayMonthDate) {
		document.forms[formName][fldName].value = d+'/'+m+'/'+y;
	} else {
		document.forms[formName][fldName].value = m+'/'+d+'/'+y;
	}
	if (document.forms[formName][fldName].onchange)
		document.forms[formName][fldName].onchange();
	var depFldName = this.fields[fld].depFldName;
	if (!depFldName || this.fields[fld].depSet) {
		this.closeCal();
		document.forms[formName][fldName].focus();
		return;
	}
	var depDate = new Date(Number(y),m - 1,d);
	var tmp = document.forms[formName][depFldName].value;
	var slash = tmp.indexOf('/');
	var lastSlash = tmp.lastIndexOf('/');
	var ry = (Number(tmp.substring(lastSlash+1)) % 100); // this way we support both 4 and 2 digits year format
	if (this.dayMonthDate) {
		var rd = tmp.substring(0,slash);
		var rm = tmp.substring(slash+1,lastSlash);
	} else {
		var rm = tmp.substring(0,slash);
		var rd = tmp.substring(slash+1,lastSlash);
	}
	var retDate = new Date(ry,rm-1,rd);
	var validDate = false;
	if (ry>=0 && parseInt(ry)>=0 && rm>=0 && parseInt(rm)>=0 && rd>=0 && parseInt(rd)>=0) validDate = true;
	if (!validDate || retDate<=depDate) {
		depDate.setDate(depDate.getDate()+this.fields[fld].depInc);
		m = depDate.getMonth() + 1;
		if (m < 10) m = '0'+m;
		d = depDate.getDate();
		if (d < 10) d = '0'+d;
		y = depDate.getFullYear().toString();
		//y = y.substr(2,2);
		if (this.dayMonthDate) {
			document.forms[formName][depFldName].value = d+'/'+m+'/'+y;
		} else {
			document.forms[formName][depFldName].value = m+'/'+d+'/'+y;
		}
	}
	document.forms[formName][fldName].focus();
	this.closeCal();
}

function yfcMiniCalMonth(mon,yr,startDOW,lastDay,dropDownLabel) {
	this.mon = mon;
	this.yr = yr;
	this.startDOW = startDOW;
	this.lastDay = lastDay;
	this.dropDownLabel = dropDownLabel;
}

function yfcMiniCalAddMonth(mon,yr,startDOW,lastDay,dropDownLabel) {
	this.months.push(new yfcMiniCalMonth(mon,yr,startDOW,lastDay,dropDownLabel));
}

function yfcMiniCalSetMonth(objId) {
	this.currMonth = objId.selectedIndex;
	this.showMonth();
}

function yfcToggleDisplay(fldDiv,fld,startFld, xOffset, yOffset) {
	if (this.displayed[fld]) {
		this.closeCal();
	} else {
		this.openCal(fldDiv,fld,startFld,xOffset,yOffset);
	}
}

function yfcMiniCalClass() {
	this.months = new Array();
	this.fields = new Array();
	this.currMonth = 0;
	this.maxMonth = 11;
	this.currField = undefined;
	this.today = 0;
	this.formFieldSeparator = '_';
	this.displayed = new Array();
	this.init = yfcMiniCalInitCalendar;
	this.prevMonth = yfcMiniCalPrevMonth;
	this.nextMonth = yfcMiniCalNextMonth;
	this.closeCal = yfcMiniCalCloseCal;
    this.closeCalHandler = undefined;
	this.openCal = yfcMiniCalOpenCal;
	this.toggleDisplay  = yfcToggleDisplay;
	this.addField = yfcMiniCalAddField;
	this.addMonth = yfcMiniCalAddMonth;
	this.showMonth = yfcMiniCalShowMonth;
	this.setMonth = yfcMiniCalSetMonth;
	this.updateArrows = yfcMiniCalUpdateArrows;
	this.updateField = yfcMiniCalUpdateField;
	this.dayMonthDate = false;
}


/*
 * yfcMiniCalAttachFields - 
 *      Attaches onclick handlers for the edit field/calendar image.
 *      This function replaces initialCalendarOnload() from Tiger's version
 *      of yfcMiniCal.
 * 
 * Required:
 * start_item, end_item - HTML input fields for dates
 * start_cal, end_cal   - Element containing calendar image
 * start_div, end_div   - Div containing input field/calendar image
 * form_name            - Form containing calendar
 * form_symbol          - Seperator character used by yfcMiniCal.openCal
 * 
 * Optional:
 * close_cal_handler    - This function is called when the calendar is closed
 * display_handler      - This function is called when the calendar is opened (e.g. when the user clicks on the input field)
 * toggle_handler       - This function is called when the calendar is 'toggled' (e.g. when the user clicks on the calendar image)
 * .*Offset             - Adjust the positioning of the calendar widget
 */
function yfcMiniCalAttachFields(start_item, end_item, start_cal, end_cal, start_div, end_div, form_name, form_symbol, 
                                close_cal_handler, display_handler, toggle_handler, xStartOffset, yStartOffset, xEndOffset, yEndOffset) {

    var start_date = document.getElementById(start_item);
    var end_date = document.getElementById(end_item);
    var start_date_Cal = document.getElementById(start_cal);
    var end_date_Cal = document.getElementById(end_cal);
    var form_start_item = form_name + form_symbol + start_item;
    var form_end_item = form_name + form_symbol + end_item;

    var f;

    if (close_cal_handler) {
        yfcMiniCal.closeCalHandler = close_cal_handler;
    }

    if (start_date && start_date.type != 'hidden')
    {
        f = function () { yfcMiniCal.openCal(start_div,form_start_item,null,xStartOffset,yStartOffset); if(display_handler) { display_handler(start_item); }};

        start_date.onclick =f;

        if (start_date_Cal)
        {
            f = function () { yfcMiniCal.toggleDisplay(start_div,form_start_item,null,xStartOffset,yStartOffset); if(toggle_handler) { toggle_handler(start_item); }};
            start_date_Cal.onclick = f;
        }
    }

    if (end_date && end_date.type != 'hidden')
    {
        f = function () { yfcMiniCal.openCal(end_div,form_end_item,form_start_item,xEndOffset,yEndOffset); if(display_handler) { display_handler(end_item); }};
        end_date.onclick = f;

        if (end_date_Cal)
        {
            f = function () { yfcMiniCal.toggleDisplay(end_div,form_end_item,form_start_item,xEndOffset,yEndOffset); if(toggle_handler) { toggle_handler(end_item); }};
            end_date_Cal.onclick = f;
        }
    }
}

var yfcMiniCal = new yfcMiniCalClass();

