﻿var	Total 	= 0;
var ImgDir	= "./img2chess/inediti/";
var	ImgPre	= "ES";
var	ImgExt	= "png";
var Paper 	= "white";

/* Dicembre 2006 pubblicati da 1 a 19 */
/* Gennaio 2007 pubblicati da 20 a 34 */
var	LastUpdate 	    = '12/2007';
var	NextSolution    = '2/2007'
var	MaxProblem 	    = 300;
var Originals 		= new Array (MaxProblem);
var	Solutions		= new Array (MaxProblem);
var	Comments		= new Array (MaxProblem);
/* 
**	dichiarazione oggetti Problem e Tourney 
*/
function Problem (n, author, country, stip, pieces, solution, date, a, h)
{
	var	nstr = n < 10 ? '000' + n : n < 100 ? '00' + n : n < 1000 ? '0' + n : n;
	
	this.Id       = n;
	this.Author = author;
	this.Country= country;
	this.Image	= ImgDir + ImgPre + nstr + '.' + ImgExt;
	this.Stip		= stip;
	this.Pieces	= pieces;
	this.Solution= 'i' + "<font size=3>" + solution + "</font>"; // 'i' = italiano
	this.Date	= date;
	this.Aiuto	= a ? a : '';
	this.Help	= h ? h : this.Aiuto;
		
	return this;
}

function	Tourney (name, judge)
{
	this.Name	= name;
	this.Judge 	= judge;
	this.Total	= 0;
	this.Problems = new Array (MaxProblem);
	return this;
}

function	ProblemToTourney (p, t)
{
	t.Problems [t.Total++] = p.Id - 1;
}

/* 
**	traduce la soluzione 
*/
var	ItChars	= 'RDTACPGNZV';
var	DeChars	= 'KDTLSBGNZW';
var	EnChars	= 'KQRBSPGNZW';
var	FrChars	= 'RDTFCPSNZV';

var	ItChars2 ='RNDNTNANCNPNRBDBTBABCBPBRnDnTnAnCnPnGnNnIMLiGNPAMAVALEALSCDRGHBPCR';
var	DeChars2 ='SKSDSTSLSSSBWKWDWTWLWSWBnKnDnTnLnSnBGnNnEMLiNHPAMAVALELLEIDRGHBPHA';
var	EnChars2 ='BKBQBRBBBSBPWKWQWRWBWSWPnKnQnRnBnSnPnGnNEMLiNHPAMAVALEBLSQDRGHBPHA';
var	FrChars2 ='RNDNTNFNCNPNRBDBTBFBCBPBRnDnTnFnCnPnSnNnEMLiNHPAMAVALEFLECDRGHBPHA';
function	Translate (str, from ,to)
{
	var	i, pos;
	var	newstr = "";
	var	intag = 0;
	var	fmchars, fmchars2;
	var	tochars, tochars2;
	
	if (from == to)
		return str;
	if (from == '!')		// string ToBeSolved
	{
		switch (to)
		{
			case 'i' : return '!<font size=3>La soluzione sar&agrave; pubblicata il prossimo ' + NextSolution + "</font>";
			case 'e': return '!<font size=3>The solution will be published next ' + NextSolution + "</font>";
			case 'f': return '!<font size=3>La solution sera &eacute;dit&eacute;e le' + NextSolution + '</font>';
			case 'd': return '!<font size=3>Die L&ouml;sung wird folgender ' + NextSolution + ' ver&ouml;ffentlicht</font>';
		}
	}
	switch (from)
	{
		case 'd': 
			fmchars 	= DeChars;
			fmchars2	= DeChars2;
			break;
		case 'e': 
			fmchars 	= EnChars;
			fmchars2	= EnChars2;
			break;
		case 'f': 
			fmchars 	= FrChars;
			fmchars2	= FrChars2;
			break;
		case 'i':
			fmchars		= ItChars;
			fmchars2	= ItChars2;
			break;
		default:
			return str;
	}
	switch (to)
	{
		case 'd': 
			tochars 		= DeChars;
			tochars2	= DeChars2;
			break;
		case 'e': 
			tochars 		= EnChars;
			tochars2	= EnChars2;
			break;
		case 'f': 
			tochars 		= FrChars;
			tochars2	= FrChars2;
			break;
		case 'i':
			tochars		= ItChars;
			tochars2	= ItChars2;
			break;
		default:
			return str;
	}
	
	newstr = to;
	var intag = 0;
	for (i = 1; i < str.length; i++)
	{
		var c = str.charAt (i);
		switch (c)
		{
			case '|': intag = 1; break;
			case '<': intag++; break;
			case '>': intag--; break;
		}
		if (intag)
			newstr += c;
		else
		{
			if (i + 1 < str.length)
			{
				var s2 = c + str.charAt (i + 1)
				pos = fmchars2.indexOf (s2);
				if (pos >= 0)
				{
					newstr += tochars2.charAt (pos);
					newstr += tochars2.charAt (pos + 1);
					i++;
					continue;
				}
			}
			pos = fmchars.indexOf (c);
			newstr += pos < 0 ? c : tochars.charAt (pos);
		}
	}
	return newstr;
}

