// JavaScript Document
// JavaScript Routines for Calculating Primary Dry Time
// Including Calculating Ti
	//Global Constants
	R=8.314772;
	A=3.49295277823909E+12;
	Mw=.018;
	deltaH=51027.24241968272;
	rho=917;
	kI=2.57;
	
	function dome()
	{
	delT=eval(form1.deltaT.value);
	Pc=eval(form1.Pc.value); //reads the display value as a string and converts to number
	unit=document.form1.Unit.value;
	Pc=Pressure_Value(Pc,unit);//routine will return Pc in units of Pascals
	Ts=Ts+273.15 //this doesn't change the display but changes the variable to Absolute
	//Pc=Pc*133.32236842 //This doesn't change the display but does changes the variable to Pa
	Rp=eval(form1.Resistance.value)*4.799605263*1E4//Converts units used to m/s and doesn't change the display
	var eqn="(Ap*deltaH*Pc+Av*Kv(Pc)*Rp*Mw*Ts-Av*Kv(Pc)*Rp*Mw*x-Av*Kv(Pc)*Rp*Mw*delT)/(Ap*deltaH)-(A*Math.exp(-deltaH/(R*x)))"
	var deriv="((-Av)*Kv(Pc)*Rp*Mw)/(Ap*deltaH)-(A*deltaH/(R*x*x))*Math.exp(-deltaH/(R*x))";
	x=248.16 //starting value
	wow=Newton(eqn,deriv,x);
	this.form1.display.value=round_decimals(wow-273.15, 3);
	Ts=eval(form1.Temp.value);//Returns to window value
	Pc=eval(form1.Pc.value);  //Returns to window value
	Rp=eval(form1.Resistance.value);  //Returns to window value
	}

	
	function Newton(eqn,deriv,x)
	{
	var m=x-1;
	var i=0;
	while ((m!=x)&&(i<21)){
	var m=x;
	var x=x-eval(eqn)/eval(deriv);
	var i=i+1;
	}
	return(x);
	}
	
		
	function Vial()
	{
	vialtype=form1.vialt.options[form1.vialt.options.selectedIndex].value;
    //vialtype is a global variable
	vial=form1.vialsize.options[form1.vialsize.options.selectedIndex].value;
	vial=eval(vial);
	//alert('vialtype='+vialtype+'  size='+vial);
	}
	
	function Pressure()// this is called onload and onreset and specifically with a button.
	{
	vialtype=form1.vialt.options[form1.vialt.options.selectedIndex].value;
	vial=form1.vialsize.options[form1.vialsize.options.selectedIndex].value;
	vial=eval(vial);
	Ts=eval(form1.Temp.value);
	Ap=(eval(form1.vialid.value)/2)*(eval(form1.vialid.value)/2)*Math.PI;
	Av=(eval(form1.vialod.value)/2)*(eval(form1.vialod.value)/2)*Math.PI;
	delT=eval(form1.deltaT.value);
	FillVol=eval(form1.Fill_Vol.value);
	Tp=eval(form1.Tp.value);
	Tg=eval(form1.Tg.value);
	Solids=eval(form1.PercentSolids.value);
	unit=get_radio_value();
	if (unit == "torr") {
	         w=0.019*(Tp);
	         Pc=0.29*Math.pow(10,w);
	         document.form1.Pc.value=round_decimals(Pc,4); //display recommendation in torr
           }
  if (unit == "mbar") {
            w=0.019*(Tp);
            Pc=0.387*Math.pow(10,w);
            document.form1.Pc.value=round_decimals(Pc,4); //display recommendation in mbar
            }
	}
	
	function Resist() // called onclick only
	{
	Rp=(4/0.09)*(Solids/100)+(1-(4/.09)*0.01);
	document.form1.Resistance.value=round_decimals(Rp,2);
	}
	
	
	function redefine() // this is called whenever one of the parameters is changed.
	{
	Ts=eval(form1.Temp.value);
	Ap=(eval(form1.vialid.value)/2)*(eval(form1.vialid.value)/2)*Math.PI;
	Av=(eval(form1.vialod.value)/2)*(eval(form1.vialod.value)/2)*Math.PI;
	delT=eval(form1.deltaT.value);
	FillVol=eval(form1.Fill_Vol.value);
	Pc=eval(form1.Pc.value);
	Tp=eval(form1.Tp.value);
	Tg=eval(form1.Tg.value);
	Solids=eval(form1.PercentSolids.value);
	Rp=eval(form1.Resistance.value);
	//alert('solids = '+Solids);
	}
	
	function Kv(Pc)
	{	
	KC=(51.981/(vial*1E6))+5.872;
	if (vialtype == "tubing") 
	   {	  
	   KD=250*vial+.025;
	   }
	   else
	   {
	   KD=191.429*vial+.033;
	   }
	KV=KC+(1.043*Pc)/(1+KD*Pc);
	return KV;
	}
	function Pressure_Value(num,unit)
	// num is a pressure value
	// it has units of 'unit'
	// this routine returns a pressure value in Pascals
	//currently the only units handled are 'torr' and 'mbar'
	{
	for (var i=0; i < document.form1.Unit.length; i++)
   {
   if (document.form1.Unit[i].checked){var unit = document.form1.Unit[i].value;}
   }
  if (unit == "torr"){num=num*133.32236842};
	if (unit == "mbar"){num=num*100};
	return num;
  }

