﻿function clrAll() {
    document.getElementById("suv").value = "";
    document.getElementById("kv").innerHTML = "";
    document.getElementById("txtAlert").innerHTML;
}
function FormatNumber(srcStr, nAfterDot) {
    var srcStr,
    nAfterDot;
    var resultStr,
    nTen;
    srcStr = "" + srcStr + "";
    strLen = srcStr.length;
    dotPos = srcStr.indexOf(".", 0);
    if (dotPos == -1) {
        resultStr = srcStr + ".";
        for (i = 0; i < nAfterDot; i++) {
            resultStr = resultStr + "0";
        }　
        return resultStr;
    } else {
        if ((strLen - dotPos - 1) >= nAfterDot) {
            nAfter = dotPos + nAfterDot + 1;
            nTen = 1;
            for (j = 0; j < nAfterDot; j++) {
                nTen = nTen * 10;
            }
            resultStr = Math.round(parseFloat(srcStr) * nTen) / nTen;
            return resultStr;
        } else {
            resultStr = srcStr;
            for (i = 0; i < (nAfterDot - strLen + dotPos + 1); i++) {
                resultStr = resultStr + "0";
            }
            return resultStr;
        }
    }
}
function Altertemp() {
    var unit_t;
    var t,
    t2,
    vaT;
    vaT = valiTemp();
    if (vaT == true) {
        Initemp();
    }
}
function Initemp() {
    t = parseFloat(document.getElementById("temp").value);
    unit_t = document.getElementById("Select1").value;
    if (unit_t == "F") {
        t2 = 5 / 9 * (t - 32);
        t2 = FormatNumber(t2, 1);
        document.getElementById("temp2").innerHTML = "(" + t2 + "°C)";
    } else {
        t2 = (9 / 5 * t) + 32;
        t2 = FormatNumber(t2, 1);
        document.getElementById("temp2").innerHTML = "(" + t2 + "°F)";
    }
}
function valiTemp() {
    var t;
    var unit_t;
    var txtSUV;
    document.getElementById("temp").value = document.getElementById("temp").value.replace(/,/, ".");
    document.getElementById("suv").value = document.getElementById("suv").value.replace(/,/, ".");
    if (document.getElementById("temp").value == "" || isNaN(document.getElementById("temp").value)) {
        document.getElementById("txtAlert").innerHTML = "Note, input temperature must be a number!";
        document.getElementById("temp").focus();
        document.getElementById("temp").select();
        return (false);
    }
    t = parseFloat(document.getElementById("temp").value);
    unit_t = document.getElementById("Select1").value;
    if (unit_t == "F") {
        t = parseFloat(document.getElementById("temp").value);
    } else {
        t = parseFloat(document.getElementById("temp").value);
        t = (9 / 5 * t) + 32;
    }
    if ((t < 0) || (t > 350)) {
        document.getElementById("txtAlert").innerHTML = "Note, this calcualtion applies to the temperature range 0 to 350 Fahrenheit. The input temperature is out of the range.";
        document.getElementById("temp").focus();
        document.getElementById("temp").select();
        return (false);
    }
    document.getElementById("txtAlert").innerHTML = "";
    return true;
}
function valiSUV() {
    var txtSUV;
    if (document.getElementById("suv").value == "" || isNaN(document.getElementById("suv").value)) {
        document.getElementById("txtAlert").innerHTML = "Note, input SUV must be a number!";
        document.getElementById("suv").focus();
        document.getElementById("suv").select();
        return (false);
    }
    txtSUV = document.getElementById("suv").value;
    if ((txtSUV < 32) || (txtSUV > 10000)) {
        document.getElementById("txtAlert").innerHTML = "Note, this calcualtion applies to the SUV range 32 to 10000 SUS. The input SUV is out of the range.";
        document.getElementById("suv").focus();
        document.getElementById("suv").select();
        return (false);
    }
    return true;
}
function valiAll() {
    var vT,
    vS;
    vT = valiTemp();
    vS = valiSUV();
    if ((vT == true) && (vS == true)) {
        document.getElementById("txtAlert").innerHTML = "";
        return true;
    } else {
        return false;
    }
}
function calSUS(txtkv) {
    var unit_t;
    var U100,
    Ut,
    B;
    unit_t = document.getElementById("Select1").value;
    if (unit_t == "F") {
        t = parseFloat(document.getElementById("temp").value);
    } else {
        t = parseFloat(document.getElementById("temp").value);
        t = (9 / 5 * t) + 32;
    }
    if ((t < 0) || (t > 350)) {
        document.getElementById("txtAlert").innerHTML = "Note, this calcualtion applies to the temperature range 0 to 350 Fahrenheit. The input temperature is out of the range.";
        return (false);
    }
    if (txtkv <= 499.5) {
        U100 = 4.6324 * txtkv + (1.00 + 0.03264 * txtkv) * 100000 / (3930.2 + 262.7 * txtkv + 23.97 * txtkv * txtkv + 1.646 * txtkv * txtkv * txtkv);
        Ut = (1.0 + 0.000061 * (t - 100)) * U100;
    } else {
        if ((t >= 0) && (t < 30)) {
            B = 4.634 + 0.0003 * (t - 100);
        }
        if ((t >= 30) && (t < 40)) {
            B = 4.627 + 0.0002 * (t - 100);
        }
        if ((t >= 40) && (t < 90)) {
            B = 4.633 + 0.0003 * (t - 100);
        }
        if ((t >= 90) && (t < 100)) {
            B = 4.632 + 0.0002 * (t - 100);
        }
        if ((t >= 100) && (t < 150)) {
            B = 4.632 + 0.0003 * (t - 100);
        }
        if ((t >= 150) && (t < 160)) {
            B = 4.637 + 0.0002 * (t - 100);
        }
        if ((t >= 160) && (t < 210)) {
            B = 4.631 + 0.0003 * (t - 100);
        }
        if ((t >= 210) && (t < 220)) {
            B = 4.642 + 0.0002 * (t - 100);
        }
        if ((t >= 220) && (t < 260)) {
            B = 4.63 + 0.0003 * (t - 100);
        }
        if ((t >= 260) && (t < 270)) {
            B = 4.646 + 0.0002 * (t - 100);
        }
        if ((t >= 270) && (t < 320)) {
            B = 4.629 + 0.0003 * (t - 100);
        }
        if ((t >= 320) && (t < 330)) {
            B = 4.651 + 0.0002 * (t - 100);
        }
        if ((t >= 330) && (t < 350)) {
            B = 4.628 + 0.0003 * (t - 100);
        }
        Ut = txtkv * B;
    }
    return Ut;
}
function calKV() {
    var txtSUV,
    kv,
    Tol,
    maxTol,
    maxKV;
    var calSUV;
    var step,
    vaT;
    vaT = valiAll();
    if (vaT == false) {
        return false;
    }
    txtSUV = parseFloat(document.getElementById("suv").value);
    Tol = 1;
    if (txtSUV < 100) {
        step = 0.005;
        kv = 1.790;
        maxTol = 0.001;
        maxKV = 20.6;
    } else if (txtSUV >= 100 && txtSUV < 200) {
        step = 0.002;
        kv = 20.5;
        maxTol = 0.0001;
        maxKV = 44;
    } else if (txtSUV >= 200 && txtSUV < 300) {
        step = 0.01;
        kv = 42.8;
        maxTol = 0.0001;
        maxKV = 64.8;
    } else if (txtSUV >= 300 && txtSUV < 400) {
        step = 0.01;
        kv = 64.5;
        maxTol = 0.0001;
        maxKV = 87;
    } else if (txtSUV >= 400 && txtSUV < 500) {
        step = 0.01;
        kv = 86;
        maxTol = 0.0001;
        maxKV = 108;
    } else if (txtSUV >= 500 && txtSUV < 600) {
        step = 0.01;
        kv = 107.6;
        maxTol = 0.0001;
        maxKV = 130;
    } else if (txtSUV >= 600 && txtSUV < 700) {
        step = 0.01;
        kv = 129.2;
        maxTol = 0.0001;
        maxKV = 152;
    } else if (txtSUV >= 700 && txtSUV < 800) {
        step = 0.01;
        kv = 150.8;
        maxTol = 0.0001;
        maxKV = 175;
    } else if (txtSUV >= 800 && txtSUV < 900) {
        step = 0.01;
        kv = 172.4;
        maxTol = 0.0001;
        maxKV = 196;
    } else if (txtSUV >= 900 && txtSUV < 1000) {
        step = 0.01;
        kv = 194;
        maxTol = 0.0001;
        maxKV = 216;
    } else if (txtSUV >= 1000 && txtSUV < 1100) {
        step = 0.01;
        kv = 215.5;
        maxTol = 0.0001;
        maxKV = 238;
    } else if (txtSUV >= 1100 && txtSUV < 1200) {
        step = 0.01;
        kv = 237;
        maxTol = 0.0001;
        maxKV = 260;
    } else if (txtSUV >= 1200 && txtSUV < 1300) {
        step = 0.01;
        kv = 258.5;
        maxTol = 0.0001;
        maxKV = 282;
    } else if (txtSUV >= 1300 && txtSUV < 1400) {
        step = 0.01;
        kv = 280.5;
        maxTol = 0.0001;
        maxKV = 304;
    } else if (txtSUV >= 1400 && txtSUV < 1500) {
        step = 0.01;
        kv = 302;
        maxTol = 0.0001;
        maxKV = 325;
    } else if (txtSUV >= 1500 && txtSUV < 1600) {
        step = 0.01;
        kv = 323.5;
        maxTol = 0.0001;
        maxKV = 347;
    } else if (txtSUV >= 1600 && txtSUV < 1700) {
        step = 0.01;
        kv = 345;
        maxTol = 0.0001;
        maxKV = 368;
    } else if (txtSUV >= 1700 && txtSUV < 1800) {
        step = 0.01;
        kv = 366.5;
        maxTol = 0.0001;
        maxKV = 390;
    } else if (txtSUV >= 1800 && txtSUV < 1900) {
        step = 0.01;
        kv = 388;
        maxTol = 0.0001;
        maxKV = 412;
    } else if (txtSUV >= 1900 && txtSUV < 2000) {
        step = 0.01;
        kv = 410;
        maxTol = 0.0001;
        maxKV = 433;
    } else if (txtSUV >= 2000 && txtSUV < 2100) {
        step = 0.01;
        kv = 431.5;
        maxTol = 0.0001;
        maxKV = 455;
    } else if (txtSUV >= 2100 && txtSUV < 2200) {
        step = 0.01;
        kv = 453;
        maxTol = 0.0001;
        maxKV = 478;
    } else if (txtSUV >= 2200 && txtSUV <= 2314) {
        step = 0.01;
        kv = 474.5;
        maxTol = 0.0001;
        maxKV = 501;
    } else if (txtSUV > 2314) {
        step = 0.1;
        kv = 499.5;
        maxTol = 0.0001;
        maxKV = 10000;
    }
    while (Tol > maxTol) {
        
        calSUV = calSUS(kv);
        
        if (calSUV>txtSUV) {
            kv = kv - step;
        
        }
        else if (calSUV<txtSUV) {
             kv = kv + step;
        }
       
        
        Tol = Math.abs((calSUV - txtSUV) / txtSUV);
        if (kv > maxKV) {
            break;
        }
    }
    if (kv < 50) {
        kv = FormatNumber(kv, 2);
    } else if ((kv >= 50) && (kv < 500)) {
        kv = FormatNumber(kv, 1);
    } else {
        kv = FormatNumber(kv, 0);
    }
    document.getElementById("kv").innerHTML = kv;
}
function writeJS() {
    var str = '';
    str += '<ul >';
    str += '<li><a href="Angle.htm">Angle<\/a><\/li>';
    str += '<li><a href="Area.htm">Area<\/a><\/li>';
    str += '<li><a href="Density.htm">Density<\/a><\/li>';
    str += '<li><a href="..\/unitguide\/Electricity.htm">Electricity<\/a><\/li>';
    str += '<li><a href="Energy.htm">Energy<\/a><\/li>';
    str += '<li><a href="FlowRate.htm">Flow Rate<\/a><\/li>';
    str += '<li><a href="Force.htm">Force<\/a><\/li>';
    str += '<li><a href="FuelEconomy.htm">Fuel Economy<\/a><\/li>';
    str += '<li><a href="..\/unitguide\/Heat.htm">Heat<\/a><\/li>';
    str += '<li><a href="Length.htm">Length<\/a><\/li>';
    str += '<li><a href="..\/unitguide\/Light.htm">Light<\/a><\/li>';
    str += '<li><a href="..\/unitguide\/Magnetism.htm">Magnetism<\/a><\/li>';
    str += '<li><a href="Mass.htm">Mass<\/a><\/li>';
    str += '<li><a href="Pressure.htm">Pressure<\/a><\/li>';
    str += '<li><a href="Power.htm">Power<\/a><\/li>';
    str += '<li><a href="..\/unitguide\/Radiation.htm">Radiation<\/a><\/li>';
    str += '<li><a href="Temperature.htm">Temerature<\/a><\/li>';
    str += '<li><a href="Time.htm">Time<\/a><\/li>';
    str += '<li><a href="Torque.htm">Torque<\/a><\/li>';
    str += '<li><a href="Velocity.htm">Velocity<\/a> <\/li> ';
    str += '<li><a href="Volume.htm">Volume<\/a><\/li>';
    str += '<li><a href="..\/unitguide\/Viscosity.htm">Viscosity<\/a><\/li>';
    str += '<\/ul>';
    document.write(str);
}
function bookmark() {
    var title = document.title;
    var url = document.location.href;
    if (window.sidebar) window.sidebar.addPanel(title, url, "");
    else if (window.opera && window.print) {
        var mbm = document.createElement('a');
        mbm.setAttribute('rel', 'sidebar');
        mbm.setAttribute('href', url);
        mbm.setAttribute('title', title);
        mbm.click();
    } else if (window.external) {
        try {
            window.external.AddFavorite;
        } catch(e) {
            if (e == "[object Error]") {
                alert("You have to press CTRL-D to add this page to your favorite");
            } else {
                window.external.AddFavorite;
            }
        }
    }
}
function bookmarks() {
    var urlstr = location.href;
    var tt = document.title;
    var a1;
    a1 = tt.indexOf("www");
    tt = tt.substring(0, a1);
    var str = '';
    str += '<a href="javascript:bookmark()">';
    str += '<img alt="Add to Favorites" src="..\/..\/images\/favorites.png" border=0><\/a>';
    str += '<a href="http:\/\/del.icio.us\/post?url=' + urlstr + '&title=' + tt + '">';
    str += '<img alt="del.icio.us" src="..\/..\/images\/delicious.png" border=0><\/a>';
    str += '<a href="http:\/\/digg.com\/submit?phase=2&url=' + urlstr + '&title=' + tt + '">';
    str += '<img alt="digg" src="..\/..\/images\/digg.gif" border=0><\/a>';
    str += '<a href="http:\/\/reddit.com\/submit?url=' + urlstr + '&title=' + tt + '">';
    str += '<img alt="Reddit" src="..\/..\/images\/reddit.gif" border=0><\/a>';
    str += '<a href="http:\/\/www.furl.net\/storeIt.jsp?t=' + tt + '&u=' + urlstr + '">';
    str += '<img alt="furl" src="..\/..\/images\/furl.gif" border=0><\/a>';
    str += '<a href="http:\/\/www.stumbleupon.com\/submit?url=' + urlstr + '&title=' + tt + '">';
    str += '<img alt="Stumbleupon" src="..\/..\/images\/su.png" border=0><\/a>';
    str += '<a href="http:\/\/myweb2.search.yahoo.com\/myresults\/bookmarklet?t=' + tt + '&u=' + urlstr + '">';
    str += '<img alt="Yahoo MyWeb" src="..\/..\/images\/yahoo-myweb.png" border=0><\/a>';
    str += '<a href="http:\/\/www.google.com\/bookmarks\/mark?op=edit&bkmk=' + urlstr + '&title=' + tt + '">';
    str += '<img alt="Google Bookmarks" src="..\/..\/images\/gg.gif" border=0><\/a>';
    document.write(str);
}
function NiftyCheck() {
    if (!document.getElementById || !document.createElement) return (false);
    var b = navigator.userAgent.toLowerCase();
    if (b.indexOf("msie 5") > 0 && b.indexOf("opera") == -1) return (false);
    return (true);
}
function Rounded(selector, bk, color, size) {
    var i;
    var v = getElementsBySelector(selector);
    var l = v.length;
    for (i = 0; i < l; i++) {
        AddTop(v[i], bk, color, size);
        AddBottom(v[i], bk, color, size);
    }
}
function RoundedTop(selector, bk, color, size) {
    var i;
    var v = getElementsBySelector(selector);
    for (i = 0; i < v.length; i++) AddTop(v[i], bk, color, size);
}
function RoundedBottom(selector, bk, color, size) {
    var i;
    var v = getElementsBySelector(selector);
    for (i = 0; i < v.length; i++) AddBottom(v[i], bk, color, size);
}
function AddTop(el, bk, color, size) {
    var i;
    var d = document.createElement("b");
    var cn = "r";
    var lim = 4;
    if (size && size == "small") {
        cn = "rs";
        lim = 2
    }
    d.className = "rtop";
    d.style.backgroundColor = bk;
    for (i = 1; i <= lim; i++) {
        var x = document.createElement("b");
        x.className = cn + i;
        x.style.backgroundColor = color;
        d.appendChild(x);
    }
    el.insertBefore(d, el.firstChild);
}
function AddBottom(el, bk, color, size) {
    var i;
    var d = document.createElement("b");
    var cn = "r";
    var lim = 4;
    if (size && size == "small") {
        cn = "rs";
        lim = 2
    }
    d.className = "rbottom";
    d.style.backgroundColor = bk;
    for (i = lim; i > 0; i--) {
        var x = document.createElement("b");
        x.className = cn + i;
        x.style.backgroundColor = color;
        d.appendChild(x);
    }
    el.appendChild(d, el.firstChild);
}
function getElementsBySelector(selector) {
    var i;
    var s = [];
    var selid = "";
    var selclass = "";
    var tag = selector;
    var objlist = [];
    if (selector.indexOf(" ") > 0) {
        s = selector.split(" ");
        var fs = s[0].split("#");
        if (fs.length == 1) return (objlist);
        return (document.getElementById(fs[1]).getElementsByTagName(s[1]));
    }
    if (selector.indexOf("#") > 0) {
        s = selector.split("#");
        tag = s[0];
        selid = s[1];
    }
    if (selid != "") {
        objlist.push(document.getElementById(selid));
        return (objlist);
    }
    if (selector.indexOf(".") > 0) {
        s = selector.split(".");
        tag = s[0];
        selclass = s[1];
    }
    var v = document.getElementsByTagName(tag);
    if (selclass == "") return (v);
    for (i = 0; i < v.length; i++) {
        if (v[i].className == selclass) {
            objlist.push(v[i]);
        }
    }
    return (objlist);
}
