/**
* Objeto AjaxNukeObject 
*/
function AjaxNukeObject(){
	this.dom = null;
	this.error = false;
}

/**
* Classe para converter XML
* @constructor
*/
function AjaxNukeParser(){
	/**
	*@var 
	*/
	this.xml = null;
	/**
	*@var DOMParser
	*/
	this.parser = null;
}
AjaxNukeParser.prototype = new AjaxNukeObject();

/**
* Parse a XML string to DOM Document
* @return DOMDocument
*/
AjaxNukeParser.prototype.loadXmlFromString = function(xmlString){
	if(!xmlString){
		xmlString = "<root/>";
	}
		return (new DOMParser()).parseFromString(xmlString, "text/xml");
};
/**
* Parse a XML string to DOM Document
* @return DOMDocument
*/
AjaxNukeParser.prototype.loadXmlFromFile = function(xmlFile){
	if(!xmlFile){
		return false;
	}
	var myXMLHTTPRequest = new XMLHttpRequest();
	myXMLHTTPRequest.open("GET", xmlFile, false);
	myXMLHTTPRequest.send(null);
	return myXMLHTTPRequest.responseXML;
};

/**
* Classe para conexões assincronas de requisições XmlHttpRequest
* @constructor
*/
function AjaxNukeHttpRequest(){
	/**
	*@var XmlHttpRequest
	*/
	this.loaded = false;
	this.loadedXml = null;
	this.loadedText = "";
	this.method = "GET";
	this.request = null;
	this.xmlObject = null;
};
AjaxNukeHttpRequest.prototype = new AjaxNukeObject();

AjaxNukeHttpRequest.prototype.openRequest = function(server, type){
	if(!type){
		type = true;
	}
	this.request = new XMLHttpRequest();
	this.request.open(this.method, server, type);
	var self = this;
	function onReadyState(){
		self.loaded = true;
		if(self.request.readyState == 4){
			try{
				if(self.request.status == 200){
					self.loadedXml = self.request.responseXML;
					self.loadedText = self.request.responseText;
					if(self.xmlObject && self.loadedXml.childNodes.length > 0){
						self.xmlObject.ready(self);
					}else{
						alert("Module Error!\n" + self.loadedText);
					}
				}
			}catch(e){
				try	{ engine.setError(e); }
				catch (e) {}
			}
		}
	}
	this.request.onreadystatechange = onReadyState;
	this.request.setRequestHeader('content-type', 'text/xml');
	this.request.send(null);
};

/**
* Set Object Called
*
*/
AjaxNukeHttpRequest.prototype.setXmlObject = function(object){
	this.xmlObject = object;
};

/**
* AjaxNuke Engine
*/
function AjaxNukeEngine(){
	this.parser = new AjaxNukeParser();
	this.serializer = new XMLSerializer();
	this.xslt = new XSLTProcessor();
	this.engineAddress = "ajaxnuke.php?";
	this.xsl = null;
	this.site = "sample";
	this.lastError = null;
}
/**
* Load XSL from server
*
*/
AjaxNukeEngine.prototype.loadXsl = function(xslName){
	if(this.controlErrors()){
		return;
	}
	this.xsl = new XslObject(xslName, this.engineAddress + "getxsl=true&xsl=" + xslName + "&site=" + this.site);
};

/**
* Transform component requested
*
*/
AjaxNukeEngine.prototype.transformComponent = function(xmlObjec){
	try{
		return this.xslt.transformToDocument(xmlObjec);
	}catch (e){
		this.controlErrors(e);
	}
};
/**
* Function called while load the request
*
*/
AjaxNukeEngine.prototype.getComponentInHTML = function(htmlObject){
	return this.serializer.serializeToString(htmlObject);
};

/**
* Keep all errors under control
*
*/
AjaxNukeEngine.prototype.controlErrors = function(){
	if(this.lastError){
		// Uncomment for Debug proposes
		//alert("AjaxNuke Engine Error\n\nError: " + this.lastError.message);
		return true;
	}
	return false;
};
/**
* Report an error to engine
*
*/
AjaxNukeEngine.prototype.setError = function(error){
	this.lastError = error;
	this.controlErrors();
};

/**
* Load AJaxNuke Class File
*
*/
AjaxNukeEngine.prototype.loadClassFile = function(file){
	anboot.loadJsFile(file);
};