/*
**
*/
function	SetPaper (col)
{
	Paper = col;
}
/*
** rende visibile la soluzione nella lingua scelta
*/
function	ToggleSolLang	(id, lang)
{
	var tdid = document.getElementById (id);

	if (!tdid)
		alert ("Errore software");
		
	tdid.style.color = lang == 'x' ? Paper : 'black';
	
	var	str 		= tdid.innerHTML;
	var	fmlang		= str.charAt (0);
	tdid.innerHTML 	= Translate (str, fmlang, lang);
}
/*
** crea anchor alla pagina delle definizioni (it o en)
*/
function	Define (word, ref)
{
	var	w, r, def;
	
	if (word.charAt (0) == 'i')
		def = 'definizioni_ita.html';
	else
		def = 'definizioni_eng.html';
	w = word.slice (1);
	r = ref ? ref : w;
	document.write ("&nbsp;<a href='" + def + "#" + r.toLowerCase () + "'>" + w + "</a>&nbsp;");
}
/*
** crea Define dalla stringa visibile (csv SENZA spazi)
*/
function	WriteHelp (str, lang)
{
	var	w;
	var	i;
	
	w = "";
	for (i = 0; i < str.length; i++)
	{
		if (str.charAt (i) == ',')
		{
			Define (lang + w);
			w = '';
		}
		else
			w += str.charAt (i);
	}
	Define (lang + w);
}

/*
** crea l'html per un problema 
*/
function WriteProblem (p)
{
	document.write ("<table class='articolo' border=0>");
	document.write ("<tr>");
		document.write ("<td colspan=2>")
			document.write ("<a name='ESP" + p.Id + "'></a>");
			document.write (p.Id + ".&nbsp;<b>");
			document.write (p.Author + "</b><br><center>" + p.Country + "</center>");
		document.write ("<td align=right><small>pubblicato/published on <b>" + p.Date + "</b></small>");
	document.write ("<tr>");
		document.write ("<td colspan=2 width='330px' valign='middle'>")
			document.write ("<center><img width='320px' alt='ESP" + p.Id +"' src='" + p.Image + "'></center>");
			document.write ("<td class='soluzione' width='470px' rowspan=4 valign=top>");
			document.write ("<button onclick=\"ToggleSolLang('sol" + p.Id + "', 'i')\"><img alt='it' src='img2chess/immagini/italiano.png' width=35'></button>"); 
			document.write ("<button onclick=\"ToggleSolLang('sol" + p.Id + "', 'e')\"><img alt='en' src='img2chess/immagini/inglese.png' width=35'></button>"); 
			document.write ("<button onclick=\"ToggleSolLang('sol" + p.Id + "', 'd')\"><img alt='de' src='img2chess/immagini/tedesco.png' width=35'></button>"); 
			document.write ("<button onclick=\"ToggleSolLang('sol" + p.Id + "', 'f')\"><img alt='fr' src='img2chess/immagini/francese.png' width=35'></button>"); 
			document.write ("<button onclick=\"ToggleSolLang('sol" + p.Id + "', 'x')\"><img alt='hide' src='img2chess/immagini/vuoto.png' width=35'></button>"); 
//			document.write ("<button onclick=\"WriteComment (" + p.Id + ")\"><img alt='comment' src='img2chess/immagini/hand.png' width=35'><img alt='comment' src='img2chess/immagini/mail.png' width=35'></button>"); 
//			document.write ("<button onclick=\"ReadComment (" + p.Id + ")\"><img alt='read' src='img2chess/immagini/read.png' width=35'></button><br />"); 
			document.write ("<br>");
			document.write ("<div class=sol id='sol" + p.Id + "'>");
			if (Solutions [p.Id - 1])
			{
				document.write (p.Solution);
				document.write ("|<font size=2><br><br>" + Comments [p.Id - 1]);
			}
			else
				document.write ('!');
			document.write ("</div>");
	document.write ("<tr>");
		document.write ("<td valign=top width='275px' ><b>" + p.Stip + "</b>");
		document.write ("<td valign=top width='55px' align=right style='font-size:12px'>" + p.Pieces);
	document.write ("<tr>");
	document.write ("<td colspan=2>&nbsp;");
	if (p.Aiuto)
	{
		document.write ("<b><font color=red>?(it)&nbsp;&nbsp;</font></b>");
		WriteHelp (p.Aiuto, 'i');
	}
	document.write ("<tr>");
	document.write ("<td colspan=2>&nbsp;");
	if (p.Help)
	{
			document.write ("<b><font color=red>?(en)</font></b>");
			WriteHelp (p.Help, 'e');
	}
//	document.write ("<tr><td colspan=3 style='background:navy; height:8'>");
	document.write ("</table>");
}

