
// ffen2diag: Written by Hans Bodlaender (c) 2001
// 

function drawsquare(pict,pictpath)
{
  outstring="<IMG SRC=\"" + pictpath+pict+".gif\" width=32 height=32>";
  document.write(outstring);
  return outstring;
}

function drawnewrow()
{
   document.write("<BR>"); 
   return("<BR>");
}

function ffen2diag2(ffenstr)
{
return ffen2diag(ffenstr,"../../gif/");
}

function ffen2diag1(ffenstr)
{
	return ffen2diag(ffenstr,"../gif/");
}


function ffen2diag(ffenstr,gifpath)
{
  outstring = "";
  strlength = ffenstr.length;

  // count number of rows
  nbrows = 1;
  for (strpos = 0; strpos < strlength; strpos++)  
  {
    curchar = ffenstr.charAt(strpos);
    if (curchar == "/")
	{
	  nbrows++;
	}
  }

  coloroffset = nbrows % 2;
  // parse options 
  if ((ffenstr.charAt(0) == "[") && (ffenstr.charAt(1) == "W") && (ffenstr.charAt(2) == "]" ))
  {
    coloroffset = 1- coloroffset;
	strpos = 3;
  }
  else
  {
    strpos = 0;
  }
  color =  coloroffset;
  // parse main string
  rows = 0;
  while (strpos < strlength)
  {
    curchar = ffenstr.charAt(strpos);
	check = 0; // did we match this char?

    if (("a" <= curchar) && (curchar <= "z") ) 
    {
       picture= curchar+"1";
	   outstring = outstring + drawsquare(picture+color,gifpath);
       color = 1-color;
       strpos++; check = 1;
    }
    if (("A" <= curchar) && (curchar <= "Z") )
    {
       picture= curchar.toLowerCase()+"0";
	   outstring = outstring + drawsquare(picture+color,gifpath);
       color = 1-color;
       strpos++; check = 1;
    }
    if (("0" <= curchar) && (curchar <= "9")) 
    {
       nbskips = parseInt(curchar);
       strpos++; check = 1;
       curchar = ffenstr.charAt(strpos);
       while (strpos < strlength && ("0" <= curchar) && (curchar <= "9"))
       {
         nbskips = 10*nbskips + parseInt(curchar);
	     strpos++;
	     curchar = ffenstr.charAt(strpos); //
       }
       for (i=1; i <= nbskips; i++)
       {
         outstring = outstring + drawsquare("x"+color,gifpath);
	     color = 1-color;
       }
       
    }
    if (curchar == "/") // elseif naar javascript controlsctructuur
    {
       outstring = outstring + drawnewrow();
       rows = rows+1;
       color = (rows + coloroffset) %2;
       strpos++; check = 1;
    }
    if (curchar == "-")
    {
       outstring = outstring + drawsquare("x",gifpath);
       color = 1-color;
       strpos++; check = 1;
    }
    if (curchar == ".")
    {
       outstring = outstring + drawsquare("x"+color,gifpath);
       color = 1-color;
       strpos++; check = 1;
    }
	if (curchar == "(")
	{
	   // find the string between (), and draw that one;
	   fpiecestr = "";
	   strpos++; check = 1;
	   curchar = ffenstr.charAt(strpos);
	   lastchar = "X"; // default
	   while ((curchar != ")") && (strpos < strlength))
	   {
		 fpiecestr = fpiecestr + curchar;
	     strpos = strpos + 1;
		 lastchar = curchar;
	     curchar = ffenstr.charAt(strpos);
	   }
	   if (lastchar == "0")
	   {
	     playercolorstr = "0";
		 fpiecestr = fpiecestr.substring(0, fpiecestr.length-1);
	   }
       else
		{ 
		   if (lastchar == "1")
	        {
	         playercolorstr = "1";
		     fpiecestr = fpiecestr.substring(0, fpiecestr.length-1);
	        }
           else
			{
			   hulpstr = fpiecestr.charAt(0);
			   if (("A" <= hulpstr) && (hulpstr <= "Z") )
	           {
	            playercolorstr = "0";
	           }
	           else
				{
				   if (("a" <= hulpstr) && (hulpstr <= "z") )
	               {
	                 playercolorstr = "1";
	               }
	               else
	               {
	                 playercolorstr = "";
		           }
			    }
		    }
		}
	   outstring = outstring + drawsquare(fpiecestr.toLowerCase() + playercolorstr + color, gifpath);
	   color = 1-color;
	   strpos = strpos + 1;
	}
    if (check == 0)
    { strpos++;
    }
  }
 document.write("<p>");
 return outstring;
}

