function ajax_create() {
  if (window.XMLHttpRequest) { //FF
    return new XMLHttpRequest();
  } else if (window.ActiveXObject) { //IE
    return new ActiveXObject("Msxml2.XMLHTTP");
  } else {
    alert('Abfrage fehlgeschlagen.');
    return false;
  }
}

function ajax_getcontent(url, objectid, onfinish) {
  ajax_request(url, function(http_request) { ajax_setcontent(http_request, objectid, onfinish) } );
}

function ajax_request (url, callback) {

  var http_request = ajax_create();

  http_request.open ("GET", url, true);
  http_request.onreadystatechange = function() {
    if (http_request.readyState == 4) {
      http_request.onreadystatechange = function(){};
      callback (http_request);
    }
  };
  http_request.send (url);
  return true;
}


function ajax_setcontent (http_request, id, onfinish) {

  if (http_request.status != 200) {
    alert('Abfrage fehlgeschlagen.');
    return;
  }

  if (!document.getElementById(id)) { window.setTimeout(function() { ajax_setcontent(http_request, id); },50); return; }
  document.getElementById(id).innerHTML = http_request.responseText;
  elid = id;

  last = anchor.lastIndexOf("*");
  anch = last == -1 ? anchor : anchor.substr(0,last);

  if (id == "content") { updatenav(); }

  if (onfinish) window.setTimeout(onfinish,5);

  if (anch == "#pages/new" || anch == "#news/new" || anch.substr(0,11) == "#pages/edit" || anch.substr(0,10) == "#news/edit") { load_js("tinymce/config.js"); return; }
  if (anch.substr(0,11) == "#users/edit") { document.users_edit.users_nick.focus(); return; }
  if ((anch == "#login" || anch == "#users/login") && elid == "content" && document.login_form) {
     document.login_form.login_nick.focus();
     ajax_getcontent('start.php?load=users/navi/login','loginarea'); return; }
  if (anch.substr(0,9) == "#jobs/new" || anch.substr(0,10) == "#jobs/edit") { load_js("core/abcode.js"); return; }
  if (anch.substr(0,12) == "#jobs/delete") { document.jobs_delete.confirm.focus(); return; }
  if (((anch == "#/Products" || anch == "#pages[24]")  && elid != "product_content") || anch == "#/Updates_2008.2" || anch == "#pages[47]") { lastmod = ""; load_js("core/products.js"); return; }  
  if (anch == "#/Adressen") {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        address = document.getElementById('language').firstChild.alt != "usa" ? new GLatLng(49.87358,8.631432) : new GLatLng(28.100342,-80.664797);
        map.setCenter(address,15);
        //map.setMapType(G_HYBRID_TYPE);
        map.addOverlay(new GMarker(address));
        map.addControl(new GLargeMapControl());
        var mapControl = new GMapTypeControl();
        map.addControl(mapControl);
     } return; }
}

var anchor = "";
var timer = window.setInterval("checkanchor()", 20);
var path = window.location.href.indexOf('/') == window.location.href.lastIndexOf('/') ? '' : '../';

function checkanchor () {
  if (window.location.hash.substr(1,1) == "_") return;
  new_anchor = window.location.hash;
  if (new_anchor == "") { new_anchor = "#news"; }
  if (new_anchor != anchor) {
    if (document.getElementById("content")) { document.getElementById("content").innerHTML = '<img src="'+path+'design/img/loading.gif" alt="Loading.." />'; }
    ajax_getcontent(path + "start.php?load=" + new_anchor.substr(1), "content");
    anchor = new_anchor;
  }
}

function form_submit(formname, load) {
  
  cform = document.forms[formname];
  params = "";
  count = cform.elements.length;
  upload = new Array();

  for (i = 0; i < count; i++) {
    el = cform.elements[i];
    value = "";
    skip = 0;

    switch (el.type) {
      case 'text': case 'select-one':  case 'hidden': case 'password':
        value = el.value; break;
      case 'textarea':
        value = el.value;
        if (el.value.length > 65000) {
          value = value.substr(0,65000);
          alert("Text zu lang. Nur die ersten 65.000 Zeichen uebermittelt."); 
        }
        break;
      case 'submit':
        value = 1; break;
      case 'checkbox':
        value = (el.checked == false) ? 0 : 1; break;
      case 'file':
        if (el.value) { upload.push(el); } skip = 1; break;
      case 'radio':
        if (el.checked) value = el.value; else skip = 1; break;
    }
    params += skip == 1 ? "" : el.name + "=" + escape(value) + "&";
  }
  document.getElementById("content").innerHTML = '<img src="'+path+'design/img/loading.gif" alt="Loading.." />';
  if (!upload.length) request_form(load, params);
  else {
    upload_file(upload, load);

    check_upload_done = window.setInterval(function() {
      if (top.frames.ifr.document.getElementById('done')) {
        params += "upload=" + escape(top.frames.ifr.document.getElementById('content').innerHTML);
        request_form(load, params);
        window.clearInterval(check_upload_done); }
    }, 100);
  }
}

function request_form(load, params) {

  if (!(http_request = ajax_create())) return;

  http_request.open('POST',"start.php?load=" + load,true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", params.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(params);
  http_request.onreadystatechange = function() {
    if (http_request.readyState == 4) {
      http_request.onreadystatechange = function(){};
      ajax_setcontent(http_request, "content")
    }
  };
}

function upload_file (els,load) {

  if (!top.frames.ifr) {
    iframe = document.createElement("iframe");
    iframe.setAttribute("name","ifr");
    iframe.setAttribute("width","0");
    iframe.setAttribute("height","0");
    iframe.setAttribute("frameborder","0");

    document.getElementById('wrapper').appendChild(iframe);
  }

  top.frames.ifr.document.open();
  top.frames.ifr.document.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>Iframe</title></head><body></body></html>');
  top.frames.ifr.document.close();

  form = document.createElement('form');
  form.name = "ifr_form";
  form.method = "post";
  form.action = "start.php?load=" + load.substr(0,load.lastIndexOf("/")) + "/upload";
  form.setAttribute("enctype","multipart/form-data");

  top.frames.ifr.document.body.appendChild(form);
  for (i in els) top.frames.ifr.document.forms.ifr_form.appendChild(els[i]);
  top.frames.ifr.document.forms.ifr_form.submit();
}

function lang_switch () {
  document.getElementById('lang_sel').style.display = document.getElementById('lang_sel').style.display == "block" ? "none" : "block";
}

function lang_select (language) {
  //if (anch.substr(0,11) == "#pages/edit")  else {
  cache = document.createElement("div");
  cache.id = "cache";
  cache.style.display = "none";
  document.getElementById('wrapper').appendChild(cache);
  ajax_getcontent("lang_switch.php?lang=" + language + "&load=" + anchor.substr(1), "cache","lang_update()");
  //}
}
function lang_update () {
  if (anch.substr(0,11) == "#pages/edit") location.reload();
  contents = document.getElementById('cache').innerHTML.split("[split]");
  
  document.getElementById('navi').innerHTML = contents[0];
  
  document.getElementById('language').innerHTML = contents[1];
  document.getElementById('headtext').innerHTML = contents[2];
  document.getElementById('content').innerHTML = contents[3];
  
  updatenav();
  
}