function Ajax(url, method, debug){
	this.url 	= url?url:"";
	this.method	= method?method:"POST";
	this.debug	= debug?debug:false;
};

Ajax.prototype = {
	object_request	:null,
	url 		 	:null,
	method 			:null,
	debug 			:false,
	response_long 	:false,
	debug_window 	:null,
	work_id			:'AjaxWork'+ new Date().getTime(),
	depth			:0,
	error_alert 	:["400","401","402","403","404","500","501","502","503"],
	obj_proc 		:null,
	status_message	:true,
	wait_cursor		:true,
	_properties		: new Array(),
	read			:false,
	resetTimer		:true
};


Ajax.prototype.containsValue = function(valueToCheck){
	for(var i=0;i<this.error_alert.length;i++){
		if(this.error_alert[i] == valueToCheck) return true;
	}
	return false;
};



Ajax.prototype.message = function(text){
	if(!this.debug) return;
	if ((text.length > 1000) && (!this.response_long)){
		text = text.substr(0,1000)+"...\n[Resposta Longa]\n...";
	}
	
	try{
		if(this.debug_window == null || this.debug_window.closed){
			this.debug_window = window.open(
									"about:blank",
									"",
									"width=640,height=480,resizable=no,toolbar=no,status=0,menubar=no,scrollbars=yes");
			this.debug_window.document.write('<html><head><title>Debug Message</title></head><body><h2>AJAX</h2><div id="div_debug" style="padding:5px; font-family:Verdana; font-size:12px; border-color:#CCCCCC; border-width:2px; border-style:solid;"></div></body></html>');
		}
			
		text = text.replace(/&/g, "&amp;")
		text = text.replace(/</g, "&lt;")
		text = text.replace(/>/g, "&gt;")
		debug_div = this.debug_window.document.getElementById('div_debug');
		
		var data = new Date();
		_data = data.getFullYear()+'-'+data.getMonth()+'-'+data.getDate()+' '+data.getHours()+':'+data.getMinutes()+':'+
		data.getSeconds()+':'+data.getMilliseconds();
		
		debug_div.innerHTML = debug_div.innerHTML + ('<b>'+_data+'</b>: ' + text + '<hr/>');
			
	}
	catch(e){
		alert("Debug Message:\n " + text+"\n"+e.message);
	}	
};

Ajax.prototype.getUri = function(){
	if(arguments.length > 0) var ext = arguments[0];
	var url = document.URL;
	if(!url) return false;
	var aUri = url.split('/');
	url = aUri[aUri.length-1];
	if(!ext) return url;
	else{
		aUri = url.split('.');
		if(aUri.length > 0) url	= aUri[0];
		if(ext.indexOf('.')== -1) return (url+'.'+ext);
		else return (url+ext);
	}
};

Ajax.prototype.getDocumentName = function(doc){
	if(!doc) doc = document;
	var url = doc.URL;
	if(!url) return false;
	var aUri = url.split('/');
	url = aUri[aUri.length-1];
	aUri = url.split('.');
	return aUri[0];
};

Ajax.prototype.strip = function(value, char){
	if(!char) char = '.';	
	var index = value.indexOf(char);
	while(index > -1){
		value = value.substring(index + 1, value.length);
		index = value.indexOf(char);
	}
	return value;
};

Ajax.prototype.split_menu = function(value, char){
	if(!char) char = '.';
	var tmp_value = value.split(char);
	var tmp_length = tmp_value.length;
	
	if(tmp_length <= 2){ return value; }
	else{ 
		try{
			value = tmp_value[tmp_length-2];
			value += '.';
			value += tmp_value[tmp_length-1];
			return value;
		}
		catch(e){				
				this.message("Erro: "+e.name+": "+e.message);
		}
		return value;
	}
};

Ajax.prototype.isIE = function(){
	var agente	= navigator.userAgent.toLowerCase();
	return(agente.indexOf("msie") != -1);
};

Ajax.prototype.get_properties = function(_element){
	var o_element;
	var _str="";
	if(typeof(_element) == "string") o_element = this.obj(_element);
	else o_element = _element;
	for(key in o_element){
		_str += key +": "+ o_element[key]+"\n";
	}
	return _str;
};

