function calculate3Stakes() {

	var ts, o1, ox, o2, s1, sx, s2, p1, px, p2, pc1, pcx, pc2;

	o1 = Number(document.forms[0].threeHome.value);
	ox = Number(document.forms[0].threeDraw.value);
	o2 = Number(document.forms[0].threeAway.value);

	if (o1 == 0 || ox == 0 || o2 == 0) {
		document.all.threeStake1.innerHTML = "";
		document.all.threeStakeX.innerHTML = "";
		document.all.threeStake2.innerHTML = "";

		document.all.threeProfit1.innerHTML = "";
		document.all.threeProfitX.innerHTML = "";
		document.all.threeProfit2.innerHTML = "";

		document.all.threeProfitP1.innerHTML = "";
		document.all.threeProfitPX.innerHTML = "";
		document.all.threeProfitP2.innerHTML = "";

		return;
	}

	ts = Number(document.forms[0].threeStake.value);

	biasIndex = Number(document.forms[0].threeBias.selectedIndex);
	biasValue = document.forms[0].threeBias.options[biasIndex].value;

	if (biasValue == "1X2") {
		p1 = (ts/100)*((100*o1*ox*o2)/((o1*ox)+(o1*o2)+(ox*o2))-100);
		s1 = (ts+p1)/o1;
		sx = (ts+p1)/ox;
		s2 = (ts+p1)/o2;
	}

	if (biasValue=="1") {
		sx = ts/ox;
		s2 = ts/o2;
		s1 = ts-sx-s2;
	}

	if (biasValue=="X") {
		s1 = ts/o1;
		s2 = ts/o2;
		sx = ts-s1-s2;
	}

	if (biasValue=="2") {
		s1 = ts/o1;
		sx = ts/ox;
		s2 = ts-s1-sx;
	}

	if (biasValue=="1X") {
		s2 = ts/o2;
		s1 = (ts-s2)/(1+(o1/ox));
		sx = (ts-s2)/(1+(ox/o1));
	}

	if (biasValue=="12") {
		sx = ts/ox;
		s1 = (ts-sx)/(1+(o1/o2));
		s2 = (ts-sx)/(1+(o2/o1));
	}

	if (biasValue=="X2") {
		s1 = ts/o1;
		sx = (ts-s1)/(1+(ox/o2));
		s2 = (ts-s1)/(1+(o2/ox));
	}

	p1 = (o1*s1)-ts;
	px = (ox*sx)-ts;
	p2 = (o2*s2)-ts;

	pc1 = p1*100/(s1+sx+s2);
	pcx = px*100/(s1+sx+s2);
	pc2 = p2*100/(s1+sx+s2);

	document.all.threeStake1.innerHTML = currency(s1);
	document.all.threeStakeX.innerHTML = currency(sx);
	document.all.threeStake2.innerHTML = currency(s2);

	document.all.threeProfit1.innerHTML = currency(p1);
	document.all.threeProfitX.innerHTML = currency(px);
	document.all.threeProfit2.innerHTML = currency(p2);

	document.all.threeProfitP1.innerHTML = currency(pc1);
	document.all.threeProfitPX.innerHTML = currency(pcx);
	document.all.threeProfitP2.innerHTML = currency(pc2);

	if (p1 < 0) {
		document.all.threeProfit1.style.color = "red";
	} else {
		document.all.threeProfit1.style.color = "navy";
	}

	if (px < 0) {
		document.all.threeProfitX.style.color = "red";
	} else {
		document.all.threeProfitX.style.color = "navy";
	}

	if (p2 < 0) {
		document.all.threeProfit2.style.color = "red";
	} else {
		document.all.threeProfit2.style.color = "navy";
	}

	if (pc1 < 0) {
		document.all.threeProfitP1.style.color = "red";
	} else {
		document.all.threeProfitP1.style.color = "navy";
	}

	if (pcx < 0) {
		document.all.threeProfitPX.style.color = "red";
	} else {
		document.all.threeProfitPX.style.color = "navy";
	}

	if (pc2 < 0) {
		document.all.threeProfitP2.style.color = "red";
	} else {
		document.all.threeProfitP2.style.color = "navy";
	}

}

