var lpartides = new Array();
var lreptes = new Array();
var numlPartides=0;
var numlReptes=0;

//------------ PARTIDES ------------
function escriurePartida() {
str = "<tr><td><img src='imatges/logomini" + this.tip + ".gif' alt='Partida'></td>";
str += "<td><img src='imatges/blank.gif' height='0' width='108' border='0'><br><a href=\'javascript:partida(" + this.num + ");\' title='Obrir partida'>";
txt="";
if (this.torneig=="R") txt="RĀPIDES";
else if (this.torneig) txt="TORNEIG";
if (txt) str+= "<font color='#803030'><strong>" + txt + "</strong></font><br>";
if (this.res) str+= this.data + "<br>";
if (this.mous && !this.res) str+="<font color='#C02010'><strong>" + this.mous + "</strong></font><br>";
str += this.jug1 + ((this.jug1elo && !this.res)?" [" + this.jug1elo + "]":"") + " - ";
str += this.jug2 + ((this.jug2elo && !this.res)?" [" + this.jug2elo + "]":"");
if (this.res) str+= " (" + this.res + ")";
str += "</a></td></tr>";
return str;
}

function Partida(num, data, tip, jug1, jug1elo, jug2, jug2elo, res, mous, torneig) {
	this.num = num;   		this.data = data;
	this.tip = tip;   		this.jug1 = jug1;
	this.jug1elo = jug1elo; this.jug2 = jug2;
	this.jug2elo = jug2elo; this.res = res;
	this.mous = mous; 		this.torneig = torneig;
	this.escriure = escriurePartida;
}

function altapartida(num, data, tip, jug1, jug1elo, jug2, jug2elo, res, mous, torneig) {
	trobat=0;
	for (i=0;i<numlPartides;i++)
		if (lpartides[i].num==num) {
			trobat=1;
			if (lpartides[i].res=="" && res!="") avisAfegir("La partida " + jug1 + " - " + jug2 + " ha finalitzat amb resultat " + res + ".", 5); 
			lpartides[i].data = data;   lpartides[i].tip = tip;
			lpartides[i].jug1 = jug1;   lpartides[i].jug1elo = jug1elo;
			lpartides[i].jug2 = jug2;   lpartides[i].jug2elo = jug2elo;
			lpartides[i].res = res;     lpartides[i].mous = mous;
		}
	
	if (!trobat) {
		lpartides[numlPartides++] = new Partida(num, data, tip, jug1, jug1elo, jug2, jug2elo, res, mous, torneig);
		avisAfegir("Acaba de comenįar la partida " + jug1 + " - " + jug2 + ".", 4);
	}
}

//----------------- REPTES -------------------------

function escriureRepte() {
textConf = (this.tip=="N"? "Estās segur que vols iniciar aquesta partida?":"Amb aquesta opciķ iniciarās aquesta partida que NO ÉS D´ESCACS CLĀSSICS SINĶ D´UNA VARIANT. Si no estās segur de quč significa aixō, consulta l´ajuda i fes clic sobre l´apartat de modalitats abans d´acceptar el repte.      Estās segur que vols iniciar aquesta partida?");

str = "<tr><td><img src='imatges/logomini" + this.tip + ".gif' alt='Repte'></td>";
str += "<td><img src='imatges/blank.gif' height='0' width='108' border='0'><br>";
if (!this.propi) {
	str += "<a href=\'javascript:confirmarInferior(\"";
	
	str += textConf + "\",\"Inferior.php?accio=LLISTES&inmenu="+(this.col=="Blanques"?"2":"0")+"&acceptar_repte=1&num_repte=" + this.num + "&menu=1\")' title='Acceptar repte'> Origen: <strong>" + this.orig + " (" + this.elo + ")</strong><br>";
	str += "Color: " + this.col + "</a>";
	if (!this.propi && this.dest) {
		str+= "<br><font color='#C02010'><strong>Repte personal</strong><br>L\'acceptes? </font><a href=\'javascript:confirmarInferior(\"" + textConf + "\",\"Inferior.php?accio=LLISTES&inmenu="+(this.col=="Blanques"?"2":"0")+"&acceptar_repte=1&num_repte=" + this.num + "&menu=1\")\' title='Acceptar repte personal'><font color='#C02010'><strong>Sí</strong></font></a>  <a href=\'javascript:confirmarInferior(\"Estās segur de que vols rebutjar aquest repte?\",\"Inferior.php?accio=LLISTES&inmenu=3&denegar_repte=1&num_repte=" + this.num + "&menu=3\")\' title='Declinar repte personal'><font color='#C02010'><strong>No</strong></font></a>";
		}
} else {
	str += this.nocolor + " [" + this.elomin + "," + this.elomax + "]<br>" + (this.dest? "a " + this.dest + " ":"");
	str += "<a href=\"javascript:confirmarInferior(\'Estās segur que vols eliminar aquest repte?\',\'Inferior.php?accio=LLISTES&inmenu=4&eliminar_repte=1&num_repte=" + this.num + "\')\" title='Eliminar repte'>eliminar</A>";		
	}
str += "</td></tr>";
return str;
}


function Repte(num, tip, orig, dest, elo, elomin, elomax, col, propi, actiu) {
	this.num = num;			this.tip = tip;
	this.orig = orig;		this.dest = dest;
	this.elo = elo;			this.elomin = elomin;
	this.elomax = elomax; 	this.col = col; if (this.col=="Blanques") this.nocolor="Negres"; else this.nocolor="Blanques";
	this.propi = propi; 	this.actiu = actiu;
	this.escriure = escriureRepte;
}

