// ajax.js by Patrick Benny
// Copyright 2007 All rights reserved


// For "Home" page (/index.php) "Random selection"

var g_IndexRandom_Loader = null;
var g_Ajax_bMoreReady = true;
function f_Ajax_IndexRandom_Fetch () {
  if (!document.getElementById)
    return false;
  // Disable "More" button
  g_Ajax_bMoreReady = false;
  // Make room to avoid scrolling
  document.getElementById("footer").style.paddingBottom = "5em";
  // Replace displayed content with buffer
  document.getElementById("random_inner").innerHTML =
     document.getElementById("random_inner_buffer").innerHTML;
  // Preload next page
  var sRootRel = location.href;
  sRootRel = sRootRel.substring(0,sRootRel.lastIndexOf(".com/")+5);
  g_IndexRandom_Loader =
     new net.ContentLoader(
        sRootRel+"index.random.php?"+(Math.random()*65535).toString(16),
        f_Ajax_IndexRandom_Parse,
        f_Ajax_IndexRandom_Fallback);
}
function f_Ajax_IndexRandom_Parse () {
  // Copy response data to buffer
  var e;
  if (  document.getElementById
     && (e = document.getElementById("random_inner_buffer")))
    e.innerHTML = this.req.responseText;
  // Enable "More" button
  g_Ajax_bMoreReady = true;
}
function f_Ajax_IndexRandom_Fallback () {
  location.hash = "random";
  location.reload();
}
function f_Ajax_IndexRandom_More () {
  if (g_Ajax_bMoreReady)
    f_Ajax_IndexRandom_Fetch();
}


// Below from Ajax In Action p.259
// by Dave Crane, Eric Pascarello, Darren James

var net = new Object();
net.ContentLoader=function(
  url,onload,onerror,method,params,contentType,headers,secure
){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.secure=secure;
  this.loadXMLDoc(url,method,params,contentType,headers);
}

net.ContentLoader.prototype={
  loadXMLDoc:function(url,method,params,contentType,headers){
    if (!method)
      method="GET";
    if (!contentType && method=="POST")
      contentType='application/x-www-form-urlencoded';
    if (window.XMLHttpRequest)
      this.req=new XMLHttpRequest();
    else
      this.req=new ActiveXObject("Microsoft.XMLHTTP");
    if (this.req){
      try{
        try{
          if (  this.secure && netscape
             && netscape.security.PrivilegeManager.enablePrivilege)
            netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
        }catch (err){}
        this.req.open(method,url,true);
        if (contentType)
          this.req.setRequestHeader('Content-Type', contentType);
        if (headers)
          for (var h in headers)
            this.req.setRequestHeader(h,headers[h]);
        var loader=this;
        this.req.onreadystatechange=function(){
          loader.onReadyState.call(loader);
        }
        this.req.send(params);
      }catch (err){
        this.onerror.call(this);
      }
    }
    else
      this.onerror.call(this);
  },

  onReadyState:function(){
    var req=this.req;
    var ready=req.readyState;
    if (ready==4){  // req.READY_STATE_COMPLETE
      var httpStatus=req.status;
      if (httpStatus==200 || httpStatus==0) {
        try{
          if (  this.secure && netscape
             && netscape.security.PrivilegeManager.enablePrivilege)
            netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
        }catch (err){}
        this.onload.call(this);
      }else{
        this.onerror.call(this);
      }
    }
  },

  defaultError:function(){
    alert(
       "error fetching data!\n"+
       "\nreadyState: "+this.req.readyState+
       "\nstatus: "+this.req.status+
       "\nheaders: "+this.req.getAllResponseHeaders());
  }
}