function WriteProblemAward (p)
{
	document.write ("<table border=0>");
	document.write ("<tr>");
		document.write ("<td colspan=2>")
			document.write ("<a name='ESP" + p.Id + "'></a>");
			document.write (p.Id + ".&nbsp;<b>");
			document.write (p.Author + "</b><br><center>" + p.Country + "</center>");
		document.write ("<td align=right><small>pubblicato/published on <b>" + p.Date + "</b></small>");
	document.write ("<tr>");
		document.write ("<td colspan=2 width='330px' valign='middle'>")
			document.write ("<center><img width='320px' alt='ESP" + p.Id +"' src='" + p.Image + "'></center>");
			document.write ("<td class='soluzione' width='470px' rowspan=4 valign=top>");
			document.write ("<button onclick=\"ToggleSolLang('sol" + p.Id + "', 'i')\"><img alt='it' src='img2chess/immagini/italiano.png' width=35'></button>"); 
			document.write ("<button onclick=\"ToggleSolLang('sol" + p.Id + "', 'e')\"><img alt='en' src='img2chess/immagini/inglese.png' width=35'></button>"); 
			document.write ("<button onclick=\"ToggleSolLang('sol" + p.Id + "', 'd')\"><img alt='de' src='img2chess/immagini/tedesco.png' width=35'></button>"); 
			document.write ("<button onclick=\"ToggleSolLang('sol" + p.Id + "', 'f')\"><img alt='fr' src='img2chess/immagini/francese.png' width=35'></button>"); 
			document.write ("<button onclick=\"ToggleSolLang('sol" + p.Id + "', 'x')\"><img alt='hide' src='img2chess/immagini/vuoto.png' width=35'></button>"); 
//			document.write ("<button onclick=\"WriteComment (" + p.Id + ")\"><img alt='comment' src='img2chess/immagini/hand.png' width=35'><img alt='comment' src='img2chess/immagini/mail.png' width=35'></button>"); 
//			document.write ("<button onclick=\"ReadComment (" + p.Id + ")\"><img alt='read' src='img2chess/immagini/read.png' width=35'></button><br />"); 
			document.write ("<br>");
			document.write ("<div class=sol id='sol" + p.Id + "'>");
			if (Solutions [p.Id - 1])
			{
				document.write (p.Solution);
			}
			document.write ("</div>");
	document.write ("<tr>");
	document.write ("<td valign=top width='275px' ><b>" + p.Stip + "</b>");
	document.write ("<td valign=top width='55px' align=right style='font-size:12px'>" + p.Pieces);
	document.write ("</table>");
	document.write ("<scr" + "ipt>ToggleSolLang('sol" + p.Id + "', 'i')\"</scr" + "ipt>"); 
}
/*
** crea l'Html per tutti i problemi di un Torneo
*/
function	WriteTourney (n, t)
{
	document.write ("<table class='articolo' width='800px'><tr><td>");

	document.write ("<a name='TT" + n + "'></a>");
	document.write ("<div width='800px' bgcolor=white class='torneo'>");
	document.write ("<br/>" + t.Name + "&nbsp;&nbsp;&nbsp;&nbsp;(" + t.Judge + ")");
	document.write ('<br><font color=red>' + t.Total + "</font> inediti/originals");
	document.write ("<br/><br/></div>");
	document.write ("</table>");
	for (var i = 0; i < t.Total; i++)
		WriteProblem (Originals [t.Problems [i]]);
//	document.write ('<hr>');
}
function	WriteOriginalsPage ()
{
	document.write ("<table class='articolo'><tr><td>");
	document.write ("Le soluzioni sono visibili cliccando sulla bandierina della lingua scelta");
	document.write (" <a href='mailto:marco.bonavoglia" + "@eteroscacco.it'>" + "Inviate i commenti al redattore</a>");
	document.write ("<br>Solutions are visible by clicking on the language flag");
	document.write (" <a href='mailto:marco.bonavoglia" + "@eteroscacco.it'>" + "Please, send comments to the editor</a>");
	
	document.write ("<ul>");
	for (var i = 0; i < TotalT; i++)
		document.write ("<li><a href='#TT" + i + "'>" + Tourneys [i].Name + "</a> (" + Tourneys [i].Total + " inediti/originals)");
	document.write ("</ul>");
	document.write ("</table>");
	for (var i = 0; i < TotalT; i++)
		WriteTourney (i, Tourneys [i]);
}


