/*
 *#############################################
 *# About this file: ajax_request_frage.js
 *#############################################
 *
 * Projekt: 1000fragen.ch
 * JavaScript for AJAX functionality
 * Autor: Marcel Biefer
 *
 * is linked into index.php
 * Does:
 * present the (selected/found) Question and its Answers to the content_frage_antworten.php
 * creates a request object, a send request and a handle response function
 * RequestObject is started by sendRequest
 * sendRequest is started first by the onload("sndReqForBox('frage_nr')") eventhandler in the Body of index.php
 *    then by a click on a Link from verzeichnis_antworten/_fragen.php,
 *    or/and from the ajax_suggest.js
 * sendRequest opens ajax_request_frage.php and sends some parameters
 *    and starts handleResponse
 * handleResponse writes the answer in the div Container
*/

//-----------------------------------------------------------------------------
// RequestObject
//-----------------------------------------------------------------------------

function createReqObj() {
    var robj;
    var Client = navigator.appName;
    if(Client == "Microsoft Internet Explorer"){
        robj = new ActiveXObject("Microsoft.XMLHTTP");
//        alert ("IE");
    }else{
        robj = new XMLHttpRequest();
//        alert ("not IE");
    }
    return robj;
}

var robj = createReqObj(); // save RequestObject

//-----------------------------------------------------------------------------
// sendRequest
//-----------------------------------------------------------------------------

function sndReqForBox(frage_nr) {
//   alert ("frage_nr");
// vorab Nummer in Nummern-Feld des Antwortformulars und Weiterleitungsformulars hinein schreiben
    document.getElementById('antwort_nummer').value=frage_nr;
    document.getElementById('frage_nummer').value=frage_nr;

//   robj.open('get', 'ajax_request_frage.php?frage_nr='+frage_nr+'&yyy=zzz', true);
   robj.open('GET', 'ajax_request_frage.php?frage_nr='+frage_nr, true);
   robj.onreadystatechange = handleResponz;
   robj.send(null);
}

//-----------------------------------------------------------------------------
// handleResponz
//-----------------------------------------------------------------------------
function handleResponz() {
    if(robj.readyState == 4){
      var responz = robj.responseText;
// alert ("robj.responseText: " + robj.responseText);
      var VollesRohr = new Array();
      if(responz.indexOf('QQQ' != -1)) {
         VollesRohr = responz.split('QQQ');
// Darstellung der Frage
         document.getElementById(VollesRohr[0]).innerHTML = VollesRohr[1];
// Darstellung der Antwort(en)
         document.getElementById(VollesRohr[2]).innerHTML = VollesRohr[3];
      }
    }
}

// ----------------------------------------------------------------------------
// Erklährungen
/*
handleResponz:  It parses the "foo|foo done" string and
splits it on the '|' and uses whatever is before the first '|' as the dom
element id in the page, the part after the first '|' as the url/content of that
element.  I must have a div tag like this in my page: <div id="foo"></div>
The part after the second '|' is any Zusatz given back
*/
/*
To do: 
1. Insert some ERROR handling.
2. Replacing this simple response "id|text or img" syntax with a rich XML.
3. Send multiple parameters in the request like this:
  function sndReqArg(action,arg) {
    http.open('get', 'rpc.php?action='+action+'&arg='+arg);
    http.onreadystatechange = handleResponse;
    http.send(null);
  }
*/
