﻿function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return false;
//		document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.' + 'Consider upgrading your browser.';	
	}
}

var httpRequest = getXmlHttpRequestObject();

var uninitialized=0;	// open has not yet been called
var loading=1;			// send has not yet been called but open has been called
var loaded=2;			// send has been called but no response from server
var interactive=3;		// data is in the process of being received from the server
var complete=4;			// response from server has arrived

var countCalls=0;

function prepareToSend(param) {
//	while(param.indexOf("'") >= 0)
//		param = param.replace("'",'"');
	return encodeURIComponent(param);
}

function AjaxMessageToSend(server, param, callback) {
    this.Server = server;
    this.Param = param;
    this.CallBack = callback;
}

AjaxMessageToSend.prototype.Send = function() {
    //alert(this.Server+'?'+this.Param);
    //httpRequest.open("GET", this.Server+'?'+this.Param, true);
    //httpRequest.open("POST", this.Server+'?'+this.Param, true);
    httpRequest.open("POST", this.Server, true);
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpRequest.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    httpRequest.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
    httpRequest.setRequestHeader("Pragma", "no-cache");
    httpRequest.onreadystatechange = function() { getParam(httpRequest); }
    httpRequest.send(this.Param);
}

/*

ProcessoMap.prototype.AlteraCursor = function(cursor) {
	this.CursorAnterior = this.Area.style.cursor;
	this.Area.style.cursor = cursor;
}
*/

var Ajax_sending = false;
var Ajax_toSend = [];
var Ajax_CurrentCursor;

function sendFirst() {
    if (Ajax_toSend.length > 0 && (!Ajax_sending)) {
        ShowWait();
        Ajax_sending = true;
        Ajax_toSend[0].Send();
        return true;
    }
    return false;
}

function sendNext() {
    if (sendFirst()) {
//        alert('Enviando pendente');
    }
}

function sendParam(server, param, callback) {
    if (!callback)
        callback = doNothing;
	if (httpRequest && (httpRequest.readyState == complete || httpRequest.readyState == uninitialized)) {
	    param += ("&countCalls=" + (countCalls++));
	    Ajax_toSend[Ajax_toSend.length] = new AjaxMessageToSend(server, param, callback);
	    sendFirst();
	}
}

function doNothing(ret) {
}

function getParam(httpRequest) {
    if (httpRequest.readyState == complete) {
		if (httpRequest.status == 200) {
		    var response = eval("(" + httpRequest.responseText + ")");
		    Ajax_toSend[0].CallBack(response);
		    Ajax_toSend.splice(0, 1);
		    Ajax_sending = false;
		    HideWait();
		    sendNext();
		} else {
			// there was a problem with the request,
			// for example the response may be a 404 (Not Found)
			// or 500 (Internal Server Error) response codes
			alert('Problemas de conexão com o servidor.');
		}
    }
}