////////////////////&copy 2004 Jim Atkins webadmin@jbatkins.com
/////////////////free for personnal use and distributation as long as this &copy header stays in place

////////////////see help file


////////////////////////declare some globals
var PRMdiv="false";
var oddend="false";
var isnotodd="false";
var trible="false";
var stmpm="false";
var getlcd="false";
var maxtry="";
var POSprm="false";
function PrFindstart(){                     /////////////this is called from PrFind and takes off after all input
   if (document.inp.inp1.value==""||document.inp.inp2.value==""){
      alert("Please enter number before selecting this function")
      return;
   }
   display10();
   setTimeout("PrFindgo()", 1);
}
function GCDstart(){                    ///////////////this is called from BegFiGCD
   if (document.inp.inp1.value==""||document.inp.inp2.value==""){
      alert("Please enter number before selecting this function")
      return;
   }
   display1();
   setTimeout("FiGCD()", 1);
}

function display10(){
   display3();
}
function PrFind(){                              ///////////////////we are just getting input here
   if(document.inp.inp1.value==""){
      document.inp.inp1.value=document.inp.inp0.value
      document.inp.inp0.value=""
      writeAns();
      return;
   }
   else{
      document.inp.inp2.value=document.inp.inp0.value
      PrFindstart()
   }
}
//////////////////////////////////////below are all the div test routines
function PrFindgo(){
   PRMdiv="false"
   var keep=new Array()
   var keepme=new Array()
   var TriN=document.inp.inp2.value
   var TriNlgth=TriN.length
   var BasE=document.inp.inp1.value
   var BasElgth=BasE.length
   var TriNUp=TriN
   var TriNUplgth=TriNUp.length
   var Metod="plus"
   var VenB=""
   var TriNQ="false"
   var TriNld=TriNUp.charAt(TriNlgth-1)
   var TriNldtmp=TriN.charAt(TriNlgth-1)
   if(TriNldtmp==0||TriNldtmp==2||TriNldtmp==4||TriNldtmp==5||TriNldtmp==6||TriNldtmp==8){
      document.inp.inp0.value="Not Prime"
      writeAns();
      display4(PRMdiv);
      return;
   }                                        //////////if divisor ends in 0,2,4,5,6,8 it's not prime we back out

isThisprm=TriN
isThisprmprt=TriN.substring(0,TriN.length-1)
isThisprmprtlgth=isThisprmprt.length
var Getresult=0
for(prmprt=0; prmprt<isThisprmprtlgth; prmprt++){
  document.inp.inp1.value=isThisprmprt.charAt(prmprt)
  document.inp.inp2.value=Getresult
  document.inp.inp3.value="plus";
  Alphacalcplus();
  Getresult= document.inp.inp0.value
}
     ///////////////here is where we use the assumption "Rythem found when you add all digits together before last digit and combine that number with last
     ////////////// digit, if the resulting number has no divisors the origional number does not"
  Getresult=Getresult+TriNldtmp
  document.inp.inp1.value=Getresult
  document.inp.inp2.value=Getresult
  getlcd="true"
  maxtry=1000
  FiGCD();
if(POSprm=="false"){
      document.inp.inp0.value="Possibly Not Prime"
      writeAns();
      display4(PRMdiv);
      return;
}
   
   /////////////////////////////////////////
   //////////here we take the divisor and add it to itself untill we have a number with a 1 or 9
   ////////////as last digit, if we start with such a number we still do this to the next highest number
   ////////////that ends in 1 or 9
   ////////////////////////
   while(TriNldtmp==1||TriNldtmp==9||TriNQ=="false"){
      document.inp.inp1.value=TriNUp
      document.inp.inp2.value=TriN
      document.inp.inp3.value="plus";
      Alphacalcplus();
      TriNUp=document.inp.inp0.value
      TriNUplgth=TriNUp.length
      TriNldtmp=TriNUp.charAt(TriNUplgth-1)
      if(TriNldtmp==1||TriNldtmp==9){
         TriNQ="true"
         TriNld=TriNldtmp
         break;
      }
   }
   if (PRMdiv=="false"){
      while(TriNldtmp==3||TriNldtmp==7){
         
         document.inp.inp1.value=TriNUp
         document.inp.inp2.value=TriN
         document.inp.inp3.value="plus";
         Alphacalcplus();
         TriNUp=document.inp.inp0.value
         TriNUplgth=TriNUp.length
         TriNldtmp=TriNUp.charAt(TriNUplgth-1)
         TriNld=TriNldtmp
      }
      
      //////once we have the number if it ends in 1 we will use subtraction else addition in our process
      /////we use last digit,1 or 9, as fraction, .1 or .9, and we round the divisor number to nearest whole
      /////this is now the divisor
      if(TriNld==1){
         Metod="minus"
         TriNUp=TriNUp.substring(0,TriNUplgth-1)
         TriNUplgth=TriNUp.length
      }
      else if(TriNld==9){
         TriNUp=TriNUp.substring(0,TriNUplgth-1)
         TriNUp=parseInt(TriNUp)+01
         TriNUp=TriNUp.toString();
         TriNUplgth=TriNUp.length
      }
      
      /////////now we process the divisor for number being tested
      var Bas=""
      var BasID=""
      var Baslgth=""
      var brk="false"
      var cHase=new Array();
      var hase="TriN";
      var cHaselgth=cHase.length
      var haselgth=hase.length
      var tp=0;
      for(tr=BasElgth-1; tr>=0; tr--){           /////////////////process start
         Bas=BasE.substring(0,BasElgth-1)
         BasID=BasE.charAt(BasElgth-1)
         Baslgth=Bas.length
         document.inp.inp1.value=TriNUp
         document.inp.inp2.value=BasID
         document.inp.inp3.value="times";
         Alphacalctimes();
         var VenB=document.inp.inp0.value
         var VenBlgth=VenB.length
         var Sme="false"
         var OVEr="false"
         //////////////////////////////////////////////////////////////////////////////////////////////////////
         while(cHaselgth<25) {
            document.inp.inp1.value=TriN
            document.inp.inp2.value=hase
            document.inp.inp3.value="plus";
            Alphacalcplus();
            hase=document.inp.inp0.value
            haselgth=hase.length
            while (hase.charAt(0)=="0"){
               hase=hase.substring(1,haselgth)
               haselgth=hase.length
            }
            
            cHase[tp]=hase
            cHaselgth=cHase.length
            tp++
         }
         for(xtr=cHaselgth-1; xtr>=0; xtr--){
            if (cHase[xtr]==BasE){
               PRMdiv="true"
               document.inp.inp0.value=""
               writeAns();
               display4(PRMdiv)
               return;
            }
         }
         //////////////////////////////////////////////////////////////////////////////////////////////////////
         ////////////////////////////////////////////////////////////////////////////////////////////////////
         ///here we ck that divisor is not greater than the number we are testing, which changes during process
         ///////////////////////////////////////////////////////////////////////////////////////////////////
         if (Baslgth<VenBlgth){
            break;
         }
         if (Baslgth==VenBlgth){
            document.inp.inp1.value=Bas
            document.inp.inp2.value=VenB
            ConvBack();
            
            StVenB=document.inp.inp1.value
            StBas=document.inp.inp2.value
            qw=0;
            wtabu=0;
            wtabu1=0;
            Ovlf=0;
            Ovrt=0;
            
            for(ute=Baslgth-1; ute>=0; ute--){
               Ovlf=matrixstr.indexOf(StVenB.charAt(qw))
               Ovrt=matrixstr.indexOf(StBas.charAt(qw))
               
               
               wadutn=parseInt(Ovlf);
               wadutn1=parseInt(Ovrt);
               if(wadutn<wadutn1&&qw==0&&Metod=="minus"){
                  var OVEr="true"
                  
                  break;
                  
               }
               if(Sme=="true"&&wadutn<wadutn1&&Metod=="minus"){
                  var OVEr="true"
                  
                  break;
               }
               if(wadutn==wadutn1){
                  Sme="true"
               }
               else{
                  Sme="false"
               }
               if (wadutn>wadutn1&&qw==0){
                  break;
               }
               qw++
            }
         }
         
         //////////////////////////////////////////////////////////////////////////////////////////////////////////
         ///////////// the rest of the processing gets kinda deep but basiclly we are checking for certain criteria
         //////////////and backing out or going forward depending on the results
         ////////////this whole process is checking if a number is div by a number that isn't div by any number
         //////////////without doing the div.It will still take some time with long numbers but not as long as the div
         //////////////////////////////////////////////////////////////..maybe
         document.inp.inp1.value=Bas
         document.inp.inp2.value=VenB
         document.inp.inp3.value=Metod;
         
         if(OVEr=="false"){
            if(Metod=="minus"){
               Alphacalcminus()
            }
            else {
               Alphacalcplus()
            }
            BasE=document.inp.inp0.value
            BasElgth=BasE.length
            
         }
         else{
            OVEr=="false"
            break;
         }
         
         
         while (BasE.charAt(0)=="0"){
            BasE=BasE.substring(1,BasElgth)
            tmpBasElgth=BasE.length
         }
         if (BasE==VenB){
            PRMdiv="true"
            document.inp.inp0.value=""
            writeAns();
            display4(PRMdiv);
            return;
         }
         PARbas=parseInt(BasE)
         PARtrin=parseInt(TriN)
         if (PARbas==0){
            PRMdiv="true"
            document.inp.inp0.value=""
            writeAns();
            display4(PRMdiv);
            return;
         }
         if (PARtrin==0){
            PRMdiv="true"
            document.inp.inp0.value=""
            writeAns();
            display4(PRMdiv);
            return;
         }
         if (PARbas==PARtrin){
            PRMdiv="true"
            document.inp.inp0.value=""
            writeAns();
            display4(PRMdiv);
            return;
         }
         if (PARbas<PARtrin){
            OVEr="true"
            break;
         }
         /////////////////////////////////////////////////////////////////////////////////////////////////////////
         ////////////////////////////////////////////////////////////////////////////////////////////////////////
         keep[tr]=BasE////////////////
         keepme[tr]=VenB/////////////keep this for later
      }                                                ////////processing ends
      if (OVEr=="false"&&PRMdiv=="false"){              //////we take whats left and div by orig divisor
         
         document.inp.inp1.value=BasE
         document.inp.inp2.value=TriN
         document.inp.inp3.value="divisors";
         Alphacalcdiv();
      }
      if (document.inp.reman.value==""&&OVEr=="false"){
         PRMdiv="true"                      /////the number is div by prime else not
      }
      else{
         PRMdiv="false"
      }
   }
   
   document.inp.inp0.value=""
   writeAns();
   display4(PRMdiv);
   
}                                                  ///////////////PrFind end


