// Crosstitch.com Stitch Calculator 
// This program is copyright - if you want to use my calculator
// on your site link to http://crosstitch.com/calc.html
function fractions(val){
	var whole = Math.floor(val + .1)
	var fract = val - whole + .15
	var result = whole 
	var d = "-"
	if (fract > .75) {result = whole + d + "3/4"; return result}
	if (fract > .5) {result = whole + d + "1/2"; return result}
	if (fract > .25) {result = whole + d + "1/4"; return result}
	return result
}
function calc(data){
  var w  = data.w.value
  var h = data.h.value
  var c = data.c.value
  var u = data.u.value
  var t = data.t.value
  var f  = (2 * data.f.value)
  var stitches = c / t
  
  if (u == 0) {
	var stitch_w = w / stitches 
	var cm = stitch_w * 25.4
	cm = Math.round(cm) / 10
	stitch_w = fractions(stitch_w) + " inches (" + cm + "cm) wide"
	var stitch_h = h / stitches
 	cm = stitch_h * 25.4
	cm = Math.round(cm) / 10
 	stitch_h = fractions(stitch_h) + " inches (" + cm + "cm) high"

	var fab_w  = (w / stitches) + f
	cm = fab_w * 2.54
	cm = Math.round(cm)
	fab_w = Math.round(fab_w) + " inches (" + cm + "cm) wide"
	var fab_h = (h / stitches) + f
 	cm = fab_h * 2.54
	cm = Math.round(cm)
 	fab_h = Math.round(fab_h) + " inches (" + cm + "cm) high"
  }
  if (u > 0) {
	if (u == 2){
		w = w / 2.54
		h = h / 2.54
	}
	var stitch_w = w * stitches 
	var cm = stitch_w * 25.4
	cm = Math.round(cm) / 10
	stitch_w = Math.round(stitch_w) + " stitches _ wide"
	var stitch_h = h * stitches
 	cm = stitch_h * 25.4
	cm = Math.round(cm) / 10
 	stitch_h = Math.round(stitch_h) + " stitches _ high"

	var fab_w  = (w*1) + f
	cm = fab_w * 2.54
	cm = Math.round(cm)
	fab_w = Math.round(fab_w) + " inches (" + cm + "cm) wide"
	var fab_h = (h*1) + f
 	cm = fab_h * 2.54
	cm = Math.round(cm)
 	fab_h = Math.round(fab_h) + " inches (" + cm + "cm) high"
  }

  data.fab_w.value = fab_w
  data.fab_h.value = fab_h

  data.stitch_w.value = stitch_w
  data.stitch_h.value = stitch_h
  
data.sstrands.value= "?"
  if (stitches > 0 ) {data.sstrands.value= 4}
  if (stitches > 10 ) {data.sstrands.value= 3}
  if (stitches > 13 ) {data.sstrands.value= 2}
  if (stitches > 18 ) {data.sstrands.value= 1}

data.bstrands.value= "?"
  if (stitches > 0 ) {data.bstrands.value= 3}
  if (stitches > 10 ) {data.bstrands.value= 2}
  if (stitches > 13 ) {data.bstrands.value= 1}
  if (stitches > 22 ) {data.bstrands.value= 1}
  
}
