// ==================================================================================
// ============================ ADMIN NAVIGATION ====================================
// ==================================================================================


function init_nav() {
   if(!document.getElementById)
      return;
   for(i = 0; i < document.getElementsByTagName('ul').length; 
       i++) {
      if(document.getElementsByTagName('ul')[i].className == 
      "opened") {
         id = 
         document.getElementsByTagName('ul')[i].parentNode.id;
         toggle(id, false);
      }
   }
   if(window.name.length > 0)
      load_nav();
   if(document.getElementById("root"))
      document.getElementById('root').style.display = "block";
}

function toggle(id, save) {
   if(!document.getElementById)
      return;
   ul = "ul_" + id;
   img = "img_" + id;
   ul_element = document.getElementById(ul);
   img_element = document.getElementById(img);
   if(ul_element) {
       if(ul_element.className == 'closed') {
          ul_element.className = "opened";
          img_element.src = "/styles/de_default/opened.gif";
       } 
       else {
          ul_element.className = "closed";
          img_element.src = "/styles/de_default/closed.gif";
       }
   }
   if(save == true) save_nav();
}

function save_nav() {
   if(!document.getElementById)
      return;
   var save = "";
   for(var i = 0; i < document.getElementsByTagName('ul').length;
   i++) {
      if((document.getElementsByTagName('ul')[i].className == 
      "opened" || 
      document.getElementsByTagName('ul')[i].className == 
      "closed") && document.getElementsByTagName('ul')[i].id != 
      'root') 
         save = save + document.getElementsByTagName('ul')[i].id 
         + "=" + document.getElementsByTagName('ul')[i].className 
         + ",";
   }
   if(save.lastIndexOf(",") > 0) 
      save = save.substring(0, save.lastIndexOf(","));
   window.name = save;
}

function load_nav() {
   if(!document.getElementById)
      return;
   if(!document.getElementById("root"))
      return;   
   if(window.name.indexOf(",") == -1)
      return;   
   var items = window.name.split(",");
   if(items.length > 0) {
      for(var i = 0; i < items.length; i++) {
         id_value = items[i].split("=");
         if(id_value.length == 2) {
            nid = id_value[0];
            value = id_value[1];
            document.getElementById(nid).className = value;
            img = "img_" + nid.substring(3, nid.length);
            img_element = document.getElementById(img);
            if(value == "closed")
               img_element.src = "/styles/de_default/closed.gif";
            else
               img_element.src = "/styles/de_default/opened.gif";
         }
      }
   }
}


// ==================================================================================
// ============================ FORM SUPPORT ========================================
// ==================================================================================



function init_form() {
}

function tag_insert(form_name, elem_name, start_tag, end_tag) {
   var input = document.forms[form_name].elements[elem_name];
   input.focus();
   if(typeof document.selection != 'undefined') {
      var range = document.selection.createRange();
      var insert_text = range.text;
      range.text = start_tag + insert_text + end_tag;
      range = document.selection.createRange();
      if(insert_text.length == 0)
         range.move('character', -end_tag.length);
      else
         range.moveStart('character', start_tag.length + insert_text.length + end_tag.length);
      range.select();
   }
   else if(typeof input.selectionStart != 'undefined') {
      var start = input.selectionStart;
      var end = input.selectionEnd;
      var insert_text = input.value.substring(start, end);
      input.value = input.value.substr(0, start) + start_tag + insert_text + end_tag + input.value.substr(end);
      var pos;
      if(insert_text.length == 0)
         pos = start + start_tag.length;
      else
         pos = start + start_tag.length + insert_text.length + end_tag.length;
      input.selectionStart = pos;
      input.selectionEnd = pos;
   }
}

