/**
 * @fileoverview nhJax Transport library.
 * Utilizada para receber os dados do servidor ,
 * efetuar o processamento e serializar dados XML e JSON.
 *
 * <pre>
 * Copyright (c) 2006 NHSOFT Tecnologia.
 * </pre>
 * 
 * @since 22/11/2006
 * @version 1.0
 * @author Loe Antonio de Souza Lobo
 * @author Paulo Ferreira Nhaia
 * 
 */

if (typeof nhJax == 'undefined') {
  /**
   * Definicao do namespace.
   * Construtor do Objeto nhJax, detentor
   * de todas as funcionalidades
   * @constructor
   */
  nhJax = function() {};
}

/**
 * Construtor do modulo de transporte do nhJax
 * @constructor
 */
nhJax.Transport = function() {};

// Determina a versao mais nova dos objetos ActiveX disponivel
if (typeof ActiveXObject != 'undefined') {

  /**
   * Variavel contendo o nome da versao do objeto ActiveX XMLDOM
   * @private
   */
  nhJax.Transport.XMLDOM = null;

  /**
   * Variavel contendo o nome da versao do objeto ActiveX XMLHTTP
   * @private
   */
  nhJax.Transport.XMLHTTP = null;

  /**
   * @ignore
   * Retorna o primeiro objeto ActiveX disponivel de uma dada lista.
   *
   * @param {object} arrVersions Lista de objetos ActiveX para efetuar o teste
   * @return O nome do primeiro objeto ActiveX disponivel ou null
   * @type string
   */
  nhJax.Transport.pickActiveXVersion = function(arrVersions) {
    for (var iVn = 0; iVn < arrVersions.length; iVn++) {
      try {
        var objDocument = new ActiveXObject(arrVersions[iVn]);
        // If it gets to this point, the string worked
        return arrVersions[iVn];
      } catch (objException) {};
    }
    return null;
  };

  /**
   * Mais nova versao do objeto ActiveX XMLDOM.
   * @private
   */
  nhJax.Transport.XMLDOM = nhJax.Transport.pickActiveXVersion([
    'Msxml2.DOMDocument.4.0',
    'Msxml2.DOMDocument.3.0',
    'MSXML2.DOMDocument',
    'MSXML.DOMDocument',
    'Microsoft.XMLDOM'
  ]);

  /**
   * Mais nova versao do objeto ActiveX XMLHTTP.
   * @private
   */
  nhJax.Transport.XMLHTTP = nhJax.Transport.pickActiveXVersion([
    'Msxml2.XMLHTTP.4.0',
    'MSXML2.XMLHTTP.3.0',
    'MSXML2.XMLHTTP',
    'Microsoft.XMLHTTP'
  ]);

  /**
   * @deprecated
   */
  nhJax.Transport.pickActiveXVersion = null;

}
/**
 * Cria o objeto XMLHttpRequest cross browser.
 *
 * @return Novo XMLHttpRequest objeto.
 * @type object
 */
nhJax.Transport.createXmlHttpRequest = function() {
  if (typeof XMLHttpRequest != 'undefined') {
    return new XMLHttpRequest();
  }
  if (typeof ActiveXObject != 'undefined') {
    try {
      return new ActiveXObject(nhJax.Transport.XMLHTTP);
    } catch (objException) {};
  }
  return null;
};

/**
 * Verifica se a imagem de Loading esta sendo exibida em um determinado
 * div.
 *
 * <pre>
 * Formato dos argumentos:
 * {
 *   busyContainer: [objeto ou string] elemento onde a imagem sera exibida
 * }
 * </pre>
 *
 * @private
 * @param {object} objArgs Argumentos do objeto
 * @return True se a imagem esta sendo exibida
 * @type boolean
 */
nhJax.Transport.isBusy = function(objArgs) {
	try{
		if(nhJax.Transport.currenteModalShadow){
			return true;
		}else{
			return false;
		}
	}catch(e){
		alert('Erro::isBusy::\nErro::'+e);
	}
};

/**
 * Retorna o caminho para o arquivo js especificado. Navega por todos os elemtos
 * script carregados, iniciando do final. Acha o arquivo js especificado
 * no atributo src do elemento script. Separa o atributo src e retorna o caminho
 * sem o nome do arquivo js.
 *
 * @param {string} strScriptFileName Nome do arquivo Js, ex. 'nhJaxUtil.js'
 * @return Caminho para o script, ex. '../src/' ou '' se o caminho nao for encontrado
 * @type string
 */
nhJax.Transport.getPath = function(strScriptFileName) {
  // Obtam todos os elementos script
  var arrScripts = document.getElementsByTagName('script');
  // Encontra o script na lista obtida
  for (var iScript = arrScripts.length - 1; iScript >= 0; iScript--) {
    var strSrc = arrScripts[iScript].getAttribute('src') || '';
    var arrTokens = strSrc.split('/');
    // Remove a ultima parte
    var strLastToken = arrTokens.pop();
    if (strLastToken == strScriptFileName) {
      return arrTokens.join('/') + '/';
    }
  }
  
  // Nao encontrado
  return '';
};

/**
 * Exibe a imagem de loading em um div especificado.
 *
 * <pre>
 * Formato dos argumentos:
 * {
 *	 busyCloseOnClick   : [string] habilita o click close quando houver click fora da area de shadow,
 *   busyShadown		: [boolean] habilita efeito shadown,
 *   busyImage			: [string] define a imagem de loading,
 * }
 * </pre>
 *
 * @private
 * @param {object} objArgs Argumentos
 */
