var monthNameArray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var daysOfWeek = new Array("S","M","T","W","T","F","S");
var searchCal;
var maxMonth = 8;
var minMonth = 4;
// global functions
function addDebug() {
 	return;
	var dd = document.createElement("div");
	dd.setAttribute("id","debugDiv");
	dd.style.position = "absolute";
	dd.style.top = "10px";
	dd.style.left = "10px";
	dd.style.width = "200px";
	dd.style.height = "100px";
	dd.style.overflow = "auto";
	dd.style.background = "#cccccc";
	// dd.style.background = "#f4fafa";
	document.body.appendChild(dd);
}
function debug(textIn) {
	try {
		var db = document.getElementById("debugDiv");
		var curText = db.innerHTML;
		db.innerHTML = textIn+"<br/>"+curText;	
	}
	catch (excep) {
	}
}
function disableSelection(element,cursorType) {
	try {
		element.onselectstart = function() {
			return false;
		};
	}
	catch (excep) {
	}
	try {
		element.unselectable = "on";
	}
	catch (excep) {
	}
	try {
		element.style.MozUserSelect = "none";
	}
	catch (excep) {
	}
	try {
		element.style.cursor = cursorType;
	}
	catch (excep) {
	}
}

function drawCalendar(month,year) {
	searchCal = new calendar(month,year);	
}
// end of global functions



function calendar(monthIn,yearIn) {
	// lert("draw calendar: "+monthIn);
	this.month = monthIn;
	this.year  = yearIn;
	this.myDiv  = document.getElementById("calendarbox");
	disableSelection(this.myDiv,"default");
	this.myDiv.innerHTML = "";
// style stuff; change if you want to 
	this.colWidth = 18;
	this.rowHeight = 15;
	this.selecting = false;
	this.firstSelection = null;
// now draw the sucker
	this.dateArray = new Array();
	this.dateSquareArray = new Array();
	this.selectedIndexArray = new Array();
	this.draw();
}