function list_insert(form_name, elem_name, type) {
   var input = document.forms[form_name].elements[elem_name];
   input.focus();
   if(typeof document.selection != 'undefined') {
      var range = document.selection.createRange();
      var insert_text = range.text;
    var lines = insert_text.split("\n");
    for(i = 0; i < lines.length; i++)
       lines[i] = "<li>" + lines[i] + "</li>";
    if(type == 'ol')
        range.text = "<ol>\n" + lines.join("\n") + "\n</ol>\n";
    else
        range.text = "<ul>\n" + lines.join("\n") + "\n</ul>\n";
      range = document.selection.createRange();
   if(insert_text.length == 0)
         range.move('character', 0);
      else
      range.moveStart('character', insert_text.length + (lines.length * 2) + 9);
      range.select();
   }
   else if(typeof input.selectionStart != 'undefined') {
      var start = input.selectionStart;
      var end = input.selectionEnd;
      var insert_text = input.value.substring(start, end);
    var lines = insert_text.split("\n");
    for(i = 0; i < lines.length; i++)
       lines[i] = "<li>" + lines[i] + "</li>";
    if(type == 'ol') {
        li_start = "<ol>\n";
        li_end = "\n</ol>";
      }
    else {
        li_start = "<ul>\n";
        li_end = "\n</ul>";
      }
    input.value = input.value.substr(0, start) + li_start + lines.join("\n") + li_end + input.value.substr(end);
      var pos;
      if(insert_text.length == 0)
         pos = end;
      else
         pos = start + insert_text.length + (lines.length * 2) + 18;
      input.selectionStart = pos;
      input.selectionEnd = pos;
   }
}




// ==================================================================================
// ============================ AJAX BASICS =========================================
// ==================================================================================


