function Ajax() {
	this.xmlHttp = this.getXMLHttpRequest();
}

Ajax.prototype.getXMLHttpRequest = function() {
	var xmlHttp = false;
	if(typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
	else {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				xmlHttp = false;
			}
		}
	}
	if(!xmlHttp) {
		alert("Cannot create XMLHTTP instance");
	}
	return xmlHttp;
}

Ajax.prototype.request = function(url, parameters, method) {
	if(typeof parameters == "undefined") {
		parameters = [];
	}

	if(typeof method == "undefined") {
		method = "GET";
	}
	method = method.toUpperCase();
	
	if(!this.xmlHttp) {
		return false;
	}
	
	this.data = "";
	this.error = false;
	
	var parametersStr = "";
	
	for(var x in parameters) {
		if(typeof parameters[x] == "object") {
			for(var y in parameters[x]) {
				addParam(x, parameters[x][y]);
			}
		}
		else {
			addParam(x, parameters[x]);
		}
	}
	if(method == "POST") {
		this.xmlHttp.open("POST", url, true);
		this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.xmlHttp.setRequestHeader("Content-length", parametersStr.length);
		this.xmlHttp.setRequestHeader("Connection", "close");
	} else {
		this.xmlHttp.open("GET", url + ((parametersStr.length && !url.search(/\?/))? "?" : "") + parametersStr, true);
	}
	
	var ajax = this;
	
	this.xmlHttp.onreadystatechange = function() {
		ajax.error = false;
		try
		{
			if(ajax.xmlHttp.status && ajax.xmlHttp.readyState == 4) {
				if(ajax.xmlHttp.status == 200 && ajax.xmlHttp.responseText != "") {
					ajax.data = ajax.xmlHttp.responseText;
					ajax.response();
				} else {
					ajax.error = true;
					try {
						ajax.data = ajax.xmlHttp.statusText;
					} catch(msg) {
						ajax.data = "Fatal error: " + msg;
					}
				}
			}
		} catch(msg) {
			ajax.error = false;
			ajax.data = "";
		}
   }

	if(method == "POST") {
		this.xmlHttp.send(parametersStr);
	} else {
		this.xmlHttp.send(null);
	}
	
	function addParam(name, value) {
		parametersStr += (parametersStr.length? "&" : "") + encodeURIComponent(name) + "=" + encodeURIComponent(value);
	}
}

Ajax.prototype.sendForm = function(form) {
   var parameters = [];

   for(var x = 0; x < form.elements.length; x++) {
      var element = form.elements[x];

      if(element.type == "checkbox" && !element.checked)
         continue;
      if(element.type == "select-one") {
    	  parameters[element.name] = element.value;
      } else {

	      if(typeof parameters[element.name] == "undefined") {
	         parameters[element.name] = [];
	      }
	
	      if(element.value != '')
	      {
	    	  parameters[element.name][x] = element.value;
	      }
      }
   }

   this.request(form.action, parameters, form.method);
}

Ajax.prototype.sendLink = function(a) {
	this.request(a.href);
}

Ajax.prototype.response = function() {}

