// JavaScript Document
// Copyright 2008 Ladislav Brychta

function ajaxKit(debug) {

  this.xmlhttp = null;
  this.debug = debug;

  this.reset = function() {
		this.requestMethod = "GET";
		this.requestFile = "";
		this.URLString = "";
    this.element = null;
    this.elementObj = null;
    this.responseStatus = new Array(2);
	  this.onLoading = function() { if(this.debug) alert('AJAX :: Request sent'); }; // Function to execute when response status is loading(1)
		this.onLoaded = function() { if(this.debug) alert('AJAX :: Response loaded'); }; // Function to execute when response status is loaded(2)
		this.onInteractive = function() { if(this.debug) alert('AJAX :: Response prepared'); }; // Function to execute when response status is interactive(3)
		this.onCompletion = function() { if(this.debug) alert('AJAX :: Request proccessed');	}; // Function to execute when response status is completed(4)
		this.onError = function() { if(this.debug) alert('AJAX :: Connection Error!'); }; // Function to execute when response status is loaded(2)
		this.onFail = function() { if(this.debug) alert('AJAX :: Init Failed'); }; // Function to execute when ajax initialization failed
  };

	this.init = function() {
	  this.reset();
    if(window.XMLHttpRequest) {
      this.xmlhttp = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
      this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (!this.xmlhttp) {
      return false;
    }
		return true;
	};
  
  this.runAJAXRequest = function(url) {
    this.URLString = url;
		if (this.element) {
			this.elementObj = document.getElementById(this.element);
		}
		if (this.requestMethod == "GET") {
			var fullurl = this.requestFile+"?"+this.URLString;
			this.xmlhttp.open(this.requestMethod,fullurl,true);
    } else if (this.requestMethod == "POST") {
      /* not work?
      this.xmlhttp.open(this.requestMethod,this.requestFile,true);
			this.xmlhttp.setRequestHeader("Method","POST "+this.URLString+" HTTP/1.1");
			this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      */
    } else {
      return false;
    }
    var self = this;
    this.xmlhttp.onreadystatechange = function() {
      switch (self.xmlhttp.readyState) {
        case 1:
          self.onLoading();
        break;
        case 2:
          self.onLoaded();
        break;
        case 3:
          self.onInteractive();
        break;
        case 4:
					self.response = self.xmlhttp.responseText;
					self.responseXML = self.xmlhttp.responseXML;
					self.responseStatus[0] = self.xmlhttp.status;
					self.responseStatus[1] = self.xmlhttp.statusText;
					if (self.elementObj) {
            // Element content replacement handling (if element is defined)
					  self.updateElementContent(self.elementObj);
					}
					if (self.responseStatus[0] == "200") {
  					// Completion handling
						self.onCompletion();
					} else {
					   // Error handling
						self.onError();
					}
				break;
      }
    }
    this.xmlhttp.send(null);
    return true;
  };

	this.updateElementContent = function(elementObj) {
	  // alert(this.response);
	  var elemNodeName = elementObj.nodeName;
		elemNodeName.toLowerCase();
		// Element has value
		if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea") {
			elementObj.value = this.response;
		} else {
			elementObj.innerHTML = this.response;
	  }
	};

	this.runElementUpdate = function(elementId,url,file,method,onLoadingFunc,onLoadedFunc,onInteractiveFunc,onCompletionFunc) {
	  this.reset();
	  this.element = elementId;
	  if(file) this.requestFile = file;
  	if(method) this.requestMethod = method;
	  if(onLoadingFunc) this.onLoading = onLoadingFunc;
	  if(onLoadedFunc) this.onLoaded = onLoadedFunc;
	  if(onInteractiveFunc) this.onInteractive = onInteractiveFunc;
	  if(onCompletionFunc) this.onCompletion = onCompletionFunc;
  	this.runAJAXRequest(url);
	};

  this.init();
}

var ajaxObj = new ajaxKit(0);
