/*
 * AJAX2.js library
 * Use YAHOO.connection.js
 * UTF-8 encoding
 */

// Write a "onLoadFunction" function in your page, 
// this function will be called when the body will be finished

// start JS when body is finished
function onLoadBody() {
	if (typeof startKupu ==	'function')
    	kupu = startKupu();
	if (typeof onLoadFunction == 'function')
		onLoadFunction();
}

if (!PIMENTECH.util.LoadHTML) {

	/**
	 * @class
	 * <p>PIMENTECH.util.LoadHTML is used for load an HTML block in a node of the document.
	 * You have to define an afterLoad() and afterPost() functions to define what to do
	 * after a load() and a post().</p>
	 */
	PIMENTECH.util.LoadHTML = function(name, container, url) {

		// container node in the document
		this.container = container;

		// default url to load
		this.url = url;

		// name of this object instance
		this.name = name;

		// default content gor the container
		this.content = "";

		this.afterLoad = null;
		this.afterPost = null;

		// can we try afterPost() function ?
		this.canAfterPost = false;

		// can we try afterLoad() function ?
		this.canAfterLoad = false;

		// Sometimes, it can be pretty to have no transitions (no 'loading...')
		this.silence = false;
	};

	/**
	 * Set silence param for a no transition loading.
	 */
	PIMENTECH.util.LoadHTML.prototype.setSilence = function () {
		this.silence = true;
	}

	/**
	 * Do the asyncRequest whith GET option, see load() and simpleLoad().
	 */
	PIMENTECH.util.LoadHTML.prototype.__get = function () {
		if (typeof this.container == 'string') {
			this.container = document.getElementById(this.container);
		}
		debug_msg('try to load ' + this.url);
		this.loadObj = YAHOO.util.Connect.asyncRequest('GET', this.url, this, null);
		if (!this.silence && YAHOO.util.Connect.isCallInProgress(this.loadObj)) {
			this.container.innerHTML = '<span class="loading" > </span>... <a style="font-size:80%" onclick="' +
				this.name + '.load()">reload</a>';
		}
	};

	/**
	 * load an url in the container and try to run afterLoad() function.
	 * @param {string} url  Absolute or relative url.
	 */
	PIMENTECH.util.LoadHTML.prototype.load = function (url) {
		if (this.beforeLoad != null) {
			this.beforeLoad();
		}
		this.canAfterLoad = true;
		if (arguments.length > 0) this.url = url;
		this.__get();
		return false;
	};

	/**
	 * load an url as load() function by does not try to run afterLoad() function.
	 * @param {string} url  Absolute or relative url.
	 */
	PIMENTECH.util.LoadHTML.prototype.simpleLoad = function(url) {
		this.canAfterLoad = false;
		if (arguments.length > 0) this.url = url;
		this.__get();
		return false;
	};

	/**
	 * post a form with GET method and reload the response. Try to run afterPost() function.
	 * @param {string} formName  The name of the form to post.
	 * @param {string} url  The OPTIONAL url = action of the form.
	 */
	PIMENTECH.util.LoadHTML.prototype.get = function(formName, url) {
		if (this.beforePost != null) {
			this.beforePost();
		}
		if (typeof this.container == 'string') {
			this.container = document.getElementById(this.container);
		}
		if (arguments.length > 1) this.url = url;

		YAHOO.util.Connect.setForm(formName, false);

		debug_msg('try to post ' + this.url);
		this.canAfterPost = true;
		this.loadObj = YAHOO.util.Connect.asyncRequest('GET', this.url, this, null);
		if (!this.silence && YAHOO.util.Connect.isCallInProgress(this.loadObj)) {
			this.container.innerHTML = '<span class="loading" > </span>... <a style="font-size:80%" onclick="' +
				this.name + '.post()">submit again</a>';
		}
		return false;
	};

	/**
	 * post a form with POST method and reload the response. Try to run afterPost() function.
	 * @param {string} formName  The name of the form to post.
	 * @param {string} url  The OPTIONAL url = action of the form.
	 */
	PIMENTECH.util.LoadHTML.prototype.post = function(formName, url, withfile) {
		if (this.beforePost != null) {
			this.beforePost();
		}
		if (typeof this.container == 'string') {
			this.container = document.getElementById(this.container);
		}
		if (arguments.length > 1) this.url = url;
		if (arguments.length != 3) { withfile = false }

		YAHOO.util.Connect.setForm(formName, withfile);

		debug_msg('try to post ' + this.url);
		this.canAfterPost = true;
		this.loadObj = YAHOO.util.Connect.asyncRequest('POST', this.url, this, null);
		if (!this.silence && !withfile && YAHOO.util.Connect.isCallInProgress(this.loadObj)) {
			this.container.innerHTML = '<span class="loading" > </span>... <a style="font-size:80%" onclick="' +
				this.name + '.post()">submit again</a>';
		}
		return false;
	};

	/**
	 * reset the content of container
	 */
	PIMENTECH.util.LoadHTML.prototype.reset = function() {
		if (typeof this.container == 'string') {
			this.container = document.getElementById(this.container);
		}
		this.container.innerHTML = "";
	};


	/**
	 * What to do in if load or post is a success
	 * This method tries to run afterLoad() or afterPost() if defined.
	 * @param {Object} response  The success response object (see YAHOO lib)
	 */
	PIMENTECH.util.LoadHTML.prototype.success = function(response) {
		try {
		  this.content = response.responseText;
		  this.container.innerHTML = this.content;
		} catch (e) {
			debug_msg(e);
		}
		/* <script> interpretation */
		evalScriptContent(this.container);
		if (this.canAfterPost && this.afterPost != null) {
			this.afterPost();
			this.canAfterPost = false;
		} else if (this.canAfterLoad && this.afterLoad != null) {
			this.afterLoad();
			this.canAfterLoad = false;
		} 
	};

	/**
	 * what to do in if load or post id a failure.
	 * build a link for trying again
	 * @param {Object} response  The failure response object (see YAHOO lib)
	 */
	PIMENTECH.util.LoadHTML.prototype.failure = function(response) {
		content = "<div>Can't load this service : " + response.statusText;
		content += '... <a onclick="' + this.name + '.load()">reload</a></div>'
		this.container.innerHTML =  content;
	};
};

