﻿$(document).ready(function()
{
    $("#cboLensFormat").addOption("9.6","1 \"");
    $("#cboLensFormat").addOption("6.6","2/3 \"");
    $("#cboLensFormat").addOption("4.8","1/2 \"");
    $("#cboLensFormat").addOption("3.6","1/3 \"");
    $("#cboLensFormat").addOption("2.4","1/4 \"");
    $("#txtDistance").numeric();
    $("#txtDimension").numeric();
    $("#cboLensFormat").selectOptions("9.6");
});


function Calculate()
{
    if($("#txtDistance").val() == "" || $("#txtDimension").val() == "") 
    {
       $("#lblResult").text("0 mm");
       return;
    }
    
    var F, D, h, f;        
    F = Number($("#cboLensFormat").selectedValues()[0]);
    D = Number($("#txtDistance").val());
    h = Number($("#txtDimension").val());
    
    f = F * (D / h);
    if (f.toFixed)
    {
        f = f.toFixed(2);
    }else
    {
        formatNumber(f,2,'','','','','','');
    }
    
    $("#lblResult").text(f +" mm");
}

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) 
{
    var x = Math.round(num * Math.pow(10,dec));
    if (x >= 0) n1=n2='';
    var y = (''+Math.abs(x)).split('');
    var z = y.length - dec; if (z<0) z--; 
    for(var i = z; i < 0; i++) 
    y.unshift('0');y.splice(z, 0, pnt); 
    if(y[0] == pnt) y.unshift('0'); 
    while (z > 3) 
    {z-=3; y.splice(z,0,thou);}
    var r = curr1+n1+y.join('')+n2+curr2;
    return r;
}
