var dir   = "";
var style = "mysql";
var size = 16;
var right = 0;
var htmlright =2;
var bbcode = true;
var shown =false;
var maxLength = 1000;
var wsize = getWindowSize();

//body onload
function init()
{
  loadPage('xml/action.xml','xsl/action.xsl','tab');
  loadPage('xml/action.xml','xsl/menu.xsl','menu');
  loadHistory();
  translate();
}
function alert(txt)
{
  visible('popup');
  setText('popupContent','<div align="center">'+txt+'<br/><input  type="submit" id="confirmButton" value="Ok"/></div>');
  var node = $("confirmButton");
  node.addEventListener('click', function (evt){
    hide('popup');
  });
}
window.onerror = logError;
function logError(message, file, line)
{
  console.log("Message: "+message+"file: "+file+"line: "+line);
}
function confirm2(txt,sub,arg,arg2,arg3)
{
  visible('popup');
  var sleep = true;
  setText('popupContent','<b>'+txt+'</b><div align="right" style="padding:0.4em;"><input type="submit" name="cancelButton" id="cancelButton" value="Cancel"/>&#160;<input  type="submit" id="confirmButton" value="Ok"/></div>');
  var node = $("confirmButton");
  node.addEventListener ('click',function (evt){
    hide('popup');
    sub(arg,arg2,arg3);
  });
  var node2 = $("cancelButton");
  node2.addEventListener ('click',function (evt){
    hide('popup');
    evt.stopPropagation();
  });
}
function errorPopUp(txt,sub,arg,arg2,arg3)
{
  visible('popup');
  var sleep = true;
  setText('popupContent','<b>'+txt+'</b><div align="right" style="padding:0.4em;">&#160;<input  type="submit" id="confirmButton" value="Ok"/></div>');
  var node = $("confirmButton");
  node.addEventListener ('click',function (evt){
    hide('popup');
    sub(arg,arg2,arg3);
  });
}
function checkForm(form)
{
  var selectElement = form.querySelectorAll('input');
  var ret = true;
  for (i = 0; i < form.length; i++) {
    if(selectElement[i]){
    var value = selectElement[i].value;
    var regexp = selectElement[i].dataset.regexp;
    if(regexp){
      if(eval('value.match('+regexp+')')){
	  selectElement[i].style.borderColor ='green';
	  selectElement[i].title = selectElement[i].dataset.right;
      }else{
	  selectElement[i].style.borderColor ='red';
	  selectElement[i].title = selectElement[i].dataset.error;
	  ret = false;
      }
     }
    }
  }
  return ret;
}
function setCurrentTab(id,title)
{
  var body   = document.getElementsByTagName('body')[0];
  var nodes   = body.getElementsByTagName("td");
  for(var i=0,j=nodes.length; i<j; i++){
      if(nodes[i].className =='headerItemHover' ){
	nodes[i].className = 'headerItem';
	nodes[i].firstChild.className ='menuLink';
      }
  }
  var entry = $(id);
  var entryDynamic = $('dynamicTab');
  if(entry){
    entry.className            = 'headerItemHover';
    entryDynamic.style.display = 'none';
    entryDynamic.className     = 'headerItem';
    var body      = document.getElementsByTagName('body')[0];
    var nodes   = body.getElementsByTagName("td");
    for(var i=0,j=nodes.length; i<j; i++){
	if(nodes[i].className =='headerItemHover' ){
	  nodes[i].className = 'headerItem';
	  if(nodes[i].firstChild)
	  nodes[i].firstChild.className ='menuLink';
	}
    }
    var entry = $(id);
    var entryDynamic = $('dynamicTab');
    if(entry){
      entry.className ='headerItemHover'
      entryDynamic.style.display ='none';
      entryDynamic.className = 'headerItem';
      if(entry.firstChild)
	entry.firstChild.className ='dynamicLink';
    }else if(title){
      entryDynamic.style.display ='';
      entryDynamic.className     = 'headerItemHover'
      entryDynamic.innerHTML     = '<a class="dynamicLink">'+translate(title)+'</a>';
    }
    if(entry.firstChild)
      entry.firstChild.className = 'dynamicLink';
  }else if(title){
    entryDynamic.style.display   = '';
    entryDynamic.className       = 'headerItemHover'
    entryDynamic.innerHTML       = '<a class="dynamicLink">'+translate(title)+'</a>';
  }
}
var menuAktive = false;
function menu(){
      var ao = getWindowSize();
      if(!menuAktive){
	$('menu').classList.remove('closed');
	visible('menuContent');
	visible('closeMenu');
	$('menu').width= sidebarX;
	$('con').width = (ao.x-ScroBarWidth() - sidebarX )+'px';
	menuAktive = true;
      }
      var node = $("closeMenu");
      node.addEventListener ('click', function (evt){
	if(menuAktive){
	  hide('menuContent');
	  hide('closeMenu');
	  $('con').width= (wsize.x -5) +"px";
	  $('menu').width='5px';
	  $('menu').classList.add('closed');
	  menuAktive = false;
	}
	evt.stopPropagation();
      }
      );
}
function closeMenu()
{
  hide('menuContent');
  $('menu').classList.add('closed');
  menuAktive = false;
}
function showTab(id)
{
  $(id).classList.remove('closed');
  if($(id).firstChild)
    $(id).firstChild.style.display ='';
  var elements = document.getElementsByClassName('cnt');
  for(var i = 0; i < elements.length;i++){
    if(elements[i].id != id && elements[i].style.display ==''){
      if(elements[i].firstChild)
	    elements[i].firstChild.style.display ='none';
      elements[i].classList.add('closed');
    }
  }
}
var bSidebarResize = false;
var sidebarX = 300;
document.onmousemove = resizeSidebar;
document.onmousedown = startResize;
document.onmouseup = stopResize;
function startResize(EVENT){
  var posX = document.all ? window.event.clientX : EVENT.pageX;
  if(sidebarX >  posX -5  &&  sidebarX   <  posX +5  )
  {
    $('menu').style.cursor="ew-resize";
    bSidebarResize =  true;
    $('menu').classList.add('noselect');
    $('menu').setAttribute('unselectable','on');
  }
}
function stopResize(EVENT){
  bSidebarResize = false;
  $('menu').style.cursor="";
  $('menu').classList.remove('noselect');
  $('menu').setAttribute('unselectable','off');
  drop(EVENT);
}
function resizeSidebar(EVENT){
  posX = document.all ? window.event.clientX : EVENT.pageX;
  if(bSidebarResize && posX > 5  && posX  <  500)
  {
    $('menu').width= posX+'px';
    sidebarX = posX;
    var ao = getWindowSize();
    $('con').width = (ao.x-ScroBarWidth() - posX )+'px';
  }
  if(sidebarX >  posX -5  &&  sidebarX   <  posX +5  )
  {
      $('menu').style.cursor="ew-resize";
  }
  else
  {
      $('menu').style.cursor= "";
  }
  drag(EVENT);
}
function loadPage(inXml,inXsl,outId,tabId,title)
{
  xml = loadXMLDoc(inXml);
  xsl = loadXMLDoc(inXsl);	
	  
  if (window.ActiveXObject || xhttp.responseType == "msxml-document"){
    ex = xml.transformNode(xsl);
    $(outId).innerHTML = ex;
  }else if (document.implementation && document.implementation.createDocument){
    xsltProcessor = new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    resultDocument = xsltProcessor.transformToFragment(xml, document);
    node = $(outId);
    setText(outId,'');
    if(node.childNodes[0])
      node.removeChild(node.childNodes[0]);
    if(node)
      node.appendChild(resultDocument);
  }
  if(tabId && title)
      setCurrentTab(tabId,title);
  disableOutputEscaping('content');
}
function markInput(bool)
{
  var body  = document.getElementsByTagName("body")[0];
  var node = body.getElementsByTagName("input");
  for(var i=0,j=node.length; i<j; i++)
  if(node[i].className == 'markBox') 
    node[i].checked = bool;
  visible (bool ? 'umarkAll' : 'markAll' );
  hide (bool ? 'markAll' : 'umarkAll' );
}
function markTables(bool)
{
  var body  = document.getElementsByTagName("body")[0];
  var node = body.getElementsByTagName("option");
  for(var i=0,j=node.length; i<j; i++)
  if(node[i].className == 'table') 
    node[i].selected = bool;
  visible (bool ? 'umarkAll2' : 'markAll2' );
  hide (bool ? 'markAll2' : 'umarkAll2' );
}
var nCurrentShown;
function DisplayTable(id)
{
  hide( nCurrentShown);
  visible(id);
  nCurrentShown = id;
}
var aCurrentShown = new Array();
function DisplayTables(i,id)
{
    hide( aCurrentShown[i] );
    aCurrentShown[i] = id;
    visible(id);
}
function setIndexType(type)
{
    if(type == 'FOREIGN KEY')
      hideForeign( false );
    else
      hideForeign( true );
}
function hideForeign(bHide)
{
    var body  = document.getElementsByTagName("body")[0];
    var node = body.getElementsByTagName("td");
    for(var i=0,j=node.length; i<j; i++){
      var a =  node[i].classList ;
	for(var k=0,l=node.length; k<l; k++){
	  if(a[k] == 'foreign')
	    node[i].style.display  =  bHide ? 'none' :'';
	}
    }
}
function DisplayKeyWords(b)
{
  if(b){
    $('akeywods').className = 'currentLink';
    $('afieldNames').className = 'link';
    hide('divTables');
    hide( nCurrentShown);
    visible('selKeyword');
    $('selKeyword').focus();
  }else{
    $('akeywods').className = 'link';
    $('afieldNames').className = 'currentLink';
    hide('selKeyword');
    visible('divTables');
    $('divTables').focus();
  }
}
function setText(id,string)
{
  var element = $(id);
  if(element)
    element.innerHTML = string;
  else
    window.status = id+string;
}
function getText(id)
{
  var element = $(id);
    if(element)
      return element.innerHTML;
}
function hide(id)
{
  if($(id))
      $(id).style.display = "none";
}
function visible(id)
{
       if($(id))
	  $(id).style.display = "";
}
function intputMask(id ,regexp)
{
  var rxObj = regexp;
  rxObj.exec($(id).value);
  $(id).value = RegExp.$1;
}
var cAction ='ShowTables';
function setAction(n)
{
       cAction = n;
}
var tmpTxt;
var tmpID;
function showPopup(id,hideCloseButton)
{
    visible('popup');
    if(hideCloseButton)
      hide('closeButton');
    else
      visible('closeButton');
    tmpTxt = getText(id);
    tmpID = id;
    setText(id,'');
    setText('popupTitle',translate(id));
    setText('popupContent',tmpTxt);
    evalId(id);
}
function closePopup()
{
    setText(tmpID,tmpTxt);	
    hide('popup');
    $('popupContent1').style.left='25%';
    $('popupContent1').style.width='50%';
}
var currentId, currentTxt;
var act ='save';
function submitForm(node,tabId,tabTitle,bHistory,method,uri)
{
    bHistory = typeof bHistory !== 'undefined' ? bHistory : true;
    method   = typeof method   !== 'undefined' ? method   : 'POST';
    var url  = typeof uri      !== 'undefined' ? uri      : "cgi-bin/mysql.pl?";
    if(checkForm(node)){
      var formData = new FormData();
      for(var i =0; i < node.elements.length;i++){
	if(node.elements[i].type == 'checkbox' || node.elements[i].type == 'radio'){
		if(node.elements[i].checked){
		  if(method == 'POST')
		    formData.append(node.elements[i].name,node.elements[i].value);
		  else
		    url += node.elements[i].name+"="+encodeURIComponent(node.elements[i].value)+"&";
		}
	}else if(node.elements[i].name == 'submit'){
	    if(method == 'POST')
	      formData.append(node.elements[i].name,act);
	    else
	      url += node.elements[i].name+"="+encodeURIComponent(act)+"&";
	}else if(node.elements[i].type == 'file'){       
	      var file = node.elements[i];
	      if(method == 'POST')
		formData.append(file.name, file.files[0]);
	}else if(node.elements[i].type == 'select-multiple'){
	    for (var j=0, len=node.elements[i].options.length; j<len; j++) {
	      var  opt = node.elements[i].options[j];
	      if ( opt.selected ){
		  if(method == 'POST')
		    formData.append(node.elements[i].name,node.elements[i].options[j].value);
		  else
		    url += node.elements[i].name +"="+ encodeURIComponent(node.elements[i].options[j].value)+"&";
	      }
	    }
	}else{
	  if(method == 'POST')
	    formData.append(node.elements[i].name,node.elements[i].value);
	  else
	    url += node.elements[i].name+"="+encodeURIComponent(node.elements[i].value)+"&"; 
	}
      }
      requestURI(url,tabId,tabTitle,bHistory,formData,method);
  }
}
var http_request = false;
var oldpage = 0;
//requestURI('cgi-bin/mysql.pl?action=loadSidebar',action,action,false,false,'GET',false)
function requestURI(url,id,txt,bHistory,formData,method,bWaid)
{
  setAction(id);
      closePopup();
      if(txt)
	window.document.title = translate(txt);
      bHistory = typeof bHistory !== 'undefined' ? bHistory : true;
      method   = typeof method   !== 'undefined' ? method   : 'GET';
      if(bHistory && method == 'GET'){
	history.pushState(null, '','?'+url);
	action = id;
      }
      if( bWaid )
      {
	var waid = getText('animationContainer');
	oldpage = '';
	oldpage = getText('content');
	var w = '<div class="waid" >'+waid+'</div>';
	setText('content',w);
      }
      http_request = false;

      if (window.XMLHttpRequest) { //Firefox
	  http_request = new XMLHttpRequest();
	  if (http_request.overrideMimeType)
	      http_request.overrideMimeType('text/xml');
      } else if (window.ActiveXObject) { //IE
	  try {
	      http_request = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
	      try {
		  http_request = new ActiveXObject("Microsoft.XMLHTTP");
	      } catch (e) {}
	  }
      }
      if($(id))
	setCurrentTab(id,txt);			
      else if(txt)
	setCurrentTab('dynamicLink',txt);	
      http_request.onreadystatechange = setContent;
      if(method != 'POST'){
	http_request.open('GET', url, true);
	http_request.send(null);
      }else{
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-Type", "multipart/form-data");
	http_request.send(formData);
      }
}

