var g_bIE = (navigator.appName == "Microsoft Internet Explorer")? 1 : 0;
var httpRequest = false;
function newhttpRequest() 
{
	if(g_bIE)	return new ActiveXObject("Microsoft.XMLHTTP");
	else		  return new XMLHttpRequest();
}


function login_frame_pop()
{
		width=(screen.availWidth-400)/2;
		height=(screen.availHeight-300)/2;
		
		window.scrollTo(0, 0);
		
		document.getElementById("login_box_bg").style.filter="Alpha(opacity=80);";
		document.getElementById("login_box_bg").style.MozOpacity=0.8;
		document.getElementById("login_box_bg").style.visibility="visible";
		document.getElementById("body").style.overflow="hidden";
		
		document.getElementById("login_box").style.top=height;
		document.getElementById("login_box").style.left=width;
		document.getElementById("login_box").style.visibility="visible";
		
		document.getElementById("benutzername").focus();
}
function login_frame_hide()
{
		document.getElementById("login_box_bg").style.visibility="hidden";
		document.getElementById("login_box").style.visibility="hidden";
		document.getElementById("body").style.overflow="auto";
}
function logout_frame_pop()
{
	if(!httpRequest)
		{
			httpRequest = newhttpRequest();
		}
		if(httpRequest)
		{
		  	httpRequest.open("GET", "includes/backend/logout.php");
			httpRequest.onreadystatechange = function ()
			{
				if(httpRequest.readyState == 4 && httpRequest.status==200)
				{
					document.getElementById("admin_button").href="javascript:login_frame_pop()";
					document.getElementById("admin_button_span").innerHTML="Login";	
					document.getElementById("admin_button_span").style.color="#C0C0C0";
					setTimeout("self.location.reload();",10);
				}
			}
			httpRequest.send(null);	
		}
		
}
function auth_submit()
{
	pass=0;
	if(document.getElementById("benutzername").value!="")
	{
		document.getElementById("benutzername").style.backgroundColor="white";
		pass=1;
	}
	else
	{
		document.getElementById("benutzername").style.backgroundColor="red";
		pass=0;
	}
	if(document.getElementById("passwort").value!="")
	{
		document.getElementById("passwort").style.backgroundColor="white";
		pass=1;
	}
	else
	{
		document.getElementById("passwort").style.backgroundColor="red";
		pass=0;
	}
	
	if(pass==1)
	{
		if(!httpRequest)
		{
			httpRequest = newhttpRequest();
		}
		if(httpRequest)
		{
		  httpRequest.open("GET", "includes/backend/auth.php?uid="+document.getElementById("benutzername").value+"&pwd="+document.getElementById("passwort").value);
			httpRequest.onreadystatechange = function ()
			{
				if(httpRequest.readyState == 4 && httpRequest.status==200)
				{
					if(httpRequest.responseText.indexOf("fehlgeschlagen")!=-1)
					{
						document.getElementById("auth_failed_span").innerHTML="Benutzername oder Passwort falsch!"
					}
					else
					{
						login_frame_hide();
						document.getElementById("admin_button").href="javascript:logout_frame_pop()";
						document.getElementById("admin_button_span").innerHTML="<span style='color:red;'>> Logout</span>";
						setTimeout("self.location.reload();",200);
					}
				}
			}
			httpRequest.send(null);	
		}
	}
}





