/* Plasma Parameters Widget 
	Version 3.1
	Copyright 2006 by David Pace ( http://www.davidpace.com )
	This widget is provided for free use by all parties.  There are no warranties on this software and 
	all users do so at their own risk.  The homepage of this widget may be found at:
	http://www.davidpace.com/new2/physics/ph.d./plasma_widget/   */


/* load default values when widget opens */
function loaded() 
{
	document.inputs.ai.value = 1.0;	
	document.inputs.cl.value = 12.0;
}

/* prevents Enter key from erasing fields
taken from: http://www.cs.tut.fi/~jkorpela/forms/enter.html */
function noenter() {
	
	if ( window.event.keyCode == 13 )
	{
		calculate( document.inputs.Bo, document.inputs.mu, document.inputs.Z, document.inputs.cl, document.inputs.n_e, document.inputs.te, document.inputs.ti, document.inputs.ai );
		return !(" ");
	}
  	
}

function calculate( Bo, mu, Z, cl, n_e, te, ti, ai )
{
	var	n_e = n_e.value;
	var	n_i = n_e / Z.value;
	var	Bo = Bo.value;
	var	mu = mu.value;
	var	Z = Z.value;
	var	cl = cl.value;
	var	te = te.value;
	var	ti = ti.value;
	var gam = ai.value

	var total_t = te * 1.0 + 1.0 * ti;  // total temperature
	var n_total = n_e * 1.0 + 1.0 * n_i;  // total density
  	var	k = 1.3807e-16;  // Boltzmann's constant (erg/K)
  	var c = 3.0e+8;  // speed of light (m/s)
  
	var	fce = 2.8e+6 * Bo;  // electron cyclotron frequency (Hz)
	
	var	fci = 1.52e+3 * Bo * Z / mu;   // ion cyclotron frequency (Hz) 

	var	fpe = 8.98e+3 * Math.sqrt( n_e );   // electron plasma frequency (Hz)

	var	fpi = 2.1e+2 * Z * Math.sqrt( n_i ) / Math.sqrt( mu );  // ion plasma frequency (Hz)

	var	nu_e = 2.91e-6 * n_e * cl / Math.pow( te, 1.5 );  // electron collision frequency (Hz)

	var	nu_i = 4.8e-8 * Math.pow( Z, 4 ) * n_i * cl / ( Math.sqrt( mu ) * Math.pow( ti, 1.5 ) );  // ion collision frequency (Hz)

	var	re = 2.38 * Math.sqrt( te ) / Bo / 100.;  // electron gyroradius (m)

    var	ri = 1.02e+2 * Math.sqrt( mu * ti ) / ( Z * Bo ) / 100.;  // ion gyroradius (m)
	
    var	psd = 5.31e+5 / Math.sqrt( n_e ) / 100.;  // plasma skin depth (m)	
	
	var	ld = 7.43e+2 * Math.sqrt( total_t ) / Math.sqrt( n_e ) / 100.;   // Debye length (m)

	var	vte = 4.19e+7 * Math.sqrt( te ) / 100.;  // electron thermal velocity (m/s)

    var	vti = 9.79e+5 * Math.sqrt( ti / mu ) / 100.;  // ion thermal velocity (m/s)

	var	cs = 9.79e+5 * Math.sqrt( gam * Z * te / mu ) / 100.;  // ion sound speed (m/s)

    var	va = 2.18e+11 * Bo / Math.sqrt( mu * n_i ) / 100.;  // Alfven velocity (m/s)

 // original beta expression  	var beta = 4.03e-11 * n_total * ( total_t ) / ( Bo * Bo );  // beta (dimensionless)
	var beta = Math.pow( cs, 2 ) / Math.pow( va, 2 );
	
	var iil = c / ( 2. * Math.PI * fpi );  // ion inertial length (m)

	document.output.fce.value = fce.toExponential(2);
	document.output.fci.value = fci.toExponential(2);
	document.output.fpe.value = fpe.toExponential(2);
	document.output.fpi.value = fpi.toExponential(2);
	document.output.nu_e.value = nu_e.toExponential(2);
	document.output.nu_i.value = nu_i.toExponential(2);
	document.output.re.value = re.toExponential(2);
	document.output.ri.value = ri.toExponential(2);
	document.output.psd.value = psd.toExponential(2);
	document.output.ld.value = ld.toExponential(2);
	document.output.vte.value = vte.toExponential(2);
	document.output.vti.value = vti.toExponential(2);
	document.output.cs.value = cs.toExponential(2);
	document.output.va.value = va.toExponential(2);
	document.output.beta.value = beta.toExponential(2);
	document.output.iil.value = iil.toExponential(2);

}