function d2(){
   PRMdiv="false"
   isnotodd="false"
   oddend="false"
   var prnum=document.inp.inp0.value
   var prnLGTH=prnum.length
   if (prnum.charAt(prnLGTH-1)==1 || prnum.charAt(prnLGTH-1)==3 || prnum.charAt(prnLGTH-1)==5 || prnum.charAt(prnLGTH-1)==7||prnum.charAt(prnLGTH-1)==9){
      oddend="true"
   }
   else{
      isnotodd="true"
   }
   if (isnotodd=="true"){
      PRMdiv="true"
   }
   document.inp.inp0.value=""
   writeAns();
   display4(PRMdiv);
   return(isnotodd);
}

function d3(){
   trible="false"
   PRMdiv="false"
   digRoot();
   var Dstop=document.inp.inp0.value;
   if (Dstop==3 || Dstop==6 || Dstop==9){
      trible="true"
   }
   if (trible=="true"){
      PRMdiv="true"
   }
   document.inp.inp0.value=""
   writeAns();
   display4(PRMdiv);
}


function d4(){
   PRMdiv="false"
   var prnum=document.inp.inp0.value
   var prnLGTH=prnum.length
   var lasttwo=prnum.charAt(prnLGTH-2)+prnum.charAt(prnLGTH-1);
   var laso=lasttwo
   var evefour=lasttwo/4
   n = evefour;
   x=parseFloat(n) - parseInt(n);
   if (x==0){
      PRMdiv="true";
   }
   document.inp.inp0.value=""
   writeAns();
   display4(PRMdiv);
}

