// Redesigned on 1/6/2011
//-----------------------------------------------------------
// Calendar Display
//
//This entire file will render the display version and
//text version of your scheduled calendar events.  
//Required: Calendar.xml
//
//------------------------------------------------------------
//
//---------------------------------------------------------------
//                          Section 1: Initialize Variables
//---------------------------------------------------------------
//

//------------------------------------------
// Initialze the Event Repeating Variables
//------------------------------------------
var freq = 0;
var repval = 0;
var rdMon = false;
var rdTue = false;
var rdWed = false;
var rdThu = false;
var rdFri = false;
var rdSat = false;
var rdSun = false;
//
// End Initializing the Event Repeating Variables
//
//-------------------------------
//Calendar and Today Variables
//-------------------------------
var monthText = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var mText = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var today = new Date();
var year = today.getFullYear();
var yearval = year;
var monthnum = today.getMonth();
var monthval = monthText[monthnum];
var date = today.getDate();
var day = today.getDay();
//-----------------------
// Leap Year Calculation
//-----------------------
var febdays = 28;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) 
	{febdays = febdays + 1;}
//--------------------------------------
// Set the number of days in each month
//--------------------------------------
var daycounts = [31,febdays,31,30,31,30,31,31,30,31,30,31]; 
//
//-------------------------------------------
// Set Forward and Back Navigation by Months
//-------------------------------------------
var prioryear = year;
var priormonth = monthnum - 1;
if (priormonth < 0)
	{priormonth = priormonth + 12;
	 prioryear = year - 1;
	}
var nextyear = year;
var nextmonth = monthnum + 1;
if (nextmonth > 11)
	{nextmonth = nextmonth - 12;
	 nextyear = year + 1;
	}
//
//----------------------------------------------
// Dropdown lists and Which Calendar to Display
//----------------------------------------------

var catref = 'All';
var catshow = 0;
var showmetoday = '';

var cats = [];
var catval = 0;

var catset = false;
var testvar = '';
var selectoption = true;

var textdisplayarea = 'CalendarText';
var catdisplayarea ='CalSelect';

//
var showitems = new Array();
var showitemsday = new Array();
var showitemno = 0;
//

//
//--------------------------------------------------
//           LOAD THE CALENDAR.XML FILE
//--------------------------------------------------
//
if (window.XMLHttpRequest)//Mozilla
  {
  xhttp=new XMLHttpRequest();
  }