function loadXMLDoc(filename)
{
    if (window.ActiveXObject){
	    xhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }else{
	    xhttp = new XMLHttpRequest();
    }
    xhttp.open("GET", filename, false);
    xhttp.overrideMimeType("text/xml");
    try {
      xhttp.responseType = "msxml-document"
      
    } catch(err) {
      alert(err);
    }
    xhttp.send(null);
    return xhttp.responseXML;
}

function setContent()
{
    if (http_request.readyState == 4) {
	if (http_request.status == 200) {
	  response      = http_request.responseXML;
	  for(var i = 0; i < response.getElementsByTagName('output').length; i++){
	    var outID = response.getElementsByTagName('output')[i].getAttribute('id');
	    var txt  = response.getElementsByTagName('output')[i].textContent;
	    if (navigator.userAgent.indexOf("Firefox")!=-1)
		txt = txt.replace("<![CDATA[", "").replace("]]>", "");
	    setText(outID,txt);
	    evalId(outID);
	  }
	}
	else
	{
	  if(oldpage.length > 0){
	      setText('content',oldpage);
	      setCurrentTab(currentId, currentTxt);
	  }else{
	    errorPopUp('<div style="overflow:auto">'+http_request.status + " " +http_request.responseText+"</div>",errorMessage);
	  }
	  return false;
	}
     var closeButton = $("closeButton");
      if((!document.cookie.match('sid') || document.cookie.match('sid=123'))  && window.location.search.match('mysql.pl')  )
      {
	showPopup('login',true);
	evalId('popupContent');
      }
      else
      {
	  closeButton.addEventListener ('click',closePopup);
      }
    }

}

