var Objdate = new Date();
var year  = Objdate.getFullYear();
var month = Objdate.getMonth();
var day   = Objdate.getDay();
var date  = Objdate.getDate();

var tempYear  = Objdate.getFullYear();
var tempMonth = Objdate.getMonth();
var tempDate  = Objdate.getDate();

var xmlDoc = isNaN;
var index = 0;
var yearMonth = "";
var xmlString = "";

function initMonthsDays() {
 	
	yearMonth = new Array();
	
	yearMonth[0] = 31;
	yearMonth[1] = ((year % 4) == 0) ? 29 : 28; 
	yearMonth[2] = 31;
	yearMonth[3] = 30;
	yearMonth[4] = 31;
	yearMonth[5] = 30;
	yearMonth[6] = 31;
	yearMonth[7] = 31;
	yearMonth[8] = 30;
	yearMonth[9] = 31;
	yearMonth[10] = 30;
	yearMonth[11] = 31;

}

initMonthsDays();	

function loadCalendar() {

	var dateInFirstWeek = date % 7 + 1;
	
	if (day > 6)
		day = getDayMinPos(day);
	else if(day < 0)
		day = getDayPos(day);
		
	var getDayIndex = day - (dateInFirstWeek - 1);
	
	if (getDayIndex < 0)
		index = getDayPos(getDayIndex);
	else if (getDayIndex > 6)
		index = getDayMinPos(getDayIndex);
	else
		index = getDayIndex;
		
	var monthDay = getCurrentMonth(month);
	var monthName = getCurrentMonthName(month);
	
	document.getElementById('calendarmonth').innerHTML = monthName.toUpperCase();
	document.getElementById('calendaryear').innerHTML = year;
	
	setDateNumbers(index, monthDay);
	
}
function getDayPos(dayIndex) {
	
	var pIndex = 0;
	
	if(dayIndex == -1)
		pIndex = 6;
	else if(dayIndex == -2)
		pIndex = 5;
	else if(dayIndex == -3)
		pIndex = 4;
	else if(dayIndex == -4)
		pIndex = 3;
	else if(dayIndex == -5)
		pIndex = 2;
	else if(dayIndex == -6)
		pIndex = 1;
	else if(dayIndex == -7)
		pIndex = 0;
		
	return pIndex; 

}

function getDayMinPos(minDayIndex) {
	
	var mIndex = 0;
	
	if(minDayIndex == 7)
		mIndex = 0;
	else if(minDayIndex == 8)
		mIndex = 1;
	else if(minDayIndex == 9)
		mIndex = 2;
	else if(minDayIndex == 10)
		mIndex = 3;
	else if(minDayIndex == 11)
		mIndex = 4;
	else if(minDayIndex == 12)
		mIndex = 5;
	else if(minDayIndex == 13)
		mIndex = 6;
		
	return mIndex; 

}

function setDateNumbers(dateIndex, dateMonthDay) {
	
	for (var i = 0; i <= (dateMonthDay-1); i++) {
		
		var dateValue = i + 1;
		var cell = document.getElementById('c' + (dateIndex + i)); 
		
		if(dateValue == tempDate && month == tempMonth && year == tempYear)
			cell.className = "thisdate";
		else
			cell.className = "none";
		
		cell.innerHTML = i + 1;
	}

	loadXML();
}

function getNextMonth() {
	
	var thisMonth = getCurrentMonth(month);
	month = month + 1;
	
	if(month > 11) {
		year = year + 1;
		initMonthsDays();
		month = 0;
	}
	
	var nextMonth = getCurrentMonth(month);
	cleanCalendar();
	
	if (thisMonth == 31 && nextMonth == 31) {
		day = day + 3;
		loadCalendar();
	}
	else if  (thisMonth == 31 && nextMonth == 30){
		day = day + 3;
		loadCalendar();
	}
	else if  (thisMonth == 31 && nextMonth == 29){
		day = day + 3;
		loadCalendar();
	}
	else if  (thisMonth == 31 && nextMonth == 28){
		day = day + 3;
		loadCalendar();
	}
	else if  (thisMonth == 30 && nextMonth == 31){
		day = day + 2;
		loadCalendar();
	}
	else if  (thisMonth == 29 && nextMonth == 31){
		day = day + 1;
		loadCalendar();
	}
	else if  (thisMonth == 28 && nextMonth == 31){
		loadCalendar();
	}
}

