var activemenu = null;
var to = null;
var sim = new Array();
var savesub = new Array();

function menu_activate(n)
{
	window.clearTimeout(to);
	if(activemenu != null && activemenu != n)
		real_menu_deactivate(activemenu);
	var id = "menu_" + n;
	var imid = "men" + n;
	if(document.all)
	{
		obj = document.all[id];
		im = document.all[imid];
	}
	else
	{
		obj = document.getElementById(id);
		im = document.getElementById(imid);
	}
	if(!sim[n])
		sim[n] = im.src;
	im.src = mim[n].src;
	if(obj.className != "activemenu" && obj.className != "activemenuhover")
		obj.className = 'hover';
	else
		obj.className = 'activemenuhover';
	activemenu = n;
}

function menu_deactivate(n)
{
	to = window.setTimeout('real_menu_deactivate(' + n + ');', 1000);
}

function real_menu_deactivate(n)
{
	var id = "menu_" + n;
	var imid = "men" + n;
	if(document.all)
	{
		obj = document.all[id];
		im = document.all[imid];
	}
	else
	{
		obj = document.getElementById(id);
		im = document.getElementById(imid);
	}
	im.src = sim[n];
	if(obj.className != "activemenuhover")
		obj.className = '';
	else
		obj.className = 'activemenu';
	activemenu = null;
}

function subover(im)
{
	var id = im.firstChild.id;
	if(!savesub[id])
		savesub[id] = im.firstChild.src;
	im.firstChild.src = subim[id].src;
}


function subout(im)
{
	var id = im.firstChild.id;
	im.firstChild.src = savesub[id];
}

function writeqt(mov)
{
	document.write('<object width="100%" height="100%" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B">\n');
	document.write('<param name="src" value="'+mov+'" />\n');
	document.write('<param name="scale" value="aspect" />\n');
	document.write('<embed src="'+mov+'" width="100%" height="100%" scale="aspect">');
	document.write('</object>\n');
}

var testblockshowing = '';
var testblockprev = false;
var testblocknext = false;

$(document).ready(function()
{
	if($(".testblock").length == 0)
		return;
	
	if(!document.cookie)
	{
		alert("Bitte akzeptieren Sie Cookies um den Test auszufüllen!");
		return;
	}
	testblockshowing = $(".testblockclicker").eq(0).attr("rel");
	
	show_block(testblockshowing, true);
	
	$("#testfooternext,#testfooterprev").click(function(ev) {
		ev.preventDefault();
		show_block($(this).attr("rel"));
	});
	
	$(".testblockclicker").click(function(ev) {
		ev.preventDefault();
		show_block($(this).attr("rel"));
	});
	
	$(".testclearer").click(function(ev) {
		ev.preventDefault();
		if(confirm('Wirklich alle Eingaben löschen?'))
		{
			Delete_Cookie(etestcookievar, "/");
			document.forms['testform'].reset();
			location.reload();
			//update_test_totals();
		}
	});
	
	var testvals = Get_Cookie(etestcookievar);
	if(testvals)
	{
		testvals = testvals.split("&");
		for(var i = 0; i < testvals.length; i++)
		{
			var pair = testvals[i].split("=");
			var id = pair[1].split("%7C");
			var inp = $("#" + id[1]);
			
			if(inp.attr("type") == "radio" || inp.attr("type") == "checkbox")
			{
				inp.attr("checked", "checked");
			}
			else if(inp.get(0) && inp.get(0).tagName == "OPTION")
			{
				inp.attr("selected", "selected");
			}
			inp = $("#" + pair[0]);
			if(inp.attr("type") == "text")
			{
				inp.val(pair[1]);
			}

		}
	}
	
	update_test_totals();
	
	$(".testinput").bind("change keyup", function()
	{
		Set_Cookie(etestcookievar, $("#testform").serialize(), 500, "/");
		update_test_totals();
	});
});

function show_block(sd, nofade)
{
	$(".testblockclicker.tcactive").removeClass("tcactive");
	$(".testblockclicker[rel="+sd+"]").addClass("tcactive");
	
	$(".testblockmenu.tmactive").removeClass("tmactive");
	$(".testblockmenu[rel="+sd+"]").addClass("tmactive");
	
	if(nofade)
	{
		$("#testblock_" + testblockshowing).hide();
		$("#testblock_" + sd).show();
		testblockshowing = sd;
		calc_nextprev();
	}
	else
	{
		$("#testblock_" + testblockshowing).fadeOut('normal', function() {
			$("#testblock_" + sd).fadeIn();
			testblockshowing = sd;
			calc_nextprev();
		});
	}
}

function calc_nextprev()
{
	var curblock = $("#testblock_" + testblockshowing);
	var blocks = $(".testblock");
	
	for(var i = 0; i < blocks.length; i++)
	{
		if(curblock.get(0) == blocks.eq(i).get(0))
			break;
	}
	
	var testblockprev = i > 0 ? blocks.eq(i - 1).attr("id").split("_")[1] : false;
	var testblocknext = i < blocks.length - 1 ? blocks.eq(i + 1).attr("id").split("_")[1] : false;
	
	if(testblockprev)
		$("#testfooterprev").attr("rel", testblockprev).show();
	else
		$("#testfooterprev").hide();
	
	if(testblocknext)
	{
		$("#testfooternext").attr("rel", testblocknext).show();
		$(".testmenuhider").show();
	}
	else
	{
		$("#testfooternext").hide();
		$(".testmenuhider").hide();
	}
}