function errorMessage(text){
   location.href = "install.html";
}
function evalId(id)
{
    var content  = $(id);
    if(content){
      var node = content.getElementsByTagName("script");
      for(var i=0,j=node.length; i<j; i++){
	if(node[i] && node[i].childNodes[0])
	  eval(node[i].childNodes[0].nodeValue);
      }
    }
}
var action;
window.onpopstate =loadHistory;
function loadHistory()
{
    var rxObj =  new RegExp(/action=([^&]+)/);
    rxObj.exec(window.location.search);
    action = RegExp.$1;
    var rxObj2 =  new RegExp(/(\?[^\?]+$)/);
    rxObj2.exec(window.location.search);
    var uri = RegExp.$1;
    var url = window.location.search;
    if(uri && action){
	requestURI('cgi-bin/mysql.pl'+uri,action,action,false);
    }else
      requestURI('cgi-bin/mysql.pl',translate('defaultaction'),translate('defaultaction'));
    return true;
}
function translate(string)
{
	var lng = navigator.language.indexOf("de") > -1 ? 'de':'en';
	if(typeof Lang != 'undefined')
	{
	  var l = new Lang(); 
	  if(string){
	    var ret = eval('l.'+lng+string.toLowerCase());
	    return ret ? ret : string;
	  }
	  else
	  {
	    traversTranslate('tab1',lng,l);//todo
	    traversTranslate('tabwidget',lng,l);
	  }
	}
	else
	{
	  return string;
	}
}
function traversTranslate(id,lng,l)
{
    if($(id)){
    var node = $(id).getElementsByTagName("a");
    for(var i=0,j=node.length; i<j; i++){
      if(node[i] && node[i].childNodes[0]){
      var ret =  eval('l.'+lng+node[i].childNodes[0].nodeValue.toLowerCase());
	if(ret)
	  node[i].childNodes[0].nodeValue  = ret;
	}
      }
    }
}
function disableOutputEscaping(id)
{
  if (navigator.userAgent.indexOf("Firefox")!=-1)
      $(id).innerHTML = $(id).textContent;
}
function $(id)
{
    return document.getElementById(id);
}
var autocomplete = new Array("select","from","where","insert","set","update");
var clickA;
var oldDate;
var oldTime;
function breakOut()
{
  clickA = clickA ?false:true;
  if(clickA)
  {
    oldDate = new Date();
    oldTime = oldDate.getTime();
    return false;
  }
  else
  {
    var date = new Date();
    var time = date.getTime();
    return (time - oldTime > 1000) ? false : true;
  }
}
function Autocomplete(evt)
{
  if(breakOut())
      return;
  if (evt.which == 8 ||evt.which == 16||  evt.which === 37 || evt.which === 38 ||evt.which === 39 || evt.which === 39 || evt.which === 40  )
    return;
    var offset = 3;
    while($("sqlEdit").selectionStart-offset >= 0 && offset < 10){//todo port to IE
	  var  match = ''
	  if( $("sqlEdit").value.substr($("sqlEdit").selectionStart-offset-1, 1) ==' ' ||$("sqlEdit").value.substr($("sqlEdit").selectionStart-offset-1, 1).search(new RegExp("\\b")) == -1|| $("sqlEdit").selectionStart-offset == 0){
	      match = $("sqlEdit").value.substr($("sqlEdit").selectionStart-offset, offset);
	  }
	  var i = 0;
	  var j = autocomplete.length;
	  while( i < autocomplete.length){
		if(match == autocomplete[i].substr(0,offset)){
		  var selectionStart = $("sqlEdit").selectionStart;
		  var before = $("sqlEdit").value.substr(0,selectionStart-offset);
		  var behind = $("sqlEdit").value.substr(selectionStart,$("sqlEdit").value.length);
		  $("sqlEdit").value = before+autocomplete[i]+behind;
		  $("sqlEdit").selectionStart = selectionStart;
		  $("sqlEdit").selectionEnd   = selectionStart+autocomplete[i].length-offset;
		  break;
		}
		i++;
	}
	offset++;
      }
}
function selKeyword(id)
{
  for(var i= 0; i<  $(id).options.length;i++ )
      autocomplete.push( $(id).options[i].value);
}
var uno = true;
function showSQLEditor()
{
  $('popupContent1').style.left='5%';
  $('popupContent1').style.width='90%';
  showPopup('SqlEditor');
  $("sqlEdit").addEventListener ('keyup', Autocomplete);
  if(uno){
    selKeyword('selKeyword');
    selKeyword('tablelist');
    for(var i= 0; i<  $('tablelist').options.length;i++ )
	selKeyword($('tablelist').options[i].value);
  }
  uno = false;
}

