url = 'AJAXHandler.php';

function makeHTTPRequest(HTTPRequest, GETData, POSTData, curUrl)
{
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    HTTPRequest = new XMLHttpRequest();
    
    if (HTTPRequest.overrideMimeType) {
      HTTPRequest.overrideMimeType('text/plain');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      HTTPRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        HTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert('AJAX error 1.');
      }
    }
  }
  
  if (!HTTPRequest) {
    alert('AJAX error 2.');
    return false;
  }
  
  var randInt = parseInt(Math.random() * 99999999);
  
  HTTPRequest.open('POST', ((curUrl == undefined) ? url : curUrl) + '?' + GETData + '&rand=' + randInt, true);
  HTTPRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  HTTPRequest.send(POSTData);
  
  return HTTPRequest;
}

function makeHTTPRequestWait(GETData, POSTData, curUrl)
{

  var HTTPRequest;

  if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
    HTTPRequest = new XMLHttpRequest();
		if (HTTPRequest.overrideMimeType) 
		{ 
			HTTPRequest.overrideMimeType('text/xml');
		} 
	}
	else if (window.ActiveXObject) 
	{ 
    try {
      HTTPRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } 
		catch (e) {
      try {
        HTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        
      }
    }
  }

  if (!HTTPRequest) {
    // !!!
    return false;
  }
 
  HTTPRequest.open('POST', ((curUrl == undefined) ? url : curUrl) + '?' + GETData, false);
  HTTPRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  HTTPRequest.send(POSTData);
  
  return HTTPRequest;
}

function resultString(responseText)
{
  this.string = responseText;
  this.pos = 0;
}

resultString.prototype.scanInt = function()
{
  var ret = parseInt(this.string.substring(this.pos));
  while(isInt(this.string.charAt(this.pos))) this.pos++;
  this.pos++;
  return ret;
  
  function isInt(chr)
  {
    if((chr >= '0') && (chr <= '9')) return 1
    return 0
  }
}

resultString.prototype.scanStr = function()
{
  var len = this.scanInt();
  if(len == 0) return "";
  var ret = this.string.substring(this.pos, this.pos + len);
  this.pos += len;
  return ret;
}

