
dojo.require("dojo.fx");

var __currentSection = null;

dojo.addOnLoad(function() {
	dojo.query(".hidden_section").style("display", "none");
	dojo.query(".show_section").onclick(function(e) {
		dojo.stopEvent(e);
		var hashInd = this.href.indexOf("#");
		if (hashInd === -1) {
			return;
		}
		var sectionName = this.href.substring(hashInd + 1);
		var sectionId = "section_" + sectionName;

		if (sectionId == __currentSection) {
			var anim = dojo.fx.wipeOut({node: __currentSection, rate: 80});
			__currentSection = null;
			anim.play();
			return;
		}
		
		var anim = dojo.fx.wipeIn({node: sectionId, rate: 80});
		if (__currentSection !== null) {
			anim = dojo.fx.combine([anim, dojo.fx.wipeOut({node: __currentSection, rate: 80})]);
		}
		anim.play();
		__currentSection = sectionId;
	});
	dojo.query("#contact .hidden_section .cancel").onclick(function(e) {
		dojo.stopEvent(e);
		updateCalendarSelection(null);
	});
	dojo.query("#contact .calendar").forEach(function(o) {
		loadDateMonth(o);
		dojo.query(".prev_month a", o).onclick(function(e) {
			dojo.stopEvent(e);
			loadDateMonth(o, o.curDate.getMonth() - 1);
		});
		dojo.query(".next_month a", o).onclick(function(e) {
			dojo.stopEvent(e);
			loadDateMonth(o, o.curDate.getMonth() + 1);
		});
	});
});

var __g_monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var __g_strCalendarDates = {};

function updateCalendarSelection(oDate) {
	var oField = dojo.byId("contactus_date_selected");
	oField.value = (!oDate) ? "" : dateToString(oDate);

	dojo.attr("appt", "innerHTML", (!oDate) ? "No appointment currently requested" : "You've selected " + oField.value + " for an appointment");
	dojo.attr("appt_click", "innerHTML", (!oDate) ? "Click to request an appointment" : "Click to change your request");

	if (__currentSection == "section_appointment") {
		var anim = dojo.fx.wipeOut({node: __currentSection, rate: 80});
		__currentSection = null;
		anim.play();
	}
}

function parseDate(str) {
	var parts = dojo.trim(str).split("-");
	var year  = parseInt(parts[0], 10);
	var month = parseInt(parts[1], 10);
	var date  = parseInt(parts[2], 10);
	return new Date(year, month - 1, date);
}

function dateToString(oDate) {
	var year  = oDate.getFullYear();
	var month = oDate.getMonth() + 1;
	var date  = oDate.getDate();
	if (month < 10) {
		month = "0" + month;
	}
	if (date < 10) {
		date = "0" + date;
	}
	return year + "-" + month + "-" + date;
}

function isAvailable(oDate) {
	//Sunday and Saturday
	var str = dateToString(oDate);
	if (__g_strCalendarDates && __g_strCalendarDates[str]) {
		return (__g_strCalendarDates[str] == "0");
	}
	if (oDate.getDay() == 0 || oDate.getDay() == 6) {
		return false;
	}
	return true;
}

function loadDateMonth(oCal, iMonth) {
	var dispDate = new Date();
	dispDate.setDate(1);
	if (!iMonth && iMonth !== 0) {
		var oField = dojo.byId("contactus_date_selected");
		if (oField && dojo.trim(oField.value).length != 0) {
			dispDate = parseDate(oField.value);
		}
	}
	else if (oCal.curDate) {
		dispDate = oCal.curDate;
		dispDate.setMonth(iMonth);
	}
	var strDays = "";
	var tmpDate = new Date(dispDate.getFullYear(), dispDate.getMonth(), 1);
	var offset  = tmpDate.getDay();

	//offset from Monday instead of Sunday...
	if (offset == 0) {
		offset = 7;
	}
	--offset;
	
	for (var i = 0; i < offset; ++i) {
		strDays += "<div>&nbsp;</div>";
	}

	var today = new Date();
	today.setHours(0);
	today.setMinutes(0);
	today.setSeconds(0);
	
	while (tmpDate.getMonth() == dispDate.getMonth()) {
		var strHtml = "<div>";
		if (tmpDate > today && isAvailable(tmpDate)) {
			strHtml += "<a href=\"#day_" + tmpDate.getDate() + "\">" + tmpDate.getDate() + "</a>";
		}
		else {
			strHtml += tmpDate.getDate();
		}
		strHtml += "</div>";
		strDays += strHtml;
		tmpDate.setDate(tmpDate.getDate() + 1);
	}
	dojo.query(".month_name", oCal).attr("innerHTML", __g_monthNames[dispDate.getMonth()] + ", " + dispDate.getFullYear());
	dojo.query(".days", oCal).attr("innerHTML", strDays);
	dojo.query(".days a", oCal).onclick(function(e) {
		dojo.stopEvent(e);
		var hashInd = this.href.indexOf("#day_");
		if (hashInd === -1) {
			return;
		}
		var day = parseInt(this.href.substring(hashInd + 5));
		var oField = dojo.byId("contactus_date_selected");
		dispDate.setDate(day);
		updateCalendarSelection(dispDate);
	});
	oCal.curDate = dispDate;
}

