
var pPrime = false;

if ( p%2 == 0 ) {
  var maxFactor = p/2;
  }
else {
  var maxFactor = (p - 1)/2;
  }

var nextPrime = 2;
var primeFlag = false;

var firstFactor = new Array();
var secondFactor = new Array();

function findNextPrime() {
  if ( nextPrime == 2 ) {
    nextPrime = 3;
    }
  else {
    primeFlag = false;
    while ( primeFlag == false ) {
      nextPrime += 2;
      checkDivision(nextPrime);
      }
    }
  }

function checkDivision(testPrime) {
  primeFlag = true;
  for ( a = 2; a <= ((testPrime - 1) / 2); a++ ) {
    if ( testPrime%a == 0 ) {
      primeFlag = false;
      break;
      }
    }
  }

function checkSecondFactor(factor) {
  secondFactorFlag = true;
  for ( a = 2; a <= ((factor - 1) / 2); a++ ) {
    if ( factor%a == 0 ) {
      secondFactorFlag = false;
      break;
      }
    }
  }


var stepNo = 0;
var secondFactorFlag = false;
var pNew = p;

while (!secondFactorFlag) {
  if ( pNew%nextPrime == 0 ) {
    firstFactor[stepNo] = nextPrime;
    secondFactor[stepNo] = Math.round(pNew / nextPrime);
    pNew = Math.round(pNew / nextPrime);
    stepNo += 1;
    if (pNew%2 != 0 || pNew == 2 ) {
      checkSecondFactor(pNew);
      }
    }
  else {
    findNextPrime();
    }
  }
firstFactor[stepNo] = secondFactor[(stepNo - 1)];
if (p == firstFactor[0] ) {
  pPrime = true;
  }

var currentStep = 1;

function showNextStep() {
  if ( currentStep < (firstFactor.length - 1) ) {
    var tempStep = document.getElementById("step" + currentStep);
    var tempLink = document.getElementById("link" + currentStep);
    tempStep.style.visibility = "visible";
    tempLink.style.visibility = "visible";
    if ( currentStep == (firstFactor.length - 2) ) {
      document.getElementById("solutionSteps").style.visibility = "hidden";
      }
    currentStep += 1;
    }
  }