function goTo(url) {
	return location.href=url;
}

function confirmMsg(message, url) {
	if (confirm(message))
		goTo(url);
	else
		return false;
}

function fillInput(string, inputId) {

	var re = /(\\|\/)*(((\\|\/)+(\\|\/)*)*)/;
	var trimmed = string.replace(re, "$1");
	
	if (document.getElementById(inputId)) {
		document.getElementById(inputId).value = trimmed;
		document.getElementById(inputId).select();
		document.getElementById(inputId).focus();
		return true;
	}
		
	return false;
}

function showCategories() {
	document.getElementById('categories').style.display = 'block';
	document.getElementById('categories_selector').style.display = 'none';
}

function showSyllabuses() {
	document.getElementById('syllabuses').style.display = 'block';
	document.getElementById('syllabuses_selector').style.display = 'none';
}

function showSolution() {
	document.getElementById('after_check').style.display = 'block';
	document.getElementById('result').style.display = 'block';
}

if (!Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i=0; i<this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}

function markCorrect(spanId, wide) {
	
	var answerSpan = document.getElementById(spanId);
	var inputScore = document.getElementById('inp_score');
	var wide = (wide == null) ? "0" : wide;
	
	if (clicked.indexOf(spanId) == -1) {
		clicked.push(spanId);
		answersCorrect++;
		
		if (wide == 0)
			answerSpan.firstChild.className = "bg_green";
		else
			answerSpan.firstChild.className = "wide bg_green";
	}

	var scorePercent = document.getElementById('user_score_percent');
	var scoreAnswers = document.getElementById('user_score_answers');
			
	var percent = (answersCorrect/answersTotal*100) | 0;
	if (percent < 51)
		cls = "red";
	else if (percent < 90)
		cls = "orange";
	else
		cls = "green";
	
	scorePercent.className = "percent " + cls;
	scorePercent.innerHTML = percent + "%";
	
	scoreAnswers.className = "percent " + cls;
	scoreAnswers.innerHTML = answersCorrect + "/" + answersTotal;
	
	inputScore.value = percent;
}

function markCorrectWord(spanId) {
	
	var answerSpan = document.getElementById(spanId);
	var inputScore = document.getElementById('inp_score');
	
	if (clicked.indexOf(spanId) == -1) {
		clicked.push(spanId);
		answersCorrect++;
		
		inputs = answerSpan.getElementsByTagName("input");
		if(inputs[0]) {
			inputs[0].value = answerSpan.title;
			inputs[0].className = "bg_green";
		}
	}

	var scorePercent = document.getElementById('user_score_percent');
	var scoreAnswers = document.getElementById('user_score_answers');
			
	var percent = (answersCorrect/answersTotal*100) | 0;
	if (percent < 51)
		cls = "red";
	else if (percent < 90)
		cls = "orange";
	else
		cls = "green";
	
	scorePercent.className = "percent " + cls;
	scorePercent.innerHTML = percent + "%";
	
	scoreAnswers.className = "percent " + cls;
	scoreAnswers.innerHTML = answersCorrect + "/" + answersTotal;
	
	inputScore.value = percent;
}

function validateNewWord(new_word, new_translation) {
	
	new_word_input = document.getElementById(new_word);
	new_translation_input = document.getElementById(new_translation);

	if (new_word_input.value == "" || new_translation_input.value == "")
		return false;
		
	return true;
}

function validateEmptyField(new_word) {
	
	new_word_input = document.getElementById(new_word);

	if (new_word_input.value == "")
		return false;
		
	return true;
}

function popUp(url, parameters, name) {

	eval(name+" = window.open('"+url+"','"+name+"','"+parameters+"')");
	if (eval(name) && window.focus) eval(name).focus();
}



jQuery(document).ready(function() {
	
	jQuery(".popupTextFromTitle").click(function() {
		var text = jQuery(this).attr("title");
		var nw = window.open('','','width=700 ,height=600,toolbar=no,directories=no,menubar=no,locations=no,status=no,scrollbars=yes,resizable=no,fullscreen=no,left=100,top=50');
		nw.document.open();
		nw.document.write('<html><meta http-equiv="Content-type" content="text/html; charset=iso-8859-2" /><body>' + text + '</body></html>');
		nw.document.close();
		nw.focus();
	});
	
	jQuery("div.exercise_body input").autoGrowInput({comfortZone: 20, maxWidth: 450});

	jQuery(".tooltip, .exerciseHint, .info").tooltip({showURL: false});

});
