/*script for perfect numbers page

*/

function integer_check() //
{
var a = eval(document.form1.Value1.value);
if (a!=Math.floor(a)) 
	{document.form1.Value1a.value = "Not a whole number; please enter an integer!!";}
 else 
	{factorsum()}
}

//.........................................

/* This version empties the first, third, fifth ...form values. So it doesn't delete the labels on the buttons! (Assuming there is a text box, then a button etc)
 
function empty_the_boxes() {
var loops=document.form1.length;
//alert(loops);
for (i=0;i<(loops-2);i++)
{document.form1[i].value="";}
}
*/
function empty_boxes() {
document.form1.Value1.value="";
document.form1.Value1a.value="";
document.form2.Value2.value="";
document.form3.Value3.value="";
}


//.........................................
/*
//This is a handy testing function
function rxarray() {
	var rx= new Array();
	rx[0]=/Tim/;
	
return rx
	}

.......................................*/

function test() //works; just puts the value in the first box into the second
{
var a = eval(document.form1.Value1.value);
document.form1.Value1a.value = a;
}


function factorsum() // this function calculates the factor sum of the number, a.
{
var a = eval(document.form1.Value1.value) ;
var b ="";
var k = 0;
var facsum = 0




factors = new Array(1);


	for (i = 1; i <= (Math.sqrt(a)); i++)
{// am going to add the factors in the array as they are generated
b=a/i;
if ((b==Math.floor(b)) && (b!=i))
		{factors[k]=i;
		factors[k+1]=b;
		facsum=facsum + factors[k] + factors[k+1];
		k=k+2;
		};
if (b==i) //ie if 'a' is a square number
		{factors[k]=i;
		facsum=facsum + factors[k]
		k=k+1;
		};

}
function perfect_test() //checks whether 'facsum' is twice 'a'
{
if (2*a == facsum)
	{document.form3.Value3.value = "A perfect number!!";}

else 
	{document.form3.Value3.value = "Not perfect";}
}
test();
perfect_test();
document.form2.Value2.value = facsum;
writetable(factors,k)
}


    function writetable(factors,k) {

        // get the reference for the body
        var body = document.getElementsByTagName("body")[0];

        // creates a <table> element and a <tbody> element
        var tbl     = document.createElement("table");
        var tblBody = document.createElement("tbody");

        // creating all cells
        for (var j = 0; j < k; j=j+2) {
            // creates a table row
            var row = document.createElement("tr");

            for (var i = 0; i < 2; i++) {
                // Create a <td> element and a text node, make the text
                // node the contents of the <td>, and put the <td> at
                // the end of the table row
                var cell = document.createElement("td");
                var cellText = document.createTextNode(factors[j+i]);
                cell.appendChild(cellText);
                row.appendChild(cell);
            }

            // add the row to the end of the table body
            tblBody.appendChild(row);
        }

        // put the <tbody> in the <table>
        tbl.appendChild(tblBody);
        // appends <table> into <body>
        body.appendChild(tbl);
        // sets the border attribute of tbl to 2;
        tbl.setAttribute("border", "2");
    }

    /* ---------------------------------------------- */

