var onloadstack = new Array();
var ad_wallpaper, suggest_timer, push_timer, chat_timer, chat_timer_mod;
var mod_mids = new Array();
var push_stat = 0;

onerror = stopError;

function stopError() {
  return true;
}
function init() {
  set_ad();
  onload_func();
}
function onload_func() {
  for ( var i=0;i<onloadstack.length;i++ ) { 
    eval(onloadstack[i]); 
  } 
}
function tabs(elem, offset) {
  elem.className = 'tab_active';
  elem = elem.parentNode.parentNode.childNodes;
  for ( var i=0;i<elem.length;i++ ) {
    if ( i != offset ) { elem[i].firstChild.className = 'tab_inactive'; }
  }
}
function bookmark(title, url) {
  var browser = get_browser();
  if ( browser != false ) {
    if ( browser == 'mozilla' ) { window.sidebar.addPanel(title, url, ""); }
    else if (browser == 'ie') { window.external.AddFavorite(url, title); }
    else if (browser == 'opera') { error_message(); }
  }
}
function startpage(url) {
  var browser = get_browser();
  if ( browser != false && browser == 'ie' ) {
    document.body.style.behavior = 'url(#default#homepage)';
	if ( typeof document.body.setHomePage != 'undefined' ) { 
	  document.body.setHomePage(url);
	}
	else { error_message(); }
  }
  else { error_message() }
}
function error_message() {
  alert("Bitte benutzen Sie die Funktion ihres Browsers.");
}
function get_browser() {
  var BrowserName = navigator.appName;
  var BVersion = navigator.appVersion.match(/MSIE/) ? navigator.appVersion.replace(/^.*MSIE\s(.+?);.*$/, '$1') : parseFloat(navigator.appVersion);

  if ( BrowserName == "Opera" || BrowserName == "Netscape" || BrowserName == "Microsoft Internet Explorer" ) {
    if ( BrowserName == "Microsoft Internet Explorer" ) {
	  if ( BVersion >= 4 ) { return 'ie'; }
      else { return false; }
	}
    else if ( BrowserName == "Netscape" ) {
      if ( BVersion >= 5 ) { return 'mozilla'; }
      else { return false; }
    }
    else if ( BrowserName == "Opera" ) {
      if ( BVersion >= 5 ) { return 'opera'; }
      else { return false; }
    }
  }
  else { return false; }
}
function get_top(elem) {
  if ( !elem ) { return; }
  var parent_elem = elem.offsetParent;
  var pos = elem.offsetTop;
  while ( parent_elem ) {
    pos = pos + parent_elem.offsetTop;
    parent_elem = parent_elem.offsetParent;
  }
  return pos;
}
function get_left(elem) {
  if ( !elem ) { return; }
  var parent_elem = elem.offsetParent;
  var pos = elem.offsetLeft;
  while ( parent_elem ) {
    pos = pos + parent_elem.offsetLeft;
    parent_elem = parent_elem.offsetParent;
  }
  return pos;
}
function set_ad() {
  var ads = new Array('bigsize', 'sky', 'content', 'portrait', 'profil');
  if ( document.getElementById("ad_bigsize") ) {
    if ( ad_wallpaper == "1" ) { document.getElementById("ad_bigsize").style.textAlign = "right"; }
  }
  if ( document.getElementById("ad_sky") ) {
    if ( ad_wallpaper == "1" ) { 
      var pos_left = get_left(document.getElementById("ad_sky"));
	  document.getElementById("ad_sky").style.position = "absolute";	  
	  document.getElementById("ad_sky").style.top = "0px";
	  document.getElementById("ad_sky").style.left = pos_left + "px";
    }	  
  }
  for ( var i=0;i<ads.length;i++ ) {
    if ( document.getElementById("ad_" + ads[i]) ) { document.getElementById("ad_" + ads[i]).style.visibility = "visible"; }
  }
}
function suggest_init(no_reset) {
  if ( document.getElementById("suggest_div").style.display == "block" ) {
    document.getElementById("suggest_div").innerHTML = "";
    document.getElementById("suggest_div").style.display = "none";
  }
  if ( !no_reset && document.getElementById("suggest_input").value == "Name, WKN, ISIN" ) { document.getElementById("suggest_input").value = ""; }
}
function suggest_start(search) {
  if ( !search && document.getElementById("suggest_input") ) { search = document.getElementById("suggest_input").value; }
  search = escape(search).replace(/^(%0A|%20)/g, "");
  search = escape(search).replace(/(%0A|%20)$/g, "");
  if ( search != "" && search.length >= 1 ) {
    var post = "";
    post = "todo=suggest";
    post += "&search=" + search;
    request_data(post, "suggest", document.getElementById("suggest_input").value, "/data.php");
  }
  else { suggest_init(); }
}
function suggest_div_resize() {
  var elem_0 = document.getElementById("suggest_div");
  var elem_1 = document.getElementById("suggest_table");
  if ( elem_0 && elem_1 && elem_1.offsetWidth >= elem_0.offsetWidth ) {
    elem_0.style.width = elem_1.offsetWidth + "px";
  }
}
function addevents(obj, type, fn) {
  if ( obj.addEventListener ) { obj.addEventListener(type, fn, false); } 
  else if ( obj.attachEvent ) {
    obj['e' + type + fn] = fn;
    obj[type+fn] = function() { obj['e' + type + fn]( window.event ); }
    obj.attachEvent('on' + type, obj[type + fn] );
  }
}

