// bridge library

SPADE  ="<img src=\"s.gif\" alt=\"S\">";
HEART  ="<img src=\"h.gif\" alt=\"H\">";
DIAMOND="<img src=\"d.gif\" alt=\"D\">";
CLUB   ="<img src=\"c.gif\" alt=\"C\">";

TSPADE  ="<tr><td><img src=\"s.gif\" alt=\"S\">";
THEART  ="<tr><td><img src=\"h.gif\" alt=\"H\">";
TDIAMOND="<tr><td><img src=\"d.gif\" alt=\"D\">";
TCLUB   ="<tr><td><img src=\"c.gif\" alt=\"C\">";

EndSuit="</td></tr>";
SpecTab="<td valign=TOP><img src=\"x.gif\" width=100></td>";
NameTab="<td valign=TOP><img src=\"x.gif\" width=80></td>";
TABLE  ="<img src=\"t.gif\" alt=\"W   E\">";
position=["WEST","NORTH","EAST","SOUTH"];


function HelpMe(str){
 document.write(str);
}

function Players(str){
 var names = str.split(",");
 document.write("<table><tr>");
 for(i=0;i<4;i++ )document.write(NameTab);
 document.write("</tr><tr>");
 for(i=0;i<4;i++) document.write("<td>",names[i],"</td>");
 document.write("</tr><tr>");
 for(i=0;i<4;i++) document.write("<td>",position[i],"</td>");
 document.write("</tr></table>");
}

function WNES(){
 document.write("<table><tr>");
 for(i=0;i<4;i++ )document.write(NameTab);
 document.write("</tr><tr>");
 for(i=0;i<4;i++) document.write("<td>",position[i],"</td>");
 document.write("</tr></table>");
}

function Bidding(str){
var bidpointer=0;c=str.charAt(0);
 switch (c) {
  case "W": bidpointer=0;break;
  case "N": bidpointer=1;break;
  case "E": bidpointer=2;break;
  case "S": bidpointer=3;
  }

 document.write("<table><tr>");
 for(i=0;i<3;i++ )document.write(NameTab);
 document.write("</tr><tr>");
 for(i=0;i<bidpointer;i++ )document.write(NameTab);

 for(i=1;i<=str.length;i++){
 c=str.charAt(i);
 if (c=="," || c==":" || c==" " || c==";") continue;
 if(c=="P" || c=="p"){c="pass";}
 if(c=="X" || c=="x"){c="dbl"; }
 if(c=="R" || c=="r"){c="Redbl";}
 if(c=="A" || c=="a"){c="Allpass";
             document.write("<td>",c,"</td>");
             break}

 if((c>"0") && (c<"8")) {
   if (str.charAt(i+1)=="C" || str.charAt(i+1)=="c")
                {c=c+CLUB;i++;}
   if (str.charAt(i+1)=="D" || str.charAt(i+1)=="d")
                {c=c+DIAMOND;i++;};
   if (str.charAt(i+1)=="H" || str.charAt(i+1)=="h")
                {c=c+HEART;i++;};
   if (str.charAt(i+1)=="S" || str.charAt(i+1)=="s")
                {c=c+SPADE;i++;};
   if (str.charAt(i+1)=="N" || str.charAt(i+1)=="n")
                {c=c+"NT";i++;};
  }

outc :
 document.write("<td>",c,"</td>");

  bidpointer++;
    if (bidpointer==4){
      document.write("</tr><tr>");
      bidpointer=0;}
 }

 document.write("</tr></table>");
}

function Hand(str) {
var player=0, suit=1;
var nesw=new Array(new Array(0,"","","",""),
               new Array(0,"","","",""),
               new Array(0,"","","",""),
               new Array(0,"","","",""));

// read cards
  for (i=0; i<str.length; i++) { //>
    c=str.charAt(i);

     if (c==',') {
        suit++;
     } else
     if (c==':' || c==' ' || c=='0' || c==';' ) {
        // ignore
     } else {
        if (c=='X' || c=='1'|| c=='T')
            c='10';
        nesw[player][suit] += ' ' + c;
        nesw[player][0] ++;
     }
  }

// Create the table
  document.write("<table>");
// Tabelator
document.write(SpecTab);
// hand parse
  if (nesw[0][0]>0) {
    document.write("<td><table>");
    document.write(TSPADE  ,nesw[0][1],EndSuit);
    document.write(THEART  ,nesw[0][2],EndSuit);
    document.write(TDIAMOND,nesw[0][3],EndSuit);
    document.write(TCLUB   ,nesw[0][4],EndSuit);
    document.write("</table></td>");
  }
 document.write("</table>");
}


