//============================================================+
// PCB Impedance and           || Author:                     |
//  Capacitance Calculator     ||-----------------------------|
//  A JavaScript Program       || Nicola Asuni                |
//                             || Via  Della Pace n.11        |
// ----------------------------|| 09044 Quartucciu (CA)       |
// start          : 2001-01-24 || info@technick.net           |
// last update    : 2009-03-17 || www.technick.net            |
//============================================================+
// (c) Copyright 1999-2001 Tecnick.com s.r.l.

//variables and arrays
var PI=Math.PI;
var CONV1 = Math.pow(10,-12)/0.0254; //Convert [pF/in] to [F/m]
var CONV2 = Math.pow(10,-9)/0.3048; //Convert [ns/ft] to [s/m]

var Z0=0,C0=0,L0=0,TPD=0;

// Round a number ----------------------------------------------------------
function RoundNumber(num, digits)
{
 return( Math.round(num * Math.pow(10,digits))/ Math.pow(10,digits) );
}

// Error message  ----------------------------------------------------------
function InputError()
{
	alert("INPUT ERROR: Please check values");
}

// Check input values
// --------------------------------------------------------------------------
function Check_W(){if(isNaN(document.pcbimpform.W.value)) return(0); return(1);}
function Check_H(){if(isNaN(document.pcbimpform.H.value)) return(0); return(1);}
function Check_H1(){if(isNaN(document.pcbimpform.H1.value)) return(0); return(1);}
function Check_T(){if(isNaN(document.pcbimpform.T.value)) return(0); return(1);}
function Check_C(){if(isNaN(document.pcbimpform.C.value)) return(0); return(1);}
function Check_S(){if(isNaN(document.pcbimpform.S.value)) return(0); return(1);}
function Check_ER(){if(isNaN(document.pcbimpform.ER.value) || (document.pcbimpform.ER.value<1) || (document.pcbimpform.ER.value>15)) return(0); return(1);}
function Check_La(){if(isNaN(document.pcbimpform.La.value)) return(0); return(1);}
function Check_Ca(){if(isNaN(document.pcbimpform.Ca.value)) return(0); return(1);}
function Check_Z0(){if(isNaN(document.pcbimpform.Z0.value)) return(0); return(1);}
function Check_S(){if(isNaN(document.pcbimpform.S.value)) return(0); return(1);}

// Calculate Capacitive Loads Changes --------------------------------------
function CapLoad()
{
  var ZC,TPD1,K;
  var La = 1.0*document.pcbimpform.La.value;
  var Ca = 1.0*document.pcbimpform.Ca.value*Math.pow(10,-12);
	 
  if ( Check_La() && Check_Ca() )  
  {
   K = Math.sqrt(1+(Ca/(C0*La)));
   ZC = Z0/K;
   TPD1 = TPD*K;
	  
   document.pcbimpform.ZC.value=ZC;
   document.pcbimpform.TPD1.value=TPD1;
  }
  else InputError();

 return;
}

// Microstrip ---------------------------------------------------------------
function Microstrip()
{
	Z0=0; C0=0; L0=0; TPD=0;
	
	var W = 1.0*document.pcbimpform.W.value;
	var H = 1.0*document.pcbimpform.H.value;
	var T = 1.0*document.pcbimpform.T.value;
	var ER = 1.0*document.pcbimpform.ER.value;
	
		
	if ( Check_W() && Check_H() && Check_T() && Check_ER() && ((W/H)>0.1) && ((W/H)<3.0) )
	{
	 Z0 = (87.0/Math.sqrt(ER+1.41))*Math.log((5.98*H)/((0.8*W)+T));
	 C0 = ((0.67*(ER+1.41))/(Math.log((5.98*H)/((0.8*W)+T))))*CONV1;
	 L0 = C0*Z0*Z0;
	 TPD = 1.017*Math.sqrt((0.475*ER)+0.67)*CONV2;
	 
	 document.pcbimpform.Z0.value = Z0;
	 document.pcbimpform.C0.value = C0;
	 document.pcbimpform.L0.value = L0;
	 document.pcbimpform.TPD.value = TPD;
	}
	else InputError();
	
	if (document.pcbimpform.cload[1].checked) {CapLoad();}
	
 return;
}

// Embedded Microstrip ---------------------------------------------------------------
function EmbeddedMicrostrip()
{
	Z0=0; C0=0; L0=0; TPD=0;
	
	var W = 1.0*document.pcbimpform.W.value;
	var H = 1.0*document.pcbimpform.H.value;
	var H1 = 1.0*document.pcbimpform.H1.value;
	var T = 1.0*document.pcbimpform.T.value;
	var ER = 1.0*document.pcbimpform.ER.value;
	var ER1 = 0;
		
	if ( Check_W() && Check_H() && Check_H1() && Check_T() && Check_ER() && (H1>(1.2*H)) )
	{
	 
	 ER1 = ER*(1-Math.exp((-1.55*H1)/H));
	 
	 Z0 = (60.0/Math.sqrt(ER1))*Math.log((5.98*H)/((0.8*W)+T));
	 C0 = ((1.41*ER1)/(Math.log((5.98*H)/((0.8*W)+T))))*CONV1;
	 L0 = C0*Z0*Z0;
	 TPD = 0.08475*Math.sqrt(ER1)*CONV2;
	 
	 document.pcbimpform.Z0.value = Z0;
	 document.pcbimpform.C0.value = C0;
	 document.pcbimpform.L0.value = L0;
	 document.pcbimpform.TPD.value = TPD;
	}
	else InputError();
	
	if (document.pcbimpform.cload[1].checked) {CapLoad();}
	
 return;
}