function update_wfbes()
{
	if($(".wfbe_gwa").length > 0)
	{
		var gwa = $(".wfbe_gwa").val();
		gwa = gwa.replace(/,/g, ".");
		gwa = parseFloat(gwa);
		
		var as = $(".wfbe_as").val();
		as = as.replace(/,/g, ".");
		as = parseFloat(as);
		
		if(gwa && as)
		{
			var calcwfbe = Math.round(gwa / as * 100);
			$(".wfbecalc").html(calcwfbe);
			$(".wfbe").val(calcwfbe);
		}
		var input = calcwfbe;
	}
	else
	{
		var input = parseInt($(".wfbe").val());
	}
	
	if(!input)
		input = 0;
	$("#wfbe_result").html(get_wfbe(input));
	
	if($(".wfbe_doku_gwa").length > 0)
	{
		var gwa = $(".wfbe_doku_gwa").val();
		gwa = gwa.replace(/,/g, ".");
		gwa = parseFloat(gwa);
		
		var as = $(".wfbe_doku_as").val();
		as = as.replace(/,/g, ".");
		as = parseFloat(as);
		
		if(gwa && as)
		{
			var calcwfbe = Math.round(gwa / as * 100);
			$(".wfbe_dokucalc").html(calcwfbe);
			$(".wfbe_doku").val(calcwfbe);
		}
		var input = calcwfbe;
	}
	else
	{
		var input = parseInt($(".wfbe_doku").val());
	}
	if(!input)
		input = 0;
	$("#wfbe_doku_result").html(get_wfbe_doku(input));
}

function get_wfbe(val)
{
	return !val ? 0 : val < 100 ? 0 : Math.min(3 + Math.round((val - 100) / 50), 15);
}

function get_wfbe_doku(val)
{
	return !val ? 0 : val < 100 ? 0 : Math.min(6 + Math.round((val - 100) / 40), 16);
}

function update_test_totals()
{
	$(".inputtype_checkboxes").each(function()
	{
		var total = 0;
		$(this).find("input:checked:enabled").each(function()
		{
			total += parseInt($(this).val());
		});
		$(this).find("option:selected:enabled").each(function()
		{
			total += parseInt($(this).val());
		});
		$(this).find(".points").html(total);
	});
	
	update_wfbes();
	var vals = $("#testform").serialize().split("&");
	var total = 0;
	for(var i = 0; i < vals.length; i++)
	{
		var pair = vals[i].split("=");
		var addnow = 0;
		if($("#" + pair[0]).hasClass("wfbe"))
		{
			addnow = get_wfbe(parseInt(pair[1]));
		}
		else if($("#" + pair[0]).hasClass("wfbe_doku"))
		{
			addnow = get_wfbe_doku(parseInt(pair[1]));
		}
		else if(!$("#" + pair[0]).hasClass("dontcount"))
		{
			addnow = parseInt(pair[1]);
		}
		total += addnow;
		$(".points_" + pair[0]).html(addnow);
		if(addnow == 1)
			$(".pointsname_" + pair[0]).html('Punkt');
		else
			$(".pointsname_" + pair[0]).html('Punkte');
	}
	
	var bestanden = true;
	
	$(".testblock").each(function()
	{
		var blocktotal = 0;
		$(this).find(".points").each(function()
		{
			blocktotal += parseInt($(this).html());
		});
		$("." + $(this).attr("id").replace(/testblock/, "testblockreached")).html(blocktotal);
		$("." + $(this).attr("id").replace(/testblock/, "testblockreachedp")).html(blocktotal == 1 ? "Punkt" : "Punkte");
		var blockafford = parseInt($("." + $(this).attr("id").replace(/testblock/, "testblockreachedmax")).html());
		var blockdiff = blockafford - blocktotal;
		if(blockdiff <= 0)
		{
			$("." + $(this).attr("id").replace(/testblock/, "testblockreachedfail")).html('<span style="color: #229944;">Erforderliche Punkteanzahl erreicht.</span>');
			$("." + $(this).attr("id").replace(/testblock/, "testblockreachedfailmenu")).html('Erforderliche Punkte erreicht.');
		}
		else
		{
			if(blockafford > 0)
				bestanden = false;
			$("." + $(this).attr("id").replace(/testblock/, "testblockreachedfail")).html('Sie haben um ' + blockdiff + ' '+(blockdiff == 1 ? "Punkt" : "Punkte")+' zu wenig erreicht.');
			$("." + $(this).attr("id").replace(/testblock/, "testblockreachedfailmenu")).html('Es '+(blockdiff == 1 ? "fehlt" : "fehlen")+' noch ' + blockdiff + ' '+(blockdiff == 1 ? "Punkt" : "Punkte")+'.');
		}
	});
	
	if(bestanden)
	{
		$(".testtotalfail").html('<span style="color: #229944; font-weight: bold;">Erforderliche Punkteanzahl erreicht.</span>');
	}
	else
	{
		$(".testtotalfail").html('<span style="color: #EE344F; font-weight: bold;">Erforderliche Punkteanzahl in mindestens einem Teil nicht erreicht.</span>');
	}
	
	$(".testtotal").html(total);
}




function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	var i = '';
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) 
	{
		return null;
	}
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