function currency(anynum) {

   anynum=eval(anynum);
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum;
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));
   dNum=dStr-0;
   pStr=workStr.substr(workStr.indexOf("."));
   while (pStr.length<3){pStr+="0"}
   
   retval = dStr + pStr ;
   //-- Put "-" in front of number if negative
   if (anynum<0) {retval="-"+retval}

   return retval;
}

function roundNumber(anynum) {

   anynum=eval(anynum);
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum;
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));
   dNum=dStr-0;
   pStr=workStr.substr(workStr.indexOf("."));
   while (pStr.length<3){pStr+="0"}
   
   retval = dStr + pStr ;
   //-- Put "-" in front of number if negative
   if (anynum<0) {retval="-"+retval}

   sl=retval.length;
   roundRetval=retval.substr(0,sl-3);

   return roundRetval;
}

function toggleOutcomes() {

   if (threeOutcomes.style.visibility=='hidden') {
      threeOutcomes.style.visibility='visible';
      twoOutcomes.style.visibility='hidden';
   } else {
      threeOutcomes.style.visibility='hidden';
      twoOutcomes.style.visibility='visible';
   }
}

function calculate2Stakes() {

	var ts, o1, ox, o2, s1, sx, s2, p1, px, p2, pc1, pcx, pc2;

	o1 = Number(document.forms[0].twoHome.value);
	o2 = Number(document.forms[0].twoAway.value);

	if (o1 == 0 || o2 == 0) {
		document.all.twoStake1.innerHTML = "";
		document.all.twoStake2.innerHTML = "";

		document.all.twoProfit1.innerHTML = "";
		document.all.twoProfit2.innerHTML = "";

		document.all.twoProfitP1.innerHTML = "";
		document.all.twoProfitP2.innerHTML = "";

		return;
	}

	ts = Number(document.forms[0].twoStake.value);

	biasIndex = Number(document.forms[0].twoBias.selectedIndex);
	biasValue = document.forms[0].twoBias.options[biasIndex].value;

	if (biasValue=="1") {
		s2 = ts/o2;
		s1 = ts-s2;
	}

	if (biasValue=="2") {
		s1 = ts/o1;
		s2 = ts-s1;
	}

	if (biasValue=="12") {
		s1 = ts/(1+(o1/o2));
		s2 = ts/(1+(o2/o1));
	}

	p1 = (o1*s1)-ts;
	p2 = (o2*s2)-ts;

	pc1 = p1*100/(s1+s2);
	pc2 = p2*100/(s1+s2);

	document.all.twoStake1.innerHTML = currency(s1);
	document.all.twoStake2.innerHTML = currency(s2);

	document.all.twoProfit1.innerHTML = currency(p1);
	document.all.twoProfit2.innerHTML = currency(p2);

	document.all.twoProfitP1.innerHTML = currency(pc1);
	document.all.twoProfitP2.innerHTML = currency(pc2);

	if (p1 < 0) {
		document.all.twoProfit1.style.color = "red";
	} else {
		document.all.twoProfit1.style.color = "navy";
	}

	if (p2 < 0) {
		document.all.twoProfit2.style.color = "red";
	} else {
		document.all.twoProfit2.style.color = "navy";
	}

	if (pc1 < 0) {
		document.all.twoProfitP1.style.color = "red";
	} else {
		document.all.twoProfitP1.style.color = "navy";
	}

	if (pc2 < 0) {
		document.all.twoProfitP2.style.color = "red";
	} else {
		document.all.twoProfitP2.style.color = "navy";
	}

}

