﻿// File JScript

var points = new Array(
"p", "", "_0",
"d", "", "_0",
"p", "", "_0",
"d", "", "_0",
"p", "", "_0",
"d", "", "_0",
"p", "", "_0",
"d", "", "_0",
"p", "", "_0",
"d", "", "_0",
"p", "", "_0",
"d", "", "_0",
"d", "", "_0",
"n", "", "_0",
"d", "", "_0",
"n", "", "_0",
"d", "", "_0",
"n", "", "_0",
"d", "", "_0",
"n", "", "_0",
"d", "", "_0",
"n", "", "_0",
"d", "", "_0",
"n", "", "_0"
);

var bar = new Array (
	"_0",
	"_0"
	);

var off = new Array (
	"o_w_0.gif",
	"o_b_0.gif"
	);

var cube = new Array (
	"_0",
	"_blank",
	"_0"
	);
	
function notEmpty(s) {
s = s.replace(/ /g, "");
if (s == "")
	return false;
else
	return true;
}

function greaterOf(a,b) {
	if (a > b)
		return a;
	else
		return b;
}

function lesserOf(a,b) {
	if (a < b)
		return a;
	else
		return b;
}

function initArrays() {

for (i=1, j=0; j < 24; j++, i+=3) {
  points[i] = "";
  points[i+1] = "_0";
  cube[0] = "_0";
  cube[1] = "_blank";
  cube[2] = "_0";
  }
}


function setarrays() {

initArrays();

for (i=1, j=0; j < 24; j++, i+=3) {
	if (notEmpty(document.forms[0].p[j].value)) {
		points[i] = "_" + document.forms[0].p[j].value.substring(0,1);
		points[i+1] = "_" + document.forms[0].p[j].value.substring(1);
		}
	}

if (notEmpty(document.forms[0].o_white.value)) {
	off[0] = "o_w";
	if (document.forms[0].o_white.value.substring(0,1) < "0" || document.forms[0].o_white.value.substring(0,1) > "9")
		document.forms[0].o_white.value = document.forms[0].o_white.value.substring(document.forms[0].o_white.value.length);
	off[0] = off[0] + "_" + document.forms[0].o_white.value + ".gif";
}
if (notEmpty(document.forms[0].o_blue.value)) {
	off[1] = "o_b";
	if (document.forms[0].o_blue.value.substring(0,1) < "0" || document.forms[0].o_blue.value.substring(0,1) > "9")
		document.forms[0].o_blue.value = document.forms[0].o_blue.value.substring(1,document.forms[0].o_blue.value.length);
	off[1] = off[1] + "_" + document.forms[0].o_blue.value + ".gif";
	}
if (notEmpty(document.forms[0].b_blue.value)) {
	if (document.forms[0].b_blue.value.substring(0,1) < "0" || document.forms[0].b_blue.value.substring(0,1) > "9")
		document.forms[0].b_blue.value = document.forms[0].b_blue.value.substring(1,document.forms[0].b_blue.value.length);
	bar[0] = "_" + document.forms[0].b_blue.value;
	}
if (notEmpty(document.forms[0].b_white.value)) {
	if (document.forms[0].b_white.value.substring(0,1) < "0" || document.forms[0].b_white.value.substring(0,1) > "9")
		document.forms[0].b_white.value = document.forms[0].b_white.value.substring(1,document.forms[0].b_white.value.length);
	bar[1] = "_" + document.forms[0].b_white.value;
	}

document.forms[0].c_value.value = document.forms[0].c_value.value.replace(/ /g, "");
if (notEmpty(document.forms[0].c_value.value)) {
  if (document.forms[0].r_cube[0].checked) {
  	cube[1] = "_center";
  	}
  if (document.forms[0].r_cube[1].checked) {
    	cube[0] = "_" + document.forms[0].c_value.value;
  	}
  if (document.forms[0].r_cube[2].checked) {
  	cube[2] = "_" + (notEmpty(document.forms[0].c_value.value) ? document.forms[0].c_value.value : "0");
  	}
  }

bscore = document.forms[0].r_bscore.value;
wscore = document.forms[0].r_wscore.value;
len = document.forms[0].r_length.value;	
document.forms[0].r_value.value = document.forms[0].r_value.value.replace(/ /g, "");
d1 = document.forms[0].r_value.value.substring(0,1);
d2 = document.forms[0].r_value.value.substring(1,2);
dice = lesserOf(d1, d2) + greaterOf(d1, d2);
roll = ""
craw = false;
strCaption = ""

if (document.forms[0].r_roll[0].checked) {
	if (notEmpty(dice)) {
		roll = dice + "right"
		}
	onroll = "b";
	}
else if (document.forms[0].r_roll[1].checked) {
	if (notEmpty(dice)) {
		roll = dice + "left";
		}
	onroll = "w";
	}
else {
	onroll = "c";
	}

if (document.forms[0].r_forum[0].checked)
	imgdir = "Images/";
else if (document.forms[0].r_forum[1].checked)
	imgdir = "http://www.cnbackgammon.eu/Position/Images/";
else
  imgdir = "http://www.dbgf.dk/BBSImages/";

if (document.forms[0].r_cap[1].checked)
	strCaption = "Money Game";
else if (document.forms[0].r_cap[2].checked) {
	if (bscore == wscore)
		strCaption = "<br>Match ai " + len + ", pari a " + bscore;
	else if (bscore > wscore)
		strCaption = "<br>Match ai " + len + ", il Nero conduce " + bscore + " a " + wscore;
	else
		strCaption = "<br>Match ai " + len + ", il Bianco conduce " + wscore + " a " + bscore;

	if (len - greaterOf(bscore, wscore) == 1 && document.forms[0].c_craw.checked) {
		strCaption = strCaption + ", Crawford";
		craw = true;
		}
	}

if (!document.forms[0].r_cap[0].checked) {  
	if (document.forms[0].c_cube.checked && !craw) {
		if (onroll == "b")
			strCaption = strCaption + "<br>Nero al tiro. Cube Action?";
		else if (onroll == "w")
			strCaption = strCaption + "<br>Bianco al tiro. Cube Action?";
		else
			strCaption = strCaption + "<br>Cube Action?";
		}
	}
}