Ajax.prototype.get_elemenet_properties = function(__doc){
	var __elements;
	if(__doc.all){__elements = __doc.all;}
	else{__elements = __doc.getElementsByTagName("*");}
	var _styles		= this._properties['style'];
	var _xml		= "<ajaxquery><q>";
	var _first		= true;
	for(var i=0; i < __elements.length; i++){
		_name = __elements[i].name;
		if(!_name) _name = __elements[i].id;
		if(!_name) continue;
		_type = __elements[i].type;
		if(!_type) _type = __elements[i].tagName;
		if(!this._properties[_type]) continue;
		var __properties = this._properties[_type];		
		if(!_first) _xml += '&';
		else _first = false;			
		var _str = '';
		for(var j=0; j < __properties.length; j++){
			if((_type == 'LABEL') && (__properties[j] == 'innerHTML') && __elements[i].dataSrc){ continue; }
			prop = eval("__elements[i]."+__properties[j]);
			if(!prop) continue;
			_str += __properties[j] + "="+prop+";";
		}			
		for(var j=0; j < _styles.length; j++){
			prop = eval("__elements[i].style."+_styles[j]);
			if(!prop) continue;
			_str += 'style.'+_styles[j] + "="+prop+";";
		}			
		_xml += _name +"="+escape(_str);
	}
	_xml += "</q></ajaxquery>";
	return _xml;
}; // get_element_properties

Ajax.prototype.getRequest = function(){	
	this.message("Inicializando o objeto de requisição");		
	var req = null;
	if(typeof XMLHttpRequest != "undefined")
		req = new XMLHttpRequest();
	if(!req && typeof ActiveXObject != "undefined"){
		try{
			req	= new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				req	= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				try{
					req	= new ActiveXObject("Msxml2.XMLHTTP.4.0");
				}
				catch (e){
					req=null;
				}
			}
		}
	}
	if(!req && window.createRequest) req = window.createRequest();		
	if (!req) this.message("Falha na inicialização do objeto de requisição.");			
	return req;
};


Ajax.prototype.obj = function(id){
	if(!id) return null;		
	var element = document.getElementById(id);
	if(!element && document.all) element = document.all[id];
	if(!element && document.getElementsByName){	// mozila, if element only name
		element = document.getElementsByName(id);
		if(element.length > 0) return element[0];
	}
	if(!element && (id != this.work_id)) this.message("O Elemento com o id \"" + id + "\" não foi encontrado.");
	return element;
};

Ajax.prototype.include = function(file_name){
		var objHead		= document.getElementsByTagName('head');
		var objScript	= document.createElement('script');
		objScript.type	= 'text/javascript';
		objScript.src	= file_name;
		objHead[0].appendChild(objScript);
};	
	
Ajax.prototype.stripOnPrefix = function(EventName){
	EventName = EventName.toLowerCase();
	if(EventName.indexOf('on') == 0){
		EventName = EventName.replace(/on/,'');
	}		
	return EventName;
};
	
Ajax.prototype.addOnPrefix = function(EventName){
	EventName = EventName.toLowerCase();
	if(EventName.indexOf('on') != 0){
		EventName = 'on' + EventName;
	}
	return EventName;
};
	
Ajax.prototype.addHandler = function(element, Event, FunctionName){
	
	if(typeof(element) != 'object'){ element = this.obj(element); }
	
	if (window.addEventListener){
		Event = this.stripOnPrefix(Event);
		eval("element.addEventListener('"+Event+"',"+FunctionName+",false);");
	}else{
		AltEvent = this.addOnPrefix(Event);
		eval("element.attachEvent('"+AltEvent+"',"+FunctionName+",false);");
	}
};
	
Ajax.prototype.removeHandler = function(ElementId, Event, FunctionName){
	if(window.addEventListener){
		Event = this.stripOnPrefix(Event);
		eval("this.obj('"+ElementId+"').removeEventListener('"+Event+"',"+FunctionName+",false);");
	}else{
		AltEvent = this.addOnPrefix(Event);
		eval("this.obj('"+ElementId+"').detachEvent('"+AltEvent+"',"+FunctionName+",false);");
	}
};

Ajax.prototype.create = function(ParentId, Tag, Id){
	var objParent = this.obj(ParentId);
	objElement = document.createElement(Tag);
	objElement.setAttribute('id',Id);
	if (objParent) objParent.appendChild(objElement);
};
	
Ajax.prototype.insert = function(BeforeId, Tag, Id){
	var objSibling = this.obj(BeforeId);
	objElement = document.createElement(Tag);
	objElement.setAttribute('id',Id);
	objSibling.parentNode.insertBefore(objElement, objSibling);
};

Ajax.prototype.insertAfter = function(AfterId, Tag, Id){
	var objSibling = this.obj(AfterId);
	objElement = document.createElement(Tag);
	objElement.setAttribute('id',Id);
	objSibling.parentNode.insertBefore(objElement, objSibling.nextSibling);
};
	
