//Constraints
var divLimit = 10;
var widthLimit = 500;
var heightLimit = 300;
var topLimit = 550;
var leftLimit = 700;
var colorLimit = 256;

//Table limits
var rowLimit = 30;
var colLimit = 30;
var cellLimit = 30;

var rand; //The random number generator.

//functions
//You can't use "start()" because start is a reserved word in internet explorer.
function startScript(){
	var body = document.getElementById("body");

	//get a random number generatorthe seed
	var seed = document.getElementById("seed").value;
	//if no seed is put in, generate a random one.
	if(!seed){seed = Math.random();}
	rand = new RandomNumberGenerator(seed);

	//print the random seed
	body.innerHTML+="<h2>Seed "+seed+"</h2>\n"; //testing

	//create a number of div's to create, within constraints.
	var limit = rand.nextMinMax(1,divLimit);
	body.innerHTML+="<h2>Div limit "+limit+"</h2>\n"; //testing

	//Loop to create those div's. Keep using the random seed!
	for(i=0; i<limit; i++){ body.innerHTML+=makeDiv(i); }
	body.innerHTML+="<hr />\n";
}

function makeDiv(index){
	var str = '<div id="div'+index+'" name="div'+index+'" style="position:absolute;';

	str+='top:'+rand.nextMinMax(0,topLimit)+'px;';
	str+='left:'+rand.nextMinMax(0,leftLimit)+'px;';
	str+='height:'+rand.nextMinMax(0,heightLimit)+'px;';
	str+='width:'+rand.nextMinMax(0,widthLimit)+'px;';

	str+=randColorRGB()+'">'+makeTable(index)+'</div>'; //Add in a table too!!!

	return str+"\n";
}

function makeTable(index){
	var str = '<table id="table'+index+'" name="table'+index+'">';

	var rows = rand.nextMinMax(0,rowLimit);
	var cols = rand.nextMinMax(0,colLimit);
	var height = rand.nextMinMax(0,cellLimit);
	var width = rand.nextMinMax(0,cellLimit);

	for(r=0; r<rows; r++){
		str+='<tr>';
		for(c=0; c<cols; c++){
			str+='<td style="height:'+height+';width:'+width+';'+randColorRGB()+'"></td>';
		}
		str+='</tr>';
	}

	return str+'</table>';
}

function randColorRGB(){
	var str='background:rgb('+rand.nextMinMax(0,colorLimit)+',';
	str+=rand.nextMinMax(0,colorLimit)+',';
	str+=rand.nextMinMax(0,colorLimit)+');';

	return str;
}
