function control()
{
var EC=document.resin.EC.value
var chemical=document.resin.chemical.value
var concentration=document.resin.concentration.value
var amountwater=document.resin.amountwater.value
var densityresin=document.resin.densityresin.value
var pH=document.resin.pH.value
var flow=document.resin.flow.value
var volumecolumn=document.resin.volumecolumn.value

if (chemical&&concentration&&amountwater&&EC&&densityresin)

{		if	(chemical!="no")	{bereken()}
		else					{alert("Give the chemical element that you want to remove from the water")}
}

else	{		 if		(chemical=="no")	{alert("Give the chemical element that you want to remove from the water")}	
			else if		(concentration=="")	{alert("Give the concentration for the chemical element that you want to remove")}
			else if		(amountwater=="")	{alert("Give the amount of water that you want to treat")}
			else if		(flow=="")			{alert("Without the flow it is not possible to calculate the time elapsed between two regenerations")}	
			//else if		(pH=="")			{alert("Give the pH of the water that you want to treat")}
			else if		(EC=="")			{alert("Give the value for the exchange capacity")}
			else if		(densityresin=="")	{alert("Give the density of the resin")}
			else if		(volumecolumn=="")	{alert("Without the volume of a column it is not possible to calculate the number of columns you need")} 	
		}

}

