var aktreiter = 0;
function reiterwechsel(reiter)
{
  if (aktreiter && aktreiter!=reiter)
  {
    document.getElementById('reiter'+aktreiter).style.display = 'none';
    document.getElementById('R'+aktreiter).className = 'reiterPassiv';
  }
  aktreiter = reiter;
  if (reiter)
  {
    document.getElementById('reiter'+reiter).style.display = 'block';
    document.getElementById('R'+reiter).className = 'reiterAktiv';
    if (el = document.getElementById('tabno'))
      el.value = reiter;
  }
}
var flag = 0;
function setFlag(val)
{
  flag = val;
}
function checkFlag()
{
  var win=window, label='';
  if (checkFlag.arguments.length)
    win = checkFlag.arguments[0];
  if (checkFlag.arguments.length>1)
    label = ' ' + checkFlag.arguments[1];
  if (win.flag)
    return confirm ('Ihre Eingaben'+label+' wurden noch nicht gespeichert.\nMöchten Sie trotzdem fortfahren?\nUm Ihre Eingaben zu sichern, drücken Sie bitte abbrechen!');
  return true;
}

function popup(w,h)
{
  return popup2(w,h,'eakpop');
}
function popup2(w,h,n)
{
  return window.open('about:blank', n, 'width='+w+',height='+h+',resizable=yes,scrollbars=yes');
}

function showlen(srcel, trgname, maxlen)
{
  document.getElementById(trgname).innerText=srcel.innerText.length+' Zeichen (max. '+maxlen+')';
}

ajax_callback = null;
ajax_return = '';
function ajax(url)
{
  if (ajax.arguments.length>1)
    ajax_callback = ajax.arguments[1];
  else
    ajax_callback = null;
  var p = url.lastIndexOf('/');
  var s = url.substr(0,p+1) + 'ajax/' + url.substr(p+1, 255);
  if (navigator.appName.match('^Opera') && document.getElementById('ajaxframe').src == s)
    document.getElementById('ajaxframe').contentWindow.location.reload();
  else
    document.getElementById('ajaxframe').src = s;
  return false;
}
function ajax_rcv()
{
  if (navigator.appName.match('^Opera') && ajax_rcv.arguments.length)
    ajax_return = ajax_rcv.arguments[0];
  else
    ajax_return = document.getElementById('ajaxframe').contentWindow.document.body.innerHTML;
  if (ajax_callback)
    eval(ajax_callback+';');
}

menueoffen = false;
offen = false;

function showmenue(element,wert)
{
  //alert(menueoffen);
  if(wert)
  {
    if(offen != false)
	  document.getElementById(offen).style.display='none'
	menueoffen = true;	
	menuecontrol(element,'block');
	offen = element;
    
  }
  else
  {
    menueoffen = false;
	window.setTimeout("menuecontrol('"+element+"','none')",200);
  }
}

function menuecontrol(element,wert)
{
  if(wert == 'block' || menueoffen==false)
  {
    document.getElementById(element).style.display=wert;
	breite = document.getElementById(element).offsetWidth;
	for(i=0; i<document.getElementById(element).childNodes.length; i++)
	{
	  if(document.getElementById(element).childNodes[i].offsetWidth > breite)
	    breite=document.getElementById(element).childNodes[i].offsetWidth;
	}
	//alert(breite);
	links = document.getElementById(element).offsetLeft;
	fenster = document.getElementsByTagName("body")[0].offsetWidth;
	rest = (fenster-links)-breite;	
	if(rest < 0)
	{
	  minus=40;
	  if(navigator.appName.substring(0, 9) == 'Microsoft')
	    minus = 20;
	  document.getElementById(element).style.left =(fenster-breite)-minus;
	  //document.getElementById(element).style.setAttribute("text-align","right");
	}
	//alert(rest);
	//document.getElementById(element).style.setAttribute("background-color", "red");
  }
}