function d5(){
   PRMdiv="false"
   var prnum=document.inp.inp0.value
   var prnLGTH=prnum.length
   if (prnum.charAt(prnLGTH-1)==0 ||prnum.charAt(prnLGTH-1)==5){
      PRMdiv="true"
   }
   document.inp.inp0.value=""
   writeAns();
   display4(PRMdiv);
}

function d6(){
   var prnum=document.inp.inp0.value
   var prnLGTH=prnum.length
   PRMdiv="false"
   trible="false"
   isnotodd="false"
   d2(isnotodd)
   PRMdiv="false"
   if (isnotodd=="true"){
      document.inp.inp0.value=prnum
      digRoot();
      var Dstop=document.inp.inp0.value;
      if (Dstop==3 || Dstop==6 || Dstop==9){
         trible="true"
      }
      
   }
   if (isnotodd=="true"&&trible=="true"){
      PRMdiv="true"
   }
   document.inp.inp0.value=""
   writeAns();
   display4(PRMdiv);
   
}

function d7(){
   var prnum=document.inp.inp0.value
   var prnLGTH=prnum.length
   PRMdiv="false"
   var kpar=prnum;
   var sevens=0;
   var stat=1;
   var iw=kpar.length-1;
   while(iw>=0){
      if (iw>=0){
         sevens=sevens+kpar.charAt(iw)*stat
         stat=stat+2   ///stat=3
         iw--
      }
      if (iw>=0){
         sevens=sevens+kpar.charAt(iw)*stat
         stat=stat-1  ///stat=2
         iw--
      }
      if (iw>=0){
         sevens=sevens+kpar.charAt(iw)*stat
         stat=stat-3 ///stat=-1
         iw--
      }
      if (iw>=0){
         sevens=sevens+kpar.charAt(iw)*stat
         stat=stat-2 ///stat=-3
         iw--
      }
      if (iw>=0){
         sevens=sevens+kpar.charAt(iw)*stat
         stat=stat+1 ///stat=-2
         iw--
      }
      if (iw>=0){
         sevens=sevens+kpar.charAt(iw)*stat
         stat=1
         iw--
      }
   }
   
   var troll=sevens/7
   var n = troll;
   var x=parseFloat(n) - parseInt(n);
   if (x==0){
      PRMdiv="true"
   }
   
   document.inp.inp0.value=""
   writeAns();
   display4(PRMdiv);
   
}

