// Dual Calendar

YAHOO.widget.PopDateRangeCalendar = function(startId, endId, startContainer, endContainer, pageCount, dateOffset) {
	if (arguments.length > 0) {
		this.init(startId, endId, startContainer, endContainer, pageCount, dateOffset);
	}
};

YAHOO.widget.PopDateRangeCalendar.prototype = {
	inputStartDate : null,
	inputEndDate : null,
	calStartDate : null,
	calEndDate : null,
	formatDate : function(d) {
		var str = d.getFullYear() + "-";
		if (d.getMonth() < 9) {
			str += "0"
		}
		str += (d.getMonth() + 1) + "-";
		if (d.getDate() < 10) {
			str += "0"
		}
		str += d.getDate();
		return str;
	}
};

YAHOO.widget.PopDateRangeCalendar.prototype.init = function(startId, endId, startContainer, endContainer, pageCount, dateOffset) {
	this.inputStartDate = document.getElementById(startId);
	if (this.inputStartDate == null) {
		alert("Luopan Error: can not find " + startId);
		return;
	}
	this.inputEndDate = document.getElementById(endId);
	if (this.inputEndDate == null) {
		alert("Luopan Error: can not find " + endId);
		return;
	}
	if (dateOffset == undefined) {
		dateOffset = 0;
	}

	if (pageCount == 1) {
		this.calStartDate = new YAHOO.widget.Calendar(startId + '_table', startContainer, {close:true, title:startDateTitle, mindate:YAHOO.widget.DateMath.add(new Date(), YAHOO.widget.DateMath.DAY, dateOffset)});
	} else {
		this.calStartDate = new YAHOO.widget.CalendarGroup(startId + '_table', startContainer, {pages:pageCount, close:true, title:startDateTitle, mindate:YAHOO.widget.DateMath.add(new Date(), YAHOO.widget.DateMath.DAY, dateOffset)});
	}
	this.calStartDate.selectEvent.subscribe(this.setStartDate, this, true);
	resetCalendarLocale(this.calStartDate);
	this.calStartDate.render();
	if (pageCount == 1) {
		this.calEndDate = new YAHOO.widget.Calendar(endId + '_table', endContainer, {close:true, title:endDateTitle, mindate:YAHOO.widget.DateMath.add(new Date(), YAHOO.widget.DateMath.DAY, dateOffset+1)});
	} else {
		this.calEndDate = new YAHOO.widget.CalendarGroup(endId + '_table', endContainer, {pages:pageCount, close:true, title:endDateTitle, mindate:YAHOO.widget.DateMath.add(new Date(), YAHOO.widget.DateMath.DAY, dateOffset+1)});
	}
	this.calEndDate.selectEvent.subscribe(this.setEndDate, this, true);
	resetCalendarLocale(this.calEndDate);
	this.calEndDate.render();

	YAHOO.util.Event.addListener(this.inputStartDate, "focus", this.showCalStartDate, this);
	YAHOO.util.Event.addListener(this.inputEndDate, "focus", this.showCalEndDate, this);
};

YAHOO.widget.PopDateRangeCalendar.prototype.showCalStartDate = function(e, obj) {
	obj.calEndDate.hide();
	var pos = YAHOO.util.Dom.getXY(this);
	obj.calStartDate.oDomContainer.style.display="block";
	YAHOO.util.Dom.setXY(obj.calStartDate.oDomContainer, [pos[0],pos[1]+this.offsetHeight+1]);
};

YAHOO.widget.PopDateRangeCalendar.prototype.showCalEndDate = function(e, obj) {
	obj.calStartDate.hide();
	var pos = YAHOO.util.Dom.getXY(this);
	var posStart = YAHOO.util.Dom.getXY(obj.inputStartDate);
	obj.calEndDate.oDomContainer.style.display="block";
	if (pos[0] == posStart[0]) {
		YAHOO.util.Dom.setXY(obj.calEndDate.oDomContainer, [pos[0],pos[1]+this.offsetHeight+1]);
	} else {
		YAHOO.util.Dom.setXY(obj.calEndDate.oDomContainer, [pos[0]+this.offsetWidth-obj.calEndDate.oDomContainer.offsetWidth,pos[1]+this.offsetHeight+1]);
	}
};

YAHOO.widget.PopDateRangeCalendar.prototype.setStartDate = function(e, obj) {
	var selectedDate = this.calStartDate.getSelectedDates()[0];
	this.inputStartDate.value = this.formatDate(selectedDate);

	var autoEndDate = YAHOO.widget.DateMath.add(selectedDate, YAHOO.widget.DateMath.DAY, 3);
	this.inputEndDate.value = this.formatDate(autoEndDate);
	this.calEndDate.cfg.setProperty("pagedate", autoEndDate);
	this.calEndDate.cfg.setProperty("mindate", YAHOO.widget.DateMath.add(selectedDate, YAHOO.widget.DateMath.DAY, 1));
	this.calEndDate.render();

	this.calStartDate.hide();
	this.inputEndDate.focus();
};

YAHOO.widget.PopDateRangeCalendar.prototype.setEndDate = function(e, obj) {
	var selectedDate = this.calEndDate.getSelectedDates()[0];
	this.inputEndDate.value = this.formatDate(selectedDate);
	this.calEndDate.hide();
};


//Single Calendar

YAHOO.widget.PopDateCalendar = function(startId, startContainer, pageCount) {
	if (arguments.length > 0) {
		this.init(startId, startContainer, pageCount);
	}
};

YAHOO.widget.PopDateCalendar.prototype = {
	inputStartDate : null,
	calStartDate : null,
	formatDate : function(d) {
		var str = d.getFullYear() + "-";
		if (d.getMonth() < 9) {
			str += "0"
		}
		str += (d.getMonth() + 1) + "-";
		if (d.getDate() < 10) {
			str += "0"
		}
		str += d.getDate();
		return str;
	}
};

YAHOO.widget.PopDateCalendar.prototype.init = function(startId, startContainer, pageCount) {
	this.inputStartDate = document.getElementById(startId);
	if (this.inputStartDate == null) {
		alert("Luopan Error: can not find " + startId);
		return;
	}

	if (pageCount == 1) {
		this.calStartDate = new YAHOO.widget.Calendar(startId + '_table', startContainer, {close:true});
	} else {
		this.calStartDate = new YAHOO.widget.CalendarGroup(startId + '_table', startContainer, {pages:pageCount, close:true});
	}
	this.calStartDate.selectEvent.subscribe(this.setStartDate, this, true);
	resetCalendarLocale(this.calStartDate);
	this.calStartDate.render();

	YAHOO.util.Event.addListener(this.inputStartDate, "focus", this.showCalStartDate, this);
};

YAHOO.widget.PopDateCalendar.prototype.showCalStartDate = function(e, obj) {
	var pos = YAHOO.util.Dom.getXY(this);
	obj.calStartDate.oDomContainer.style.display="block";
	YAHOO.util.Dom.setXY(obj.calStartDate.oDomContainer, [pos[0],pos[1]+this.offsetHeight+1]);
};

YAHOO.widget.PopDateCalendar.prototype.setStartDate = function(e, obj) {
	var selectedDate = this.calStartDate.getSelectedDates()[0];
	this.inputStartDate.value = this.formatDate(selectedDate);

	this.calStartDate.hide();
};