
/* MyAjaxEngine v0.5 #_#  + funzioni gestione calendario */


/* impostazioni script */
var sUrl = "dyn_cal_rc/dyn_cal.php";
var sMethod = "get";		//metodo richiesta http
var sDivId = "dynamic_cal";	//id del div da modificare
var sSpanId = "current_date";	//id dello span che mostra in formato testo il meso e l'anno visualizzati

var oXmlHttp = null;
var iCurShowingMonth = -1;	//il mese che il calendario sta visualizzando attualmente
var sGeneratedUrl = sUrl;	//l'url che viene generato dalla funzione generate_url()
var months_of_year = Array("Gennaio","Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
var date, month, year;


function createReqAndSend() {
	//oXmlHttp = new XMLHttpRequest();	//crea xmlHttp senza libreria zXml
	//oXmlHttp.open( sMethod, sGeneratedUrl, true );
	try {
  	  	oXmlHttp = new XMLHttpRequest();
  	} catch(e) {
		//IE
  	 	try {
  	 	     oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  	 	 } catch(e) {
  	 	     oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 	  	 }
 	 }

	//evita che IE metta in cache l'url
	sGeneratedUrl += "&no-cache=" + new Date().getTime();
	oXmlHttp.open( sMethod, sGeneratedUrl, true );

	//funzione che gestisce la risposta del server
	oXmlHttp.onreadystatechange = function() {
		//i dati sono arrivati completamente
		if( oXmlHttp.readyState == 4 ) {
			//la risposta è andata a buon fine, HTTP STATUS 200
			if( oXmlHttp.status == 200 ) {

				//mostra il calendario - IE ha dei seri problemi mentali...
				if( document.getElementById( sDivId ) ) {	
					document.getElementById( sDivId ).innerHTML = oXmlHttp.responseText;
				}
				else document.getElementById( sDivId ).innerHTML = oXmlHttp.responseText;
				
				//scrive il mese e l'anno
				document.getElementById( sSpanId ).innerHTML = months_of_year[month-1] + " " + year;
			}
			else alert( "Si è verificato un'errore durante la richiesta: " + oXmlHttp.statusText );
		}
	}

	//invia la richiesta
	oXmlHttp.send(null);
}



function generate_url(iValue) {
	var oDate = new Date(); 
	//oDate.getDate();

	if( iValue == 0 ) {
		if( iCurShowingMonth == -1 ) {
			year = oDate.getFullYear();
			month = oDate.getMonth() + 1;	//aggiunge 1 perche js parte da 0
			day = oDate.getDate();
			iCurShowingMonth = month;
		}
	
		sGeneratedUrl += "?day="+day+"&month="+month+"&year="+year;
	} //if(iValue==0)
	else if( iValue == 1 ) {
		//controlla che non si abbia il mese 13 :X
		if( (month+1) <= 12 ) month++;
		else { month = 1; year++; }
		
		iCurShowingMonth = month;
		sGeneratedUrl = sUrl + "?day=0&month="+month+"&year="+year;
	} //if(iValue==1)
	else if( iValue == -1 ) {
		if( (month-1) >= 1 ) month--;
		else { month = 12; year--; }

		iCurShowingMonth = month;
		sGeneratedUrl = sUrl + "?day=0&month="+month+"&year="+year;
	}

	//lancia la richiesta al server :o
	createReqAndSend();
}



function popup_news_up(title, day, month, year) {
	var url = "dyn_cal_rc/shownews.php?day=" + day + "&month=" + month + "&year=" + year;
	var popuptitle = /*title*/"popupfix" + "_";		//il titolo nn deve presentare spazi, altrimenti IE si incacchia...[-.-]
	var screen_w = screen.width; var screen_h = screen.height; 
	var popup_w = (screen_w * 40) / 100; var popup_h = (screen_h * 40 / 100);//la popup è il 40% dello schermo


	var popup_conf = "status=1, resizeable = 0, height = " + popup_h + ", width = " + popup_w;
	window.open( url, popuptitle, popup_conf);

}