Ajax.prototype.getInput = function(Type, Name, Id){
	var Obj;
	if(!window.addEventListener){
		Obj = document.createElement('<input type="'+Type+'" id="'+Id+'" name="'+Name+'">');
	}else{
		Obj = document.createElement('input');
		Obj.setAttribute('type',Type);
		Obj.setAttribute('name',Name);
		Obj.setAttribute('id',Id);
	}
	return Obj;
};
	
Ajax.prototype.createInput = function(ParentId, Type, Name, Id){
	var objParent = this.obj(ParentId);
	var objElement = this.getInput(Type, Name, Id);
	if(objParent && objElement)
		objParent.appendChild(objElement);
};
	
Ajax.prototype.insertInput = function(BeforeId, Type, Name, Id){
	var objSibling = this.obj(BeforeId);
	var objElement = this.getInput(Type, Name, Id);
	if (objElement && objSibling && objSibling.parentNode)
		objSibling.parentNode.insertBefore(objElement, objSibling);
};

Ajax.prototype.insertInputAfter = function(AfterId, Type, Name, Id){
	var objSibling = this.obj(AfterId);
	var objElement = this.getInput(Type, Name, Id);
	if(objElement && objSibling && objSibling.parentNode){
		objSibling.parentNode.insertBefore(objElement, objSibling.nextSibling);
	}
};
		
Ajax.prototype.remove = function(Id){
	objElement = this.obj(Id);
	if(objElement && objElement.parentNode && objElement.parentNode.removeChild){
		objElement.parentNode.removeChild(objElement);
	}
};	

Ajax.prototype.replace = function(Id, Attribute, Search, Replace){
	var bFunction = false;	
	if(Attribute == "innerHTML") Search = this.getBrowserHTML(Search);
	
	eval("var txt=this.obj('"+Id+"')."+Attribute);
	if(typeof txt == "function"){
		txt = txt.toString();
		bFunction = true;
	}
	if(txt.indexOf(Search)>-1){
		var newTxt = '';
		while (txt.indexOf(Search) > -1){
			x = txt.indexOf(Search)+Search.length+1;
			newTxt += txt.substr(0,x).replace(Search,Replace);
			txt = txt.substr(x, txt.length-x);
		}
		newTxt += txt;
		if (bFunction){
			eval('this.obj("'+Id+'").'+Attribute+'=newTxt;');
		}
		else if (this.willChange(Id, Attribute, newTxt)){
			eval('this.obj("'+Id+'").'+Attribute+'=newTxt;');
		}
	}
};

Ajax.prototype.getBrowserHTML = function(html){
	tmpAjax = this.obj(this.workId);
	if (!tmpAjax){
		tmpAjax = document.createElement("div");
		tmpAjax.setAttribute('id',this.workId);
		tmpAjax.style.display = "none";
		tmpAjax.style.visibility = "hidden";
		document.body.appendChild(tmpAjax);
	}
	tmpAjax.innerHTML = html;
	var browserHTML = tmpAjax.innerHTML;
	tmpAjax.innerHTML = '';	
	return browserHTML;
};
	
Ajax.prototype.willChange = function(element, attribute, newData){
	if(!document.body) return true;
	if(attribute == "innerHTML"){
		newData = this.getBrowserHTML(newData);
	}
	elementObject = this.obj(element);
	if(elementObject){
		var oldData;		
		eval("oldData=this.obj('"+element+"')."+attribute);
		if(newData !== oldData) return true;
	}
	return false;
};
	
//Returns the source code of the page after it's been modified by ajax
Ajax.prototype.viewSource = function(){
	return "<html>"+document.getElementsByTagName("HTML")[0].innerHTML+"</html>";
};