function Deal(str ) {
var nesw=new Array(new Array(0,"","","",""),
               new Array(0,"","","",""),
               new Array(0,"","","",""),
               new Array(0,"","","",""));

  suit=0;
// citanie a rozpis vstupneho retazca
  for (i=0; i<str.length; i++) {
     c=str.charAt(i);
     if (c=='N') {
        player=0;
        suit=1;
     } else
     if (c=='E') {
        player=1;
        suit=1;
     } else
     if (c=='S') {
        player=2;
        suit=1;
     } else
     if (c=='W') {
        player=3;
        suit=1;
     } else
     if (c==',') {
        suit++;
     } else
     if (c==':' || c==' ' || c=='0'|| c==';' ||c==",") {
        // ignore
     } else {
        if (c=='X' || c=='1'|| c=='T')
            c='10';
        nesw[player][suit] += ' ' + c;
        nesw[player][0] ++;
     }
  }

// Create the table
  document.write("<table>");

// North
  if (nesw[0][0]>0) {
    document.write("<tr>",SpecTab);
    document.write("<td><table>");
    document.write(TSPADE  ,nesw[0][1],EndSuit);
    document.write(THEART  ,nesw[0][2],EndSuit);
    document.write(TDIAMOND,nesw[0][3],EndSuit);
    document.write(TCLUB   ,nesw[0][4],EndSuit);
    document.write("</table></td><tr>");
  }

// West
  if (nesw[3][0]>0) {
    document.write("<tr><td><table>");
    document.write(TSPADE  ,nesw[3][1],EndSuit);
    document.write(THEART  ,nesw[3][2],EndSuit);
    document.write(TDIAMOND,nesw[3][3],EndSuit);
    document.write(TCLUB   ,nesw[3][4],EndSuit);
    document.write("</table></td>");
  }
  else document.write("<tr>",SpecTab);

// table in middle
  document.write("<td>",TABLE," </td>");

// East
  if (nesw[1][0]>0) {
    document.write("<td><table>");
    document.write(TSPADE  ,nesw[1][1],EndSuit);
    document.write(THEART  ,nesw[1][2],EndSuit);
    document.write(TDIAMOND,nesw[1][3],EndSuit);
    document.write(TCLUB   ,nesw[1][4],EndSuit);
    document.write("</table></td></tr>");
    }
   else document.write("</tr>");

// south
  if (nesw[2][0]>0) {
    document.write(SpecTab);
    document.write("<td><table>");
    document.write(TSPADE  ,nesw[2][1],EndSuit);
    document.write(THEART  ,nesw[2][2],EndSuit);
    document.write(TDIAMOND,nesw[2][3],EndSuit);
    document.write(TCLUB   ,nesw[2][4],EndSuit);
    document.write("</table></td>");
  }

//
   document.write("</table>");
}

function HandLine(str) {
var player=0, suit=1;
var nesw=new Array(new Array(0,"","","",""),
               new Array(0,"","","",""),
               new Array(0,"","","",""),
               new Array(0,"","","",""));

// read cards
  for (i=0; i<str.length; i++) { //>
    c=str.charAt(i);

     if (c==',') {
        suit++;
     } else
     if (c==':' || c==' ' || c=='0' || c==';' ) {
        // ignore
     } else {
        if (c=='X' || c=='1'|| c=='T')
            c='10';
        nesw[player][suit] += ' ' + c;
        nesw[player][0] ++;
     }
  }

// Create the table
//  document.write("<table>");
// Tabelator
//document.write(SpecTab);
// hand parse
  if (nesw[0][0]>0) {
//    document.write("<td><table>");
    document.write(SPADE  ,nesw[0][1]," ");
    document.write(HEART  ,nesw[0][2]," ");
    document.write(DIAMOND,nesw[0][3]," ");
    document.write(CLUB   ,nesw[0][4]);
//    document.write("</table></td>");
  }
// document.write("</table>");
}


