
/*
Used by the load_xml() function
*/
tag_selector="performance"; //Set value to the xml element you would like to select from.
script_selector="Scripts/calendar.xml"; //Set value to the location of the xml file.

load_xml(); //call the load_xml() function to load the xml file

/*
Loads the xml file
*/
function load_xml() {
	//Load the xml file
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.open("GET",script_selector,false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML; 
	x=xmlDoc.getElementsByTagName(tag_selector);
}


/*
Loads the dates from the xml file 
and stores them in an array
*/
function date_array() {
 
 thisYear=new Date();
 thisYear=thisYear.getFullYear(); //This is the current year
 thisMonth=new Date();            //This is the current month
 thisMonth=thisMonth.getMonth();
 year=new Array();
 month=new Array();
 day=new Array();
 title=new Array();
 priv=new Array();
 
	for (var i=0;i<x.length;i++){
		priv[i]=(x[i].getElementsByTagName("private")[0].childNodes[0].nodeValue);
		title[i]=(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
		year[i]=(x[i].getElementsByTagName("year")[0].childNodes[0].nodeValue);
		month[i]=(x[i].getElementsByTagName("month")[0].childNodes[0].nodeValue);
		day[i]=(x[i].getElementsByTagName("day")[0].childNodes[0].nodeValue);
	}

}


/*
function get_mouse_coords(event) {
	var x = event.clientX;
	var y = event.clientY;
	var object = document.getElementById("info");
	object.style.left=x+"px";
	object.style.top=y+"px";
}
*/

/*
Display the information for the selected event in
the pop up div
*/
function display_info(i) {
	var monthName = new Array("Jan", "Feb", "Mar", "Apr", "May", 
       "Jun", "Jul", "Aug", "Sep", "Oct","Nov", "Dec");
    /*
    ++++++++Check to see that window.event is supported.+++++++++++++++
    ++++++++In Firefox events are not global so this block does not++++
    ++++++++work in Firefox... Firefox will Default++++++++++++++++++++
    */
    if(window.event){
	 var coorx = window.event.clientX;
	 var coory = window.event.clientY-200;
	 document.getElementById("info").style.left=coorx+"px";
	 document.getElementById("info").style.top=coory+"px";
	}
	title=(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
	partner=(x[i].getElementsByTagName("partner")[0].childNodes[0].nodeValue);
	city=(x[i].getElementsByTagName("city")[0].childNodes[0].nodeValue);
	event=(x[i].getElementsByTagName("event")[0].childNodes[0].nodeValue);
	size=(x[i].getElementsByTagName("size")[0].childNodes[0].nodeValue);
	venue=(x[i].getElementsByTagName("venue")[0].childNodes[0].nodeValue);
	year=(x[i].getElementsByTagName("year")[0].childNodes[0].nodeValue);
	month=(x[i].getElementsByTagName("month")[0].childNodes[0].nodeValue);
	day=(x[i].getElementsByTagName("day")[0].childNodes[0].nodeValue);
	txt="Title: "+title+"<br />Partner: "+partner+"<br />City: "+city+"<br />Event: "+event+"<br />Size: "+size+"<br />Venue: "+venue+"<br />Date: "+day+"/"+monthName[string_to_int(month)]+"/"+year;
	document.getElementById("info").style.display='block';
	document.getElementById("info").innerHTML=txt;
}

/*
Hide the pop up div
*/
function hide_info(){
	document.getElementById("info").style.display='none';
}


/*
Loads the dates from the xml file +++++++++++++____HERE____+++++++++++++
and stores them in an array
*/
function print_dates(dayCount) {
 printed = false;
	//this works don't mess it up! :P
	for (var i=0;i<x.length;i++){
	  if(priv[i]=="false"){
		if(year[i]==thisYear && month[i]==thisMonth){
			if(dayCount==day[i]){
	 		   document.write("<td class='calendar_dates' \
	 		   		id='calendar_performance' \
	 		   		onmouseover='display_info("+ i +")' \
	 		   		onmouseout='hide_info()'> \
	 		   		<a href='Pages/Calendar/Schedule.htm' \
	 		   		title='"+title[i]+"'>"+dayCount+"</a></td>");
	 		   printed = true;
	 		   return printed;
	 		   }
		}
	  }
	}
}


//Set the title = the current year/month
function writeCalTitle(calendarDay) {
   var monthName = new Array("January", "February", "March", "April", "May", 
       "June", "July", "August", "September", "October","November", "December");
   var thisMonth=calendarDay.getMonth();
   var thisYear=calendarDay.getFullYear();
   document.write("<tr>");
   document.write('<th id="calendar_head" colspan="7">');
   document.write(monthName[thisMonth]+" "+thisYear);
   document.write("</th>");
   document.write("</tr>");
}


//Name the columns according to the days of the week
function writeDayTitle() {
   var wdName = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
   document.write("<tr>");
   for (i=0;i<=6;i++) {
      document.write("<th class='calendar_weekdays'>"+wdName[i]+"</th>");
   }
   document.write("</tr>");
}

/*
++++++++++++++++++______MEL__THIS__IS__WHERE__WE__WERE__WORKING+++++++++++++++++++
++++++++++++++++++______ALSO__CHECK__UP__ABOVE__^^^^++++++++++++++++++++++++++++++
*/
function writeCalDays(calendarDay) {
   var thisDay=calendarDay.getDate();
   calendarDay.setDate(1);   
   var weekDayNum=calendarDay.getDay();
   var thisMonth=calendarDay.getMonth();
   var thisYear=calendarDay.getFullYear();
   
   date_array();

   document.write("<tr>");

   for(i=0;i < weekDayNum;i++) {
      document.write("<td></td>");
   }
   
   var cellCount=1;
   var dayCount=1;

   while (dayCount==cellCount) {
      
      if (weekDayNum==0) {
         document.write("<tr>");
      }
      
      print_dates(dayCount);
      
      if(printed!=true){
      	if(dayCount == thisDay){
      	document.write("<td class='calendar_dates' id='calendar_today'>"+dayCount+"</td>");
      	} else {
      	document.write("<td class='calendar_dates'>"+dayCount+"</td>");
      	}
	  }
	  
	  
      if (weekDayNum==6) {
         document.write("</tr>");
      }
      
      cellCount++;
      calendarDay.setDate(cellCount);
      weekDayNum=calendarDay.getDay();
      dayCount=calendarDay.getDate();
   }
}
/*
Convert String values for the months of the year into
integer values.
*/
function string_to_int(s) {
	var strings = new Array('01','02','03','04','05','06','07','08','09','10','11','12','13');
	var ints = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13);
	
	for(i=0; i<14; i++) {
		if(s==strings[i]) {
			return ints[i];
		}
	}
}

//create the calendar widget
function calendar(thisDate) {

   if (thisDate=="today") {
      calDate=new Date();
   } else {
      calDate=new Date(thisDate);
   }
   
   document.write('<table id="calendar_table">');
   writeCalTitle(calDate);
   writeDayTitle();
   writeCalDays(calDate);
   document.write("</tr></table>");
   
}

/*
-------------------+++++++++++++For The Main Calendar Page Only!++++++++----------
*/

function loadXMLDoc(url) {
 
  if (window.XMLHttpRequest){
   //Code for IE7+, Firefox, Chrome, Opera, Safari
   connect = new XMLHttpRequest ();
   connect.open("GET",url,false);
   connect.send(null);
  } else {
   //Code for IE5, IE6
   conect = new ActiveXObject("Microsoft.XMLHTTP");
   connect.open("GET",url,false);
   //Do Not send null for ActiveX
   connect.send();
  }
  
  document.getElementById('calendar_detail').innerHTML=connect.responseText;
  
 }

/*
*This Function displays the events in a list form for the Schedule page.
*/
function display_events() {
var title,link,city,year,month,day;
var monthName = new Array("Jan", "Feb", "Mar", "Apr", "May", 
       "Jun", "Jul", "Aug", "Sep", "Oct","Nov", "Dec");

document.write("<table border='1'>");
document.write("<tr>");
document.write("<th>Date</th>");
document.write("<th>City</th>");
document.write("<th>Event</th>");
document.write("</tr>");
for (var i=0;i<x.length;i++)
  {
  title=x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
  link=x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
  city=x[i].getElementsByTagName("city")[0].childNodes[0].nodeValue;
  year=x[i].getElementsByTagName("year")[0].childNodes[0].nodeValue;
  month=x[i].getElementsByTagName("month")[0].childNodes[0].nodeValue;
  day=x[i].getElementsByTagName("day")[0].childNodes[0].nodeValue;
  document.write("<tr onclick='caller("+ i +")'>");
  document.write("<td>");
  document.write(monthName[string_to_int(month)]+" "+day);
  document.write("</td><td>");
  document.write(city);
  document.write("</td><td>");
  document.write('<a href="'+link+'">'+title+'</a>');
  document.write("</td></tr>");
  }
document.write("</table>");
}


//one attempt at displaying the details... does not work.
function display_detail(i) {
var link,aOpen,aClose,url;
link=x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
url=link.toString();
aOpen='<a href="';
aClose='" target="calendar_detail"/>';
//document.getElementById("calendar_detail").innerHTML='"'+aOpen+link+aClose+'"';
document.write(url);
loadXMLDoc(url);
}

//another attempt at displaying the details... kinda works
function my_display(i) {
var title, description, location, time, map;

  title=x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
  description=x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
  location=x[i].getElementsByTagName("location")[0].childNodes[0].nodeValue;
  time=x[i].getElementsByTagName("time")[0].childNodes[0].nodeValue;
  map=x[i].getElementsByTagName("map")[0].childNodes[0].nodeValue;
  document.write("<h3>"+title+"</h3>");
  document.write("<p>"+description+"</p>");
  document.write("<ul>");
  document.write("<li>Location: "+location+"</li>");
  document.write("<li>Performance Times: "+time+"</li>");
  document.write("<li>Directions: "+map+"</li>");
  document.write("</ul>");

}

function caller(i) {
	document.getElementById("calendar_detail").innerHTML=my_display(i);
}