Ajax.prototype.getFormValues = function(frm){
	if(!frm) frm = this.getDocumentName();
	var objForm;
	if (typeof(frm) == "string") objForm = this.obj(frm);
	else objForm = frm;
	var submitDisabledElements = false;
	var sXml = "<ajaxquery><q>";
	if(objForm && objForm.tagName == 'FORM'){
		var formElements = objForm.elements;
		for( var i=0; i < formElements.length; i++){
			if(!formElements[i].name) continue;
			if(formElements[i].type && (formElements[i].type == 'radio' || formElements[i].type == 'checkbox') && formElements[i].checked == false)
				continue;
			if(formElements[i].disabled && formElements[i].disabled == true && submitDisabledElements == false)
				continue;
			var name = formElements[i].name;
			if (sXml != '<ajaxquery><q>') sXml += '&';
			if(formElements[i].type=='select-multiple'){
				for (var j = 0; j < formElements[i].length; j++){
					if (formElements[i].options[j].selected == true)
						sXml += name+"="+escape(formElements[i].options[j].value)+"&";
				}
			}else{
				sXml += name+"="+escape(formElements[i].value);
			}
		}
	}	
	sXml +="</q></ajaxquery>";	
	return sXml;
};
Ajax.prototype.objectToXML = function(_obj){
		//var sXml = "<ajaxobj>";
		var sXml = "";
		var i = 0;
		for (key in _obj){
			try{
				if(key == 'constructor') continue;
				if(_obj[key] && typeof(_obj[key]) == 'function') continue;
					
				var value = _obj[key];
				
				/*
				if (value && typeof(value)=="object" && this.depth <= 50){
				//if(value && typeof(value)=="object" && key == "currentStyle"){
					this.depth++;
					value = this.objectToXML(value);
					this.depth--;
				}
				
				*/
				
				//sXml += "<e><k>"+key+"</k><v>"+value+"</v></e>";
				sXml += key+"="+value+"<br>";
				i++;
				
			}
			catch(e){				
				this.message("Erro convertendo objeto para xml: "+e.name+": "+e.message);
			}
		}
		 
		//sXml += "</ajaxobj>";
		sXml += "";
	
		return sXml;
};

// unserializes data structure from ajaxResponse::_buildObjXml()
Ajax.prototype._nodeToObject = function(node){
	// parentNode here is weird, have to tune
	if (node.nodeName == '#cdata-section') {
		var data = "";
		for (var j=0; j<node.parentNode.childNodes.length; j++) {
			data += node.parentNode.childNodes[j].data;
		}
		return data;
	}
	else if (node.nodeName == 'ajaxobj') {
		var data = new Array();
		for (var j=0; j<node.childNodes.length; j++) {
			var child = node.childNodes[j];
			var key;
			var value;
			if(child.nodeName == 'e'){
				for (var k=0; k<child.childNodes.length; k++){
					if (child.childNodes[k].nodeName == 'k') {
						key = child.childNodes[k].firstChild.data;
					}
					else if(child.childNodes[k].nodeName == 'v'){
						value = this._nodeToObject(child.childNodes[k].firstChild);
					}
				}
				if(key != null && value != null){
					data[key] = value;
					key = value = null;
				}
			}
		}
		return data;
	}		
}; // end _nodeToObject

Ajax.prototype.loadingFunction = function(){
		
	if(this.obj_proc != null){
		this.obj_proc.style.visibility = 'visible';
		this.obj_proc.style.left = 1;
	}else{
		this.obj_proc = document.createElement('div');
		this.obj_proc.innerHTML = 'Processando...';
		this.obj_proc.style.position = 'absolute';
		this.obj_proc.style.backgroundColor = '#CCCCCC';
		this.obj_proc.style.borderStyle = 'solid';
		this.obj_proc.style.fontFamily = 'Verdana'; 
		this.obj_proc.style.fontSize='10px';
		this.obj_proc.style.color ='#666666';
		this.obj_proc.style.borderColor = '#666666';
		this.obj_proc.style.borderWidth = '1px';
		this.obj_proc.style.width = '100px';
		this.obj_proc.style.padding = '2px';
		this.obj_proc.style.left = 1;//document.body.clientWidth - 87;
		this.obj_proc.style.top = 1; 
		this.obj_proc.onselectstart = function(){ return false; };
		document.body.appendChild(this.obj_proc);
	}
};
Ajax.prototype.doneLoadingFunction = function(){
	if(this.obj_proc != null){
			this.obj_proc.style.visibility = 'hidden';
	}
};

Ajax.prototype.loadingTimeout = null;

Ajax.prototype.getUrl = function(){
	if(_dir_language) this.url =  _dir_language;
	if(_language) this.url += '/' + this.getUri(_language);
	else this.url += this.getUri('.php');
	return this.url;
};

