Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     var volts=null;
  2.     var amps=null;
  3.     var ohms=null;
  4.     var watts=null;
  5.     var calcVolts=false;
  6.     var calcAmps=false;
  7.     var calcOhms=true;
  8.     var calcWatts=true;
  9.    
  10.     function readVolts(inedit)
  11.     {
  12.         val = $('#iVoltage').val();
  13.    
  14.         if (val=="" || val==null)
  15.         {
  16.             volts=null;
  17.         }
  18.         else if ($.isNumeric(val))
  19.         {
  20.             volts=Number(val);
  21.         }
  22.         else if (!inedit)
  23.         {
  24.             $('#iVoltage').val(volts);
  25.         }
  26.    
  27.        updateForm();
  28.     }
  29.  
  30.     function readAmps(inedit)
  31.     {
  32.         var val=$('#iCurrent').val();
  33.         var scale=$('#sCurrentUnit').val();
  34.         if (val=="" || val==null)
  35.         {
  36.             amps=null;
  37.         }
  38.         else if ($.isNumeric(val))
  39.         {
  40.             amps=Number(val)*Number(scale);
  41.         }
  42.         else if (!inedit)
  43.         {
  44.             $('#iCurrent').val(amps/scale);
  45.         }
  46.    
  47.        updateForm();
  48.     }
  49.  
  50.     function readOhms(inedit)
  51.     {
  52.         var val=$('#iResistance').val();
  53.         var scale=$('#sResistanceUnit').val();
  54.         if (val=="" || val==null)
  55.         {
  56.             ohms=null;
  57.         }
  58.         else if ($.isNumeric(val))
  59.         {
  60.             ohms=Number(val)*Number(scale);
  61.         }
  62.         else if (!inedit)
  63.         {
  64.             $('#iResistance').val(ohms/scale);
  65.         }
  66.    
  67.        updateForm();
  68.     }
  69.    
  70.     function readWatts(inedit)
  71.     {
  72.         val = $('#iPower').val();
  73.    
  74.         if (val=="" || val==null)
  75.         {
  76.             watts=null;
  77.         }
  78.         else if ($.isNumeric(val))
  79.         {
  80.             watts=Number(val);
  81.         }
  82.         else if (!inedit)
  83.         {
  84.             $('#iPower').val(watts);
  85.         }
  86.    
  87.        updateForm();
  88.     }
  89.    
  90.     function updateForm()
  91.     {
  92.         numFields=0;
  93.    
  94.         if (volts>0)
  95.             numFields++;
  96.         if (amps>0)
  97.             numFields++;
  98.         if (ohms>0)
  99.             numFields++;
  100.         if (watts>0)
  101.             numFields++;
  102.    
  103.         if (numFields>=2)
  104.         {
  105.             $('#btnCalc').removeAttr('disabled');
  106.         }
  107.         else
  108.         {
  109.             $('#btnCalc').attr('disabled','disabled');
  110.         }
  111.    
  112.         if (numFields==2 || numFields==3)
  113.         {
  114.             if (volts>0)
  115.             {
  116.                 calcVolts=false;
  117.                 $('#iVoltage').removeClass('to-calc');
  118.             }
  119.             else
  120.             {
  121.                 calcVolts=true;
  122.                 $('#iVoltage').addClass('to-calc');
  123.             }
  124.             if (amps>0)
  125.             {
  126.                 calcAmps=false;
  127.                 $('#iCurrent').removeClass('to-calc');
  128.             }
  129.             else
  130.             {
  131.                 calcAmps=true;
  132.                 $('#iCurrent').addClass('to-calc');
  133.             }
  134.             if (ohms>0)
  135.             {
  136.                 calcOhms=false;
  137.                 $('#iResistance').removeClass('to-calc');
  138.             }
  139.             else
  140.             {
  141.                 calcOhms=true;
  142.                 $('#iResistance').addClass('to-calc');
  143.             }
  144.             if (watts>0)
  145.             {
  146.                 calcWatts=false;
  147.                 $('#iPower').removeClass('to-calc');
  148.             }
  149.             else
  150.             {
  151.                 calcWatts=true;
  152.                 $('#iPower').addClass('to-calc');
  153.             }
  154.         }
  155.     }
  156.    
  157.     function calc()
  158.     {
  159.         updateForm();
  160.    
  161.         $('#iVoltage').parent().parent().removeClass('has-error');
  162.         $('#iCurrent').parent().parent().removeClass('has-error');
  163.         $('#iResistance').parent().parent().removeClass('has-error');
  164.         $('#iPower').parent().parent().removeClass('has-error');
  165.         $('#iVoltage').attr('title','');
  166.         $('#iCurrent').attr('title','');
  167.         $('#iResistance').attr('title','');
  168.         $('#iPower').attr('title','');
  169.    
  170.         if (calcVolts && calcAmps)
  171.         {
  172.             amps  = Math.sqrt((watts/ohms),2);
  173.             volts = amps*ohms;
  174.         }
  175.         else if (calcVolts && calcOhms)
  176.         {
  177.             volts = watts/amps;
  178.             ohms  = volts/amps;
  179.         }
  180.         else if (calcVolts && calcWatts)
  181.         {
  182.             volts = amps*ohms;
  183.             watts = amps*amps*ohms;
  184.         }
  185.         else if (calcAmps && calcOhms)
  186.         {
  187.             amps  = watts/volts;
  188.             ohms  = volts/amps;
  189.         }
  190.         else if (calcAmps && calcWatts)
  191.         {
  192.             amps  = volts/ohms;
  193.             watts = volts*amps;
  194.         }
  195.         else if (calcOhms && calcWatts)
  196.         {
  197.             ohms  = volts/amps;
  198.             watts = volts*amps;
  199.         }
  200.         else if (calcVolts)
  201.         {
  202.             volts = amps*ohms;
  203.             testWatts = volts*amps;
  204.             if (Math.abs((testWatts-watts)/testWatts)>0.0001)
  205.             {
  206.                 $("#iPower").parent().parent().addClass("has-error");
  207.                 $("#iPower").attr('data-original-title','Expected '+testWatts);
  208.             }
  209.         }
  210.         else if (calcAmps)
  211.         {
  212.             amps  = volts/ohms;
  213.             testWatts = volts*amps;
  214.             if (Math.abs((testWatts-watts)/testWatts)>0.0001)
  215.             {
  216.                 $("#iPower").parent().parent().addClass("has-error");
  217.                 $("#iPower").attr('data-original-title','Expected '+testWatts);
  218.             }
  219.         }
  220.         else if (calcOhms)
  221.         {
  222.             ohms  = volts/amps;
  223.             testWatts = volts*amps;
  224.             if (Math.abs((testWatts-watts)/testWatts)>0.0001)
  225.             {
  226.                 $("#iPower").parent().parent().addClass("has-error");
  227.                 $("#iPower").attr('data-original-title','Expected '+testWatts);
  228.             }
  229.         }
  230.         else if (calcWatts)
  231.         {
  232.             watts = volts*amps;
  233.             testOhms = volts/amps;
  234.             if (Math.abs((testOhms-ohms)/testOhms)>0.0001)
  235.             {
  236.                 $("#iResistance").parent().parent().addClass("has-error");
  237.                 $("#iResistance").attr('data-original-title','Expected '+testOhms);
  238.             }
  239.         }
  240.    
  241.         volts = Math.round (volts*100000);
  242.         volts = volts/100000;
  243.         amps = Math.round(amps*100000);
  244.         amps = amps/100000;
  245.         ohms = Math.round(ohms*100000);
  246.         ohms = ohms/100000;
  247.         watts = Math.round(watts*100000);
  248.         watts = watts/100000;
  249.    
  250.         $('#iVoltage').val(volts);  
  251.         $('#iCurrent').val(amps/$('#sCurrentUnit').val());
  252.         $('#iResistance').val(ohms/$('#sResistanceUnit').val());
  253.         $('#iPower').val(watts);
  254.     }
  255.  
  256.     $(document).ready(function() {
  257.         $('#iVoltage').change(function () {
  258.             readVolts(false);});
  259.         $('#iVoltage').keyup(function () {
  260.             readVolts(true);});
  261.             $('#iCurrent').change(readAmps);
  262.  
  263.         $('#iCurrent').change(function () {
  264.             readAmps(false);});
  265.         $('#iCurrent').keyup(function () {
  266.             readAmps(true);});
  267.    
  268.         $('#sCurrentUnit').change(function () {
  269.             readAmps(false);});
  270.    
  271.         $('#iResistance').change(function () {
  272.             readOhms(false);});
  273.         $('#iResistance').keyup(function () {
  274.             readOhms(true);});
  275.    
  276.         $('#sResistanceUnit').change(function () {
  277.             readOhms(false);});
  278.  
  279.         $('#iPower').change(function () {
  280.             readWatts(false);});
  281.         $('#iPower').keyup(function () {
  282.             readWatts(true);});
  283.  
  284.         $('input').not('#search input').keyup(function(e) {
  285.             var focusables = $("input:focusable").not('#search input');      
  286.             if (e.keyCode == 40) {
  287.                 // down key
  288.                 var current = focusables.index(this);
  289.                 next = focusables.eq(current+1).length ? focusables.eq(current+1) : focusables.eq(0);
  290.                 next.focus();
  291.             } else if (e.keyCode == 38) {
  292.                 // up key
  293.                 var current = focusables.index(this),
  294.                 prev = focusables.eq(current-1).length ? focusables.eq(current-1) : focusables.eq(focusables.length-1);
  295.                 prev.focus();
  296.             }
  297.         });
  298.        
  299.         $('#btnCalc').click(calc);
  300.         $('input').keypress(function (e) {
  301.             if (e.which == 13 && $('#btnCalc').attr('disabled')==undefined) {
  302.                 console.log("calrc");
  303.                 calc();
  304.             }
  305.         });
  306.    
  307.         $('[data-toggle="tooltip"]').tooltip({ container: 'body'});
  308.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement