function loadHighscore(id) {	
	showLoadingCenter("#highscore .cont");
	$("#highscore .cont").load(baseUrl+'/test/highscore?id='+id);
}

function showNewTests(name) {
	$("#topTests").removeClass('entactual');
	$("#adminTests").removeClass('entactual');
	$("#newTests").removeClass('entactual');
	$("#olderTests").removeClass('entactual');
	
	$("#"+name+"Tests").addClass('entactual');
	
	showLoadingCenter("#entries table#ent0");
	$("#entries table#ent0").load(baseUrl+'/index/newtests?type='+name);
}

function showDayTests(name) {	
	showLoadingCenter("#comments table#ent0");
	$("#comments table#ent0").load(baseUrl+'/index/newtests?type='+name);
}


function addNamedElement(i) {
	$("div#addElements").append(questionHtml(i));

	id = i;
	i = $("div#element"+id+" div.options div.option-element").size()+1;
	
	while (getOptionValue(id, i) != '') {
		$("div#element"+id+" div.options").append(optionHtml(id, i));
		i++;
	}
	
	counts[id] = 0;
	
	if (i == 1) {
		while (i <= optionsCount) {
			$("div#element"+id+" div.options").append(optionHtml(id, i));
			i++;
			counts[id]++;
		}
	}
}

function addOption(id) {
	i = $("div#element"+id+" div.options div.option-element").size()+1;
	
	$("div#element"+id+" div.options").append(optionHtml(id, i));
	counts[id]++;
	
	$("#showAlert").fadeOut("slow");
}

function addElement() {
	elementsCount++;
	myElementsCount++;
	i = elementsCount;
	
	addNamedElement(i);	
	
	$("#showAlert").fadeOut("slow");
}

function removeQuestion(id) {	
	if (myElementsCount > 2) {
		$("div#"+id).remove();
		myElementsCount--;
	} else {
		$("#showAlert").load(baseUrl+"/xml/alert?type=info&text=too_few_questions").fadeIn("slow");
	}
}

function removeOption(question, option) {	
	if (counts[question] > 2) {
		$("div#element"+question+"option"+option).remove();
		counts[question]--;
	} else {
		$("#showAlert").load(baseUrl+"/xml/alert?type=info&text=too_few_options").fadeIn("slow");
	}
	
}

function removeElement(id) {	
	$("div#"+id).remove();
}

function addAllElements() {
	for (i = 1; i <= elementsCount; i++) {
		addNamedElement(i);
	}
}

function refreshSubcategories() {
	var categoryId = $("select#category").val();
	
	//$("select#subcategory option[value=0]").remove();
	$("select#subcategory").append('<option id="loading">Loading...</option>');
	$("select#subcategory").load(baseUrl+"/xml/subcategories?id="+categoryId);
	$("select#subcategory option#loading").remove();
}

function showComments(url) {
	//$("div#comments div").remove();
	$("div#comments").load(url);
}

function changeTestType(type, steps, disable) {
	$("span#stepsCount span").remove();
	$("span#stepsCount").append("<span>"+steps+"</span>");
	
	if (disable) {
		$("input#optionsCount").attr("disabled", "disabled");
		$("div#optionsCountDiv").addClass("disabled");
	} else {
		$("input#optionsCount").removeAttr("disabled");
		$("div#optionsCountDiv").removeClass("disabled");
	}
}

function removeLoading(name) {
	$(name+" *").remove();
}

function showLoading(name) {
	removeLoading(name);
	$(name).append('<img src="'+baseUrl+'/public/images/loading.gif" alt="loading..." />');	
}

function showLoadingCenter(name) {
	removeLoading(name);
	$(name).append('<center><img src="'+baseUrl+'/public/images/loading.gif" alt="loading..." /></center>');	
}

function sendToFriend(id) {
	var mail = $("input#test-link-mail").val();

	showLoadingCenter("div#test-link-alert");
	//$("div#test-link-alert div").remove();
	//$("div#test-link-alert").append('<img src="'+baseUrl+'/public/images/loading.gif" alt="loading..." />');
	$("div#test-link-alert").load(baseUrl+'/test/mail?id='+id+'&mail='+mail);
}

function removeRequest(id) {
	var reason = $("input#test-remove-reason").val();

	showLoadingCenter("div#test-remove-alert");

	$("div#test-remove-alert").load(baseUrl+'/test/removerequest?id='+id+'&reason='+encodeURI(reason));
}

/**
 * Check if all questions has answers
 */
function checkTest(message) {
	var n;
	var i;
	var answered = new Array();
	var success = true;
	
	var button = $("div#test-submit div").html();
	showLoading("div#test-submit");
	
	$("li.question").addClass('unchecked');
	
	$("ul.test-options input").each(
		function (index) {
			n = $(this).attr('name').split('n');
			i = n[1];
			if ($(this).attr('checked')) {
				answered[i] = true;
				nParent = $(this).attr('class').split('n');
				iParent = nParent[1];
				$("#question"+iParent).removeClass('unchecked');
			}
		}
	);
	

	
	
	$("ul.test-options input").each(
		function (index) {
			n = $(this).attr('name').split('n');
			i = n[1];
			if (answered[i] && success) {
				success = true;
			} else {
				success = false;
			}
		}
	);
	
	if (!success) {
		removeLoading("div#test-submit");
		$("div#test-submit").append('<strong>'+message+'</strong><br /><div>'+button+'</div>');
	} else {
		document.getElementById("test-form").submit();
	}

	//return success;
}

function changeHtmlExport() {
	var width = $("#iframeWidth").val();
	var height = $("#iframeHeight").val();
	var color = $("#iframeColor").val();
	var replacement = /#/;
	var bgcolor = $("#iframeBgcolor").val().replace(replacement, '@');
	
	var htmlExport = '<script language="JavaScript" src="'+testExportUrl+'?width='+width+'&height='+height+'&color='+color+'&bgcolor='+bgcolor+'"></script>';
				
	$("#htmlExport").fadeOut('slow', function() {
		$("#htmlExport").attr("value", htmlExport).fadeIn('slow');
	});
}

var countSmilies = 0;

function smilie(textarea, text) {
	if (countSmilies < 5) {
		countSmilies++;
		textarea = document.getElementById(textarea);
		if (document.selection) {
			textarea.focus();
			vyber = document.selection.createRange();
			vyber.text = text;
		} else if (textarea.selectionStart || textarea.selectionStart == 0) {
			startPos = textarea.selectionStart;
			endPos = textarea.selectionEnd;
			textarea.value = textarea.value.substring(0, startPos) + text + textarea.value.substring(endPos, textarea.value.length);
		} else {
			textarea.value += text;
		}
	}
}

var showPublicTest = true;
function publicTest() {
	if (showPublicTest) {
		$("#category").attr("disabled", "disabled");
		$("#category").addClass("disabled");
 		$("#subcategory").attr("disabled", "disabled");
		$("#subcategory").addClass("disabled");
		showPublicTest = false;
	} else {
		$("#category").removeAttr("disabled", "disabled");
		$("#category").removeClass("disabled");
		$("#subcategory").removeAttr("disabled");
		$("#subcategory").removeClass("disabled");
		showPublicTest = true;
	}
}

function showTestLink(id, element) {	
	$(element).fadeOut("slow");
	$(element).load(baseUrl+"/xml/link?id="+id).fadeIn("slow");
}

function changeCategory(id) {	
	$('#changeCategory').fadeOut("slow");
	$('#changeCategory').load(baseUrl+"/admin/changecategory?id="+id).fadeIn("slow");
}

function closeBlock(element) {	
	$(element).fadeOut("slow");
}


function changeColor(name, color) {
	$("#"+name).attr("value", "#"+color);
	$("#"+name+"Img").attr("src", baseUrl+'/public/phpscripts/circle.php?color='+color);
}

