// Copyright (c) 2002 Kathleen M. Dyer.  All rights reserved.
//
// If you copy and use this without permission, you are stealing.

function clearAll(aform){
  aform.width.value = ""
  aform.height.value = ""
  aform.count.value = ""
  aform.extra.value = ""
  if (aform.threads.type != "hidden") {
    aform.threads.value = "";
  }

  clearAnswer(aform)
}


function clearAnswer(aform){
  aform.fab_width.value = ""
  aform.fab_height.value = ""
}


function checkFloat(val, description, min_val){
  if (val.length == 0 ) {
    alert("There is no number entered for the " + description)
    return 1
  }
  if (val < min_val) {
    alert("The " + description + " cannot be less than " + min_val + ".")
    return 1
  }
  return 0
}


function checkInt(val, description, min_val){
  if (val.length == 0 ) {
    alert("There is no number entered for the " + description)
    return 1
  }
  if (val < min_val) {
    alert("The " + description + " cannot be less than " + min_val + ".")
    return 1
  }
  if (Math.round(val) != val) {
    alert("The " + description + " must be a whole number.")
    return 1
  }
  return 0
}



function computeForm(aform){

  var width  = aform.width.value
  result = checkInt(width, "number of stitches wide", 1)
  if (result == 1) {return}

  var height = aform.height.value
  result = checkInt(height, "number of stitches high", 1)
  if (result == 1) {return}
  

  var count = aform.count.value
  result = checkFloat(count, "fabric count", 1.0)
  if (result == 1) {return}
  if (aform.units.value == "metric") {
    for (var i = 0; i < aform.count_button.length; i++) {
      if (aform.count_button[i].checked) {
        count_button = aform.count_button[i].value
        break
      }
    }
    if (count_button == "count_inch") {
      var count = aform.count.value / 2.54
    }
    else {
      var count = aform.count.value
    }
  }

  var extra  = 2 * aform.extra.value
  result = checkFloat(extra, "extra fabric", 0.0)
  if (result == 1) {return}

  var threads = aform.threads.value
  result = checkInt(threads, "number of threads per X", 1)
  if (result == 1) {return}


  var stitches_per = count / threads
  
  var fab_width  = Math.ceil((width / stitches_per) + extra)
  var fab_height = Math.ceil((height / stitches_per) + extra)

  aform.fab_width.value = fab_width
  aform.fab_height.value = fab_height
		
}


