//This file has functions used soley in tab0501algebra01.htm - the one with Rule/ Pattern tabls
var TheNewForm
var TheNewTable
var Current = new Array()

function WriteTable(TableNo)
{
var DocFormNo = 'document.TheOperations' + TableNo;
var TableLayer = 'Table' + TableNo;
var MaxValue = eval(DocFormNo + '.BiggestNumber.value');
//LATER Check for valid Maximum Number Input

var NumUnique=5  //The number of unique numbers required
var Count=0

var Op1 = eval(DocFormNo + '.FirstOperation.value');
var Number1 = eval(DocFormNo + '.FirstNumber.value');
var Op2 = eval(DocFormNo + '.SecondOperation.value');		
var Number2 = eval(DocFormNo + '.SecondNumber.value');
if (Op2 ==' ')
   {
	var Number2 = ' ';
   }

//Randomly Get Unique Numbers for Input
    for (i=0;Count<NumUnique;Count++)
    {
      var Found=false
      var rndValue = Math.round(Math.random()*MaxValue);
      var j=0
      for (j=0;j<Current.length;j++)
      {
        if (Current[j] == rndValue)
        {
          Found=true
          break
        }
      }
      if (Found)
      {
        Count--
      } else {
        Current[Count]=rndValue
      }
    }
//End of Get Unique Numbers


   //build and write table
   TheNewTable = '<table>';
   TheNewTable = TheNewTable + '<tr><td align="center" width="75" style="border-bottom: 1px solid #000000;  border-right: 1px solid #000000;">Input</td><td align="center" width="75" style="border-bottom: 1px solid #000000;">Output</td></tr>';
   for(i = 0; i < 5; i++){
       TheNewTable = TheNewTable + '<tr>';

   //write the input
       TheNewTable = TheNewTable + '<td align="center" style="border-right: 1px solid #000000;">' + Current[i] + '</td>';
   //calc and write output
	  var TheAnswer = eval('(' + Current[i] + Op1 + Number1 +')' + Op2 + Number2);
       TheNewTable = TheNewTable + '<td align="center"><span class="answer">' + TheAnswer + '</span></td>';
       TheNewTable = TheNewTable + '</tr>';
     }
    TheNewTable = TheNewTable + '</table>'
    document.getElementById(TableLayer).innerHTML =" " + TheNewTable + " ";
}





function WriteForm(FormNo,BigNo,FirstOp,FirstNo,SecondOp,SecondNo)
{
if (SecondNo==0) {SecondNo = '';}
FormLayer = 'Form' + FormNo;
TheNewForm = '<form action="" name="TheOperations' + FormNo + '" id="TheOperations' + FormNo + '"><span class="DontPrint2">'

      TheNewForm = TheNewForm + 'Max. Number:<input name="BiggestNumber" type="text" size="1" value="' + BigNo +'"/></span><br/>'
        TheNewForm = TheNewForm + 'Rule: <span class="Boxes"><select name="FirstOperation" size="1">'
          TheNewForm = TheNewForm + '<option value="+">+</option>'
          TheNewForm = TheNewForm + '<option value ="-">-</option>'
          TheNewForm = TheNewForm + '<option value="*">x</option>'
          TheNewForm = TheNewForm + '<option value="/">&divide;</option>'
        TheNewForm = TheNewForm + '</select>'
      TheNewForm = TheNewForm + '<input name="FirstNumber" type="text" size="1" value="' + FirstNo +'"/>'
      TheNewForm = TheNewForm + '<select name="SecondOperation" size="1">'
        TheNewForm = TheNewForm + '<option value=" "> </option>'
          TheNewForm = TheNewForm + '<option value="+">+</option>'
          TheNewForm = TheNewForm + '<option value ="-">-</option>'
          TheNewForm = TheNewForm + '<option value="*">x</option>'
          TheNewForm = TheNewForm + '<option value="/">&divide;</option>'
      TheNewForm = TheNewForm + '</select>'
      TheNewForm = TheNewForm + '<input name="SecondNumber" type="text" size="1" value="' + SecondNo +'" />'
TheNewForm = TheNewForm + '</span></form>'

document.getElementById(FormLayer).innerHTML =" " + TheNewForm + " ";

var SelectedOption = 'document.TheOperations' + FormNo + '.FirstOperation.options[FirstOp].selected=true';
eval (SelectedOption)
var SelectedOption = 'document.TheOperations' + FormNo + '.SecondOperation.options[SecondOp].selected=true';
eval (SelectedOption)
}

function CheckNumbersWrite(FormNumber){
	//crude check for valid data entered
var testresult=0;
var x=eval('document.TheOperations' + FormNumber + '.BiggestNumber.value');
var y=eval('document.TheOperations' + FormNumber + '.FirstNumber.value');
var z=eval('document.TheOperations' + FormNumber + '.SecondNumber.value');
var anum=/(^\d+$)|(^\d+\.\d+$)/;

//Check Max. Number
if (anum.test(x) && (x > 5) && (x < 1000000)) {
    testresult=1;
}
else{
    alert("Please input a valid number between 5 and 1000000 as the Max. Number!");
    testresult=false;
}
//Check First Number
if (anum.test(y) && (y > -100) && (y < 100)) {
    testresult=testresult+1;
}
else{
    alert("Please input a valid number between -100 and 100 as the First Number!");
    testresult=false;
}

//Check Second Number
if (z == '') {
testresult=testresult+1;
}
else {
   if (anum.test(z) && (z > -100) && (z < 100)) {
       testresult=testresult+1;
   }
   else{
       alert("Please input a valid number between -100 and 100 as the Second Number!");
       testresult=false;
   }
}
//end of crude check
if (testresult==3) { //good data to rewrite table
testresult=true;
WriteTable(FormNumber)
}
return (testresult);
}


function togAnsRules(TheSpan) { //toggle the visibility of the boxes and output
var divs=document.getElementsByTagName("SPAN");
	for(var i=0; i < divs.length; i++){
		/* look for the class name */
		if(divs[i].className.indexOf(TheSpan) != -1){
			/* found, do something */
			if(divs[i].style.visibility == "hidden"){
			divs[i].style.visibility = "visible";
			}
			else{
			divs[i].style.visibility = "hidden";
			}
		}
	}
	
}

