
var accessFile = "";
var xmlHttp = null;
var tgtHttp = null;

var callBack = null;

function urlencode(str) {	// do it the same as php
    str = str.replace(/\n/g, '%0A');
    str = str.replace(/\r/g, '%0D');

    str = escape(str);
    str = str.replace(/\+/g, '%2B');
    str = str.replace(/%20/g, '+');
    str = str.replace(/\*/g, '%2A');
    str = str.replace(/\//g, '%2F');
    str = str.replace(/@/g, '%40');

    return str;
}


function urldecode(str) {	// do it the same as php
    str = str.replace('+', ' ');
    str = unescape(str);
    return str;
}

// ------- fetch data and call a function ----------
function waitForIt() {
   if(callBack == null) {
      alert( "no place to put data");
      return;
   }
   if( xmlHttp.readyState == 4 ) {
      if ( xmlHttp.status == 200 ) {
         callBack( xmlHttp.responseText);
      } else {
         var msg;
         msg  = "<p> Error loading file " + accessFile + "</p>";
         msg += "<p> Status code " +  xmlHttp.status + "</p>";
         msg += "<p>" + xmlHttp.statusText + "</p>"
         callBack( msg );
      }
      callBack = null;     // once used, kill it.
   }
}

function getHtml( url, params, callme ) {
   if(getXmlHttp() == false) {
      return "<p> Error getting file " + filename + "</p>";
   }

   callBack = callme;
   accessFile = url;     // for the error message...

   xmlHttp.onreadystatechange = waitForIt;
   xmlHttp.open("POST", url, true);
   xmlHttp.setRequestHeader("Content-Type","text/html");
   xmlHttp.send(params);
}

// --------- fetch data and stuff in div -------------
function checkIt() {
  if( xmlHttp.readyState == 4 ) {
	if ( xmlHttp.status == 200 ) {
	  tgtHttp.innerHTML = xmlHttp.responseText;
	} else {
	  tgtHttp.innerHTML = "<p> Error loading file " + accessFile + "</p><p>" + xmlHttp.statusText + "</p>";
	  tgtHttp.innerHTML += "<p> Status code " +  xmlHttp.status + "</p>";
	  tgtHttp.innerHTML += xmlHttp.responseText;
	}
  }
}

function doPost(target, url, params) {
   if(getXmlHttp() == false) {
	  target.innerHTML = "<p> Error accessing " + url + "</p>";
	  //target.style.display="block";
	  return;
   }

   var parmStr = urlencode(params);
   parmStr = params;
   tgtHttp = target;    // where response goes.
   accessFile = url;

   xmlHttp.onreadystatechange = checkIt;
   xmlHttp.open("POST", url, true);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", parmStr.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(parmStr);
}

function doGet(target, filename) {
   if(getXmlHttp() == false) {
        //target.style.display="block";
	target.innerHTML = "<p> Error getting to file " + filename + "</p>";
	return;
   }

   tgtHttp = target;    // where response goes.
   accessFile = filename;

   xmlHttp.onreadystatechange = checkIt;
   xmlHttp.open("GET", filename, true);
   xmlHttp.setRequestHeader("Content-Type", "text/html");
   xmlHttp.send(null);
}

function access(target, filename) { doGet(target, filename); }
function access2(target, filename) {
	alert(target );
}

// -------- fetch page and open new window -----------
function doGet_new(target, url) {
   window.location = url;
}

// -------- establish xmlHttp object -----------
function getXmlHttp() {
   //if(xmlHttp != null)  xmlHttp = null;
	   //return true;

  try {
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	} catch (e) {
	// Internet Explorer
	try {
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
	  try {
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			alert("Sorry, your browser does not support AJAX");
			return false;
		}
	  }
	}
	return true;
 }

