﻿// JScript 
//
// 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 mesg = new Array(11);

mesg[0]="You can't break up PRIME numbers";
mesg[1]="as you can with other numbers";
mesg[2]="for instance, ";
mesg[3]="6 / 2 = 3 or 6 / 3 = 2";
mesg[4]="in addition, 6 / 6 = 1 or 6 / 1 = 6."
mesg[5]="now, let's look at a number 7,";
mesg[6]="7 / 7 = 1 or 7 / 1 = 7";
mesg[7]="it can only be divided by ";
mesg[8]="itself or 1";
mesg[9]="this is called";
mesg[10]="a PRIME number";
mesg[11]="2 is the smallest prime and the only even prime number."

var count = 0;


// *************************************************************************
// JScript Function for the prime number identifier/finder.
// *************************************************************************

// The main function called by a submit action from a web page.
// The input value is grabed and evaluated for the type of value - numeric or alphanumeric?
// Then it calls a function that identifies whether it is a prime or not.
//
function ValidateNum(){

var numP = "";
var Num = 0;
var tempMsg = "";
var len;

document.getElementById('result2').innerHTML = "" ;
document.getElementById('result3').innerHTML = "" ;

numP = document.getElementById('dEntry1').value;

len = numP.length;

if (len > 10) {
  alert("No more than 10 digits please!");
  return;
}
 
if (len == 0){
  alert("You must enter a numeric (positive integer) value!");
  return;
}

if (IsNum(numP) == false){
  alert ("Numbers (positive integer value) only, please!");
  return;
}
else
{ 
  Num = Number(numP);
  if(IsPrime(Num)== true){
    tempMsg = "Yes, " + Num + " is a PRIME number!"
    document.getElementById('result3').innerHTML = tempMsg ;
  }
  else {
    tempMsg = "Sorry, " + Num + " is NOT a prime number!"
    document.getElementById('result3').innerHTML = tempMsg ;
  }
}

}

//
// Validates the input value - 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;

}

//
// Validate if the entered number is prime or not.
//
function IsPrime(k){

var n = k;
var byT = true, byTh = true, byF = true, byS = true;
var ret = false;

if ((n == 2) || (n % 2) != 0)
  byT = false;
  
if ((n == 3) || (n % 3) != 0)
  byTh = false;
  
if ((n == 5) || (n % 5) != 0)
  byF = false;

if ((n == 7) || (n % 7) != 0)
  byS = false;
  
if ((byT == false) && (byTh == false) && (byF == false) && (byS == false)){
  if (checkPrime(n) == true)
    ret = false;
  else
    ret = true;
}
  
return ret;

}



function checkPrime(k){

var i, j, t, f;
var noPrime = false;

t = k / 2;

f = Math.floor(t);
for(i=11;(i<f) && (noPrime == false);i++){
  if (((i % 2) != 0) && ((i % 3) != 0) && ((i % 5) != 0) && ((i % 7) != 0)){
    j = k % i;
    if (j == 0)
      noPrime = true;
  }
}

return noPrime;
}



//
// A function to reset the input field.
//
function Field1Reset() {

document.getElementById('dEntry1').value = "";
document.getElementById('result2').innerHTML = "" ;
document.getElementById('result3').innerHTML = "" ;


}

function Field2Reset() {

document.getElementById('dEntry2').value = "";
document.getElementById('dEntry3').value = "";

document.getElementById('result2').innerHTML = "" ;
document.getElementById('result3').innerHTML = "" ;


}

//
// Given the range of valid numeric value, searches the prime numbers.
//
function FindPrimes() {

var numP1 = "", numP2 = "";
var Num1, Num2;
var len1, len2;

document.getElementById('result2').innerHTML = "" ;
document.getElementById('result3').innerHTML = "" ;

numP1 = document.getElementById('dEntry2').value;
numP2 = document.getElementById('dEntry3').value;

len1 = numP1.length;
len2 = numP2.length;

if ((len1 > 4) || (len2 > 4)){
  alert("No more than 4 digits please!");
  return;
}

if ((len1 == 0) || (len2 == 0)){
  alert("You must enter the ranges!");
  return;
}

if ((IsNum(numP1) == false) || (IsNum(numP2) == false)){
  alert ("Numbers (positive integer value) only, please!");
  return;
}
else
{
  Num1 = Number(numP1);
  Num2 = Number(numP2);
  PrimeFinder(Num1, Num2);
}

}


//
// Here all primes are identified.
//
function PrimeFinder(N1, N2){

var n1 = N1, n2 = N2, n3;
var i, j, k, t;
var a = new Array(1500);
var temp;

var two = true, three = true, five = true, seven = true;

for (i=0; i<1500; i++){
  a[i] = 0;
}

if (n1 > n2){
  n3 = n1;
  n1 = n2;
  n2 = n3;
}

j = 0;
for (i=n1; i<=n2; i++){
  if (((i % 2) != 0) || (i == 2))
    two = false;
  if (((i % 3) != 0) || (i == 3))
    three = false;
  if (((i % 5) != 0) || (i == 5))
    five = false;
  if (((i % 7) != 0) || (i == 7))
    seven = false;
    
  if ((two == false) && (three == false) && (five == false) && (seven == false)&& (i != 1)){
    if (checkPrime(i) != true){
      a[j] = i;
      j++;
    }
  }
  
  two = true;
  three = true;
  five = true;
  seven = true;
  
}

var tempMsg3 = "";
var tempMsg2 = "";
var tempMsg4 = "";

t = 0;
  
for (i=0; i<j ; i++){
  temp = a[i];
  if (t < 800){
    if (t == 0)
      tempMsg3 = tempMsg3 + a[t];
    else
      tempMsg3 = tempMsg3 + ",    " + a[t];
  }
  else
    tempMsg4 = tempMsg4 + ",    " + a[t];
  
  t++;
} 

if (t > 0){
  tempMsg2 = "There are " + t + " PRIME numbers between " + n1 + " and " + n2 + ".";
  document.getElementById('result2').innerHTML = tempMsg2 ;
  document.getElementById('result3').innerHTML = tempMsg3 + "   " + tempMsg4;
}
else
  document.getElementById('result3').innerHTML = "No PRIME numbers are found!" ;


}


//
// message function by setting timeout function
//
function DisplayMessage() {

var i, j, t;
var temp = "";

i = count + 1;
temp = "Display" + i;
document.getElementById(temp).innerHTML = mesg[count];
count++;


if (count <= 11)
  setTimeout("DisplayMessage()", 1500);
else
  clearTimeout();

}