/*
Script: ktajax.js

License:
	commercial msse licence

Copyright:
	Copyright (c) 2008 Michael Schwarzl


*/


var ktClass = {
  create: function() {
    return function() { 
      this.initialize.apply(this, arguments);
    }
  }
}

ktAjax = ktClass.create();
ktAjax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.onAfterComplete = options.onAfterComplete || null;
		this.update = $(options.update) || null;
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == 'post') {
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			if (this.update)
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
			if (this.onAfterComplete) 
				setTimeout(function(){this.onAfterComplete(this.transport);}.bind(this), 10);
			this.transport.onreadystatechange = function(){};
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false; 
	}
};

ktAjaxSplit = ktClass.create();
ktAjaxSplit.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.update = $(options.update) || null;
		this.updatenames = options.updatenames || '';
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == 'post') {
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);

				var strresponse = this.transport.responseText;
				var splitresponse = strresponse.split("|");
			    	var strupdate = this.updatenames;
				var splitupdate = strupdate.split("|");
				var obj;

				var strtemp;
                		for(i=0; i<splitresponse.length; ++i) {
           		  	  strtemp = splitresponse[i].split("=");
				  if (strtemp[0] == "alert") { alert(strtemp[1]); }
				}

                		for(i=0; i<splitupdate.length; ++i) {
           		          obj = $(splitupdate[i]) || null;
			          if (obj  && i < splitresponse.length) { obj.innerHTML = splitresponse[i]; }
  			        }
			   
			this.transport.onreadystatechange = function(){};
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};