function createOneCalendar(id,title,color,active){
var listDiv = document.getElementById('my_calendar');
var oldDiv = document.getElementById('calendar_'+id);

var calendarDiv = document.createElement('DIV');
calendarDiv.id='calendar_'+id;
calendarDiv.className='calendar_list';
calendarDiv.innerHTML = title;
if(active == 1){
calendarDiv.style.backgroundColor=colors[color][2];
calendarDiv.onmouseover=new Function('this.style.color=\'#000000\'; this.style.backgroundColor=\''+colors[color][4]+'\';');
calendarDiv.onmouseout=new Function('this.style.color=\'#ffffff\'; this.style.backgroundColor=\''+colors[color][2]+'\';');
calendarDiv.onclick = kliknij;
}else{
calendarDiv.style.backgroundColor='#ffffff';
calendarDiv.style.color='#000000';
calendarDiv.onmouseover=new Function('this.style.color=\'#000000\'; this.style.backgroundColor=\''+colors[color][4]+'\';');
calendarDiv.onmouseout=new Function('this.style.color=\'#000000\'; this.style.backgroundColor=\'#ffffff\';');;
calendarDiv.onclick = kliknij2;
}

if(oldDiv)
	listDiv.replaceChild(calendarDiv,oldDiv);
else
	listDiv.appendChild(calendarDiv);


var oldButtonDiv = document.getElementById('calendar_buttons_'+id);

var calendarButtonDiv = document.createElement('DIV');
calendarButtonDiv.id='calendar_buttons_'+id;
calendarButtonDiv.className='calendar_buttons';
calendarButtonDiv.innerHTML = '';
calendarButtonDiv.onclick=ContextMouseDown;

pos = (color-1)*15 > 0 ? (color-1)*15 : 0;
calendarButtonDiv.style.backgroundPosition = -pos+'px 0px';

if(oldButtonDiv)
	listDiv.replaceChild(calendarButtonDiv,oldButtonDiv);
else{
	listDiv.appendChild(calendarButtonDiv);
	var clearDiv = document.createElement('DIV');
	clearDiv.className='clearing';
	listDiv.appendChild(clearDiv);
}



return calendarDiv;
}


function clearCalendars()
{
for(var prop in configProperties){
if(configProperties[prop]['id']){
	if(document.getElementById('calendar_'+configProperties[prop]['id'])){
		var obj = document.getElementById('calendar_'+configProperties[prop]['id']);
		obj.parentNode.removeChild(obj);
	}
	if(document.getElementById('calendar_buttons_'+configProperties[prop]['id'])){
		var obj = document.getElementById('calendar_buttons_'+configProperties[prop]['id']);
		obj.parentNode.removeChild(obj);
	}
	configProperties[prop]['id'] = false;
}

}
}


function clearCalendars2(calid)
{
for(var prop in configProperties){
if(configProperties[prop]['id'] && configProperties[prop]['id'] == calid){
	if(document.getElementById('calendar_'+configProperties[prop]['id'])){
		var obj = document.getElementById('calendar_'+configProperties[prop]['id']);
		obj.parentNode.removeChild(obj);
	}
	configProperties[prop]['id'] = false;
}

}
}

function getConfigFromServer(calId){var ajaxIndex = weekSchedule_ajaxObjects.length;weekSchedule_ajaxObjects[ajaxIndex] = new sack();weekSchedule_ajaxObjects[ajaxIndex].requestFile = externalSourceFile_config  + '?id=' + userId;	weekSchedule_ajaxObjects[ajaxIndex].onCompletion = function(){ parseConfigFromServer(ajaxIndex); getItemsFromServer(calId); getItemsAllDayFromServer(calId);};weekSchedule_ajaxObjects[ajaxIndex].runAJAX();		
}

function getConfigMonthFromServer(calId)
{
var ajaxIndex = weekSchedule_ajaxObjects.length;weekSchedule_ajaxObjects[ajaxIndex] = new sack();weekSchedule_ajaxObjects[ajaxIndex].requestFile = externalSourceFile_config  + '?id=' + userId;	weekSchedule_ajaxObjects[ajaxIndex].onCompletion = function(){ parseConfigFromServer(ajaxIndex); getItemsAllDayMonthFromServer(calId);};	weekSchedule_ajaxObjects[ajaxIndex].runAJAX();		}