function jsload(nodeid, url, silence, afterLoad) {
	var obj = new PIMENTECH.util.LoadHTML('obj', nodeid, url);
	if (afterLoad) {
		obj.afterLoad = afterLoad;
	}
	if (silence) {
		obj.setSilence();
	}
	obj.load();
};
function jssubmit(form, nodeid, url, afterPost) {
	var obj = new PIMENTECH.util.LoadHTML('obj', nodeid, url);
	if (afterPost) {
		obj.afterPost = afterPost;
	}
	obj.post(form);
};

function jsupload(form, nodeid, url, afterPost) {
	var obj = new PIMENTECH.util.LoadHTML('obj', nodeid, url);
	obj.setSilence();
	if (afterPost) {
		obj.afterPost = afterPost;
	}
	obj.post(form, url, true);
};

// loadXML failure handler
function xml_failure(response) {
	return false;
};
/**
 * Return an connection object.
 * @param {function} response  Handler is of 'function(response)' 
 *                   structure with a response.responseXML property
 * @param {string} url  Is an XML page (with xml HTTP header)
 */
function loadXML(url, handler) {
	var obj = YAHOO.util.Connect.asyncRequest('GET', url, 
		{success : handler, failure : xml_failure}, 
											  null);
	return obj;
};

if (!PIMENTECH.util.StackLoad) {

	PIMENTECH.util.StackLoad = function() {
		this.stack = new Array();
	}

	PIMENTECH.util.StackLoad.prototype.clear = function (object) {
		this.stack = new Array();
	}

	PIMENTECH.util.StackLoad.prototype.push = function (object) {
		this.stack.push(object);
	}

	PIMENTECH.util.StackLoad.prototype.append = function (object) {
		this.stack.unshift(object);
	}

	PIMENTECH.util.StackLoad.prototype.load = function () {
		if (this.stack.length != 0) {
			var object = this.stack.pop();
			var self = this;
			var afterLoadFunction = (object.afterLoad != null && object.afterLoad() || function() {});
			object.afterLoad = function () {
				afterLoadFunction();
				self.load();
			};
			object.load();
		}
	}
}


