/*

AJAX CMS Handler Object
Using DOJO
V0.2

Laki Zsolt
V 0.1 - 2007-04-21

V 0.2 - 2007-05-03
	- Run a PHP without any visible result
	- Call DOJO sync

*/

CMS_Handler = function() {

	var instance = this;
	var main_url;
	var load_div_id;
	var show_loading;

	// Initialize the Object
	//
	// link - the main page to handler requests
	// load_div_id - the div show while loading
	this.init = function(link, load_div_id) {

		dojo.require("dojo.widget.*");
		dojo.require("dojo.widget.ContentPane");
		dojo.require("dojo.widget.Dialog");
		dojo.require("dojo.widget.LayoutContainer");
		dojo.require("dojo.widget.FloatingPane");

		dojo.require("dojo.io.*");
		dojo.require("dojo.io.IframeIO");

		dojo.hostenv.writeIncludes();

		this.main_url = link + '?ajax=1'

		this.load_div_id = load_div_id;
		this.show_loading = false;
	}

	// alert a message in a dialog div
	//
	// message - the message visible in the dialog
	this.alert = function(message, no_close_button) {

		var dialog_div = document.getElementById('dialog');
		if (! dialog_div) {
			dialog_div = document.createElement('div');
			dialog_div.setAttribute('id', 'dialog');
			dialog_div.setAttribute('dojoType', 'dialog');
			dialog_div.setAttribute('bgColor', 'black');
			dialog_div.setAttribute('bgOpacity', '0.5');
			dialog_div.setAttribute('toggle', 'fade');
			dialog_div.setAttribute('refreshOnShow', 'true');
			dialog_div.setAttribute('parseContent', 'true');
			dialog_div.setAttribute('toggleDuration', '1');
			var body = document.getElementsByTagName('body');
			body.item(0).appendChild(dialog_div);
		}

		dojo.html.disableSelection(dialog_div);
		dialog_div.innerHTML = message;

		if (! no_close_button) {
			var button = dojo.byId('dialog_close_button');
			if (! button) {
				button = document.createElement('input');
				button.setAttribute('type', 'button');
				button.setAttribute('id', 'dialog_close_button');
				button.setAttribute('value', 'MA©gsem');
				dialog_div.appendChild(button);
			}
		}

		instance.createWidget('dialog');
		instance.evalScripts(dojo.byId('dialog'));

		var dialog = dojo.widget.byId('dialog');
		if (! no_close_button) {
			dialog.setCloseControl(button);
		}
		dialog.show();
	}

	// send a form using ajax
	// if there no content after the form request and there is a div_url to load a content
	// the div_id innerHTML is canged to the div_url content
	//
	// form_id - the form id to send
	// div_url - the url called after there is no replay from the form. default is 'content'
	// div_id - the div id refresh by the div_url
	this.sendForm = function(form_id, div_url, div_id) {

		instance.addToForm(form_id, 'ajax', '1');

		var form = document.getElementById(form_id);
		var file = form.getAttribute('enctype');
		var url = form.getAttribute('action');

		if (file) {
			if (file == 'application/x-www-form-urlencoded') {
				file = null;
			}
		}

		var transport = (file) ? "IframeTransport" : "XMLHTTPTransport";

		instance.callDOJOIOBind({
			formNode: form,
			url: url,
			transport: transport,
			load: function(data) {
				if (div_url && ! data.length) {
					instance.loadDiv(div_url, (div_id) ? div_id : 'content');
				} else {
					if (data.length) {
						instance.alert(data);
					} else {
						instance.loadDiv(div_url, (div_id) ? div_id : 'content');
					}
				}
			}
		});
	}

	// remove all popup divs
	this.removeDialog = function() {

		var dialog = dojo.widget.byId('dialog');
		if (dialog) {
			dialog.hide();
			dialog.uninitialize();
		}
		this.show_loading = false;
	}

	// search the script tag and run the content
	//
	// object - an element object whitch contains a script tag
	this.evalScripts = function(object) {

		var scripts = object.getElementsByTagName('script');
		for(var i = 0; i < scripts.length; i++) {
			var script = scripts[i];
			var text = '';
			if (script.text) {
				text = script.text;
			} else {
				if (script.childNodes) {
					text = dojo.dom.textContent(script);
				}
			}
			if (text.match("<!--")) {
				text = text.replace("<!--", " ");
			}
			eval (text);
		}
	}

	// create a DOJO widget from the element
	//
	// name - the id name of the element
	this.createWidget = function(name) {

		var div = dojo.byId(name);
		var xmlParser = new dojo.xml.Parse();
		var frag = xmlParser.parseElement(div, null, true);
		dojo.widget.getParser().createComponents(frag);
	}

	// handle the DOJO.io.bind errors
	//
	// error - the dojo error object
	this.handleError = function(error) {

		// DOJO 0.4.2 Bug
		// XMLHttpTransport.watchInFlight Error in IE

		var error_msg = dojo.errorToString(error);
		if (! error_msg.match("watchInFlight")) {
			instance.alert(error_msg);
		}
	}

	// loading a content and display in a div
	//
	// url - the url parameter called by
	// div_id - the div id name replace by the answer
	this.loadDiv = function(url, div_id) {

		var url = this.main_url + '&' + url;

		instance.callDOJOIOBind({
			url: url,
			transport: "XMLHTTPTransport",
			load: function(data) {

				var div = dojo.byId(div_id);
				if (! div) {
					div = document.createElement('div');
					div.setAttribute('id', div_id);
					var body = document.getElementsByTagName('body');
					body.item(0).appendChild(div);
				}
				div.innerHTML = data;
				instance.evalScripts(div);
				instance.hideLoadingPanel();
			}
		});
	}

	// call a PHP script without any replay
	//
	// url - the url parameter called by
	this.runPHP = function(url) {

		var url = this.main_url + '&' + url;

		instance.callDOJOIOBind({
			url: url,
			transport: "XMLHTTPTransport",
			load: function(data) {
				if (data.length) {
					instance.alert(data);
				}
			}
		});
	}

	// replace a div content by an another
	//
	// data - the new content of the div
	// id - the div id
	this.showDiv = function(data, id) {

		var div = document.getElementById(id);
		if (div) {
			div.innerHTML = data;
			instance.evalScripts(div);
		}
	}

	// load a content and show on a dialog div (popup)
	//
	// url - the url params load on the div
	this.loadDialog = function(url) {

		var url = this.main_url + '&' + url;
		instance.callDOJOIOBind({
			url: url,
			transport: "XMLHTTPTransport",
			load: function(data) {
				instance.alert(data);
			}
		});

	}

	// add a hidden input to a form
	//
	// form_id - the id of the form
	// name - the name of the hidden field
	// value - the value of the hidden field
	this.addToForm = function(form_id, name, value) {

		var forms = document.getElementsByTagName('form');
		for (var i = 0; i < forms.length; i++) {
			if (forms[i].getAttribute('id') == form_id) {
				var form = forms[i];
			}
		}

		if (form) {
			var input = document.createElement('input');
			input.setAttribute('type', 'hidden');
			input.setAttribute('name', name);
			input.setAttribute('value', value);
			form.appendChild(input);
		}
	}

	// set all checkbox value checked in a form
	//
	// form_id - the id of the form
	this.selectAllChkbox = function(form_id) {

		var form = document.getElementById(form_id);
		if (form) {
			for (var i = 0; i < form.elements.length; i++) {
				if (form.elements[i].type == 'checkbox') {
					form.elements[i].checked = true;
				}
			}
		}
	}

	// set all checkbox value to unshecked in a form
	//
	// form_id - the id of the form
	this.unSelectAllChkbox = function(form_id) {

		var form = document.getElementById(form_id);
		if (form) {
			for (var i = 0; i < form.elements.length; i++) {
				if (form.elements[i].type == 'checkbox') {
					form.elements[i].checked = false;
				}
			}
		}
	}

	// reverse all the checkbox values in a form
	//
	// form_id - the id of the form
	this.reverseSelectAllChkbox = function(form_id) {

		var form = document.getElementById(form_id);
		if (form) {
			for (var i = 0; i < form.elements.length; i++) {
				if (form.elements[i].type == 'checkbox') {
					if (form.elements[i].checked == false) {
						form.elements[i].checked = true;
					} else {
						form.elements[i].checked = false;
					}
				}
			}
		}
	}

	// create a normal popup window
	//
	// url - the content of the popup window
	// width - the width of the window
	// height - the height of the window
	this.popup = function(url, width, height) {

		var str = "status=no, width=" + width + ", height=" + height + " scrollbars=no";
		var popup = document.open(url, "print", str);
	}

	// call the dojo.io.bind with the parameters
	//
	// url - the url, call to
	// transport - the type of the transport ("XMLHTTPTransport", "IframeTransport")
	this.callDOJOIOBind = function(arguments) {

		instance.showLoadingPanel();

		var return_data = null;
		var url = arguments.url;
		var transport = arguments.transport;

		var formnode = null;
		if (arguments["formNode"]) {
			formnode = arguments.formNode;
		}
		var callback = arguments.load;

		dojo.io.bind({
			url: url,
			formNode: formnode ? formnode : null,
			mimeType: "text/plain",
			encoding: "UTF-8",
			preventCache: true,
			sync: true,
			transport: transport,
			error: function(type, error) {
				instance.handleError(error);
			},
			load: function(type, data, event) {
				callback(data);
			}
		});
	}

	// get the loading div content and return it
	this.getLoadingDivHTML = function() {

		var load_div = document.getElementById(this.load_div_id);
		var html = null;

		if (load_div) {
			html = load_div.innerHTML;
		}

		return html;
	}

	// show the loading panel in a dialog
	this.showLoadingPanel = function() {

		if (! this.show_loading) {
			instance.alert(instance.getLoadingDivHTML(), true);
			this.show_loading = true;
		}
	}

	// hide the loading panel
	this.hideLoadingPanel = function() {

		if (this.show_loading) {
			instance.removeDialog();
			this.show_loading = false;
		}
	}

}


