/*

Cross-Platform AJAX Support Library.
Installation:
  <script language="JavaScript" src="ajax.js" type="text/javascript"></script>

Usage:

Replace page without refresh
    ajaxReplacePage('blah.php?a=123');

Replace a ID'd Div section
    ajaxReplaceArea('divname','ajx.php','option=1&u=0')
*/

var xmlHttp;
var myObj=null;

function ajaxGetObject(b) {
   var imgs,i, temp;
   temp =document.getElementById(b);
   if(temp) return temp;
   imgs=document.getElementsByTagName('div');
   for(i in imgs) {
     if(imgs[i].id==b) return imgs[i];
   }
 }

function ajaxReplacePageRecv() {
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
    document.write(xmlHttp.responseText);
  }
}

//Replaces Entire page by AJAX.
function ajaxReplacePage(url) {
  xmlHttp = getHTTPObject();            //Create AJAX Object
  if(xmlHttp==null) {                   //Check if AJAX is supported
    alert ("Ajax not available")
    return
  }
  xmlHttp.onreadystatechange=replacePageRecv;      //State change goes to this function
  xmlHttp.open("GET",url,true);                 //Ready Socket
  xmlHttp.send(null);                           //Send request
}

function stateChanged() {
 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
    myObj.innerHTML = xmlHttp.responseText;
    myObj=null;
  }
 }

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

function ajaxReplaceArea(objname,url,params) {
  if(!myObj) {
//    myObj = document.getElementById(objname);
    myObj = ajaxGetObject(objname);
  } else {
    alert('There is a request currently in progress, please wait.');
  }

  xmlHttp = getHTTPObject();            //Create AJAX Object
  if(xmlHttp==null) {                   //Check if AJAX is supported
    alert ("Ajax not available");
    return;
  }

  if(params) {
    url=url+'?'+params;
    url=url+"&rand_sid="+Math.random();
  } else {
    url=url+"?rand_sid="+Math.random();
  }
  xmlHttp.onreadystatechange=stateChanged;      //State change goes to this function
  xmlHttp.open("GET",url,true);                 //Ready Socket
  xmlHttp.send(null);                           //Send request
  return false;
 }

