function add_edit_link(t,p,o,top)
{
 var link=document.createElement("div");
 if (t==1)
 {
  link.innerHTML='> edit';
  var functext="edit_info("+p+","+o+")";
 }
 if (t==2)
 {
  link.innerHTML='> delete';
  var functext="delete_info("+p+","+o+")";
 }
 if (t==3)
 {
  link.innerHTML='> move up';
  var functext="move_info(-1,"+p+","+o+")";
 }
 if (t==4)
 {
  link.innerHTML='> move down';
  var functext="move_info(1,"+p+","+o+")";
 }
 if (t==5)
 {
  link.innerHTML='> insert new entry';
  var functext="insert_info("+p+")";
 }
 link.className='editlink';
 link.style.top=top+'px';
 link.style.height='20px';
 var func=new Function(functext);
 link.onclick=func;
 document.getElementById('sopo_right2').appendChild(link);
}

function open_edit_window()
{
 var window=document.createElement("div");
 window.className="edit_window";
 window.id="edit_window";
 var close=document.createElement("div");
 close.className="edit_link_close";
 close.innerHTML="close";
 var functext="close_edit_window()";
 var func=new Function(functext);
 close.onclick=func;
 window.appendChild(close);
 var content=document.createElement("div");
 content.className="edit_window_content";
 content.id="edit_window_content";
 window.appendChild(content);
 document.getElementById('sopo_body').appendChild(window);
}

function close_edit_window()
{
 if (document.getElementById("edit_window")) document.getElementById('sopo_body').removeChild(document.getElementById("edit_window"));
}

function clear_edit_links()
{
 document.getElementById('sopo_right2').innerHTML='';
}

function edit_info(page,opt) {
  var httpRequest;
  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      httpRequest = new XMLHttpRequest();
      if (httpRequest.overrideMimeType) {
          httpRequest.overrideMimeType('text/xml');
      }
  }
  else if (window.ActiveXObject) { // IE
      try {
          httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
          }
          catch (e) {
                     try {
                          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                         }
                       catch (e) {}
                    }
                                 }

  if (!httpRequest) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
  }
  var url = "edit_info.php?p="+page+"&o="+opt;
  httpRequest.onreadystatechange = function() { get_edit_info(httpRequest); };
  httpRequest.open('GET', url, true);
  httpRequest.send('');
}

