var showId = null;
var showInAction = false;
var winner = null;
var hidden = new Array();
var doQuerySleep = false;
function show(id) {
	if (showInAction || id == showId) return;
	var numId = new String(id).replace(/popup_/, '');
	for (x in hidden) {
		if (hidden[x] == numId) return;
	}
	if (document.getElementById('close_'+numId).innerHTML2)
		document.getElementById('close_'+numId).innerHTML = document.getElementById('close_'+numId).innerHTML2;
	showInAction = true;
	setTimeout(function() { showInAction = false; }, 500);
	if (showId != null) {
		hide(showId);
		setTimeout('show("'+id+'")', 500);
	} else {
		showId = id;
		opacity(id, 0, 95, 500);
	}
}
function hide(id) {
	showId = null;
	var numId = new String(id).replace(/popup_/, '');
	document.getElementById('close_'+numId).innerHTML2 = document.getElementById('close_'+numId).innerHTML;
	document.getElementById('close_'+numId).innerHTML = '';
	setWinner(numId, 0);
	resetDiff(numId);
	document.getElementById('text_'+numId).innerHTML = '';
	document.getElementById('klaida_p_'+numId).innerHTML = '';
	opacity(id, 95, 0, 500);
}
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}
//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
	if (opacity == 0) object.display = "none";
	else object.display = "inline";
}
function resetDiff(id) {
	var el = document.getElementById('persvara_'+id);
	el.value = '';
}
function resetWinner(w1, w2) {
	var i1 = new String(w1.innerHTML);
	w1.innerHTML = i1.replace(/<b>/i, "");
	w1.style.textDecoration = 'none';
	
	var i2 = new String(w2.innerHTML);
	w2.innerHTML = i2.replace(/<b>/i, "");
	w2.style.textDecoration = 'none';
}
function setWinner(id, wid) {
	var w1 = document.getElementById('_'+id+'_winner1');
	var w2 = document.getElementById('_'+id+'_winner2');
	
	document.getElementById('klaida_w_'+id).innerHTML = '';
	resetWinner(w1, w2);
	if (wid == 1) {
		w1.innerHTML = '<b>'+w1.innerHTML+'</b>';
		w1.style.textDecoration = 'underline';
		winner = 1;
	} else if (wid == 2) {
		w2.innerHTML = '<b>'+w2.innerHTML+'</b>';
		w2.style.textDecoration = 'underline';
		winner = 2;
	} else if (wid == 0) {
		winner = null;
	}
}
function patikrintiPersvara(id) {
	var err2 = document.getElementById('klaida_p_'+id);
	var pEl = document.getElementById('persvara_'+id);
	var persvara_val = pEl.value;
	if (persvara_val == '' || !isIntegerInRange(persvara_val, 1, 999)) {
		err2.innerHTML = 'Įveskite persvarą (1-999)';
		return false;
	} else {
		err2.innerHTML = '';
	}
	return true;
}
function doQuery(id) {
	if (doQuerySleep) return;
	doQuerySleep = true;
	setTimeout(function() { doQuerySleep = false; }, 1000);
	for (x in hidden) {
		if (hidden[x] == id) return;
	}
	var persvara_val = document.getElementById('persvara_'+id).value;
	var error = false;
	var err1 = document.getElementById('klaida_w_'+id);
	err1.innerHTML = '';
	
	if (winner == null) {
		err1.innerHTML = 'Pasirinkite laimėtoją';
		error = true;
	}
	if (!patikrintiPersvara(id)) error = true;
	if (error) return;
	if (xmlHttp == null) return;
	if (xmlHttp.readyState != 4) xmlHttp.abort();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			var string = new String(xmlHttp.responseText);
			var code = string.substr(0, 1);
			var msg = string.substr(2);
			document.getElementById('text_'+id).innerHTML = msg;
			if (code == 1) {
				showInAction = true;
				setTimeout(function() { showInAction = false; }, 2000);
				setTimeout(function() { var speti = document.getElementById('speti_'+id); speti.innerHTML = ''; }, 1500);
				setTimeout('hide("popup_'+id+'")', 1000);
				hidden[hidden.length+1] = id;
				document.getElementById('close_'+id).innerHTML2 = document.getElementById('close_'+id).innerHTML;
				document.getElementById('close_'+id).innerHTML = '';
			}
		}
	}
	xmlHttp.open("GET","/krepsinis/spejimas.php?laimetojas="+winner+"&persvara="+persvara_val+"&id="+id,true);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(null);
}
// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
function isIntegerInRange (s, a, b) {
	if (isEmpty(s))
	if (isIntegerInRange.arguments.length == 1) return false;
	else return (isIntegerInRange.arguments[1] == true);

	// Catch non-integer strings to avoid creating a NaN below,
	// which isn't available on JavaScript 1.0 for Windows.
	if (!isInteger(s, false)) return false;

	// Now, explicitly change the type to integer via parseInt
	// so that the comparison code below will work both on
	// JavaScript 1.2 (which typechecks in equality comparisons)
	// and JavaScript 1.1 and before (which doesn't).
	var num = parseInt(s);
	return ((num >= a) && (num <= b));
}

function isInteger(s) {
	var i;

	if (isEmpty(s))
	if (isInteger.arguments.length == 1) return 0;
	else return (isInteger.arguments[1] == true);

	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}

	return true;
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}

function isDigit(c) {
	return ((c >= "0") && (c <= "9"));
}

function openChat(area) {
	var logo = document.getElementById('logotipas');
	logo.src = templatePath+'images/logonc.png';
	area.coords = '0, 0, 0, 0';
	window.open(chatPath, 'livechat', 'toolbar=0,location=0,statusbar=0,menubar=0,resizable=0,scrollbars=yes,width=650,height=600,left=197,top=59');
}
