//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------

OnLoadFunctions[OnLoadFunctions.length] = "CALENDAR = new Calendar(); CALENDAR.init();";

function Calendar() {
	
	// privat vars
	this.header = false;
	this.title = 'Kalendar';
	this.content = false;
	this.id = false;
	this.dataUrl 		= '/php-os/app_kinderschutz/scripts/ajax_request_calendar.php'; // XML-Page of calendar entry
	this.editorUrl 		= '/php-os/app_kinderschutz/scripts/ajax_request_editor_calendar.php'; // XML-Page of calendar entry
	//this.window = false;
	
	//---------------------------------------------------------------------------------------
	this.init = function(sid) {
		
		//if(!this.window) this.buildWindow();
		
		
	}



	//---------------------------------------------------------------------------------------
	// loads an XML-Page through XHTTP-Object
	this.getData = function(sid) {
		
		// get sid
		if(!sid) return false;

		myAjax = new ajax();
		myAjax.initialize(this.dataUrl, {method:'post',postBody: 'sid='+sid+'&colorsuffix='+active_color_set, onComplete: CALENDAR.getResponse}); // update: $('testrequest'),

	}

	//---------------------------------------------------------------------------------------
	// loads an XML-Page through XHTTP-Object
	this.getEditor = function(event,date,context) {
		KS.window.clear();
		// get sid
		if(!date) return false;
		
		myAjax = new ajax();
		myAjax.initialize(this.editorUrl, {method:'post',postBody: 'context='+context+'&date='+date+'&colorsuffix='+active_color_set, onComplete: CALENDAR.getResponse}); // update: $('testrequest'),

	}
	
	//---------------------------------------------------------------------------------------
	// just a container for AJAX Response and call to Respond()
	this.getResponse = function(request) {
		JRESPONSE = new Respond(request.responseXML);
		JRESPONSE.interpretResponse();
	}
	//---------------------------------------------------------------------------------------
	// is called via Respond() through type of XML-node evaluatetype
	this.viewData = function(node) {
		
		// node from XMLResponse
		fields = node.getElementsByTagName("fields");
		var fl = fields.length;
		
		this.fields = new Array();
		
		for(i=0;i<fl;i++) {
			

			var nodes = fields[i].childNodes;
			nl = nodes.length;
			
			//for(var ch in nodes) { // go through childs of fields 
			for(var fidx=0;fidx<nl;fidx++) {
				if(!nodes[fidx].nodeType) continue;
				switch(nodes[fidx].nodeType) {
					case  1:

						update_field = false;
						tagName = nodes[fidx].tagName;
						this.fields[tagName] = new Array();

						this.fields[tagName]['name'] = tagName;
						this.fields[tagName]['attr'] = JRESPONSE.parseAttributes(nodes[fidx]);

						// get value
						if(nodes[fidx].firstChild // of data
						//&& (nodes[fidx].firstChild.nodeType == 3)
						&& (nodes[fidx].firstChild.data)
						&& (nodes[fidx].firstChild.data.length>0)) { // text
							this.fields[tagName]['value'] = nodes[fidx].firstChild.data; //unescape(nodes[fidx].firstChild.data);
						} else if(this.fields[tagName]['attr']  // of attribute value
						&& (this.fields[tagName]['attr']['value'])
						&& (this.fields[tagName]['attr']['value'].length > 0)) {
							this.fields[tagName]['value'] = fields[i][tagName]['attr']['value'];
						} else {
							this.fields[tagName]['value'] = '';
						}
						
						break;
				}
			}
		}
		this.updateWindow();
		
	}


	//---------------------------------------------------------------------------------------
	// is called via Respond() through type of XML-node evaluatetype
	this.viewEditorData = function(node) {

		//DebugDump('huhu','huhu');
		// node from XMLResponse
		editorfield = node.getElementsByTagName("editor");
		this.editorAttr = JRESPONSE.parseAttributes(editorfield[0]);
		//DebugDump(this.editorAttr,'this.editorAttr');
		this.editorData = editorfield[0].firstChild.data; //unescape(nodes[fidx].firstChild.data);
		//DebugDump(this.editorData,'this.editorData');
		//this.updateWindow();
		
		KS.window.updateWindow(this.editorAttr['date'],this.editorData);
		//this.window.updateWindow(this.editorAttr['date'],this.editorData);
	}
	
	//---------------------------------------------------------------------------------------
	this.error = function(node) {
		// node from XMLResponse
		this.XMLerror = node.getElementsByTagName("error")[0];
		// get attributes of this node
		this.errorAttr = new Array(); 
		this.errorAttr = JRESPONSE.parseAttributes(this.XMLerror);
		alert(this.errorAttr['value']);
	}
	
	//---------------------------------------------------------------------------------------
	this.updateWindow = function() {
		this.w_wrapper.style.display = '';
		for(f_tagname in this.fields) {
			this[f_tagname] = this.fields[f_tagname];
		}
		content = '';
		content += '<p>';
		content += 'Datum: '+this.start_date['value'];
		if(this.start_time['value']>0) content += ', '+this.start_time['value']+' Uhr';
		content += '<br>';
		//DebugDump(this.end_date['value'],'this.end_date[value]');
		var t = parseInt(this.end_date['value']); 
		if(t>0) {
			//content += '<p>';
			content += 'Voraussichtliches Ende: '+this.end_date['value'];
			if(this.end_time['value']>0) content += ', '+this.end_time['value']+' Uhr';
			content += '<br>';		
		}
		
		//content += '<br>';
		if(this.location['value'].length > 0) {
			content += 'Ort: '+this.location['value'];
			content += '<br>';		
			//content += '<p>';
		}
		content += 'Angelegt von: '+this.owner_completename['value'];
		content += '</p>';
		
		content += '<p>';
		content += this.description['value'];
		content += '</p>';
		content += this.buttons['value'];
		this.window.updateWindow(this.title['value'],content);
	}
}





















