(function() {

var $wnd = window;
var $doc = $wnd.document;

/*
** Private static declarations
*/

if(!$doc.getElementById) {
	$doc.getElementById = function(id) {
		return $doc.all[id];
	};
}

var private_static_copyAttributes = function(obj, optionsObj, ignoreUndefinedProps)
{
	if (!optionsObj)
		return;

	for (var optionName in optionsObj)
	{
		if (ignoreUndefinedProps && optionsObj[optionName] === undefined)
			continue;
		obj[optionName] = optionsObj[optionName];
	}
};

// private static msProgIDs: Array
var msProgIDs = [
	"MSXML2.XMLHTTP.6.0", 
	"MSXML2.XMLHTTP.5.0", 
	"MSXML2.XMLHTTP.4.0", 
	"MSXML2.XMLHTTP.3.0", 
	"Msxml2.XMLHTTP", 
	"Microsoft.XMLHTTP"
];

// private static
var private_static_createXMLHttpRequest = function() 
{
	var req = null;
	try 
	{
		if ($wnd.XMLHttpRequest) {
			req = new $wnd.XMLHttpRequest();
		}

		if (!req && $wnd.ActiveXObject)
		{
			while (!req && msProgIDs.length)
			{
				try { req = new ActiveXObject(msProgIDs[0]); } catch (e) { req = null; }
				if (!req)
					msProgIDs.splice(0, 1);
			}
		}
	}
	catch (e) { req = null;	}

	if (!req) {
		alert("N\xE3o foi poss\xEDvel estabelecer comunica\xE7\xE3o com o servidor" );
	}

	return req;
};

// Public innerClass declaration
var RequestClass = function() 
{
	this.method = "GET";
	this.async = true;
	this.headers = {};

	this.url = this.username = this.password = this.postData = this.successCallback = 
	this.errorCallback = this.userData = this.xhRequest = null;
};
RequestClass.prototype = 
{
	
	clone: function() 
	{
		var req = new RequestClass();
		
		req.url = this.url;
		req.username = this.username;
		req.password = this.password;
		req.postData = this.postData;
		req.successCallback = this.successCallback;
		req.errorCallback = this.errorCallback;
		req.userData = this.userData;
		
		if (this.headers) {
			private_static_copyAttributes(req.headers, this.headers);
		}

		return req;	
	}
	
};

// private static
var private_static_callback = function(req)
{
	if (!req || req.xhRequest.readyState != 4)
		return;
	if (req.successCallback && (req.xhRequest.status == 200 || req.xhRequest.status == 0))
		req.successCallback(req);
	else if (req.errorCallback)
		req.errorCallback(req);
};

var private_static_eval = function(s) 
{
	return $wnd.eval(s);
};

var private_static_setInnerHTML = function(ele, str, preventScripts) 
{
	if (!ele) return;
		
	if(typeof(ele) === 'string') {
		ele = $doc.getElementById(ele);
		if (!ele) return;
	}

	var scriptExpr = "\x3Cscript[^\x3E]*\x3E(.|\s|\n|\r)*?\x3C\x2Fscript\x3E";
	ele.innerHTML = str.replace(new RegExp(scriptExpr, "img"), "");

	if (preventScripts)
		return;

	var matches = str.match(new RegExp(scriptExpr, "img"));
	if (matches)
	{
		var numMatches = matches.length;
		for (var i = 0; i < numMatches; i++)
		{
			var s = matches[i].replace(/<script[^>]*>[\s\r\n]*(<\!--)?|(-->)?[\s\r\n]*<\/script>/img, "");
			private_static_eval(s);
		}
	}
};


// Static class declaration
var SAjax = $wnd.SAjax = {

	// class
	Request: RequestClass,
	

	loadURL: function(method, url, async, callback, opts) {
		var req = new RequestClass();
		req.method = method;
		req.url = url;
		req.async = async;
		req.successCallback = callback;
		private_static_copyAttributes(req, opts);

		try
		{
			req.xhRequest = private_static_createXMLHttpRequest();
			if (!req.xhRequest)
				return null;

			if (req.async)
				req.xhRequest.onreadystatechange = function() { private_static_callback(req); };

			req.xhRequest.open(req.method, req.url, req.async, req.username, req.password);

			if (req.headers)
			{
				for (var name in req.headers)
					req.xhRequest.setRequestHeader(name, req.headers[name]);
			}

			req.xhRequest.send(req.postData);

			if (!req.async)
				private_static_callback(req);
		}
		catch(e)
		{
			if (req.errorCallback)
				req.errorCallback(req);
			else
				dPrint("Exception caught while loading " + url + ": " + e);
			req = null;
		}

		return req;
	},
	
	updateContent: function (ele, url, finishFunc, opts) {
		SAjax.loadURL("POST", url, true, function(req) 
		{
			private_static_setInnerHTML(ele, req.xhRequest.responseText, true);
			if (finishFunc) {
				finishFunc(ele, url);
			}
		}, opts);
	},
	
	setInnerHTML: private_static_setInnerHTML
};
})();