var imgFalse = '/images/main/icons/false.gif';
var imgTrue = '/images/main/icons/true.gif';

function replaceChecks() {
	
	//get all the input fields on the page
	inputs = document.getElementsByTagName('input');
	
	//cycle trough the input fields
	for(var i=0; i < inputs.length; i++) {
		
		//check if the input is a checkbox
		if(inputs[i].getAttribute('type') == 'checkbox') {
			
			//create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(inputs[i].checked) {
				img.src = imgTrue;
			} else {
				img.src = imgFalse;
			}
			
			//set image ID and onclick action
			img.id = 'checkImage'+i;
			//set image 
			img.onclick = new Function('checkChange('+i+')');
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
		}
	}
}

//change the checkbox status and the replacement image
function checkChange(i) {
	if(inputs[i].checked) {
		inputs[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalse;
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrue;
	}
	cosCalendar.refresh();
}

// Start Calendar Class for COS
// Modified for Global Perspectives
// By Daniel Walker
// March, 07 2007
// Instance of CosCalendar must be called 'cosCalendar'

function CosCalendar() {
		
		//global vars
		var eventWindowWidth = 422;  //pixels
		var eventWindowHeight = 300; //pixels
		var calendarDate = null;
		var CalendarEventWindow = 'CalendarEventWindow';
		var calendarHolder = 'calendar_holder';
		var CalendarEventWindowContent = 'CalendarEventWindowContent';
		
		this.handleClickClose = function(e, obj)
		{
			obj.hideEventWindow();
		}
		
		this.construct = function()
		{
			replaceChecks();
			calendarDate = new Date();
			calendarRefresh();
			C.hideElement(CalendarEventWindow, 0);
			
			this.refresh = function()
			{
				calendarRefresh();
			}
			
			this.calendar_increment_month = function(delta)
			{
				var year = calendarDate.getFullYear();
				var month = calendarDate.getMonth();
				var day = calendarDate.getDate();
				
				if(month == 11 && delta == 1)
				{
					month = 0;
					year++;
				}
				
				else if(month == 0 && delta == -1)
				{
					month = 11;
					year --;
				}
				
				else
					month += delta;
				
				calendarDate = new Date(year, month, day);
				calendarRefresh();
			}
			
			this.showEventWindow = function(year, month, day) {
				fillContent(year, month, day);
				C.showTransBG(0.77);
				showModalWindow(CalendarEventWindow);
				YAHOO.util.Event.addListener(C.transBG, 'click', this.handleClickClose, this);
			}
			
			this.hideEventWindow = function() {
				C.hideElement(CalendarEventWindow, 0);
				C._E(CalendarEventWindow + 'Content').innerHTML = "";
				C.hideTransBG();
				YAHOO.util.Event.removeListener(C.transBG, 'click');
			}
		}
		
		function showModalWindow(eid) {
			C.setWidth(eid, eventWindowWidth);
			C.setHeight(eid, eventWindowHeight);
			C.centerElementInWindow(eventWindowWidth, eventWindowHeight, eid);
			C.showElement(eid, 1);
		}
		
		var calendarRefreshObject = {
			
			success:	function(o) {
							C._E(calendarHolder).innerHTML = o.responseText;
						},
			
			failure:	function(o) {
							C._E(calendarHolder).innerHTML = 'Calendar Not Found';
						}
		};
		
		var eventWindowRefreshObject = {
			
			success:	function(o) {
							C._E(CalendarEventWindowContent).innerHTML = o.responseText;
						},
			
			failure:	function(o) {
							C._E(CalendarEventWindowContent).innerHTML = 'Calendar Events Not Found';
						}
		};
		
		function calendarRefresh()
		{
			options		=	'year='	+	calendarDate.getFullYear();
			options		+=	'&month='	+	(calendarDate.getMonth()+1);
			filterByEventType = '&filterByEventType=';
			
			eventChecked = C._E('checkbox_events').checked;
			dvChecked = C._E('checkbox_dv').checked;
			
			if(eventChecked == true)
				filterByEventType = filterByEventType + TYPE_EVENT + '|';
			
			if(dvChecked == true)
				filterByEventType = filterByEventType + TYPE_DV;
			
			options		+=	filterByEventType;
			
			YAHOO.util.Connect.asyncRequest(
											'POST',
											'/include/CalendarWriteMonthlyCalendar.php',
											calendarRefreshObject,
											options
											);
		}
		
		function fillContent(y, m, d) {
			options	=	'year='	+y;
			options	+=	'&month='	+m;
			options	+=	'&day='	+d;
			
			YAHOO.util.Connect.asyncRequest(
											'POST',
											'/include/CalendarGetEvents.php',
											eventWindowRefreshObject,
											options
											);
		}
	
	return this.construct();
}

