<!--
// file: ajax.js
// author: philip pang
// date: 15 May 2007

// http://www.phpit.net/article/ajax-php-without-xmlhttprequest/
function ajaxCall (url) {

  // add dynamic elements to url
  url = ajaxAddGet2Url (url,"zzz",(new Date()).getTime());

  // Create new JS element
  var jsElement = document.createElement('SCRIPT');
  jsElement.type = 'text/javascript';
  jsElement.src = url;
  
  // Append JS element (therefore executing the 'AJAX' call)
  document.body.appendChild (jsElement);
}


function ajaxAddGet2Url (purl,name,value) {
  var sep = "&";
  if (purl.indexOf("?")<0) sep = "?";
  purl += sep + name + "=" + escape(value);
  return purl;  
}

// post data synchronously
function ajaxPostData (url,data) {

  var xmlhttp = false;

  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }

  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  // use synchronous method to post data
  xmlhttp.open ("POST", url, false);

  // request header
  xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

  // data in the body of the request needs to be urlencoded!
  data = escape (data);

  // javascript does not automatically urlencode "+" character!
  // which php requires
  data = data.replace (/[+]/g,"%2B");

  // post now! - data is in the body of the request
  xmlhttp.send ("data="+data);

  // get http response from ajax server
  var strResult = xmlhttp.responseText;
  return strResult;
}

// callback function expected to have the following signature
// function mycallback (txt,cbdata)
// ASYNC ajax call!
function ajaxGetFile (urlFile, cbfunc, cbdata) {

  var xmlhttp = false;

  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }

  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  // use asynchronous method to get file
  xmlhttp.open ("GET", urlFile, true);

  xmlhttp.onreadystatechange=function() {
    //alert ("xmlhttp ready state is " + xmlhttp.readyState);
    if (xmlhttp.readyState==4) {
      //alert ("xmlhttp status is " + xmlhttp.status);
      var txt = xmlhttp.responseText;
      var cmd = cbfunc + "(txt";
      if (cbdata != null) cmd += ",'" + cbdata + "'";             
      cmd += ");";      
      eval (cmd);
    }
  }

  // make true ajax call!
  xmlhttp.send (null);
  
  //alert ("xmlhttp status is " + xmlhttp.status);
  //return xmlhttp.responseText;
}


// synchronous ajax, works only when compiled to exe
function ajaxGetFileSync (urlFile) {

  var xmlhttp = false;

  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }

  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  // use synchronous method to get file
  xmlhttp.open ("GET", urlFile, false);


  // make true ajax call!
  xmlhttp.send (null);
  
  // return file data
  if (xmlhttp.status==200) return xmlhttp.responseText;
  
  // return null string
  return "";
}


var ajaxFileData = null;

function ajaxGetFileData () {
  return ajaxFileData;	
}

function ajaxGetBinaryFile (urlFile, cbfunc) {

  var xmlhttp = false;

  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }

  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  // create http Request message
  xmlhttp.open ("GET", urlFile, false);

  // clear file data variable
  ajaxFileData = null;

  // set up async function to grab file data
  xmlhttp.onreadystatechange=function() {

    if (xmlhttp.readyState==4 && xmlhttp.status==0) {
      alert ("xmlhttp internal callback!");
      ajaxFileData = xmlhttp.responseBody; // this is a multibyte string! NOT unicode!
      var cmd = cbfunc + "();";
      eval (cmd);
    }
  }

  // make true ajax call!
  xmlhttp.send (null);	
}

function ajaxHello () {
  alert ("ajaxHello!");
}

// this var must be global for ajaxEvalJS to work in Mozilla
// technique for Mozilla is to stuff JS codes into ajaxEvalJScode
// global var and to call setTimeout on the VARIABLE DIRECTLY!
// setTimeout (ajaxEvalJScode,0);
var ajaxEvalJScode = "";

// this DOM method works only for Mozilla
// for IE, use window.execScript()
function ajaxJScodeAppend (txt) {

  // Create new JS element
  var jsElem = document.createElement('SCRIPT');
  jsElem.type = 'text/javascript';

  // add text node to JS element
  //  only for NON-IE browsers
  if (!document.all) {
    var txtNode = document.createTextNode (txt); 
    jsElem.appendChild (txtNode);
  }
  
  // Append JS element (therefore executing the 'AJAX' call)
  document.body.appendChild (jsElem);
}


// cbfunc header is function my_func()
// cbfunc arg is "my_func()" when calling
// this function will execute code in global scope in IE
// whereas eval() will NOT!
function ajaxEvalJS (code,cbfunc) {
  code += ";" + cbfunc + ";";
  ajaxEvalJScode = code;
  // IE only uses the execScript method
  if (window.execScript) {
    window.execScript (code);
    return;
  } 
  // eval cannot be used for Mozilla. Must use setTimeout call
  // with direct codes stored in a local global variable!
  // viz setTimeout (ajaxEvalJScode,0);
  // otherwise use DOM script element append method 
  ajaxJScodeAppend (code); 
}

//-->
