var xmlHttp = null;
try {
    xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
} catch(Error) {
    try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(Error) {
        xmlHttp  = null;
    }
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
}
if (!xmlHttp) {
	alert("Erzeugung fehlgeschlagen!");
}
function pause(millis)
{
date = new Date();
var curDate = null;

do { var curDate = new Date(); }
while(curDate-date < millis);
}
function urlcompiler(url)
{
	if(url.lastIndexOf("\\")>-1) start=url.lastIndexOf("\\");
	else if (url.lastIndexOf("/")>-1.) start=url.lastIndexOf("/");
	ende=url.length;
	seite=url.substring(start+1,ende);
	return seite;
}
function dl()
{
	document.getElementById("hinweis").style.display = "none";
	document.getElementById("searchanim").style.display = "block";
	var count = 0;
	var j=0;
	var nam=new Array();
	var	d = document.getElementById("leuchtkasten");
	anz = d.getElementsByTagName("img").length/2;
	if(anz>0)
	{
		for(x=0; x<anz; x++)
		{
			nam[x]=d.getElementsByTagName("img")[j].src;
			j=j+2;
		}
	}
	if(nam.length > 0)
	{
		for(x=0;x<nam.length;x++)
		{
			nam[x] = urlcompiler(nam[x]);
			if (count == 0)
			{
				string = "id"+count+"=";
				string += nam[x];
				count = count + 1;
			}
			else
			{
				string += "&id"+count+"=";
				string += nam[x];
				count = count + 1;
			}
		}
		xmlHttp.open('get', 'inc.download.php?'+string, true);
		xmlHttp.onreadystatechange = function machWas()
		{
			if (xmlHttp.readyState == 4)
			{
				if (xmlHttp.status == 200)
				{
					url = xmlHttp.responseText;
					if(!document.getElementById("downlink"))
					{
						var down = document.createElement("a");
						down.href = url;
						down.id = "downlink";
						down.className = "dl";
						var text_d = document.createTextNode("Download");
						down.appendChild(text_d);
						document.getElementById("dl_box").appendChild(down);
						document.getElementById("dl_box").style.display = "block";
					}
					document.getElementById("searchanim").style.display = "none";
					document.getElementById("dl_box").style.display = "block";
					document.getElementById("del_lk").style.display = "block";
				}
			}
		}
		xmlHttp.send(null);
	}
}
function add(nr,size)
{
	if(document.getElementById("leuchtkasten").getElementsByTagName("img").length < 10)
	{
		document.getElementById("hinweis").style.display = "none";
		document.getElementById("dl_box").style.display = "none";
		document.getElementById("searchanim").style.display = "block";
		xmlHttp.open('get', 'inc.leuchtkasten.php?id='+nr, true);
		xmlHttp.onreadystatechange = function machWas()
		{
			if (xmlHttp.readyState == 4)
			{
				if (xmlHttp.status == 200)
				{
					// DIV-Elemente erstellen
					var box = document.createElement("div");
					var boxthumb = document.createElement("div");
					// THUMB erstellen
					var bild = document.createElement("img");
					var req = xmlHttp.responseText;
					bild.src = req;
					boxthumb.className = size;
					bild.id = "pic_"+nr;
					bild.name = "b";
					bild.alt = nr;
					// THUMB in Thumb-DIV einfügen
					boxthumb.appendChild(bild);
					// Der kompletten BOX eindeutige ID zuweisen
					box.id = "box_"+nr;
					// Löschen-Bild-Link erstellen
					var dellink = document.createElement("a");
					var button = document.createElement("img");
					button.src = "img/del_button.gif";
					button.alt = "Foto entfernen!";
					button.className = "del_abstand";
					dellink.appendChild(button);
					// Boxen in Hauptdiv einfügen
					box.appendChild(boxthumb);
					box.appendChild(dellink);
					// LÖSCHEN - Link Ziel zuweisen
					dellink.href = "javascript:del('"+nr+"')";
					// Box in Leuchtkasten hinzufügen
					document.getElementById("leuchtkasten").appendChild(box);
					dl();
				}
			}
		}
		xmlHttp.send(null);
	}
	else
	{
		alert("Sie können maximal 5 Bilder auswählen!");
	}
  document.getElementById("leuchtkasten").style.display = "block";
}
function del(nr)
{
	document.getElementById("dl_box").style.display = "none";
	document.getElementById("searchanim").style.display = "block";
	xmlHttp.open('get', 'inc.leuchtkasten_del.php?id='+nr, true);
	xmlHttp.onreadystatechange = function machWas()
	{
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				var menu = document.getElementById("leuchtkasten");
				var box = document.getElementById("box_"+nr);
				menu.removeChild(box);
				if(document.getElementById("leuchtkasten").getElementsByTagName("img").length > 0)	dl();
				if(document.getElementById("leuchtkasten").getElementsByTagName("img").length == 0)
				{
					document.getElementById("hinweis").style.display = "block";
					document.getElementById("dl_box").style.display = "none";
					document.getElementById("searchanim").style.display = "none";
					document.getElementById("del_lk").style.display = "none";
				}
			}
		}
	}
	xmlHttp.send(null);
}
function del_lk()
{
	document.getElementById("hinweis").style.display = "none";
	document.getElementById("del_lk").style.display = "none";
	document.getElementById("dl_box").style.display = "none";
	document.getElementById("searchanim").style.display = "block";
	xmlHttp.open('get', 'inc.del_lk.php', true);
	xmlHttp.onreadystatechange = function machWas()
	{
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				document.getElementById("dl_box").style.display = "none";
				document.getElementById("searchanim").style.display = "none";
				document.getElementById("del_lk").style.display = "none";
				document.getElementById("hinweis").style.display = "block";
        var lk = document.getElementById("leuchtkasten");
        var anz = lk.getElementsByTagName("div").length/2;
        for(r=0;r<anz;r++)
        {
          var rem = lk.firstChild;
          lk.removeChild(rem);
        }
        document.getElementById("leuchtkasten").style.display = "none";
			}
		}
	}
	xmlHttp.send(null);
}