function altarepte(num, tip, orig, dest, elo, elomin, elomax, col, propi, actiu) {
	trobat=0;
	for (i=0;i<numlReptes;i++)
		if (lreptes[i].num==num) {
			trobat=1;
			lreptes[i].num = num;		lreptes[i].tip = tip;
			lreptes[i].orig = orig;		lreptes[i].dest = dest;
			lreptes[i].elo = elo;		lreptes[i].elomin = elomin;
			lreptes[i].elomax = elomax; lreptes[i].col = col;
			lreptes[i].propi = propi; 	lreptes[i].actiu = actiu;
		}
	if (!trobat) lreptes[numlReptes++] = new Repte(num, tip, orig, dest, elo, elomin, elomax, col, propi, actiu);
	if (dest) avisAfegir("L'usuari " + orig + " t'ha creat un repte personal.", 1);
	else if (tip=='N') avisAfegir("L'usuari " + orig + " ha creat un nou repte.", 0);
	else avisAfegir("Hi ha reptes oberts per jugar variants.",3);
}


//-------------------------------------------------------------

function escriuSeparador() {
return "<tr height='1' ><td colspan='2'><img src='imatges/gris.gif' height='1' width='135' border='0'></td></tr>";
}

function partidesencurs() {
	str=""; primer=1;
	for (i=0;i<numlPartides;i++)
		if (!lpartides[i].res) {
			if (!primer) str += escriuSeparador();
			str += lpartides[i].escriure();
			primer=0;
		}
	if (primer) str="<tr><td>No hi ha partides en curs</td></tr>";
	return str;
}
function partidesontumous() {
	str=""; primer=1;
	for (i=0;i<numlPartides;i++)
		if (!lpartides[i].res && lpartides[i].mous) {
			if (!primer) str += escriuSeparador();
			str += lpartides[i].escriure();
			primer=0;
		}
	if (primer) str="<tr><td>No et toca moure en cap partida</td></tr>";
	return str;
}
function partidesacabades() {
	str=""; primer=1;
	for (i=0;i<numlPartides;i++)
		if (lpartides[i].res) {
			if (!primer) str += escriuSeparador();
			str += lpartides[i].escriure();
			primer=0;
		}
	if (primer) str="<tr><td>No hi ha partides acabades</td></tr>";
	return str;
}

function reptespropis() {
	str=""; primer=1;
	for (i=0;i<numlReptes;i++)
		if (lreptes[i].propi && lreptes[i].actiu) {
			if (!primer) str += escriuSeparador();
			str += lreptes[i].escriure();
			primer=0;
		}
	if (primer) str="<tr><td>No tens cap repte pendent</td></tr>";
	return str;
}

function reptesaliens() {
	str=""; primer=1;
	for (i=0;i<numlReptes;i++)
		if ((!lreptes[i].propi) && lreptes[i].actiu) {
			if (!primer) str += escriuSeparador();
			str += lreptes[i].escriure();
			primer=0;
		}
	if (primer) str="<tr><td>No hi ha reptes vigents</td></tr>";
	return str;
}

function hihareptespersonals() {
	for (i=0;i<numlReptes;i++)
		if ((!lreptes[i].propi) && lreptes[i].actiu && lreptes[i].dest) {
			return "<br><div style='font-weight:bold;color:#922'>&nbsp;(tens reptes pendents)</div>";
		}
	return "";
}
/*function capcM(txt) {return "<table width='100%'><tr><td><div style='background:#C0D4AB; border: 1px solid gray; font-size:9;' width='120px'>&nbsp;"+txt+"<table width='97%'>";}
function opcM1(nO,tO,cO,opO) {
	return "<tr><td><div class='opcio1' style='font-size:9; border: 1px solid #606060; width:140px; background:" + cO + "'><a href=\'" + 
	(opO?opO:"javascript:menu("+nO+")") + "\'>&nbsp;" + tO + "</a></div></td></tr>";}
function opcM2(nO,tO,cO,opO,fopO) {if (fopO=="L") return "<tr><td>" + eval(opO) + "</td></tr>";
else return "<tr><td>&nbsp;<a href=\'" + (fopO=="C"?"javascript:carregaText(\""+opO+"\")":opO) + "\'>" + tO + "</a></td></tr>";}
*/

function capcM(txt) {return "<table width='100%'><tr><td><div style='font-weight:bold; font-size:9;color:#334;margin:0px;padding:0px;' width='120px'>&nbsp;"+txt+"<table width='97%'>";}
function opcM1(nO,tO,cO,opO) {
	return "<tr><td><div class='opcio1' style='font-size:9; border: 1px solid #AAA; width:140px; background:" + cO + "'><a href=\'" + 
	(opO?opO:"javascript:menu("+nO+")") + "\'>&nbsp;" + tO + "</a></div></td></tr>";}
function opcM2(nO,tO,cO,opO,fopO) {if (fopO=="L") return "<tr><td>" + eval(opO) + "</td></tr>";
else return "<tr><td>&nbsp;<a href=\'" + (fopO=="C"?"javascript:carregaText(\""+opO+"\")":opO) + "\'>" + tO + "</a></td></tr>";}

