var req;

function navigate(month,year) {
		//viewDay(0,month,year);
        var url = "calendar/calendar.php?month="+month+"&year="+year;
        if(window.XMLHttpRequest) {
                req = new XMLHttpRequest();
        } else if(window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.open("GET", url, true);
        req.onreadystatechange = callback;
        req.send(null);
}

function callback() {        
        obj = document.getElementById("calendar");
        setFade(0);
        
		if(req.readyState == 4) {
                if(req.status == 200) {
                        response = req.responseText;
                        obj.innerHTML = response;
                        fade(0);
                } else {
                        alert("There was a problem retrieving the data:\n" + req.statusText);
                }
        }
}

function fade(amt) {
	if(amt <= 100) {
		setFade(amt);
		amt += 10;
		setTimeout("fade("+amt+")", 5);
    }
}

function setFade(amt) {
	obj = document.getElementById("calendar");
	
	amt = (amt == 100)?99.999:amt;
  
	// IE
	obj.style.filter = "alpha(opacity:"+amt+")";
  
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = amt/100;
  
	// Mozilla and Firefox
	obj.style.MozOpacity = amt/100;
  
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = amt/100;
}

function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;

  this.update = function(passData,postMethod) { 
    if (that.updating==true) { return false; }
    that.updating=true;                       
    var AJAX = null;                          
    if (window.XMLHttpRequest) {              
      AJAX=new XMLHttpRequest();              
    } else {                                  
      AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (AJAX==null) {                             
      return false;                               
    } else {
      AJAX.onreadystatechange = function() {  
        if (AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(AJAX.responseText,AJAX.status,AJAX.responseXML);        
          delete AJAX;                                         
        }                                                      
      }                                                        
      var timestamp = new Date();                              
      if (postMethod=='POST') {
        var uri=urlCall+'?'+timestamp.getTime();
        AJAX.open("POST", uri, true);
        AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(timestamp*1); 
        AJAX.open("GET", uri, true);                             
        AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}  

function attachScript(responseText, responseStatus) {
   if (responseStatus==200) {
        eval(responseText);
     }
}

function viewDay(day,month,year){
	$connector = new ajaxObject('calendar/getContent.php',renderContent);
	$connector.update('day='+day+'&month='+month+'&year='+year);
	var obj = document.getElementById('content');
	obj.innerHTML = "<img src='calendar/calWaiting.gif'>";
}

function renderContent(responseText, responseStatus) {
	if (responseStatus==200) {
		var obj = document.getElementById('content');
		obj.innerHTML = responseText;
	}else{
		alert('Error in communication');
	}
}