function SAjaxException(timeout) {
	this.toString = function() {
      	return "ERRO (ajax) - Nao foi possivel ler o arquivo!";
    };
}

function WindowException(timeout) {
	this.toString = function() {
      	return "Local de retorno nao existe!";
    };
}

function RequestException(win) {
    this.w = win;
	this.w.close();
	this.toString = function() {
      	return "Request indefinido!";
    };
}

function NotFindLocationException(ele) {
	this.toString = function() {
      	return "Este local '"+ ele +"' nao existe!";
    };
}

function SAjax() {
	var ajax 		= null;
	var timeout 	= false;
	var functionBack= null;

	this.getAjax = function() {
		var ajx = null;
		try {
		    ajx = new XMLHttpRequest();
		} catch(ee) {
		    try {
		        ajx = new ActiveXObject("Msxml2.XMLHTTP");
		    } catch(e) {
		        try {
		            ajx = new ActiveXObject("Microsoft.XMLHTTP");
		        } catch(E) {
		            ajx = false;
		        }
		    }
		}
		return ajx;
	}

	this.setFunctionBack = function(func) {
		functionBack = func;
	}

	this.loader = function(oform, retorno, request, url, text_load) {
		ajax = this.getAjax();
		if (San.$(retorno))
			retorno = San.$(retorno);
		else
			throw new WindowException();

		if (!text_load)
			text_load = 'Carregando...';

		var formValues;
		//var url = 'program/steps/contrato/enviapedido.php?btn_dados=1';

		request = request ? request.toUpperCase() : null;
		if (request == "GET") {
			ajax.open(request, url, true);
			ajax.send(null);
		} else if(request == "POST") {
			formValues = San.getFormValues(oform);
			ajax.open(request, url, true);
	      	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	      	ajax.setRequestHeader("Content-length", formValues.length);
	      	ajax.setRequestHeader("Connection", "close");
			ajax.send(encodeURI(formValues));
		} else {
			throw new RequestException(win);
		}

		retorno.innerHTML = "<center>"+text_load+"</center>";
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 0 || ajax.status == 200) {
					result = ajax.responseText;
					retorno.innerHTML = result;
					if (functionBack != null)
						eval(functionBack);
					functionBack = null;
				}
			}
		}
	}
};

sanAjax = new SAjax();