// Copyright (c) Willett Consulting Limited 2010

Namespace.Register("WCL");

WCL.AJAX =
{
  MakeRequest: function(url, callback, data)
  {
    WCL.Debug.Log("Making AJAX request for " + url);
    if(window.XMLHttpRequest)
    {
      xhr = new XMLHttpRequest;
    }
    else if(window.ActiveXObject)
    {
      try
      {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
      }
    }

    if(!xhr)
    {
      WCL.Debug.Log("Failed to create XMLHttpRequest " + url + " with callback " + callback);
      return false;
    }

    // make the request asynchronously (do the open first as it stops caching)
    if(data == null)
    {
      xhr.open("GET", url, true);
    }
    else
    {
      xhr.open("POST", url, true);
    }
    xhr.onreadystatechange = function()
    {
      // 4 == download complete
      if(xhr.readyState == 4)
      {
        if(xhr.status != 200)
        {
          WCL.Debug.Log("Request for " + url + " failed with code " + xhr.status);
          return;
        }

        // check if the url contains .html/
        if(/\.html/.test(url) == true)
        {
          callback(xhr.responseText)
        }
        else
        {
          callback(xhr.responseXML);
        }

        // cleanup the object
        delete xhr;
        xhr = null;
        WCL.Debug.Log("Completed AJAX request for " + url);
      }
    }

    // don't cache
    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.setRequestHeader("Cache-Control", "must-revalidate");
    xhr.setRequestHeader("Cache-Control", "no-store");
    xhr.setRequestHeader("Pragma", "no-cache");
    xhr.setRequestHeader("Expires", "0");
    xhr.send(data);
    return true;
  },

  // variable to store the content div
  ContentDiv: null,
  ScriptFindRE: new RegExp(/<script[^>]*?>[\s\S]*?<\/script>/gi),
  ScriptRemoveRE: new RegExp(/<\/?script[^>]*?>/gi),
  UpdateContentDiv: function(url)
  {
    WCL.AJAX.MakeRequest(url, WCL.AJAX.UpdateContentDivCallback, null);
  },
  UpdateContentDivCallback: function(content)
  {
    if(WCL.AJAX.ContentDiv == null)
    {
      WCL.AJAX.ContentDiv = document.getElementById("contents");
    }

    if(WCL.AJAX.ContentDiv)
    {
      WCL.AJAX.ContentDiv.innerHTML = content;
      setTimeout(WCL.General.findPopups, 0);
      var scripts = content.match(WCL.AJAX.ScriptFindRE);
      if(scripts != null)
      {
        for(i = 0;i < scripts.length;++i)
        {
          eval(scripts[i].replace(WCL.AJAX.ScriptRemoveRE, ''));
        }
      }
    }
  },

  UpdateRightContentDiv: function(url)
  {
    WCL.AJAX.MakeRequest(url, WCL.AJAX.UpdateRightContentDivCallback, null);
  },
  UpdateRightContentDivCallback: function(content)
  {
    rightContentDiv = document.getElementById("rightContent");

    if(rightContentDiv)
    {
      rightContentDiv.innerHTML = content;
      setTimeout(WCL.General.findPopups, 0);
      var scripts = rightContentDiv.getElementsByTagName('script');
      for(var i = 0;i < scripts.length;++i)
      {
        eval(scripts[i].text);
      }
    }
  }
}

