var br;
if(typeof br == 'undefined') {
	br = {};
}
if(typeof br.com == 'undefined') {
	br.com = {};
}
br.com.l2software = {
			api: {
				core: {
					CoreProxy: function(url) {
						this.url = url;
					}
				}
			}
		};

br.com.l2software.api.core.CoreProxy.prototype.isActive = function() {
        if(!this._isActive) {              
           try {
             this._isActive = this.remoteCall('PING', {}) == 'PONG';
           } catch (e) {           
             this._isActive = false;
           }        
        }
	return this._isActive;
}


br.com.l2software.api.core.CoreProxy.prototype.newHTTPObject = function(name, values) { 
  if (typeof XMLHttpRequest != 'undefined') { 
    return new XMLHttpRequest(); 
  } 
  try { 
    return new ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (e) { 
    try { 
      return new ActiveXObject("Microsoft.XMLHTTP"); 
    } catch (e) {} 
  } 
  return false; 
}

br.com.l2software.api.core.CoreProxy.prototype.remoteCall = function(name, values) {
	var http = this.newHTTPObject();
	http.open('POST', this.url + name, false);
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var pEncoded = '';
	for(key in values) {
                var value = values[key];                     
                if(value == null) {
                  //value = '';
                  continue;                
                } else {
                  value = encodeURIComponent(value)                
                }
		pEncoded += key.replace(/%20/g, '+') + '=' + value + '&';
	}

        try {
   	  http.send(pEncoded);
	  var content = http.responseText;
          if(http.status > 299) throw 'ERRO: ' + content;
  	  return content;
        } catch (e) {
          throw 'ERRO: ' + e;        
        }
}


br.com.l2software.api.core.CoreProxy.prototype.remoteCallAsync = function(name, values, flowName, callId) {
	var http = this.newHTTPObject();
	http.open('POST', this.url + name, true);
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var pEncoded = '';
	for(key in values) {
                var value = values[key];                     
                if(value == null) {
                  //value = '';
                  continue;                
                } else {
                  value = encodeURIComponent(value)                
                }
		pEncoded += key.replace(/%20/g, '+') + '=' + value + '&';
	}
        http.onreadystatechange = function() {               
            if (this.readyState == 4) {                      
               if(this.status == 200) {                                  
                 var imgData = eval('(' + this.responseText + ')');                 
                 imgData = (new Map()).getInstance(imgData);
                 ebfFlowExecute(flowName, [callId, imgData]);                   
               } else {
                 ebfFlowExecute(flowName, [callId, {"sucesso": false, "desc": "Erro na resposta do servidor."}]);                
               }
            }
        };
        try {
   	  http.send(pEncoded);
	  var content = http.responseText;
          if(http.status > 299) throw 'ERRO: ' + content;
  	  return content;
        } catch (e) {
          throw 'Falha na comunicação com o Agente.';        
        }
}