function d8(){
   var prnum=document.inp.inp0.value
   var prnLGTH=prnum.length
   PRMdiv="false"
   var lasttwo=prnum.charAt(prnLGTH-2)+prnum.charAt(prnLGTH-1);
   if (prnum.charAt(prnLGTH-3)==0 && prnum.charAt(prnLGTH-2)==0 && prnum.charAt(prnLGTH-1)==0){
      var eights="true"
   }
   else if(prnum.charAt(prnLGTH-3)==0 || prnum.charAt(prnLGTH-3)==2 || prnum.charAt(prnLGTH-3)==4 || prnum.charAt(prnLGTH-3)==6 || prnum.charAt(prnLGTH-3)==8){
      var eve="true"
   }
   else
   {
      eve="false"
   }
   if (eve=="true"){
      var evetwo=lasttwo/8
      n = evetwo;
      x=parseFloat(n) - parseInt(n);
      if (x==0){
         PRMdiv="true"
      }
   }
   else{
      evetwo=lasttwo-4
      evetwo=evetwo/8
      n = evetwo;
      x=parseFloat(n) - parseInt(n);
      if (x==0){
         PRMdiv="true"
      }
      
   }
   document.inp.inp0.value=""
   writeAns();
   display4(PRMdiv);
}

function d9(){
   var prnum=document.inp.inp0.value
   var prnLGTH=prnum.length
   PRMdiv="false"
   digRoot();
   var Dstop=document.inp.inp0.value;
   if (Dstop==9){
      PRMdiv="true"
   }
   document.inp.inp0.value=""
   writeAns();
   display4(PRMdiv);
}