function blur3Fractional() {

	var hd, dd, ad, hm, dm, am;

// fractional to decimal
	hd = eval(document.forms[0].threeHomeFractional.value) + 1;
	if (hd>=1) document.forms[0].threeHome.value = currency(hd);
	dd = eval(document.forms[0].threeDrawFractional.value) + 1;
	if (dd>=1) document.forms[0].threeDraw.value = currency(dd);
	ad = eval(document.forms[0].threeAwayFractional.value) + 1;
	if (ad>=1) document.forms[0].threeAway.value = currency(ad);

// decimal to moneyline
	if (hd>=2) hm = 100 * (hd - 1);
	if (hd>=1 && hd<2) hm = 100/(1 - hd);
	if (hm>=100 || hm<=100) document.forms[0].threeHomeMoneyLine.value = roundNumber(hm);
	if (dd>=2) dm = 100 * (dd - 1);
	if (dd>=1 && dd<2) dm = 100/(1 - dd);
	if (dm>=100 || dm<=100) document.forms[0].threeDrawMoneyLine.value = roundNumber(dm);
	if (ad>=2) am = 100 * (ad - 1);
	if (ad>=1 && ad<2) am = 100/(1 - ad);
	if (am>=100 || am<=100) document.forms[0].threeAwayMoneyLine.value = roundNumber(am);

	if (hd>=1 && dd>=1 && ad>=1) calculate3Stakes();
}

function blur3Decimal() {

	var hd, dd, ad, hm, dm, am;
	hd = document.forms[0].threeHome.value;
	dd = document.forms[0].threeDraw.value;
	ad = document.forms[0].threeAway.value;
	document.forms[0].threeHome.value = currency(hd);
	document.forms[0].threeDraw.value = currency(dd);
	document.forms[0].threeAway.value = currency(ad);

// decimal to moneyline
	if (hd>=2) hm = 100 * (hd - 1);
	if (hd>=1 && hd<2) hm = 100/(1 - hd);
	if (hm>=100 || hm<=100) document.forms[0].threeHomeMoneyLine.value = roundNumber(hm);
	if (dd>=2) dm = 100 * (dd - 1);
	if (dd>=1 && dd<2) dm = 100/(1 - dd);
	if (dm>=100 || dm<=100) document.forms[0].threeDrawMoneyLine.value = roundNumber(dm);
	if (ad>=2) am = 100 * (ad - 1);
	if (ad>=1 && ad<2) am = 100/(1 - ad);
	if (am>=100 || am<=100) document.forms[0].threeAwayMoneyLine.value = roundNumber(am);

// decimal to fractional
//	document.forms[0].threeHomeFractional.value = decimal2Fractional(hd, document.forms[0].threeHomeFractional.value);
//	document.forms[0].threeDrawFractional.value = decimal2Fractional(dd, document.forms[0].threeDrawFractional.value);
//	document.forms[0].threeAwayFractional.value = decimal2Fractional(ad, document.forms[0].threeAwayFractional.value);

	if (hd>=1 && dd>=1 && ad>=1) calculate3Stakes();
}

function blur3MoneyLine() {

	var hd, dd, ad, hm, dm, am;
	hm = document.forms[0].threeHomeMoneyLine.value;
	dm = document.forms[0].threeDrawMoneyLine.value;
	am = document.forms[0].threeAwayMoneyLine.value;

// moneyline to decimal
	if (hm>=100) hd = (hm/100) + 1;
	if (hm<-100) hd = 1 - (100/hm);
	if (hd>=1) document.forms[0].threeHome.value = currency(hd);
	if (dm>=100) dd = (dm/100) + 1;
	if (dm<-100) dd = 1 - (100/dm);
	if (dd>=1) document.forms[0].threeDraw.value = currency(dd);
	if (am>=100) ad = (am/100) + 1;
	if (am<-100) ad = 1 - (100/am);
	if (ad>=1) document.forms[0].threeAway.value = currency(ad);

// decimal to fractional
//	document.forms[0].threeHomeFractional.value = decimal2Fractional(hd, document.forms[0].threeHomeFractional.value);
//	document.forms[0].threeDrawFractional.value = decimal2Fractional(dd, document.forms[0].threeDrawFractional.value);
//	document.forms[0].threeAwayFractional.value = decimal2Fractional(ad, document.forms[0].threeAwayFractional.value);

	if (hd>=1 && dd>=1 && ad>=1) calculate3Stakes();
}