calendar.prototype.draw = function() {
	var monthTitle = document.createElement("h3");
	monthTitle.appendChild(document.createTextNode(monthNameArray[this.month]));
	this.myDiv.appendChild(monthTitle);
	monthTitle.onmouseup = function(e) {
		// ebug("mouse month calendar!");
		searchCal.clearSelected();
	}
// navigation
	var navDiv = document.createElement("div");
	navDiv.setAttribute("id","calendar-nav");
// previous stuff
	var prevAnc = document.createElement("a");
	prevAnc.setAttribute("href","#");		
// IE Sucks!!!
	var prevMonth = this.month-1;
	var prevYear = this.year;
	if (prevMonth < 0) {
		prevMonth = 11;
		prevYear = this.year - 1;
	}
	prevAnc.onclick = function() {
		drawCalendar(prevMonth,prevYear);
		return false;
	}
	prevAnc.setAttribute("onmouseout","msir();");		
	prevAnc.setAttribute("onmouseout","msir('previous_o','','images/events/previous_o.gif',1);");		
	var prevImg = document.createElement("img");
	prevImg.setAttribute("src","images/events/previous.gif");
	prevImg.setAttribute("alt","previous");
	prevImg.setAttribute("border","0");
	prevImg.setAttribute("id","previous_o");
	prevAnc.appendChild(prevImg);
	if (prevMonth > minMonth) {
		navDiv.appendChild(prevAnc);
	}

// next stuff
	var nextAnc = document.createElement("a");
	nextAnc.setAttribute("href","#");		
	nextAnc.setAttribute("onmouseout","msir();");		
	nextAnc.setAttribute("onmouseout","msir('next_o','','images/events/next_o.gif',1);");		
// IE Sucks!!!
	var nextYear = this.year;
	var nextMonth = this.month+1;
	if (nextMonth > 11) {
		nextMonth = 0
		nextYear = this.year + 1;
	}
	nextAnc.onclick = function() {
		drawCalendar(nextMonth,nextYear);
		return false;
	}
	var nextImg = document.createElement("img");
	nextImg.setAttribute("src","images/events/next.gif");
	nextImg.setAttribute("alt","next");
	nextImg.setAttribute("border","0");
	nextImg.setAttribute("id","next_o");
	nextAnc.appendChild(nextImg);
	if (nextMonth < maxMonth) {
		navDiv.appendChild(nextAnc);
	}
	this.myDiv.appendChild(navDiv);


	
	for (var i=0;i<daysOfWeek.length;i++) {
		var ds = document.createElement("div");
		disableSelection(ds,"cursor");
		ds.appendChild(document.createTextNode(daysOfWeek[i]));
		ds.style.width = this.colWidth+"px";
		ds.style.textAlign = "center";
		ds.style.position = "absolute";
		ds.style.left = (10 + this.colWidth*i)+"px";
		ds.style.top = "20px";
		ds.style.color = "#47B9C9";
		this.myDiv.appendChild(ds);
	}
	this.addDays();
	this.drawDays();
}
calendar.prototype.addDays = function() {
	this.startDate = new Date();
	this.startDate.setFullYear(this.year,this.month,1);
	this.startDate.setHours(5);
	this.startDate.setMinutes(0);
	this.startDate.setSeconds(0);
	var startTime = this.startDate.getTime();
	for (var i=this.startDate.getDay();i>0;i--) {
		var curDate = new Date();
		curDate.setTime(startTime - 3600*1000*24*i*1);
		this.dateArray.push(curDate);
	}
	for (var i=0;i<31;i++) {
		var curDate = new Date();
		curDate.setTime(startTime + 3600*1000*24*i);
		if (curDate.getMonth() == this.month) {
	       		this.dateArray.push(curDate);
		}
	}
	this.weekCount = this.dateArray.length/7;
}
calendar.prototype.drawDays = function() {
	var curDay = 0;
	var curY = 34;
	for (var i=0;i<this.weekCount;i++) {
		for (var j=0;j<7;j++) {
			if (curDay < this.dateArray.length) {
				curDate = this.dateArray[curDay];
				if (curDate.getMonth() == this.month) {
					var dSquare = new dateSquare(curDate,this.dateSquareArray.length);
					var ds = dSquare.draw();
					var cy = Number(curDate.getYear());
					if (cy < 2000) {
						cy += 1900;
					}
					if (
						(curDate.getMonth() * 1 == currentMonth * 1) 
						&& (curDate.getDate() * 1 == currentDate * 1) 
						&& (cy * 1 == currentYear * 1) 
					) {
						// ebug(curDate.getYear()+"  "+currentYear);
						ds.style.color = "#47B9C9";
					}
					ds.style.position = "absolute";
					ds.style.width = this.colWidth+"px";
					ds.style.left = (10 + this.colWidth*j)+"px";
					ds.style.top = curY+"px";
					this.dateSquareArray.push(dSquare);
					this.myDiv.appendChild(ds);
				}
				curDay++;
			}
		}
		curY += this.rowHeight;
	}
}
calendar.prototype.startSelect = function(indexIn) {
	// alert("string select: "+this.dateArray[indexIn]);
	this.clearSelected();
	document.onmouseup = function() {
		searchCal.stopSelect();
		searchCal.performSearch();
	}
	this.selecting = true;
	this.firstSelection = indexIn;
	this.dateSquareArray[indexIn].myDiv.style.background = "#cccccc";
	// this.dateSquareArray[indexIn].myDiv.style.background = "#f4fafa";
	this.selectedIndexArray = new Array();
	this.selectedIndexArray.push(indexIn);
	// ebug("step 1");
	this.setDateRange(this.dateSquareArray[indexIn].myDate,this.dateSquareArray[indexIn].myDate);
}
calendar.prototype.changeSelection = function(indexIn) {
	// ebug(this.firstSelection+" "+indexIn);
	this.selectedIndexArray = new Array();
	this.clearSelected();
	if (this.firstSelection > indexIn) {
		for (var i=indexIn;i<=this.firstSelection;i++) {
			this.dateSquareArray[i].myDiv.style.background = "#cccccc";
			// this.dateSquareArray[i].myDiv.style.background = "#f4fafa";
			this.selectedIndexArray.push(i);
		}
	}
	else {
		for (var i=this.firstSelection;i<=indexIn;i++) {
			this.dateSquareArray[i].myDiv.style.background = "#cccccc";
			// this.dateSquareArray[i].myDiv.style.background = "#f4fafa";
			this.selectedIndexArray.push(i);
		}
	}
	this.setDateRange(this.dateSquareArray[this.selectedIndexArray[0]].myDate,this.dateSquareArray[this.selectedIndexArray[this.selectedIndexArray.length-1]].myDate);
	// ebug("step 2"+indexIn);
}
calendar.prototype.setDateRange = function(startDate,endDate) {
	// ebug("set date range: "+startDate+" "+endDate);
	var startMonth = startDate.getMonth() + 1;
	if (startMonth < 10) {
		startMonth = "0"+startMonth;
	}
	var startDt = startDate.getDate();
	if (startDt < 10) {
		startDt = "0"+startDt;
	}
	var startNice = startDate.getFullYear()+"-"+startMonth+"-"+startDt;

	var endMonth = endDate.getMonth() + 1;
	if (endMonth < 10) {
		endMonth = "0"+endMonth;
	}
	var endDt = endDate.getDate();
	if (endDt < 10) {
		endDt = "0"+endDt;
	}
	var endNice = endDate.getFullYear()+"-"+endMonth+"-"+endDt;
	// ebug("1: "+startNice+"  "+endNice);
	document.getElementById("sDate").value = startNice;
	document.getElementById("eDate").value = endNice;
}
calendar.prototype.stopSelect = function() {
	// ebug("mouse up!");
	this.selecting = false;
	document.onmouseup = null;
}
calendar.prototype.clearSelected = function() {
	document.getElementById("sDate").value = "";
	document.getElementById("eDate").value = "";
	for (var i=0;i<this.dateArray.length;i++) {
		try {
			// this.dateSquareArray[i].myDiv.style.background = "#f2f4f2";
			this.dateSquareArray[i].myDiv.style.background = "#f4fafa";
		}
		catch(excep) {
		}
	}
}
calendar.prototype.selectDates = function(startDate,endDate) {
	var dateParts = startDate.split("-");
	var dateParts2 = endDate.split("-");
	// ebug("select dates! "+dateParts[1]+" "+dateParts2[1]);
	for (var i=0;i<this.dateSquareArray.length;i++) {
		if (this.dateSquareArray[i].myDate.getDate() * 1 == dateParts[2] * 1) {
			this.startSelect(i);
		}
	}
	// var dateParts = endDate.split("-");
	if (dateParts2[1] * 1 > dateParts[1] * 1) {
		this.changeSelection(this.dateSquareArray.length-1);
	}
	else {
		for (var i=0;i<this.dateSquareArray.length;i++) {
			if (this.dateSquareArray[i].myDate.getDate() * 1 == dateParts2[2] * 1) {
				this.changeSelection(i);
			}
		}
	}
	this.stopSelect();
}
calendar.prototype.performSearch = function() {
	// debug("searching? "+document.getElementById("sDate").value);
//	var t=setTimeout("searchCal.searchAfterDelay();",100);
 	document.getElementById("searchForm").submit();

}
calendar.prototype.searchAfterDelay = function() {
	// debug("search after delay: "+document.getElementById("sDate").value);
 	document.getElementById("searchForm").submit();
}
// start of date square class
function dateSquare(dateIn,indexIn) {
	this.myDate = dateIn;
	this.index = indexIn;
}
dateSquare.prototype.draw = function() {
	this.myDiv = document.createElement("div");
	this.myDiv.setAttribute("id","ds_"+this.index);
	this.myDiv.style.textAlign = "center";
	this.myDiv.appendChild(document.createTextNode(this.myDate.getDate()));
	var dateIndex = this.index;
	this.myDiv.onmousedown = function(e) {
// window.event.shiftKey
		evt = (e) ? e : ((window.event) ? window.event : "");
        	var shiftPressed=evt.shiftKey;
		var ctrlPressed =evt.ctrlKey;
		if (((shiftPressed)||(ctrlPressed))&&(searchCal.firstSelection != null)) {
				// ebug("change 1");
			searchCal.changeSelection(dateIndex);
		}
		else {
			searchCal.startSelect(dateIndex);
		}
	}
	this.myDiv.onmouseover = function(e) {
		if (searchCal.selecting) {
				// ebug("change 2");
			searchCal.changeSelection(dateIndex);
		}
	}
	disableSelection(this.myDiv,"pointer");
	return this.myDiv;
}


