

	
function calcBanruptcy(document) {
	

	
	var BR2 = document.getElementById('BR').value;
	var BU2 = document.getElementById('BU').value;
	var dontCalc = false;

	var BR = removeLeadingZeros(BR2);
	var BU = removeLeadingZeros(BU2);

	if (BR <= 0 | !isInteger(BR2)) {
		if(!isBlank(BR2)) {
			brError();
		}
		dontCalc = true;
	} else {
		clearBRError();
	}
	
	if (BU <= 0 | !isInteger(BU2)) {
		if(!isBlank(BU2)) {
			buError();
		}
		dontCalc = true;
	} else {
		clearBUError();
		
	}
	if(dontCalc) {
		var brPercent = document.getElementById("brPercent");
		brPercent.firstChild.nodeValue = "Percent chance of Bankruptcy";
	} else {
		var brPercent = document.getElementById("brPercent");
   		brPercent.firstChild.nodeValue = "Percent chance of Bankruptcy is " + (BR + BU) + "%";
	}
}

function calcBankRoll(document) {

	var BU2 = document.getElementById('BU2').value;
	var games2 = document.getElementById('games').value;
	var min2 = document.getElementById('minBet').value;
	var max2 = document.getElementById('maxBet').value;
	var dontCalc = false;

	var BU = removeLeadingZeros(BU2);
	var games = removeLeadingZeros(games2);
	var minb = removeLeadingZeros(min2);
	var maxb = removeLeadingZeros(max2);

	if (minb <= 0 | !isInteger(min2)) {
		if(!isBlank(min2)) {
			minBetError();
		}
		dontCalc = true;
	} else {
		clearminBetError();
	}
	
	if (maxb <= 0 | !isInteger(max2)) {
		if(!isBlank(max2)) {
			maxBetError();
		}
		dontCalc = true;
	} else {
		clearmaxBetError();
		
	}
	
	if (BU <= 0 | !isInteger(BU2)) {
		if(!isBlank(BU2)) {
			startbrError();
		}
		dontCalc = true;
	} else {
		clearStartBRError();
	}
	
	if (games <= 0 | !isInteger(games2)) {
		if(!isBlank(games2)) {
			gamesError();
		}
		dontCalc = true;
	} else {
		clearGamesError();
		
	}
	if(dontCalc) {
		var endBR = document.getElementById("endBR");
		endBR.firstChild.nodeValue = "Expected Bankroll Over Number of Games Played";
	} else {
		var avgBet = (minb*0.001424162)+(minb*0.001393338)+(minb*0.002807794)+(minb*0.004829362)+(minb*0.009462747)+(minb*0.01620657) + (minb*0.029197052)+(minb*0.050883476)+(minb*0.091550641)+(minb*0.169830319)+(minb*0.262545791)+(minb*0.160227572);
		
		avgBet += (minb*1.25E-04)+(minb*1.23E-04)+(minb*2.43E-04)+(minb*4.16E-04)+(minb*8.37E-04)+(minb*0.001472063) + (minb*0.002654112)+(minb*0.004699789)+(minb*0.008504694)+(minb*0.015776135)+(minb*0.026764248)+(minb*0.016805631);
				
		if(2*BU < maxb) {
			avgBet += (2*BU)*0.087162335;
			avgBet += (2*BU)*0.009250367;
		} else {
			avgBet += maxb*0.087162335;
			avgBet += maxb*0.009250367;
		}
		if(3*BU < maxb) {
			avgBet += (3*BU)*0.049202252;
			avgBet += (3*BU)*0.005483862;
		} else {
			avgBet += maxb*0.049202252;
			avgBet += maxb*0.005483862;
		}
		if(4*BU < maxb) {
			avgBet += (4*BU)*0.02818943;
			avgBet += (4*BU)*0.003066916;
		} else {
			avgBet += maxb*0.02818943;
			avgBet += maxb*0.003066916;
		}
		if(5*BU < maxb) {
			avgBet += (5*BU)*0.015667524;
			avgBet += (5*BU)*0.001635589;
		} else {
			avgBet += maxb*0.015667524;
			avgBet += maxb*0.001635589;
		}
		if(6*BU < maxb) {
			avgBet += (6*BU)*0.00925249;
			avgBet += (6*BU)*9.31E-04;
		} else {
			avgBet += maxb*0.00925249;
			avgBet += maxb*9.31E-04;
		}
		if(7*BU < maxb) {
			avgBet += (7*BU)*0.00469669;
			avgBet += (7*BU)*4.51E-04;
		} else {
			avgBet += maxb*0.00469669;
			avgBet += maxb*4.51E-04;
		}
		if(8*BU < maxb) {
			avgBet += (8*BU)*0.0027334;
			avgBet += (8*BU)*2.51E-04;
		} else {
			avgBet += maxb*0.0027334;
			avgBet += maxb*2.51E-04;
		}
		if(9*BU < maxb) {
			avgBet += (9*BU)*0.001360128;
			avgBet += (9*BU)*1.19E-04;
		} else {
			avgBet += maxb*0.001360128;
			avgBet += maxb*1.19E-04;
		}
		if(10*BU < maxb) {
			avgBet += (10*BU)*0.001376926;
			avgBet += (10*BU)*1.10E-04;
		} else {
			avgBet += maxb*0.001376926;
			avgBet += maxb*1.10E-04;
		}

		var amountWon = (minb*0.001424162*-0.04755682)+(minb*0.001393338*-0.04208293)+(minb*0.002807794*-0.03808105)+(minb*0.004829362*-0.03385145)+(minb*0.009462747*-0.02936807)+(minb*0.01620657*-0.02480838) + (minb*0.029197052*-0.02052133)+(minb*0.050883476*-0.01595486)+(minb*0.091550641*-0.01140967)+(minb*0.169830319*-0.00627892)+(minb*0.262545791*-0.00226835)+(minb*0.160227572*0.001954269);
		
		amountWon += (minb*1.25E-04*-0.04755682)+(minb*1.23E-04*-0.04208293)+(minb*2.43E-04*-0.03808105)+(minb*4.16E-04*-0.03385145)+(minb*8.37E-04*-0.02936807)+(minb*0.001472063*-0.02480838) + (minb*0.002654112*-0.02052133)+(minb*0.004699789*-0.01595486)+(minb*0.008504694*-0.01140967)+(minb*0.015776135*-0.00627892)+(minb*0.026764248*-0.00226835)+(minb*0.016805631*0.001954269);
				
		if(2*BU < maxb) {
			amountWon += (2*BU)*0.087162335*0.009201914;
			amountWon += (2*BU)*0.009250367*0.009201914;
		} else {
			amountWon += maxb*0.087162335*0.009201914;
			amountWon += maxb*0.009250367*0.009201914;
		}
		if(3*BU < maxb) {
			amountWon += (3*BU)*0.049202252*0.015150031;
			amountWon += (3*BU)*0.005483862*0.015150031;
		} else {
			amountWon += maxb*0.049202252*0.015150031;
			amountWon += maxb*0.005483862*0.015150031;
		}
		if(4*BU < maxb) {
			amountWon += (4*BU)*0.02818943*0.018133059;
			amountWon += (4*BU)*0.003066916*0.018133059;
		} else {
			amountWon += maxb*0.02818943*0.018133059;
			amountWon += maxb*0.003066916*0.018133059;
		}
		if(5*BU < maxb) {
			amountWon += (5*BU)*0.015667524*0.022238049;
			amountWon += (5*BU)*0.001635589*0.022238049;
		} else {
			amountWon += maxb*0.015667524*0.022238049;
			amountWon += maxb*0.001635589*0.022238049;
		}
		if(6*BU < maxb) {
			amountWon += (6*BU)*0.00925249*0.025317066;
			amountWon += (6*BU)*9.31E-04*0.025317066;
		} else {
			amountWon += maxb*0.00925249*0.025317066;
			amountWon += maxb*9.31E-04*0.025317066;
		}
		if(7*BU < maxb) {
			amountWon += (7*BU)*0.00469669*0.028181455;
			amountWon += (7*BU)*4.51E-04*0.028181455;
		} else {
			amountWon += maxb*0.00469669*0.028181455;
			amountWon += maxb*4.51E-04*0.028181455;
		}
		if(8*BU < maxb) {
			amountWon += (8*BU)*0.0027334*0.031285191;
			amountWon += (8*BU)*2.51E-04*0.031285191;
		} else {
			amountWon += maxb*0.0027334*0.031285191;
			amountWon += maxb*2.51E-04*0.031285191;
		}
		if(9*BU < maxb) {
			amountWon += (9*BU)*0.001360128*0.034862452;
			amountWon += (9*BU)*1.19E-04*0.034862452;
		} else {
			amountWon += maxb*0.001360128*0.034862452;
			amountWon += maxb*1.19E-04*0.034862452;
		}
		if(10*BU < maxb) {
			amountWon += (10*BU)*0.001376926*0.04205683;
			amountWon += (10*BU)*1.10E-04*0.04205683;
		} else {
			amountWon += maxb*0.001376926*0.04205683;
			amountWon += maxb*1.10E-04*0.04205683;
		}


		 
		var singleStdDev = 0.001424162*(square(minb - avgBet)) + 0.001393338*(square(minb - avgBet)) + 0.002807794*(square(minb - avgBet)) + 0.004829362*(square(minb - avgBet)) + 0.009462747*(square(minb - avgBet)) + 0.01620657*(square(minb - avgBet)) + 0.029197052*(square(minb - avgBet)) + 0.050883476*(square(minb - avgBet)) + 0.091550641*(square(minb - avgBet)) + 0.169830319*(square(minb - avgBet)) + 0.262545791*(square(minb - avgBet)) + 0.160227572*(square(minb - avgBet));
		
		singleStdDev += 1.25E-04*square(minb - avgBet) + 1.23E-04*square(minb - avgBet) + 2.43E-04*square(minb - avgBet) + 4.16E-04*square(minb - avgBet) + 8.37E-04*square(minb - avgBet) + 0.001472063*square(minb - avgBet) + 0.002654112*square(minb - avgBet) + 0.004699789*square(minb - avgBet) + 0.008504694*square(minb - avgBet) + 0.015776135*square(minb - avgBet) + 0.026764248*square(minb - avgBet) + 0.016805631*square(minb - avgBet);
		
		if(2*BU < maxb) {
			singleStdDev += 0.087162335*(square((2*BU) - avgBet));
			singleStdDev += 0.009250367*square((2*BU) - avgBet);
		} else {
			singleStdDev += 0.087162335*square(maxb - avgBet);
			singleStdDev += 0.009250367*square(maxb - avgBet);
		}
		if(3*BU < maxb) {
			singleStdDev += 0.049202252*(square((3*BU) - avgBet));
			singleStdDev += 0.005483862*square((3*BU) - avgBet);
		} else {
			singleStdDev += 0.049202252*square(maxb - avgBet);
			singleStdDev += 0.005483862*square(maxb - avgBet);
		}
		if(4*BU < maxb) {
			singleStdDev += 0.02818943*(square((4*BU) - avgBet));
			singleStdDev += 0.003066916*square((4*BU) - avgBet);
		} else {
			singleStdDev += 0.02818943*square(maxb - avgBet);
			singleStdDev += 0.003066916*square(maxb - avgBet);
		}
		if(5*BU < maxb) {
			singleStdDev += 0.015667524*(square((5*BU) - avgBet));
			singleStdDev += 0.001635589*square((5*BU) - avgBet);
		} else {
			singleStdDev += 0.015667524*square(maxb - avgBet);
			singleStdDev += 0.001635589*square(maxb - avgBet);
		}
		if(6*BU < maxb) {
			singleStdDev += 0.00925249*(square((6*BU) - avgBet));
			singleStdDev += 9.31E-04*square((6*BU) - avgBet);
		} else {
			singleStdDev += 0.00925249*square(maxb - avgBet);
			singleStdDev += 9.31E-04*square(maxb - avgBet);
		}
		if(7*BU < maxb) {
			singleStdDev += 0.00469669*(square((7*BU) - avgBet));
			singleStdDev += 4.51E-04*square((7*BU) - avgBet);
		} else {
			singleStdDev += 0.00469669*square(maxb - avgBet);
			singleStdDev += 4.51E-04*square(maxb - avgBet);
		}
		if(8*BU < maxb) {
			singleStdDev += 0.0027334*(square((8*BU) - avgBet));
			singleStdDev += 2.51E-04*square((8*BU) - avgBet);
		} else {
			singleStdDev += 0.0027334*square(maxb - avgBet);
			singleStdDev += 2.51E-04*square(maxb - avgBet);
		}
		if(9*BU < maxb) {
			singleStdDev += 0.001360128*(square((9*BU) - avgBet));
			singleStdDev += 1.19E-04*square((9*BU) - avgBet);
		} else {
			singleStdDev += 0.001360128*square(maxb - avgBet);
			singleStdDev += 1.19E-04*square(maxb - avgBet);
		}
		if(10*BU < maxb) {
			singleStdDev += 0.001376926*(square((10*BU) - avgBet));
			singleStdDev += 1.10E-04*square((10*BU) - avgBet);
		} else {
			singleStdDev += 0.001376926*square(maxb - avgBet);
			singleStdDev += 1.10E-04*square(maxb - avgBet);
		}

		singleStdDev = Math.sqrt(singleStdDev);
		
		var winPercent = amountWon/avgBet;
		var actualGames = games*1.029187054;
		var stdDev = Math.sqrt(actualGames)*(singleStdDev);
		var endBR = document.getElementById("endBR");
   		endBR.firstChild.nodeValue = "Expected Bankroll Over " + games + " games is $" + addCommas((winPercent*avgBet*actualGames).toFixed(2)) + " +- $" + addCommas(stdDev.toFixed(2));
	} // + " +- " + stdDev
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function clearStartBRError() {
   var BUText = document.getElementById("BUText");
   BUText.firstChild.nodeValue= "";
}
function clearGamesError() {
   var gamesText = document.getElementById("gamesText");
   gamesText.firstChild.nodeValue= "";
}
function startbrError() {
   var BUText = document.getElementById("BUText");
   BUText.firstChild.nodeValue= "Must be an integer greater than 0.";
}
function gamesError() {
   var gamesText = document.getElementById("gamesText");
   gamesText.firstChild.nodeValue= "Must be an integer greater than 0.";
}
function minBetError() {
   var minText = document.getElementById("minText");
   minText.firstChild.nodeValue= "Must be an integer greater than 0.";
}
function maxBetError() {
   var maxText = document.getElementById("maxText");
   maxText.firstChild.nodeValue= "Must be an integer greater than 0.";
}
function clearminBetError() {
   var minText = document.getElementById("minText");
   minText.firstChild.nodeValue= "";
}
function clearmaxBetError() {
   var maxText = document.getElementById("maxText");
   maxText.firstChild.nodeValue= "";
}

function clearBRError() {
   var brText = document.getElementById("brText");
   brText.firstChild.nodeValue= "";
}
function clearBUError() {
   var buText = document.getElementById("buText");
   buText.firstChild.nodeValue= "";
}
function brError() {
   var brText = document.getElementById("brText");
   brText.firstChild.nodeValue= "Must be an integer greater than 0.";
}
function buError() {
   var buText = document.getElementById("buText");
   buText.firstChild.nodeValue= "Must be an integer greater than 0.";
}


function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
	}

function initializeDiv() {
		   var buText = document.getElementById("buText");
   buText.firstChild.nodeValue= "";
      var brText = document.getElementById("brText");
   brText.firstChild.nodeValue= "";
   
   		   var gamesText = document.getElementById("gamesText");
   gamesText.firstChild.nodeValue= "";
      var BUText = document.getElementById("BUText");
   BUText.firstChild.nodeValue= "";
   
      		   var minText = document.getElementById("minText");
   minText.firstChild.nodeValue= "";
      var maxText = document.getElementById("maxText");
   maxText.firstChild.nodeValue= "";
}


function isInteger(val){
	if (isBlank(val)){return false;}
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))){return false;}
		}
	return true;
	}


function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
	}


function removeLeadingZeros(str) {
    while (str.charAt(0)=="0" && str.length>1) // if has leading zero (but not just "0")
      str= str.substring(1, str.length); // remove any leading zeros (otherwise assumes octal)
    return(parseInt(str));
}

function square(number) {
   return number * number;
}









