// Cross Stitch Calculator
// copyright 11th March 2007, by Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration
function stripBlanks(fld) {var result = "";var c = 0;for (i=0; i < fld.length; i++) {if (fld.charAt(i) != " " || c > 0) {result += fld.charAt(i); if (fld.charAt(i) != " ") c = result.length;}} return result.substr(0,c);}
function valButton(btn) {var cnt = -1;for (var i=btn.length-1; i > -1; i--) {if (btn[i].checked) {cnt = i; i = -1;}} if (cnt > -1) return btn[cnt].value; else return null;}
function calc(thisform) {var ewt = stripBlanks(thisform.ew.value); var ew = parseInt(ewt,10); if (ew != ewt || ew <= 0) {alert('invalid width'); thisform.ew.focus(); return;} var eht = stripBlanks(thisform.eh.value); var eh = parseInt(eht,10); if (eh != eht || eh <= 0) {alert('invalid height'); thisform.eh.focus(); return;} var tht = stripBlanks(thisform.th.value); var th = parseFloat(tht,10); if (th != tht || th <= 0) {alert('invalid fabric count'); thisform.th.focus(); return;} var ms = valButton(thisform.ms); if (ms == null) {alert('inches or cm?'); return;} ew = ew / th * ms; eh = eh / th * ms; thisform.cw.value = Math.floor(ew*100)/100; thisform.ch.value = Math.floor(eh*100)/100; ew /= 2.54; thisform.iw.value = Math.floor(ew*1000)/1000; eh /= 2.54; thisform.ih.value = Math.floor(eh*1000)/1000;}
