
//
// smsgra.js
// gestione gioco interattivo 'SMS gratis'
//

// **********************************************
//                   DATI LOCALI
// **********************************************
MAX_CASELLE   = 20; // numero massimo caselle per titolo o regista o interprete
NUM_CIFRE     = 10; // numero cifre
NUM_LETTERE   = 26; // numero lettere
NUM_CELLE_TAS = NUM_CIFRE+NUM_LETTERE;
                    // numero caselle cliccabili su tastiera

NOMI_CAMPI=['Titolo film:','Diretto da:','Interpretato da:'];

SchemaGiocato = new Array(3*MAX_CASELLE),
tasti_cifre   = '....ABC.DEF.GHI.JKL.MNO.PQRSTUV.WXYZ';
                    // corrispondenza caratteri con tasti

var cifre = new Array(2);  // cifre cliccate

var cliccifre=0, // numero di cifre cliccate
    cliccarat=0; // numero di caratteri cliccati

Dim=25;

// **********************************************
// FUNZIONI COMUNI, ADATTATE AL SINGOLO GIOCO
// **********************************************
function reset_schema()
// imposta strutture dati per (ri)partire col gioco
{
// CODICE COMUNE
  if (Risolto || SoluzioneVista || VediSoluzione || VediInfo)
    return;

// CODICE DIPENDENTE DAL SINGOLO GIOCO
  for (i=0;i<3;i++)
  // per ogni riga (titolo, regista, interprete)
    for (j=0;j<MAX_CASELLE;j++)
    // per ogni casella
      SchemaGiocato[i*MAX_CASELLE+j]=' ';
      // inizializza a spazio ogni casella schema giocato da utente

  for (i=0;i<3;i++)
  // per ogni riga (titolo, regista, interprete)
  {
    lung=Esposto[i].length;
    // acquisisce lunghezza stringa da convertire in cifre

    for (j=0;j<lung;j++)
    // per ogni carattere
    {
      c=Esposto[i].charAt(j).toUpperCase();
      // acquisisce carattere
      SchemaGiocato[i*MAX_CASELLE+j]=
        (c!=' '?Math.floor((tasti_cifre.indexOf(c))/4)+1:' ');
        // inserisce casella cifra
    }
  }

// CODICE COMUNE
  vedi_schema();
}

function nuovo_schema()
// visualizza cifre per schema scelto
{
// CODICE DIPENDENTE DAL SINGOLO GIOCO
  Esposto=Esposto.split(','); // adatta Esposto
}

function vedi_schema()
// visualizza schema
{
  var c, i, j, lung, sorg;

  ss='<table align="center">';
  // tabella contenente schema

  for (i=0;i<3;i++)  // per ogni campo (film, regista, attore/attrice)
  {
    ss+="<tr>";
    // inizio nome campo
    ss+='<td colspan="'+MAX_CASELLE+'">'+NOMI_CAMPI[i]+'</td>';
    // nome campo
    ss+='</tr>';
    // fine nome campo
    ss+='<tr>';
    // inizio contenuto campo
    for (j=0;j<MAX_CASELLE;j++)
      ss+='<td><div class="csl" onClick="cliccata('+(i*MAX_CASELLE+j)+
          '); return false;" id="'+ (eittag(i*MAX_CASELLE+j)) +
          '"></div></td>';
    ss+='</tr>';
    // fine contenuto campo
  }

  ss+='</table>';
  // chiusura tabella

  document.getElementById('eitsk').innerHTML=ss;
  // inserisce codice html intero schema

  for (i=0;i<3;i++)
    for (j=0;j<MAX_CASELLE;j++)
      document.getElementById(eittag(i*MAX_CASELLE+j)).innerHTML='';
  // cancella caselle

  for (i=0;i<3;i++)
  // per ogni riga (titolo, regista, interprete)
  {
    lung=Esposto[i].length;
    // acquisisce lunghezza riga attuale

    for (j=0;j<lung;j++)
    // per ogni carattere riga
    {
      c=SchemaGiocato[i*MAX_CASELLE+j];
      document.getElementById(eittag(i*MAX_CASELLE+j)).innerHTML=
        (alfabeto.indexOf(c)>-1?c.toUpperCase():c);
    }
  }
  // inserisce contenuto schema giocato da utente
  zooma_font(0);
}

function cliccata(c)
// reagisce a clic su casella
{
  if (Risolto || VediSoluzione)
    return;

  car=document.getElementById(eittag(c)).innerHTML;
  // acquisisce contenuto casella

  if  ((j=tasti_cifre.indexOf(car))==-1)
  // se (non e' un carattere)
  {
    n=car;         // acquisisce cifra
    car=tasti_cifre.charAt((n-1)*4); // primo carattere tasto cifra
  }
  else
    if (j%4==3) // se ultimo carattere gruppo
      car=Math.floor(j/4)+1; // cifra
    else
    {
      car=tasti_cifre.charAt(j+1);
      if (car=='.')
        car=Math.floor(j/4)+1; // cifra
    }

  document.getElementById(eittag(c)).innerHTML=car;
  // inserisce contenuto in casella
  SchemaGiocato[c]=car;
  // acquisisce carattere in soluzione giocata da utente

  controlla();
  // controlla soluzione parziale costruita da utente
}

function inkeys(e)
// gestore pressione tasti
{
  return false;
}

function controlla()
// controlla soluzione parziale costruita da utente
{
  diverso=false;

  i=0;
  while (i<3 && !diverso)
  // per ogni riga (titolo, regista, interprete)
  {
    lung=Esposto[i].length;
    // acquisisce lunghezza stringa da convertire in cifre

    j=0;
    while (j<lung && !diverso)
    // per ogni carattere
    {
      c=SchemaGiocato[i*MAX_CASELLE+j];
      if (tasti_cifre.indexOf(c)>-1 || c==' ')
        if (c.toUpperCase()!=Esposto[i].charAt(j).toUpperCase())
          diverso=true;
        else
          j++;
      else
        diverso=true;
    }
    i++;
  }

  if (!diverso)
  {
    Risolto=true;
    stopTimer();
    // gioco risolto
    vediApplauso();
    // richiama animazione sfinge Kidi che applaude
    // confirm("Ottimo!");
  }
}

function soluzione()
// mostra soluzione schema scelto
{
  if (VediInfo)
    return;

  var c, i, j, lung, sorg;

  SoluzioneVista=true;
  VediSoluzione=!VediSoluzione;

  for (i=0;i<3;i++)
  // per ogni riga (titolo, regista, interprete)
  {
    lung=Esposto[i].length;
    // acquisisce lunghezza stringa da convertire in cifre

    for (j=0;j<lung;j++)
    // per ogni carattere
    {
      if (VediSoluzione)
        c=Esposto[i].charAt(j);
      else
        c=SchemaGiocato[i*MAX_CASELLE+j];
      // acquisisce carattere
      document.getElementById(eittag(i*MAX_CASELLE+j)).innerHTML=
        (alfabeto.indexOf(c)>-1?c.toUpperCase():c);
      // sceglie e inserisce carattere
    }
  }

  stopTimer();
}

// -------------------------------
// INIZIO CODICE LOCALE AGGIUNTIVO
// -------------------------------

document.onkeydown=inkeys;
// aggancia gestore pressione tasti alla pagina