var zeilencount = 0;
var zeilencountb = 0;
var chstring;
var chergebnis;
var xmlHttp2 = false;
 
//XMLHttpRequest-Instanz erstellen
//... fr Internet Explorer
try {
    xmlHttp2  = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
    try {
        xmlHttp2  = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        xmlHttp2  = false;
    }
}
//... fr Mozilla, Opera, Safari usw.
if (!xmlHttp2  && typeof XMLHttpRequest != 'undefined') {
    xmlHttp2 = new XMLHttpRequest();
}
// Ende ajax

chstring = getcharacters();

function showstartform(){


  newzeile(zeilencount);
  
}



function getcharacters(){
  chergebnis = "";
  while(chergebnis == ""){
    if (xmlHttp2) {
      xmlHttp2.onreadystatechange = getcharacters2;
      xmlHttp2.open('POST', './ajax/search.php');
      xmlHttp2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      xmlHttp2.send('action=getchar');
      xmlHttp2.send(null);
    }
  }
  return chergebnis;
}

function getcharacters2() {
         if (xmlHttp2.readyState == 4) {
             if (xmlHttp2.status == 200) {
                chergebnis = xmlHttp2.responseText;
            } else {
                chergebnis = "unbekannter Fehler..";
            }
             
             //alert(chergebnis);
             
             //if(output != "") return output;
         }
         //alert("Hallo");
         //alert(chergebnis);
}

function newzeile(){

  //auf 6 beschraenken
  if(zeilencount>=6) return '';
  
  var row = document.getElementById("searchtable").insertRow( zeilencount );
  
  var cell = row.insertCell( 0 );
  var select1 = document.createElement( 'SELECT' );
  select1.id = 'eins['+row.rowIndex+']';
  select1.onchange = function() { if(this.selectedIndex != 2){
                                      select2.options[0] = new Option('ist gleich', 'gleich', true, true);
                                      select2.options[1] = new Option('enthält', 'enthaelt', false, false);
  				      select2.options[2] = new Option('beginnt mit', 'beginnt', false, false);
  				      
  				      row.deleteCell(2);
  				      cell = row.insertCell( 2 );
  				      var input = document.createElement( 'INPUT' );
  				      input.id = 'drei['+row.rowIndex+']';
  			  	      input.type = 'text';
				      input.size = '20';
  			  	      cell.appendChild( input );
  				    }
  				    //alert("ups: "+zeilencount);
  				    if(this.selectedIndex == 2){
  				      select2.options[0] = new Option('enthält', 'enthaelt', false, false);
                                      select2.options[1] = new Option('beginnt mit', 'beginnt', false, false);
  				      select2.options[2] = null;
  				      row.deleteCell(2);
  				      
  				      cell = row.insertCell( 2 );
  				      var input = document.createElement( 'INPUT' );
  				      input.id = 'drei['+row.rowIndex+']';
  			  	      input.type = 'text';
				      input.size = '20';
  			  	      cell.appendChild( input );
  				    }
  				    if(this.selectedIndex == 3){
                                      select2.options[0] = new Option('enthält', 'enthaelt', false, false);
                                      select2.options[1] = null;
  				      select2.options[1] = null;
  				      
  				      row.deleteCell(2);
  				      cell = row.insertCell( 2 );
  				      var input = document.createElement( 'SELECT' );
  				      input.id = 'drei['+row.rowIndex+']';
  		
			      
  				      if(chergebnis == "") getcharacters();
  				      var charr = chergebnis.split(";");
  				      for(var i = 0; i < charr.length; i++){
  				        if(i==0)
  			  	          input.options[i] = new Option(charr[i], charr[i], true, true);
  			  	        else
 			  	          input.options[i] = new Option(charr[i], charr[i], false, false);
  			  	      }
  			  	      cell.appendChild( input );
  				    }
                                  };
  select1.options[0] = new Option('Autor', 'Autor', true, true);
  select1.options[1] = new Option('Storytitel', 'Storytitel', false, false);
  select1.options[2] = new Option('Inhaltsangabe', 'Summary', false, false);
  select1.options[3] = new Option('Charakter', 'Charakter', false, false);
  select1.options[4] = new Option('Genre', 'Genre', false, false);
  select1.options[5] = new Option('Altersfreigabe', 'Altersfreigabe', false, false);
  select1.options[6] = new Option('Warnung', 'Warnung', false, false);
  cell.appendChild( select1 );
  
  cell = row.insertCell( 1 );
  var select2 = document.createElement( 'SELECT' );
  select2.id = 'zwei['+row.rowIndex+']';
  select2.options[0] = new Option('ist gleich', 'gleich', true, true);
  select2.options[1] = new Option('enthält', 'enthaelt', false, false);
  select2.options[2] = new Option('beginnt mit', 'beginnt', false, false);
  cell.appendChild( select2 );
  
  cell = row.insertCell( 2 );
  var input = document.createElement( 'INPUT' );
  input.type = 'text';
  input.id = 'drei['+row.rowIndex+']';
  input.size = '20';
  cell.appendChild( input );
  

  cell = row.insertCell( 3 );
  var entfern = document.createElement( 'INPUT' );
  entfern.type = 'button';
  entfern.value = '-';
  entfern.onclick = function() { if(zeilencount>1){ document.getElementById("searchtable").deleteRow( this.parentNode.parentNode.rowIndex ); zeilencount -= 1; } }
  cell.appendChild( entfern );

  
  zeilencount += 1;
  zeilencountb += 1;
  
}






function performsearch(seite){
  document.getElementById("suchergebnisse").innerHTML = "Bitte warten...";
  if (xmlHttp2) {
      xmlHttp2.open('POST', './ajax/search.php');
      xmlHttp2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      var sendstring = 'seite='+seite+'&';
      for(var i = 0; i <= zeilencountb; i++){
        if(document.getElementById("eins["+i+"]")){
          sendstring += 'eins['+i+']='+document.getElementById("eins["+i+"]").value + "&";
          sendstring += 'zwei['+i+']='+document.getElementById("zwei["+i+"]").value + "&";
          sendstring += 'drei['+i+']='+document.getElementById("drei["+i+"]").value + "&";
        }
      }
      xmlHttp2.send(sendstring);
      xmlHttp2.onreadystatechange = function () {
         if (xmlHttp2.readyState == 4) {
             if (xmlHttp2.status == 200) {
                response = xmlHttp2.responseText;
                document.getElementById("suchergebnisse").innerHTML = response;
            } else {
                document.getElementById("suchergebnisse").innerHTML = "Ein Fehler ist aufgetreten...";
            }
             
         }else{
             document.getElementById("suchergebnisse").innerHTML = "Bitte warten....";
         }
      };
      //xmlHttp2.send(null);
  }else{
      document.getElementById("suchergebnisse").innerHTML = "Fehler...";
  }
}

function blaettern(seite){
  performsearch(seite)
}

