var Ajax = function(){
  this.Object = null;
  this.RequestType = 'POST';
  this.ContentType = 'application/x-www-form-urlencoded; charset=UTF-8';
  this.Function = null;
  this.Parameters = new Array();
  this.RequestedUrl = '';
  this.PreviousUrl = '';

  this.ReadyStates = Array('Uninitialized','Loading','Loaded','Running','Ready');

  this.GetObject(); //Initialize http request object
}

Ajax.prototype.SetType = function(newType){
  this.RequestType = newType;
}

Ajax.prototype.GetState = function(){
  return this.Object.readyState;
}

Ajax.prototype.GetData = function(){
  return this.Object.responseText;
}

Ajax.prototype.GetStateText = function(){
  return this.ReadyStates[this.Object.readyState];
}

Ajax.prototype.CallFile = function(url){
  if(this.Object) {
  	if(navigator.vendor && navigator.vendor.indexOf('Apple') != -1)
  		this.Object.open(this.RequestType,url,false);
  	else
    	this.Object.open(this.RequestType,url,true);
    this.Object.setRequestHeader('Content-Type', this.ContentType);
    this.Object.setRequestHeader("Connection", "close");
    this.Object.onreadystatechange = this.Function;

    var parameters = "";
    for(var key in this.Parameters){
      var param = this.DecodeHtml(this.Parameters[key]);

      if(parameters == "")
        parameters = parameters + key + '=' + param;
      else
        parameters = parameters + '&' + key + '=' + param;
    }
	if(parameters == "")
		parameters = false;

	this.PreviousUrl = this.RequestedUrl;
	this.RequestedUrl = url;
    this.Object.send(parameters);
  }
}

Ajax.prototype.ReadyFunction = function(func){
  this.Function = func;
}

Ajax.prototype.GetObject = function(){
  var obj;
  try {
       obj = new ActiveXObject("Microsoft.XMLHTTP");    // Trying Internet Explorer
    }
    catch(e)    // Failed
    {
      obj = new XMLHttpRequest()
    }
  this.Object = obj;
}

Ajax.prototype.addLoadEvent = function(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

Ajax.prototype.addMessageDivToPage = function(){
  if(document.getElementById('ajaxMsg') == null){
	var ajaxMsg = document.createElement('div');
	ajaxMsg.setAttribute('id','ajaxMsg');
	ajaxMsg.setAttribute('className','ajaxMsg');
	ajaxMsg.style.cssText = 'display:none;position:absolute;';
	document.body.appendChild(ajaxMsg);
  }
}

Ajax.prototype.showMessage = function(string){
	this.addMessageDivToPage();
	var msg = document.getElementById('ajaxMsg');
	var width = 200;
	var height = 150;
	msg.innerHTML = '<p class="ajaxMessageText">'+string+'<p>';
	msg.style.top = ((document.documentElement.clientHeight / 2)) + document.documentElement.scrollTop + 'px';
	msg.style.width = width + 'px';
	msg.style.height = height + 'px';
	msg.style.left = ((document.documentElement.clientWidth / 2)) - (width / 2) + 'px';
	msg.style.display = 'block';

}

Ajax.prototype.hideMessage = function(){
	if(document.getElementById('ajaxMsg'))
		document.getElementById('ajaxMsg').style.display = 'none';
}

Ajax.prototype.utf8_encode = function (string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

        var c = string.charCodeAt(n);

        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }

    }

    return utftext;
}

Ajax.prototype.utf8_decode = function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while ( i < utftext.length ) {

        c = utftext.charCodeAt(i);

        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        }
        else if((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i+1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else {
            c2 = utftext.charCodeAt(i+1);
            c3 = utftext.charCodeAt(i+2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }

    }

    return string;
}


Ajax.prototype.DecodeHtml = function(str){
	if(str){
		str = str.replace(/&auml;/g,'ä').replace(/&Auml;/,'Ä');
		str = str.replace(/&ouml;/g,'ö').replace(/&Ouml;/,'Ö');
		str = str.replace(/&aring;/g,'å').replace(/&Aring;/,'Å');
		str = str.replace(/&lt;/g,'<');
		str = str.replace(/&gt;/g,'>');
	}

	return str;
}

