/**
 * AjaxController
 */

var MJL_Ajax_Handler_Response = Class.create({
	options:{},
	// handle all the request types
	initialize:function(transport, options) {
		this.transport 	= transport;
		this.options	= options;
	},
	
	response:function() {
	
		var strXml 			= '<?xml version="1.0"?>\n<templates>\n';
		strXml				+= this.transport.responseText + '\n';
		strXml				+= '</templates>';
		
		var xml 			= this.stringToXMLParser(strXml);
		var xmlDocRoot		= xml.documentElement;
		var nodes			= xmlDocRoot.getElementsByTagName('ajax');
		var len				= nodes.length;
		
		var disabled		= false;
		for(var i=0;i<len;i++) {
			
			var node 		= nodes[i];
			var content		= node.childNodes[0] ? node.childNodes[0].nodeValue : '';
			var insert		= node.getAttribute('insertion') && !this.options.insertion ? node.getAttribute('insertion') : typeof(this.options.insertion) == 'object' ? this.options.insertion[i] : this.options.insertion;
			var id			= node.getAttribute('id');
			if(!id) {
				id			= !this.options.responseId || node.getAttribute('id') == 'debugWindow' ? node.getAttribute('id') : typeof(this.options.responseId) == 'object' ? this.options.responseId[i] : this.options.responseId;
			}
			
			if(id && !id.empty()) {
				
				switch(insert){
					case 'value':
						$(id).value	= content;
						break;
					case 'src':
						$(id).src	= content;
						break;
					case 'none':
						break;
					default:
						try {
						$(id).update(content);
						} catch(e) {}
						break;
				}
				
			}
			
			
		}
		
		nodes = null;
		
		return true;
		
	},
	
	stringToXMLParser:function(response) {

		if(application.IE) {
			var myDocument;
	   		//Internet Explorer, create a new XML document using ActiveX
	  	 	//and use loadXML as a DOM parser.
	   		myDocument = new ActiveXObject("Microsoft.XMLDOM")
	   		myDocument.async= "false";
			
	   		var parsed_xml = myDocument.loadXML(response);
	
	   		return myDocument;
		} else {
			var xml = new DOMParser().parseFromString(response, 'text/xml');
			return xml;
		}
	}
});