function changeColors(c1, c2, c3, c4, c5, c6, c7) {
	changeColor('fontColor', c1);
	changeColor('headerColor', c2);
	changeColor('borderColor', c3);
	changeColor('bodyColor', c4);
	changeColor('backgroundColor', c5);
	changeColor('footerColor', c6);
	changeColor('questionColor', c7);
}

resShown = false;
function showResizable() {
	if (!resShown) {
		//$("#resizeImage").append('<img class="resizable" src="'+baseUrl+'/public/images/test-sample.png" width="'+$("#width").val()+'" height="'+$("#height").val()+'" alt="resize" />');
		startResize();
		//$("#showResize").removeClass('btnMeassure').addClass('btnOK');
		resShown = true;
	} else {
		//$("#resizeImage div, #resImage span, #resImage img").remove();
		//$("#showResize").removeClass('btnOK').addClass('btnMeassure');
		//resShown = false;
	}
}

function help(name) {
	$("em.help").hide();
	Help(name);
	window.setTimeout('$("#'+name+'-help").fadeOut("slow")', 3000);
}

/**
 * switching between rounded and normal corners
 */
function roundCorners(round, name) {
	help(name);
	if (!round) {
		$('#footerColorCo').removeClass('hidden');
		$('#borderColorCo').removeClass('hidden');
		$('#headerColorCo').removeClass('hidden');
		$('#headerColorRound').addClass('hidden');
	} else {		
		$('#footerColorCo').addClass('hidden');
		$('#borderColorCo').addClass('hidden');
		$('#headerColorCo').addClass('hidden');
		$('#headerColorRound').removeClass('hidden');
	}
}

var titleVal = '';
function showTitle() {
	var ch = document.getElementById("sTitle").checked;
	
	if (ch) {
		$("#title").attr('disabled', '').attr('value', titleVal);
	} else {
		titleVal = $("#title").val();
		$("#title").attr('disabled', 'disabled').val('');
	}
}

function ch(name) {
	if (document.getElementById(name).checked) {
		return 1;
	} else {
		return 0;
	}	
}

function customExport(id) {
	//$("#wwwPost").load(baseUrl+'/test/wwwpost?id='+id);
	var data = {title: $('#title').val(),
	width: $('#width').val(),
	height: $('#height').val(),
	fontFace: $('#fontFace').val(),
	fontSize: $('#fontSize').val(),
	corners: $('input[name=corners]:checked').val(),
	headerColor: $('#headerColor').val(),
	headerColor2: $('#headerColor2').val(),
	fontColor: $('#fontColor').val(),
	borderColor: $('#borderColor').val(),
	bodyColor: $('#bodyColor').val(),
	backgroundColor: $('#backgroundColor').val(),
	footerColor: $('#footerColor').val(),
	questionColor: $('#questionColor').val(),
	badQuestionColor: $('#badQuestionColor').val(),
	isLink: ch('isLink'),
	quality: ch('quality'),
	bar: ch('bar'),
	button: ch('button')}
	
	$("#wwwPost").fadeOut('slow');
	$.post(baseUrl+'/test/wwwpost?id='+id, data, function (data) {
		$("#wwwPost").html(data);
		$("#wwwPost").fadeIn('slow');
		var src = $("iframe").attr('src');
		$("iframe").attr('src', src);
		$("iframe").attr('width', $('#width').val());
		$("iframe").attr('height', $('#height').val());
		});
}

var htmlExportVal = '';
var iframeSrc = '';
function useStyle(testId) {
	if (htmlExportVal == '') {
		htmlExportVal = $('#htmlExport').val();
		iframeSrc = $("iframe").attr('src');
	}
	
	if (testId > 0) {
		$('#htmlExport').fadeOut('slow').val(htmlExportVal.replace('/js">', '/js?useStyle='+testId+'">')).fadeIn('slow');
		$("iframe").attr('src', iframeSrc.replace('?useStyle=0', '?useStyle='+testId));
	} else {
		$('#htmlExport').val(htmlExportVal);
		$("iframe").attr('src', iframeSrc);
	}
}