function pipCountWhite() {
	var total = 0;
	
for (var i = 1, j = 1; j < 13; i +=3, j++) {
	if (points[i] == "_w")
		total += (j * points[i+1].substring(1));
	}
	
for (j = 24; j > 12; i+=3, j--) {
	if (points[i] == "_w")
		total += (j * points[i+1].substring(1));
	}
total += 25 * document.forms[0].b_white.value;
return total;
}

function pipCountBlue() {
	var total = 0;
	
	for (var i = 1, j = 24; j > 12; i +=3, j--) {
		if (points[i] == "_b")
			total += (j * points[i+1].substring(1));
	}
	
	for (j = 1; j < 13; i+=3, j++) {
		if (points[i] == "_b")
			total += (j * points[i+1].substring(1));
	}
total += 25 * document.forms[0].b_blue.value;
return total;
}

function showboard(s) {
setarrays();
var w_pips = 0;
var b_pips = 0;
var ow = off[0];
var ob = off[1];
var bw = "b_dn" + bar[1] + ".gif";
var bb = "b_up" + bar[0] + ".gif";
var c_up = "c_up" + cube[0] + ".gif";
var r = roll;
var o = onroll;
var idir = imgdir;
var c_center = "c" + cube[1] + ".gif";
var c_dn = "c_dn" + cube[2] + ".gif";
var cap = strCaption;
var w = window.open();

var pt = new Array (
"", "", "", "", "", "", 
"", "", "", "", "", "",
"", "", "", "", "", "",
"", "", "", "", "", ""
);
	
for (var i=0, j=0; i<12; i++, j+=3)
	pt[i] = "p_u" + points[j] + points[j+1] + points[j+2] + ".gif";
	
for (; i<24; i++, j+=3)
	pt[i] = "p_d" + points[j] + points[j+1] + points[j+2] + ".gif";

w_pips = pipCountWhite();
b_pips = pipCountBlue();

if (s == "HTML")
	w.document.writeln('<XMP>');

w.document.writeln('<TABLE><Caption>', cap, '</caption><TR>');
w.document.writeln('<td valign=top > Pips: <b>', w_pips);

w.document.write('</b><TD rowspan=2 valign=top ><IMG');

if (o == "w")
	w.document.write(" SRC=", idir, "n_low.gif ><br><IMG");
else
	w.document.write(" SRC=", idir, "n_high.gif ><br><IMG");

w.document.write(" SRC=", idir, ow, " ><IMG");

for (var i=0; i<6; i++) {
	w.document.write(" SRC=", idir, pt[i], " ><IMG");
	}

w.document.write(" SRC=", idir, bb, " ><IMG");

for (; i<12; i++) {
	w.document.write(" SRC=", idir, pt[i], " ><IMG");
	}

w.document.write(" SRC=", idir, c_up, " ><br><IMG");
w.document.write(" SRC=", idir, "b_center", r, ".gif ><IMG");
w.document.write(" SRC=", idir, c_center, " ><br><IMG");
w.document.write(" SRC=", idir, ob, " ><IMG");

for (; i<18; i++) {
	w.document.write(" SRC=", idir, pt[i], " ><IMG");
	}

w.document.write(" SRC=", idir, bw, " ><IMG");

for (; i<24; i++) {
	w.document.write(" SRC=", idir, pt[i], " ><IMG");
	}

w.document.write(" SRC=", idir, c_dn, " ><br><IMG");

if (o == "w")
	w.document.write(" SRC=", idir, "n_high.gif >");
else
	w.document.write(" SRC=", idir, "n_low.gif >");
	
w.document.writeln('<tr><td valign=bottom >Pips: <b>', b_pips, '</b></td></tr>');
w.document.writeln("</table>");
if (s == "HTML")
	w.document.writeln('</XMP>');
w.document.close();
}

function clearBoard() {
  
  initArrays();
  
	for (var i = 0; i < 24; i++)
		document.forms[0].p[i].value = "";
	
	document.forms[0].o_white.value = "";
	document.forms[0].o_blue.value = "";
	document.forms[0].b_white.value = "";
	document.forms[0].b_blue.value = "";
		
	bar[0] = "_0";
	bar[1] = "_0";

	off[0] = "o_w_0.gif";
	off[1] = "o_b_0.gif";

	cube[0] = "_0";
	cube[1] = "_blank";
	cube[2] = "_0";
}

function startPos() {
	initArrays();
	document.forms[0].p[0].value = "b2";
	document.forms[0].p[5].value = "w5";
	document.forms[0].p[7].value = "w3";
	document.forms[0].p[11].value = "b5";
	document.forms[0].p[23].value = "w5";
	document.forms[0].p[19].value = "b3";
	document.forms[0].p[17].value = "b5";
	document.forms[0].p[12].value = "w2";
}