function request_data(post, todo, param, url) {
  post += "&ref=" + window.location.pathname;
  var xmlhttp = null;
  if ( typeof XMLHttpRequest != 'undefined' ) { xmlhttp = new XMLHttpRequest(); }
  if ( !xmlhttp ) {
    try { xmlhttp  = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
      try { xmlhttp  = new ActiveXObject("Microsoft.XMLHTTP"); } 
      catch(e) { xmlhttp  = null; }
    }
  }
  if ( xmlhttp ) {
    xmlhttp.open("POST", url, true);
    xmlhttp.onreadystatechange = function() { if ( xmlhttp.readyState == 4 ) { load_data(xmlhttp,todo,param); } }
    xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xmlhttp.send(post);
  }
}
function load_data (xmlhttp,todo,param) {
  if ( xmlhttp && xmlhttp.status == 200 ) {
    var status_data;
    switch (todo) {
      case 'suggest':
        status_data = edit_request_stat(xmlhttp,"text");
        if ( status_data[0] == 'ready' ) {
          if ( param != document.getElementById("suggest_input").value ) { return; }
          status_data.shift();
		  var elem_0 = document.getElementById("suggest_div");
		  var elem_1 = document.getElementById("suggest_input").parentNode;
          elem_0.style.visibility = "hidden";
          elem_0.innerHTML = status_data;
          if ( elem_0.style.display != "block" ) {
            elem_0.style.top = (get_top(elem_1)+elem_1.offsetHeight) + "px";
			elem_0.style.width = elem_1.offsetWidth + "px";
            elem_0.style.left = (get_left(elem_1)-1) + "px";
            elem_0.style.display = "block";
          }
          suggest_div_resize();
          elem_0.style.visibility = "visible";
        }
        else if ( status_data[0] == 'error' ) { suggest_init(1); }
      break;
      case 'log':
        status_data = edit_request_stat(xmlhttp,"text");
		if ( document.getElementById('log_container') ) {
		  document.getElementById('log_container').innerHTML += status_data[0];
		  window.scrollTo(0, document.getElementById('log_container').offsetHeight);
		}
      break;
      case 'push':
	    status_data = edit_request_stat(xmlhttp,"text");
        if ( status_data[0] == 'ready' ) {
	      status_data.shift();
		  for ( var i=0;i<param.length;i++ ) {
		    var elem = document.getElementById(param[i]);
		    if ( !elem || status_data[i] == '' || status_data[i] == '-' ) { continue; }
			var elem_id = param[i].split('-');
			if ( elem_id[2] == 'p' ) {
			  elem.firstChild.src = '/images/' + status_data[i] + '.gif';
			}
			else {
			  if ( elem_id[2] == 'v' ) {
			    var value_old = document.getElementById(param[i]).innerHTML.replace(/\./, '');
				value_old = value_old.replace(/,/, '.');
				var value_new = status_data[i].replace(/\./, '');
				value_new = value_new.replace(/,/, '.');
				if ( value_old != '' && value_old != '-' && value_new != '' && value_old != '-' && value_old != value_new ) {
				  document.getElementById(param[i]).className += ' ' + (Math.max(value_old, value_new) == value_new ? 'bg_green' : 'bg_red');
				}
			  }
			  document.getElementById(param[i]).innerHTML = status_data[i];
			}
		  }
	    }
		push_timer = window.setTimeout('push_data()', 2000);
      break;	
      case 'get_messages':
	    status_data = edit_request_stat(xmlhttp,"text");
        if ( status_data[0] == 'ready' ) {
	      status_data.shift();
		  var mod = status_data.shift();
		  var messages = status_data.shift().split('#');
		  var chatter = status_data.shift().split('#');
		  if ( param[1] == 0 ) {
		    var elem_chat = document.getElementById("chat").firstChild.firstChild;
		    var del_messages = status_data.shift().split('#');
		  }
		  if ( param[1] == 1 ) {
		    var elem_chat_mod = document.getElementById("chat_mod");
		    if ( elem_chat_mod ) { elem_chat_mod = elem_chat_mod.firstChild.firstChild; }
		  }
		  var elem_chat_lobby = document.getElementById("chat_lobby");
		  for ( var i=0;i<messages.length;i++ ) {
		    var m_data = messages[i].split(';');
			if ( !m_data[0] ) { continue; }
			if ( param[1] == 0 ) {
			  var tr = document.createElement('tr');
			  tr.id = 'mid' + m_data[0];
			  var td_1 = document.createElement('td');
			  td_1.setAttribute('vAlign', 'top');
			  td_1.setAttribute('Align', 'left');
//			  td_1.setAttribute('noWrap', 1);
			  if ( m_data[4] == '1' ) { td_1.className = 'mod'; }
			  td_1.innerHTML = '<span class="small_fnt"><u>' + m_data[2] + ' ' + m_data[1] + '</u></span><br>' + m_data[3];
			  tr.appendChild(td_1);
			  elem_chat.appendChild(tr);
			}
			if ( mod == '1' && param[1] == 1 ) {
			  tr = document.createElement('tr');
			  tr.id = 'mod_mid' + m_data[0];
			  td_1 = document.createElement('td');
			  td_1.setAttribute('vAlign', 'top');
			  td_1.setAttribute('Align', 'left');
			  td_1.setAttribute('noWrap', 1);
			  if ( m_data[4] == '1' ) { td_1.className = 'mod'; }
			  td_1.innerHTML =  '<span class="mod_tools"><input type="checkbox" onclick="mod_mids.push(new Array(' + m_data[0] + ', this));" ' + (m_data[6] == '1' ? 'checked' : '') + '></span> <span class="small_fnt"><u>' + m_data[2] + ' ' + m_data[1] + '</u></span><br>' + m_data[3];
			  tr.appendChild(td_1);
			  elem_chat_mod.appendChild(tr);
			}
		  }
		  if ( param[1] == 0 ) {
		    for ( var i=0;i<del_messages.length;i++ ) {
		      del_elem = document.getElementById("mid" + del_messages[i]);
		      if ( del_elem ) {
			    del_elem.parentNode.removeChild(del_elem);
			  }
		    }
		  }
		  if ( param[1] == 0 ) { elem_chat.parentNode.parentNode.scrollTop = elem_chat.parentNode.offsetHeight; }
		  else if ( param[1] == 1 ) { elem_chat_mod.parentNode.parentNode.scrollTop = elem_chat_mod.parentNode.offsetHeight; }
		  elem_chat_lobby.innerHTML = '<br><div>' + chatter.join('</div><div>') + '</div>';
		  if ( param[0] == 1 ) { 
		    if ( param[1] == 0 ) { chat_timer = window.setTimeout('get_messages(1, ' + param[1] + ')', 5000); }
		    else if ( param[1] == 1 ) { chat_timer_mod = window.setTimeout('get_messages(1, ' + param[1] + ')', 2000); }
		  }
		}
      break;
      case 'send_message':
	    param.value = '';
		param.focus();
        //get_messages();
      break;
      case 'edit_messages':
        status_data = edit_request_stat(xmlhttp,"text");
        if ( status_data[0] == 'ready' ) {
		  return;
		}
//		else { param.checked = param.checked === true ? false : true; }
      break;	  
	}
  }
}
function edit_request_stat(xmlhttp,todo) {
  if ( todo == "text" ) {
    var status_data = xmlhttp.responseText;
    status_data = status_data.split("*");
    for ( var i=0;i<status_data.length;i++ ) {
      if ( escape(status_data[i].substring(status_data[i].length-1,status_data[i].length)) == "%0A" ) status_data[i] = status_data[i].substring(0,status_data[i].length-1);
      if ( escape(status_data[i].substring(0,1)) == "%0A" ) status_data[i] = status_data[i].substring(1,status_data[i].length);
    }
  }
  return status_data;
}
function filter_v(value) {
  var elem_filter = document.getElementById('filter_m');
  var elem_emi = document.getElementById('emi');
  if ( value.match(/^kag_/) ) {
    elem_emi.style.display = 'block';
	elem_filter.style.display = 'none';
  }
  else {
    elem_filter.style.display = 'block';
	if ( elem_emi ) { elem_emi.style.display = 'none'; }
  }  
}
function push_data(user) {
  window.clearTimeout(push_timer);
  var button = document.getElementById("push_button");
  var elems = document.getElementsByTagName("td");
  var push_list = new Array();
  for ( var i=0;i<elems.length;i++ ) {
    if ( elems[i].className.match(/^dynamic/) ) {
      elems[i].className = 'dynamic';
	  push_list.push(elems[i].id); 
	}
  }
  if ( user && button ) {
    if ( push_stat == 0 ) {
	  button.innerHTML = "Push<br>stoppen";
	  push_stat = 1;
	}
	else {
	  button.innerHTML = "Push<br>starten";
	  push_stat = 0;
	  return;
	}
  }
  if ( push_list.length > 0 ) {
    var post = "todo=push";
    post += "&list=" + push_list.join(';');
    request_data(post, "push", push_list, "/data.php");
  }
}
function init_chat(active, mod) {
  if ( active == 1 ) {
//    var elem = document.getElementById("message");	
//	if ( elem ) { addevents(elem, 'keyup', send_message); } 
    document.getElementById("chat_send").onclick = function() { send_message(); }
	document.getElementById("chat_del").onclick = function() { document.getElementById("message").value = ''; }
	if ( mod == 1 ) { document.getElementById("chat_go").onclick = function() { edit_message(); } }
  }
  if ( mod == 1 ) { get_messages(active, 0); }
  get_messages(active, mod);
}
function send_message(e) {
//  if ( !e && window.event ) { e = window.event; }
//  var key = e.keyCode;
  var elem = document.getElementById("message");
//  if ( key == 13 ) {
    var post = "todo=send_message";
    post += "&message=" + elem.value;
	post += post_sw();
    request_data(post, "send_message", elem, "/data.php");
//  }
}
function get_messages(active, mod) {
  if ( mod == 0 ) { window.clearTimeout(chat_timer); }
  else if ( mod == 1 ) { window.clearTimeout(chat_timer_mod); }
  var elem_id = mod == 1 ? "chat_mod" : "chat";
  var elem = document.getElementById(elem_id).firstChild.firstChild;
  var last_id = new Array();
  for ( var i=0;i<elem.childNodes.length;i++ ) {
    if ( elem.childNodes[i].id ) {
	  last_id.push(mod == 1 ? elem.childNodes[i].id.replace(/mod_mid/, '') : elem.childNodes[i].id.replace(/mid/, ''));
	}
  }
  last_id = last_id.length > 0 ? last_id.join(';') : '';
  var post = "todo=get_messages";
  post += "&last_id=" + last_id;
  post += "&mod=" + mod;
  post += post_sw();
  request_data(post, "get_messages", new Array(active, mod), "/data.php");
}
function post_sw() {
  var found = window.location.search.match(/.+?(user_id=.+?)\&(hash_id=.+?)$/);
  if ( found ) {
    return "&" + found[1] + "&" + found[2];
  }
  
  return '';
}
function edit_message() {
  if ( mod_mids.length <= 0 ) { return; }
  var post = "todo=edit_messages";
  var mids_out = new Array();
  var stat;
  for ( var i=0;i<mod_mids.length;i++ ) {
    if ( document.getElementById("mod_action").checked === true ) { stat = mod_mids[i][1].checked === true ? "1" : "0"; }
	else { stat = mod_mids[i][1].checked === true ? "0" : "1"; }
    mids_out.push("mids[" + mod_mids[i][0] + "]=" + stat);
  }
  mod_mids = new Array();
  post += "&" + mids_out.join("&");
  request_data(post, "edit_messages", "", "/data.php");
}

