var XmlHttpCal;
var xmlDocCal;
var myded;
var now = new Date();
var curmonth;
var curyear;
curmonth=now.getMonth()+1;
curyear=now.getFullYear();
var monthname = new Array(
"ΙΑΝΟΥΑΡΙΟΣ ",
"ΦΕΒΡΟΥΑΡΙΟΣ ",
"ΜΑΡΤΙΟΣ ",
"ΑΠΡΙΛΙΟΣ ",
"ΜΑΪΟΣ ",
"ΙΟΥΝΙΟΣ ",
"ΙΟΥΛΙΟΣ ",
"ΑΥΓΟΥΣΤΟΣ ",
"ΣΕΠΤΕΜΒΡΗΣ ", 
"ΟΚΤΩΒΡΗΣ ",
"ΝΟΕΜΒΡΗΣ ",
"ΔΕΚΕΜΒΡΗΣ ")

function CreateXmlHttpCal()
{
	
    //Creating object of XMLHTTP in IE
    try
    {
        XmlHttpCal = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            XmlHttpCal = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(ex)
        {
            XmlHttpCal = null;
        }
    }
    //Creating object of XMLHTTP in Mozilla and Safari
    if(!XmlHttpCal && typeof XMLHttpRequest != "undefined")
    {
        XmlHttpCal = new XMLHttpRequest();
    }
    
}

function getCalendarData(dd){
	targ = document.getElementById(dd);	
	//targ.disabled=true;
	CreateXmlHttpCal();
	myded=dd;	
	//wind=win;

    if(XmlHttp)
    {    
        XmlHttpCal.onreadystatechange = HandleCalendarResponse;
        XmlHttpCal.open("GET", "/calendar.php?month="+curmonth+"&year="+curyear+"&salt="+now,  true);
	
        XmlHttpCal.send(null);           
    }
}

function HandleCalendarResponse()
{	
	//alert(XmlHttp.readyState);
    if(XmlHttpCal.readyState == 4)
    {
    	
        if(XmlHttpCal.status == 200)
        {        	
			//alert(XmlHttpCal.responseXML.documentElement.getElementsByTagName('record').length);
            xmlDocCal=XmlHttpCal.responseXML.documentElement;
			//alert(xmlDoc.getElementsByTagName('record')[0].childNodes[1].firstChild.nodeValue);
            Calendarize(myded);           
        }
        else
        {
            alert("There was a problem retrieving data from the server." );
        }
    }else{
    	//HandleResponse();
    }
}



function Calendarize(dd){
	document.getElementById('calendartitle').innerHTML=monthname[curmonth-1]+curyear;
	targ = document.getElementById(dd);	
	while(targ.hasChildNodes()){
		targ.removeChild(targ.lastChild);
	}	
	records=xmlDocCal.getElementsByTagName('day');	
	if(Number(records[0].childNodes[1].firstChild.nodeValue)!=1){	
		for (var k=1;k<Number(records[0].childNodes[1].firstChild.nodeValue);k++){
			var eil=document.createElement('div');	
			eil.innerHTML="";	
			eil.className="calendarEmpty";
			targ.appendChild(eil);			
		}
	}
	for (var i=0;i<records.length;i++){		
		var fil=document.createElement('div');		
		try{
			fil.setAttribute("calevent",records[i].childNodes[2].firstChild.nodeValue);
			if(curmonth==now.getMonth()+1 && curyear==now.getFullYear() && i==now.getDate()-1){
				fil.className="calendarToday";
			}else{
				fil.className="calendarEvent";
			}
			fil.style.zIndex=40-i;
			fil.onmouseover=function(e){showEvent(this)};
			fil.onmouseout=function(e){hideEvent(this)};
					
		}catch(ex){
			if(curmonth==now.getMonth()+1 && curyear==now.getFullYear() && i==now.getDate()-1){
				fil.className="calendarToday";
			}else{
				fil.className="calendarDay";
			}
		}

		fil.innerHTML=records[i].childNodes[0].firstChild.nodeValue;		
		targ.appendChild(fil);
	}		
}	

function showEvent(dd){

	var txt = dd.getAttribute("calevent");
	
	var ril = document.createElement('div');
	ril.className="rollover";
	ril.innerHTML=txt;
	dd.appendChild(ril);
	
}
function nextMonth(dd){
	curmonth++;
	if (curmonth==13){
		curmonth=1;
		curyear++;
	}	
	getCalendarData(dd);
}

function prevMonth(dd){
	curmonth--;
	if (curmonth==0){
		curmonth=12;
		curyear--;
	}	
	getCalendarData(dd,curmonth,curyear);
}
function hideEvent(dd){
	if(dd.childNodes.length>1){
		dd.removeChild(dd.lastChild);
	}

}