nhJax.Transport.currenteModalShadow = null; // usado no removeBusy
nhJax.Transport.showBusy = function(objArgs) {
	try{
		var imageDynamic = new Image;
		var imageDynamicLoaded = false;
		imageDynamic.onload = function(){
			if(!imageDynamicLoaded){
				var strImg = '<img style="position: absolute;" src="'+imageDynamic.src+'" width="'+imageDynamic.width+'" height="'+imageDynamic.height+'" />';
				var controlOpen;
				if(Control.Modal.current){
					nhJax.Transport.currenteModalBefore = Control.Modal.current;
					Control.Modal.current.options.roundCorner = false;
					Control.Modal.current.update('<div class="imgLoader">'+strImg+'</div>');
					nhJax.Transport.currenteModalShadow = false;
					nhJax.Transport.currenteModalShadow = false;
					imageDynamicLoaded = true;
				}else{
					controlOpen = new Control.Modal.open(strImg,
					{
						overlayDisplay: (typeof(objArgs.busyShadown) == 'boolean')? objArgs.busyShadown : true ,/*exibe o shadow*/
						overlayCloseOnClick: (typeof(objArgs.busyCloseOnClick) != 'boolean')? objArgs.busyShadownCloseOnClick : true,/*click close*/
						opacity: 0.7,
						position: 'absolute',
						fade: true,
						roundCorner: false,
						containerClassName: 'imgLoader' // Formatacao especifica no css, geral.css
					});
					nhJax.Transport.currenteModalShadow = controlOpen;//Control.Modal.current; // usado no close
					imageDynamicLoaded = true;					
				}
			}			
		};
		if(typeof(objArgs.busyImage) != 'undefined' && objArgs.busyImage != ''){
			imageDynamic.src = objArgs.busyImage;
		}else{
			imageDynamic.src = 'img/ajax-loader.gif';
		}
	}catch(e){
		alert('Erro::showBusy::\nErro::'+e);
	}
};

/**
 * Remove a imagem de Loading que foi colocada por {@link nhJax.Transport#showBusyGif}
 * de um especificado div.
 *
 * @private
 * @param {object} objArgs Argumentos
 */
nhJax.Transport.removeBusy = function() {
	if(nhJax.Transport.currenteModalShadow){
		Control.Modal.current.options.roundCorner = nhJax.Transport.currenteModalShadow.options.roundCorner;
		Control.Modal.current.close();//usa objeto criado pela imagem dinamica
	}	
};

/**
 * Busca uma URL especifica utilizando um objeto XMLHttpRequeste novo.
 *
 * <pre>
 * Modo asincrono a recomendado pois a mais seguro e nao apresenta risco de ter
 * o script parado por problemas de internet. Modos sincronos significa que o 
 * codigo ira ficar parado enquanto nao obtiver resposta.
 *
 * Quando uma requisicao a completa, uma das funacoes retorno sao chamadas:
 * onLoad quando obteve sucesso ou onError quando obteve erro. Em modo sincrono
 * a funcao onLoad pode ser omitida. Ao invas utilize o objeto retornado.
 *
 * A funcao de retorno onLoad recebe o objeto XMLHttpRequest como argumento e
 * pode utilizar suas varias propriedade como por exemplo responseText, responseXML, etc.
 *
 * A funcao de retorno onError recebe o seguinte objeto:
 * {
 *   errorCode: numero do status do servidor (404, etc.) [number],
 *   errorDescription: descricao do erro [string]
 * }
 *
 * Nota: Alguns browsers implementam cache para requisiacoes GET. O cacheamento a 
 * previnido adicionando 'r=' + Math.random() como parametro para a URL.
 *
 * Se voce utiliza o matodo POST, o argumento de contaudo deve ser algo assim 
 * 'var1=value1&var2=value2' com os valorer utilizando urlencode. Se voce deseja
 * enviar outro conteaudo, utilize o contentType apropriado. Ex. 'multipart/form-data',
 * 'text/xml', * etc.
 *
 * Se a resposta do servidor contiver caracteres nao ASCII, o servidor deve enviar
 * o cabeacalho content-type correspondente. Ex.
 * "Content-type: text/plain; charset=utf-8" ou
 * "Content-type: text/plain; charset=windows-1251".
 *
 * Formato do argumento:
 * {
 *   url: [string] URL relativa ou absoluta que sera inquisitada,
 *   method: [string, optional] matodo ('GET', 'POST', 'HEAD', 'PUT'),
 *   async: [boolean, optional] use modo asincrono (default: true),
 *   contentType: [string, optional] content type when using POST,
 *   content: [string or object, optional] string postavel ou dados de objeto DOM
 *    quando usa POST,
 *   onLoad: [function, optional] funcao chamada quando se obteve sucesso,
 *   onError: [function, optional] funcao chamada quando se obteve erro,
 *   username: [string, optional] username,
 *   password: [string, optional] password,
 *   busyShadown	: [boolean] habilita efeito shadown,
 *   busyImage			: [string] define a imagem de loading,
*	 busyCloseOnClick   : [string] habilita o click close quando houver click fora da area de shadow,
 *   busyWaitRedirect: [boolean] true aguarda o redirect mantendo o efeito de carregando
 * }
 * </pre>
 *
 * @param {object} objArgs Argumentos
 * @return No modo sincrono objeto XMLHttpRequest ou null. No modo assincrono 
 * sempre null.
 * @type object
 */
