function calc1()
{
        if (!isInteger(document.form1.Temp.value)||document.form1.Temp.value=="")
        {
                alert("The temperature value must be a number");
                document.form1.Temp.focus();
                return;
        }
        if (!isInteger(document.form1.Signal.value)||document.form1.Signal.value=="")
        {
                alert("The signal value must be a number");
                document.form1.Signal.focus();
                return;
        }                
        var Temp=parseFloat(document.form1.Temp.value);
        var Signal=parseFloat(document.form1.Signal.value);

        if(document.form1.temptype.selectedIndex==0)
        {
                Temp+=273;
        }
        if (Temp<500+273)
        {
                alert("The temperature value is too low");
                document.form1.Temp.focus();
                return;
        }
        if (Temp>1760+273)
        {
                alert("The temperature value is too high");
                document.form1.Temp.focus();
                return;
        }
        if (Signal<0)
        {
                alert("The output (mV) is too low");
                document.form1.Signal.focus();
                return;
        }        
        if (Signal>1200)
        {
                alert("The output (mV) is too high");
                document.form1.Signal.focus();
                return;                
        }

        var Atmos=0.209*Math.exp(-46.421*Signal/Temp);
        document.form1.Atmos.value=shrink(Atmos, 0);
}
function calc2()
{
        if (!isInteger(document.form2.Temp.value)||document.form2.Temp.value=="")
        {
                alert("The temperature value must be a number");
                document.form2.Temp.focus();
                return;
        }        
        if (!isInteger(document.form2.Atmos.value)||document.form2.Atmos.value=="")
        {
                alert("The oxygen value must be a number");
                document.form2.Atmos.focus();
                return;
        }
        var oxy=document.form2.Atmos.value+"";
        var Temp=parseFloat(document.form2.Temp.value);
        var Atmos=parseFloat(document.form2.Atmos.value);
        if (oxy.indexOf("^")>="0" && oxy.indexOf("*")>="0")
        {
                Atmos=crunch(oxy);
        }
        if(document.form2.temptype.selectedIndex==0)
        {
                Temp+=273;
        }
        if (Temp<500+273)
        {
                alert("The temperature value is too low");
                document.form2.Temp.focus();
                return;
        }
        if (Temp>1760+273)
        {
                alert("The temperature value is too high");
                document.form2.Temp.focus();
                return;
        }
        if (Atmos<0.000000000000000000000001)
        {
                alert("The oxygen level is too low");
                document.form2.Temp.focus();
                return;
        }
        if (1<Atmos)
        {
                alert("The oxygen level is too high");
                document.form2.Temp.focus();
                return;
        }
        var Signal=0.0496*Temp*(Math.log(0.209/Atmos))*Math.LOG10E;
        document.form2.Signal.value=shrink(Signal, 1);
}
function isInteger(inputVal){
        inputStr = "" + inputVal;
                for (var i=0; i<inputStr.length; i++){
                var oneChar = inputStr.charAt(i);
                if (i==0&&oneChar=="-"){
                        continue
                }
                if ((oneChar<"0"||oneChar>"9")&&oneChar!="."&&oneChar!="^"&&oneChar!="*"&&oneChar!="-"){
                        return false
                }
        }
        return true
}
function shrink(val, formnum)
{
        var valstr=val+"";
        var power=document.forms[formnum].decplaces.value;
        power=parseInt(power);
        if(valstr.indexOf("e")>=0)
        {
                var zeros=valstr.substring(valstr.indexOf("e")+1, valstr.length);
                var number=parseFloat(valstr.substring(0, valstr.length-(zeros.length+2)));
                val=number;
                zeros=parseFloat(zeros);
                var result=(Math.round(val*Math.pow(10, power))/Math.pow(10, power))+"*10^"+zeros;
                return result;
        }
        if(valstr.charAt(valstr.indexOf(".")+1)=="0"&&valstr.charAt(valstr.indexOf(".")-1)=="0")
        {
                var zeros=countzeros((valstr.substring(valstr.indexOf(".")+1, valstr.length)));
                if (zeros>=power&&power!=0&&zeros>2)
                {        
                        var sval=val*Math.pow(10, zeros);
                        var result=(Math.round(sval*Math.pow(10, power))/Math.pow(10, power))+"*10^-"+zeros;
                        return result;
                }
        }
        if(formnum=="0")
        {
                 var result=Math.round(val*Math.pow(10, power+2))/Math.pow(10, power)+"%";
                return result;                
        }
        var result=Math.round(val);
        return result;
}

function countzeros(num)
{
        count=0;
        var bit=0;
        while(num.charAt(bit)=="0")
        {
                count++;
                bit++;
        } 
        return count+1;
}
function crunch(oxy)
{
        var firstnum=parseFloat(oxy.substring(0, oxy.indexOf("*")));
        var secondnum=parseInt(oxy.substring((oxy.indexOf("*")+1), oxy.indexOf("^")));
        var thirdnum=parseInt(oxy.substring((oxy.indexOf("^")+1), oxy.length));
        return parseFloat(firstnum)*Math.pow(parseFloat(secondnum), parseFloat(thirdnum));
}