function get_radio_value()
{
for (var i=0; i < document.form1.Unit.length; i++)
   {
if (document.form1.Unit[i].checked){var unit = document.form1.Unit[i].value;}
}
return unit;
}


	function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
	}

function pad_with_zeros(rounded_value, decimal_places) { 
    var value_string = rounded_value.toString()  // Convert the number to a string 
    var decimal_location = value_string.indexOf(".")   // Locate the decimal point
    if (decimal_location == -1) { // Is there a decimal point?
        decimal_part_length = 0 // If no, then all decimal places will be padded with 0s      
        value_string += decimal_places > 0 ? "." : "" // If decimal_places is greater than zero, tack on a decimal point
    }
    else { 
        decimal_part_length = value_string.length - decimal_location - 1 // If yes, then only the extra decimal places will be padded with 0s
    }
    var pad_total = decimal_places - decimal_part_length // Calculate the number of decimal places that need to be padded with 0s    
    if (pad_total > 0) {       
        for (var counter = 1; counter <= pad_total; counter++) // Pad the string with 0s
            value_string += "0"
        }
    return value_string
}
	
function Calc_Rate()
    {
    TI=eval(document.form1.display.value)+273.15;
    PI=A*Math.exp(-deltaH/(R*TI));
    AP=(eval(form1.vialid.value)/2)*(eval(form1.vialid.value)/2)*Math.PI;
      num=eval(form1.Pc.value);
      unit=get_radio_value();
    PC=Pressure_Value(num,unit);
    RP=eval(form1.Resistance.value)*4.799605263*1E4;//Converts units used to m/s and doesn't change the display
    Rate1=AP*(PI-PC)/RP; //SI units are kg/s.
    Rate=(PI-PC)/RP; //SI units are kg/(s*m^2)
    Rate=Rate*360; //units are gm/(hr*cm^2)
    document.form2.ratebox.value=round_decimals(Rate, 4);
    Primary=1.5*(FillVol/Rate1)*1.66667E-5;
    document.form2.primary_time.value=round_decimals(Primary,0);
    }
    
    function update()
    {
    document.form2.Freeze_Temp.value=eval(document.form1.Tp.value)-18;
    document.form2.Freeze_Ramp.value=document.form2.Freeze_Ramp.value;
    document.form2.Freeze_Hold.value=document.form2.Freeze_Hold.value;
    Calc_Rate();
    document.form2.Primary_Temp.value=document.form1.Temp.value;
    document.form2.Primary_Ramp.value=document.form2.Primary_Ramp.value;
    document.form2.Primary_Hold.value=document.form2.primary_time.value;
    
    document.form2.Secondary_Temp.value=document.form2.Secondary_Temp.value;
    document.form2.Secondary_Ramp.value=document.form2.Secondary_Ramp.value;
    document.form2.Secondary_Hold.value=document.form2.Secondary_Hold.value;
    }