var	Subject = "";
var	ComText   = "<textarea cols='50' rows='12' id='comment_text'></textarea>";
		ComText += "<br><center><span class='butt' onclick='SendComment ()'><img src='img2chess/immagini/mail.png' alt='mail' width='40px'></span>&nbsp;&nbsp;&nbsp;<span class='butt' onclick='CloseComment ()'><img src='img2chess/immagini/close.png' alt='close' width='40px'></span></center>";

function	WriteComment (n)
{
	Subject = "Commento a Eteroscacco n." + n + ' ' + Originals [n - 1].Author;
	did = document.getElementById ('comment_div');
	if (!did)
		return;
	did.innerHTML = Subject + "<br>" + ComText;
	did.style.left = 350;
	did.style.top = document.body.scrollTop + 50;	
}

function	ReadComment (n)
{
	var did = document.getElementById ('comments');
	
	did.innerHTML = Comments [n - 1];
	did = document.getElementById ('comment_show');
	if (!did)
		return;
	did.style.left = 350;
	did.style.top = document.body.scrollTop + 50;	
}


function	SendComment ()
{
	var did = document.getElementById ('comment_div');
	
	did.style.left = -1000;
	did.style.top = -1000;
	
	did = document.getElementById ('comment_text');
	if (!did)
		return;
	MailToCommand  = "mailto:marco.bo" + "navoglia@" + "eteroscacco.it?";
	MailToCommand += "subject=" + Subject;
	if (did.value)
		MailToCommand += "&body=" + did.value.split ("\n");
	location.replace (MailToCommand);
}

function	CloseComment ()
{
	var did = document.getElementById ('comment_div');
	did.style.left = -1000;
	did.style.top = -1000;
	did = document.getElementById ('comment_show');
	did.style.left = -1000;
	did.style.top = -1000;
}

function	LinkToProblems (n)
{
	var	i;
	
	for (i = 0; i < n.length; i++)
	{
		if (n [i] <= 55)
			document.write ("<a href='inediti_0607.html#ESP" + n [i] + "'>" + n [i] + "</a>&nbsp;");
		else
			document.write ("<a href='inediti_0809.html#ESP" + n [i] + "'>" + n [i] + "</a>&nbsp;");
	}
}