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');
	});
}
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 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';
	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(){
      if(!menuAktive){
	$('menu').classList.remove('closed');
	visible('menuContent');
	menuAktive = true;
      }
      var node = $("closeMenu");
      node.addEventListener ('click', function (evt) {
	if(menuAktive){
	  hide('menuContent');
	  $('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');
	}
      }
}
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');

}

window.onerror = logError;
function logError(message, file, line){
         alert("Message: "+message+"file: "+file+"line: "+line);
}

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;
}
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;popupTitle
	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){
	if(checkForm(node)){
	  var url = "../cgi-bin/mysql.pl?";
	  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)
			url += node.elements[i].name+"="+encodeURIComponent(node.elements[i].value)+"&"; 
	    }else if(node.elements[i].name == 'submit'){
	      url += node.elements[i].name+"="+encodeURIComponent(act)+"&";
	    }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 )
		      url += node.elements[i].name +"="+ encodeURIComponent(node.elements[i].options[j].value)+"&";
		}
	    }else{
	      url += node.elements[i].name+"="+encodeURIComponent(node.elements[i].value)+"&"; 
	    }
	  }
	  requestURI(url,tabId,tabTitle);
	}
}
var http_request = false;
var oldpage;
function requestURI(url,id,txt,bHistory){
	closePopup();
	bHistory = typeof bHistory !== 'undefined' ? bHistory : true;
	if(bHistory)
	  history.pushState(null, '','?'+url);
	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
	  setCurrentTab('dynamicLink',txt);	
        http_request.onreadystatechange = setContent;
        http_request.open('GET', url, true);
        http_request.send(null);
}

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{
		setText('content',oldpage);
		setCurrentTab(currentId, currentTxt);
	  }
	  var closeButton = $("closeButton");
	  if(!document.cookie.match('sid') || document.cookie.match('sid=123') ){
	    showPopup('login',true);
	    evalId('popupContent');
	  }else{
	    closeButton.addEventListener ('click',closePopup);
	  }
	  hide('toolbarcontent2');
	  hide('toolbarcontent');
	  if($('toolbar'))
	    $('toolbar').classList.add('closed');
	  if($('toolbar2'))
	    $('toolbar2').classList.add('closed');
        }
}
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);
	  }
	}
}
window.onerror = logError;
function logError(a,b,c){
       alert(a+" "+" "+b+" "+c);
}
window.onpopstate =loadHistory;
function loadHistory(){
	var rxObj =  new RegExp(/action=([^&]+)/);
	rxObj.exec(window.location.search);
	var action = RegExp.$1;
	var rxObj2 =  new RegExp(/(\?[^\?]+$)/);
	rxObj2.exec(window.location.search);
	var uri = RegExp.$1;
	if(uri && action)
	  requestURI('cgi-bin/mysql.pl'+uri,action,action,false);
	else
	  requestURI('cgi-bin/mysql.pl?action=showDatabases','ShowDatabases','Datenbanken');
	return true;
}
function translate(string){
	var lng = navigator.language.indexOf("de") > -1 ? 'de':'en';
        var l = new Lang(); 
	if(string){
	  var ret = eval('l.'+lng+string.toLowerCase());
	  return ret ? ret : string;
	} else{
	  traversTranslate('tab1',lng,l);
	  traversTranslate('tabwidget',lng,l);
	}
}
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");
function Autocomplete(evt){
  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.toLowerCase() == 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.toLowerCase());
}
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  = document.getElementById('sqlEdit');
       if(typeof document.selection != 'undefined'){ //IE6
              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;	
       }
}

function toolbar(){
    displayTree('toolbar2');  
    displayTree('toolbar'); 
}

function displayTree(id){
  var entry = $(id);
  if(entry && entry.firstChild){
    if(entry.firstChild.style.display == 'none'){
      bToolbar = true;
      entry.classList.remove('closed');
        if(entry.firstChild)
		entry.firstChild.style.display ='';
    }else{
      entry.classList.add('closed');
      bToolbar = false;
        if(entry.firstChild)
		entry.firstChild.style.display ='none';
    }
  }
}