function ShowNewRow()
{
  $('popupContent1').style.left='2%';
  $('popupContent1').style.width='96%';
  showPopup('ShowNewRow');
}

function insertAtCursorPosition(txt)
{
       var textarea  = $('sqlEdit');
       if(typeof document.selection != 'undefined'){
              range = document.selection.createRange();
              var txt = range.text;
	      range.text = txt;
	      range.moveStart('character', txt.length );
              range.select();
       }else if(textarea.selectionStart &&(textarea.selectionEnd == textarea.selectionStart)){//insert at gecko
              var ia = textarea.selectionStart;
              var a = textarea.value.substring(0,ia);
              var b = textarea.value.substring(ia,textarea.value.length);
              textarea.value = a+txt+b;
       }else{
             textarea.value += txt;	
       }
}
var nCurrentRow = 0;
function enter(event){
  var keyCode = event.keyCode ? event.keyCode :event.charCode ? event.charCode :event.which;
  if (keyCode == 13) 
  return true;
  else
  return false;
}
function setAll(){
       var body  = document.getElementsByTagName("body")[0];
       var node = body.getElementsByTagName("option");
       for(var i=0,j=node.length; i<j; i++)
       if(node[i].className == 'set') 
	 node[i].selected = true;
}
function editSet(input,select){
  $(input).value = select.options[select.options.selectedIndex].value;
  nCurrentRow = select.options.selectedIndex;
}
function setEnter(input,select)
{
  if(!$(select).options[nCurrentRow] ){
    addEntry(select,input.id);
  }
  $(select).options[nCurrentRow].value = input.value;
  $(select).options[nCurrentRow].text = input.value;
}
function deleteEntry (idSelect)
{
  $(idSelect).options[$(idSelect).options.selectedIndex] = null;
}
function clearSelect (idSelect)
{
     $(idSelect).innerHTML ='';
     nCurrentRow = 0;
}
function addEntry(idSelect,idEdit) {
  newEntry = new Option('', '', false, true);
  var n = $(idSelect).length;
  $(idSelect).options[n] = newEntry;
  $(idSelect).options[n].className ='set';
}
function displayTree(id){
  var e = $(id);
  if(!e)return;
  var display = e.style.display;
  if(display == "none"){
	e.style.display = "";
  }else if(display == ""){
	e.style.display = "none";
  }
}
// object.x - .y =  getElementPosition(id);
function getElementPosition(id)
{
  var node = $(id);
  var offsetLeft = 0;
  var offsetTop  = 0;
  while (node){
    offsetLeft += node.offsetLeft;
    offsetTop += node.offsetTop;
    node = node.offsetParent;
  }
  var position = new Object();
  position.x = offsetLeft;
  position.y = offsetTop;
  return position;
}
function move(id,x,y)
{
  Element = $(id);
  Element.style.position = "absolute";
  Element.style.left = x + "px";
  Element.style.top  = y + "px";
}
var openMenu;
function showMenu(linkId,menuId)
{
  if(menuId != openMenu) hide(openMenu);
  openMenu = menuId;
  var oLink = $(linkId);
  var oMenu = $(menuId);
  var o = getElementPosition(linkId);
  var ao = getWindowSize();
  var c = (( o.x +  oLink.offsetWidth  )- oMenu.offsetWidth );
  c = c > 0 ? c : o.x-1;
  move(menuId,ao.x > o.x+ oMenu.offsetWidth ?  o.x-1  : c+1  ,o.y+oLink.offsetHeight );
  displayTree(menuId);
}
function getWindowSize()
{
  var nWidth = 0, nHeight = 0;
  var o = new Object;
  if( typeof( window.innerWidth ) == 'number' ) {//Gecko
	o.x = window.innerWidth;
	o.y =  window.innerHeight;
	return o;
  } else if( document.documentElement &&  document.documentElement.clientWidth && document.documentElement.clientHeight  ) {//Ie
	o.x = document.documentElement.clientWidth;
	o.y = document.documentElement.clientHeight;
	return o;
  }
  o.x = 0;
  o.y = 0;
  return o;
}
function ChangeToolTip(sId,sDataTyp){
 $(sId).title = translate(sDataTyp);
}
var pnPageY = 0;
function scrollToTop()
{
  pnPageY =  window.pageYOffset;
  var node = $('topUp');
  window.setTimeout(pageUp,1);
}
function pageUp()
{
  pnPageY-=250;
  window.scrollTo(0,pnPageY);
  if(pnPageY > 0)
  window.setTimeout(pageUp,1);
}

