// Original script:  alistair@silverlight.co.uk
// Web Site:  http://www.silverlight.co.uk
// Modified by TAAT (c) 2006

// do not copy without written permission!

function GlebiaOstrosci() 
{

    var przyslona = document.getElementById("przyslona").value;
    var ogniskowa = document.getElementById("ogniskowa").value;
    var jednostka = document.getElementById("jednostka").value;

    var odleglosc = document.getElementById("odleglosc").value;
    var KR = document.getElementById("format").value;

    KR = KR*1.0;
    jednostka = jednostka*1.0;
    ogniskowa = ogniskowa*1.0; 
    przyslona = przyslona*1.0;

    var liczOdleglosc = true;
    if (isNaN(odleglosc) || odleglosc <= 0)  
        liczOdleglosc = false;

    var hiper = (ogniskowa * ogniskowa) / (przyslona * KR) + ogniskowa;

    var GOPoczatek = 0;
    var GOKoniec = 0;
    var GOwSumie = 0;
    var GOPoczatekProcent = 0
    var GOKoniecProcent = 0

    if (liczOdleglosc) {
        odleglosc = odleglosc*jednostka*1000;
        GOPoczatek = ((hiper - ogniskowa) * odleglosc) / (hiper + odleglosc - (2*ogniskowa));

        if ((hiper-odleglosc) <= 0.00001) GOKoniec = 10000000;
        else GOKoniec = ((hiper-ogniskowa) * odleglosc) / (hiper-odleglosc);

        GOPoczatekProcent = (odleglosc - GOPoczatek)/(GOKoniec-GOPoczatek) * 100.0;
        GOKoniecProcent = (GOKoniec - odleglosc)/(GOKoniec-GOPoczatek) * 100.0;

        GOPoczatek = GOPoczatek / 1000.0 / jednostka;
        GOKoniec  = GOKoniec / 1000.0 / jednostka;
        GOwSumie = GOKoniec - GOPoczatek;
        odleglosc = odleglosc / 1000.0 / jednostka;
    }		

    hiper = hiper / 1000.0 / jednostka;

    if (jednostka > 0.4)
        znak = " m";
    else if (jednostka < 0.02)
        znak = " cm";
    else if (jednostka < 0.1)
        znak = " in";
    else
        znak = " ft";

    if (hiper < 10.0)
        document.getElementById("hiper").value  = Math.round(hiper*100)/100 + znak;
    else
        document.getElementById("hiper").value  = Math.round(hiper*10)/10 + znak;

    if (liczOdleglosc) {
        if (GOPoczatek < 10.0)
            document.getElementById("GOPoczatek").value = Math.round(GOPoczatek*100)/100 + znak;
        else
            document.getElementById("GOPoczatek").value = Math.round(GOPoczatek*10)/10 + znak;

        if ( GOKoniec < 10000.0) {	
            if (GOKoniec < 10.0)
                document.getElementById("GOKoniec").value  = Math.round(GOKoniec*100)/100 + znak;
            else
                document.getElementById("GOKoniec").value  = Math.round(GOKoniec*10)/10 + znak;

            if (GOwSumie < 10.0) {
                document.getElementById("GOwSumie").value  = Math.round(GOwSumie*100)/100 + znak;
                document.getElementById("GOPrzod").value = Math.round((odleglosc-GOPoczatek)*100)/100 + znak +
                        "\t(" + Math.round(GOPoczatekProcent) + "%)";
                document.getElementById("GOTyl").value = Math.round((GOKoniec - odleglosc)*100)/100 + znak +
                        "\t(" + Math.round(GOKoniecProcent)  +  "%)";
            } else {
                document.getElementById("GOwSumie").value  = Math.round(GOwSumie*10)/10 + znak;
                document.getElementById("GOPrzod").value = Math.round((odleglosc-GOPoczatek)*10)/10 + znak + "\t(" + Math.round(GOPoczatekProcent) + "%)";
                document.getElementById("GOTyl").value = Math.round((GOKoniec - odleglosc)*10)/10 + znak + "\t(" + Math.round(GOKoniecProcent)  +  "%)";
            }
        } else {
            document.getElementById("GOKoniec").value  = "&infin;";
            document.getElementById("GOwSumie").value  = "&infin;";
            document.getElementById("GOPrzod").value = Math.round((odleglosc-GOPoczatek)*10)/10 + znak;
            document.getElementById("GOTyl").value = "&infin;";
        }
    } else {
        document.getElementById("GOwSumie").value = " ";
        document.getElementById("GOTyl").value = " ";
        document.getElementById("GOKoniec").value = " ";
        document.getElementById("GOPoczatek").value = " ";
        document.getElementById("GOPrzod").value = " ";
    }

    document.getElementById("krdwf").value = KR + " mm"
}

function WybierzFormat() {
    KR = document.getElementById("format").value;
    KR = KR * 1.0;
    document.getElementById("krdwf").value = KR;
    GlebiaOstrosci();
}

function ObliczKR(form) {
	referenceKR = (form.referenceKR.options[form.referenceKR.selectedIndex].value);
	equivFocalLength = (form.equivalentFocalLength.options[form.equivalentFocalLength.selectedIndex].value);
	actualFocalLength = (form.actualFocalLength.options[document.form.actualFocalLength.selectedIndex].value);

	actualFocalLength = actualFocalLength*1.0; 
	equivFocalLength = equivFocalLength*1.0;
	referenceKR = referenceKR*1.0;

	digitalKR = actualFocalLength * referenceKR / equivFocalLength;
		
	digitalKR = Math.round(digitalKR*1000)/1000; 
		
	form.digitalKR.value = digitalKR; 
}

