/*	yarntree.com started 12/04
	revisions:
	

*/

function clearAll(xsform){
  xsform.width.value = ""
  xsform.height.value = ""
  xsform.count.value = ""
  xsform.extra_border.value = "2"
  xsform.extra_finishing.value = "2"
  xsform.threads.value = "1"
  ClearResults(xsform)
}
       	
function ClearResults(xsform){
  xsform.fab_width.value = ""
  xsform.fab_height.value = ""
  xsform.stitch_width.value = ""
  xsform.stitch_height.value = ""
  xsform.needle.value = ""
  xsform.xs_strands.value = ""
  xsform.bs_strands.value = ""
}


function checkFloat(val, description, min_val){
  if (val.length == 0 ) {
    alert("Please enter a number in 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("Please enter a number in 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 eighths(val){
	var whole = Math.floor(val + .063)
	var fract = val - whole + .063
	var infraction = whole 
	if (fract > .875) {infraction = whole + "  7/8"; return infraction}
	if (fract > .75) {infraction = whole + "  3/4"; return infraction}
	if (fract > .625) {infraction = whole + "  5/8"; return infraction}
	if (fract > .5) {infraction = whole + "  1/2"; return infraction}
	if (fract > .375) {infraction = whole + "  3/8"; return infraction}
	if (fract > .25) {infraction = whole + "  1/4"; return infraction}
	if (fract > .125) {infraction = whole + "  1/8"; return infraction}
	return infraction
}

function halfs(val){
	var whole = Math.floor(val + .25)
	var fract = val - whole + .25
	var infraction = whole 
	if (fract > .5) {infraction = whole + "  1/2"; return infraction}
	return infraction
}

function computeForm(xsform){
  var width  = xsform.width.value
  result = checkInt(width, "stitches wide box", 1)
  if (result == 1) {return}

  var height = xsform.height.value
  result = checkInt(height, "stitches high box", 1)
  if (result == 1) {return}
  
  var count = xsform.count.value
  result = checkFloat(count, "threads per inch box", 1.0)
  if (result == 1) {return}

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

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

  var stitches_per = count / threads
  
  var fab_width  = (width / stitches_per) + extra
  	fab_width = halfs(fab_width)
  	
  var fab_height = (height / stitches_per) + extra
  	fab_height = halfs(fab_height)

  var stitch_width = width / stitches_per
  	stitch_width = eighths(stitch_width)
  	
  var stitch_height = height / stitches_per
  	stitch_height = eighths(stitch_height)

  xsform.fab_width.value = fab_width
  xsform.fab_height.value = fab_height
  
  xsform.stitch_width.value = stitch_width
  xsform.stitch_height.value = stitch_height	
  
  xsform.needle.value= "?"
  if (count > 5 ) {xsform.needle.value= 16}
  if (count > 7 ) {xsform.needle.value= 20}
  if (count > 11 ) {xsform.needle.value= 22}
  if (count > 13 ) {xsform.needle.value= 24}
  if (count > 15 ) {xsform.needle.value= 26}
  if (count > 31 ) {xsform.needle.value= 28}  
 
xsform.xs_strands.value= "?"
  if (stitches_per > 6 ) {xsform.xs_strands.value= 4}
  if (stitches_per > 10 ) {xsform.xs_strands.value= 3}
  if (stitches_per > 13 ) {xsform.xs_strands.value= 2}
  if (stitches_per > 18 ) {xsform.xs_strands.value= 1}

xsform.bs_strands.value= "?"
  if (stitches_per > 6 ) {xsform.bs_strands.value= 3}
  if (stitches_per > 10 ) {xsform.bs_strands.value= 2}
  if (stitches_per > 13 ) {xsform.bs_strands.value= 1}
  if (stitches_per > 22 ) {xsform.bs_strands.value= 1}
  
}