// Stripline ---------------------------------------------------------------
function Stripline()
{
	Z0=0; C0=0; L0=0; TPD=0;
	
	var W = 1.0*document.pcbimpform.W.value;
	var H = 1.0*document.pcbimpform.H.value;
	var T = 1.0*document.pcbimpform.T.value;
	var ER = 1.0*document.pcbimpform.ER.value;
		
	if ( Check_W() && Check_H() && Check_T() && Check_ER() && ((W/H)>0.1) && ((W/H)<2.0) && ((T/H)<0.25) )
	{
	 
	 Z0 = (60.0/Math.sqrt(ER))*Math.log((1.9*((2*H)+T))/((0.8*W)+T));
	 C0 = ((1.41*ER)/(Math.log((3.81*H)/((0.8*W)+T))))*CONV1;
	 L0 = C0*Z0*Z0;
	 TPD = 1.017*Math.sqrt(ER)*CONV2;
	 
	 document.pcbimpform.Z0.value = Z0;
	 document.pcbimpform.C0.value = C0;
	 document.pcbimpform.L0.value = L0;
	 document.pcbimpform.TPD.value = TPD;
	}
	else InputError();
	
	if (document.pcbimpform.cload[1].checked) {CapLoad();}
	
 return;
}

// DualStripline ---------------------------------------------------------------
function DualStripline()
{
	Z0=0; C0=0; L0=0; TPD=0;
	
	var W = 1.0*document.pcbimpform.W.value;
	var H = 1.0*document.pcbimpform.H.value;
	var T = 1.0*document.pcbimpform.T.value;
	var C = 1.0*document.pcbimpform.C.value;
	var ER = 1.0*document.pcbimpform.ER.value;
		
	if ( Check_W() && Check_H() && Check_T() && Check_C() && Check_ER() && ((W/H)>0.1) && ((W/H)<2.0) && ((T/H)<0.25) )
	{
	 
	 Z0 = (80.0/Math.sqrt(ER))*Math.log((1.9*((2*H)+T))/((0.8*W)+T))*(1-(H/(4*(H+C+T))));
	 C0 = ( (2.82*ER)/(Math.log((2*(H-T))/((0.268*W)+(0.335*T)))))*CONV1;
	 L0 = C0*Z0*Z0;
	 TPD = 1.017*Math.sqrt(ER)*CONV2;
	 
	 document.pcbimpform.Z0.value = Z0;
	 document.pcbimpform.C0.value = C0;
	 document.pcbimpform.L0.value = L0;
	 document.pcbimpform.TPD.value = TPD;
	}
	else InputError();
	
	if (document.pcbimpform.cload[1].checked) {CapLoad();}
	
 return;
}

// AsymStripline ---------------------------------------------------------------
function AsymStripline()
{
	Z0=0; C0=0; L0=0; TPD=0;
	
	var W = 1.0*document.pcbimpform.W.value;
	var H = 1.0*document.pcbimpform.H.value;
	var T = 1.0*document.pcbimpform.T.value;
	var H1 = 1.0*document.pcbimpform.H1.value;
	var ER = 1.0*document.pcbimpform.ER.value;
		
	if ( Check_W() && Check_H() && Check_T() && Check_H1() && Check_ER() && ((W/H)>0.1) && ((W/H)<2.0) && ((T/H)<0.25) )
	{
	 
	 Z0 = (80.0/Math.sqrt(ER))*Math.log((1.9*((2*H)+T))/((0.8*W)+T))*(1-(H/(4*(H1))));
	 C0 = ( (2.82*ER)/(Math.log((2*(H-T))/((0.268*W)+(0.335*T)))))*CONV1;
	 L0 = C0*Z0*Z0;
	 TPD = 1.017*Math.sqrt(ER)*CONV2;
	 
	 document.pcbimpform.Z0.value = Z0;
	 document.pcbimpform.C0.value = C0;
	 document.pcbimpform.L0.value = L0;
	 document.pcbimpform.TPD.value = TPD;
	}
	else InputError();
	
	if (document.pcbimpform.cload[1].checked) {CapLoad();}
	
 return;
}

// DiffMicrostrip  ---------------------------------------------------------------
function DiffMicrostrip()
{
	var ZD=0;
	var Z0 = 1.0*document.pcbimpform.Z0.value;
	var H = 1.0*document.pcbimpform.H.value;
	var S = 1.0*document.pcbimpform.S.value;
	
	if ( Check_Z0() && Check_H() && Check_S() )
	{
	 ZD = 2*Z0*(1-(0.48*Math.exp(-0.96*(S/H))));
	 document.pcbimpform.ZD.value = ZD;
	}
	else InputError();

 return;
}

// DiffStripline  ---------------------------------------------------------------
function DiffStripline()
{
	var ZD=0;
	var Z0 = 1.0*document.pcbimpform.Z0.value;
	var H = 1.0*document.pcbimpform.H.value;
	var S = 1.0*document.pcbimpform.S.value;
	
	if ( Check_Z0() && Check_H() && Check_S() )
	{
	 ZD = 2*Z0*(1-(0.347*Math.exp(-2.9*(S/H))));
	 document.pcbimpform.ZD.value = ZD;
	}
	else InputError();

 return;
}
// -------------------------------------------------------------------------
// END OF SCRIPT
// -------------------------------------------------------------------------