function bereken()
{
var EC=document.resin.EC.value
	EC=parseFloat(EC)
var ECunit=document.resin.ECunit.value
var EC2unit=document.resin.EC2unit.value

var chemical=document.resin.chemical.value
var concentration=document.resin.concentration.value
	concentration=parseFloat(concentration)
var concentrationunit=document.resin.concentrationunit.value
var amountwater=document.resin.amountwater.value
var amountwaterunit=document.resin.amountwaterunit.value
var densityresin=document.resin.densityresin.value
var densityresinunit=document.resin.densityresinunit.value
var ECneededunit=document.resin.ECneededunit.value
var massresinunit=document.resin.massresinunit.value
var volumeresinunit=document.resin.volumeresinunit.value
var Cconvertunit=document.resin.Cconvertunit.value

var volumecolumn=document.resin.volumecolumn.value
var volumecolumnunit=document.resin.volumecolumnunit.value
var ECcolumn=document.resin.ECcolumn.value
var ECcolumnunit=document.resin.ECcolumnunit.value
var pH=document.resin.pH.value
var flow=document.resin.flow.value
var flowunit=document.resin.flowunit.value
var regenerationwaterunit=document.resin.regenerationwaterunit.value
var regenerationtimeunit=document.resin.regenerationtimeunit.value

//convert density to kg/L
if		(densityresinunit=="kg/L")		{densityresin=densityresin}
else if (densityresinunit=="kg/m3")		{densityresin=densityresin/1000}

//convert the exchange capacity to eq/L
if		(ECunit=="eq/L")			{EC=EC}		
else if (ECunit=="meq/L")			{EC=EC/1000}
else if (ECunit=="eq/kg")			{EC=EC*densityresin}
else if (ECunit=="meq/kg")			{EC=EC*densityresin/1000}

//convert the exchange capacity to specified unit
if		(EC2unit=="eq/L")		{EC2=EC}		
else if (EC2unit=="meq/L")		{EC2=EC*1000}
else if (EC2unit=="eq/kg")		{EC2=EC/densityresin}
else if (EC2unit=="meq/kg")		{EC2=EC/densityresin*1000}

C="You need a cationic resin"
A="You need an anionic resin"

B="HCL or NaCl"
E="NaOH , NaCl or Ca(OH)2"

//give valence and molar mass of different elements


	 if	(chemical=="Ca")	{Z=2;M=40.08;typeresin=C;typeregenerant=B}
else if	(chemical=="Mg")	{Z=2;M=24.31;typeresin=C;typeregenerant=B}
else if	(chemical=="Na")	{Z=1;M=22.99;typeresin=C;typeregenerant=B}
else if	(chemical=="K")		{Z=1;M=39.10;typeresin=C;typeregenerant=B}

else if	(chemical=="HCO3")	{Z=1;M=61.02;typeresin=A;typeregenerant=E}
else if	(chemical=="SO4")	{Z=2;M=96.06;typeresin=A;typeregenerant=E}
else if	(chemical=="Cl")	{Z=1;M=35.45;typeresin=A;typeregenerant=E}
else if	(chemical=="NO3")	{Z=1;M=62	;typeresin=A;typeregenerant=E}
	
//convert concentration to eq/L
if		(concentrationunit=="eq/L")		{concentration=concentration}
else if (concentrationunit=="meq/L")	{concentration=concentration/1000}
else if (concentrationunit=="mol/L")	{concentration=concentration*Z}
else if (concentrationunit=="mmol/L")	{concentration=concentration*Z/1000}
else if (concentrationunit=="g/L")		{concentration=concentration*Z/M}
else if (concentrationunit=="mg/L")		{concentration=concentration*Z/M/1000}

//convert concentration to g/L and mol/L
if		(Cconvertunit=="eq/L")		{	Cconvert	=concentration}
else if (Cconvertunit=="meq/L")		{	Cconvert	=concentration*1000}
else if (Cconvertunit=="mol/L")		{	Cconvert	=concentration/Z}
else if (Cconvertunit=="mmol/L")	{	Cconvert	=concentration/Z*1000}
else if	(Cconvertunit=="g/L")		{	Cconvert	=concentration*M/Z}
else if (Cconvertunit=="mg/L")		{	Cconvert	=concentration*M/Z*1000}

//convert amount of water unit to L
if		(amountwaterunit=="L")		{amountwater=amountwater}
else if	(amountwaterunit=="m3")		{amountwater=amountwater*1000}

//calculation needed exchange capacity eq
ECneeded=concentration*amountwater

//calculation required volume of resin in L
volumeresin=ECneeded/EC

//calculation required mass of resin in kg
massresin=densityresin*volumeresin

//convert volume column to L
if		(volumecolumnunit=="L")			{volumecolumn=volumecolumn}
else if	(volumecolumnunit=="m3")		{volumecolumn=volumecolumn*1000}

//capacity in one coloumn in eq
ECcolumn=EC*volumecolumn

if		(ECcolumnunit=="eq")			{ECcolumn=ECcolumn}
else if (ECcolumnunit=="meq")			{ECcolumn=ECcolumn*1000}

//alert ("the volume of one column in liter is "+volumecolumn)
//alert ("the exchange capacity in one column is eq:"+ECcolumn)

//calculating number of coloumns needed
numbercolumns=Math.round(ECneeded/ECcolumn)

regenerationwater=ECcolumn/concentration

//conversion to L/s
if		(flowunit=="L/s")		{flow=flow}
else if	(flowunit=="L/h")		{flow=flow/3600}
else if	(flowunit=="m3/s")		{flow=flow*1000}
else if	(flowunit=="m3/h")		{flow=flow*1000/3600}

regenerationtime=1/(flow*concentration)*ECcolumn

//convert capacity to right unit
if		(ECneededunit=="eq")			{ECneeded=ECneeded}
else if (ECneededunit=="meq")			{ECneeded=ECneeded*1000}

//convert mass of resin to right unit
if		(massresinunit=="kg")		{massresin=massresin}
else if	(massresinunit=="g")		{massresin=massresin*1000}

//convert volume of resin to right unit
if		(volumeresinunit=="L")		{volumeresin=volumeresin}
else if	(volumeresinunit=="m3")		{volumeresin=volumeresin/1000}

if 		(regenerationwaterunit=="L"){regenerationwater=regenerationwater}
else if (regenerationwaterunit=="m3"){regenerationwater=regenerationwater/1000}

if		(regenerationtimeunit=="s"){regenerationtime=regenerationtime}
else if	(regenerationtimeunit=="h"){regenerationtime=regenerationtime/3600}


document.resin.ECneeded.value=ECneeded
document.resin.massresin.value=massresin.toPrecision(6)
document.resin.EC2.value=EC2.toPrecision(6)
document.resin.volumeresin.value=volumeresin.toPrecision(6)
document.resin.typeresin.value=typeresin
document.resin.ECcolumn.value=ECcolumn.toPrecision(6)
document.resin.numbercolumns.value=numbercolumns
document.resin.regenerationwater.value=regenerationwater.toPrecision(6)
document.resin.typeregenerant.value=typeregenerant
document.resin.regenerationtime.value=regenerationtime.toPrecision(6)
document.resin.Cconvert.value=Cconvert

}

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()}
}