function edit_data(nr,id,text)
{
	
		if(!httpRequest)
		{
			httpRequest = newhttpRequest();
		}
		if(httpRequest)
		{
		  httpRequest.open("GET", "includes/backend/edit.php?nr="+nr+"&id="+id+"&text="+text);
			httpRequest.onreadystatechange = function ()
			{
				if(httpRequest.readyState == 4 && httpRequest.status==200)
				{
						document.getElementById("edit_box").innerHTML=httpRequest.responseText;
				}
			}
			httpRequest.send(null);	
		}
	
		width=(screen.availWidth-400)/2;
		height=(screen.availHeight-300)/2;
		
		window.scrollTo(0, 0);
		
		document.getElementById("edit_box_bg").style.filter="Alpha(opacity=80); -moz-opacity: 0.80;";
		document.getElementById("edit_box_bg").style.visibility="visible";
		document.getElementById("body").style.overflow="hidden";
		
		document.getElementById("edit_box").style.top=height;
		document.getElementById("edit_box").style.left=width;
		document.getElementById("edit_box").style.visibility="visible";
		
}
function edit_data2(nr,id)
{
		conf=confirm("Sind Sie wirklich sicher?")
		if(conf)
		{
			if(!httpRequest)
			{
				httpRequest = newhttpRequest();
			}
			if(httpRequest)
			{
				httpRequest.open("GET", "includes/backend/edit.php?nr="+nr+"&id="+id);
				httpRequest.send(null);	
			}
			
			setTimeout("self.location.reload();",200);
		}
}
function edit_data_hide()
{
		document.getElementById("edit_box_bg").style.visibility="hidden";
		document.getElementById("edit_box").style.visibility="hidden";
		document.getElementById("body").style.overflow="auto";
}

function edit_submit(nr,id)
{
	nr=parseInt(nr)+1;
	if(!httpRequest)
	{
		httpRequest = newhttpRequest();
	}
	if(httpRequest)
	{
	 	httpRequest.open("GET", "includes/backend/edit.php?nr="+nr+"&id="+id+"&text="+document.getElementById("edit_new").value);
		httpRequest.send(null);	
	}
	
	/*if(!httpRequest)
	{
		httpRequest = newhttpRequest();
	}
	if(httpRequest)
	{
	 	httpRequest.open("GET", "includes/products.php?reload=1");
		httpRequest.onreadystatechange = function ()
		{
			if(httpRequest.readyState == 4 && httpRequest.status==200)
			{
					document.getElementById("main_content").innerHTML=httpRequest.responseText;
			}
		}
		httpRequest.send(null);	
	}*/
	
	edit_data_hide();
	
	setTimeout("self.location.reload();",200);
	
}



document.onmousemove = mouseMove;   
var mousePos;
function mouseMove(ev){   
    ev           = ev || window.event;   
    mousePos = mouseCoords(ev);   
}   
  
function mouseCoords(ev){   
    if(ev.pageX || ev.pageY){   
         return {x:ev.pageX, y:ev.pageY};   
     }   
     return {   
         x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,   
         y:ev.clientY + document.body.scrollTop  - document.body.clientTop   
     };   
 }  

function scrollbox_pop(cat_id)
{
		width=300;
		height=200;
		
		document.getElementById("scrollbox").style.top=mousePos.y;
		document.getElementById("scrollbox").style.left=mousePos.x;
		document.getElementById("scrollbox").style.width=width;
		document.getElementById("scrollbox").style.visibility="visible";
		
		if(!httpRequest)
		{
			httpRequest = newhttpRequest();
		}
		if(httpRequest)
		{
		  httpRequest.open("GET", "includes/backend/product_list.php?cat_id="+cat_id);
			httpRequest.onreadystatechange = function ()
			{
				if(httpRequest.readyState == 4 && httpRequest.status==200)
				{
						document.getElementById("scrollbox").innerHTML=httpRequest.responseText;
				}
			}
			httpRequest.send(null);	
		}
		
}
function scrollbox_hide()
{
	document.getElementById("scrollbox").style.visibility="hidden";
}
function product_add(cat_id, prod_id)
{
	if(!httpRequest)
	{
		httpRequest = newhttpRequest();
	}
	if(httpRequest)
	{
	    httpRequest.open("GET", "includes/backend/product_list_edit.php?mode=add&cat_id="+cat_id+"&prod_id="+prod_id);
		httpRequest.send(null);	
	}
	
	scrollbox_hide();
	setTimeout("self.location.reload();",200);
}
function product_delete(cat_id)
{
	if(!httpRequest)
	{
		httpRequest = newhttpRequest();
	}
	if(httpRequest)
	{
	    httpRequest.open("GET", "includes/backend/product_list_edit.php?mode=delete&cat_id="+cat_id);
		httpRequest.send(null);	
	}
	
	setTimeout("self.location.reload();",200);
}



