// bij het geven van tds. verdeel de tds over natrium chloride

function invullenNaCl()
{
var tds1=document.osmose.tds1.value

Na=tds1*22.99/58.44
Cl=tds1*35.45/58.44

document.osmose.NH4.value=""
document.osmose.K.value=""
document.osmose.Na.value=Na.toPrecision(5)
document.osmose.Mg.value=""
document.osmose.Ca.value=""
document.osmose.Sr.value=""
document.osmose.Ba.value=""
document.osmose.CO3.value=""
document.osmose.HCO3.value=""
document.osmose.NO3.value=""
document.osmose.Cl.value=Cl.toPrecision(5)
document.osmose.F.value=""
document.osmose.SO4.value=""
document.osmose.SiO2.value=""
document.osmose.B.value=""
document.osmose.temp.value=20
bereken()
}

// Bij het drukken op de knop seawater de functie invullensw() geroepen
function invullensw()
{
NH4=0
K=390
Na=10900
Mg=1310
Ca=410
Sr=13
Ba=0.05
CO3=0
HCO3=152
NO3=0.6
Cl=19700
F=1.4
SO4=2740
SiO2=5
B=5
temp=20
tds1="not used"

document.osmose.NH4.value=NH4
document.osmose.K.value=K
document.osmose.Na.value=Na
document.osmose.Mg.value=Mg
document.osmose.Ca.value=Ca
document.osmose.Sr.value=Sr
document.osmose.Ba.value=Ba
document.osmose.CO3.value=CO3
document.osmose.HCO3.value=HCO3
document.osmose.NO3.value=NO3
document.osmose.Cl.value=Cl
document.osmose.F.value=F
document.osmose.SO4.value=SO4
document.osmose.SiO2.value=SiO2
document.osmose.B.value=B
document.osmose.tds1.value=tds1
document.osmose.temp.value=temp
bereken()
}

// Bij het drukken op de knop brakish water wordt de functie invullenbw() geroepen
function invullenbw()
{
NH4=0.05
K=3.3
Na=36
Mg=6
Ca=84
Sr=0.7
Ba=0.07
CO3=0
HCO3=265
NO3=4.3
Cl=45
F=0.14
SO4=24
SiO2=9
B=0
temp=12
tds1="not used"

document.osmose.NH4.value=NH4
document.osmose.K.value=K
document.osmose.Na.value=Na
document.osmose.Mg.value=Mg
document.osmose.Ca.value=Ca
document.osmose.Sr.value=Sr
document.osmose.Ba.value=Ba
document.osmose.CO3.value=CO3
document.osmose.HCO3.value=HCO3
document.osmose.NO3.value=NO3
document.osmose.Cl.value=Cl
document.osmose.F.value=F
document.osmose.SO4.value=SO4
document.osmose.SiO2.value=SiO2
document.osmose.B.value=B
document.osmose.temp.value=temp
document.osmose.tds1.value=tds1
bereken()
}