function parseConfigFromServer(ajaxIndex)
{

var xotree = new XML.ObjTree();
var configToBeCreated = xotree.parseDOM(weekSchedule_ajaxObjects[ajaxIndex].responseXML)

if(!configToBeCreated['#document']['error']){

weekSchedule_ajaxObjects[ajaxIndex] = false;

var ile = 1;
for (var i in configToBeCreated['#document']['configuration']['config']) {
	if(!isNaN(i)) ile++;
}

if(ile>1){
	configToBeCreated = configToBeCreated['#document']['configuration']['config'];
}else{
	configToBeCreated = configToBeCreated['#document']['configuration'];
}


var firstCalendar = 0;
activeCalendarId = 0;

for (var i in configToBeCreated) {
if(configToBeCreated[i]['id']){
currentCalendar = createOneCalendar(configToBeCreated[i]['id'],configToBeCreated[i]['title'],configToBeCreated[i]['color'],configToBeCreated[i]['active']);
currentCalendar = false;		var newIndex = configToBeCreated[i]['id'];		configProperties[newIndex] = new Array();		configProperties[newIndex]['id'] = configToBeCreated[i]['id'];		configProperties[newIndex]['title'] = configToBeCreated[i]['title'];		configProperties[newIndex]['active'] = configToBeCreated[i]['active'];		configProperties[newIndex]['color'] = configToBeCreated[i]['color'];		configProperties[newIndex]['object'] = currentCalendar;		if(!firstCalendar) firstCalendar = configToBeCreated[i]['id'];
if(activeCalendarId == 0 && configToBeCreated[i]['active'] == 1)
activeCalendarId = configToBeCreated[i]['id'];
}
}

if(!activeCalendarId) activeCalendarId = firstCalendar;

}else{
alert(configToBeCreated['#document']['error']);
}
}

function kliknij(e,inputDiv)
{
if(!inputDiv)inputDiv = this;

inputDiv.style.backgroundColor='#ffffff';
inputDiv.style.color='#000000';
inputDiv.onmouseout=new Function('this.style.color=\'#000000\'; this.style.backgroundColor=\'#ffffff\';');;

clearAppointments2(inputDiv.id.substr(9));
clearAppointments3(inputDiv.id.substr(9));
inputDiv.onclick = kliknij2;

saveConfigFromServer(inputDiv.id.substr(9),0);
}

function kliknij2(e,inputDiv)
{
if(!inputDiv)inputDiv = this;
inputDiv.style.backgroundColor='#ffffff';
inputDiv.style.color='#000000';
inputDiv.onmouseout=new Function('this.style.color=\'#000000\'; this.style.backgroundColor=\'#ffffff\';');;

inputDiv.style.backgroundColor=colors[configProperties[inputDiv.id.substr(9)]['color']][0];
inputDiv.onmouseover=new Function('this.style.color=\'#000000\'; this.style.backgroundColor=\''+colors[configProperties[inputDiv.id.substr(9)]['color']][2]+'\';');
inputDiv.onmouseout=new Function('this.style.color=\'#ffffff\'; this.style.backgroundColor=\''+colors[configProperties[inputDiv.id.substr(9)]['color']][0]+'\';');

saveConfigFromServer(inputDiv.id.substr(9),1);
inputDiv.onclick = kliknij;

}

function saveConfigFromServer(id,active)
{
var ajaxIndex = weekSchedule_ajaxObjects.length;
weekSchedule_ajaxObjects[ajaxIndex] = new sack();
weekSchedule_ajaxObjects[ajaxIndex].requestFile = externalSourceFile_configsave  + '?id=' + id + '&active=' + active;	

if(activeCalendarView != 'day' && activeCalendarView != 'month' && activeCalendarView != 'week'){
returntocalendar();
}


if(activeCalendarView == 'month')
weekSchedule_ajaxObjects[ajaxIndex].onCompletion = function(){ getConfigMonthFromServer(id); };	
else
weekSchedule_ajaxObjects[ajaxIndex].onCompletion = function(){ getConfigFromServer(id); };	

weekSchedule_ajaxObjects[ajaxIndex].runAJAX();		
}

function saveOnlyConfigFromServer(id)
{
var ajaxIndex = weekSchedule_ajaxObjects.length;
weekSchedule_ajaxObjects[ajaxIndex] = new sack();
weekSchedule_ajaxObjects[ajaxIndex].requestFile = externalSourceFile_configsave  + '?id=' + id + '&userId=' + userId + '&only=1';	


if(activeCalendarView != 'day' && activeCalendarView != 'month' && activeCalendarView != 'week'){
returntocalendar();
}


if(activeCalendarView == 'month') 
weekSchedule_ajaxObjects[ajaxIndex].onCompletion = function(){var ideki=weekSchedule_ajaxObjects[ajaxIndex].response.split(",");	for(var i=0;i<ideki.length;i++){		clearAppointments3(ideki[i]); } getConfigMonthFromServer();
};	

else
weekSchedule_ajaxObjects[ajaxIndex].onCompletion = function(){

var ideki=weekSchedule_ajaxObjects[ajaxIndex].response.split(",");
for(var i=0;i<ideki.length;i++){
	clearAppointments();
	clearAppointments3(ideki[i]);
}
getConfigFromServer();
};	

weekSchedule_ajaxObjects[ajaxIndex].runAJAX();		
}
