// JavaScript Document
// this is a means of generating worksheets from random numbers
function look(){
a=document.add.xval.value;  //length of minuend
	if(a==""){a=1;}
b=document.add.yval.value;  //length of subtrahend
	if(b==""){b=1;}
c=document.add.num.value;  //number of problems
	if(c==""){c=5;}
// a and b will serve as exponents for powers of 10
// Graphics routines........
gop="<img src=\"generator/add.gif\">";
gcsp="<img src=\"generator/sp.gif\">";  //spacer below counter
show="";
copy="<br>"+"This Worksheet Was Generated by G2Worksheets.com";
namedate="Addition Practice Worksheet: Name____________  Date_____________";
namedate=namedate+"<br><br>";
check="";
	for(loop=0; loop<c; loop++){
//pick the numbers for the problem
	x=Math.random()*Math.pow(10,a)+1;
	x=Math.floor(x);
	z=Math.random()*Math.pow(10,b);
	z=Math.floor(z);
	
//get the answer
	diff=x+z;	

//  x-z=diff
	check=check+(loop+1)+")  "+x+" + "+z+" = "+diff+"\n";

// this converts the first number into an html string of graphics
	sourcex=""+x; // forces a string from a number
	lx=sourcex.length;
	sourcez=""+z;
	lz=sourcez.length;

// need to insert the table row/column tabs here:
	if(loop==0){
		show=show+"<tr><td width=\"25%\">";}
	if (loop!=0){
		show=show+"<td width=\"25%\">";}
	show=show +(loop+1)+"). ";

// load the top string
//this is the spot for the filler loop
		if (lz>lx){
			for(lop=0; lop<(lz-lx);lop++)
				{show=show +" ";}
			}

		for(poo=0; poo<lx; poo++){
			show=show+sourcex.charAt(poo);
		}  //end poo

if (loop+1<10){
show=show+"<br>"+gop+gcsp;
}
else if ((loop+1>9) && (loop+1<99)){
show=show+"<br>"+gop+gcsp+gcsp;
}
else{
show=show+"<br>"+gop+gcsp+gcsp+gcsp;
}   //add the problem number space/subtraction sign 

//  compare the number lengths
lc=lx-lz;

// add fillers
	if (lc!=0){
		for(filler=0;filler<lc;filler++){
			show=show+gcsp;}
	}
		for(poo=0;poo<lz;poo++){
		show=show+sourcez.charAt(poo);
		} //end of the 2nd poo
show=show+"<br>_________"+"<br><br>"+"_________<br>"+"</td>";
	if(loop>0&&(loop+1)/4==Math.floor((loop+1)/4)){
		show=show+"</tr>";
		if ((loop+1)<c){
			show=show+"<tr>";}
//  Might need to add a rpw termination in here somewhere
		}
		else{
			if ((loop+1)==c&&(loop+1)/4!=Math.floor((loop+1)/4)){
				show=show+"</tr>";}
			}

	} //end loop

document.add.worksp.value=check;

// new window = newwin
// Open the window

	var newwin=window.open("","f","width=640,height=480,scrollbars=yes,toolbar=yes");
	newwin.document.writeln("<!doctype html public '-//w3c//dtd html 3.2//en'><html><head>");
	newwin.document.writeln("<title>Addition Worksheet Generated by G2WORKSHEETS.COM</title></head><body><blockquote><table width=\"94%\">");
	newwin.document.writeln(namedate);
	newwin.document.writeln(show);
	newwin.document.writeln("</table>\n\n\n"+copy);
	newwin.document.writeln("</blockquote></body></html>");

	newwin.document.close();	
}
