function ajaxRequest(props) {
  this.properties = props;
  this.xmlObject = null;
  var that = this;
  if(typeof(this.properties.type) == 'undefined') {
    this.properties.type = 'GET';
  }
  try {
		this.xmlObject = new XMLHttpRequest();
	} catch(e) {
		try {
			this.xmlObject = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				this.xmlObject = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
  var handleResponse = function() {
    if(typeof(that.properties.callback) != 'function') {
      return false;
    }
  	if(that.xmlObject.readyState == 4) {
  		that.properties.callback({
  		  text      : that.xmlObject.responseText,
        xml       : that.xmlObject.responseXML,
        options   : that.properties.options
      });
  	}
  }
  this.xmlObject.onreadystatechange = handleResponse;
  this.startRequest = function() {
    var now = new Date;
    var time = that.properties.url.substr(that.properties.url.length-3,3) == 'php' || that.properties.url.substr(that.properties.url.length-4,4) == 'html' ? '?time=' + now.getTime() : '&time=' + now.getTime();
    that.xmlObject.open(that.properties.type, that.properties.url + time, true);
  	// ajaxRequest.setRequestHeader("Pragma", "no-cache");
  	that.xmlObject.setRequestHeader("Cache-Control", "must-revalidate");
  	that.xmlObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  	var postData = '';
  	if(typeof(that.properties.data) != 'undefined') {
  	  if(typeof(that.properties.data) === 'string') {
    	  postData += 'data=' + encodeURIComponent(that.properties.data) + '&';
  	  } else {
  	    for(name in that.properties.data) {
    	    if(name === 'click' || name === 'clickElement' || name === 'mouseout' || name === 'mouseover' || name === 'queryState' || name === 'toggle'){
    	      continue;
    	    }
    	    postData += name + '=' + encodeURIComponent(that.properties.data[name]) + '&';
    	  }
  	  }
  	}
  	that.xmlObject.send(postData);
  }
}

function $(id) {
  return document.getElementById(id);
}

function json_decode(str_json) {
  var json = this.window.JSON;
  if (typeof json === 'object' && typeof json.parse === 'function') {
    return json.parse(str_json);
  }
  var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  var j;
  var text = str_json;
  cx.lastIndex = 0;
  if (cx.test(text)) {
      text = text.replace(cx, function (a) {
          return '\\u' +
          ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
      });
  }
  if (/^[\],:{}\s]*$/.
    test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
        replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
        replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
    j = eval('(' + text + ')');
    return j;
  }
  throw new SyntaxError('json_decode');
}

/**
 * Bewertet einen Witz
 */
function bewerteWitz(witz_id, bewertung, reload) {
  witz_id = (typeof(witz_id)=='number') ? witz_id : 0;
  bewertung = (typeof(bewertung)=='number') ? bewertung : 0;
  reload = (typeof(reload)=='number') ? parseInt(reload,2) : 0;
  var props = {
    url       : 'request.php',
    type      : 'POST',
    callback  : bewerteWitz_CALLBACK,
    data      : {
      baustein  : 'bewertung_abgeben',
      witz_id   : witz_id,
      bewertung : bewertung,
      reload    : reload
    }
  };
  var request = new ajaxRequest(props);
  request.startRequest();
}
function bewerteWitz_CALLBACK(response) {
  var data = json_decode(response.text);
  var htmlobj = $('bewertung_'+data['witz_id']);
  if(typeof(htmlobj)!='undefined') {
    htmlobj.innerHTML = data['aktuelle_bewertung'];
  }
  if(data['reload']==1) {
    location.reload();
  }
}

/**
 * toggle Menu
 */
function MenuOn(menu_item) {
  menu_item.src = 'gfx/'+menu_item.id+'_2.jpg';
}
function MenuOff(menu_item) {
  menu_item.src = 'gfx/'+menu_item.id+'_1.jpg';
}