function $(v) { return document.getElementById( v ); }

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function showLoading() {
	$("loadingwin").style.height = $("displaycontent").scrollHeight+"px";
	$("loadingwin").style.display= "block";
}

function loadcontent(whichone,isfull){
	showLoading();
	
	var ao = new AjaxObject101();
	ao.funcDone = replace_extended_content;
	if( !isfull )
		ao.sndReq("get", "/index.php", "co=1&p=" + whichone);
	else
		ao.sndReq("get", "/index.php", "co=1&" + whichone);
	cpage = whichone;
	return false;
}

function replace_extended_content()
{
	var rd = this.http.responseText;
	var items = rd.split(/###itemsplit###/);
	var tc = $("innercontent");
	tc.innerHTML = items[0];
	if( String(items[1]) != "undefined" )
	{
		// $("sidenavi").innerHTML = items[1];
		var amadbg = String(items[2]);
		if( amadbg != "undefined" && trim(amadbg) != "" )
		{
			$("amadebug").innerHTML = amadbg;
			$("amadebug").style.display="block";
		}
		else
		{
			$("amadebug").innerHTML = "";
			$("amadebug").style.display="none";
		}
	}
	
	if( cpage == "screenshots" )
		initLightbox();
	
	if( $("excodeuri") != null )
		$("code").src = $("excodeuri").innerHTML;

	$("loadingwin").style.display="none";

	return "done";
}

function dopostform(which) {
	showLoading();
	try {
		var pvars = {};
		for (var e in which.elements) {
			if ((which.elements[e].type == "text") ||
			(which.elements[e].type == "password") ||
			(which.elements[e].type == "hidden") ||
			(which.elements[e].type == "textarea")) {
				pvars[which.elements[e].name] = which.elements[e].value;
			}
			else {
				if (which.elements[e].type == "checkbox") {
					if (which.elements[e].checked) 
						pvars[which.elements[e].name] = which.elements[e].value;
				}
			}
		}
		var aopost = new AjaxObject101();
		pvars["co"] = "1";
		aopost.funcDone = replace_extended_content;
		var paramstr = new Array();
		for (var nm in pvars) 
			paramstr.push(encodeURIComponent(nm) + "=" + encodeURIComponent(pvars[nm]));
		
		aopost.sndReq("post", which.action, paramstr.join("&"));
	}
	catch(E) {
		// Failover: standard submit
		// alert( "failed with "+E );
		which.submit();
	}
}


function dumpthevar( thevar, indent, maxdepth )
{
	var outtext = "";
	var cmaxd = maxdepth;
	if( cmaxd != undefined )
	{
		cmaxd--;
		if( cmaxd <= 0 &&  typeof( thevar ) == "object" ) return "XXXX\n";
	}

	if( indent == undefined ) indent = "";
	if( typeof( thevar ) == "object" )
	{
		outtext += "object => \n"+indent+"{\n";
	
		for( var prop in thevar )
		{
			if( prop == "myparent" )
				outtext += indent + "  " + prop + " => <PARENTCLASS>\n";
			else
				outtext += indent + "  " + prop + " => " + dumpthevar( thevar[prop], indent + "  ", cmaxd );
		}
		outtext += indent + "}\n";
	}
	else if( typeof(thevar) == "function" )
		outtext += "("+typeof(thevar)+")\n";
	else
		outtext += "("+typeof(thevar)+"): "+thevar+"\n";

	return outtext;
}

function checkagb_internal(forcegamestart,noreturn)
{
	try {
		if (!$("agbacc").checked) {
			alert($("erroragbaccept_text").innerHTML);
			if (noreturn) 
				return;
			else 
				return false;
		}
		else {
			if ((cpage == "spielstart") || (forcegamestart)) {
				// ignore errors on stopping flash sound
				var thiswindow = false;
				
				try {
					var xy = parent.location.href;
				} 
				catch (E) {
					thiswindow = true;
				}
				if( document.location.href.match(/popmog/) )
					thiswindow = true;
				
				if (thiswindow) {
					document.location.href = "member/maingui.php";
				}
				else {
					window.open("member/maingui.php", "_blank");
				}
			}
			else {
				dopostform(document.forms["form1"]);
			}
			if (noreturn) 
				return;
			else 
				return false;
		}
	}
	catch(E) {
		alert("asdf "+E);
	}
}