// AJAX-Initialisierung
function init_ajax(ah) { 
   ah = false;
   if(window.XMLHttpRequest)  // Mozilla, Safari,...
      ah = new XMLHttpRequest();
   else if(window.ActiveXObject) { // IE
      try {
         ah = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {
         try {
            ah = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (e) {}
      }
   }
   return(ah);
}


// Ende AJAX-Initialisierung

function do_http_get_request(ah, type, url, output_id) { 
   if(!ah)
      return false;
   ah.onreadystatechange = function() {
      if(ah.readyState == 4) {
         if(ah.status == 200) {
            if(type == "text") 
               handle_text_response(ah.responseText, output_id); 
            else if(type == "xml")
               handle_xml_response(ah, ah.responseXML, output_id);
         }
         else
            return false;
      }
      else
         return false;
   } 
   ah.open('GET', url, true);
   ah.send(null);
}
 
function do_http_post_request(ah, type, url, post_field_ids, output_id) {   
   if(!ah)
          return false;
   var post_field_array = post_field_ids.split("+");
   post_field_str = "";
   for(i = 0; i < post_field_array.length; i++) {
          post_field_id = post_field_array[i];
          post_field_value = document.getElementById(post_field_id).value;
      post_field_str += post_field_id + "=" + encodeURIComponent(post_field_value) + "&";
   }
   if(post_field_str.length > 1)
      post_field_str = post_field_str.substr(0, post_field_str.length - 1);
   ah.onreadystatechange = function() {
      if(ah.readyState == 4) {
         if(ah.status == 200) {
            if(type == "text")
               handle_text_response(ah.responseText, output_id);
            else if(type == "xml")
               handle_xml_response(ah, ah.responseXML, output_id);
                 }
             else
                return false;
      }
          else
             return false;
   } 
   ah.open('POST', url, true);
   ah.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   ah.send(post_field_str);
}
 
function do_http_get_request_plus(ah, type, url, output_id, js_code) { 
   if(!ah)
      return false;
   ah.onreadystatechange = function() {
      if(ah.readyState == 4) {
         if(ah.status == 200) {
            if(type == "text") 
               handle_text_response(ah.responseText, output_id); 
            else if(type == "xml")
               handle_xml_response(ah, ah.responseXML, output_id);
			eval(js_code);
	     }
         else
            return false;
      }
      else
         return false;
   } 
   ah.open('GET', url, true);
   ah.send(null);
}
 
function do_http_post_request_plus(ah, type, url, post_field_ids, output_id, js_code) {   
   if(!ah)
          return false;
   var post_field_array = post_field_ids.split("+");
   post_field_str = "";
   for(i = 0; i < post_field_array.length; i++) {
          post_field_id = post_field_array[i];
          post_field_value = document.getElementById(post_field_id).value;
      post_field_str += post_field_id + "=" + encodeURIComponent(post_field_value) + "&";
   }
   if(post_field_str.length > 1)
      post_field_str = post_field_str.substr(0, post_field_str.length - 1);
   ah.onreadystatechange = function() {
      if(ah.readyState == 4) {
         if(ah.status == 200) {
            if(type == "text")
               handle_text_response(ah.responseText, output_id);
            else if(type == "xml")
               handle_xml_response(ah, ah.responseXML, output_id);
            else if(type == "value")
               handle_value_response(ah, ah.responseXML, output_id);
            eval(js_code);

		 }
             else
                return false;
      }
          else
             return false;
   } 
   ah.open('POST', url, true);
   ah.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   ah.send(post_field_str);
}
 
function handle_text_response(content, output_id) {
   if(!document.getElementById)
      return false;
   if(!document.getElementById(output_id))
      return false;
   document.getElementById(output_id).innerHTML = content;
} 
 
function handle_xml_response(ah, content, output_id) {
   if(!document.getElementById)
      return false;
   if(!document.getElementById(output_id))
      return false;
   if(xml_response_handler)
      xml_response_handler(ah.responseXML, output_id);
}

function handle_value_response(content, output_id) {
   if(!document.getElementById)
      return false;
   if(!document.getElementById(output_id))
      return false;
   document.getElementById(output_id).value = content;
}

// ==================================================================================
// ============================ DIALOG SUPPORT ======================================
// ==================================================================================

var dlg_zindex = 10001;
var dlg_active_ids = new Array();
var dlg_active_id = "";
var dlg_wait = false;
var back_id = "content_wrap";
var dX = 250;
var dY = 150;
var X = 0;
var Y = 0;

document.onmousedown = dlg_mousedown;
function dlg_mousedown(e) {
   if (!e)
       e = window.event;
}

document.onkeydown = dlg_keydown;
document.onclick = dlg_click;

function dlg_keydown(e) {
   if (!e)
       e = window.event;
   if(e.which) {
      if(e.which == 27 && dlg_active_id.length > 0 && dlg_wait == false)
         dlg_hide(dlg_active_id);
   }
   else if(e.keyCode) {
      if(e.keyCode == 27 && dlg_active_id.length > 0 && dlg_wait == false)
         dlg_hide(dlg_active_id);
   }
}
function dlg_click(e) {
   if (!e)
       e = window.event;
   if(e.clientX) {
      X = e.clientX;
      Y = e.clientY;
   }
   else if(e.pageX) {
      X = e.pageX ;
      Y = e.pageY;
   }
}


function show_dlg(dlg_id, width, height) {
  if(!document.getElementById(dlg_id))
      return false;
   dlg_zindex += 1;
   var scrollLeft = 0;
   if(document.body.scrollLeft)
      scrollLeft = document.body.scrollLeft;
   var scrollTop = 0;
   if(document.body.scrollTop)
      scrollTop = document.body.scrollTop;
   document.getElementById(dlg_id).style.zIndex = dlg_zindex;
   document.getElementById(dlg_id).style.width = width + "px";
   document.getElementById(dlg_id).style.height = height + "px";
   document.getElementById(dlg_id).style.left = (X- width) + "px";
   document.getElementById(dlg_id).style.top = (Y - height) + "px";
   dlg_active_ids.push(dlg_id);
   dlg_active_id = dlg_id;
   document.getElementById(dlg_id).style.display = "block";

   return true;
}

function show_dlg_and_hide(dlg_id, width, height, hide_after) {
   show_dlg(dlg_id, width, height);
   dlg_wait = true;
   document.getElementById(dlg_id).style.cursor = "wait";
   window.setTimeout('dlg_hide("' + dlg_id + '")', hide_after);
}

function dlg_hide(dlg_id) {
   if(!document.getElementById(dlg_id))
      return false;
   if(dlg_active_ids.length > 0) {
      del_i = dlg_active_ids.length - 1;
      for(i = 0; i < dlg_active_ids.length; i++)
         if(dlg_active_ids[i] == dlg_id)
		    del_i = i;
      dlg_active_ids.splice(del_i, 1);
   }	    
   dlg_active_id = "";
   dlg_wait = false;
   if(dlg_active_ids.length == 0)
      document.getElementById(back_id).className = "body_normal";
   else
      document.getElementById(back_id).className = "body_dlg";
   document.getElementById(dlg_id).style.cursor = "auto";
   document.getElementById(dlg_id).style.display = "none";
}




// ==================================================================================
// ============================ ALERTS ==============================================
// ==================================================================================

function alert_on(msg) {
  o = document.getElementById('alert');
  o.innerHTML = msg;
  o.style.display = "block";
  blink_in('alert', 250, 4);
}
function alert_off() {
  o = document.getElementById('alert');
  o.innerHTML = "";
  o.style.display = "none";
}




// ==================================================================================
// ============================ MISC FUNCS ==========================================
// ==================================================================================

function hide(ID) {
   if(!document.getElementById) return;
    document.getElementById(ID).style.display = "none";	
}
function blank(ID) {
   if(!document.getElementById) return;
    document.getElementById(ID).innerHTML = " ";	
}
function clear(ID) {
   if(!document.getElementById) return;
    document.getElementById(ID).innerHTML = "";	
}
function unhide(ID, mode) {
   if(!document.getElementById) return;
    document.getElementById(ID).style.display = mode;	
}
function noval(ID) {
   if(!document.getElementById) return;
    document.getElementById(ID).value = "";	
}
function hide_after(ID, seconds) {
   if(!document.getElementById) return;
   window.setTimeout("hide('"+ID+"')", seconds * 1000);
}
function blank_after(ID, sec) {
   if(!document.getElementById) return;
   setTimeout("blank('"+ID+"')", sec * 1000);
}
function clear_after(ID, sec) {
   if(!document.getElementById) return;
   setTimeout("clear('"+ID+"')", sec * 1000);
}
function blink_in(obj, ms, times) {
  document.getElementById(obj).style.visibility = "visible";
  times = times - 1;
  if(times > 0) window.setTimeout("blink_out('"+obj+"', "+ms+", "+times+")", ms);
}
function blink_out(obj, ms, times) {
  document.getElementById(obj).style.visibility = "hidden";
  if(times > 0) window.setTimeout("blink_in('"+obj+"', "+ms+", "+times+")", ms);
}
function str_replace(str, search, replace) {
   var new_str = "";
   var i = 0;
   while(i < str.length) {
      if(str.substring(i, i + search.length) == search) {
         new_str = new_str + replace;
         i = i + search.length - 1;
      }
      else
         new_str = new_str + str.substring(i, i + 1);
   i++;
  }
  return new_str;
}
function getElementsByClassName(tag_name, class_name) {
   var obj, ret_obj = new Array(),j = 0;
   obj = document.getElementsByTagName(tag_name);
   for(i = 0; i < obj.length; i++) {
      if(obj[i].className == class_name) {
         ret_obj[j] = obj[i];
         j++
      }
   }
   return ret_obj;
}
function get_GET_params() {
   var GET = new Array();
   if(location.search.length > 0) {
      var get_param_str = location.search.substring(1, location.search.length);
      var get_params = get_param_str.split("&");
      for(i = 0; i < get_params.length; i++) {
         var key_value = get_params[i].split("=");
         if(key_value.length == 2) {
            var key = key_value[0];
            var value = key_value[1];
            GET[key] = value;
         }
      }
   }
   return(GET);
}
function get_GET_param(key) {
   var get_params = get_GET_params();
   if(get_params[key])
      return(get_params[key]);
   else
      return false;
}


// ==================================================================================
// ============================ Preloadings =========================================
// ==================================================================================

var buttonbig = new Image();
buttonbig.src = "/styles/de_default/buttonbig.gif";
var buttonbig_hover = new Image();
buttonbig_hover.src = "/styles/de_default/buttonbig-hover.gif";