// function maxMin(id){
//        $(id).style.width = "";
//        if($(id).style.position == "absolute" ){
//               if($(id).className == 'fmin' || $(id).className == 'min'){
//                      $(id).className ='fmax';
//                      move(id,0,window.pageYOffset);
//                      $(id).style.width =wsize.x-ScroBarWidth()+"px";
// 
//               }else{
//                      $(id).className = 'fmin';
//                      move(id,(wsize.x/100)*10-ScroBarWidth(),window.pageYOffset+(wsize.x/100)*5);
//                      $(id).style.width = "80%";
//               }
//        }else{
//               if($(id).className == 'fmax' || $(id).className == 'max'){
//                      $(id).className ='min';
//               }else{
//                      $(id).className ='max';
//               }
//        }
// }

function undock(id){
       var wname = id;
       var element = $(wname);
       var caption = $("tr"+id);
       var pos     = element.style.position;
       var o       = getElementPosition(wname);

       if(pos == "absolute"){
              element.style.position ="";
              caption.style.cursor ="pointer";
              element.style.width = (element.className == 'fmax' || element.className == 'max' ) ? "100%":"90%";
       }else{
              var w = element.offsetWidth;
              move(wname,o.x,o.y);
              element.style.width= w+"px";
       }
}

function checkLength(){
	var message = $("txt");
	var m = message.value;
	if(m.length >= maxLength){
		message.value =  m.substring(0,maxLength);
		$("msglength").value=0;
		$("msglength").style.color= "red";
	}else{
		var no = (maxLength - m.length);
		$("msglength").value = no ;
		$("msglength").style.color= "green";
	}
}
//<<editor.pm
JString.prototype = new Array();
JString.prototype.constructor = JString;
JString.superclass = Array.prototype;

