var PROCESS_COMPLETE = 4;
var STATUS_OK = 200;

// Cross-Browser support
function getHTTPObject() {
  if (window.XMLHttpRequest) {
    // put overrideMimeType code here.
    return new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
     return new ActiveXObject("Microsoft.XMLHTTP");
  }
}

var HTTP = getHTTPObject();

var CallbackMap = new Array();

// Counting mutex which will tell us whether or
// not all Ajax requests have returned.
var RemainingCallbacks = 0;

// Constructor for AjaxRequest object
function AjaxRequest() {
  this.Params = new Array(); //Associative Array
  this.CallBackMethod; 
  this.Service;
  this.Method;
  this.URL;
}

AjaxRequest.prototype.toString = function() {
  return "AjaxRequest";
}

AjaxRequest.prototype.GlobalCallBack = function() {
  //prompt(HTTP.readyState,HTTP.status);
  if (HTTP.readyState == PROCESS_COMPLETE && (HTTP.status) && HTTP.status == STATUS_OK) {
    RemainingCallbacks--;
    var reg = new RegExp(/\[Service\](.+)\[\/Service\]\[value\](.+)\[\/value\]/);
    if(reg.toString() == "//\\[Service\\](.+)\\[\\/Service\\]\\[value\\](.+)\\[\\/value\\]//")
    {
      var tempReg = "\\[Service\\](.+)\\[\\/Service\\]\\[value\\](.+)\\[\\/value\\]";
      reg = new RegExp(tempReg);
    }
    var response = HTTP.responseText;
    response = response.replace(/\&lt\;/g,"<");
    response = response.replace(/\&gt\;/g,">");
    response = response.replace(/\&amp\;/g,"&");
    var result = reg.exec(response);
    if (result == null || result.length < 3) {
      //log.add("AJAX callback error processing response " + response.text);
      return;
    }
    
    var service = result[1]; // gets service
    var val = result[2].replace(/\&lt\;/g,"<");
    val = val.replace(/\&gt\;/g,">");
    
    //prompt('',val);
    this.CallBackMethod = CallbackMap[service];
    return this.CallBackMethod(val);
  }
  else if (HTTP.readyState == PROCESS_COMPLETE && (HTTP.status) && HTTP.status == "500")
  {
    RemainingCallbacks--;
    alert(HTTP.responseText);
  }
}


// Invoke this once you set up properties
// Passing in false as a param makes a synchronous request
AjaxRequest.prototype.send = function(synch) {
  
  this.URL = this.Service + ".asmx/" + this.Method;
  
  if(synch == null || synch == "undefined" || synch == true)
  {
    HTTP.open("POST", this.URL, true);
    
    HTTP.onreadystatechange = this.GlobalCallBack;//this.CallBackMethod;//;
    synch = true;
  }
  else
  {
    HTTP.open("POST", this.URL, false);
  }
  
  HTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  
  CallbackMap[this.Method] = this.CallBackMethod;
    
  var sendStr = "";
  for (key in this.Params) {
    sendStr += key + "=" + this.Params[key] + "&";
  }
  sendStr += "asynch=" + synch;
  RemainingCallbacks++;
  HTTP.send(sendStr);
  
  if(synch != true)
  {
    return this.GlobalCallBack();
  }
}

// This is how it works
function ExampleUsage(codeTable, code) {
   var ajax = new AjaxRequest();
	 ajax.Service = "CodeTableService";
	 ajax.Method = "GetCodeDesc";
	 ajax.CallBackMethod = ExampleCallback;
   ajax.Params["CodeTableName"] = codeTable;
   ajax.Params["Code"] = code;
   ajax.send();
}

// This is how you fish out information
function ExampleCallback(response) {
  var textArea = document.getElementById("ajaxResults");
  textArea.value = response;
}


//function to parse up an ajax list and place it into a drop down list
//format is controlname|-|option|-|value|-|option|-|value....
function GetNewList(response) {
  var splitResponse = response.split('|-|');
  var x = 0,y = 0;
  
  var listControl = getControl(splitResponse[0]);
  x++;

  for (var i = listControl.options.length; i >= 0; i--){
    listControl.options[i] = null;
  }
 
 listControl.selectedIndex = -1;


  for(x;x<splitResponse.length;x=x+2)
  {
     listControl.options[y] = new Option(splitResponse[x], splitResponse[x+1]);
     y++;
  }

}
