/**
 * Jigsaw - Javascript framework for AJAX.
 * Copyright (C) 2006 Sandaruwan Gunathilake

 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * @package    Jigsaw - http://www.sandaru1.com/jigsaw
 * @author     Sandaruwan Gunathilake http://www.sandaru1.com
 */

var globalHandlers = {
	onComplete: function(response) {
		var elements = response.transport.responseXML.getElementsByTagName('element');
		for (var i=0;i<elements.length;i++) {
			var attributes=elements[i].attributes;
			var attribute="";
			var id="";
			for (var k=0;k<attributes.length;k++) {
				var name=attributes[k].nodeName;
				switch (true) {
					case (name=="attribute") : 
						attribute=attributes[k].nodeValue;
						break;
					case (name=="id") :
						id=attributes[k].nodeValue;
						break;
				}
			}
			if (attribute=="") {
				try {
					$(id).innerHTML=elements[i].firstChild.data;
				} catch(e) {};
			} else {
				try {
					$(id).setAttribute(attribute,elements[i].firstChild.data);
				} catch(e) {};
			}
		}
		var scripts = response.transport.responseXML.getElementsByTagName('script');
		for (var i=0;i<scripts.length;i++) {
			eval(scripts[i].firstChild.data);
		}
		var objects = response.transport.responseXML.getElementsByTagName('object');
		var objs = new Array();
		for (var i=0;i<objects.length;i++) {
			var temp = new Object();
			var attributes = objects[i].attributes;
			for (var k=0;k<attributes.length;k++) {
				temp[attributes[k].nodeName]=attributes[k].nodeValue;
			}
			var attributes = objects[i].getElementsByTagName('attribute');
			for (var k=0;k<attributes.length;k++) {
				temp[attributes[k].attributes[0].nodeValue]=attributes[k].firstChild.data;
			}
			objs.push(temp);
		}
		response.objects=objs;
		try {
			response.options['onFinish'](response);
		}
		catch (e) {}
	}
};

Ajax.Responders.register(globalHandlers);