// board2diag: Written by Joao Neto based on code by Hans Bodlaender (c) 2001, 2002
// 
// Non-commercial use is permitted: `no fee - no guarantee' 
// For commercial use, ask Hans at hans@chessvariants.com and Joao at jpn@di.fc.ul.pt

function drawsquare(pict,pictpath,row,col) {

  if (pict=="z")
    outstring="<IMG SRC=\"" + pictpath+pict+".gif\" width=24 height=133>";
  else
    outstring="<IMG SRC=\"" + pictpath+pict+".gif\" alt=\"cell " + String.fromCharCode(64+col).toLowerCase() + row + "\" width=24 height=133>";
  document.write(outstring);
}

function drawnewrow() {
   document.write("<BR>"); 
}

function back2diag(boardstr) {
	board2diagp(boardstr,"");
}

function backgv2diag(boardstr) {  //jpn note: to use in the WGA website
	board2diagp(boardstr,"back2diag/");
}

function board2diagp(boardstr,gifpath) {

	// parse boardstr
    strlength = boardstr.length;
    strpos = 0;
	// first, determine which sides are existent
	// this is done by having names u (upper), l (left), r(right), or d (down)
    curchar = boardstr.charAt(strpos);

    dborder=0;  lborder=0;  rborder=0;   uborder=0;
    strpos=0;

    while (("a" <= curchar) && (curchar <= "z"))
    {
	if (curchar == "d"){dborder=1;}
	if (curchar == "u"){uborder=1;}
	if (curchar == "l"){lborder=1;}
	if (curchar == "r"){rborder=1;}
	strpos ++;
	curchar = boardstr.charAt(strpos);
    }
    borderoption = parseInt(curchar);

	// skip a possible comma
    if (curchar == ",")
    {
	strpos ++;
	curchar = boardstr.charAt(strpos);
    }

	// now, an integer follows that gives the number of rows
    nbrows =0;
    while (strpos < strlength && ("0" <= curchar) && (curchar <= "9"))
    {
         nbrows = 10*nbrows + parseInt(curchar);
	 strpos++;
	 curchar = boardstr.charAt(strpos);
    }

	// skip the comma
    if (curchar == ",")
    {
	strpos ++;
	curchar = boardstr.charAt(strpos);
    }

	// now an integer follows that gives the number of columns
    nbcolumns =0;
    while (strpos < strlength && ("0" <= curchar) && (curchar <= "9"))
    {
         nbcolumns = 10*nbcolumns + parseInt(curchar);
	 strpos++;
	 curchar = boardstr.charAt(strpos);
    }

	// skip the comma
    if (curchar == ",")
    {
	strpos ++;
	curchar = boardstr.charAt(strpos);
    }

	// and now we get the part that describes the board description, hence:
	// make diagram

    row = 1; column = 1;
    while (strpos < strlength) {

	curchar = boardstr.charAt(strpos);
      check = 0; // check tells: did we match this char?

	// options: what are we reading in the string

  	  // first case: a lower case letter from the alphabet
        if (("a" <= curchar) && (curchar <= "z") ) {
          drawsquare(curchar,gifpath,nbrows-row+1,column);
 	    column++;
            strpos++; 
 	    check = 1;
        }
	
        // second case: upper case letter from alphabet
        if (("A" <= curchar) && (curchar <= "Z") ) {
          drawsquare("k" + curchar.toLowerCase(),gifpath,nbrows-row+1,column);
	  column ++;
          strpos++; 
          check = 1;
        }
	
        // third case: digit
        if (("0" <= curchar) && (curchar <= "9")) {
           nbskips = parseInt(curchar);
           strpos++; 
           check = 1;
           curchar = boardstr.charAt(strpos);

           while (strpos < strlength && ("0" <= curchar) && (curchar <= "9")) {
              nbskips = 10*nbskips + parseInt(curchar);
	        strpos++;
	        curchar = boardstr.charAt(strpos); //
           }

           for (i=1; i <= nbskips; i++) {
	      picture = "cell";

            if ((row==1)      && (uborder==1))           picture = picture + "u";
            if ((row==nbrows) && (dborder==1))           picture = picture + "d";
            if ((column==1)   && (lborder==1))	         picture = picture + "l";
            if ((column == nbcolumns) && (rborder == 1)) picture = picture + "r";

            drawsquare(picture,gifpath,nbrows-row+1,column);
            column++;
	      if (column > nbcolumns) {
		  column = 1;
		  row ++;
		  if (row<=nbrows) drawnewrow();  // if it's the last row, don't change line
	      }
          } //for(i)
       }

       // forth case: end of row marker: '/'
       if (curchar == "/") {     // sometimes we have already skipped them
	   if (column > 1) {
              drawnewrow();
              row = row+1; 
              column = 1;
              strpos++; 
              check = 1;
	   }
       }


	// if character hasn't been matched, we just skip it this avoids infinite loops
       if (check == 0) strpos++;
    
   }
   document.write("<br>");
}
