//----------------------------------------------------------------
// Copyright© 2002-2008 New Vintage Software Inc.
//----------------------------------------------------------------

function LL_AJAX_CLIENT(IsDebug) {

  this.DebugMode = 0;
  var BaseURL = location.protocol + "//" + location.hostname + "/"  
  
  if (location.hostname == "nvs") BaseURL = BaseURL + "webs/"
  
  var BaseInclude = BaseURL + "settings/java/ajax/"    
  var LastURL = "";

  var LL_Includes = Array("LLPopup.html");  
  for (var iii in LL_Includes) document.write("<SCRIPT type=\"text/javascript\" src=\"" + BaseInclude + LL_Includes[iii] + "\"></SCRIPT>");
  



 
  var MaxRequests = 3;
  var MaxTimeout = 5000;


  var ActiveRequests = 0;
  var RequestedViews = Array(0);
  this.Views = Array(0);  
  
  this.QueryString = new LoadQueryString()



  this.Request = function(URL,Callback_Function,Timeout_Function) {     
    if (RequestedViews[URL]) return
    if (ActiveRequests > MaxRequests || URL < 1) return     
    RequestedViews[URL] = true
    ActiveRequests++;
    
    LastURL = URL;  
    var FullURL = BaseURL + URL;
    if (this.DebugMode) alert("Posting URL: " + FullURL);  

    var Request = GetAJAXConnection();                          
    var Timeout = setTimeout(function() {LLAPP.TimeoutCallBack(Request,Timeout_Function,LastURL); },MaxTimeout) 
    Request.open("POST", FullURL, true);                   
    Request.onreadystatechange = function() {LLAPP.ReceivedCallBack(Request,Timeout,Callback_Function,LastURL); };     // pass the request and timer object
    Request.send(""); 
  }
    

  
  this.ReceivedCallBack = function(Request,Timeout,CallBack,URL) {
    if (Request.readyState != 4) return;       
    
   
    
    clearTimeout(Timeout)   

    RequestedViews[URL] = false
    ActiveRequests--;
    if (ActiveRequests<0) ActiveRequests = 0;
        
    if (Request.responseText.length > 0) {        
      if (this.DebugMode) alert("Received: " + Request.responseText.length + " bytes for " + URL);            
      this.Views[URL] = Request.responseText            
      try {
        CallBack();
      } catch(failed) {
        alert("Error: unable to execute callback:" + CallBack);
      }            
    } else {
      if (this.DebugMode) alert("Error: Server returned an empty string");     
    }
  }

  this.TimeoutCallBack = function(request,Timeout){ 
 
    request.abort(); 
    if (this.DebugMode) alert("Timeout Error: no result received from server before timeout.");        
    try {
      Timeout();
    } catch(failed) {
      alert("Error: unable to execute callback:" + Timeout);
    }
  }
  
  
  
  function GetAJAXConnection() {
    var request = false;
    try { request = new XMLHttpRequest();
    } catch (trymicrosoft) {
      try { request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (othermicrosoft) {
        try { request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (failed) {
           request = false;
    }}}         
    return request
  }




  //-----------------------------------------------------------------------------------------------------------
  // FrameBuster           --- called if body has a LL_FrameBuster attribute
  //-----------------------------------------------------------------------------------------------------------
      this.FrameBuster = function() { if (top!=self.parent) top.location=self.parent.location; }


  //-----------------------------------------------------------------------------------------------------------
  // LoadQueryString
  //-----------------------------------------------------------------------------------------------------------
      function LoadQueryString() {
        if (!location.search) return;       
        var qs = (location.search.slice(1)).split("&")
        for (var i in qs) {
          var qsx = qs[i].split("=")
          this[qsx[0]] = qsx[1];              
      }}          



  // this.VirtualizeBody = function(BodyDiv)   {                                      // virtualizes a new div as the page body
  //    window.onload = function() {LLAPP.Initalize();}  //??? still needed?????// 
  //    window.onbeforeunload = function() {LLAPP.beforeUnload();}
  //  }


    
}

 
//----------------------------------------------------------
    var LLAPP = new LL_AJAX_CLIENT();               //  Initalize the AJAX Engine
    
  //  LLAPP.DebugMode = 1;
//----------------------------------------------------------

