nhJax.Transport.fetch = function(objArgs) {
  // Check arguments
  if (objArgs == null || typeof objArgs != 'object') {
    return null;
  }
  if (!objArgs.url) {
    return null;
  }
  if (!objArgs.method) {
    objArgs.method = 'GET';
  }
  if (typeof objArgs.async == 'undefined') {
    objArgs.async = true;
  }
  if (!objArgs.contentType && objArgs.method.toUpperCase() == 'POST') {
    objArgs.contentType = 'application/x-www-form-urlencoded';
  }
  if (!objArgs.content) {
    objArgs.content = null;
  }
  if (!objArgs.onLoad) {
    objArgs.onLoad = null;
  }
  if (!objArgs.onError) {
    objArgs.onError = null;
  }
  // Request URL
  var objRequest = nhJax.Transport.createXmlHttpRequest();
  if (objRequest == null) {
    return null;
  }
  // Show "Busy" animated GIF
  nhJax.Transport.showBusy(objArgs);
  // IE 6 calls onreadystatechange and then raises an exception if local file is
  // not found. This flag is used to prevent duplicate onError calls.
  var boolErrorDisplayed = false;
  try {
    // Open request
    if (typeof objArgs.username != 'undefined' &&
     typeof objArgs.password != 'undefined') {
      objRequest.open(objArgs.method, objArgs.url, objArgs.async,
       objArgs.username, objArgs.password);
    } else {
      objRequest.open(objArgs.method, objArgs.url, objArgs.async);
    }
    // Onready handler
    var funcOnReady = function () {
      // Remove "Busy" animated GIF
      if(typeof(objArgs.busyWaitRedirect) != 'undefined' && objArgs.busyWaitRedirect != '1'){
      	nhJax.Transport.removeBusy();
      }
      // Process response
      if (objRequest.status == 200 || objRequest.status == 304 ||
       (location.protocol == 'file:' && !objRequest.status)) {
        // OK or found, but determined unchanged and loaded from cache
        if (typeof objArgs.onLoad == 'function') {
          objArgs.onLoad(objRequest);
        }
      } else if (!boolErrorDisplayed) {
        boolErrorDisplayed = true;
        // 404 Not found, etc.
        nhJax.Transport.displayError(objRequest.status,
         'Error::fetch::\nError: Cannot fetch ' + objArgs.url + '.\n' +
         (objRequest.statusText || ''),
         objArgs.onError);
      }
    };
    // Prevent duplicate funcOnReady call in synchronous mode
    if (objArgs.async) {
      // Set onreadystatechange handler
      objRequest.onreadystatechange = function () {
        if (objRequest.readyState == 4) {
          // Request complete
          funcOnReady();
          // Prevent memory leak 
 	  objRequest.onreadystatechange = {}; 
 	}
      };
    }
    // Set content type if needed
    if (objArgs.contentType) {
		 objRequest.setRequestHeader('Content-Type', objArgs.contentType);
		 objRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    }
    // Send request
    objRequest.send(objArgs.content);
    // In synchronous mode the result is ready on the next line
    if (!objArgs.async) {
      funcOnReady();
      return objRequest;
    }
  } catch (objException) {
    // Remove "Busy" animated GIF
    nhJax.Transport.removeBusy();
    // Process error
    if (!boolErrorDisplayed) {
      boolErrorDisplayed = true;
      if (objException.name &&
       objException.name == 'NS_ERROR_FILE_NOT_FOUND') {
        nhJax.Transport.displayError(0,
         'Error::fetch::\nError: Cannot fetch ' + objArgs.url + '.\nFile not found.',
         objArgs.onError);
      } else {
        nhJax.Transport.displayError(0,
         'Error::fetch::\nError: Cannot fetch ' + objArgs.url + '.\n' +
         (objException.message || ''),
         objArgs.onError);
      }
    }
  };
  return null;
};

/**
 * Efetua o processamento de um fragmento de HTML dentro de um objeto HTMLElement.
 *
 * @param {string} strHtml Fragmento HTML
 * @return Elemento Div que contem o fragmento HTML processado
 * @type object
 */
nhJax.Transport.parseHtml = function(strHtml) {
  // Convert to string
  strHtml += '';
  // Remove leading whitespace characters because Firefox and Opera don't parse
  // fragment that starts from whitespace character
  strHtml = strHtml.replace(/^\s+/g, '');
  // Create temporaty container
  var objTempContainer = null;
	if (document.createElementNS) {
		// use the XHTML namespace
		objTempContainer =
		 document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
	} else {
		objTempContainer = document.createElement('div');
	}
  // Parse HTML fragment
  objTempContainer.innerHTML = strHtml;
  // Return container element
  return objTempContainer;
};

/**
 * Valida o javascript no escopo global.
 *
 * <p><b>
 * Note: Variaveis globias devem ser declaradas sem a palavra chave "var". Senao
 * elas serao ignoradas pelo Safari.
 * </b></p>
 *
 * @param {string} strScript Script a ser validado
 */
nhJax.Transport.evalGlobalScope = function(strScript) {
  if (typeof strScript != 'string' || !strScript.match(/\S/)) {
    return;
  }
  if (window.execScript) {
    // IE
    window.execScript(strScript, 'javascript');
  } else if (window.eval) {
    // Others
    window.eval(strScript);
/*
 This should never be reached
  } else {
    var funcScript = new Function(strScript);
    funcScript.call(window);
*/
  }
};

/**
 * Associa o fragmento de HTML a propriedade innerHTML do elemento especificado
 * e valida o javascript no escopo global, encontrado no fragmento
 *
 * <pre>
 * Arguments object format:
 * {
 *   html: [string] HTML fragment,
 *   container: [object or string, optional] element or id of element to put
 *    HTML fragment into
 * }
 * </pre>
 *
 * <p><b>
 * Note: Scripts sao executados apos o fragmento de HTML ser associado ao innerHTML.
 * Se scripts internos sao usados, eles sao carregados assincronamente e a sequencia
 * de execucao nao a preservada.
 * </b></p>
 *
 * @param {object} objArgs Arguments object
 */