function scrollbox_pop_down(cat_id)
{
		width=300;
		height=200;
		
		document.getElementById("scrollbox").style.top=mousePos.y;
		document.getElementById("scrollbox").style.left=mousePos.x;
		document.getElementById("scrollbox").style.width=width;
		document.getElementById("scrollbox").style.visibility="visible";
		
		if(!httpRequest)
		{
			httpRequest = newhttpRequest();
		}
		if(httpRequest)
		{
		  httpRequest.open("GET", "includes/backend/down_list.php?down_id="+cat_id);
			httpRequest.onreadystatechange = function ()
			{
				if(httpRequest.readyState == 4 && httpRequest.status==200)
				{
						document.getElementById("scrollbox").innerHTML=httpRequest.responseText;
				}
			}
			httpRequest.send(null);	
		}
		
}
function product_add_down(down_id, prod_id)
{
	if(!httpRequest)
	{
		httpRequest = newhttpRequest();
	}
	if(httpRequest)
	{
	    httpRequest.open("GET", "includes/backend/down_list_edit.php?mode=add&down_id="+down_id+"&prod_id="+prod_id);
		httpRequest.send(null);	
	}
	
	scrollbox_hide();
	setTimeout("window.location.href='index.php?pid=49&id="+down_id+"'",200);
}
function product_delete_down(cat_id,down_id)
{
	if(!httpRequest)
	{
		httpRequest = newhttpRequest();
	}
	if(httpRequest)
	{
	    httpRequest.open("GET", "includes/backend/down_list_edit.php?mode=delete&link_id="+cat_id);
		httpRequest.send(null);	
	}
	
	setTimeout("window.location.href='index.php?pid=49&id="+down_id+"'",200);
}













function insert_tag(id, aTag, eTag) {

	input=document.getElementById(id);
	 
  input.focus();
  /* für Internet Explorer */
  if(typeof document.selection != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);      
    }
    range.select();
  }
  /* für neuere auf Gecko basierende Browser */
  else if(typeof input.selectionStart != 'undefined')
  {
    /* Einfügen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* für die übrigen Browser */
  else
  {
    /* Abfrage der Einfügeposition */
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    /* Einfügen des Formatierungscodes */
    var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
    input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
  }
}

function delete_product_bild()
{
	conf=confirm("Sind Sie sich wirklich sicher?");
	if(conf)
	{
		document.getElementById('delete').value=1;
		document.getElementById('fileform').submit();
	}
}

function folder(id)
{
	if(document.getElementById("isfiles"+id).innerHTML=="0")
	{
		document.getElementById("files"+id).innerHTML=document.getElementById("tempfiles"+id).innerHTML;
		document.getElementById("pm"+id).src="style/pictures/minus.png";
		document.getElementById("isfiles"+id).innerHTML="1";
	}
	else
	{
		document.getElementById("files"+id).innerHTML="";
		document.getElementById("pm"+id).src="style/pictures/plus.png";
		document.getElementById("isfiles"+id).innerHTML="0";
	}
}

var focus=false;
function getUser(first)
{
	if(first || focus)
	{
		filter=document.getElementById('filterfield').value;
		
		if(!httpRequest)
		{
			httpRequest = newhttpRequest();
		}
		if(httpRequest)
		{
		  httpRequest.open("GET", "includes/backend/getUser.php?filter="+filter);
			httpRequest.onreadystatechange = function ()
			{
				if(httpRequest.readyState == 4 && httpRequest.status==200)
				{
						document.getElementById("userdiv").innerHTML=httpRequest.responseText;
				}
			}
			httpRequest.send(null);	
		}
	}
	
	setTimeout("getUser('')",500);	
}

function getUserBlur()
{
	focus=false;
}
function getUserFocus()
{
	focus=true;
}
function setUser(id)
{
	window.location.href="index.php?pid=64&bid="+id;
}
function deleteUser(id)
{
	conf=confirm("Wollen Sie den Benutzer wirklich löschen?");
	if(conf)
	{
		window.location.href="index.php?pid=63&did="+id;
	}
}
function deleteFolder(id)
{
	conf=confirm("Wollen Sie den Ordner wirklich löschen?\nDie Dateien bleiben erhalten!");
	if(conf)
	{
		document.getElementById("hiddenIFrame").src="includes/backend/deleteKat.php?id="+id;
		setTimeout("window.location.href='index.php?pid=59'",200);
	}
}