function int2colname(col)
{
	if (col==1){return "a";}
	if (col==2){return "b";}
	if (col==3){return "c";}
	if (col==4){return "d";}
	if (col==5){return "e";}
	if (col==6){return "f";}
	if (col==7){return "g";}
	if (col==8){return "h";}
	if (col==9){return "i";}
	if (col==10){return "j";}
	if (col==11){return "k";}
	if (col==12){return "l";}
	if (col==13){return "m";}
	if (col==14){return "n";}
	if (col==15){return "o";}
	if (col==16){return "p";}
	if (col==17){return "q";}
	if (col==18){return "r";}
	if (col==19){return "s";}
	if (col==20){return "t";}
	if (col==21){return "u";}
	if (col==22){return "v";}
	if (col==23){return "w";}
	if (col==24){return "x";}
	if (col==25){return "y";}
	if (col==26){return "z";}
	if (col==27){return "capa";}
	if (col==28){return "capb";}
	if (col==29){return "capc";}
	if (col==30){return "capd";}
	if (col==31){return "cape";}
	if (col==32){return "capf";}
	if (col==33){return "capg";}
	if (col==34){return "caph";}
	if (col==35){return "capi";}
	if (col==36){return "capj";}
	if (col==37){return "capk";}
	if (col==38){return "capl";}
	if (col==39){return "capm";}
	if (col==40){return "capn";}
}

function ffen2diag2b(ffenstr)
{
return ffen2diagb(ffenstr,"../../gif/");
}

function ffen2diag1b(ffenstr)
{
	return ffen2diagb(ffenstr,"../gif/");
}


