/*
2004-2007 Copyright Vitalii Vanovschi. All rights reserved.
Licence: Modification, usage and distribution of this javascript code are strictly prohibited unless you have a written (email or letter) permission from the author.
*/


function sciformat(x) {
	if (x <= 100 && x >= 1 || x==0)
		return x.toString().substr(0,8);
	sgn = 1;
	if (x <0) {
		x = -x;
		sgn = -1;
	}
	with (Math) {
		exp = floor(Math.log(x)/Math.LN10);
		mnt = (sgn*x*pow(10,-exp)).toString().substr(0,8);
		if (mnt =='0.999999') { 
			mnt = 1;
		}
		if (mnt =='9.999999') { 
			mnt = 1;
			exp += 1;
		}
		if (exp == '0') {
			return (mnt);
		} else {
			return (mnt+"E"+exp);
		}
	}
}

function tformat(x) {
	return Math.round(x*1000)/1000;
}

function econv() {
//hartree to other units
h2e = new Array();
h2e['hr'] = 1;
h2e['e'] = 27.2107;
h2e['c'] = 219474.63;
h2e['kc'] = 627.503;
h2e['kj'] = 2625.5;
h2e['k'] = 315777;
h2e['j'] = 4.360E-18;
h2e['hz'] = 6.57966E+15;

with (document) {
    if (eform.etype.value == "nm") {
        energy = 1E7 / (eform.einput.value * h2e['c']);
    } else if (eform.etype.value == "s") {
        energy = 1 / (eform.einput.value * h2e['hz']);
    } else {
    	energy = eform.einput.value / h2e[eform.etype.value];
    }

	eform.eouthr.value = sciformat(energy * h2e['hr']);
	eform.eoute.value = sciformat(energy * h2e['e']);
	eform.eoutc.value = sciformat(energy * h2e['c']);
	eform.eoutkc.value = sciformat(energy * h2e['kc']);
	eform.eoutkj.value = sciformat(energy * h2e['kj']);
	eform.eoutk.value = sciformat(energy * h2e['k']);
	eform.eoutj.value = sciformat(energy * h2e['j']);
	eform.eouthz.value = sciformat(energy * h2e['hz']);
	eform.eoutnm.value = sciformat(1E7/(energy * h2e['c']));
	eform.eouts.value = sciformat(1/(energy * h2e['hz']));
}
}


function tconv() {

with (document) {
	//get it in K first
	T = tform.tinput.value; 
	if (tform.ttype.value=='c') {
		T = tform.tinput.value*1.0 + 273.15;
	} else if (tform.ttype.value=='f') {
		T = (tform.tinput.value*1.0-32)/1.8 + 273.15;
	}
	tform.toutk.value = tformat(T);
	tform.toutc.value = tformat(T-273.15);
	tform.toutf.value = tformat((T-273.15)*1.8+32);
}
}

function pconv() {

pr = new Array();
pr['p'] = 1;
pr['psi'] = 145.04E-6;
pr['b'] = 0.00001;
pr['a'] = 9.8692E-6;
pr['t'] = 0.0075006;	
with (document) {
	//get it in Pa first
	pres = pform.pinput.value / pr[pform.ptype.value];

	pform.poutp.value = sciformat(pres * pr['p']);
	pform.poutb.value = sciformat(pres * pr['b']);
	pform.pouta.value = sciformat(pres * pr['a']);
	pform.poutt.value = sciformat(pres * pr['t']);
	pform.poutpsi.value = sciformat(pres * pr['psi']);

}
}


function lconv() {

//conversion to metres

m2l = new Array();
m2l['au'] = 5.291772083E-11;
m2l['ang'] = 1E-10;
m2l['fm'] = 1E-15;
m2l['pm'] = 1E-12;
m2l['nm'] = 1E-9;
m2l['mkm'] = 1E-6;
m2l['mm'] = 1E-3;
m2l['cm'] = 1E-2;
m2l['m'] = 1;
m2l['in'] = 25.4E-3;
m2l['ft'] = 0.30480061;
m2l['mi'] = 1609.344;

with (document) {
	l = lform.linput.value * m2l[lform.ltype.value];

	lform.loutau.value = sciformat(l / m2l['au']);
	lform.loutang.value = sciformat(l / m2l['ang']);
	lform.loutfm.value = sciformat(l / m2l['fm']);
	lform.loutpm.value = sciformat(l / m2l['pm']);
	lform.loutnm.value = sciformat(l / m2l['nm']);
	lform.loutmkm.value = sciformat(l / m2l['mkm']);
	lform.loutmm.value = sciformat(l / m2l['mm']);
	lform.loutcm.value = sciformat(l / m2l['cm']);
	lform.loutm.value = sciformat(l / m2l['m']);
	lform.loutin.value = sciformat(l / m2l['in']);
	lform.loutft.value = sciformat(l / m2l['ft']);
	lform.loutmi.value = sciformat(l / m2l['mi']);
}
}

function kconv() {

with (document) {
	//get it in K first
	k = kform.kinput.value; 
    switch (kform.ktype.value) {
        case 'ka': ka = k; break;
        case 'pka': ka = Math.pow(10,-k); break;
        case 'kb': ka = 1e-14/k; break;
        case 'pkb': ka = Math.pow(10,k-14); break;
    }
	kform.koutka.value = sciformat(ka);
	kform.koutpka.value = sciformat(-Math.log(ka)/Math.log(10));
	kform.koutkb.value = sciformat(1e-14/ka);
	kform.koutpkb.value = sciformat(14+Math.log(ka)/Math.log(10));
}
}
/*
2004-2007 Copyright Vitalii Vanovschi. All rights reserved.
Licence: Modification, usage and distribution of this javascript code are strictly prohibited unless you have a written (email or letter) permission from the author.
*/