function get_edit_info(httpRequest) {
  if (httpRequest.readyState == 4)
  {
    if (httpRequest.status == 200)
    {
      if (document.getElementById("edit_window"))
      {
        document.getElementById("edit_window_content").innerHTML='';
      } else
      {
       open_edit_window();
      }

      var xml = httpRequest.responseXML;
      var target=document.getElementById("edit_window_content");

      var nopt=xml.getElementsByTagName("la").length;

      page=xml.getElementsByTagName("page")[0].firstChild.data;
      opt=xml.getElementsByTagName("optnumber")[0].firstChild.data;

      var table=document.createElement('table');
      table.style.width="100%";
      var tr=document.createElement('tr');
      var td=document.createElement('td');
      td.innerHTML="Page: "+page;
      td.colSpan=2;
      td.style.width="50%";
      tr.appendChild(td);
      var td=document.createElement('td');
      td.innerHTML="Option: "+opt;
      td.colSpan=2;
      td.style.width="50%";
      tr.appendChild(td);
      table.appendChild(tr);

      for (iopt=0;iopt<nopt;iopt++)
      {
        thisla=xml.getElementsByTagName("language")[iopt].firstChild.data;
        title=xml.getElementsByTagName("title")[iopt].firstChild.data;
        text=xml.getElementsByTagName("text")[iopt].firstChild.data;
        var tr=document.createElement('tr');
        var td=document.createElement('td');
        td.colSpan=4;
        td.style.borderTop="1px solid #664422";
        td.style.paddingTop="5px";
        td.innerHTML="Language: "+thisla;
        tr.appendChild(td);
        table.appendChild(tr);
        var tr=document.createElement('tr');
        var td=document.createElement('td');
        td.style.width="25%";
        td.innerHTML="Title:";
        tr.appendChild(td);

        var td=document.createElement('td');
        td.colSpan=3;
        td.style.width="75%";

        var input=document.createElement('input');
        input.id="title_"+thisla;
        input.value=title;
        input.style.width="90%";
        td.appendChild(input);
        tr.appendChild(td);
        table.appendChild(tr);

        var tr=document.createElement('tr');
        var td=document.createElement('td');
        td.style.width="25%";
        td.innerHTML="Text:";
        tr.appendChild(td);

        var td=document.createElement('td');
        td.colSpan=3;
        td.style.width="75%";
        if (iopt<(nopt-1))
        {
          td.style.paddingBottom="5px";
        }
        var textarea1=document.createElement('textarea');
        textarea1.id="text_"+thisla;
        textarea1.value=text;
        textarea1.style.width="90%";
        textarea1.rows=5;
        td.appendChild(textarea1);

        tr.appendChild(td);
        table.appendChild(tr);
      }
      target.appendChild(table);
      var form=document.createElement('form');
      form.id='option_form';
      var input=document.createElement('input');
      input.type='hidden';
      input.name='option_input';
      input.id='option_input';
      input.value=opt;
      form.appendChild(input);
      var input=document.createElement('input');
      input.type='hidden';
      input.name='type';
      input.value='edit';
      form.appendChild(input);
      var input=document.createElement('input');
      input.type='hidden';
      input.name='page';
      input.value=page;
      form.appendChild(input);
      var input=document.createElement('input');
      input.type='hidden';
      input.name='languages';
      input.value=languages.join(",");
      form.appendChild(input);
      for (ila=0;ila<languages.length;ila++)
      {
        thisla=languages[ila];
        var input=document.createElement('input');
        input.type='hidden';
        input.name='title_input_'+thisla;
        input.id='title_input_'+thisla;
        form.appendChild(input);
        var input=document.createElement('input');
        input.type='hidden';
        input.name='text_input_'+thisla;
        input.id='text_input_'+thisla;
        form.appendChild(input);
      }
      target.appendChild(table);
      target.appendChild(form);
      var button=document.createElement('div');
      button.className="edit_link_close";
      button.style.textAlign='center';
      button.style.width='100px';
      button.style.left=((target.offsetWidth-100)/2)+'px';
      button.style.top=(table.offsetTop+table.offsetHeight+10)+'px';
      button.innerHTML="> change <";
      var functext="send_data(document.getElementById('option_form'));";
      var func=new Function(functext);
      button.onclick=func;
      target.appendChild(button);
    } else
    {
      alert('There was a problem with the request.');
    }
  }
}

function move_info(d,p,o)
{
  var httpRequest;
  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      httpRequest = new XMLHttpRequest();
      if (httpRequest.overrideMimeType) {
          httpRequest.overrideMimeType('text/xml');
      }
  }
  else if (window.ActiveXObject) { // IE
      try {
          httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
          }
          catch (e) {
                     try {
                          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                         }
                       catch (e) {}
                    }
                                 }

  if (!httpRequest) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
  }
  var url = "move_info.php?p="+page+"&o="+o+"&d="+d;
  httpRequest.onreadystatechange = function() { move_info_result(httpRequest,p,o); };
  httpRequest.open('GET', url, true);
  httpRequest.send('');
}

function move_info_result(httpRequest,p,o)
{
  if (httpRequest.readyState == 4)
  {
    if (httpRequest.status == 200)
    {
      var xml = httpRequest.responseXML;
      status=xml.getElementsByTagName("status")[0].firstChild.data;
      if (status==1) current_page(); else alert("An error occured!");
    }
  }
}

