﻿
	/**
	 * Adiciona método trim() à classe String. Elimina brancos no início e fim da String.
	 */
	String.prototype.trim = function() {
		return this.replace(/^\s*/, "").replace(/\s*$/, "");
	}
	
	var AjaxUtil = function() {
	}

	AjaxUtil.zIndexCorrente = 200;
	
	AjaxUtil.call = function(url, metodoCallback, naoMostrarMsg) {
				
		if (AjaxUtil.isEmpty(url)) {
			alert("URL vazia (AjaxUtil)");
			return;
		}

		if (naoMostrarMsg != true) {
			AjaxUtil.mostrarMsgAjax("Enviando...");
		}
		
		var request = AjaxUtil.criarXMLHttpRequest();

		request.open("GET", url, true);
		
		if (metodoCallback != null) {
			request.onreadystatechange = function() { 
				AjaxUtil.callback(request, metodoCallback);
			}
		}

		request.send(null);
	}

	AjaxUtil.criarXMLHttpRequest = function() {
	
		var request = false;

		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				request = false;
			}
		}
		
		if (!request && typeof XMLHttpRequest != 'undefined') {
			try {
				request = new XMLHttpRequest();
			} catch (e) {
				request = false;
			}
		}
		
		if (!request && window.createRequest) {
			try {
				request = window.createRequest();
			} catch (e) {
				request = false;
			}
		}
		
		return request;
	}
	
	AjaxUtil.callback = function(request, metodoCallback) {
		
		if (request.readyState != 4) {
			return;
		}
		
		if (request.status != 200) {
			alert("Não foi possível completar a operação (erro ajax: " + request.status + ", " + request.statusText + ")");
			return;
		}
		
		// loaded 
		
		if (metodoCallback != null) {
			metodoCallback.call(null, request.responseText);
		}
		
		AjaxUtil.esconderMsgAjax();
	}	

	AjaxUtil.mostrarMsgAjax = function(msgAjax, tempoExibicao) {
	
		if (tempoExibicao == null) {
		
			var divMsgAjax = document.getElementById("divMsgAjax");
	
			if (!divMsgAjax) {
				divMsgAjax = document.createElement('div');
	
				divMsgAjax.setAttribute('id', 'divMsgAjax');
				divMsgAjax.style.zIndex = AjaxUtil.getNextZIndex();
				divMsgAjax.className = "flutuanteDireita msgAjaxCarregando";
	
				document.body.appendChild(divMsgAjax);
			}
	
			divMsgAjax.innerHTML = msgAjax;
			divMsgAjax.style.visibility = 'visible';
			
		} else {
			
			// cria uma div nova para esconder posteriormente
			var zIndex = WidgetsVH.getNextZIndex();
			
			var divMsgAjax = document.createElement('div');

			divMsgAjax.setAttribute('id', "divMsgAjax" + zIndex);
			divMsgAjax.style.zIndex = zIndex;
			divMsgAjax.className = "flutuanteDireita msgAjaxCarregando";

			document.body.appendChild(divMsgAjax);

			divMsgAjax.innerHTML = msgAjax;
			divMsgAjax.style.visibility = 'visible';
			
			setTimeout("document.getElementById('divMsgAjax" + zIndex + "').style.visibility = 'hidden'", tempoExibicao * 1000);		
		}	
	}	

	AjaxUtil.esconderMsgAjax = function() {
		document.getElementById("divMsgAjax").style.visibility = 'hidden';
	}	

	AjaxUtil.isEmpty = function(str) {
		return ((str == null) || (str.toString().trim().length == 0));
	}	

	AjaxUtil.getNextZIndex = function() {
		return ++AjaxUtil.zIndexCorrente;
	}	
	