Ajax.prototype.call = function(_Function, Args, RequestType){
	var i,r,postData;
	if(document.body && this.wait_cursor) document.body.style.cursor = 'wait';
	if(this.status_message == true) window.status = 'Enviando Requisição...';
	clearTimeout(this.loadingTimeout);
	this.message('Enviando Requisição...');
	
	this.loadingTimeout = setTimeout("ajax.loadingFunction();",200);
	if(RequestType != null) this.method = RequestType;
	if(!this.url){ this.getUrl(); }
	var value;
	switch(this.method){
		case "GET":{ // get
			var uriGet = this.url.indexOf("?")==-1?"?ajax="+escape(_Function):"&ajax="+escape(_Function);
			if(Args){
				for(i = 0; i<Args.length; i++){
					value = Args[i];
					if (typeof(value)=="object") value = this.objectToXML(value);
					uriGet += "&ajaxargs[]="+escape(value);
				}
			}
			uriGet += "&ajaxr=" + new Date().getTime();
			this.url += uriGet;
			postData = null;
			} break;
		case "POST":{ // post
			postData = "ajax="+escape(_Function);
			postData += "&ajaxr="+new Date().getTime();
			if(Args){
				for(i = 0; i < Args.length; i++){

					value = Args[i];
					if (typeof(value)=="object")
						value = this.objectToXML(value);
					postData = postData+"&ajaxargs[]="+escape(value);
				}
			}
			} break;
		default: alert("Tipo de requisição ilegal: " + this.method); return false; break;
	}

	var r = this.getRequest();
	if(!r) return false;
	r.open(this.method, this.url, true);
	if(this.method=='POST'){
		try{
			r.setRequestHeader("Method", "POST " + this.url + " HTTP/1.1");
			r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		catch(e){
			alert("O browser não suporta requisição assíncronos usando POST.");
			return false;
		}
	}
	
	var oAjx = this;
	
	r.onreadystatechange = function(){
		if(r.readyState != 4) return;			
		if(r.status == 200){
			if(oAjx.debug) oAjx.message("Recebido:\n" + r.responseText);
			if(r.responseXML && r.responseXML.documentElement)
				oAjx.processResponse(r.responseXML);
			else{
				oAjx.doneLoadingFunction();
				var errorString = "Erro: a resposta de XML que foi devolvida do servidor é inválida.";
				errorString += "\nRecebido:\n" + r.responseText;
				trimmedResponseText = r.responseText.replace( /^\s+/g, "" );// strip leading space
				trimmedResponseText = trimmedResponseText.replace( /\s+$/g, "" );// strip trailing
				if (trimmedResponseText != r.responseText)
					errorString += "\nYou have whitespace in your response.";
				alert(errorString);
				document.body.style.cursor = 'default';
				if(oAjx.status_message) window.status = 'Invalid XML response error';				
			}
		}
		else{
			oAjx.doneLoadingFunction();
			if(oAjx.containsValue(r.status)) {
				var errorString = "Error: the server returned the following HTTP status: " + r.status;
				errorString += "\nReceived:\n" + r.responseText;
				alert(errorString);
			}
			document.body.style.cursor = 'default';
			if(oAjx.status_message) window.status = 'Invalid XML response error';								
		}
		
		delete r;
		r = null;
	}
	this.message("Chamando "+_Function +" uri="+this.url+" (post:"+ postData +")");
	r.send(postData);
	if(this.status_message) window.status = 'Esperando por dados...';
	delete r;
	delete i;
	delete postData;
	return true;
};

/* Processa Resposta do servidor

 * formato da resposta
<?xml version="1.0" encoding="utf-8"?>
<responsedata content="html;dataset;ajaxcmd;object;images;functions">
	<html destination="element.name"><![CDATA[content HTML]]></html>
	<dataset>
		<fields>
			<field name="col_id" max_length="size_column" type="data_type" />
			<field name="col_description" max_length="size_column" type="data_type" />
		</fields>
		<data>
		  <row col_id="data_column" col_description="data_column"/>
		</data>
	</dataset>
	<ajaxcmd>
		<cmd n="as" t="sel1" p="style.display"><![CDATA[inline]]></cmd>
	</ajaxcmd>
	<objects>
		<object name="form1.input1"><![CDATA[style.visibility=hidden;class="edt";]]></object>
	</objects>
	<images>
		<image destination="form1.img1"><![CDATA[<object>]]></image>
	</images>
	<functions>
		<function name="fx_name"><![CDATA[function(){ alert('teste...'); }]]></function>
	</functions>
</responsedata>
*/
Ajax.prototype.processResponse = function(xml){

	clearTimeout(this.loadingTimeout);
	this.doneLoadingFunction();


	if(this.status_message) window.status = "Processando Resposta...";
	var tmpxml = null;
	xml = xml.documentElement;
	if(xml == null) return;
	
	//verifica quais as tags a processar...
	if(xml.nodeName != "responsedata") alert('a resposta do servidor é inválida...');
	else{

		var content		= xml.attributes[0].value;
		var ar_content	= content.split(";");
		var tmp_xml;
		
		for(var i=0; i < ar_content.length; i++){
			content = ar_content[i];
			tmp_xml = xml.getElementsByTagName(content)[0];
			try{
				eval('this.process_'+content+'(tmp_xml);');
				//alert('this.process_'+content+'(tmp_xml);');
			}
			catch(e){
				alert("Erro ao executar o procedimento: process_"+content+"(xml);\nMensagem: "+e.message);
				break;
			}
		}
		delete tmp_xml;
		delete ar_content;
		delete content;
	}
	
	delete xml;
	delete i;
	document.body.style.cursor = 'default';
	if(this.status_message) window.status = 'Concluido';
	
	if(this.resetTimer){ //top.timer.reset(); 
		//top.timer_restart();
	}
};

Ajax.prototype.process_ajaxcmd = function(xml){
	
		var tmpAjax 		= null;
		var skipCommands 	= 0;
		for (var i=0; i<xml.childNodes.length; i++){
			
			if(skipCommands > 0){
				skipCommands--;
				continue;
			}
			
			if(xml.childNodes[i].nodeName == "cmd"){
				var cmd;
				var id;
				var property;
				var data;
				var search;
				var type;
				var before;
				var objElement = null;

				for (var j=0; j<xml.childNodes[i].attributes.length; j++){
					if (xml.childNodes[i].attributes[j].name == "n"){
						cmd = xml.childNodes[i].attributes[j].value;
					}
					else if (xml.childNodes[i].attributes[j].name == "t"){
						id = xml.childNodes[i].attributes[j].value;
					}
					else if (xml.childNodes[i].attributes[j].name == "p"){
						property = xml.childNodes[i].attributes[j].value;
					}
					else if (xml.childNodes[i].attributes[j].name == "c"){
						type = xml.childNodes[i].attributes[j].value;
					}
				}
				if(xml.childNodes[i].childNodes.length > 1 && xml.childNodes[i].firstChild.nodeName == "#cdata-section"){
					data = "";
					for (var j=0; j<xml.childNodes[i].childNodes.length; j++){
						data += xml.childNodes[i].childNodes[j].data;
					}
				}
				else if(xml.childNodes[i].firstChild && xml.childNodes[i].firstChild.nodeName == 'ajaxobj'){
					data = this._nodeToObject(xml.childNodes[i].firstChild);
					objElement = "XJX_SKIP";
				}
				else if(xml.childNodes[i].childNodes.length > 1){
					for (var j=0; j<xml.childNodes[i].childNodes.length; j++){
						if (xml.childNodes[i].childNodes[j].childNodes.length > 1 && xml.childNodes[i].childNodes[j].firstChild.nodeName == "#cdata-section"){
							var internalData = "";
							for (var k=0; k<xml.childNodes[i].childNodes[j].childNodes.length;k++){
								internalData+=xml.childNodes[i].childNodes[j].childNodes[k].nodeValue;
							}
						}else {
							var internalData = xml.childNodes[i].childNodes[j].firstChild.nodeValue;
						}
					
						if (xml.childNodes[i].childNodes[j].nodeName == "s"){
							search = internalData;
						}
						if (xml.childNodes[i].childNodes[j].nodeName == "r"){
							data = internalData;
						}
					}
				}
				
				else if (xml.childNodes[i].firstChild) data = xml.childNodes[i].firstChild.nodeValue;
				else data = "";
				
				if (objElement != "XJX_SKIP") objElement = this.obj(id);
				var cmdFullname;
				
				try{
					// 
					data = unescape(data);
					
					if (cmd=="cc"){
						cmdFullname = "addConfirmCommands";
						var confirmResult = confirm(data);
						if(!confirmResult) skipCommands = id;
					}
					if (cmd=="al"){
						cmdFullname = "addAlert";
			            data=data.replace(/\+/g," ");
			           // data=unescape(data);
						alert(data);						
					}
					else if(cmd=="js"){
						cmdFullname = "addScript/addRedirect";
						eval(data);
					}
					else if(cmd=="jc"){
						cmdFullname = "addScriptCall";
						var scr = id + '(';
						if (data[0] != null) {
							scr += 'data[0]';
							for (var l=1; l<data.length; l++) {
								scr += ',data['+l+']';
							}
						}
						scr += ');';
						eval(scr);
					}
					else if(cmd=="in"){
						cmdFullname = "addIncludeScript";
						this.include(data);
					}
					else if(cmd=="as"){
						cmdFullname = "addAssign/addClear";
						if(this.willChange(id,property,data)){
							eval("objElement."+property+"=data;");
						}
					}
					else if(cmd=="ap"){
						cmdFullname = "addAppend";
						eval("objElement."+property+"+=data;");
					}
					else if(cmd=="pp"){
						cmdFullname = "addPrepend";
						eval("objElement."+property+"=data+objElement."+property);
					}
					else if(cmd=="rp"){
						cmdFullname = "addReplace";
						this.replace(id,property,search,data)
					}
					else if(cmd=="rm"){
						cmdFullname = "addRemove";
						this.remove(id);
					}
					else if(cmd=="ce"){
						cmdFullname = "addCreate";
						this.create(id,data,property);
					}
					else if(cmd=="ie"){
						cmdFullname = "addInsert";
						this.insert(id,data,property);
					}
					else if(cmd=="ia"){
						cmdFullname = "addInsertAfter";
						this.insertAfter(id,data,property);
					}
					else if(cmd=="ci"){
						cmdFullname = "addCreateInput";
						this.createInput(id,type,data,property);
					}
					else if(cmd=="ii"){
						cmdFullname = "addInsertInput";
						this.insertInput(id,type,data,property);
					}
					else if(cmd=="iia"){
						cmdFullname = "addInsertInputAfter";
						this.insertInputAfter(id,type,data,property);
					}
					else if(cmd=="ev"){
						cmdFullname = "addEvent";
						property = this.addOnPrefix(property);
						eval("this.obj('"+id+"')."+property+"= function(){"+data+";}");
					}
					else if(cmd=="ah"){
						cmdFullname = "addHandler";
						this.addHandler(id, property, data);
					}
					else if(cmd=="rh"){
						cmdFullname = "addRemoveHandler";
						this.removeHandler(id, property, data);
					}
				}
				catch(e){
					this.message("While trying to '"+cmdFullname+"' (command number "+i+"), the following error occured:\n"
							+ e.name+": "+e.message+"\n"
							+ (id&&!objElement?"Object with id='"+id+"' wasn't found.\n":""));
				}
				delete objElement;
				delete cmd;
				delete cmdFullname;
				delete id;
				delete property;
				delete search;
				delete data;
				delete type;
				delete before;
				delete internalData;
				delete j;
				delete k;
			}	
		}
		delete xml;
		delete i;

}; // ajaxcmd

Ajax.prototype.process_objects = function(xml){
	var node;
	var obj_prop;
	for(var i=0; i<xml.childNodes.length; i++){
		node = xml.childNodes[i];
		var _obj_name = this.strip(node.attributes[0].value);
		var _obj = this.obj(_obj_name);
		if((!_obj) || (!_obj.tagName)){ // Create object and set properties
			continue;
		}else{
			// set properties
			try{
				obj_prop = unescape(node.firstChild.nodeValue);				
				arr_prop = obj_prop.split(";");
				for(j=0; j < arr_prop.length; j++){
					if(arr_prop[j]){
						_values = arr_prop[j].split("=");
						if(_values.length < 2){ continue; }
						prop = _values[0];
						data = _values[1];
						if((prop != "tagName") && (prop != "form.name") && (prop != "id")){
							eval("_obj."+prop+"=data;");
						}
					}
				}
			}catch(e){
				alert("Erro ao processar objeto: "+_obj_name+"\n\n"+e.message);
			}
		}
	}
	delete node;
	delete _obj;
	delete obj_prop;
	this.read = true;
};
	
Ajax.prototype.process_select = function(xml){	

	var node;
	var sel_name = xml.attributes[0].value;
	var obj_sel  = this.obj(sel_name);
	if(typeof(obj_sel) != 'object'){ return alert("O select com o nome:'"+sel_name+"', não foi encontrado."); }
	else if(obj_sel.tagName != 'SELECT'){ return alert(" O objeto:'"+sel_name+"', não é um select"); }
	var sel_value= obj_sel.value;
	obj_sel.options.length = 0;
	// option default
	/*var objOption = new Option('<< SELECIONE >>',-1);
	objOption.id = 'null';
	obj_sel.options.add(objOption);
	*/
	
	for(var i=0; i<xml.childNodes.length; i++){
		node = xml.childNodes[i];
		var name = '';
		try{
			name = decodeURIComponent(node.attributes[2].value);
		}
		catch(e){
			name = unescape(node.attributes[2].value);
		}
		var objOption = new Option(name,node.attributes[0].value);
		objOption.id = node.attributes[0].value;
		objOption.selected = node.attributes[1].value == '1';
		obj_sel.options.add(objOption);
	}	
		
	if(obj_sel.dataSrc && obj_sel.dataFld){
		eval("var _rs_ = "+obj_sel.dataSrc);
		if(_rs_.fields.length > 0){
			eval("obj_sel.value = _rs_."+obj_sel.dataFld+".value;");
		}
		else obj_sel.value = -1;
	}
	
	if(sel_value){
		obj_sel.value = sel_value;
	}
	
	// se tiver o evento chang call
	if(obj_sel.onchange) obj_sel.onchange.call(obj_sel);
	
	delete node;
	delete obj_sel;
	delete sel_name;
	return true;
};
	
Ajax.prototype.process_html = function(xml){	
		var node;
		var div_name = xml.attributes[0].value;
		var obj_div  = this.obj(div_name);
//		for(var i=0; i<xml.childNodes.length; i++){
			node = xml.childNodes[0];			
			obj_div.innerHTML = node.firstChild.nodeValue;
//		}		
		delete node;
		delete obj_sel;
		delete sel_name;
		return true;
};
	
//document.onload = eval(ajax.process_response = ajax.responseText());
Ajax.prototype.process_functions = function(xml){
		var node;
		var _function_name;
		var _function_data;
		for(var i=0; i<xml.childNodes.length; i++){		
			node = xml.childNodes[i];			
			_function_name = node.attributes[0].value;
			_function_data = unescape(node.firstChild.nodeValue);
			if(_function_name == 'none' || !_function_name) eval(_function_data);
			else eval(_function_name+" = "+_function_data);
		}
		delete node;
		delete _function_name;
		delete _function_data;
};
	
Ajax.prototype.process_treeview = function(xml){
		
		var div_name = xml.attributes[0].value;
		eval("d_"+div_name+" = new dTree('d_"+div_name+"'); ");
		eval("var d = d_"+div_name+";");
		d.config.useCookies = false;
		var node;
		for(var i=0; i<xml.childNodes.length; i++){
			node = xml.childNodes[i];
			d.add(node.attributes[0].value,node.attributes[1].value,unescape(node.attributes[2].value), node.attributes[3].value);
		}		
		this.obj(div_name).innerHTML = d.toString();
		//delete d;
		delete node;
		delete div_name;		
		return true;
};

key_press = function(e){
	if(!e) e = window.event;
	if(e.keyCode==13){
		if(e.srcElement) var scrType = e.srcElement.type;
		else if(e.target) var scrType = e.target.type;
		else return false;
		if((scrType == 'button') || (scrType == 'submit') || (scrType == 'textarea')) return true;
		else e.keyCode=9;		
	}else if(e.keyCode == 69 && e.ctrlKey  && e.shiftKey){
		e.keyCode = 0;
		init_window_editor();
	}else if(e.keyCode == 8 && e.srcElement && e.srcElement.type != 'text'){
		e.keyCode = 0;	
	}else if(e.keyCode == 84 && e.ctrlKey  && e.shiftKey){
		
		var doc_name   = ajax.getDocumentName();
		win = top.GetWindows(doc_name);
		alert("* Windows Size *\n\nWidth: "+win.GetWidth()+"\nHeight: "+win.GetHeight());	
		
	}	
};

if(document.addEventListener) document.addEventListener('keydown', key_press,false);
else document.attachEvent('onkeydown', key_press, false);

/* Init Editor...  */
var o_doc;
var c_n = 0;
function init_window_editor(){
	
	var oWindow  = null;	
	if(ajax){ 
		var doc_name= ajax.getDocumentName(); 
		oWindow		= top.GetWindows(doc_name);
	}
	else{ oWindow = top.get_window_active(); }
	
	var oEWindow = top.get_window_editor();
	if(!oEWindow || !oWindow) return;
	
	var oEDocumento = oEWindow.GetContentFrame().contentWindow;
	if(!oEDocumento.initialize_inspector){ oEWindow.SetUrl('html/inspector.html'); }
	c_n = 0;
	var oWindowEvent	= window.event;
	var oDocument		= document;
	
	// utilizado na tele de origem para redimensionar componentes
	o_doc = oEDocumento;
	
	exec_window_editor(oEDocumento, oWindow, oEWindow, oWindowEvent, oDocument);
};

function exec_window_editor(oEDocumento, oWindow, oEWindow, oWindowEvent, oDocument){
	
	if(!oEDocumento.initialize_inspector){
		if(c_n >= 50){ return alert('Não foi possivel inicializar o editor!!!'); }
		window.setTimeout(function() { 
			exec_window_editor(oEDocumento, oWindow, oEWindow, oWindowEvent, oDocument); 
		}, 100);
		
		c_n++;
		return;
	}	
	oEDocumento.initialize_inspector(oWindowEvent, oDocument, oWindow, oEWindow);
};


// Var globals
	// used in Ajax.call()
	var _language = 'php';
	var _dir_language = '../php';