function insert_info(page) {
      if (document.getElementById("edit_window"))
      {
        document.getElementById("edit_window_content").innerHTML='';
      } else
      {
       open_edit_window();
      }

      var target=document.getElementById("edit_window_content");

      var table=document.createElement('table');
      table.style.width="100%";
      var tr=document.createElement('tr');
      var td=document.createElement('td');
      td.innerHTML="Page: "+page;
      td.colSpan=2;
      td.style.width="50%";
      tr.appendChild(td);
      var td=document.createElement('td');
      td.innerHTML="Option: ";
      td.style.width="25%";
      tr.appendChild(td);
      var td=document.createElement('td');
      td.style.width="25%";
      var input=document.createElement('input');
      input.id="option";
      td.appendChild(input);
      tr.appendChild(td);
      table.appendChild(tr);

      for (ila=0;ila<languages.length;ila++)
      {
        thisla=languages[ila];
        var tr=document.createElement('tr');
        var td=document.createElement('td');
        td.colSpan=4;
        td.style.borderTop="1px solid #664422";
        td.style.paddingTop="5px";
        td.innerHTML="Language: "+thisla;
        tr.appendChild(td);
        table.appendChild(tr);
        var tr=document.createElement('tr');
        var td=document.createElement('td');
        td.style.width="25%";
        td.innerHTML="Title:";
        tr.appendChild(td);

        var td=document.createElement('td');
        td.colSpan=3;
        td.style.width="75%";

        var input=document.createElement('input');
        input.style.width="90%";
        input.id="title_"+thisla;
        td.appendChild(input);
        tr.appendChild(td);
        table.appendChild(tr);

        var tr=document.createElement('tr');
        var td=document.createElement('td');
        td.style.width="25%";
        td.innerHTML="Text:";
        tr.appendChild(td);

        var td=document.createElement('td');
        td.colSpan=3;
        td.style.width="75%";
        if (ila<(languages.length-1))
        {
          td.style.paddingBottom="5px";
        }
        var textarea1=document.createElement('textarea');
        textarea1.style.width="90%";
        textarea1.rows=5;
        textarea1.id="text_"+thisla;
        td.appendChild(textarea1);

        tr.appendChild(td);
        table.appendChild(tr);
      }
  var form=document.createElement('form');
  form.id='option_form';
  var input=document.createElement('input');
  input.type='hidden';
  input.name='option_input';
  input.id='option_input';
  form.appendChild(input);
  var input=document.createElement('input');
  input.type='hidden';
  input.name='type';
  input.value='insert';
  form.appendChild(input);
  var input=document.createElement('input');
  input.type='hidden';
  input.name='page';
  input.value=page;
  form.appendChild(input);
  var input=document.createElement('input');
  input.type='hidden';
  input.name='languages';
  input.value=languages.join(",");
  form.appendChild(input);
  for (ila=0;ila<languages.length;ila++)
  {
    thisla=languages[ila];
    var input=document.createElement('input');
    input.type='hidden';
    input.name='title_input_'+thisla;
    input.id='title_input_'+thisla;
    form.appendChild(input);
    var input=document.createElement('input');
    input.type='hidden';
    input.name='text_input_'+thisla;
    input.id='text_input_'+thisla;
    form.appendChild(input);
  }
  target.appendChild(table);
  target.appendChild(form);
  var button=document.createElement('div');
  button.className="edit_link_close";
  button.style.textAlign='center';
  button.style.width='100px';
  button.style.left=((target.offsetWidth-100)/2)+'px';
  button.style.top=(table.offsetTop+table.offsetHeight+10)+'px';
  button.innerHTML="> submit <";
  var functext="send_data(document.getElementById('option_form'));";
  var func=new Function(functext);
  button.onclick=func;
  target.appendChild(button);
}

function delete_info(page,opt) {
  if (confirm("Eintrag loeschen/Delete entry?")) {
  sendRequest('get.php', "?page="+page+"&option_input="+opt+"&type=delete");
  }
}

function send_data(obj)
{
//  obj.getElementById("option_input").name="option";
  if (document.getElementById("option_input").value=="") document.getElementById("option_input").value=document.getElementById("option").value;
  for (ila=0;ila<languages.length;ila++)
  {
    thisla=languages[ila];
    document.getElementById("text_input_"+thisla).value=document.getElementById("text_"+thisla).value;
    document.getElementById("title_input_"+thisla).value=document.getElementById("title_"+thisla).value;
  }

  var getstr = "?";
  for (i=0; i<obj.childNodes.length; i++) {
    if (i>0) getstr += "&";
    getstr += obj.childNodes[i].name + "=" + remove_umlaute(obj.childNodes[i].value);
  }

//  close_edit_window()
//	alert(getstr)
  sendRequest('get.php', getstr);
}

function sendRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      // set type accordingly to anticipated content type
      //http_request.overrideMimeType('text/xml');
       http_request.overrideMimeType('text/html');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!http_request) {
    alert('Cannot create XMLHTTP instance');
    return false;
  }
  http_request.onreadystatechange = alertContents;
  http_request.open('GET', url + parameters, true);
  http_request.send(null);
}

function alertContents() {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      result = http_request.responseText;
      if (result==1) alert("Task successfully finished!"); else alert("An error has occured!");
			close_edit_window();
      current_page(1);
			//document.getElementById("edit_window").innerHTML=result;
				//document.getElementById("edit_window").innerHTML=result;
    } else {
      alert('There was a problem with the request.');
    }
  }
}