function decimal2Fractional(dec, oldVal) {
	decval = currency(dec);
	switch(decval) {
	
		case "1.01" :
			retval="1/100";
			break;
		case "1.02" :
			retval="1/50";
			break;
		case "1.03" :
			retval="3/100";
			break;
		case "1.04" :
			retval="1/25";
			break;
		case "1.05" :
			retval="1/20";
			break;
		case "1.06" :
			retval="1/16";
			break;
		case "1.07" :
			retval="1/14";
			break;
		case "1.08" :
			retval="1/12";
			break;
		case "1.09" :
			retval="1/11";
			break;
		case "1.10" :
			retval="1/10";
			break;
		case "1.11" :
			retval="1/9";
			break;
		case "1.13" :
			retval="1/8";
			break;
		case "1.14" :
			retval="1/7";
			break;
		case "1.15" :
			retval="2/13";
			break;
		case "1.17" :
			retval="1/6";
			break;
		case "1.18" :
			retval="2/11";
			break;
		case "1.20" :
			retval="1/5";
			break;
		case "1.22" :
			retval="2/9";
			break;
		case "1.25" :
			retval="1/4";
			break;
		case "1.30" :
			retval="3/10";
			break;
		case "1.33" :
			retval="1/3";
			break;
		case "1.35" :
			retval="7/20";
			break;
		case "1.40" :
			retval="2/5";
			break;
		case "1.44" :
			retval="4/9";
			break;
		case "1.45" :
			retval="9/20";
			break;
		case "1.50" :
			retval="1/2";
			break;
		case "1.53" :
			retval="8/15";
			break;
		case "1.55" :
			retval="11/20";
			break;
		case "1.57" :
			retval="4/7";
			break;
		case "1.60" :
			retval="6/10";
			break;
		case "1.62" :
			retval="8/13";
			break;
		case "1.65" :
			retval="13/20";
			break;
		case "1.67" :
			retval="2/3";
			break;
		case "1.70" :
			retval="7/10";
			break;
		case "1.73" :
			retval="8/11";
			break;
		case "1.75" :
			retval="3/4";
			break;
		case "1.80" :
			retval="4/5";
			break;
		case "1.83" :
			retval="5/6";
			break;
		case "1.85" :
			retval="17/20";
			break;
		case "1.90" :
			retval="9/10";
			break;
		case "1.91" :
			retval="10/11";
			break;
		case "1.95" :
			retval="19/20";
			break;
		case "2.00" :
			retval="1/1";
			break;
		case "2.10" :
			retval="11/10";
			break;
		case "2.20" :
			retval="6/5";
			break;
		case "2.25" :
			retval="5/4";
			break;
		case "2.37" :
			retval="11/8";
			break;
		case "2.50" :
			retval="3/2";
			break;
		case "2.62" :
			retval="13/8";
			break;
		case "2.75" :
			retval="7/4";
			break;
		case "2.87" :
			retval="15/8";
			break;
		case "3.00" :
			retval="2/1";
			break;
		case "3.20" :
			retval="11/5";
			break;
		case "3.25" :
			retval="9/4";
			break;
		case "3.40" :
			retval="12/5";
			break;
		case "3.50" :
			retval="5/2";
			break;
		case "3.60" :
			retval="13/5";
			break;
		case "3.75" :
			retval="11/4";
			break;
		case "3.80" :
			retval="14/5";
			break;
		case "4.00" :
			retval="3/1";
			break;
		case "4.33" :
			retval="10/3";
			break;
		case "4.50" :
			retval="7/2";
			break;
		case "5.00" :
			retval="4/1";
			break;
		case "5.50" :
			retval="9/2";
			break;
		case "6.00" :
			retval="5/1";
			break;
		case "6.50" :
			retval="11/2";
			break;
		case "7.00" :
			retval="6/1";
			break;
		case "7.50" :
			retval="13/2";
			break;
		case "8.00" :
			retval="7/1";
			break;
		case "8.50" :
			retval="15/2";
			break;
		case "9.00" :
			retval="8/1";
			break;
		case "9.50" :
			retval="17/2";
			break;
		case "10.00" :
			retval="9/1";
			break;
		case "11.00" :
			retval="10/1";
			break;
		case "12.00" :
			retval="11/1";
			break;
		case "13.00" :
			retval="12/1";
			break;
		case "15.00" :
			retval="14/1";
			break;
		case "17.00" :
			retval="16/1";
			break;
		case "21.00" :
			retval="20/1";
			break;
		case "26.00" :
			retval="25/1";
			break;
		case "34.00" :
			retval="33/1";
			break;
		case "51.00" :
			retval="50/1";
			break;
		case "67.00" :
			retval="66/1";
			break;
		case "101.00" :
			retval="100/1";
			break;
		case "151.00" :
			retval="150/1";
			break;
		case "201.00" :
			retval="200/1";
			break;
		case "501.00" :
			retval="500/1";
			break;
		case "1001.00" :
			retval="1000/1";
			break;			
		default :
			retval="";
	}
	return retval;
}