function BegFiGCD() {
   if(document.inp.inp1.value==""){
      document.inp.inp1.value=document.inp.inp0.value
      document.inp.inp0.value=""
      writeAns();
      return;
   }
   else{
      document.inp.inp2.value=document.inp.inp0.value
      GCDstart();
   }
}
function FiGCD() {

   stmpm="true"
   var flip="false"
   var Sme="false"
   var Ls=document.inp.inp1.value
   var Lslgth=Ls.length
   var Ss=document.inp.inp2.value
   var Sslgth=Ss.length
   if (Lslgth<Sslgth){
      flip="true"
   }
   if (Lslgth==Sslgth){
      ConvBack();
      StVenB=document.inp.inp1.value
      StBas=document.inp.inp2.value
      qw=0;
      wtabu=0;
      wtabu1=0;
      Ovlf=0;
      Ovrt=0;
      
      for(ute=Lslgth-1; ute>=0; ute--){
         Ovlf=matrixstr.indexOf(StVenB.charAt(qw))
         Ovrt=matrixstr.indexOf(StBas.charAt(qw))
         
         
         wadutn=parseInt(Ovlf);
         wadutn1=parseInt(Ovrt);
         if(wadutn<wadutn1&&qw==0){
            flip="true"
            
            break;
            
         }
         if(Sme=="true"&&wadutn<wadutn1){
            flip="true"
            break;
         }
         if(wadutn==wadutn1){
            Sme="true"
         }
         else{
            Sme="false"
         }
         if (wadutn>wadutn1&&qw==0){
            break;
         }
         qw++
      }
   Conv()
   }
   if (flip=="true"){
      document.inp.inp2.value=Ls
      document.inp.inp1.value=Ss
      Ls=document.inp.inp1.value
      Ss=document.inp.inp2.value
   }
   document.inp.inp1.value=Ls
   document.inp.inp2.value=Ss
   DrillD();
   var GnumD = document.inp.inp0.value
   var GnumR = document.inp.reman.value
   var PRnum=document.inp.reman.value
   var Pgcd=parseInt(GnumR)
   document.inp.inp1.value=Ss
   document.inp.inp2.value=GnumR
   if(Pgcd>0){
      DrillD();
      GnumD=GnumR
      GnumR = document.inp.reman.value
   }
   else {
      PRnum=Ss
      GnumD=GnumR
      GnumR = document.inp.reman.value
   }
   Pgcd=parseInt(GnumR)
   while(Pgcd>0){
      PRnum=document.inp.reman.value
      document.inp.inp1.value=GnumD
      document.inp.inp2.value=GnumR
      document.inp.inp3.value="div"
      DrillD();
      GnumD = GnumR
      GnumR = document.inp.reman.value
      Pgcd=parseInt(GnumR)
      document.inp.inp0.value=""
   }
   document.inp.inp0.value=PRnum
   document.inp.reman.value="0"

   if(getlcd=="true"){   ////////////////////this is where we look to see if LCD is on
      var targetcd=parseInt(PRnum)
      var flcd=2
	var sqrt=1
   for (iq = 0; iq < 10; iq++){      ////// ok this is straying from my not using javascript methods
    sqrt =sqrt - ((sqrt*sqrt - targetcd) / (2*sqrt)); ///////This is a quik fix for an issue...this is an extra anyways..not the calc
  sqrt=Math.round(sqrt)
}

       while(flcd<sqrt){
         if(flcd<11){
            document.inp.inp0.value=PRnum
            d2();
            flcd++
            if(PRMdiv=="true"){
               PRMdiv="false"
               document.inp.inp0.value=Ls;
               d2();
               if(PRMdiv=="true"){
                  PRMdiv="false"
                  PRnum="2"
                  break;
               }
            }
            document.inp.inp0.value=PRnum
            d3();
            flcd++
            if(PRMdiv=="true"){
               PRMdiv="false"
               document.inp.inp0.value=Ls;
               d3();
               if(PRMdiv=="true"){
                  PRMdiv="false"
                  PRnum="3"
                  break;
               }
            }
            document.inp.inp0.value=PRnum
            d4();
            flcd++
            if(PRMdiv=="true"){
               PRMdiv="false"
               document.inp.inp0.value=Ls;
               d4();
               if(PRMdiv=="true"){
                  PRMdiv="false"
                  PRnum="4"
                  break;
               }
            }
            document.inp.inp0.value=PRnum
            d5();
            flcd++
            if(PRMdiv=="true"){
               PRMdiv="false"
               document.inp.inp0.value=Ls;
               d5();
               if(PRMdiv=="true"){
                  PRMdiv="false"
                  PRnum="5"
                  break;
               }
            }
            document.inp.inp0.value=PRnum
            d6();
            flcd++
            if(PRMdiv=="true"){
               PRMdiv="false"
               document.inp.inp0.value=Ls;
               d6();
               if(PRMdiv=="true"){
                  PRMdiv="false"
                  PRnum="6"
                  break;
               }
            }
            document.inp.inp0.value=PRnum
            d7();
            flcd++
            if(PRMdiv=="true"){
               PRMdiv="false"
               document.inp.inp0.value=Ls;
               d7();
               if(PRMdiv=="true"){
                  PRMdiv="false"
                  PRnum="7"
                  break;
               }
            }
            document.inp.inp0.value=PRnum
            d8();
            flcd++
            if(PRMdiv=="true"){
               PRMdiv="false"
               document.inp.inp0.value=Ls;
               d8();
               if(PRMdiv=="true"){
                  PRMdiv="false"
                  PRnum="8"
                  break;
               }
            }
            document.inp.inp0.value=PRnum
            d9();
            flcd++
            if(PRMdiv=="true"){
               PRMdiv="false"
               document.inp.inp0.value=Ls;
               d9();
               if(PRMdiv=="true"){
                  PRMdiv="false"
                  PRnum="9"
                  break;
               }
            }
         }
         flcd++
         
         document.inp.inp1.value=PRnum
         var flcdstr=flcd.toString()
         var flcdlgth=flcdstr.length
         var cklast=flcdstr.charAt(flcdlgth-1)
         cklast=parseInt(cklast)
         while(cklast==0||cklast==2||cklast==4||cklast==5||cklast==6||cklast==8){
            flcd++
            flcdstr=flcd.toString()
            flcdlgth=flcdstr.length
            cklast=flcdstr.charAt(flcdlgth-1)
            cklast=parseInt(cklast)
         }
         document.inp.inp2.value=flcd
         document.inp.inp3.value="div"
         document.inp.inp0.value=""
         DrillD()
         var tempPR=document.inp.inp0.value
         if(document.inp.reman.value==""){
            document.inp.inp1.value=Ls
            document.inp.inp2.value=flcd
            document.inp.inp3.value="div"
            document.inp.reman.value=""
            DrillD()
            var tempPR=document.inp.inp0.value
            if(document.inp.reman.value==""){
               PRnum=flcd
               break;
            }
         }
         document.inp.inp0.value=""
      }
   }
   document.inp.inp0.value=PRnum;

   if(PRnum==1||Ls==PRnum){
    POSprm="true";
    }




   document.inp.inp1.value="";
   document.inp.inp2.value="";
   stmpm="false"
   stmpmDIV=="false"
   writeAns();
   display2();
   filIn2()
   filIn()
      return;
}
function Changedecplaces(){
   if(decimalmode=="true"){
      var decptxt = prompt("Please enter Max number of decimal places! See help file for use.", "")
      decplaces=parseInt(decptxt);
   }
}
function ChangeGCD(){
   
   if(getlcd=="false"){
      
      var maxtrytxt = prompt("Please enter Max number of attempts to optain LCD! See help file for use.", "")
      maxtry=parseInt(maxtrytxt);
      getlcd="true";
      var	layerGCD = document.getElementById("div5");
      var	FDplyr='lcd';
      layerGCD.innerHTML = FDplyr;
      
   }
   else{
      
      getlcd="false"
      var	layerGCD = document.getElementById("div5");
      var	FDplyr='GCD';
      layerGCD.innerHTML = FDplyr;
      
   }
}
