var commentDings;
var html_path = 'http://www.eierkraulen.net/';
function setCommentRequest(kind, id, rating) {
  // Request erzeugen
  if (window.XMLHttpRequest) {
  	request = new XMLHttpRequest(); // Mozilla, Safari, Opera
  } else if (window.ActiveXObject) {
  	try {
  		request = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
  	} catch (e) {
  		try {
  			request = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
  		} catch (e) {}
  	}
  }

  // überprüfen, ob Request erzeugt wurde
  if (!request) {
  	alert("Kann keine XMLHTTP-Instanz erzeugen");
  	return false;
  } else {
  	var url = html_path+'submitAJAX.php';
    switch (kind) {
      case "add":
        commentDings = 'comments';
        // vars auslesen
        var comment_post_id = document.getElementById('comment_post_id').value;
        var comment_author = document.getElementById('comment_author').value;
        var comment_content = document.getElementById('comment_content').value;
        var comment_status = document.getElementById('comment_status').value;
        var comment_parent = document.getElementById('comment_parent').value;
        var user_id = document.getElementById('user_id').value;
        // Timer zeigen
        showTimer('commentbox', 'load.gif');
      	// Request öffnen
      	request.open('post', url, true);
      	// Requestheader senden
      	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      	// Request senden
      	request.send('comment_post_id='+comment_post_id+
                     '&comment_author='+comment_author+
                     '&comment_content='+comment_content+
                     '&comment_status='+comment_status+
                     '&comment_parent='+comment_parent+
                     '&user_id='+user_id+
                     '&submitKind=comment'+
                     '&commentKind='+kind);
      	// Request auswerten
      	request.onreadystatechange = interpretCommentRequest;
        break;
      case "check":
        // vars auslesen
        var comment_post_id = document.getElementById('comment_post_id').value;
        var comment_author = document.getElementById('comment_author').value;
        var comment_content = document.getElementById('comment_content').value;
        var comment_status = document.getElementById('comment_status').value;
        var comment_parent = document.getElementById('comment_parent').value;
        var question = document.getElementById('question').value;
        var question_id = document.getElementById('question_id').value;
        var question_user_id = document.getElementById('question_user_id').value;
        var user_id = 0;
        commentDings = "comments";
        // Timer zeigen
        showTimer('comments', 'load.gif');
        // Request öffnen
        request.open('post', url, true);
        // Requestheader senden
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // Request senden
        request.send('comment_post_id='+comment_post_id+
                     '&comment_author='+comment_author+
                     '&comment_content='+comment_content+
                     '&comment_status='+comment_status+
                     '&comment_parent='+comment_parent+
                     '&question='+question+
                     '&question_id='+question_id+
                     '&question_user_id='+question_user_id+
                     '&user_id='+user_id+
                     '&submitKind=comment'+
                     '&commentKind='+kind);
        // Request auswerten
        request.onreadystatechange = interpretCommentRequest;
        break;
      case "delete":
        commentDings = "comment_"+id;
        // Timer zeigen
        showTimer('comment_'+id, 'comment_load.gif');
        // Request öffnen
        request.open('post', url, true);
        // Requestheader senden
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // Request senden
        request.send('comment_id='+id+
                     '&submitKind=comment'+
                     '&commentKind='+kind);
        // Request auswerten
        request.onreadystatechange = interpretCommentRequest;
        break;
      case "delete_sure":
        commentDings = "comment_u_"+id;
        // Timer zeigen
        showTimer('comment_'+id, 'comment_load.gif');
        // Request öffnen
        request.open('post', url, true);
        // Requestheader senden
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // Request senden
        request.send('comment_id='+id+
                     '&submitKind=comment'+
                     '&commentKind='+kind);
        // Request auswerten
        request.onreadystatechange = interpretCommentRequest;
        break;
      case "edit":
        commentDings = "comment_"+id;
        // Timer zeigen
        showTimer('comment_'+id, 'comment_load.gif');
        // Request öffnen
        request.open('post', url, true);
        // Requestheader senden
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // Request senden
        request.send('comment_id='+id+
                     '&submitKind=comment'+
                     '&commentKind='+kind);
        // Request auswerten
        request.onreadystatechange = interpretCommentRequest;
        break;
      case "page":
        commentDings = "comments";
        // vars auslesen
        var comment_post_id = document.getElementById('comment_post_id').value;
        // Timer zeigen
        showTimer('commentbox','load.gif');
        // Request öffnen
        request.open('post', url, true);
        // Requestheader senden
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // Request senden
        request.send('comment_post_id='+comment_post_id+
                     '&comment_page='+id+
                     '&submitKind=comment'+
                     '&commentKind='+kind);
        // Request auswerten
        request.onreadystatechange = interpretCommentRequest;
        break;
      case "rate":
        commentDings = "comment_rating_"+id;
        // Timer zeigen
        showTimer('comment_rating_'+id,'comment_load.gif');
        // Request öffnen
        request.open('post', url, true);
        // Requestheader senden
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // Request senden
        request.send('comment_id='+id+
                     '&comment_rating='+rating+
                     '&submitKind=comment'+
                     '&commentKind='+kind);
        // Request auswerten
        request.onreadystatechange = interpretCommentRequest;
        break;
      case "spam":
        commentDings = "comment_u_"+id;
        // Timer zeigen
        showTimer('comment_'+id, 'comment_load.gif');
        // Request öffnen
        request.open('post', url, true);
        // Requestheader senden
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // Request senden
        request.send('comment_id='+id+
                     '&submitKind=comment'+
                     '&commentKind='+kind);
        // Request auswerten
        request.onreadystatechange = interpretCommentRequest;
        break;
      case "update":
        commentDings = "comment_"+id;
        // vars auslesen
        var comment_content = document.getElementById('comment_content_'+id).value;
        if(true == document.getElementById('comment_editby').checked) {
          var comment_editby = document.getElementById('comment_editby').value;
        } else {
          var comment_editby = 1;
        }
        // Timer zeigen
        showTimer('comment_'+id, 'comment_load.gif');
        // Request öffnen
        request.open('post', url, true);
        // Requestheader senden
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // Request senden
        request.send('comment_id='+id+
                     '&comment_content='+comment_content+
                     '&comment_editby='+comment_editby+
                     '&submitKind=comment'+
                     '&commentKind='+kind);
        // Request auswerten
        request.onreadystatechange = interpretCommentRequest;
        break;
      }
    }
  }
  // Request auswerten
function interpretCommentRequest() {

  switch (request.readyState) {
  	// wenn der readyState 4 und der request.status 200 ist, dann ist alles korrekt gelaufen
  	case 4:
  		if (request.status != 200) {
  			alert("Der Request wurde abgeschlossen, ist aber nicht OK\nFehler:"+request.status);
  		} else {
  			var content = request.responseText;
  			// den Inhalt des Requests in das <div> schreiben
  			document.getElementById(commentDings).innerHTML = content;
  		}
  		break;
  	default:
  		break;
  }
}

function showTimer(where, name) {
    content = "<center><img src='"+html_path+"images/"+name+"'></center>";
    document.getElementById(where).innerHTML = content;
}