function bereken()
{
var temp=document.osmose.temp.value
var eenheidtemp=document.osmose.eenheidtemp.value
var tds1=document.osmose.tds1.value

if		(eenheidtemp=="degree C")	{temp1=temp*1+273}
else if	(eenheidtemp=="degree F")	{temp1=(0.555*(temp-32))+273}
else if (eenheidtemp=="kelvin")		{temp1=temp}

if (temp1>=308) {alert("This calculator gives you a good indication for T <35 degree Celcius")}

var NH4=document.osmose.NH4.value
var K=document.osmose.K.value
var Na=document.osmose.Na.value
var Mg=document.osmose.Mg.value
var Ca=document.osmose.Ca.value
var Sr=document.osmose.Sr.value
var Ba=document.osmose.Ba.value
var CO3=document.osmose.CO3.value
var HCO3=document.osmose.HCO3.value
var NO3=document.osmose.NO3.value
var Cl=document.osmose.Cl.value
var F=document.osmose.F.value
var SO4=document.osmose.SO4.value
var SiO2=document.osmose.SiO2.value
var B=document.osmose.B.value

var eenheidtds=document.osmose.eenheidtds.value
var eenheidtds1=document.osmose.eenheidtds1.value

if		(eenheidtds1=="mg/L") {tds1=tds1}
else if (eenheidtds1=="ppm")  {tds1=tds1}

var eenheidNH4=document.osmose.eenheidNH4.value
var eenheidK=document.osmose.eenheidK.value
var eenheidNa=document.osmose.eenheidNa.value
var eenheidMg=document.osmose.eenheidMg.value
var eenheidCa=document.osmose.eenheidCa.value
var eenheidSr=document.osmose.eenheidSr.value
var eenheidBa=document.osmose.eenheidBa.value
var eenheidCO3=document.osmose.eenheidCO3.value
var eenheidHCO3=document.osmose.eenheidHCO3.value
var eenheidNO3=document.osmose.eenheidNO3.value
var eenheidCl=document.osmose.eenheidCl.value
var eenheidF=document.osmose.eenheidF.value
var eenheidSO4=document.osmose.eenheidSO4.value
var eenheidSiO2=document.osmose.eenheidSiO2.value
var eenheidB=document.osmose.eenheidB.value

if	(NH4=="" ||NH4==0)	{molarmassNH4=0}
else					{molarmassNH4=18.04}

//alert("de maolaire massa van NH4 is: "+molarmassNH4)

if	(K=="" ||K==0)		{molarmassK=0}
else					{molarmassK=39.10}

if	(Na=="" ||Na==0)	{molarmassNa=0}
else					{molarmassNa=22.99}

if	(Mg=="" ||Mg==0)	{molarmassMg=0}
else					{molarmassMg=24.31}

if	(Ca=="" ||Ca==0)	{molarmassCa=0}
else					{molarmassCa=40.08}

if	(Sr=="" ||Sr==0)	{molarmassSr=0}
else					{molarmassSr=87.62}

if	(Ba=="" ||Ba==0)	{molarmassBa=0}
else					{molarmassBa=137.33}

if	(CO3=="" ||CO3==0)	{molarmassCO3=0}
else					{molarmassCO3=60.01}

if	(HCO3=="" ||HCO3==0){molarmassHCO3=0}
else					{molarmassHCO3=61.02}

if	(NO3=="" ||NO3==0)	{molarmassNO3=0}
else					{molarmassNO3=62}

if	(Cl=="" ||Cl==0)	{molarmassCl=0}
else					{molarmassCl=35.45}

if	(F=="" ||F==0)		{molarmassF=0}
else					{molarmassF=19.00}

if	(SO4=="" ||SO4==0)	{molarmassSO4=0}
else					{molarmassSO4=96.06}

if	(SiO2=="" ||SiO2==0){molarmassSiO2=0}
else					{molarmassSiO2=92.15}

if	(B=="" ||B==0)		{molarmassB=0}
else					{molarmassB=10.81}

//convert to mg/L
if 		(eenheidNH4=="mg/L")	{NH4=NH4}
else if (eenheidNH4=="ppm")		{NH4=NH4}
else if (eenheidNH4=="g/m3")	{NH4=NH4}
else if (eenheidNH4=="mol/L")	{NH4*molarmassNH4*1e3}
else if (eenheidNH4=="mmol/L")	{NH4*molarmassMNH4}

if 		(eenheidK=="mg/L")		{K=K}
else if (eenheidK=="ppm")		{K=K}
else if (eenheidK=="g/m")		{K=K}
else if (eenheidK=="mol/L")		{K*molarmassK*1e3}
else if (eenheidK=="mmol/L")	{K*molarmassK}

if 		(eenheidNa=="mg/L")		{Na=Na}
else if (eenheidNa=="ppm")		{Na=Na}
else if (eenheidNa=="g/m3")		{Na=Na}
else if (eenheidNa=="mol/L")	{Na*molarmassNa*1e3}
else if (eenheidNa=="mmol/L")	{Na*molarmassNa}

if 		(eenheidMg=="mg/L")		{Mg=Mg}
else if (eenheidMg=="ppm")		{Mg=Mg}
else if (eenheidMg=="g/m3")		{Mg=Mg}
else if (eenheidMg=="mol/L")	{Mg*molarmassMg*1e3}
else if (eenheidMg=="mmol/L")	{Mg*molarmassMg}

if 		(eenheidCa=="mg/L")		{Ca=Ca}
else if (eenheidCa=="ppm")		{Ca=Ca}
else if (eenheidCa=="g/m3")		{Ca=Ca}
else if (eenheidCa=="mol/L")	{Ca*molarmassCa*1e3}
else if (eenheidCa=="mmol/L")	{Ca*molarmassCa}

if 		(eenheidSr=="mg/L")		{Sr=Sr}
else if (eenheidSr=="ppm")		{Sr=Sr}
else if (eenheidSr=="g/m3")		{Sr=Sr}
else if (eenheidSr=="mol/L")	{Sr*molarmassSr*1e3}
else if (eenheidSr=="mmol/L")	{Sr*molarmassSr}

if 		(eenheidBa=="mg/L")		{Ba=Ba}
else if (eenheidBa=="ppm")		{Ba=Ba}
else if (eenheidBa=="g/m3")		{Ba=Ba}
else if (eenheidBa=="mol/L")	{Ba*molarmassBa*1e3}
else if (eenheidBa=="mmol/L")	{Ba*molarmassBa}

if 		(eenheidCO3=="mg/L")	{CO3=CO3}
else if (eenheidCO3=="ppm")		{CO3=CO3}
else if (eenheidCO3=="g/m3")	{CO3=CO3}
else if (eenheidCO3=="mol/L")	{CO3*molarmassCO3*1e3}
else if (eenheidCO3=="mmol/L")	{CO3*molarmassCO3}

if 		(eenheidHCO3=="mg/L")	{HCO3=HCO3}
else if (eenheidHCO3=="ppm")	{HCO3=HCO3}
else if (eenheidHCO3=="g/m3")	{HCO3=HCO3}
else if (eenheidHCO3=="mol/L")	{HCO3*molarmassHCO3*1e3}
else if (eenheidHCO3=="mmol/L")	{HCO3*molarmassHCO3}

if 		(eenheidNO3=="mg/L")	{NO3=NO3}
else if (eenheidNO3=="ppm")		{NO3=NO3}
else if (eenheidNO3=="g/m3")	{NO3=NO3}
else if (eenheidNO3=="mol/L")	{NO3*molarmassNO3*1e3}
else if (eenheidNO3=="mmol/L")	{NO3*molarmassNO3}

if 		(eenheidCl=="mg/L")		{Cl=Cl}
else if (eenheidCl=="ppm")		{Cl=Cl}
else if (eenheidCl=="g/m3")		{Cl=Cl}
else if (eenheidCl=="mol/L")	{Cl*molarmassCl*1e3}
else if (eenheidCl=="mmol/L")	{Cl*molarmassCl}

if 		(eenheidF=="mg/L")		{F=F}
else if (eenheidF=="ppm")		{F=F}
else if (eenheidF=="g/m3")		{F=F}
else if (eenheidF=="mol/L")		{F*molarmassF*1e3}
else if (eenheidF=="mmol/L")	{F*molarmassF}

if 		(eenheidSO4=="mg/L")	{SO4=SO4}
else if (eenheidSO4=="ppm")		{SO4=SO4}
else if (eenheidSO4=="g/m3")	{SO4=SO4}
else if (eenheidSO4=="mol/L")	{SO4*molarmassSO4*1e3}
else if (eenheidSO4=="mmol/L")	{SO4*molarmassSO4}

if 		(eenheidSiO2=="mg/L")	{SiO2=SiO2}
else if (eenheidSiO2=="ppm")	{SiO2=SiO2}
else if (eenheidSiO2=="g/m3")	{SiO2=SiO2}
else if (eenheidSiO2=="mol/L")	{SiO2*molarmassSiO2*1e3}
else if (eenheidSiO2=="mmol/L")	{SiO2*molarmassSiO2}

if 		(eenheidB=="mg/L")		{B=B}
else if (eenheidB=="ppm")		{B=B}
else if (eenheidB=="g/m3")		{B=B}
else if (eenheidB=="mol/L")		{B*molarmassB*1e3}
else if (eenheidB=="mmol/L")	{B*molarmassB}

//alles is naar mg/L geconverteert.



tds=NH4*1+K*1+Na*1+Mg*1+Ca*1+Sr*1+Ba*1+CO3*1+HCO3*1+NO3*1+Cl*1+F*1+SO4*1+SiO2*1+B*1 //tds in mg/L
//alert("de tds is: "+tds)
totalmolarmass=molarmassNH4+molarmassK+molarmassNa+molarmassMg+molarmassCa+molarmassSr+molarmassBa+molarmassCO3+molarmassHCO3+molarmassNO3+molarmassCl+molarmassF+molarmassSO4+molarmassSiO2+molarmassB //totale molaire massa
//alert("de totale molaire massa: "+totalmolarmass)
totalmolality=NH4*1/18.04+K*1/39.10+Na*1/22.99+Mg*1/24.31+Ca*1/40.08+Sr*1/87.62+Ba*1/137.33+CO3*1/60.01+HCO3*1/61.02+NO3*1/62+Cl*1/35.45+F*1/19+SO4*1/96.06+SiO2*1/92.15+B*1/10.81 //molality in mol/kg
//alert("de totale molality: "+totalmolality)

c=tds/totalmolarmass //totale concentratie in mol per liter

//if 		(temp==25){pressure=1.12*temp*totalmolality*1e-3*6.8948*1e3/1e5}

//else if	(temp>=25){pressure=1.12*temp*to


pressure=1.12*temp1*totalmolality*1e-3*6.8948*1e3/1e5 //psi conversion

	if 					(tds>43000)		{alert("This calculator gives you a good indication ONLY if the the TDS < 43000 mg/L")}
	else	{	if 		(tds<43000 && tds>=35000){indication1=pressure*2}
				else if (tds<35000 && tds>=22000){indication1=pressure*2.2}
				else if (tds<22000 && tds>=15000){indication1=pressure*2.7}
				else 							 {indication1=pressure*3}
			}

if 		(temp==25){indication=indication1}
else if	(temp<=25){factor=25-temp;indication=indication1+factor*0.01*indication1}
else if (temp>=25){factor=temp-25;indication=indication1-factor*0.015*indication1}

if 		(eenheidtds=="mg/L"){tds=tds}
else if (eenheidtds=="ppm"){tds=tds}

document.osmose.totalmolarmass.value=totalmolarmass.toPrecision(5)
document.osmose.totalmolarmass.className="blauw"

document.osmose.totalmolality.value=totalmolality.toPrecision(5)
document.osmose.totalmolality.className="blauw"

document.osmose.tds.value=tds.toPrecision(5)
document.osmose.tds.className="blauw"

document.osmose.pressure.value=pressure.toPrecision(5)
document.osmose.pressure.className="blauw"

document.osmose.indication.value=indication.toPrecision(5)
document.osmose.indication.className="blauw"
}

function sturen(form)
{
var missingField = false
var liformName = ""
var liformCountry = ""
var liformPhone = ""
var liformEmail = ""
var str =form.formEmail.value
var filter=/^.+@.+\..{2,3}$/

	if (form.formName.value == "") {liformName = "\n * Name"; missingField = true}
	if (form.formCountry.value == "") {liformCountry = "\n * Country"; missingField = true}
	if (form.formPhone.value == "") {liformPhone = "\n * Phone"; missingField = true}
	if (form.formEmail.value == "") {liformEmail = "\n * Email"; missingField = true}
	if (!filter.test(str) && form.formEmail.value!="") {alert("Please enter a valid email address.")}	
	else if (missingField==true || form.formEmail.value=="") {
		var message = "For proper contact registration at Lenntech, please fill out all the required fields:\t\n"+liformName+liformCountry+liformEmail+liformPhone
		alert(message)
	}
	else
	{form.submit()}
}