/* load default parameters */
function loadDefault()
{
	var def = document.inputs.defaults.options[document.inputs.defaults.selectedIndex].text
	
	switch (def)
	{
		case "Tokamak":
			var Bo = 3e+4;
			var mu = 2;
			var Z = 1;
			var cl = 12.;
			var n_e = 5e+13;
			var te = 1e+4;
			var ti = 1e+4;
			var ai = 5./3.;
			
			document.inputs.Bo.value = Bo.toExponential(3);
			document.inputs.mu.value = mu.toFixed(2);
			document.inputs.Z.value = Z.toFixed(2);
			document.inputs.cl.value = cl.toFixed(3);
			document.inputs.n_e.value = n_e.toExponential(3);
			document.inputs.te.value = te.toExponential(3);
			document.inputs.ti.value = ti.toExponential(3);
			document.inputs.ai.value = ai.toFixed(3);
			
			calculate( document.inputs.Bo, document.inputs.mu, document.inputs.Z, document.inputs.cl, document.inputs.n_e, document.inputs.te, document.inputs.ti, document.inputs.ai );
		break;

		case "Pace Thesis":
			var Bo = 1.2e+3;
			var mu = 2;
			var Z = 1;
			var cl = 12.;
			var n_e = 2e+12;
			var te = 6;
			var ti = 1;
			var ai = 5./3.;
			
			document.inputs.Bo.value = Bo.toExponential(1);
			document.inputs.mu.value = mu.toFixed(2);
			document.inputs.Z.value = Z.toFixed(2);
			document.inputs.cl.value = cl.toFixed(3);
			document.inputs.n_e.value = n_e.toExponential(3);
			document.inputs.te.value = te.toExponential(3);
			document.inputs.ti.value = ti.toExponential(3);
			document.inputs.ai.value = ai.toFixed(3);
			
			calculate( document.inputs.Bo, document.inputs.mu, document.inputs.Z, document.inputs.cl, document.inputs.n_e, document.inputs.te, document.inputs.ti, document.inputs.ai );
		break;

		// parameters provided by Tom Moore (tmoore@pop600.gsfc.nasa.gov)
		case "Solar Wind 10-100":
			var Bo = 1.e-5;
			var mu = 1.;
			var Z = 1.;
			var cl = 12.;
			var n_e = 5.0;
			var te = 10.;
			var ti = 100.;
			var ai = 1.0;
			
			document.inputs.Bo.value = Bo.toExponential(1);
			document.inputs.mu.value = mu.toFixed(2);
			document.inputs.Z.value = Z.toFixed(2);
			document.inputs.cl.value = cl.toFixed(3);
			document.inputs.n_e.value = n_e.toExponential(3);
			document.inputs.te.value = te.toFixed(1);
			document.inputs.ti.value = ti.toFixed(1);
			document.inputs.ai.value = ai.toFixed(2);
			
			calculate( document.inputs.Bo, document.inputs.mu, document.inputs.Z, document.inputs.cl, document.inputs.n_e, document.inputs.te, document.inputs.ti, document.inputs.ai );
		break;

		// parameters provided by Melvyn L Goldstein (u2mlg@lssp-mail.gsfc.nasa.gov)
		case "Solar Wind 30-10":
			var Bo = 5.e-5;
			var mu = 1.;
			var Z = 1.;
			var cl = 12.;
			var n_e = 5.0;
			var te = 30.;
			var ti = 10.;
			var ai = 1.0;
			
			document.inputs.Bo.value = Bo.toExponential(1);
			document.inputs.mu.value = mu.toFixed(2);
			document.inputs.Z.value = Z.toFixed(2);
			document.inputs.cl.value = cl.toFixed(3);
			document.inputs.n_e.value = n_e.toExponential(3);
			document.inputs.te.value = te.toFixed(1);
			document.inputs.ti.value = ti.toFixed(1);
			document.inputs.ai.value = ai.toFixed(2);
			
			calculate( document.inputs.Bo, document.inputs.mu, document.inputs.Z, document.inputs.cl, document.inputs.n_e, document.inputs.te, document.inputs.ti, document.inputs.ai );
		break;

	}
	/* originally taken from Apple Voices example (see plasma_params.css for link) */
	document.getElementById("popupMenuText").innerText = def;
}