var xmlHttp;
var xmlHttp2;

var time = null;
var sort = "";
var desc = 0;

function setTime() {
	time = new Date();
}

function updateCalendarContent() {
	xmlHttp2=GetXmlHttpObject();
	if (xmlHttp2==null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var url="calendarcontent.php";
	url=url+"?year="+time.toString('yyyy');
	url=url+"&sort="+sort;
	url=url+"&desc="+desc;
	xmlHttp2.onreadystatechange=stateChangedCalendar;
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);
}

function getCalendar(next, _sort, _desc)
{ 	
	
	if (time == null) {
		time = new Date();
	} else {	
		if (next > time.toString('yyyy')) {
			time = time.add({years: 1});			
		} else {
			time = time.add({years: -1});
		}
	}
	
	sort = typeof(_sort) == "undefined" ? "begin" : _sort ;
	desc = typeof(_desc) == "undefined" ? 1 : _desc ;	
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	
	var thisyear = Math.round(time.getTime()/1000.0);
	
	var url="calendar.php";
	url=url+"?time="+thisyear;
	//url=url+"&property="+property;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function showCalendarMonth(next, month) {	
	getCalendarByMonth(month);
	$("calendar").scrollTo();
}

function getCalendarByMonth(month)
{ 			
	if (month == 1){
		if (time.toString('M') == 1) {
			time = time;
		} else {
			time = time.moveToMonth(0, -1);
		}
	} else if (month == 2){
		if (time.toString('M') == 2) {
			time = time;
		} else if (time.toString('M') > 2) {
			time = time.moveToMonth(1, -1);
		} else {
			time = time.moveToMonth(1);
		}
	} else if (month == 3){
		if (time.toString('M') == 3) {
			time = time;
		} else if (time.toString('M') > 3) {
			time = time.moveToMonth(2, -1);
		} else {
			time = time.moveToMonth(2);
		}	
	} else if (month == 4){
		if (time.toString('M') == 4) {
			time = time;
		} else if (time.toString('M') > 4) {
			time = time.moveToMonth(3, -1);
		} else {
			time = time.moveToMonth(3);
		}		
	} else if (month == 5){
		if (time.toString('M') == 5) {
			time = time;
		} else if (time.toString('M') > 5) {
			time = time.moveToMonth(4, -1);
		} else {
			time = time.moveToMonth(4);
		}	
	} else if (month == 6){
		if (time.toString('M') == 6) {
			time = time;
		} else if (time.toString('M') > 6) {
			time = time.moveToMonth(5, -1);
		} else {
			time = time.moveToMonth(5);
		}	
	} else if (month == 7){
		if (time.toString('M') == 7) {
			time = time;
		} else if (time.toString('M') > 7) {
			time = time.moveToMonth(6, -1);
		} else {
			time = time.moveToMonth(6);
		}	
	} else if (month == 8){
		if (time.toString('M') == 8) {
			time = time;
		} else if (time.toString('M') > 8) {
			time = time.moveToMonth(7, -1);
		} else {
			time = time.moveToMonth(7);
		}	
	} else if (month == 9){
		if (time.toString('M') == 9) {
			time = time;
		} else if (time.toString('M') > 9) {
			time = time.moveToMonth(8, -1);
		} else {
			time = time.moveToMonth(8);
		}	
	} else if (month == 10){
		if (time.toString('M') == 10) {
			time = time;
		} else if (time.toString('M') > 10) {
			time = time.moveToMonth(9, -1);
		} else {
			time = time.moveToMonth(9);
		}		
	} else if (month == 11){
		if (time.toString('M') == 11) {
			time = time;
		} else if (time.toString('M') > 11) {
			time = time.moveToMonth(10, -1);
		} else {
			time = time.moveToMonth(10);
		}	
	} else if (month == 12){
		if (time.toString('M') == 12) {
			time = time;
		} else {
			time = time.moveToMonth(11);
		}	
	}
		
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	
	var thisyear = Math.round(time.getTime()/1000.0);
	
	var url="calendar.php";
	url=url+"?time="+thisyear;
	//url=url+"&property="+property;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getBookingInfo(divname, parentdiv)
{ 	
	document.getElementById(divname).style.top = parentdiv.parentNode.offsetTop +15 - document.getElementById(divname).offsetHeight +"px";
	document.getElementById(divname).style.left = parentdiv.parentNode.offsetLeft - 86+"px";	
	document.getElementById(divname).style.display='inline';
	document.getElementById(divname).style.top = parentdiv.parentNode.offsetTop +86 - document.getElementById(divname).offsetHeight +"px";
}

function hideBookingInfo(divname, parentdiv)
{ 		
	document.getElementById(divname).style.display='none';
}


function stateChanged() { 
	if (xmlHttp.readyState==4) { 
		document.getElementById("calendar").innerHTML=xmlHttp.responseText;
		updateCalendarContent();
	}
}

function stateChangedCalendar() { 
	if (xmlHttp2.readyState==4) { 
		
		document.getElementById("calendarcontent").innerHTML=xmlHttp2.responseText;
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
	  xmlHttp=new XMLHttpRequest();
	} catch (e){
	  try {
		  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }catch (e){
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
	return xmlHttp;
}
