// Adaptação do script oyXMLRPC.js
//
//  Copyright 2005, Pavel Simakov, 
//  http://www.softwaresecretweapons.com
//
//
function ServerRequest () 
{	
	var status = null;
  var noCache = '?noCache=' + (new Date()).getTime();
	var url = null;	
	var req = null;
	var msgCount = 0;	
	var nIndex = 0;
	var inProgress = false;
	var isComplete = false;
	var maxLogMsgs = 1000;
	var aFunctions = Array;
	var typeSubmit = 'GET';

	var STATE_COMPLETED = 4;
	var STATUS_200 = 200;
		
	var oThis = this;
	
	// Diz enquanto está ocupado carregando o arquivo
	var internalIsBusy = function()
	{
		return inProgress && !isComplete;
	}	
	
	// Função chamada enquanto é carregado o arquivo
	var internalRequestComplete = function() 
	{
		if (!internalIsBusy()) { /* nenhum request foi enviado */	}
		
		if (req.readyState == STATE_COMPLETED) 
		{
			status = req.status;
			inProgress = false;
			isComplete = true;

			if (status == STATUS_200) 
			{
				if(oThis.onComplete)
				{
					oThis.onComplete(req.responseText, req.responseXML);
					oThis.runScripts();
				}
			} 
			else 
			{
				// Erro
			}
		}
	}	
	
	this.getLastModified= function()
	{
		return req.getResponseHeader("Last-Modified");
	}
	
	// Chamar está função para verificar se pode ter mais chamadas ou se o request feito já foi finalizado
	this.isBusy = function()
	{
		return internalIsBusy();
	}		

	//  Enviar um novo request
	this.submit = function(_url)
	{	
		if (internalIsBusy()) { /* processando outro request */ }
		
		if (_url.indexOf("?") != -1) { noCache = noCache.replace("?","&"); }		
		url = _url + noCache;	
		status = null;
		inProgress = true;
		isComplete = false;
		
	    if (window.XMLHttpRequest) 
	    {
	      req = new XMLHttpRequest();
	      req.onreadystatechange = internalRequestComplete;
				//req.overrideMimeType('text/html; charset=ISO-8859-15'); 
	      req.open(typeSubmit, url, true);
        req.send(null);	
	    } 
	    else 
	    { 
	    	if (window.ActiveXObject) 
	    	{
					try {
					req = new ActiveXObject('Msxml2.XMLHTTP');
					} catch (e) {
					try {
					req = new ActiveXObject('Microsoft.XMLHTTP');
					} catch (e) {} 
					}	    		

		    	if (req) 
		    	{
						req.onreadystatechange = internalRequestComplete;
						req.open(typeSubmit, url, true);
						req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
			    	req.setrequestheader("Pragma","no-cache");
		   	  	req.setrequestheader("Cache-control","no-cache");
		      	req.send();	
					} 
					else 
					{
						// Erro... impossivel criar Microsoft.XMLHTTP
		    	}
		    } 
		    else 
		    {
		    	// Erro... o browser nao suporta XML HTTP Request
		    }
	    }
     	return req;
	}
	
	// essa função é chamada quando é finalizado o request, pode ser sobrescrita
	this.onComplete = function(responseText, responseXML)
	{
		
	}

	this.run = function(fctn)
	{
		aFunctions[nIndex++] = fctn;
	}

	this.runScripts = function()
	{
		for (i in aFunctions)
			eval(aFunctions[i]);
	}
}