function getPreviousMonth() {
	
	var thisMonth = getCurrentMonth(month);
	month = month - 1;
	
	if(month < 0) {
		year = year - 1;
		initMonthsDays();
		month = 11;
	}
	
	var nextMonth = getCurrentMonth(month);
	cleanCalendar();
	
	if (thisMonth == 31 && nextMonth == 31) {
		day = day - 3;
		loadCalendar();
	}
	else if  (thisMonth == 31 && nextMonth == 30){
		day = day - 2;
		loadCalendar();
	}
	else if  (thisMonth == 31 && nextMonth == 29){
		day = day - 1;
		loadCalendar();
	}
	else if  (thisMonth == 31 && nextMonth == 28){
		loadCalendar();
	}
	else if  (thisMonth == 30 && nextMonth == 31){
		day = day - 3;
		loadCalendar();
	}
	else if  (thisMonth == 29 && nextMonth == 31){
		day = day - 3;
		loadCalendar();
	}
	else if  (thisMonth == 28 && nextMonth == 31){
		day = day - 3;
		loadCalendar();
	}
}

function getCurrentMonth(currentMonth) {
	return yearMonth[currentMonth];
}

function getCurrentMonthName(monthId) {
	
	var monthName = new Array();
	
	monthName[0]  = "OCAK";
	monthName[1]  = "&#350;UBAT";
	monthName[2]  = "MART";
	monthName[3]  = "N&#304;SAN";
	monthName[4]  = "MAYIS";
	monthName[5]  = "HAZ&#304;RAN";
	monthName[6]  = "TEMMUZ";
	monthName[7]  = "A&#286;USTOS";
	monthName[8]  = "Eylül";
	monthName[9]  = "EK&#304;M";
	monthName[10] = "KASIM";
	monthName[11] = "ARALIK";
	
	return monthName[monthId];

}

function cleanCalendar() {
	for (var i = 0; i <= 41; i++) {
		var cleanCell = document.getElementById("c" + i);
		if (cleanCell.className == "thisdate")
			cleanCell.className = "none";
		cleanCell.innerHTML = "&nbsp;"
	}
}

function loadXML() {
	
	var url = "/xht?r=calendar_notes&year=" + year + "&month=" + (month + 1);
	if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.load(url);
		parseMessage()
	}

	else if (document.implementation && document.implementation.createDocument){
		xmlDoc = document.implementation.createDocument("","",null);
		xmlDoc.load(url);
  		xmlDoc.onload=parseMessage
	}

	else {
		alert('Your browser cannot handle this script');
	}
	
}

function parseMessage() {
	
	var noteFlag = false;
	var nodeList = xmlDoc.getElementsByTagName("note");
	if(nodeList.length == 0 && month == tempMonth && year == tempYear) {
		showNoteMessage("<table><tr><td>" + "Bug&uuml;nl&uuml;k not yok." + "</td></tr></table>", tempDate);
	}
		
	for(var i = 0; i < nodeList.length; i++) {
		var xmlDate = nodeList[i].getElementsByTagName("date")[0].firstChild.nodeValue;
		var messageList = nodeList[i].getElementsByTagName("message");
		var message = "";
		message += "<table>"
		for(var x = 0; x < messageList.length; x++) {
			message += "<tr><td>" + messageList[x].firstChild.nodeValue + "</td></tr>";
			if (x != (messageList.length - 1))
				message += "<tr><td></td></tr>";
		}		
		message += "</table>"	
		if (xmlDate == tempDate)
			noteFlag = true;
		for (var j = 0; j <= 41; j++) {
				var cellNumber = document.getElementById("c" + j);
				if(xmlDate == cellNumber.innerHTML) {
					if(cellNumber.innerHTML == date)
						if(xmlDate == tempDate && month == tempMonth && year == tempYear)
							showNoteMessage(message, xmlDate);
					
					cellNumber.innerHTML = "<span onclick=\"showNoteMessage('"+message+"', '"+xmlDate+"', true);\" class=\"underline\">" + cellNumber.innerHTML + "</span>";
				}
		}
		
		if (noteFlag == false && date == tempDate && month == tempMonth && year == tempYear)
			showNoteMessage("<table><tr><td>" + "Bug&uuml;nl&uuml;k not yok." + "</table></tr></td>", tempDate);
		
	}
}

function showNoteMessage (noteMessage, noteDate, showFlag) {
	
	document.getElementById("notetext").innerHTML = noteMessage;
	
	var noteMonth = month + 1; 
	if(noteMonth < 10)
		noteMonth = "0" + noteMonth;
	if(noteDate < 10)
		noteDate = "0" + noteDate;
	
	if(noteDate == tempDate && month == tempMonth && year == tempYear) {
		document.getElementById("notedate").innerHTML = "Bugün";
	}
	
	else {
		if(showFlag)
			document.getElementById("notedate").innerHTML = year + "-" + noteMonth + "-" + noteDate;
	}
}