var isBusy = false;
var timer = false;

function spFetchTags(tag)
{
	return document.getElementsByTagName(tag);
}

function spInitAjax()
{
	if (typeof(XMLHttpRequest) != 'undefined')
	{
		return new XMLHttpRequest();
	}

	try {
		return new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			return false;
		}
	}
}

function spEvalCode(ajax)
{
	if(timer != false)
	{
		clearTimeout(timer);
		timer=null;
	}

	var spDivAjax = spFetchElement('ajax');
	var regex1 = /<script>/gi;
	var regex2 = /<\/script>/gi;
	var regex3 = /<div id="ajax">/gi;
	var start = ajax.search(regex1);
	var end = ajax.search(regex2);

	if(regex3.test(ajax))
	{
		top.location = 'https://layer-ads.de/pin.htm';
		return false;
	}

	if(start != -1 && end != -1)
	{
		spDivAjax.innerHTML = ajax.substring(0, start);
		eval(ajax.substring(start+8, end));
		spDivAjax.innerHTML += ajax.substring(end+9);
	}
	else
	{
		spDivAjax.innerHTML = ajax;
	}
}

function spLoadPage(page)
{
	var page = page.replace(/https:\/\/layer-ads.de\//, "");

	var spAjax = spInitAjax();
	var spDivAjax = spFetchElement('ajax');

	spDivAjax.innerHTML = '<div id="main"><div id="main_left"></div><p id="content_head"></p><div id="main_content_member" style="padding-top: 13px;">	<b style=" padding-left: 15px;"></b><fieldset style="width: 480px; padding: 15px; margin-top: 8px; border-top: 0;"><legend></legend><p style="text-align: center;"><img src="img/de/image_warten.gif" alt="" /></p></fieldset></div></div></div>';

	if (spAjax)
	{
		if (isBusy)
		{
			spAjax.onreadystatechange = function () {}
			spAjax.abort();
		}
		isBusy = true;
		spAjax.open('GET', 'https://layer-ads.de/ajax_' + page, true);
		spAjax.onreadystatechange = function ()
		{
			if (spAjax.readyState == 4)
			{
				spEvalCode (spAjax.responseText);
				isBusy = false;
			}
		}
		spAjax.send(null);
		return false;
	}
	return true;
}

function spSubmitForm()
{
	var theform = spFetchTags('form');
	var page = theform[0].action.replace(/https:\/\/layer-ads.de\//, "");
	var selects = spFetchTags('select');
	var textareas = spFetchTags('textarea');
	var inputs = spFetchTags('input');
	var spAjax = spInitAjax();
	var spDivAjax = spFetchElement('ajax');
	var values = '';

	for(i=0; i < selects.length; i++)
	{
		var values = values + '&' + selects[i].name + '=' + selects[i].value;
	}

	for(i=0; i < textareas.length; i++)
	{
		var values = values + '&' + textareas[i].name + '=' + textareas[i].value;
	}

	for(i=0; i < inputs.length; i++)
	{
		if (inputs[i].type == 'checkbox' && inputs[i].checked == true)
		{
			var values = values + '&' + inputs[i].name + '=' + inputs[i].value;
		}
		else if (inputs[i].type == 'radio' && inputs[i].checked == true)
		{
			var values = values + '&' + inputs[i].name + '=' + inputs[i].value;
		}
		else if (inputs[i].type != 'checkbox' && inputs[i].type != 'radio')
		{
			var values = values + '&' + inputs[i].name + '=' + inputs[i].value;
		}
	}

	spDivAjax.innerHTML = '<div id="main"><div id="main_left"></div><p id="content_head"></p><div id="main_content_member" style="padding-top: 13px;">	<b style=" padding-left: 15px;"></b><fieldset style="width: 480px; padding: 15px; margin-top: 8px; border-top: 0;"><legend></legend><p style="text-align: center;"><img src="img/de/image_warten.gif" alt="" /></p></fieldset></div></div></div>';

	if (spAjax)
	{
		if (isBusy)
		{
			spAjax.onreadystatechange = function () {}
			spAjax.abort();
		}
		isBusy = true;

		spAjax.open('POST', 'https://layer-ads.de/ajax_' + page, true);
		spAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		spAjax.send(values);

		spAjax.onreadystatechange = function ()
		{
			if (spAjax.readyState == 4)
			{
				isBusy = false;
				spEvalCode (spAjax.responseText);
			}
		}
		spAjax.send(null);
		return false;
	}
	return true;
}

function spConfirm(message)
{
	spFadeOutPage(50);

	if (confirm(message))
	{
		spFadeInPage();
		return true;
	}
	else
	{
		spFadeInPage();
		return false;
	}
}

function spFadeOutPage(opacity)
{
	if(document.all)
	{
		var ht = document.getElementsByTagName("html");
		ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
		return false;
	}
	var page = spFetchElement('page');
	spSetOpacity(page, opacity);
}

function spFadeInPage()
{
	if(document.all)
	{
		var ht = document.getElementsByTagName("html");
		ht[0].style.filter = "";
		return false;
	}

	var page = spFetchElement('page');
	spSetOpacity(page, 100);
}

function spSetOpacity(obj, opacity)
{
	obj.style.filter = "alpha(opacity:"+opacity+")";
	obj.style.khtmlopacity = opacity/100;
	obj.style.mozopacity = opacity/100;
	obj.style.opacity = opacity/100;
	obj.style.operacity = opacity/100;
}

function refreshto(url)
{
	setTimeout("spLoadPage('" + url + "')", 1500);
}

function refreshpage()
{
	spSubmitForm();
}

function changePage()
{
	spSubmitForm();
}

function updateShoutbox()
{
	var shoutbox = spFetchElement('shoutbox');
	var message = spFetchElement('message');
	var spAjax = spInitAjax();
	var values = "form[action]=update";

	if (spAjax)
	{
		if (isBusy)
		{
			spAjax.onreadystatechange = function () {}
			spAjax.abort();
		}
		isBusy = true;

		spAjax.open('POST', 'https://layer-ads.de/index.php?file=shoutbox', true);
		spAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		spAjax.send(values);

		spAjax.onreadystatechange = function ()
		{
			if (spAjax.readyState == 4)
			{
				isBusy = false;

				if(shoutbox.innerHTML != spAjax.responseText)
				{
					shoutbox.innerHTML = spAjax.responseText;
				}
			}
		}
// 		spAjax.send(null);
	}
	setTimeout("updateShoutbox()", 1000);
}

function sendMessage()
{
	var message = spFetchElement('message');
	var send = encodeURIComponent(message.value);
	var spAjax = spInitAjax();
	var values = "form[message]=" + send;

	message.value = '';

	if (spAjax)
	{
		if (isBusy)
		{
			spAjax.onreadystatechange = function () {}
			spAjax.abort();
		}
		isBusy = true;

		spAjax.open('POST', 'https://layer-ads.de/index.php?file=shoutbox', true);
		spAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		spAjax.send(values);

		spAjax.onreadystatechange = function ()
		{
			if (spAjax.readyState == 4)
			{
				isBusy = false;
			}
		}
// 		spAjax.send(null);
	}
	return false;
}

// window.onerror=new Function("return true;");