function tometric(mvar) {
    if (check_dim(mvar)) {
        return tnFracval = Math.round((Math.floor(mvar.value) + (mvar.value - Math.floor(mvar.value)) * 6.25) * 25.4, 0);
    }
    else
    { return "Error"; }
}

function tostandard(mvar) {
    if (jisnumeric(mvar)) {
        return jfrac(mvar/25.4)
    }
    else
    { return "Error"; }
}

function jisnumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

function jfrac(mobj) {
    var mval = parseInt(mobj, 10) + Math.round((mobj - parseInt(mobj)) / 6.25 * 100) / 100
    if (Math.round((mval - parseInt(mval, 10)) * 100) / 100 >= .16) { mval = parseInt(mobj, 10) + 1 }
    mval = Math.round(mval * 100) / 100
    return mval
}


function jtotal_score() {
    var merror = 0
    if (!numcheck(scores.score1.value)) {
        scores.score1.value = 'Error'
        merror = 1
    }
    if (!numcheck(scores.score2.value)) {
        scores.score2.value = 'Error'
        merror = 1
    }
    if (!numcheck(scores.score3.value)) {
        scores.score3.value = 'Error'
        merror = 1
    }
    if (!numcheck(scores.score4.value)) {
        scores.score4.value = 'Error'
        merror = 1
    }
    if (!numcheck(scores.score5.value)) {
        scores.score5.value = 'Error'
        merror = 1
    }
    if (!numcheck(scores.score6.value)) {
        scores.score6.value = 'Error'
        merror = 1
    }
    if (merror == 1) {
        scores.score_total.value = "Error"
        scores.submitter.disabled = true
        scores.submitter.value = "Error"
    }
    else {
        var mtotal = jfrac(jdec(scores.score1.value) + jdec(scores.score2.value) + jdec(scores.score3.value) + jdec(scores.score4.value) + jdec(scores.score5.value) + jdec(scores.score6.value))
        if (mtotal == scores.total_width.value) {
            scores.score_total.value = mtotal
            scores.submitter.disabled = false
            scores.submitter.value = "Update"
        }
        else {
            scores.score_total.value = mtotal
            scores.submitter.disabled = true
            scores.submitter.value = "Error"
        }
    }
}

function numcheck(ms) {
    if (jisnumeric(ms)) {
        if (ms - parseInt(ms, 10) > 0.155 || (ms < 0.08 && ms > 0) || ms > 200 || (ms > .15 && ms < .99)) {
            return false
        }
    }
    else {
        return false
    }

    return true
}


function jdec(tnFracval) {
    if (tnFracval >= 1)
    { return Math.floor(tnFracval) + (tnFracval - Math.floor(tnFracval)) * 6.25 }
    else
    { return tnFracval * 6.25 }
}

function jround(mval, mdec)
{ return Math.round(mval * 10 ^ mdec) / 10 ^ mdec }

function jint(mval)
{ return parseInt(mval, 10) }

function formatnumber(pnumber, decimals) {
    if (isNaN(pnumber)) { return 0 };
    if (pnumber == '') { return 0 };
    var snum = new String(pnumber);
    var sec = snum.split('.');
    var whole = parseFloat(sec[0]);
    var result = '';

    if (sec.length > 1) {
        var dec = new String(sec[1]);
        dec = String(parseFloat(sec[1]) / Math.pow(10, (dec.length - decimals)));
        dec = String(whole + Math.round(parseFloat(dec)) / Math.pow(10, decimals));
        var dot = dec.indexOf('.');
        if (dot == -1) {
            dec += '.';
            dot = dec.indexOf('.');
        }
        while (dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    } else {
        var dot;
        var dec = new String(whole);
        dec += '.';
        dot = dec.indexOf('.');
        while (dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    }
    if (decimals == 0) { result = Math.floor(pnumber); }

    return result;
}

function check_dim(mval) {

    if (!jisnumeric(mval.value))
    { mval.value = 0; return false; }

    if (mval.value * 100 - parseInt(mval.value) * 100 > 15.001)
    { mval.value = 0; return false; }

    else {
        var str = mval.value;
        if (str.substr(0, 1) == '.')
            { mval.value = '0' + mval.value; }
        
        mval.value = formatnumber(mval.value, 2);
        return true; }

}


function check_dim2(mval) {

    if (!jisnumeric(mval.value))
    {return false;}

    if (mval.value * 100 - parseInt(mval.value) * 100 > 15.001)
    {return false;}

    else {return true;}

}

function inkvisible(mval) {
    if (mval > 1)
    { return "inline"; }
    else
    { return "none"; }
}
