/*----------------------------------------------------------------------------*/
/* Spiral Inductor Calculator                                                 */
/* created: 1/14/2000                                                         */
/* (c) 2000, Rafael Betancourt                                                */
/* betasoft@ieee.org                                                          */
/* Reference: S.S. Mohan, M. Hershenson, S.P. Boyd and T.H. Lee,              */
/*            "Simple Accurate Expressions for Planar Spiral Inductances,"    */
/*            IEEE Journal of Solid-State Circuits, Oct. 1999, pp. 1419-24.   */
/*----------------------------------------------------------------------------*/

var n, s, w, rho, din, dout, davg, delta, err_msg;
var u = 4e-7*Math.PI;

   function testSelect2(form) { 
     Item = form.list.selectedIndex;  
     if (Item != 0) {
        Result = form.list.options[Item].value;
        this.window.location.href = Result;
     }
   } 
   function flipImage(img_name,img_src) {
    document[img_name].src=img_src;
   }
   function round(x, f) {
     return Math.round(x*Math.pow(10,f))/Math.pow(10,f);
   }
   function wheeler(K1, K2) {
     return K1*u*n*n*davg/(1+K2*rho)*1e3;
   }
   function mohan(c1, c2, c3, c4) {
     return u*n*n*davg*c1*0.5*(Math.log(c2/rho)+c3*rho+c4*rho*rho)*1e3;
   }
   function monomial(beta, a1, a2, a3, a4, a5) {
     return beta*Math.pow(dout,a1)*Math.pow(w,a2)*Math.pow(davg,a3)*Math.pow(n,a4)*Math.pow(s,a5);
   }
   function calculate() {
     n = Math.abs(document.spiralcalc.n.value * 1.0);
     w = Math.abs(document.spiralcalc.w.value * 1.0);
     s = Math.abs(document.spiralcalc.s.value * 1.0);

     dout = Math.abs(document.spiralcalc.dout.value * 1.0);
     delta = n*(w+s) - s;
     din  = dout - 2*delta;
     davg = dout - delta;
     rho = (dout - davg) / davg;
     if (din <= 0) {
       err_msg = "There are too many turns for an inductor of this size, din="+round(din,1)+"um.";
       alert(err_msg);
       return;
     }
     document.spiralcalc.Lws.value = round(wheeler(2.34, 2.75),3);
     document.spiralcalc.Lwh.value = round(wheeler(2.33, 3.82),3);
     document.spiralcalc.Lwo.value = round(wheeler(2.25, 3.55),3);

     document.spiralcalc.Lcs.value = round(mohan(1.27,2.07,0.18,0.13),3);
     document.spiralcalc.Lch.value = round(mohan(1.09,2.23,0.00,0.17),3);
     document.spiralcalc.Lco.value = round(mohan(1.07,2.29,0.00,0.19),3);

     document.spiralcalc.Lms.value = round(monomial(1.62e-3,-1.21,-0.147,2.40,1.78,-0.030),3);
     document.spiralcalc.Lmh.value = round(monomial(1.28e-3,-1.24,-0.174,2.47,1.77,-0.049),3);
     document.spiralcalc.Lmo.value = round(monomial(1.33e-3,-1.21,-0.163,2.43,1.75,-0.049),3);
   }
   function initdef() {
     document.spiralcalc.n.value    =   5;
     document.spiralcalc.w.value    =  10; 
     document.spiralcalc.s.value    =   2;
     document.spiralcalc.dout.value = 250;
     calculate();
   }