function JString(string){
       for(i = 0; i < string.length;i++){
              JString.prototype[i] = string[i];
       }
}
JString.prototype.splice = function(i,n,array){
       JString.superclass.splice.call(this,i,n,array);
       return this.join("");
};
JString.prototype.toString = function(){
       return this.join("");
};
function markInput(bool){
       var body  = document.getElementsByTagName("body")[0];
       var node = body.getElementsByTagName("input");
       for(var i=0,j=node.length; i<j; i++)
       if(node[i].className == 'markBox')    node[i].checked = bool;

       visible (bool ? 'umarkAll' : 'markAll' );
       hide (bool ? 'markAll' : 'umarkAll' );
}
function markTables(bool){
       var body  = document.getElementsByTagName("body")[0];
       var node = body.getElementsByTagName("option");
       for(var i=0,j=node.length; i<j; i++)
       if(node[i].className == 'table')    node[i].selected = bool;

       visible (bool ? 'umarkAll2' : 'markAll2' );
       hide (bool ? 'markAll2' : 'umarkAll2' );
}
var html = 0;
function enableHtml(){
       if(html ==  false){
              html = true;
              $('htmlButton').checked = true;
              if($('enlarged')){
                     $('enlarged').style.display ='none';
              }
       }else{
              html = false;
              $('htmlButton').checked = false;
              if($('enlarged')){
                     $('enlarged').style.display ='';
              }
       }
}
function put(t){
       addText(t);
}
//<< bbcode buttons
function addText(text){
       var element = $("txt");
                     element.value += text;
       element.focus();
}
function align(a){
       if(html){
              insertT("div align=\""+a+"\"","div");
       }else{
              insertT(a,a);
       }
}
function left(){
       align("left");
}
function center(){
       align("center");
}
function aright(){
       align("right");
}
function justify(){
       align("justify");
}
function strike(){
       insertT("s");
}
function underline(){
       insertT("u");
}
function bold(){
       insertT("b");
}
function italicize(){
       insertT("i");
}
function sub(){
       insertT("sub");
}
function sup(){
       insertT("sup");
}
function img(){
       var textarea  = $('txt');
       if(typeof document.selection != 'undefined'){
              var range = document.selection.createRange();
              var txt = range.text;
              range = document.selection.createRange();
              if(txt.length == 0){
                     txt = prompt("Insert Image location");
                     var o ="";
                     if(html){
                            o = "<img src='"+txt+"'/>";
                     }else{
                            o = "[img]"+txt+"[/img]";
                     }
                     textarea.value += o;
              }else{
                     if(html){
                            range.text = "<img src='"+txt+"'/>";
                     }else{
                            range.text = "[img]"+txt+"[/img]";
                     }
                     range.moveStart('character', txt.length + 11);
              }
              range.select();
       }else if(textarea.selectionEnd > textarea.selectionStart){
              var i = textarea.selectionStart;
              var n = textarea.selectionEnd;
              var img = textarea.value.substring(i,n);
              o = new JString(textarea.value);
              if(html){
                     textarea.value = o.splice(i,(n-i),"<img src='"+img+"'/>");
              }else{
                     textarea.value = o.splice(i,(n-i),"[img]"+img+"[/img]");
              }
       }else if(textarea.selectionStart &&(textarea.selectionEnd == textarea.selectionStart)){//insert at gecko
              var ia = textarea.selectionStart;
              var txta = prompt("Insert Image location:");
              var a = textarea.value.substring(0,ia);
              var b = textarea.value.substring(ia,textarea.value.length);
              if(html){
                     textarea.value = a +"<img src='"+txta+"'/>"+b;
              }else{
                     textarea.value = a +"[img]"+txta+"[/img]"+b;
              }
       }else{
              var imga = prompt("Insert Image location:");
              if(html){
                     if(img){textarea.value += "<img src='"+imga+"'/>";}
              }else{
                     if(img){textarea.value += "[img]"+imga+"[/img]";}
              }
       }
}
var color = "red";
function setCColor(c){
       color = c;
       $("showColor").style.backgroundColor = color;
}
function ShowColor(){
       var e = $("coloor");
       e.style.color = color;
       if(html){
              insertT("span style=\"color:"+color+"\"","span");
       }else{
              insertT("color="+color+"","color");
       }
}
function email(){
       var link = prompt("Insert a Email Address:");
       if(html){
              insertT("a href='mailto:"+link+"'","a");
       }else{
              insertT("email="+link,"email");
       }
}
function link(){
       var link = prompt("Insert a url:");
       if(html){
              insertT("a href=\""+link+"\"","a");
       }else{
              insertT("url="+link+"","url");
       }
}
function insertT(tag,tag2){
       if(!tag2)tag2 = tag;
       var textarea  = $('txt');
       if(typeof document.selection != 'undefined'){ //IE6
              range = document.selection.createRange();
              var txt = range.text;
              if(txt.length == 0){
                     txt = prompt("Insert text");
                     if(txt.length > 0){
                            var o ="";
                            if(html){
                                   o = "<"+tag+">"+txt+"</"+tag2+">";
                            }else{
                                   o = "["+tag+"]"+txt+"[/"+tag2+"]";
                            }
                            textarea.value += o;
                     }
              }else{
                     if(html){
                            range.text = "<"+tag+">"+txt+"</"+tag2+">";
                     }else{
                            range.text = "["+tag+"]"+txt+"[/"+tag2+"]";
                     }
                     range.moveStart('character', tag.length + txt.length + tag2.length);
              }
              range.select();
       }else if(textarea.selectionEnd > textarea.selectionStart){//selektieren gecko
              var i = textarea.selectionStart;
              var n = textarea.selectionEnd;
              var txtb = textarea.value.substring(i,n);
              o = new JString(textarea.value);
              if(html){
                     textarea.value = o.splice(i,(n-i),"<"+tag+">"+txtb+"</"+tag2+">");
              }else{
                     textarea.value = o.splice(i,(n-i),"["+tag+"]"+txtb+"[/"+tag2+"]");
              }
       }else if(textarea.selectionStart &&(textarea.selectionEnd == textarea.selectionStart)){//insert at gecko
              var ia = textarea.selectionStart;
              var txta = prompt("Insert text");
              var a = textarea.value.substring(0,ia);
              var b = textarea.value.substring(ia,textarea.value.length);
              if(html){
                     textarea.value = a +"<"+tag+">"+txta+"</"+tag+">"+b;
              }else{
                     textarea.value =   a +"["+tag+"]"+txta+"[/"+tag2+"]"+b;
              }
       }else{
              var bol = prompt("insert text");
              if(html){
                     textarea.value += "<"+tag+">"+bol+"</"+tag2+">";
              }else{
                     textarea.value += "["+tag+"]"+bol+"[/"+tag2+"]";
              }
       }
}
function clearIt(){
       var element = $("txt");
                     element.value = "";
       element.focus();
}
var lang = 'Perl';
function setlang(stext){
       lang = stext;
}
function getLang(){
       return lang;
}
function getValue(id){
       return $(id).value;
}
function setValue(id,txt){
       $(id).value = txt;
}
//editor.pm>>
//login