function go(showflash,width,height){

  if (showflash != "") {

    var str = "height=" + height + ",innerHeight=" + height;
    str += ",width=" + width + ",innerWidth=" + width;
    if (window.screen) {
      var ah = screen.availHeight - 30;
      var aw = screen.availWidth - 10;

      var xc = (aw - width) / 2;
      var yc = (ah - height) / 2;

      str += ",left=" + xc + ",screenX=" + xc;
      str += ",top=" + yc + ",screenY=" + yc;
    }
    str += ",scrollbars=Yes,dependent=Yes"
    //open(DoWhat.value, "remote", str);
     var wintoload4 = open(showflash, "ebizadminpopup",str);
     wintoload4.focus();
  }
}

function dialogTreeDel(id, folder)
{
  var rValue=showModalDialog(
    ('tpl/de')
    + '/dlg.treedel.html', dialogTreeDel.arguments,
    'resizable:yes;scroll:no;status=no;dialogHeight=85px;dialogWidth=433px;dialogTop=300px;dialogLeft=300px');
  if (rValue) location.href=rValue;
}

function help(ident)
{
//alert (location.href.match(/\/admin\/(index\.php)?/));
  var w = popup2(400,300,'eaffhelp'), reg = /\/admin\/(index\.php)?/;
  w.location.href=(location.href.match(reg) ? '../' : '') + 'help_'+ident+'.html';
}

function suchform()
{
  layer = document.getElementById('suchform');
  status = layer.style.visibility;
  neu = 'visible';
  if(status == 'visible')
    neu = 'hidden';
  layer.style.visibility = neu;
}

//von jan
var pop = null;

function do_popdown() {
		if (pop && !pop.closed) pop.close();
}

function do_popup(obj) {
	var width = 400;
	var height = 250;
	
  if (arguments[1])
	{
		width = arguments[1];
	}
	
	if (arguments[2])
	{
		height = arguments[2];
	}
		
	var url = (obj.getAttribute) ? obj.getAttribute('href') : obj.href;
	if (!url) return true;
	var args = 'width='+width+',height='+height+',resizable=yes,top=150,left=200,scrollbars=yes,status=yes';
	do_popdown();
	pop = window.open(url,'',args);
	return (pop) ? false : true;
}

var api_key = 'ABQIAAAA0tXF1tJR1QgSG9HJf4lrjBQdTyYrkQQbm1riap6HwRLyrDuwORSeu-Ruz3kTCR15xMQx2T0Hea0TaQ';
var timer_qs;
var row_selection = -1;
var row_count = 0;
var row_list;

function presearch_quick(text_element, e, do_search){
    var key = (e ? (e.which ? e.which : e.keyCode) : 0);
    var text = text_element.value;
    
    // Key controls			
    if ((key == 13) || (key == 27) || (key == 38) || (key == 40)) {
        if (key == 13) {
            // Return
            if (row_selection >= 0) {
                text_element.value = row_list[row_selection];
            }
            $('#SEARCH').val(text_element.value);
            
            //$('#SEARCH_FORM').submit();
            return;
        }
        if (key == 27) {
            // Escape
            window.setTimeout(function(){
                $('#list_offers').hide();
            }, 300);
            return;
        }
        if (key == 38) 
            row_selection = row_selection - 1; // Up
        if (key == 40) {
            row_selection = row_selection + 1; // Down
        }
        if (row_selection >= row_count) 
            row_selection = row_count - 1;
        if (row_selection < 0) 
            row_selection = -1;
        
        // Update selection
        var rows = $('#list_offers > tr');
        for (var row in rows) {
            
        }
        $.each(rows, function(row, content) {
        	if (row == row_selection) {
                rows[row].className = "selected";
                $('#sfeld1').val($(content).find('a').first().html());
        	}
            else 
                rows[row].className = "";
        });
        return;
    }
    
    $('#SEARCH').val(text);
    
    // Hack for reducing request count
    if (!do_search && (text.length >= 3)) {
        window.clearTimeout(timer_qs);
        timer_qs = window.setTimeout(function(){
            presearch_quick(text_element, false, true);
        }, 500);
        return;
    }
    
    if (text.length >= 2) {
		$.ajax({
			url:		"/index.php?page=artikel-suche&frame=ajax&SEARCH_AJAX=" + encodeURI(text),
			type: 		'GET',
			dataType:	'json',
			success:	function(obj) {
				// Erfolg
                if (obj.fail) {
                    $('#list_offers').hide();
                } else {
                    var table = document.createElement("table");
                    table.id = "list_offers";
                    table.className = "list_offers";
                    table.cellPadding = 0;
                    table.cellSpacing = 0;
                    table.style.position = "absolute";
                    $('#div_offers').html("");
                    $('#div_offers').append(table);
                    
                    row_count = 0;
                    row_list = new Array();                 
                    
                    $.each(obj.offers, function(offer, val) { 
                    	var text = obj.offers[offer];
                        var row = document.createElement("tr");
                        var col = document.createElement("td");
                        var link = document.createElement("a");
                        // Click event
                        //link.href = "javascript: $('#SEARCH').val(\"" + text.replace('"', '\"') + "\"); $('#SEARCH_FORM').submit();";
                        $(link).click(function() { $('#SEARCH').val(text); $('#sfeld1').val(text);  });
                        link.innerHTML = text;
                        // Add to table
                        col.appendChild(link);
                        row.appendChild(col);
                        if (row_count == row_selection) 
                            row.className = "selected";
                        table.appendChild(row);
                        // Update "cache"
                        row_count = row_count + 1;
                        row_list.push(text);
                    });
                    
                    var element_pos = $(text_element).position();
                    $('#list_offers').css({
                    	left:			element_pos.left+"px",
                    	top:			(element_pos.top+20)+"px",
                    	'min-width':	$(text_element).outerWidth()
                    }).show();
                }
			}
		});
    }
    else {
        $('#list_offers').hide();
    }
    
    return;
}