function ffen2diagb(ffenstr,gifpath)
{
  outstring = "";
  strlength = ffenstr.length;

  // count number of rows
  nbrows = 1;
  for (strpos = 0; strpos < strlength; strpos++)  
  {
    curchar = ffenstr.charAt(strpos);
    if (curchar == "/")
	{
	  nbrows++;
	}
  }

  // we try to compute the number of columns, while parsing the string;
  nbcolumns = 0; 
  incolumn = 0;

  coloroffset = nbrows % 2;
  // parse options 
  if ((ffenstr.charAt(0) == "[") && (ffenstr.charAt(1) == "W") && (ffenstr.charAt(2) == "]" ))
  {
    coloroffset = 1- coloroffset;
	strpos = 3;
  }
  else
  {
    strpos = 0;
  }
  color =  coloroffset;

  // write number of top row;
  currow = nbrows;
  outstring="<IMG SRC=\"" +gifpath + "border"+ currow+".gif\" width=32 height=32>";
  document.write(outstring);


  // parse main string
  rows = 0;
  while (strpos < strlength)
  {
    curchar = ffenstr.charAt(strpos);
	check = 0; // did we match this char?

    if (("a" <= curchar) && (curchar <= "z") ) 
    {
       picture= curchar+"1";
	   outstring = outstring + drawsquare(picture+color,gifpath);
	   incolumn++; 
	   if (incolumn > nbcolumns) 
	   {
		   nbcolumns = incolumn;
	   }
       color = 1-color;
       strpos++; check = 1;
    }
    if (("A" <= curchar) && (curchar <= "Z") )
    {
       picture= curchar.toLowerCase()+"0";
	   outstring = outstring + drawsquare(picture+color,gifpath);
	   incolumn++; 
	   if (incolumn > nbcolumns) 
	   {
		   nbcolumns = incolumn;
	   }
       color = 1-color;
       strpos++; check = 1;
    }
    if (("0" <= curchar) && (curchar <= "9")) 
    {
       nbskips = parseInt(curchar);
       strpos++; check = 1;
       curchar = ffenstr.charAt(strpos);
       while (strpos < strlength && ("0" <= curchar) && (curchar <= "9"))
       {
         nbskips = 10*nbskips + parseInt(curchar);
	     strpos++;
	     curchar = ffenstr.charAt(strpos); //
       }
       for (i=1; i <= nbskips; i++)
       {
         outstring = outstring + drawsquare("x"+color,gifpath);
		 incolumn++; 
	     if (incolumn > nbcolumns) 
	     {
		   nbcolumns = incolumn;
	     }
	     color = 1-color;
       }
       
    }
    if (curchar == "/")
    {
       outstring = outstring + drawnewrow();
	   incolumn = 0;
       rows = rows+1;
       color = (rows + coloroffset) %2;
       strpos++; check = 1;
	   currow--;
	   outstring="<IMG SRC=\"" +gifpath + "border"+ currow+".gif\" width=32 height=32>";
	   document.write(outstring);
    }
    if (curchar == "-")
    {
       outstring = outstring + drawsquare("x",gifpath);
	   incolumn++; 
	   if (incolumn > nbcolumns) 
	   {
		   nbcolumns = incolumn;
	   }
       color = 1-color;
       strpos++; check = 1;
    }
    if (curchar == ".")
    {
       outstring = outstring + drawsquare("x"+color,gifpath);
	   incolumn++; 
	   if (incolumn > nbcolumns) 
	   {
		   nbcolumns = incolumn;
	   }
       color = 1-color;
       strpos++; check = 1;
    }
	if (curchar == "(")
	{
	   // find the string between (), and draw that one;
	   fpiecestr = "";
	   strpos++; check = 1;
	   curchar = ffenstr.charAt(strpos);
	   lastchar = "X"; // default
	   while ((curchar != ")") && (strpos < strlength))
	   {
		 fpiecestr = fpiecestr + curchar;
	     strpos = strpos + 1;
		 lastchar = curchar;
	     curchar = ffenstr.charAt(strpos);
	   }
	   if (lastchar == "0")
	   {
	     playercolorstr = "0";
		 fpiecestr = fpiecestr.substring(0, fpiecestr.length-1);
	   }
       else
		{ 
		   if (lastchar == "1")
	        {
	         playercolorstr = "1";
		     fpiecestr = fpiecestr.substring(0, fpiecestr.length-1);
	        }
           else
			{
			   hulpstr = fpiecestr.charAt(0);
			   if (("A" <= hulpstr) && (hulpstr <= "Z") )
	           {
	            playercolorstr = "0";
	           }
	           else
				{
				   if (("a" <= hulpstr) && (hulpstr <= "z") )
	               {
	                 playercolorstr = "1";
	               }
	               else
	               {
	                 playercolorstr = "";
		           }
			    }
		    }
		}
	   outstring = outstring + drawsquare(fpiecestr.toLowerCase() + playercolorstr + color, gifpath);
	   incolumn++; 
	   if (incolumn > nbcolumns) 
	   {
		   nbcolumns = incolumn;
	   }
	   color = 1-color;
	   strpos = strpos + 1;
	}
    if (check == 0)
    { strpos++;
    }
  }

  // write the bottomrow;
  drawnewrow();
  drawsquare("xtransparent",gifpath);
  for (i=1;i<=nbcolumns;i++  )
  {
     outstring="<IMG SRC=\"" +gifpath + "border"+ int2colname(i)+".gif\" width=32 height=32>";
     document.write(outstring);
  }

 document.write("<p>");
 return outstring;
}