else // Internet Explorer 5/6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
xhttp.open("GET","/calendar/Calendar.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
xmlObj=xmlDoc.documentElement;
var group=xmlObj.getElementsByTagName("CALENDAR");
var getcats=xmlObj.getElementsByTagName("CATS");
//
//-------------------------
// Load The Calendar Names
//-------------------------
C=0;
for (x=0;x<getcats.length;x++)  //Loop through each CALENDAR Group that was found in the XML file
{
	bnodes = getcats[x].childNodes.length;
	for(P=0;P<bnodes;P++)
	{
		currentPnode = getcats[x].childNodes[P];
		if(typeof currentPnode.tagName !="undefined"){
			if(getcats[x].getElementsByTagName(currentPnode.tagName)[0].hasChildNodes()){
				currentPval = getcats[x].getElementsByTagName(currentPnode.tagName)[0].childNodes[0].nodeValue;
				cats[C] = currentPval;
				C=C+1;
			}
		}
	}
}
//
//---------------------------------------------------------------
//                  Section 2: BEGIN FUNCTIONS
//---------------------------------------------------------------
//
//
// ShowEvent hides/unhides the Text View details
//
function ShowEvent(EventID) {
	var econtent = document.getElementById(EventID);
	var btnstatus = document.getElementById("btn"+EventID);
		if (econtent.style.display == "none") {
		econtent.style.display = "";
		btnstatus.src = "/calendar/arrow_up.gif";
		}
		else {
		econtent.style.display = "none";
		btnstatus.src = "/calendar/arrow_down.gif";
		}
}
//
// Todaysdate transforms the current date to show the weekday and full calendar date.
//
function todaysdate(catshow) {
		var week = new Array();
		week[0] = "Sunday";
		week[1] = "Monday";
		week[2] = "Tuesday";
		week[3] = "Wednesday";
		week[4] = "Thursday";
		week[5] = "Friday";
		week[6] = "Saturday";
		tyear = today.getFullYear();
		tmonthnum = today.getMonth();
		tdate = today.getDate();
		tday = today.getDay();
		
		if(tdate == 0){
			showmetoday = ("Current View " + (monthText[tmonthnum]) +" "+ (tyear));
		}
		else{
			showmetoday = ("<a id ='todayis' href=javascript:JCal('"+monthText[tmonthnum]+"',"+yearval +",'" +catref +"'," +catshow +")>Today is " + (week[tday]) +", " + (monthText[tmonthnum]) +" "+ (tdate) + ", "+ (tyear) +"</a>");
		}
}
//
// -------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------
//                                Event Information 
// *Note: Events will appear under the calendar if the template does not define a place for the textview.
// -------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------- 
//
function EventInfo(tyear,tmonth,tday,tdisplayarea,catshow,tid){
	displayarea=tdisplayarea;
	regid = tid;
	var dcode = '';
	var fm=0;
	
	for(count = 0; count < 12; count++){
		if(monthText[count] == tmonth){
			fm = count;
		}
	}	
	
	var m = fm + 1;
	var testmonth = (m * 100);
	var testyear = (tyear * 10000);
	var testmonthday = (m * 100) + (tday*1);
	var testyearmonth = (tyear * 10000) + (m * 100);
	var testdate = (tyear * 10000) + testmonthday;
	var startdate;
	var sdate = 0;
	var edate = 0;
	var enddate;
	var cattype = '';
	var recordsfound = false;
	var addthisrecord = false;
	var sectional = false;

	var addtodcode = '';
	
	if((displayarea == 'CalendarText') || (document.getElementById(displayarea) == undefined)){
		dcode = dcode + ("<table border='0' cellpadding='4' cellspacing='0'><tr><td align='left'>");
		if (document.getElementById(displayarea) != undefined){
		dcode = dcode +("<label id='TCalSelect'></label><select name='selMonth' onChange=JCal(this.options[this.selectedIndex].value," +tyear +",'" +catref +"'," +catshow +"); class='calselect'><option value='" + monthval + "' selected>--Month--</option>"); 
		
		for(count = 0; count < 12; count++){
			dcode = dcode +("<option value='" + monthText[count] + "'");
			if(monthText[count] == monthval){
				dcode = dcode +(" selected");
				dmonth = (monthText[count]);
			}
			dcode = dcode +(">" + mText[count] +"</option>");
		}
		dcode = dcode +("</select>");
	
		dcode = dcode +("<select name='selYear' onChange=JCal('" +dmonth +"',this.options[this.selectedIndex].value,'" +catref +"'," +catshow +"); class='calselect'><option value=' ' selected>--Year--</option>");
	
		selyear=tyear-5;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");
		selyear=tyear-4;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");	
		selyear=tyear-3;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");
		selyear=tyear-2;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");
		selyear=tyear-1;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");
		selyear=tyear-0;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");
		selyear = selyear + 1;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");
		selyear = selyear + 1;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");
		selyear=selyear+1;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");	
		selyear=selyear+1;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");	
		selyear=selyear+1;
		dcode = dcode +("<option value='" + selyear + "'");
		if (tyear  == selyear)
		dcode = dcode +(" selected");
		dcode = dcode +(">" + selyear +"</option>");	
		dcode = dcode +("</select>");	
		dcode = dcode +("</td></tr></table>");	
		}
	}
	else{
		//dcode = '<div class="caltextdateline" style="text-align:center;">' +catref + ' Calendar Events</div>'
	}

	for (i=0;i<showitems.length;i++){
		regid = showitems[i];
	
	for (n=0;n<group.length;n++)  //Loop through each CALENDAR Group that was found in the XML file
	{

		if((regid==group[n].getAttribute('id'))){
			gnodes = group[n].childNodes.length;
			testme = 0;
			sdate = 0;
			edate = 0;
			asmonth = "";
			aemonth = "";
			evaldate = false;
			ttime = "";
			showevent = false;
			addtodcode = '';
			ERR = '';
			locationcode = '';
			contactcode = '';
			regcode ='';
			maplink='<br />';
			tweetxt='';
			currentcal = group[n].getAttribute("name");

			repval = parseInt(group[n].getAttribute("repid"));
			freq = repval;
		// ------------------------------------------------------
		// Call the freqchk function to see if this event repeats
		// freqchk is located in the event_frequency.js file
		// ------------------------------------------------------
			freqchk();	
			
			for (G=0;G<gnodes;G++){  //Loop through each Item of the Group
				currentGnode = group[n].childNodes[G];
				if(typeof currentGnode.tagName !="undefined"){ //Current Item is not empty 
					currentGval = "empty tag";
					
					if(group[n].getElementsByTagName(currentGnode.tagName)[0].hasChildNodes()){
						currentGval = group[n].getElementsByTagName(currentGnode.tagName)[0].childNodes[0].nodeValue;
					}
						
					if (currentGval !== "empty tag"){ //This Item has information to show
						switch(currentGnode.tagName){
							case "email" : currentGval = "<a href='mailto:" + currentGval +"'>" + currentGval + "</a>";break;
						}
						if(currentGnode.tagName == "map" || currentGnode.tagName == "url"){
								currentGval = "<a href='http://" + currentGval +"' target='_blank'>Click Here</a>";
						}	

						switch(currentGnode.tagName){
							case "start" : addtodcode=addtodcode + " <div class='caltextdateline' onclick=ShowEvent("+i  +") style='cursor:pointer;cursor:hand;' ><img id='btn" +i +"' src='/calendar/arrow_down.gif' align='right' valign='top'>";break;
							case "end" : addtodcode=addtodcode + "";break;
							case "event" : addtodcode=addtodcode + "";break;
							case "location" : if(displayarea == 'CalendarText' || displayarea == 'HoverArea'){addtodcode=addtodcode + ""}else{addtodcode = addtodcode + '</div>'};break;
							case "contact" : break;
							case "registration" : break;
							case "id" : if(displayarea == 'CalendarText' || displayarea =='HoverArea'){
								if (locationcode != ''){
									locationcode = '<strong>Location: </strong>' +maplink +locationcode;
									addtodcode = addtodcode + locationcode;
								}								
								if (contactcode != ''){
									contactcode = '<br /><br /><strong>Contact: </strong><br />' + contactcode;
									addtodcode = addtodcode + contactcode;
								}
								if (regcode != ''){
									regcode = '<br /><strong>Registration: </strong><br />' + regcode;
									addtodcode = addtodcode + regcode;
								}

							
								addtodcode = addtodcode +'</div>';	
								};break;
							default : if(displayarea == 'CalendarText' || displayarea =='HoverArea'){addtodcode = addtodcode + "<strong>" + currentGnode.tagName +": </strong>" +currentGval + "<br />"};
						}
					
						GnodeChildren = group[n].getElementsByTagName(currentGnode.tagName)[0].childNodes.length;
					
						for(C=0;C<GnodeChildren;C++){  //Loop through each Element of the Item if an Element exists
							GChild = group[n].getElementsByTagName(currentGnode.tagName)[0].childNodes[C];

							if(displayarea == 'CalendarText' || displayarea == 'HoverArea'){

							
							if(GChild.hasChildNodes()){//An Element exists so show it
								GChildval =  group[n].getElementsByTagName(currentGnode.tagName)[0].childNodes[C].childNodes[0].nodeValue;
								switch(GChild.tagName){
									case "email" : GChildval = "<a href='mailto:" + GChildval +"'><img src='/calendar/contact.gif'> <strong>" + GChildval + "</strong></a>";break;
									case "subject" :currentsubject = GChildval; 
									tweetxt = tweetxt + GChildval;
									GChildval = "<font class='esubject'>"+ GChildval + "</font>  </a></div><div class=caltextdesc id='" +i +"' style='display:none;' >";
									
									break;
								}
								if(GChild.tagName == "url"){
									GChildval = "<a href='http://" + GChildval +"' target='_blank'><strong>Click Here</strong></a>";
								}
								
								if(currentGnode.tagName == "start"){
									switch(GChild.tagName){
										case "day" : sday = parseInt(GChildval) * 1;break;
										case "month" : smonth = parseInt(GChildval) * 100;break;
										case "year" : syear = parseInt(GChildval) * 10000;break;
										case "time" : stime = GChildval;break;
									}
								}
									
								if(currentGnode.tagName == "end"){
									switch(GChild.tagName){
										case "day" : eday = parseInt(GChildval) * 1;break;
										case "month" : emonth = parseInt(GChildval) * 100;break;
										case "year" : eyear = parseInt(GChildval) * 10000;break;
										case "time" : etime = GChildval;
													  evaldate = true;
											break;
									}
								}
								
								if(evaldate){
									switch(freq){
										case 0 : //None
										break;
										
										case 7 : //Monthly
											if(smonth == emonth){
												smonth = testmonth;
												emonth = testmonth;
												syear = testyear;
												eyear = testyear;
											}
											else {
												if(tday > sday){
													smonth = testmonth -100;
													emonth = testmonth;
													if (syear != eyear){
														syear = testyear - 10000;
													}
													else{
														syear = testyear;
													}	
													eyear = testyear;
												}
												else{
													smonth = testmonth;
													emonth = testmonth + 100;
													if (syear != eyear){
														eyear = testyear + 10000;
													}
													else{
														eyear = testyear;
													}
													syear = testyear;
												}
											}
											starttest = syear + smonth;
											endtest = eyear + emonth;
										break;
										
										case 8 : //Annual
											if (syear == eyear){
												syear = testyear;
												eyear = testyear;	
											}
											else if(testmonth == smonth) {
												syear = testyear;
												eyear = testyear + 10000;
											}
											else {
												syear = testyear - 10000;
												eyear = testyear;
											}
								
										break;
											
										default: //Weekday reoccurance
										break;
									}
									
									starttest = syear + smonth + sday;
									endtest = eyear + emonth + eday;
								
									evaldate = false;
								}
								
								if(currentGnode.tagName == "start" || currentGnode.tagName == "end"){
									switch(GChild.tagName){
										case "day" : 
											if(tday == 0){testme = testme;}
											else if(0 < freq && freq < 7){testme=(showitemsday[i]*1);}
											else {testme = testme + (parseInt(GChildval));}
													 
											if(currentGnode.tagName == "start"){
												if(freq > 0 && freq < 7 ){
													sdate = sdate + (showitemsday[i]*1);
													sdaydisplay = (showitemsday[i]*1); 
												}
												else {
												 	sdate = sdate + (parseInt(GChildval) * 1);
													sdaydisplay = (parseInt(GChildval) * 1);
												}
											}
											else {
												if(freq > 0 && freq < 7 ){
													edate = edate + (showitemsday[i]*1);
													edaydisplay = (showitemsday[i]*1);	
												}
												else {
													edate = edate + (parseInt(GChildval) * 1);
													edaydisplay = (parseInt(GChildval) * 1);
												}
											}
											break;
											
										case "month" :  
											eventmonth = testmonth;

											if(currentGnode.tagName == "start"){
												if(parseInt(GChildval) > m){
													testme = 0;
												}

												sdate = sdate + testmonth;
															
												if ((parseInt(GChildval) != testmonth) && (freq== 0 || freq == 8)){
													testme = testme + (parseInt(GChildval) * 100);
													smtext = monthText[(parseInt(GChildval))-1];
												}
												else{
													testme = testme + testmonth;
													smtext = tmonth;
												}
											}
											else {
												edate = edate + testmonth;
												if(freq == 8 || freq == 0){
													testme = testme + (parseInt(GChildval) * 100);
												}
												else{
													testme = testme + testmonth;
												}
															
												if ((parseInt(GChildval) != testmonth) && ((freq == 0) || (freq == 8))){
													emtext = monthText[(parseInt(GChildval))-1];
												}
												else{
													emtext = tmonth;
												}
											}
											break;
											
										case "year" : 
											if ((parseInt(GChildval) != testmonth) && (freq != 0)){
												testme = testme;
												xtyear = 0;
											}
											else {
												testme = testme + (parseInt(GChildval) * 10000);
												xtyear = GChildval;
											}
										 	break;
											
										case "time" : 
											ttime = GChildval;
											evaldate = true;
											break;
									}
								}
								if(evaldate){
									ERR = '';
									if (currentGnode.tagName == "start"){
										if(tday == 0){//Show all events for this month
											if(xtyear == 0){
												if(eventmonth ==testmonth){
													showstart = true;
													addtodcode = addtodcode + smtext +" " + sdaydisplay;
													stime = ttime;												
												}
												else {
													ERR= "S1 - " + testme;
													showstart = false;
													addtodcode = "";
													C=GnodeChildren;
													G=gnodes;													
												}	
											}
											else if(testyearmonth >= testme){//Event occurs during this month and year
												showstart = true;
												addtodcode = addtodcode + smtext +" " + sdaydisplay;
												stime = ttime;											
											}
											else {//Event does not occur during this month
												ERR= "S2 - " + testme;
												showstart = false;
												addtodcode = "";
												C=GnodeChildren;
												G=gnodes;										
											}
										}
										else if (testme <= 1299){//Event does not have a specific year
											if(testmonthday >= testme){//Day and Month match 
												showstart = true;
												addtodcode = addtodcode + smtext +" " + sdaydisplay;
												stime = ttime;
											}
											else {//Day and Month do not match
												ERR= "S3 - " + testme;
												showstart = false;
												addtodcode = "";
												C=GnodeChildren;
												G=gnodes;
											}
										}
										else if(testdate >= testme){//Show event only if it falls on this specific date
											showstart = true;
											addtodcode = addtodcode + smtext +" " + sdaydisplay;
											stime = ttime;
										}
										else{//Date does not match
											ERR= "S4 - " + testme + " " +testdate;
											showstart = false;
											addtodcode = "";
											C=GnodeChildren;
											G=gnodes;
										}
									}
									showend = false;
									if (currentGnode.tagName == "end"){
										if(tday == 0){//Show all events for this month
											if(xtyear == 0){
												if(eventmonth ==testmonth){
													showend = true;
													if(edate != sdate){

														addtodcode = addtodcode + " to " +emtext +" " +edaydisplay;
													}
													if(stime != ttime){
														addtodcode = addtodcode + "<br />Time: " +stime + " - " + ttime +"<br />";
													}
													else{
														addtodcode = addtodcode + "<br />";
													}
												}
												else {
													ERR= "E1 - " + testme;
													showend = false;
													addtodcode = "";
													C=GnodeChildren;
													G=gnodes;													
												}	
											}									
											else if(testyearmonth <= testme){
												showend = true;
												if(edate != sdate){
													addtodcode = addtodcode + " to " +emtext +" " +edaydisplay;
												}
												if(stime != ttime){
													addtodcode = addtodcode + "<br />Time: " +stime + " - " + ttime +"<br />";
												}
												else{
													addtodcode = addtodcode + "<br />";
												}
											}
											else {
												ERR= "E2 - " + testme;
												showend = false;
												addtodcode = "";
												C=GnodeChildren;
												G=gnodes;										
											}
										}								
										else if (testme <= 1299){
											if(testmonthday <= testme){//End Date is good
												showend = true;
												if(edate != sdate){
													addtodcode = addtodcode + " to " +emtext +" " +edaydisplay;
												}
												if(stime != ttime){
													addtodcode = addtodcode + "<br />Time: " +stime + " - " + ttime +"<br />";
												}
												else{
													addtodcode = addtodcode + "<br />";
												}
											}
											else {//End date is less than the date we are evaluating
												ERR= "E3 - " + testme;
												showend = false;
												addtodcode = "";
												C=GnodeChildren;
												G=gnodes;
											}										
										}
										else if(testdate <= testme){//End Date is good
											showend = true;
											if(edate != sdate){
												addtodcode = addtodcode + " to " +emtext +" " +edaydisplay;
											}
											if(stime != ttime){
												addtodcode = addtodcode + "<br />Time: " +stime + " - " + ttime +"<br />";
											}
											else{
												addtodcode = addtodcode + "<br />";
											}
										}
										else{//End date is less than the date we are evaluating
											ERR= "E4 - " + testme;
											showend = false;
											addtodcode = "";
											C=GnodeChildren;
											G=gnodes;
										}								
									}
									
									if (showstart && showend){
										addthisrecord = true;
										recordsfound = true;
										showstart = false;
										showend = false;
									}

									
									testme = 0;
									evaldate=false;
	
								}
								
								switch(currentGnode.tagName){
									case "start" : 
										addtodcode = addtodcode + "";
										break;
										
									case "end" : 
										addtodcode = addtodcode + "";
										break;
										
									case "event" : 
										switch(GChild.tagName){
											case "subject": 
												addtodcode = addtodcode + "<strong>" +GChildval +"</strong>";
												break;
											case "url" : 
												addtodcode = addtodcode + "<br/><strong>Web Page:</strong> " +GChildval +"<br/>";
												break;
											case "details" : 
												addtodcode = addtodcode + "<strong>" +GChildval +"</strong><br/>";
												break;
											default : 
												addtodcode = addtodcode +"<br/>" + GChildval ;
												break;
										}
										break;	
										
									case "location" :
										switch(GChild.tagName){
											case "notes": 
												locationcode = locationcode +"<br/>" +GChildval +"</b>";
												break;
											case "city" : 
												locationcode = locationcode +"<br/>"  + GChildval +", ";
												break;
											case "state" : 
												locationcode = locationcode + GChildval +" ";
												break;
											case "map" : 
												maplink = "<a href='http://" +GChildval +"'><img src='/calendar/map_pin.gif' border='0'><strong>Map</strong></a>";
												break;
											default : locationcode = locationcode +"<br/>"  + GChildval;break;
										}
										break;
										
									case "contact" :
										switch(GChild.tagName){
											case "firstname" : contactcode = contactcode + GChildval +" ";break;
											case "lastname": contactcode = contactcode + GChildval +"</b><br/>";break;
											case "state" : contactcode = contactcode + GChildval +" ";break;
											case "city" : contactcode = contactcode + GChildval +", ";break;
											case "state" : contactcode = contactcode + GChildval +" ";break;	
											case "email" : contactcode = contactcode + GChildval +"<br/>";break;										
											default : contactcode = contactcode + GChildval +"<br/>";break;
										}
										break;	
										
									case "registration" :
										switch(GChild.tagName){
											case "regform" :regcode=regcode + "<a href='/registration/" + GChildval +"?id=" +group[n].getAttribute('id') +"' rel='lyteframe' title='" +currentsubject +"'  rev='width: 600px; height: 600px; scrolling: yes;'><img src='/calendar/reg.gif'></a><br/> ";break;													
											case "regpayurl": break;
											case "regpaybtn": break;
											case "regtitleone" : regcode = regcode + GChildval +" ";break;
											case "regcostone" : regcode = regcode + GChildval +"<br/>";break;
											case "regtitletwo" : regcode = regcode + GChildval +" ";break;
											case "regcostwo" : regcode = regcode + GChildval +"<br/>";break;	
											case "regtitlethree" : regcode = regcode + GChildval +" ";break;
											case "regcostthree" : regcode = regcode + GChildval +"<br/>";break;
											case "regtitlefour" : regcode = regcode + GChildval +" ";break;
											case "regcostfour" : regcode = regcode + GChildval +"<br/>";break;											
											default : regcode = regcode + GChildval +"<br/>";break;
										}
										break;											
									default : addtodcode = addtodcode + " <li><strong>" +currentGnode.tagName + " : </strong>(" +GChild.tagName + ") " + GChildval + "</li>";break;
								}						
							}  //END IF An Element exists so show it
						}  //END Loop through each Element of the Item if an Element exists
						}
					}  //END IF This Item has information to show
				}  //END IF Current Item is not empty 
			}  //END Loop through each item of the Group
		};

		if(addthisrecord){
			dcode = dcode + addtodcode;
			addthisrecord = false;
			tweetxt = '';
		}		
		
	}  //END Loop through each Group found
	};

   if(document.getElementById(displayarea) == undefined || document.getElementById(displayarea) == null){
	   displayarea = 'HoverArea'; //No Text View area defined.  Show Event Details under the Graphical Calendar
//	   document.getElementById(displayarea).innerHTML=dcode;
   }
   else if (!recordsfound)
   {document.getElementById(displayarea).innerHTML=(dcode +"<div class=caltextdateline>No " +catref  +" events scheduled</div>");
   }
   else {
	document.getElementById(displayarea).innerHTML=dcode;   
   }
    
   if(displayarea == 'CalendarText'){
		catdisplayarea = 'TCalSelect';
		if(catshow == 1)	 JCat(tmonth,tyear,catref,catdisplayarea,catshow);	
   }
   
 initLytebox();  
}//close the function
//
// ----------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------- 
//                                Calendar Display 
//								
// *Note: Events will appear below the calendar if the template does not define a place for the event information.
// ----------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------- 
//
function JCal(month,iyear,ccatref,catshow)
{
	showitems = new Array();
	showitemsday = new Array();
	showitemno = 0;
	//--------------------------------------------------------------------------
	// Recalculate Leap Year and Reset the number of days in each month 
	// because a different year may have been selected. 
	//--------------------------------------------------------------------------	
	febdays = 28;	
	if (((iyear % 4 == 0) && (iyear % 100 != 0)) || (iyear % 400 == 0)) 
		{febdays = febdays + 1;}
 	daycounts = [31,febdays,31,30,31,30,31,31,30,31,30,31];
	
	//if ((document.getElementById('MonthView') != undefined) && (document.getElementById('CalendarText') != undefined)){
	catref=ccatref;		
	todaysdate();
	var ccode = '';
	var done = false;
	var day = 0;
	var firstdays = [0,0,0,0,0,0,0,0,0,0,0,0];
	monthval = month;
	repmonth = monthval;
	//
	// Determine which calendar we are using
	//
	var catpos = 0;
	for(var count = 0; count < cats.length; count++){
		if(cats[count] == catref) catpos = count; 
	}
	catval = catpos;
	if(catpos == 0){
//		  catref = 'All';
		  catshow = 1;
	}

	for (mycount = 0; mycount < 12; mycount++){
		dm=new Date(iyear, mycount, 1);
		ww=dm.getDay();
		firstdays[mycount]=ww;
		if(monthText[mycount] == monthval)	 monthnum = mycount;
	}
	
	if ((firstdays[monthnum] + daycounts[monthnum]) % 7 > 0){
		numofweeks = Math.round(((firstdays[monthnum] + daycounts[monthnum]) /7) -0.5 ) + 1;
	}
	else {
		numofweeks = (firstdays[monthnum] + daycounts[monthnum]) / 7;
	}
/*alert(monthText[monthnum] + ' has ' + numofweeks + ' weeks');*/
//-------------------------------------------------------------
// Testing the repeat event funtion
// Determine which weekday we are on and how many in a month
// example: 5 Wednesdays in a month
//-------------------------------------------------------------
	weeknum = 1;
	Suncount=0;
	lastSun=4;	
	Moncount = 0;
	lastMon=4;	
	Tuecount = 0;	
	lastTue=4;	 
	Wedcount=0; 
	lastWed=4; 
	Thucount=0; 	
	lastThu=4;
	Fricount=0; 	
	lastFri=4;
	Satcount=0; 	
	lastSat=4;

	if (firstdays[monthnum] == 0){
		if (daycounts[monthnum] >= 29){
			lastSun = lastSun+1;
			if (daycounts[monthnum] >= 30){
				lastMon = lastMon+1;
				if(daycounts[monthnum] = 31){
					lastTue = lastTue+1;
				}
			}
		}
	};

	if (firstdays[monthnum] == 1){
		if (daycounts[monthnum] >= 29){
			lastMon = lastMon+1;
			if (daycounts[monthnum] >= 30){
				lastTue = lastTue+1;
				if(daycounts[monthnum] = 31){
					lastWed = lastWed+1;
				}
			}
		}
	};	

	if (firstdays[monthnum] == 2){
		if (daycounts[monthnum] >= 29){
			lastTue = lastTue+1;
			if (daycounts[monthnum] >= 30){
				lastWed = lastWed+1;
				if(daycounts[monthnum] = 31){
					lastThu = lastThu+1;
				}
			}
		}
	};	

	if (firstdays[monthnum] == 3){
		if (daycounts[monthnum] >= 29){
			lastWed = lastWed+1;
			if (daycounts[monthnum] >= 30){
				lastThu = lastThu+1;
				if(daycounts[monthnum] = 31){
					lastFri = lastFri+1;
				}
			}
		}
	};		
	if (firstdays[monthnum] == 4){
		if (daycounts[monthnum] >= 29){
			lastThu = lastThu+1;
			if (daycounts[monthnum] >= 30){
				lastFri = lastFri+1;
				if(daycounts[monthnum] = 31){
					lastSat = lastSat+1;
				}
			}
		}
	};	
	if (firstdays[monthnum] == 5){
		if (daycounts[monthnum] >= 29){
			lastFri = lastFri+1;
			if (daycounts[monthnum] >= 30){
				lastSat = lastSat+1;
				if(daycounts[monthnum] = 31){
					lastSun = lastSun+1;
				}
			}
		}
	};	
	if (firstdays[monthnum] == 6){
		if (daycounts[monthnum] >= 29){
			lastSat = lastSat+1;
			if (daycounts[monthnum] >= 30){
				lastSun = lastSun+1;
				if(daycounts[monthnum] = 31){
					lastMon = lastMon+1;
				}
			}
		}
	};	
// END : Repeat Function section
//------------------------------------------------------	
	
//	ccode = ccode + '<h2>' +catref + ' Calendar</h2>';
	ccode = ccode + ('<div id="calendar_wrap"><table id="weekview" style="border-width="0;" cellspacing="0" summary="Calendar">');
	ccode = ccode + ("<thead ><tr><td id='selectrow' colspan='7' align='center'>");
	ccode = ccode +("<label id='CalSelect'></label><select name='selMonth' onChange=JCal(this.options[this.selectedIndex].value," +iyear +",'" +catref +"'," +catshow +"); class='calselect'><option value='" + monthval + "' selected>--Month--</option>"); 
	for(count = 0; count < 12; count++){
		ccode = ccode +("<option value='" + monthText[count] + "'");
		if(monthText[count] == monthval){
			ccode = ccode +(" selected");
			dmonth = (monthText[count]);
		}
		ccode = ccode +(">" + mText[count] +"</option>");
	}
	ccode = ccode +("</select>");
	
	ccode = ccode +("<select name='selYear' onChange=JCal('" +dmonth +"',this.options[this.selectedIndex].value,'" +catref +"'," +catshow +"); class='calselect'><option value=' ' selected>--Year--</option>");
	
	selyear=iyear-5;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");
	selyear=iyear-4;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");	
	selyear=iyear-3;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");
	selyear=iyear-2;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");
	selyear=iyear-1;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");
	selyear=iyear-0;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");
	selyear = selyear + 1;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");
	selyear = selyear + 1;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");
	selyear=selyear+1;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");	
	selyear=selyear+1;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");	
	selyear=selyear+1;
	ccode = ccode +("<option value='" + selyear + "'");
	if (iyear  == selyear)
	ccode = ccode +(" selected");
	ccode = ccode +(">" + selyear +"</option>");	
	ccode = ccode +("</select>");	
	ccode = ccode +("</td></tr></thead>");

	ccode = ccode +('<thead class="weekdayheader"><tr id="weekdayheader"><th scope="col" >S</th><th scope="col" >M</th><th scope="col" >T</th><th scope="col" >W</th><th scope="col" >T</th><th scope="col" >F</th><th scope="col" >S</th></tr></thead><tbody id="caldays">');

	while(!done){
    	ccode = ccode +("<tr>");
   		var dm;
		
		for(var j = 0; j < 7; j++){
		
      		dm = day + j;
      		if(dm >= firstdays[monthnum] && dm < daycounts[monthnum] + firstdays[monthnum]){
	   			var dayno = dm - firstdays[monthnum] + 1;
				var desc = new String(dayno);
				var esubject = '';
				var m = monthnum + 1;
				var mydayno = desc;
				var testmonthday = (m * 100) + dayno;
				var testdate = (iyear * 10000) + testmonthday;
				var startdate;
				var enddate;
				repmonth = monthval;
				//-------------------------------------------------------------------------
				// Repeat Event Test - Increament that a day of the week was just displayed
				//
				switch (j) {
					case 0 : Suncount = Suncount + 1;break;
					case 1 : Moncount = Moncount + 1;break;
					case 2 : Tuecount = Tuecount + 1;break;
					case 3 : Wedcount = Wedcount + 1;break;
					case 4 : Thucount = Thucount + 1;break;
					case 5 : Fricount = Fricount + 1;break;
					case 6 : Satcount = Satcount + 1;break;
				}
				//--------------------------------------------------------------------------					
				
/*				if (today.getMonth() == monthnum && today.getDate() == dayno && today.getFullYear()==iyear)//Make Today Bold
					{ccode = ccode + ('<td id="today">');
				}
				else {
					ccode = ccode ;
				}*/
	  
				for (n=0;n<group.length;n++)  //Loop through each Group that was found
				{
					if((group[n].getAttribute("name") ==catref)  || (group[n].getAttribute("name") =='General') || (catref =="All")){
						
						gnodes = group[n].childNodes.length;	  
						testme = 0;
						evaldate = false;
						showstart = false;
						showend = false;
						
				// Reset repeater values prior to lookup		
					repok = false;
					rdMon = false;
					rdTue = false;
					rdWed = false;
					rdThu = false;
					rdFri = false;
					rdSat = false;
					rdSun = false;	
					 
		// ------------------------------------------------------
		// Call the freqchk function to see if this event repeats
		// freqchk is located in the event_frequency.js file
		// ------------------------------------------------------
					repval = parseInt(group[n].getAttribute("repid"));
					freq = repval;		
					freqchk();	
						if (freq > 6 || freq < 1){
							repok = true;
						}
						else{
							if (rdSun && j== 0) {
								if(Suncount == freq || ((freq == 5 && Suncount == lastSun) || freq==6)){
									repok = true;
								}
							}
							if (rdMon && j== 1) {
								if(Moncount == freq || ((freq == 5 && Moncount == lastMon) || freq==6)){
									repok = true;
								}
							}
							if (rdTue && j== 2) {
								if(Tuecount == freq || ((freq == 5 && Tuecount == lastTue) || freq==6)){
									repok = true;
								}
							}
							if (rdWed && j== 3) {
								if(Wedcount == freq || ((freq == 5 && Wedcount == lastWed) || freq==6)){
									repok = true;
								}
							}
							if (rdThu && j== 4) {
								if(Thucount == freq || ((freq == 5 && Thucount == lastThu) || freq==6)){
									repok = true;
								}
							}
							if (rdFri && j== 5) {
								if(Fricount == freq || ((freq == 5 && Fricount == lastFri) || freq==6)){
									repok = true;
								}
							}
							if (rdSat && j== 6) {
								if(Satcount == freq || ((freq == 5 && Satcount == lastSat) || freq==6)){
									repok = true;
								}
							}
						
						}
				//--------------------------------------------		
						for (G=0;G<gnodes;G++){  //Loop through each Item of the Group
							currentGnode = group[n].childNodes[G];
							if(typeof currentGnode.tagName !="undefined"){ //Current Item is not empty 
								currentGval = "empty tag";
								
								if(group[n].getElementsByTagName(currentGnode.tagName)[0].hasChildNodes()){
									currentGval = group[n].getElementsByTagName(currentGnode.tagName)[0].childNodes[0].nodeValue;
								}
								if (currentGval !== "empty tag"){ //This Item has information to show
									GnodeChildren = group[n].getElementsByTagName(currentGnode.tagName)[0].childNodes.length;
									for(C=0;C<GnodeChildren;C++){  //Loop through each Element of the Item if an Element exists
										GChild = group[n].getElementsByTagName(currentGnode.tagName)[0].childNodes[C];
										if(GChild.hasChildNodes()){//An Element exists so show it
											GChildval =  group[n].getElementsByTagName(currentGnode.tagName)[0].childNodes[C].childNodes[0].nodeValue;
											
								switch(GChild.tagName){
									case "subject" :
										rid = group[n].getAttribute('id');
										GChildval = GChildval.replace("'","&rsquo;");
										esubject =	esubject +"<a href= javascript:EventInfo(&quot;"+iyear+"&quot;,&quot;"+repmonth +"&quot;,&quot;"+mydayno +"&quot;,&quot;CalendarText&quot;,"+catshow +","+rid+");>" +GChildval +"</a><br>";
									break;
								}												
											
								if(currentGnode.tagName == "start" || currentGnode.tagName == "end"){
									switch(GChild.tagName){
										case "day" : 
											if (freq > 0 && freq < 7 ){
												testme = testme + (mydayno * 1);
											}
											else{
												testme = testme + (parseInt(GChildval) * 1);
												}
											break;
											
										case "month" : 
											if (freq > 0 && freq < 8){
												testme = testme + ((monthnum +1) *100);
												repmonth =monthText[parseInt(GChildval)-1];
											}
											else {
												testme = testme + (parseInt(GChildval) * 100);
											}
											break;
											
										case "year" : 
											if (freq ==0 && repval == 0){
												testme = testme + (parseInt(GChildval) * 10000); 
											}
											break;
									}
									
									if (GChild.tagName =="year")
										{evaldate = true;}
									}
									
									if (evaldate){
										if (currentGnode.tagName == "start"){
											if (testme <= 1299){
												if (testmonthday >= testme){//Start Date is good
													showstart = true;
												}
												else {//Start date is greater than the date we are evaluating
													showstart = false;
													C=GnodeChildren;
													G=gnodes;
												}
											}
											else if(testdate >= testme){//Start Date is good
												showstart = true;
											}
											else{//Start Date is greater than the date we are evaluating
												showstart = false;
												C=GnodeChildren;
												G=gnodes;
											}
										}
										
										if (currentGnode.tagName == "end"){
											if (testme <= 1299){
												if (testmonthday <= testme){//End Date is good
													showend = true;
												}
												else {//End date is less than the date we are evaluating
													showend = false;
													C=GnodeChildren;
													G=gnodes;
												}										
											}
											else if(testdate <= testme){//End Date is good
												showend = true;
											}
											else{//End date is less than the date we are evaluating
												showend = false;
												C=GnodeChildren;
												G=gnodes;
											}								
										}
												
										if (showstart && showend && repok){
											newitem = true;
											for (i=0;i<showitems.length;i++){
												if (showitems[i] == group[n].getAttribute('id')){
													if (freq == 0 || freq >6){
														newitem = false;
													}
												}
											}
											
										if (newitem){										
											showitems[showitemno] = group[n].getAttribute('id');
											if (freq > 0 && freq < 7){
												showitemsday[showitemno] = mydayno;
											}
											else {
												 showitemsday[showitemno] = 0;
											}
											
											showitemno = showitemno + 1;
										}
										
										pagextra = 	monthval + "&year=" + iyear + "&day=" + dayno + "&cat=" + catref;	
										if (document.getElementById('CalendarText') == undefined){
											desc = "<a name='calendarevent' class='calendarevent' style='cursor:pointer;cursor:hand;' onClick=EventInfo('" +iyear +"','" +repmonth +"','" +mydayno +"','CalendarText','" + catshow +",0);><div  title='Click for Details'><strong>" + mydayno + "</strong></div></a>";		
										}
										else {
											desc = "<a name='calendarevent' class='calendarevent' style='cursor:pointer;cursor:hand;' onmouseover=EventInfo('" +iyear +"','" +repmonth +"','" +mydayno +"','HoverArea',"+catshow+",0); onClick=EventInfo('" +iyear +"','" +repmonth +"','" +mydayno +"','CalendarText','" + catshow +"',0);><div  title='Click'><strong>" +  mydayno + "</strong></div></a>";	
										}
										
										showstart = false;
										showend = false;							
									}
												
									testme = 0;
									evaldate=false;
								}
							}  //END IF An Element exists so show it
						}  //END Loop through each Element of the Item if an Element exists
					}  //END IF This Item has information to show
				}  //END IF Current Item is not empty 
			}  //END Loop through each item of the Group
		};
	};  //END Loop through each Group found
	if (document.getElementById('CalendarText') != undefined){
		displayarea = 'CalendarText';
	}
	else {
		displayarea = 'HoverArea';
	}				
		if (desc != mydayno){
			ccode = ccode +  "<td id='calendarevent' class='calendarevent'><a name='calendarevent' class='calendarevent' title='Click for Event Details' style='cursor:pointer;cursor:hand;' onClick=EventInfo('" +iyear +"','" +monthval +"','" +mydayno +"','"+displayarea+"','" + catshow +"',0);>" +  mydayno + "</a>";
		}
		else {
			
			if (today.getMonth() == monthnum && today.getDate() == dayno && today.getFullYear()==iyear)//Make Today Bold
				{ccode = ccode + ('<td id="today">');
			}
			else {
				ccode = ccode +("<td>");
			}			
			ccode = ccode + mydayno;
		}
	
			ccode = ccode +("</td>");
			repmonth = monthval;
		}
		else {
			ccode = ccode +("<td class='calendarBlank'> </td>");
		}
	}
	ccode = ccode +("</tr>");
	day = day + 7;
	weeknum = weeknum + 1;
    if( day > daycounts[monthnum] + firstdays[monthnum]){
         done = true;
    }
	}

	ccode = ccode +("</tbody>");	

	todaysdate(catshow);					
	ccode = ccode +("<tfoot id='tfoot'><tr><td  colspan='7'>" +showmetoday +"</tr><tr><td id='HoverArea' colspan='7' align='center'>");
	
	ccode = ccode +("</td></tr></tfoot></table></div>");

	//Only show if MonthView is defined in the original document		
	if (document.getElementById('MonthView') != undefined){
		document.getElementById('MonthView').innerHTML=(ccode);	
		catdisplayarea = 'CalSelect';
		if(catshow == 1)	 JCat(month,iyear,catref,catdisplayarea,catshow);	
		}
	//END: Only show if MonthView is defined in the original document	

	//
	// Show the Event Details now that the Graphical Calendar is complete
	//
	if (document.getElementById('CalendarText') != undefined){
		EventInfo(iyear,month,0,'CalendarText',catshow,0);
	}
	else {
		EventInfo(iyear,month,0,'HoverArea',catshow,0);
	}
	initLytebox();
}

//}
// ----------------------------------------------------------------
//-----------------------------------------------------------------
// JCat creates a dropdown select box with all available calendars
//-----------------------------------------------------------------
// ----------------------------------------------------------------
//
function JCat(month,iyear,jcatref,catdisplayarea,catshow){
	var jcode='';
	
	catref = jcatref;
	jcode=jcode+("<select name='selCat' onChange=JCal('" + month +"'," +iyear +",this.options[this.selectedIndex].value," +catshow +"); class='calselect'><option value='All' >--Filter--</option>");
	for(var count = 0; count < cats.length; count++)
	{
		if(count == 0)
			{
				jcode=jcode+("<option value='All'");
				if(cats[count] == catref) {jcode=jcode+(" selected");}
				jcode=jcode+(">All Events</option>");
			}
		else
			{
				jcode=jcode+("<option value='" +  cats[count] + "'");
				if(cats[count] == catref) {jcode=jcode+(" selected");}
				jcode=jcode+(">" + cats[count] + "</option>");
			}
	}
	jcode=jcode+("</select>");
	if (catdisplayarea == 'CalSelect'){jcode=jcode+"<br>";}
	document.getElementById(catdisplayarea).innerHTML=(jcode);
	initLytebox();

}
//
//