function setBlobText(button, text, visible){
    var div_blob = $('#blob');
    var tbl_blob = $('#blobtable');
    var txt_blob = $('#blobtext');
    
    if (text.length == 0) 
        text = "Keine Hilfe vorhanden.";
    
    if (visible == true) {
        div_blob.show();
        div_blob.click(function() {
            // initially hide all containers for tab content
            setBlobText(button, "", false);
        });
    }
    else {
        div_blob.hide();
    }
    
    txt_blob.html(text.replace("||", "<br />"));
    
    var button_pos = $(button).position();
    var height = txt_blob.outerHeight() + 47;
    var width = 300;

    var page_center_h = window.innerWidth / 2;
    var page_center_v = window.innerHeight / 2;
    if ((button_pos.left - window.scrollX) > page_center_h) {
        div_blob.css("left", (button_pos.left - width)+"px");
    } else {
        div_blob.css("left", (button_pos.left + $(button).width())+"px");
    }
    if ((button_pos.top - window.scrollY) < page_center_v) {
        div_blob.css("top", (button_pos.top + $(button).height())+"px");
    } else {
        div_blob.css("top", (button_pos.top - height)+"px");
    }
    //tbl_blob.clonePosition(div_blob);

    tbl_blob.css("height", height + "px");
    div_blob.css("height", height + "px");
    tbl_blob.css("width", width + "px");
    div_blob.css("width", width + "px");
    //tbl_blob.style.height = div_blob.style.height = height + "px";
    //tbl_blob.style.width = div_blob.style.width = width + "px";
}

function user_pay(stat, id_invoice)
{
	var setval = 0;
	var img = '/bilder/unpayed.png';
	var on_click = function onclick(event) {
	    user_pay(true, id_invoice);
	};
	var quest = 'Soll die Rechnung als unbezahlt markiert werden?';
	if(stat == true)
	{
		setval = 1;
		img = '/bilder/payed.png';
		on_click = function onclick(event) {
		    user_pay(false, id_invoice);
		};
		quest = 'Soll die Rechnung als bezahlt markiert werden?';
	}
	ask = confirm(quest);
	if(!ask)
	{
		return false;
	}
	$.ajax({
		url: '/index.php?page=invoice_set&frame=ajax&ID_INVOICE='+id_invoice+'&SET='+setval,
		/*
		success: function(result) {
			
		}
		*/
	});
	$('#user_pay_'+id_invoice)[0].src = img;
	$('#user_pay_act_'+id_invoice).click(on_click);
}

function minilogin()
{
	var setvis = 'none';
	var vis = $('#minilogin')[0].style.display;
	if(vis == 'none')
	{
		setvis = 'block';
	}
	$('#minilogin').css('display', setvis);
}