function blur2Fractional() {

	var hd, dd, ad, hm, dm, am;

// fractional to decimal
	hd = eval(document.forms[0].twoHomeFractional.value) + 1;
	if (hd>=1) document.forms[0].twoHome.value = currency(hd);
	ad = eval(document.forms[0].twoAwayFractional.value) + 1;
	if (ad>=1) document.forms[0].twoAway.value = currency(ad);

// decimal to moneyline
	if (hd>=2) hm = 100 * (hd - 1);
	if (hd>=1 && hd<2) hm = 100/(1 - hd);
	if (hm>=100 || hm<=100) document.forms[0].twoHomeMoneyLine.value = roundNumber(hm);
	if (ad>=2) am = 100 * (ad - 1);
	if (ad>=1 && ad<2) am = 100/(1 - ad);
	if (am>=100 || am<=100) document.forms[0].twoAwayMoneyLine.value = roundNumber(am);

	if (hd>=1 && ad>=1) calculate2Stakes();
}

function blur2Decimal() {

	var hd, dd, ad, hm, dm, am;
	hd = document.forms[0].twoHome.value;
	ad = document.forms[0].twoAway.value;
	document.forms[0].twoHome.value = currency(hd);
	document.forms[0].twoAway.value = currency(ad);

// decimal to moneyline
	if (hd>=2) hm = 100 * (hd - 1);
	if (hd>=1 && hd<2) hm = 100/(1 - hd);
	if (hm>=100 || hm<=100) document.forms[0].twoHomeMoneyLine.value = roundNumber(hm);
	if (ad>=2) am = 100 * (ad - 1);
	if (ad>=1 && ad<2) am = 100/(1 - ad);
	if (am>=100 || am<=100) document.forms[0].twoAwayMoneyLine.value = roundNumber(am);

// decimal to fractional
//	document.forms[0].twoHomeFractional.value = decimal2Fractional(hd, document.forms[0].twoHomeFractional.value);
//	document.forms[0].twoAwayFractional.value = decimal2Fractional(ad, document.forms[0].twoAwayFractional.value);

	if (hd>=1 && ad>=1) calculate2Stakes();
}

function blur2MoneyLine() {

	var hd, ad, hm, am;
	hm = document.forms[0].twoHomeMoneyLine.value;
	am = document.forms[0].twoAwayMoneyLine.value;

// moneyline to decimal
	if (hm>=100) hd = (hm/100) + 1;
	if (hm<-100) hd = 1 - (100/hm);
	if (hd>=1) document.forms[0].twoHome.value = currency(hd);
	if (am>=100) ad = (am/100) + 1;
	if (am<-100) ad = 1 - (100/am);
	if (ad>=1) document.forms[0].twoAway.value = currency(ad);

// decimal to fractional
//	document.forms[0].twoHomeFractional.value = decimal2Fractional(hd, document.forms[0].twoHomeFractional.value);
//	document.forms[0].twoAwayFractional.value = decimal2Fractional(ad, document.forms[0].twoAwayFractional.value);

	if (hd>=1 && ad>=1) calculate2Stakes();
}