nhJax.Transport.setInnerHtml = function(objArgs) {
  // Check arguments
  if (!objArgs || typeof objArgs.html != 'string') {
    return;
  }
  var strHtml = objArgs.html;
  // Get container
  var objContainer = null;
  if (typeof objArgs.container == 'string') {
    objContainer = document.getElementById(objArgs.container);
  } else if (typeof objArgs.container == 'object') {
    objContainer = objArgs.container;
  }
  // Extract javascripts
  var arrScripts = [];
  if (strHtml.match(/<\s*\/\s*script\s*>/i)) {
    // Split whole string by </script>
    var arrTokens = strHtml.split(/<\s*\/\s*script\s*>/i);
    var arrHtml = [];
    for (var iToken = arrTokens.length - 1; iToken >= 0; iToken--) {
      var strToken = arrTokens[iToken];
      if (strToken.match(/\S/)) {
        // Search <script ... > in the middle of each token
        var arrMatch = strToken.match(/<\s*script([^>]*)>/i);
        if (arrMatch) {
          // Separate HTML from javascript
          var arrCouple = strToken.split(/<\s*script[^>]*>/i);
          // IE doesn't put empty tokens into the array
          while (arrCouple.length < 2) {
            if (strToken.match(/^<\s*script[^>]*>/i)) {
              // HTML part is absent
              arrCouple.unshift('');
            } else {
              // javascript part is absent
              arrCouple.push('');
            }
          }
          // Save HTML fragment
          arrHtml.unshift(arrCouple[0]);
          // Get script attributes
          var strAttrs = arrMatch[1];
          // Get script text
          var srtScript = arrCouple[1];
          // Ignore script text if "src" attribute is present
          if (strAttrs.match(/\s+src\s*=/i)) {
            srtScript = '';
          } else {
            // Fix functions: function aaa() -> aaa = function()
            srtScript = srtScript.replace(/function\s+([^(]+)/g, '$1=function');
          }
          arrScripts.push([strAttrs, srtScript]);
        } else if (iToken < arrTokens.length - 1) {
          // On error assume this token is a part of previous token
          arrTokens[iToken - 1] += '</script>' + strToken;
        } else {
          // If this is last token, assume it is HTML fragment
          arrHtml.unshift(strToken);
        }
      } else {
        // Empty token
        arrHtml.unshift(strToken);
      }
    }
    // Get HTML part
    strHtml = arrHtml.join('');
  }
  // Set inner HTML
  if (objContainer) {
    // Opera hack
    objContainer.innerHTML = '<form></form>';
    objContainer.innerHTML = strHtml;
  }
  // Evaluate javascripts
  for (var iScript = 0; iScript < arrScripts.length; iScript++) {
    if (arrScripts[iScript][1].length) {
      // Evaluate in global scope
      nhJax.Transport.evalGlobalScope(arrScripts[iScript][1]);
    }
    // Load external script
    var strAttrs = arrScripts[iScript][0];
    strAttrs = strAttrs.replace(/\s+/g, ' ').replace(/^\s/, '')
     .replace(/\s$/, '').replace(/ = /g, '=');
    if (strAttrs.indexOf('src=') >= 0) {
      // Get container
      var objContainer = document.body;
      if (!objContainer) {
        objContainer = document.getElementsByTagName('head')[0];
        if (!objContainer) {
          objContainer = document;
        }
      }
      // Get attributes
      var arrAttrs = strAttrs.split(' ');
      // Load script
      var objScript = nhJax.Utils.createElement('script');
      for (var iAttr = 0; iAttr < arrAttrs.length; iAttr++) {
        var arrAttr = arrAttrs[iAttr].split('=');
        if (arrAttr.length > 1) {
          objScript.setAttribute(arrAttr[0],
           arrAttr[1].match(/^[\s|"|']*([\s|\S]*[^'|"])[\s|"|']*$/)[1]);
        } else {
          objScript.setAttribute(arrAttr[0], arrAttr[0]);
        }
      }
      // It's important for Safari to assign attributes before appending
      objContainer.appendChild(objScript);
    }
  }
};

/**
 * Adiciona e efetua o processamento do documento XML proveniente da URL especificada.
 *
 * <pre>
 * Quando o documento XML a adicionado e processado, uma das funacoes de retorno especificadas
 * a chamada: onLoad quando sucesso ou onError quando erro. Em modo sincrono a funcao onLoad
 * de retorno pode ser omitida. Ao invas use o objeto retornado.
 *
 * A funcao de retorno onLoad recebe o objeto XMLDocument como argumento e pode usar
 * o seu documentElement e outras propriedades.
 *
 * Funcao de retorno onError recebe o objeto a seguir:
 * {
 *   errorCode: codigo do erro [number],
 *   errorDescription: descricao do erro [string]
 * }
 * O codigo do erro sera 0 a nao ser que nhJax.Transport.fetch foi usado para para adicionar a URL
 * e houve um problema duranto o processo.
 *
 * Se o o argumento method nao foi definido, o mais eficiente XMLDOM no IE e
 * document.implementation.createDocument no Mozilla serao usados para adicionar
 * e efetuar o processamento do documento. Caso contrario nhJax.Transport.fetch sera usado para adicionar
 * o documento e nhJax.Transport.parseXml para efetuar o processamento.
 *
 * Nota: Alguns browsers implementam cache para as requisiacoes GET. O cacheamento pode ser
 * previnido adicionando o parametro 'r=' + Math.random() a URL.
 *
 * Se voce usa o matodo POST, o argumento de conteaudo deve ser algo parecido com 
 * 'var1=valor&var2=valor'. Se voce deseja enviar outro tipo de conteaudo, defina o  
 * contentType apropriado. Ex. para enviar uma string XML, voce deve definir contentType: 'text/xml'.
 *
 * Se a resposta do servidor contiver caracteres nao-ASCII, a codificacao deve ser especificada.
 * Ex. <?xml version="1.0" encoding="utf-8"?> ou 
 * <?xml version="1.0" encoding="windows-1251"?>.
 *
 * Se a resposta do servidor contiver caracteres nao-ASCII, o servidor deve enviar
 * o cabeacalho content-type correspondente. Ex.
 * "Content-type: text/xml; charset=utf-8" ou
 * "Content-type: text/xml; charset=windows-1251".
 *
 * Formato do objeto de argumento:
 * {
 *   url: [string] relativa ou absoluta a ser adicionada,
 *   method: [string, optional] matodo ('GET', 'POST', 'HEAD', 'PUT'),
 *   async: [boolean, optional] use modo assincrono (default: true),
 *   contentType: [string, optional] tipo de conteudo quando usado POST,
 *   content: [string or object, optional] string que sera enviada ou objeto de dados DOM
 *   quando usa POST,
 *   onLoad: [function, optional] funcao que sera chamda quando sucesso,
 *   onError: [function, optional] funcao que sera chamda quando erro,
 *   username: [string, optional] nome do usuario,
 *   password: [string, optional] senha,
 *   busyShadown	: [boolean] habilita efeito shadown,
 *   busyImage			: [string] define a imagem de loading,
 *	 busyCloseOnClick   : [string] habilita o click close quando houver click fora da area de shadow,
 *   busyWaitRedirect: [boolean] true aguarda o redirect mantendo o efeito de carregando
 * }
 * </pre>
 *
 * @param {object} objArgs Objeto de Argumento
 * @return Em modo sincrono objeto XMLDocumento ou null. Em modo assioncrono
 * sempre null.
 * @type object
 */
nhJax.Transport.fetchXmlDoc = function(objArgs) {
  // Check arguments
  if (objArgs == null || typeof objArgs != 'object') {
    return null;
  }
  if (!objArgs.url) {
    return null;
  }
  if (typeof objArgs.async == 'undefined') {
    objArgs.async = true;
  }
  if (!objArgs.onLoad) {
    objArgs.onLoad = null;
  }
  if (!objArgs.onError) {
    objArgs.onError = null;
  }
  // Try more efficient methods first
  if (!objArgs.method && typeof objArgs.username == 'undefined' &&
   typeof objArgs.password == 'undefined') {
    if (document.implementation && document.implementation.createDocument) {
      // Mozilla
      var objDocument = document.implementation.createDocument('', '', null);
      // Opera 8.51 also has document.implementation, but hasn't implemented
      // XMLDOM load method yet
      if (objDocument.load) {
        objDocument.async = objArgs.async;
        // Prevent duplicate onXmlDocLoad call in synchronous mode
        if (objArgs.async) {
          objDocument.onload = function() {
            // Remove "Busy" animated GIF
            nhJax.Transport.removeBusy();
            // Process response
            nhJax.Transport.onXmlDocLoad(objDocument, objArgs.onLoad,
             objArgs.onError);
          };
        }
        // Show "Busy" animated GIF
        nhJax.Transport.showBusy(objArgs);
        // Load document
        try {
          objDocument.load(objArgs.url);
          // In synchronous mode the result is ready on the next line
          if (!objArgs.async) {
            // Remove "Busy" animated GIF
            nhJax.Transport.removeBusy();
            // Process response
            nhJax.Transport.onXmlDocLoad(objDocument, objArgs.onLoad,
             objArgs.onError);
            return objDocument;
          }
          return null;
        } catch (objException) {
          // Remove "Busy" animated GIF
          nhJax.Transport.removeBusy();
          // Process error
          if (objException.name &&
           objException.name == 'NS_ERROR_FILE_NOT_FOUND') {
            nhJax.Transport.displayError(0,
             'Error: Cannot fetch ' + objArgs.url + '.\nFile not found.',
             objArgs.onError);
          } else {
            nhJax.Transport.displayError(0,
             'Error: Cannot fetch ' + objArgs.url + '.\n' +
             objException.toString(),
             objArgs.onError);
          }
        };
      }
    }
    if (typeof ActiveXObject != 'undefined') {
      // IE
      // Show "Busy" animated GIF
      nhJax.Transport.showBusy(objArgs);
      // Load document
      try {
        var objDocument = new ActiveXObject(nhJax.Transport.XMLDOM);
        objDocument.async = objArgs.async;
        // Prevent duplicate onXmlDocLoad call in synchronous mode
        if (objArgs.async) {
          objDocument.onreadystatechange = function () {
            if (objDocument.readyState == 4) {
              // Remove "Busy" animated GIF
              nhJax.Transport.removeBusy();
              // Process response
              nhJax.Transport.onXmlDocLoad(objDocument, objArgs.onLoad,
               objArgs.onError);
              // Prevent memory leak 
              objDocument.onreadystatechange = {}; 
            }
          };
        }
        objDocument.load(objArgs.url);
        // In synchronous mode the result is ready on the next line
        if (!objArgs.async) {
          // Remove "Busy" animated GIF
          nhJax.Transport.removeBusy();
          // Process response
          nhJax.Transport.onXmlDocLoad(objDocument, objArgs.onLoad,
           objArgs.onError);
          return objDocument;
        }
        return null;
      } catch (objException) {
        // Remove "Busy" animated GIF
        nhJax.Transport.removeBusy();
      };
    }
  }
  // Try XMLHttpRequest
  // Form argument for fetch
  var objFetchArgs = {};
  for (var strKey in objArgs) {
    objFetchArgs[strKey] = objArgs[strKey];
  }
  // Prevent duplicate parseXml call in synchronous mode
  if (objArgs.async) {
    objFetchArgs.onLoad = function(objRequest) {
      nhJax.Transport.parseparseXml({
        strXml: objRequest.responseText,
        onLoad: objArgs.onLoad,
        onError: objArgs.onError
      });
    };
  } else {
    objFetchArgs.onLoad = null;
  }
  // Fetch URL
  var objRequest = nhJax.Transport.fetch(objFetchArgs);
  // In synchronous mode the result is ready on the next line
  if (!objArgs.async && objRequest) {
    return nhJax.Transport.parseXml({
      strXml: objRequest.responseText,
      onLoad: objArgs.onLoad,
      onError: objArgs.onError
    });
  }
  return null;
};

/**
 * Processa uma string XML em um objeto XMLDocument.
 *or
 * <pre>
 * Quando uma string XML a processada, uma das funacoes de retorno especificadas
 * a chamada: onLoad quando sucesso ou onError quando erro. Em modo sincrono a funcao onLoad
 * de retorno pode ser omitida. Ao invas use o objeto retornado.
 *
 * A funcao de retorno onLoad recebe o objeto XMLDocument como argumento e pode usar
 * seu documentElement e outras propriedades.
 *
 * Funcao de retorno onError recebe o seguinte objeto:
 * {
 *   errorCode: codigo do erro [number],
 *   errorDescription: descricao do erro [string]
 * }
 * O codigo de erro sempre sera igual a 0.
 *
 * Retorna um obeto XMLDocument, assim a funcao onLoad a opcional.
 * O valor retornado e sua propriedade documentElement devem ser verificadas antes de serem
 * utilizadas, pois estas podem ser null ou undefined.
 *
 * Se a string XML contam caracteres nao-ASCII, a codificacao deve ser especificada.
 * Ex. <?xml version="1.0" encoding="utf-8"?> ou 
 * <?xml version="1.0" encoding="windows-1251"?>.
 *
 * Formato do objeto de argumento:
 * {
 *   strXml: string XML a ser processada [string],
 *   onLoad: funcao que sera chamda quando sucesso [function] (optional),
 *   onError: funcao que sera chamda quando erro [function] (optional)
 * }
 * </pre>
 *
 * @param {object} objArgs Objeto de Argumento
 * @return XMLDocument objeto ou null
 * @type object
 */
nhJax.Transport.parseXml = function(objArgs) {
  if (objArgs == null || typeof objArgs != 'object') {
    return null;
  }
  if (!objArgs.strXml) {
    return null;
  }
  if (!objArgs.onLoad) {
    objArgs.onLoad = null;
  }
  if (!objArgs.onError) {
    objArgs.onError = null;
  }
  if (window.DOMParser) {
    // Mozilla
    try {
      var objDocument = (new DOMParser()).parseFromString(objArgs.strXml,
       'text/xml');
      nhJax.Transport.onXmlDocLoad(objDocument, objArgs.onLoad,
       objArgs.onError);
      return objDocument;
    } catch (objException) {
      nhJax.Transport.displayError(0,
       'Error:parseXml::\nError: Cannot parse.\n' +
       'String does not appear to be a valid XML fragment.',
       objArgs.onError);
    };
    return null;
  }
  if (typeof ActiveXObject != 'undefined') {
    // IE
    try {
      var objDocument = new ActiveXObject(nhJax.Transport.XMLDOM);
      objDocument.loadXML(objArgs.strXml);
      nhJax.Transport.onXmlDocLoad(objDocument, objArgs.onLoad,
       objArgs.onError);
      return objDocument;
    } catch (objException) {};
  }
  return null;
};

/**
 * Veririca se ocorreram erros durante o processo de adicao e processamento do 
 * documento XML document e chama as funacoes correspondentes ao onLoad ou onError.
 *
 * @private
 * @param {object} objDocument Objeto XMLDocument
 * @param {function} funcao de retorno do onLoad informada pelo usuario
 * @param {function} funcao de retorno do onError informada pelo usuario
 */
nhJax.Transport.onXmlDocLoad = function(objDocument, onLoad, onError) {
  var strError = null;
  if (objDocument.parseError) {
    // Parsing error in IE
    strError = objDocument.parseError.reason;
    if (objDocument.parseError.srcText) {
      strError += 'Location: ' + objDocument.parseError.url +
       '\nLine number ' + objDocument.parseError.line + ', column ' +
       objDocument.parseError.linepos + ':\n' +
       objDocument.parseError.srcText + '\n';
    }
  } else if (objDocument.documentElement &&
   objDocument.documentElement.tagName == 'parsererror') {
    // If an error is caused while parsing, Mozilla doesn't throw an exception.
    // Instead, it creates an XML string containing the details of the error:
    // <parsererror xmlns="http://www.w3.org/1999/xhtml">XML Parsing Error: ...
    // Check if strings has been generated.
    strError = objDocument.documentElement.firstChild.data + '\n' +
     objDocument.documentElement.firstChild.nextSibling.firstChild.data;
  } else if (!objDocument.documentElement) {
    strError = 'String does not appear to be a valid XML fragment.';
  }
  if (strError) {
    // Parsing error
    nhJax.Transport.displayError(0,
     'onXmlDocLoad::\nError: Cannot parse.\n' + strError,
     onError);
  } else {
    // Success
    if (typeof onLoad == 'function') {
      onLoad(objDocument);
    }
  }
};

/**
 * Serializa o objeto XMLDocument em uma string XML.
 *
 * @param {object} objDocument objeto XMLDocument
 * @return XML string
 * @type string
 */
nhJax.Transport.serializeXmlDoc = function(objDocument) {
  if (window.XMLSerializer) {
    // Mozilla
    return (new XMLSerializer).serializeToString(objDocument);
  }
  if (objDocument.xml) {
    // IE
    return objDocument.xml;
  }
};

/**
 * Adiciona e processa o objeto JSON de uma URL espeificada.
 *
 * <pre>
 * Quando o objeto JSON a adicionado e processado, uma das funacoes de retorno a 
 * chamada: onLoad quando sucesso ou onError quando erro. Em modo sincrono afuncao de 
 * retorno onLoad pode ser omitida. Ao invas utiliza o objeto retornado.
 *
 * A funcao de retorno onLoad recebe o objeto JSON como argumento.
 *
 * a funcao de retorno onError recebe o seguinte objeto:
 * {
 *   errorCode: codigo do erro [number],
 *   errorDescription: descricao do erro [string]
 * }
 * O codigo de erro sera 0 a nao ser que ocorra um problema durante o processamento.
 *
 * Nota: Alguns browsers fazem cache de requisiacoes GET. O cacheamento pode ser previnido
 * adicionando o parametro 'r=' + Math.random() a URL.
 *
 * Se voce usa o matodo POST, o argumento de conteudo deve ser algo parecido com
 * 'var1=valor1&var2=valor'. Se voce deseja enviar outro tipo de conteaudo, deve definir
 * o contentType apropriado. Ex. para enviar uma string XML, voce deve definir o contentType: 'text/xml'.
 *
 * Se a resposta do servidor tiver caracteres nao-ASCII, o servidor deve enviar
 * o cabeacalho content-type apropriado. Ex.
 * "Content-type: text/plain; charset=utf-8" ou
 * "Content-type: text/plain; charset=windows-1251".
 *
 * Formato do Objeto de Argumento:
 * {
 *   url: [string] relativa ou absoluta URL a ser adicionada,
 *   reliable: [boolean, optional] false (String sera processada) or true
 *   (evaluated) (default: false),
 *   method: [string, optional] matodo ('GET', 'POST', 'HEAD', 'PUT'),
 *   async: [boolean, optional] use modo assincrono (default: true),
 *   contentType: [string, optional] tipo de conteaudo quando usa POST,
 *   content: [string or object, optional] string a ser enviada ou o objeto DOM
 *    quando usa POST,
 *   onLoad: [function, optional] funcao de retorno quando sucesso,
 *   onError: [function, optional] funcao de retorno quando erro,
 *   username: [string, optional] nome do usuario,
 *   password: [string, optional] senha,
 *   busyShadown	: [boolean] habilita efeito shadown,
 *   busyImage			: [string] define a imagem de loading,
 *	 busyCloseOnClick   : [string] habilita o click close quando houver click fora da area de shadow,
 *   busyWaitRedirect: [boolean] true aguarda o redirect mantendo o efeito de carregando
 * }
 * </pre>
 *
 * @param {object} objArgs Objeto Argumento
 * @return Em moso sincrono objeto JSON ou null. Em modo assincrono sempre null.
 * @type object
 */
nhJax.Transport.fetchJsonObj = function(objArgs) {
  // Check arguments
  if (objArgs == null || typeof objArgs != 'object') {
    return null;
  }
  if (!objArgs.url) {
    return null;
  }
  if (typeof objArgs.async == 'undefined') {
    objArgs.async = true;
  }
  if (!objArgs.reliable) {
    objArgs.reliable = false;
  }
  // Form argument for fetch
  var objFetchArgs = {};
  for (var strKey in objArgs) {
    objFetchArgs[strKey] = objArgs[strKey];
  }
  // Prevent duplicate parseXml call in synchronous mode
  if (objArgs.async) {
    objFetchArgs.onLoad = function(objRequest) {
          nhJax.Transport.parseJson({
	        strJson: objRequest.responseText,
	        reliable: objArgs.reliable,
	        onLoad: objArgs.onLoad,
	        returnHTML:	objArgs.returnHTML,
	        onError: objArgs.onError
	      });
    };
  } else {
    objFetchArgs.onLoad = null;
  }
  // Fetch URL
  var objRequest = nhJax.Transport.fetch(objFetchArgs);
  // In synchronous mode the result is ready on the next line
  if (!objArgs.async && objRequest) {
    return nhJax.Transport.parseJson({
      strJson: objRequest.responseText,
      reliable: objArgs.reliable,
      onLoad: objArgs.onLoad,
      returnHTML:	objArgs.returnHTML,
      onError: objArgs.onError
    });
  }
  return null;
};

/**
 * Processa a string JSON em um objeto.
 *
 * <pre>
 * Quando a string JSON a processada, uma das funacoes de retorno a chamada:
 * onLoad quando sucesso ou onError quando erro.
 *
 * A funcao de retorno onLoad recebe o objeto JSON como argumento.
 *
 * a funcao de retorno onError recebe o seguinte objeto:
 * {
 *   errorCode: codigo do erro [number],
 *   errorDescription: descricao do erro [string]
 * }
 * O codigo do erro sempre sera 0.
 *
 * Retorna o objeto JSON, assim a funcao de retorno onLoad a opcional.
 * O valor retornado deve ser verificado, pois este pode ser igual a null.
 *
 * Formato do Objeto de Argumento:
 * {
 *   strJson: JSON a string a ser processada [string],
 *   reliable: false (a string sera processada) ou true (evaluated) [boolean]
 *   (optional, false by default),
 *   onLoad: funcao de retorno quando sucesso [function] (optional),
 *   onError: funcao de retorno quando erro [function] (optional)
 * }
 * </pre>
 *
 * @param {object} objArgs objeto Argumento
 * @return JSON object ou null
 * @type object
 */
nhJax.Transport.parseJson = function(objArgs) {
  if (objArgs == null || typeof objArgs != 'object') {
    return null;
  }
  if (!objArgs.strJson) {
    return null;
  }
  if (!objArgs.reliable) {
    objArgs.reliable = false;
  }
  if (!objArgs.onLoad) {
    objArgs.onLoad = null;
  }
  if (!objArgs.onError) {
    objArgs.onError = null;
  }
  var objJson = null;
  try {
  	if (objArgs.reliable) {
      objJson = eval('(' + objArgs.strJson + ')');
    } else {
      if(objArgs.returnHTML){	
      	objJson = objArgs.strJson;
	  }else{
	  	objJson = nhJax.Transport.parseJsonStr(objArgs.strJson);
	  }	
    }
  } catch (objException) {
    nhJax.Transport.displayError(0,
     'Error::parseJson::\nError: Cannot parse.\n' +
     'String does not appear to be a valid JSON fragment: ' +
     objException.message + '\n' + objException.text,
     objArgs.onError);
  };
  if(typeof(objJson.sessionExpired) == 'string' && objJson.sessionExpired != ''){
  	window.location.replace( objJson.location ); 
  }
  if (typeof objArgs.onLoad == 'function') {
    objArgs.onLoad(objJson);
  }
  return objJson;
};

/**
 * Processo a string JSON em um objeto.
 *
 * <pre>
 * Foi retirado, com mudanacas, do http://json.org/json.js.
 *
 * Devolve exeacoes se ocorrerem erros de processamento.
 *
 * O formato do JSON esta descrito em http://json.org/js.html.
 * </pre>
 *
 * @private
 * @param {string} text string JSON a ser processada
 * @return JSON object
 * @type object
 */
nhJax.Transport.parseJsonStr = function(text) {
  var p = /^\s*(([,:{}\[\]])|"(\\.|[^\x00-\x1f"\\])*"|-?\d+(\.\d*)?([eE][+-]?\d+)?|true|false|null)\s*/,
      token,
      operator;
  function error(m, t) {
      throw {
          name: 'JSONError',
          message: m,
          text: t || operator || token
      };
  }
  function next(b) {
      if (b && b != operator) {
          error("Expected '" + b + "'");
      }
      if (text) {
          var t = p.exec(text);
          if (t) {
              if (t[2]) {
                  token = null;
                  operator = t[2];
              } else {
                  operator = null;
                  try {
                      token = eval(t[1]);
                  } catch (e) {
                      error("Bad token", t[1]);
                  }
              }
              text = text.substring(t[0].length);
          } else {
              error("Unrecognized token", text);
          }
      } else {
          // undefined changed to null because it is not supported in IE 5.0
          token = operator = null;
      }
  }
  function val() {
      var k, o;
      switch (operator) {
      case '{':
          next('{');
          o = {};
          if (operator != '}') {
              for (;;) {
                  if (operator || typeof token != 'string') {
                      error("Missing key");
                  }
                  k = token;
                  next();
                  next(':');
                  o[k] = val();
                  if (operator != ',') {
                      break;
                  }
                  next(',');
              }
          }
          next('}');
          return o;
      case '[':
          next('[');
          o = [];
          if (operator != ']') {
              for (;;) {
                  o.push(val());
                  if (operator != ',') {
                      break;
                  }
                  next(',');
              }
          }
          next(']');
          return o;
      default:
          if (operator !== null) {
              error("Missing value");
          }
          k = token;
          next();
          return k;
      }
  }
  next();
  return val();
};

/**
 * Serializa o objeto JSON em uma string JSON.
 *
 * Foi retirado, com mudanacas, de http://json.org/json.js.
 *
 * @param {object} v objeto JSON
 * @return JSON string
 * @type string
 */
nhJax.Transport.serializeJsonObj = function(v) {
  var a = [];
  /*
    Emit a string.
  */
  function e(s) {
      a[a.length] = s;
  }
  /*
    Convert a value.
  */
  function g(x) {
      var c, i, l, v;
      switch (typeof x) {
      case 'object':
          if (x) {
              if (x instanceof Array) {
                  e('[');
                  l = a.length;
                  for (i = 0; i < x.length; i += 1) {
                      v = x[i];
                      if (typeof v != 'undefined' &&
                              typeof v != 'function') {
                          if (l < a.length) {
                              e(',');
                          }
                          g(v);
                      }
                  }
                  e(']');
                  return;
              } else if (typeof x.toString != 'undefined') {
                  e('{');
                  l = a.length;
                  for (i in x) {
                      v = x[i];
                      if (x.hasOwnProperty(i) &&
                              typeof v != 'undefined' &&
                              typeof v != 'function') {
                          if (l < a.length) {
                              e(',');
                          }
                          g(i);
                          e(':');
                          g(v);
                      }
                  }
                  return e('}');
              }
          }
          e('null');
          return;
      case 'number':
          e(isFinite(x) ? +x : 'null');
          return;
      case 'string':
          l = x.length;
          e('"');
          for (i = 0; i < l; i += 1) {
              c = x.charAt(i);
              if (c >= ' ') {
                  if (c == '\\' || c == '"') {
                      e('\\');
                  }
                  e(c);
              } else {
                  switch (c) {
                      case '\b':
                          e('\\b');
                          break;
                      case '\f':
                          e('\\f');
                          break;
                      case '\n':
                          e('\\n');
                          break;
                      case '\r':
                          e('\\r');
                          break;
                      case '\t':
                          e('\\t');
                          break;
                      default:
                          c = c.charCodeAt();
                          e('\\u00' + Math.floor(c / 16).toString(16) +
                              (c % 16).toString(16));
                  }
              }
          }
          e('"');
          return;
      case 'boolean':
          e(String(x));
          return;
      default:
          e('null');
          return;
      }
  }
  g(v);
  return a.join('');
};

/**
 * Exibe mensagem de erro.
 *
 * <pre>
 * Chamada a funcao de retorno onError informada pelo usuario. Se nao existe uma funcao
 * de retorno onError, entao exibe um alert com uma descricao do erro.
 * Funcao de retorno onError recebe o seguinte objeto:
 * {
 *   errorCode: codigo do erro [number],
 *   errorDescription: descricao do erro [string]
 * }
 * </pre>
 *
 * @private
 * @param {number} iErrCode Codigo do erro
 * @param {string} strError Descricao do erro
 * @param {function} onError Funcao de retorno informada pelo usuario
 */
nhJax.Transport.displayError = function(iErrCode, strError, onError) {
  if (typeof onError == 'function') {
    onError({
      errorCode: iErrCode,
      errorDescription: strError
    });
  } else {
    alert(strError);
  }
};

/**
 * Traduz uma URL a sua URL relativa or para uma URL absoluta.
 *
 * <pre>
 * Formato do objeto de argumento:
 * {
 *   url: URL absluta ou relativa a ser traduzida [string] (se absoluta, sera 
 *   retornada com a),
 *   relativeTo: "url" sera traduzida a URL relativa a sua URL absoluta ou relativa
 *    [string] (optional, current page URL by default)
 * }
 * </pre>
 *
 * @param {object} objArgs Objeto Argumento
 * @return Translated URL
 * @type string
 */
nhJax.Transport.translateUrl = function(objArgs) {
  if (!objArgs || !objArgs.url) {
    return null;
  }
  // Cut arguments part
  var arrFullUrl = objArgs.url.split('?', 2);
  var strUrl = arrFullUrl[0];
  // Check if it is absolute
  if (strUrl.charAt(0) == '/' || strUrl.indexOf(':') >= 0) {
    return objArgs.url;
  }
  // Make relative to current page URL by default
  if (!objArgs.relativeTo) {
    objArgs.relativeTo = document.location.toString();
  }
  // Remove arguments
  objArgs.relativeTo = objArgs.relativeTo.split("?", 2)[0];
  // Split URLs
  var arrUrl = strUrl.split('/');
  var arrRelativeTo = objArgs.relativeTo.split('/');
  // Remove file name
  arrRelativeTo.pop();
  // Form new URL
  for (var iToken = 0; iToken < arrUrl.length; iToken++) {
    var strToken = arrUrl[iToken];
    if (strToken == '..') {
      arrRelativeTo.pop();
    } else if (strToken != '.') {
      arrRelativeTo.push(strToken);
    }
  }
  arrFullUrl[0] = arrRelativeTo.join('/');
  // Restore arguments part
  return arrFullUrl.join('?');
};