// JavaScript Document

function doBoth(form) {
	test(form);
	go();	
}

function test(form) {       
   var d = form.textfield.value;
   var d2 = form.textfield2.value;
   var x = d.split(" ");
   var n = x.length;
   var b = 0
   var y = new Array();
   for (var a = 0; a < n; a++) {	   
   x[a]=x[a]*1;
   x[a]=Math.round(x[a]);
      if (x[a]>0 && x[a] != NaN) {
	   y[b] = x[a];	   
	   b++;
	  }
   }
   var m = y.length;
   var N = 0.0;
   
    x = y;
    n = m;
	b = d2*1;
	b = Math.round(b);
	
	
	document.write('<DIV ALIGN="CENTER" style="font-size:20px; font-weight: bold;">')
	document.write('Initial sequence = ', x, '<br />')	
	
	if (b >= 0) {		
	    document.write('Initial bankroll = ', b, '<br />')
	}
	else {
		document.write('Initial bankroll = <br />')
	}
	
	var s = 1;
	
	if (n == 0) {
		s = 0;
		document.write('<br />Initial sequence is inappropriate.<br />')
	}		
		
	if (b >= 0 && n != 0) {
		s = 1;
	}	
	else if (b >= 0 && n == 0) {
		n = 0
	}
	else {			
		n = 0;
		s = 0;
		document.write('<br />Initial bankroll is inappropriate.<br />')
	}	
	
	if (n == 1) {
		if (b < x[0]) {
			s = 0;			
			document.write('<br />Bankroll won&#39;t cover the bet.<br />')
		}
	}
	
	if (n > 1) {
			if (b < x[0] + x[n-1]) {
			s = 0;			
			document.write('<br />Bankroll won&#39;t cover the bet.<br />')
		}
	}	
	
	while (s > 0) {
		var t = 1;
		while (t > 0) {
			now = new Date();
            seed = now.getSeconds();
		    N = 37*Math.random(seed);
			if (N <= 18) {
				if (n == 1) {
					b = b + x[0];
					x = 0;
					t = 0;
					document.write('<br />Sequence = ', x,' <br />')
					document.write('Bankroll = ', b,' <br />')
				}
				else if (n == 2) {
					b = b + x[0] + x [1];
					x = 0;
					t = 0;
					document.write('<br />Sequence = ', x,' <br />')
					document.write('Bankroll = ', b,' <br />')
				}
				else {
					b = b + x[0] + x[n-1];
					var h1 = x.shift()
					var h2 = x.pop()
					document.write('<br />Sequence = ', x,' <br />')
					document.write('Bankroll = ', b,' <br />')
				}
			}
			else {
				if (n == 1) {
					b = b - x[0];
					if (b <= 0) {
						t = 0;
					}
					x[1] = x[0];
					document.write('<br />Sequence = ', x,' <br />')
					document.write('Bankroll = ', b,' <br />')
				}
				else {
					b = b - x[0] - x[n-1]
					if (b <= 0) {
						t = 0;
					}
					x[n] = x[0] + x[n-1];
					document.write('<br />Sequence = ', x,' <br />')
					document.write('Bankroll = ', b,' <br />')
				}
			}
			n = x.length;
			if (n == 1) {
				if (b < x[0]) {
					t = 0;
					document.write('<br />Bankroll won&#39;t cover the bet.<br />')
				}
			}
			else {
				if (b < x[0] + x[n-1]) {
					t = 0;
					document.write('<br />Bankroll won&#39;t cover the bet.<br />')
				}
			}			
		}
		s = 0;						
	}		  
  
  document.write('<DIV ALIGN="CENTER"><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="20%"><div align="center"></div></td><td width="60%"><div align="center"><p align="center">&nbsp;</p><script src="LaBouchere.js"></script><form id="form2" name="form2" method="post" action=""><div align="center"><span style="font-size: 20px;font-weight: bold; font-family: Times New Roman">Initial sequence&nbsp;</span><input type="text" name="textfield"  style="font-size: 20px;font-weight: bold; font-family: Times New Roman" value="',d,'"/></div><p align="center"><span style="font-size: 20px;font-weight: bold; font-family: Times New Roman">Initial bankroll&nbsp;&nbsp;</span><input type="text" name="textfield2" style="font-size: 20px;font-weight: bold; font-family: Times New Roman" value="',b,'"/></p><div align="center"><input name="Submit" type="button" style="font-size: 20px;font-weight: bold; font-family: Times New Roman" onclick="doBoth(this.form)" value="Submit"/></div></form><p align="center">&nbsp;</p></div></td><td width="20%"><div align="center"></div></td></tr></table></DIV>')    
  document.write('</DIV>')
  
}

function go(){
setTimeout("window.scrollBy(0,1000000)",0);
}
