var priorEditionCal = null;

function showPriorEditionCal(anchorObj) {
	if( priorEditionCal == null ) {
		priorEditionCal = new CalendarPopup();
		priorEditionCal.addDisabledDates(null, "2005-08-01");
		priorEditionCal.addDisabledDates("tomorrow", null);
		priorEditionCal.setReturnFunction("priorDateSelected");
	}

	var qDate = getQueryValue("storyDate");

	if( qDate == "" ) {
		qDate = document.forms['priorEdForm'].elements['hiddenPriorEditionDate'].value;
	} else {
	var msInDay = 60 * 60 * 24 * 1000;
		var today = new Date();
		today = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);
		var ts = today.getTime();

		if( qDate == "yesterday" ) {
			qDate = new Date(ts - msInDay);
		} else if( qDate == "tomorrow" ) {
			qDate = new Date(ts + msInDay);
		} else {
			try {
				qDate = new Date(getDateFromFormat(qDate, priorDateFormat));
			} catch( diediedie ) {
				qDate = "";
			}
		}
	}

	priorEditionCal.showCalendar(anchorObj.name, qDate);
	return false;
}

function priorDateSelected(y,m,d) {
	var yr = parseInt(y);
	var mo = parseInt(m);
	var da = parseInt(d);

	if( isNaN(yr) || isNaN(mo) || isNaN(da) )
		return;

	if( yr < 2005 || mo < 1 || da < 1 || mo > 12 || da > 31 )
		return;

	var dt = new Date(y, m-1, d, 0, 0, 0);

	var today = new Date();
	today = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);
	var url = priorDateUrl;

	if( dt.getTime() != today.getTime() ) {
		var dateQ = formatDate(dt, priorDateFormat);
		url += "?" + priorDateQ + "=" + dateQ;
	}

	this.location = url;
}

