﻿// JScript File
//
// Developed and coded by
// Author: Sunhee Miller
//         copyright@2006
//
// This property is protected by the copyright.  Please do not download
// and/or use this for any personal/commercial use without author's 
// permission.  For permission, contact webadmin@smifzone.org.
// Thank you for your cooperation!
//

//
// global variables
//
var count = 0;
var a = new Array(200);
var OrgNum;
var tempCount = 0;

var screenD = new Array(7);

screenD[0] = "6 is a PERFECT number.";
screenD[1] = "To identify it as a perfect number,";
screenD[2] = "Find all divisors(other than itself) of a number.";
screenD[3] = "Then, add them up all together including 1.";
screenD[4] = "1 and the number itself are divisors of all numbers.";
screenD[5] = "If the sum of divisors except the number itself is equal to the number itself, ";
screenD[6] = "we have a winner,";
screenD[7] = "a PERFECT number!";

// *************************************************************************
// JScript Function for the perfect numbers
// *************************************************************************

//
// This is the main function called by the web page when submitted by a user.
//
function numPerfect() {

var numP = "";
var Num = 0;
var temp;
var len;

//
// Cleans up the message board for new result.
document.getElementById('result1').innerHTML = "";
document.getElementById('result2').innerHTML = "";
document.getElementById('result3').innerHTML = "";
document.getElementById('result4').innerHTML = "";

numP = document.getElementById('pEntry1').value;

len = numP.length;

if (len > 6){
  alert("Up to 6 digits allowed!");
  return;
}

if (IsNum(numP) == false){
  alert ("Numbers (postive integer value) only, please!");
  return;
}
else
{ 
  Num = Number(numP);
  
  //Initialize global variables

  for (i=0; i<200; i++)
    a[i] = 0;
    
  count = 0;
  OrgNum = Num;
  tempCount = 0;
    
  temp = FindDivisors(Num);
  DisplayResult(Num, temp);

}

}

//
// To validate the input value submitted - numeric or alphanumeric...
//
function IsNum(n){

var NumRng = "0123456789";
var ch = "";
var len = 0;
var i, k;
var IsN = true;

len = n.length;

k=0;

for(i=0;i<len && k==0;i++){
  ch = n.charAt(i);
  k = NumRng.indexOf(ch);
  if (k == -1)
    IsN = false;
}

return IsN;

}

//
// Here we identify all divisors of the number provided and check if it is a perfect number.
//


function FindDivisors(N){

var i, j, k, t, f;
var sum = 1;

k = N;
t = k / 2;

f = Math.floor(t);

j = 0;
for(i=2;i<=f;i++){
  if ((k % i) == 0){
    a[j] = i;   
    j++;
  }
}

if (j == 0)
  sum = 1;
else
{
  for (i=0;i<j;i++){
    if (a[i] != 0)
      sum = sum + a[i];    
  }

}

return sum;
}






function DisplayResult(N, K) {

var n = N; // the value of a number entered
var k = K; // sum of divisors
var tempMsg = "1";

//constructing a string of divisors
for (i=0;i<200;i++){ 
  if (a[i] != 0){
    tempMsg = tempMsg + ",   " + a[i];
  }
}

if (n == 1)
  tempMsg = "";
  
document.getElementById('result1').innerHTML = "Divisors (other than itself) found are:";
document.getElementById('result2').innerHTML = tempMsg;

tempMsg = ""
if (n == 1)
  k = 0;
tempMsg = "Sum of divisors is " + k;
document.getElementById('result3').innerHTML = tempMsg;

if (n == k)
  document.getElementById('result4').innerHTML = "This is a PERFECT number!";
else if ((k == 1) && (n != 1))
  document.getElementById('result4').innerHTML = "This is a prime number.";
else
  document.getElementById('result4').innerHTML = "This is NOT a perfect number!";


}



//
// Reinitializes the input field.
//
function Field2Reset() {

document.getElementById('pEntry1').value = "";
document.getElementById('result1').innerHTML = "";
document.getElementById('result2').innerHTML = "";
document.getElementById('result3').innerHTML = "";
document.getElementById('result4').innerHTML = "";

}



//
// message function by setting timeout function
//
function DisplayMessage() {

var i, j, t;
var temp = "";

i = count + 1;
temp = "pf" + i;
document.getElementById(temp).innerHTML = screenD[count];
count++;

if (count < 8)
  setTimeout("DisplayMessage()", 1500);
else
  clearTimeout();

}


