var xmlHttp;
var entrydays;
var global_year;
var global_month;
var prev_month_y;
var prev_month_m;
var next_month_y;
var next_month_m;

function takvim(year, month) { 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {alert ("Your browser does not support AJAX!"); return;}
	var url="/js/refresh_calendar.php";
	
	global_year=year;
	global_month=month;
	
	month_int=parseInt(month,10);
	year_int=parseInt(year,10);
	
	if (month_int==1) {
		prev_month_y=year_int-1;
		prev_month_m='12';
	}
	else {
		prev_month_y=year_int;
		prev_month_m= (month_int-1<10) ? '0'+(month_int-1) : month_int-1;
	}
	
	if (month_int==12) {
		next_month_y=year_int+1;
		next_month_m='01';
	}
	else {
		next_month_y=year_int;
		next_month_m= (month_int+1<10) ? '0'+(month_int+1) : month_int+1;
	}
	
	year="year="+year;
	month="month="+month;
	str=year+"&"+month;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-9");
	xmlHttp.onreadystatechange=refresh_calendar;
	xmlHttp.send(str);
	
	return false;
}

function refresh_calendar() {
	if (xmlHttp.readyState==4) {
		var txt = xmlHttp.responseText;
		
		if (txt=='stop' || txt=='') {return false;}
		
		txt=unescape(txt);
		theResponse=txt.split("-");
		var startWeekday=theResponse[0];
		var daysinMonth=theResponse[1];
		entrydays=theResponse[2];
		var datestr=theResponse[3];
		
		var theTable = document.getElementById("gunler");
		
		rowcount=theTable.rows.length;
		for(var i = 0; i < rowcount; i++) {
			theTable.deleteRow(0);
		}
		
		
		
		document.getElementById("ay-yil").childNodes[0].nodeValue=datestr;
		document.getElementById("onceki-ay").onclick=prev_month_f;
		document.getElementById("sonraki-ay").onclick=next_month_f;
		
		var cells=new Array();
		var i=0;
		for (i=0;i<startWeekday;i++) {
			cells[i]=' ';
		}
		for (var j=1;j<=daysinMonth;j++) {
			cells[i++]=j;
			if (i==7) {
				addRow (cells, theTable);
				i=0;
				cells.length=0;
			}
		}
		if (i>0) { addRow (cells, theTable); }
	}
}

function addRow(cells, table) {
  var tr = document.createElement ('tr');
  for (var i=0,cell_length=cells.length; i<cell_length; i++) {
    var td = document.createElement ('td');
	
	 
	 if (entrydays.indexOf('('+cells[i]+')')!=-1) {
		var anchor = document.createElement("a");
		anchor.appendChild (document.createTextNode (cells[i]));
		var tempday = (cells[i]<10) ? '0'+cells[i] : cells[i];
		anchor.href = '/tarih/'+global_year+'/'+global_month+'/'+tempday;
		td.appendChild (anchor);
		td.className='dolu-gun';
	}
	 else {
		td.appendChild (document.createTextNode (cells[i]));
	 }
	
	
    tr.appendChild (td);
  }
  table.appendChild (tr);
}


function prev_month_f() { takvim(prev_month_y, prev_month_m); return false; }
function next_month_f() { takvim(next_month_y, next_month_m); return false; }

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}
	catch(e) {
	  // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	}
	return xmlHttp;
}