//<<drag&drop
var dragobjekt = null;
var dragX = 0;
var dragY = 0;
var posX = 0;
var posY = 0;
var dropenabled = false;
// document.onmousemove = drag;
// document.onmouseup = drop;
var dropText = null;
var dropzone = null;
var dropid = null;
var m_bOver = true;
var offsetLeft = 0;
function startdrag(element){
       dropid = element;
       dragobjekt = $(element);
       dragX = posX - dragobjekt.offsetLeft;
       dragY = posY - dragobjekt.offsetTop;
}
function drop(){
       if(dropenabled && dragobjekt){
              m_bOver = true;
              dragobjekt.style.cursor = "";
              dragobjekt.style.position ="";
       }
       dragobjekt = null;
}
function drag(EVENT) {
       posX = document.all ? window.event.clientX : EVENT.pageX;
       posY = document.all ? window.event.clientY : EVENT.pageY;
       if(dragobjekt){
	  dragobjekt.style.cursor ="pointer";
	  var os = dropenabled ? 25 : 0;
	  dragobjekt.style.left = (posX -dragX )+"px";
	  dragobjekt.style.top = (posY - dragY)+0+"px";
       }
}
//drag&drop>>
function displayTree(id){
       var e = $(id);
       if(!e)return;
       var display = e.style.display;
       if(display == "none"){
              e.style.display = "";
       }else if(display == ""){
              e.style.display = "none";
       }
}
// object.x - .y =  getElementPosition(id);
function getElementPosition(id){
       var node = $(id);
       var offsetLeft = 0;
       var offsetTop = 0;
       while (node){
              offsetLeft += node.offsetLeft;
              offsetTop += node.offsetTop;
              node = node.offsetParent;
       }
       var position = new Object();
       position.x = offsetLeft;
       position.y = offsetTop;
       return position;
}
function leaveFrame(){
       if (parent.frames.length>0){
              parent.location.href = location.href;
       }
}

