// Agenda class
<!--
//Create new ajenda class
//------------------------------
var Agenda = new function() {  
//------------------------------
	this.ajax = null;
	this.proc = 'ready'; 			//states: 'ready','proc','done'
	this.currOpacity = 100;
	this.statusInterval = null;
	this.isInputDisabled = false;
	this.editAgendaId = '';
	this.editMinuteId = '';
	this.editCommentId = '';
	this.saveAgendaId = '';
	this.saveMinuteId = '';
	this.saveCommentId = '';
	this.origTitle = '';
	this.origBody = '';
	this.agendaDiv = null;
	this.optionsDiv = null;
	this.swapAgeMinDiv = null;
	this.cityHeaderDiv = null;
	this.commentHeaderDiv = null;
  	this.editInstructionsSpan = null;
	this.swapAgeMinButn = null;
	this.swapAgeMinDiv = null;
	this.printButn = null;
	this.aniLoadingSpan = false;
	this.loggedIn = false;
	this.editMinutes = false;
	//this.editComments = false;
	this.editAgenda = false;
	this.cityDiscussDiv = null;
	this.minuteArr = null;
	
  	//---------------------------------------------------------
	this.init = function() {
		var self = Agenda;
		self.ajax = new Ajax();
		self.agendaDiv = document.getElementById('agendaDiv');
		self.cityDiscussDiv = document.getElementById('cityDiscussDiv');
		self.swapAgeMinButn = document.getElementById('swapAgeMinButn');
		self.printButn = document.getElementById('printButn');
		self.swapAgeMinDiv = document.getElementById('swapAgeMinDiv');
		self.optionsDiv = document.getElementById('optionsDiv');
		self.aniLoadingSpan = document.getElementById('aniLoadingSpan');
		self.editInstructionsSpan = document.getElementById('editInstructions');		
		self.editAgenda = document.getElementById('allowedEditAgenda');
		self.editMinutes = document.getElementById('allowedEditMinutes');
		//self.editComments = document.getElementById('allowedEditComments');
		self.loggedIn = document.getElementById('logged_in');

		if(self.printButn)
			self.printButn.disabled = true;
		if(self.swapAgeMinButn)
			self.swapAgeMinButn.disabled = true;
		//check if user has edit privlidges
		if(self.loggedIn.value == 1 && self.editAgenda.value == 1){
			//build add new button if security allows it
			self.buildNewAgendaButton();
			AdminAgenda.init();
			AdminItem.init();
		}
		if(self.loggedIn.value == 1 && self.editMinutes.value == 1){
			//load minute admin functions
			MinuteAdmin.init();
		}
		self.setAlpha(); //IE fade hack
		
	};
	//---------------------------------------------------------
	this.buildNewAgendaButton = function(){
		var self = Agenda;
		var newAgendaButton = createNamedElement('input','newAgenda');
		newAgendaButton.setAttribute("type","button");
		newAgendaButton.setAttribute("id","newAgenda");
		newAgendaButton.className="adminButn";
		newAgendaButton.onclick = function () {AdminAgenda.addNewAgenda('newAgenda')};
		newAgendaButton.setAttribute("value","Create New Agenda");
		if(self.optionsDiv)
			self.optionsDiv.appendChild(newAgendaButton);
		
	};
	//---------------------------------------------------------
	this.setPrompt = function(stat, msg) {
		var self = Agenda;
		var promptDiv = self.promptDiv;
		var msgSpan = document.getElementById('msgSpan');
		var statusClass = '';
		promptDiv.className = stat + 'Prompt'; // 'base', 'proc' or 'err'
		if (msgSpan.firstChild) {
			msgSpan.removeChild(msgSpan.firstChild);
		}
		msgSpan.appendChild(document.createTextNode(msg));
	};
	
	//---------------------------------------------------------
	this.viewAgenda = function(id){
		var self = Agenda;
		//begin transition animation and set color
		self.proc = 'proc';
		self.aniLoadingSpan.style.background="#f2a646";
		
		if (self.setDisplay(false)) {
		  self.currOpacity = 100;
		  self.displayOpacity();
		  self.statusInterval = setInterval(self.doProc, 90);
		}		
		//clear set
		self.resetTop();
		self.resetBottom();
		//load agenda and comments
		var url = self.uncache('ajax_getAgenda.php?id='+id)
		self.ajax.doGet(url, self.handleResult, 'xml');		//
		
	};
	
	//----------------------------------------------------------
	this.resetTop = function() {
		var self = Agenda;
		//resets display in prep for new data
		if(document.getElementById('newMinutes'))
			self.swapAgeMinDiv.removeChild(document.getElementById('newMinutes'));
		self.printButn.disabled = true;
		self.swapAgeMinButn.disabled = true;
		//prep fields
		while(self.agendaDiv.firstChild)
			self.agendaDiv.removeChild(self.agendaDiv.firstChild);
		if(self.editInstructionsSpan.firstChild)
			self.editInstructionsSpan.removeChild(self.editInstructionsSpan.firstChild);
			
		var deleteButn = document.getElementById('deleteAgenda');
		if(deleteButn){
			deleteButn.disabled = true;
			deleteButn.onclick = function () {return};
		}
	};
	//----------------------------------------------------------
	this.resetBottom = function() {
		var self = Agenda;
		if(self.loggedIn.value == '1'){
			while(self.cityDiscussDiv.firstChild)
				self.cityDiscussDiv.removeChild(self.cityDiscussDiv.firstChild);
				
			self.cityDiscussDiv.appendChild(document.createTextNode("Load an Agenda to see comments."));	
		}
		
	};
	//----------------------------------------------------------
	this.handleResult = function(xml) {
		var self = Agenda;
		var res = [];
		//alert(xml);
		res = XMLParse.xml2ObjArray(xml, 'ResultSet');
		//alert(res[0].agendaMsg.status);
		//alert(dump(res));
		//alert('logged in value: '+self.loggedIn.value);
		//success
		if(res.length == 0){
			self.displayError(res);
		}
		else{
			if(res[0].agendaMsg.status == 'success')
				self.displayAgenda(res);
			else
				self.displayError(res);
		}
	};
	
	//----------------------------------------------------------
	this.displayAgenda = function(res) {
		var self = Agenda;
		var titleDiv = null;
		var headingDiv = null;
		var clearBoth = null;
		var agendaItemDiv = null;
		var agendaDescDiv = null;
		var itemObj = null;
		var itemDesc = null;
		var meetinType = '';
		var meetingDate = '';
		var meetingHead = '';
		
		titleDiv = document.createElement('div');
		headingDiv = document.createElement('div');
		
		agendaId = res[0].agendaID;
		meetingType = res[0].type;
		meetingDate = res[0].date;
		meetingHead = res[0].heading;
		unformattedDate = res[0].unfmtdData;	//YYYY-MM-DD
		
		titleDiv.className = 'miniHeader';
		headingDiv.className = 'emph';
		headingDiv.id = 'head'+agendaId;
		
		titleDiv.innerHTML = "<span id='type"+agendaId+"'>"+meetingType+"</span>";
		titleDiv.innerHTML += " Meeting (<span id='date"+agendaId+"'>"+meetingDate+"</span>)";
		titleDiv.innerHTML += "<input type='hidden' id='unfmtdDate' value='"+unformattedDate+"'>";

		headingDiv.appendChild(document.createTextNode(meetingHead));
		self.agendaDiv.appendChild(titleDiv);
		self.agendaDiv.appendChild(headingDiv);
			
		if(res[0].count > 0){
			for(var i=0; i<res[0].count; i++){
				agendaItemDiv = document.createElement('div');
				agendaDescDiv = document.createElement('div');
				clearBoth = document.createElement('div');
				
				agendaItemDiv.className = 'listItem';
				agendaDescDiv.className = 'note';
				clearBoth.className = 'clearBoth';
				
				itemObj = eval('res[0].items.ItemAttributes'+i+'.title');
				idObj = eval('res[0].items.ItemAttributes'+i+'.id');
				itemDesc = eval('res[0].items.ItemAttributes'+i+'.desc');
				agendaItemDiv.id = 'item'+idObj;
				agendaDescDiv.id = 'desc'+idObj;
				
				agendaItemDiv.appendChild(document.createTextNode((i+1)+'. '+itemObj));
				
				agendaDescDiv.innerHTML+=itemDesc;
				self.agendaDiv.appendChild(agendaItemDiv);
				self.agendaDiv.appendChild(agendaDescDiv);
				self.agendaDiv.appendChild(clearBoth);
			}
		}
		if(self.loggedIn.value == '1' && self.editAgenda.value == '1'){
			//create button to allow addition of more agenda items
			var newAgendaItemButnSpan = document.createElement('span');
			var newAgendaItemButn = document.createElement('input');
			var hidAgendaId = createNamedElement('input','agendaId');
			newAgendaItemButnSpan.className = "adminButn";
			newAgendaItemButn.type = 'button';
			hidAgendaId.type="hidden";
			newAgendaItemButn.className = "adminButn";
			newAgendaItemButn.value = "Edit Current Agenda";
			newAgendaItemButn.id = "editAgendaButn";
			hidAgendaId.id="agendaId";
			hidAgendaId.value=res[0].agendaID;
			newAgendaItemButn.onclick = function () {AdminAgenda.editAgenda(res[0].agendaID)};
			newAgendaItemButnSpan.appendChild(newAgendaItemButn);
			self.agendaDiv.appendChild(newAgendaItemButnSpan);
			self.agendaDiv.appendChild(hidAgendaId);
			//self.agendaDiv.innerHTML="<input type='hidden' name='agendaId' id='agendaId' value='"+res[0].agendaID+"'";
		}
		
		//enable print
		self.printButn.disabled = false;
		
		//enable minutes button if minutes exist
		self.setMinuteButton(res[0].minutes,res[0].agendaID);
		
		//enable delete button if it exists
		self.setDeleteButton(res[0].agendaID);
		
		//show comments
		//alert('logged in value: '+self.loggedIn.value);
		if(self.loggedIn.value == '1'){
			if(res[0].commentMsg.status != 'error'){
				self.displayComments(res);
			}else{
				if (self.cityDiscussDiv.firstChild) 
					self.cityDiscussDiv.removeChild(self.cityDiscussDiv.firstChild);
				
				self.cityDiscussDiv.appendChild(document.createTextNode('No Discussion Topics Set for this Meeting.'));
			}
		}
		self.proc = 'done';	
/*		if(self.loggedIn.value == 1 && self.editAgenda.value == 1){
			AdminAgenda.tempFunc();
			AdminItem.tempFunc();
		}
*/	};
	//---------------------------------------------------------
	this.setMinuteButton = function(minuteExst,agendaid){
		var self = Agenda;
		if(minuteExst == 'yes'){
			//alert("mintues button enabled");
			self.swapAgeMinButn.value = 'View Minutes';
			self.swapAgeMinButn.disabled = false;
			self.swapAgeMinButn.onclick = function () {self.getAgendaMinutes(agendaid)};
		}
		else if(self.loggedIn.value == '1' && self.editMinutes.value == '1'){
			this.buildNewMinuteButton(agendaid);
		}
	};
	//---------------------------------------------------------
	this.setDeleteButton = function(agendaid){
		var self = Agenda;
		var deleteButn = document.getElementById('deleteAgenda');
		if(deleteButn){
			deleteButn.disabled = false;
			deleteButn.onclick = function () {self.deleteAgenda(agendaid)};
		}
	};
	//---------------------------------------------------------
	this.deleteAgenda = function(id){
		var self = Agenda;
		var pmtMsg = 'You are about to delete agenda '+id+', its minutes (if any) and all ';
			pmtMsg += 'associated comments. Are you sure you want to continue?';
		if(confirm(pmtMsg)){
			var city = document.getElementById('cityHid');
			document.location.href='agendaDelete.php?agendaid='+id+'&town='+city;
		}else{
			return false;
		}
	};
	//---------------------------------------------------------
	this.buildNewMinuteButton = function(id){
		var self = Agenda;
		var newMinuteButton = createNamedElement('input','newMinutes');
		newMinuteButton.setAttribute("type","button");
		newMinuteButton.setAttribute("id","newMinutes");
		newMinuteButton.className = 'adminButn';
		newMinuteButton.onclick = function () {'newMinutes',MinuteAdmin.addNewMinutes('newMinutes',id)};
		newMinuteButton.setAttribute("value","Create Minutes for Current Meeting");
		self.swapAgeMinDiv.appendChild(newMinuteButton);
		
	};
	
	//----------------------------------------------------------
	this.displayError = function(res){
		var self = Agenda;
		if(res.length > 0){
			self.agendaDiv.appendChild(document.createTextNode(res[0].agendaMsg.message));
		}
		else{
			self.agendaDiv.appendChild(document.createTextNode('Error loading requested data.'));	
		}
		self.proc = 'done';
	};
	//----------------------------------------------------------
	this.displayComments = function(res) {
    	var self = Agenda;
		var commentWrapperDiv = null;
		var commentTitleDiv = null;
		var commentInfoDiv = null;
		var clearBoth = null;
		var curCom = '';
		var comCount = '';
		var comLink = 'g';
		var comView = 'f';
		var comIdArr = new Array();
		
		if(self.cityDiscussDiv.firstChild)
			self.cityDiscussDiv.removeChild(self.cityDiscussDiv.firstChild);
		
		for(var i=0; i<res[0].comments.count; i++){
			//dynamically build the medthod's name
			curCom = eval('res[0].comments.ItemAttributes'+i);	
			
			commentWrapperDiv = document.createElement('div');
			commentTitleDiv = document.createElement('h4');
			commentInfoDiv = document.createElement('div');
			commentRemarkDiv = document.createElement('div');

			commentWrapperDiv.id = 'topic'+curCom.commid;
			commentWrapperDiv.className = 'lftBlLine';
			commentInfoDiv.className = 'comOptions';
			commentInfoDiv.id = 'commMiniHeader'+curCom.commid;
			commentRemarkDiv.id = 'com_'+curCom.commid;

			
			//populate divs	
			var id = curCom.commid;
			comIdArr[i] = id;
			commentTitleDiv.appendChild(document.createTextNode(curCom.title));	
			comCount = '<span id="comSpan'+id+'">('+curCom.replys+' Comments)</span>';
			comLink='<input type="button" id="newEntryButn'+id+'" class="formButtonBasic" onclick="Comment.addNewEntry('+id+')" value="Add Comment">';
			comView='<input type="button" id="viewHideButn'+id+'" class="formButtonBasic" onclick="Comment.viewCommentForm(this)" value="View Comments">';
			
			//no view button if zero comments
			if(curCom.replys <= 0)
				commentInfoDiv.innerHTML=comLink+'&nbsp;&nbsp;&nbsp;'+comCount;
			else
				commentInfoDiv.innerHTML=comView+'&nbsp;&nbsp;&nbsp;'+comLink+'&nbsp;&nbsp;&nbsp;'+comCount;
			
			//commentRemarkDiv.appendChild(document.createTextNode('blah'));
			
			//put divs into wrapper
			commentWrapperDiv.appendChild(commentTitleDiv);
			commentWrapperDiv.appendChild(commentInfoDiv);
			commentWrapperDiv.appendChild(commentRemarkDiv);
			
			//put wrapper in discussion
			self.cityDiscussDiv.appendChild(commentWrapperDiv);
			
		}
		//initilize comment class
		Comment.init(comIdArr);
		
	};
	
	//----------------------------------------------------------
	this.getAgendaMinutes = function(minuteID){
		var self = Agenda;
		self.proc = 'proc';
		if (self.setDisplay(false)) {
		  self.currOpacity = 100;
		  self.displayOpacity();
		  self.statusInterval = setInterval(self.doProc, 90);
		}		
		//clear set
		//if(isDefined(MinuteAdmin.editId)){
			//self.resetTop();
			if(document.getElementById('newAgenda'))
				self.optionsDiv.removeChild(document.getElementById('newAgenda'));
		//}
		//alert('about to run ajax_getMinutes, id: '+minuteID);
		//load agenda and comments
		var url = self.uncache('ajax_getMintues.php?id='+minuteID);
		self.ajax.doGet(url, self.handleMinuteResult, 'xml');	
		
	};
	//----------------------------------------------------------
	this.handleMinuteResult = function(xml) {
		var self = Agenda;
		var res = [];
		//alert('We\'re back');
		//alert(xml);
		res = XMLParse.xml2ObjArray(xml, 'ResultSet');
		//alert(res[0].agendaMsg.status);
		//alert(dump(res));
		if(res.length < 1){
			self.displayMinuteError(res);
		}else if(res[0].minuteMsg.status == 'error'){
			self.displayMinuteError(res);
		}
		else if(res[0].minuteMsg.status == 'success')
			self.displayMinutes(res);
		else
			self.agendaDiv.appendChild(document.createTextNode('Error reading Minutes'));
	};
	//----------------------------------------------------------
	this.displayMinutes = function(res) {
		var self = Agenda;
		var titleDiv = null;
		var headingDiv = null;
		var attendenceDiv = null
		var hereDiv = null;
		var absentDiv = null;
		var minBodyDiv = null;
		var minHeader = ''
		var minTitle = ''
		var minAttend = ''
		var minAbsent = ''
		var minDesc = '';
		var minUpdtDt = '';
		//alert('displaying minutes');
		
		minHeader = res[0].minHeader;
		minTitle = res[0].minTitle;
		minAttend = res[0].minAttend;
		minAbsent = res[0].minAbsent;
		minDesc = res[0].minDesc;
		minUpdtDt = res[0].minUpdtDt;
		//create elements to hold content
		titleDiv = document.createElement('div');
		headingDiv = document.createElement('div');
		attendenceDiv = document.createElement('div');
		hereDiv = document.createElement('div');
		absentDiv = document.createElement('div');
		minBodyDiv = document.createElement('div');
		//format elements
		titleDiv.className = 'miniHeader';
		headingDiv.className = 'emph';
		attendenceDiv.className = 'sideNote';
		minBodyDiv.className = 'listItem';
		//populate elements
		titleDiv.appendChild(document.createTextNode('Meeting Minutes For: ('+minUpdtDt+')'));
		headingDiv.appendChild(document.createTextNode(minHeader));
		hereDiv.appendChild(document.createTextNode('Present:'+minAttend));
		absentDiv.appendChild(document.createTextNode('Absent:'+minAbsent));
		attendenceDiv.appendChild(document.createTextNode('Attendence:'));
		attendenceDiv.appendChild(hereDiv);
		attendenceDiv.appendChild(absentDiv);
		minBodyDiv.innerHTML = minDesc;
		//insert elements into page
		self.agendaDiv.appendChild(titleDiv);
		self.agendaDiv.appendChild(headingDiv);
		self.agendaDiv.appendChild(attendenceDiv);
		self.agendaDiv.appendChild(minBodyDiv);
		
		//enable print
		self.printButn.disabled = false;
		
		//hold onto data
		self.minuteArr = res[0];
		
		if(self.loggedIn.value == 1 && self.editMinutes.value == 1){
			if(self.optionsDiv){
				self.optionsDiv.innerHTML='<span id="editInstructions"><input id="editMinuteButn" class="adminButn" onclick="MinuteAdmin.editMinutes('+res[0].minId+',\'editMinuteButn\')" value="Edit Minutes" type="button"></span><input type="button" id="printButn" class="formButtonBasic" onclick="window.print()" value="Print">';
			}
		}
		//enable button link back to agenda
		//alert("agenda button enabled");
		self.swapAgeMinButn.value = 'View Agenda';
		self.swapAgeMinButn.disabled = false;
		self.swapAgeMinButn.onclick = function () {self.viewAgenda(res[0].agendaId)};
		
		self.proc = 'done';
	};
	//----------------------------------------------------------
	this.displayMinuteError = function(res) {
		var self = Agenda;
		//alert('displaying minute error');
		if(res.length > 1){
			self.agendaDiv.appendChild(document.createTextNode(res[0].minuteMsg.message));
		}else{
			agendaDiv.appendChild(document.createTextNode('Error '+res.length+': Failed to load requested data.'));	
		}
		self.proc = 'done';
	};
	//----------------------------------------------------------
	this.setAlpha = function() {
    var self = Agenda;
    if (document.all && typeof window.opera == 'undefined') {	//IE
      var styleSheets = document.styleSheets;
      for (var i = 0; i < styleSheets.length; i++) {
        var rules = styleSheets[i].rules;
        for (var j = 0; j < rules.length; j++) {
          if (rules[j].selectorText == '#aniLoadingSpan') {
            rules[j].style.filter = 'alpha(opacity = 100)';
            return true;
          }
        }
      }
    }
    return false;
  	};
	
	//----------------------------------------------------------
	this.setDisplay = function(done) {
		var self = Agenda;
		var msg = '';
		if (done) {
			msg = 'Done';
			self.aniLoadingSpan.className = 'done';
			self.aniLoadingSpan.style.background="green";
		}
	else {
		msg = 'Processing...';
		self.aniLoadingSpan.className = 'processing';
	}
	if (self.aniLoadingSpan.firstChild) {
		self.aniLoadingSpan.removeChild(self.aniLoadingSpan.firstChild);
	}
		self.aniLoadingSpan.appendChild(document.createTextNode(msg));
		return true;
	};
	
	//----------------------------------------------------------
	this.displayOpacity = function() {
    	var self = Agenda;
    	var decOpac = self.currOpacity / 100;
		if (document.all && typeof window.opera == 'undefined') {
		  //self.aniLoadingSpan.filters.alpha.opacity = self.currOpacity;
		  self.aniLoadingSpan.style.filter = 'alpha(opacity='+self.currOpacity+')';
		  //alert(self.currOpacity+', vs: '+decOpac);
		}
		else {
		  self.aniLoadingSpan.style.MozOpacity = decOpac;
		}
		self.aniLoadingSpan.style.opacity = decOpac;
  	};

	//----------------------------------------------------------
	this.doProc = function() {
		var self = Agenda;
		//if (self.proc == 'done')
		//	alert('done');
		if (self.currOpacity == 0) {
			if (self.proc == 'proc') {
				self.currOpacity = 100;
			}
			else {
				clearInterval(self.statusInterval);
				if (self.proc == 'done') {
					self.startDone();
				}
				return false;
			}
		}
		self.currOpacity = self.currOpacity - 10;
		self.displayOpacity();
	};
  
  	//----------------------------------------------------------
	this.startDone = function() {
		var self = Agenda;
		if (self.setDisplay(true)) {
			self.currOpacity = 100;
			self.displayOpacity();
			self.statusInterval = setInterval(self.doDone, 90);
		}
	};
	
	//----------------------------------------------------------
	this.doDone = function() {
		var self = Agenda;
		if (self.currOpacity == 0) {
			clearInterval(self.statusInterval);
			self.aniLoadingSpan.style.background="#333333";
		}
		self.currOpacity = self.currOpacity - 10;
		self.displayOpacity();
		
	};
	//----------------------------------------------------------
	this.uncache = function(url){
		var self = Agenda;
		//stupid IE, need to randomise GET
		var d = new Date();
		var time = d.getTime();

		return url + '&time='+time;
	}
	//----------------------------------------------------------
	this.cleanup = function() {
		var self = Agenda;
		//IE is very messy with memory
		self.form = null;
		self.agendaDiv = null;
		self.optionsDiv = null;
		self.swapAgeMinDiv = null;
		self.cityHeaderDiv = null;
		self.commentHeaderDiv = null;
		self.editInstructionsSpan = null;
		self.swapAgeMinButn = null;
		self.swapAgeMinDiv = null;
		self.printButn = null;
		self.cityDiscussDiv = null;
	};
	
}

window.onunload = Agenda.cleanup;
//---------------------------------------------------------------------------------------------------------------------------




//-->	

