// JavaScript Document
var iCal = function( year , month , day , blockID )
{
	this.month = month;
	this.year =  year;
	this.day = day;
	//this.datestring = datestring;
	this.blockID = blockID ;
	this.onclick = "";
	
	this.NextMonth = function()
	{
		this.month++;
		if( this.month > 12 ){	this.month = 1 ;	this.year++;	}
		this.fetchMonth();
	}	
	this.PreMonth = function()
	{
		this.month--;
		if( this.month < 1 )	{	this.month = 12 ; this.year--;	}
		this.fetchMonth();		
	}

	this.NextYear = function()
	{	this.year++;	fetchMonth();	}
	this.PreYear = function()
	{	this.year--;	fetchMonth();	}		

	this.fetchMonth = function()
	{
		var url = "ajax.calendar.php?";
		var monthStr = ( this.month < 10 ) ? "0" + this.month : this.month ;
		var dateString = this.year + "" + monthStr ;
		var query = "&date=" + dateString + "&onclick=" + escape(this.onclick);
		//alert(escape(this.onclick));
		var blockID = this.blockID;

		Ajax.Request( url+query , { complete : function( httpObj )
			{
				document.getElementById(blockID).innerHTML = httpObj.responseText;
				document.getElementById("YearNo").innerHTML = year;
				document.getElementById("MonthNo").innerHTML = month;
				
				//getContent( my_id , "doctor" );
				//if( choose_id > 0 ) getContent( choose_id , "staff" );

			}
		} );
	}
	
	this.miniMonth = function( no   , year , blockID , onclick )
	{
		var url = "ajax.calendar.php?";
		var query = "&year=" + year + "&month=" + no + "&onclick=" + escape(onclick);
		//var escape("doctor_booking.php?&s=123");
		Ajax.Request( url+query , { complete : function( httpObj )
			{				
				document.getElementById( blockID ).innerHTML = httpObj.responseText;
				getMiniSchedule( year , no );
			}
		} );	
	}



	
}

	function getMiniSchedule( y , m )
	{
		var url = "ajax/course.php?cache="+(new Date()).getTime()  ;
		var query = "&y=" + y + "&m=" + m ; 
		Ajax.Request( url+query , { complete : function( httpObj )
			{	
				//alert(httpObj.responseText);
				parseCourseSchedule( httpObj.responseText );
			}
		} );		
	}
	
	function parseCourseSchedule( xml )
	{
		//alert(xml);
		var doc = getXMLParser(xml);		
		var events = doc.getElementsByTagName("event");
		var div = null;
		
		for( var i = 0 ; i < events.length ; i++)
		{
			var d = events[i].getAttribute("date")
			div = document.getElementById("mb"+ d) ;
			div.style.background = "url();"
			div.onmouseover = function()
			{
				//displayCourse( div , d);
			};
		}		
	}
	
	function displayCourse( div , date )
	{
		alert(date);
	}
/*

	function chooseSchedule( choose )
	{

		if( choose_id > 0 && choose_id != choose.value ) 
		{
			clearSchedule( choose_id );
		}
		choose_id = choose.value;
		if( choose.value <= 0 ) return ;
		getContent( choose.value , "staff" );
		
		return ;

	}	
	
	
	function getContent( id , type )
	{
		if( id == undefined ) return alert("invalid id with getContent");
		var url = "ajax.schedule.php?cache="+(new Date()).getTime()  ;
		var query = "&date=" + dateString + "&id=" + id;
		Ajax.Request( url+query , { complete : function( httpObj )
			{	

				parseSchedule( httpObj.responseText , id  , type );
			}
		} );
	}
	
	function parseSchedule( xml , id  , type )
	{
		//alert(xml);
		var doc = dataXML[id] = getXMLParser(xml);		
		var events = doc.getElementsByTagName("event");
		var div = null;
		for( var i = 0 ; i < events.length ; i++)
		{
			div = document.getElementById("mb"+events[i].getAttribute("date"));
				div.appendChild( createEventDiv(events[i] , type ) );
		}
	}

	function clearSchedule( id )
	{
		//alert( id )
		var doc = dataXML[id];
		var events = doc.getElementsByTagName("event");
		var div = null;
		for( var i = 0 ; i < events.length ; i++)
		{
			div = document.getElementById("mbe"+events[i].getAttribute("id"));
				div.parentNode.removeChild( div );

		}
	}
	
	function createEventDiv( e , type )
	{
		//alert(id);
		var path = "event.php?";
		if( calObj != "undefined" ) path = calObj.path;
		var n = document.createElement("div");
		n.id = "mbe" + e.getAttribute("id");
		n.className = "calEventStyle_" + type;
		//document.write(n.className);
		n.innerHTML = "<a href='"+path+"&id="+e.getAttribute("id")+"'>"+e.getAttribute("string")+"</a>" + e.getAttribute("timestart")+"-"+e.getAttribute("timeend");
		return n;
	}
	
	*/