function param(name){
       var lo = location.href;
       var i = 0;
       var suche = name+"=";
                     while (i< lo.length){
              if (lo.substring(i, i+suche.length)==suche){
                     var ende = lo.indexOf(";", i+suche.length);
                     ende = (ende>-1) ? ende : lo.length;
                     var cook = lo.substring(i+suche.length, ende);
                     return unescape(cook);
              }
              i++;
                     }
                     return 0;
}
function blogThis(){
       var referer = param("referer");
       var headline = param("headline");
       var q = param("quote");
       if(referer && headline && q){
              var t = "[blog="+unescape(referer)+"]\n"+unescape(q)+"\n[/blog]";
              $('title').value = unescape(headline);
              $('txt').value = t;
       }
}
function datum(){
       var z = new Date();
       var j = z.getYear();
       if(j < 999) j+=1900;
       var m = z.getMonth() + 1;
       var t = z.getDate();
       var d =  translate("Datum:")+t+"."+m+"."+j;
       return d;
}
function setText(id,string){
       var element = $(id);
      if(element)
         element.innerHTML = string;
      else
         window.status = id+string;
}
function getText(id){
       var element = $(id);
	   if(element)
          return element.innerHTML;
}
function hide(id){
       if($(id))
       $(id).style.display = "none";
}
function visible(id){
       if($(id))
       $(id).style.display = "";
}
//API>>
function showMenu(linkId,menuId){
       var oLink = $(linkId);
       var oMenu = $(menuId);
       var o = getElementPosition(linkId);
       var ao = getWindowSize();
       var c = (( o.x +  oLink.offsetWidth  )- oMenu.offsetWidth );
       c = c > 0 ? c : o.x-1;
       move(menuId,ao.x > o.x+ oMenu.offsetWidth ?  o.x-1  : c+1  ,o.y+oLink.offsetHeight );
       displayTree(menuId);
}
function getWindowSize() {
       var nWidth = 0, nHeight = 0;
       var o = new Object;
       if( typeof( window.innerWidth ) == 'number' ) {//Gecko
              o.x = window.innerWidth;
              o.y =  window.innerHeight;
              return o;
       } else if( document.documentElement &&  document.documentElement.clientWidth && document.documentElement.clientHeight  ) {//Ie
              o.x = document.documentElement.clientWidth;
              o.y = document.documentElement.clientHeight;
              return o;
       }
       o.x = 0;
       o.y = 0;
       return o;
}
function ScroBarWidth(){
       //2 verschachtelte divs erzeugen.
       var ouside            = document.createElement('div');//aussen
       ouside.style.position = 'absolute';
       ouside.style.top      = '-1000px';//in den nicht sichtbaren bereich positionieren
       ouside.style.left     = '-1000px';
       ouside.style.width    = '100px';
       ouside.style.height   = '50px';

       var inside =document.createElement('div');//innnen
       inside.style.width    = '100%';
       inside.style.height   = '100px';//das innere div ist höher damit scrollbars angezeigt werden.
       ouside.appendChild(inside);

       document.body.appendChild(ouside);//outside an body anhängen.

       var nWidthWithScrollBars = nWidthWithoutScrollBars = 0;

       // outside ohne scrollbar anzeigen
       ouside.style.overflow    = 'hidden';
       nWidthWithoutScrollBars = inside.offsetWidth;//Breite ohne scrollbar

       //outside mit scrollbar anzeigen
       ouside.style.overflow = 'auto';
       nWidthWithScrollBars = inside.offsetWidth;//Breite mit scrollbar

       document.body.removeChild(document.body.lastChild);//outside wieder entfernen

       //Breite inside ohne Scrollbar  -  inside mit Scrollbar
       return nWidthWithoutScrollBars - nWidthWithScrollBars;
}

//CreateUser
var m_bOver = true;
function prepareMove(id){
  if(document.body){
    if (typeof document.body.onselectstart != "undefined") {//ie
      document.body.onselectstart=function(){return false};
    }else if (typeof document.body.style.MozUserSelect!="undefined"){ //gecko
      document.body.style.MozUserSelect="none";
    }else{ //Opera
      document.body.onmousedown=function(){return false;}
    }
  }
  dragobjekt = $(id);
  dragX = posX - dragobjekt.offsetLeft;
  dragY = posY - dragobjekt.offsetTop;
  dropenabled = true;
  m_bOver = false;
  var o = getElementPosition(id);
  console.log("getElementPosition",o.x);
  move(id,o.x+25,o.y+25);
  $(id)
  startdrag(id);
}
function enableDropZone(id){
       if(!dragobjekt) return;
       dropzone = id;
       if(dragobjekt.id != dropzone) $(id).className = "dropzone"+size;
}
function disableDropZone(id){
         $(id).className = "treeviewlink"+size;
}
function confirmMove(){
  if(dropzone && dragobjekt.id != dropzone){
        var move = confirm("hierher verschieben ?");
        if(move){
	  requestURI('/cgi-bin/mysql.pl?action=MoveTreeViewEntry&dump=links&from='+$(dropid).id+"&to="+$(dropzone).id+"#"+$(dropzone).id);
        }else{
          dragobjekt.style.position ="";
          dropenabled = false;
          dragobjekt.className = "treeviewlink";
          dragobjekt = null;
        }
   }
  m_bOver = true;
}
function scrollToTxt(){
  var subname = param('name');
  var array   =  $('txt').value.split('\n');
  for(var i = 0; i < array.length;i++){
    if(array[i] == 'sub '+subname){
      var lineHeight = $('txt').scrollHeight/array.length;
      $('txt').scrollTop = (i - 1) * lineHeight;
    }
  }
}
