// Good sample rate finder

// Copyright 2004 Damian Yerrick
// The author grants permission to reproduce, modify, distribute,
// publicly perform, and/or publicly display this work, provided that
// each copy preserves this copyright notice, permission notice, and
// warranty disclaimer.
// THE AUTHOR MAKES ABSOLUTELY NO WARRANTY OR REPRESENTATION,
// EXPRESS OR IMPLIED, WITH RESPECT TO ANY ASPECT OF THIS WORK.
// UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY SHALL THE
// AUTHOR BE HELD LIABLE FOR ANY DAMAGES RESULTING FROM THE USE
// OF THIS WORK.


function TZ(x,n)
{
  x=""+x
  var i = x.lastIndexOf(".")
  if(i<0) {i = x.length; x += "."}
  i = x.length - i - 1
  while(i<n) {x += "0";i++}
  return x
}

// I left the innerHTML version at home on my PC
// because though it works fine in Mozilla,
// it gives "Unknown runtime error" on IE 6.

// append a cell to a row
function addCell(row, text)
{
 var td = document.createElement("TD");
 td.style.textAlign = "right";
 var a_text = document.createTextNode(text);
 td.appendChild(a_text);
 row.appendChild(td);
}

// remove all rows from a tbody
function clearBody(tbody)
{
 var n = tbody.rows.length;

 while(n > 0)
 {
  n -= 1;
  tbody.deleteRow(n);
 }
}

// generate the table using manipulation of HTML DOM
// works on Mozilla and IE 6
function dom_update()
{
 var masterClock = 1*document.getElementById("masterClock").value
 var fetchWidth = 1*document.getElementById("fetchWidth").value
 var vblankLen = 1*document.getElementById("vblankLen").value
 var vblanksPerFrame = 1*document.getElementById("vblanksPerFrame").value
 var periodMin = 1*document.getElementById("periodMin").value
 var periodMax = 1*document.getElementById("periodMax").value
 var frameLen = vblankLen * vblanksPerFrame
 var tbody = document.getElementById("datahere");

 clearBody(tbody)

 for(var period = periodMin; period <= periodMax; period++)
 {
  var fetches = frameLen / period / fetchWidth
  if(fetches == Math.floor(fetches))
  {
   var tr = document.createElement("TR")
   addCell(tr, period)
   addCell(tr, frameLen/period)
   addCell(tr, TZ(Math.round(100 * masterClock / period)/100,2))
   tbody.appendChild(tr)
  }
 }
}

function update() { dom_update() }

