// code for the animation
var anim = new Array(8);
var curPic = 0;
var startAnim;
var imagesLoaded = 0;

for (var i=0; i < 8; ++i)
	{
	anim[i] = new Image();
	anim[i].src = "bubbles" + i + ".gif";
	anim[i].onLoad = runAnimation();
	}
	
function runAnimation()
	{
	++imagesLoaded;
	if (imagesLoaded == 8)
		startAnim=setInterval("runBubbles()",175);
	}

function runBubbles()
	{
	if (curPic == 7)
			curPic = 0;
	else 
		++curPic;
		document.animation.src = anim[curPic].src;
	}


//open new window for Oils page
function oilspage() {
window.open("oils.htm", "Oils", "height=600, width=500, scrollbars=yes");
}
//open new window for Links page
function linkspage() {
window.open("links.htm", "Links", "height=600, width=500, scrollbars=yes");
}
//open new window for using calculator page
function usingpage() {
window.open("using.htm", "Using", "height=600, width=400, scrollbars=yes");
}


// confirm that the the user wants to clear the form	
function confirmReset() {
var resetForm = confirm ("Wil je al de gegevens wissen?");
if (resetForm == true)
	return true;
return false;

}


function validate() {
// verify that values have been entered in the oils and weight fields in the form
	if ((document.soap.oils1.value == ""
&& document.soap.oils2.value == ""
&& document.soap.oils3.value == ""
&& document.soap.oils4.value == ""
&& document.soap.oils5.value == ""
&& document.soap.oils6.value == ""
&& document.soap.oils7.value == ""
&& document.soap.oils8.value == "")||
(document.soap.weight1.value == ""
&& document.soap.weight2.value == ""
&& document.soap.weight3.value == ""
&& document.soap.weight4.value == ""
&& document.soap.weight5.value == ""
&& document.soap.weight6.value == ""
&& document.soap.weight7.value == ""
&& document.soap.weight8.value == "" ))
	{
	alert("Je moet tenminste 1 olie en gewicht invullen.");
	return false;
	}
}


function offLye1() {
        document.soap.lyes[0].checked = false;
}
function offLye0() {
        document.soap.lyes[1].checked = false;
}

// calculate all fields
function calculate() {
//round off values
function roundOff(value)
{
        var result = "" + Math.round(value * 1000)/1000;
        return result;
}


// calulate total weight
document.soap.totalweight.value =
roundOff((document.soap.weight1.value - 0) + (document.soap.weight2.value - 0) +
(document.soap.weight3.value - 0) + (document.soap.weight4.value - 0) +
(document.soap.weight5.value - 0) + (document.soap.weight6.value - 0) +
(document.soap.weight7.value - 0) + (document.soap.weight8.value - 0));

var NaOH = document.soap.lyes[0].checked;
var KOH = document.soap.lyes[1].checked;
if (NaOH == true){
// calculate weight of lye(NaOH)
document.soap.lye1.value = roundOff((document.soap.weight1.value * document.soap.oils1.value));
document.soap.lye2.value = roundOff((document.soap.weight2.value * document.soap.oils2.value));
document.soap.lye3.value = roundOff((document.soap.weight3.value * document.soap.oils3.value));
document.soap.lye4.value = roundOff((document.soap.weight4.value * document.soap.oils4.value));
document.soap.lye5.value = roundOff((document.soap.weight5.value * document.soap.oils5.value));
document.soap.lye6.value = roundOff((document.soap.weight6.value * document.soap.oils6.value));
document.soap.lye7.value = roundOff((document.soap.weight7.value * document.soap.oils7.value));
document.soap.lye8.value = roundOff((document.soap.weight8.value * document.soap.oils8.value));
}

else {
// calculate weight of lye (KOH)
document.soap.lye1.value = roundOff(((document.soap.weight1.value * document.soap.oils1.value)*1.4));
document.soap.lye2.value = roundOff(((document.soap.weight2.value * document.soap.oils2.value)*1.4));
document.soap.lye3.value = roundOff(((document.soap.weight3.value * document.soap.oils3.value)*1.4));
document.soap.lye4.value = roundOff(((document.soap.weight4.value * document.soap.oils4.value)*1.4));
document.soap.lye5.value = roundOff(((document.soap.weight5.value * document.soap.oils5.value)*1.4));
document.soap.lye6.value = roundOff(((document.soap.weight6.value * document.soap.oils6.value)*1.4));
document.soap.lye7.value = roundOff(((document.soap.weight7.value * document.soap.oils7.value)*1.4));
document.soap.lye8.value = roundOff(((document.soap.weight8.value * document.soap.oils8.value)*1.4));
}


// calculate total lye1
document.soap.totallye1.value =  
roundOff((document.soap.lye1.value - 0) + (document.soap.lye2.value - 0) + 
(document.soap.lye3.value - 0) + (document.soap.lye4.value - 0) +
(document.soap.lye5.value - 0) + (document.soap.lye6.value - 0) + 
(document.soap.lye7.value - 0) + (document.soap.lye8.value - 0));

//calculate 5% total excess oils
document.soap.totallye2.value = 
roundOff(((document.soap.totallye1.value - 0) * .95915));

//calculate 10% total excess oils 
document.soap.totallye3.value = 
roundOff(((document.soap.totallye1.value - 0) * .9094));

// calculate total superfatting oil 
document.soap.totalsfweight.value =
roundOff((document.soap.sfweight1.value - 0) + (document.soap.sfweight2.value - 0) + (document.soap.sfweight3.value - 0));

//calculate total oils
document.soap.totaloils.value =
roundOff((document.soap.totalsfweight.value - 0) + (document.soap.totalweight.value - 0));

//sends total lye1 to total lye 
document.soap.totallye.value = roundOff((document.soap.totallye1.value));

//calculate total water
document.soap.totalwater.value = roundOff((document.soap.totaloils.value * (1/3)));

//calculate final total weight
document.soap.total.value =
roundOff((document.soap.totalwater.value - 0) +
(document.soap.totallye.value - 0) +
(document.soap.totaloils.value - 0));

//calculate percentages of total oils 
document.soap.perc1.value = roundOff(((document.soap.weight1.value/document.soap.totalweight.value)*100));
document.soap.perc2.value = roundOff(((document.soap.weight2.value/document.soap.totalweight.value)*100));
document.soap.perc3.value = roundOff(((document.soap.weight3.value/document.soap.totalweight.value)*100));
document.soap.perc4.value = roundOff(((document.soap.weight4.value/document.soap.totalweight.value)*100));
document.soap.perc5.value = roundOff(((document.soap.weight5.value/document.soap.totalweight.value)*100));
document.soap.perc6.value = roundOff(((document.soap.weight6.value/document.soap.totalweight.value)*100));
document.soap.perc7.value = roundOff(((document.soap.weight7.value/document.soap.totalweight.value)*100));
document.soap.perc8.value = roundOff(((document.soap.weight8.value/document.soap.totalweight.value)*100));

//calculate 100% of total oils
document.soap.totalperc.value = 
roundOff((document.soap.perc1.value - 0) + (document.soap.perc2.value - 0) +
(document.soap.perc3.value - 0) + (document.soap.perc4.value - 0) +
(document.soap.perc5.value - 0) + (document.soap.perc6.value - 0) +
(document.soap.perc7.value - 0) + (document.soap.perc8.value - 0));

//calculate after-cure weight
document.soap.cured.value =
roundOff((document.soap.totalwater.value * .15) + (document.soap.totaloils.value - 0) + (document.soap.totallye.value - 0));

}



//read dropdown list text for the selected option - store value in a hidden field
function oilName(object) {
    for (var Current=0;Current < object.oils1.options.length;Current++) {
       
        if (object.oils1.options[Current].selected) 
			{if (object.oils1.options[Current] != object.oils1.options[0]) 
            	object.oilname1.value = object.oils1.options[Current].text;
			 else object.oilname1.value = '';}
			 						 
        if (object.oils2.options[Current].selected) 
			{if (object.oils2.options[Current] != object.oils2.options[0]) 
            	object.oilname2.value = object.oils2.options[Current].text;
			 else object.oilname2.value = '';}
			
		if (object.oils3.options[Current].selected) 
            {if (object.oils3.options[Current] != object.oils3.options[0]) 
            	object.oilname3.value = object.oils3.options[Current].text;
			 else object.oilname3.value = '';}
			  
		if (object.oils4.options[Current].selected) 
            {if (object.oils4.options[Current] != object.oils4.options[0]) 
            	object.oilname4.value = object.oils4.options[Current].text;
			 else object.oilname4.value = '';}
			  
		if (object.oils5.options[Current].selected) 
            {if (object.oils5.options[Current] != object.oils5.options[0]) 
            	object.oilname5.value = object.oils5.options[Current].text;
			 else object.oilname5.value = '';}
			
		if (object.oils6.options[Current].selected) 
            {if (object.oils6.options[Current] != object.oils6.options[0]) 
            	object.oilname6.value = object.oils6.options[Current].text;
			 else object.oilname6.value = '';}
			  
		if (object.oils7.options[Current].selected) 
            {if (object.oils7.options[Current] != object.oils7.options[0]) 
            	object.oilname7.value = object.oils7.options[Current].text;
			 else object.oilname7.value = '';}
			
		if (object.oils8.options[Current].selected) 
            {if (object.oils8.options[Current] != object.oils8.options[0]) 
            	object.oilname8.value = object.oils8.options[Current].text;
			 else object.oilname8.value = '';}
			
			
		if (object.superfat1.options[Current].selected) 
            {if (object.superfat1.options[Current] != object.superfat1.options[0]) 
            	object.sfoilname1.value = object.superfat1.options[Current].text;
			 else object.sfoilname1.value = '';}
			 
		if (object.superfat2.options[Current].selected) 
            {if (object.superfat2.options[Current] != object.superfat2.options[0]) 
            	object.sfoilname2.value = object.superfat2.options[Current].text;
			 else object.sfoilname2.value = '';}
			
		if (object.superfat3.options[Current].selected) 
            {if (object.superfat3.options[Current] != object.superfat3.options[0]) 
            	object.sfoilname3.value = object.superfat3.options[Current].text;
			 else object.sfoilname3.value = '';}

    }
}



//create printable recipe page
function printRecipe() {


//check units of measure and lye type for display on recipe page
var ozcheck = document.soap.units[0].checked;
var poundcheck = document.soap.units[1].checked;
var gramcheck = document.soap.units[2].checked;
var printweight;
var NaOH = document.soap.lyes[0].checked;
var KOH = document.soap.lyes[1].checked;
var lyetype;

if (ozcheck == true)
	printweight = document.soap.units[0].value;
	else if (poundcheck == true)
		printweight = document.soap.units[1].value;
		else
			printweight = document.soap.units[2].value;
if (NaOH ==true)
	lyetype = document.soap.lyes[0].value;
	else if(KOH == true)
	lyetype = document.soap.lyes[1].value;			
			
       
       recipe = window.open("", "recipeWindow", "toolbar=no,width=500,height=600,status=no,menubar=yes,resize=yes, scrollbars=yes")
 
       with (recipe.document) {
          write()
          close()
          write("<HTML>")
          write("<TITLE>Print je recept</TITLE><BODY BGCOLOR=#CDFFFF>")
		  write("<table width='425' border='0' cellspacing='2' cellpadding='2' align='center'>")
		  write("<tr><td bgcolor='#339A99' colspan=10 align='center'><font face='Lucida Sans' color='#010101'></b>www.maakzelfzeep.nl</b></td></tr>")
  		  write("<tr><td bgcolor='#339A99' colspan=5 align='center'><font face='Lucida Sans' color='#010101'><b>" + document.soap.recipeName.value + "</b></td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'><b>Basis olie</b></td><td bgcolor='#FFFFFF'><b>" + printweight + "</b></td><td bgcolor='#FFFFFF'>&nbsp;</td><td  align='right' bgcolor='#FFFFFF'><b>Overvet olie</b></td><td align='right' bgcolor='#FFFFFF'><b>" + printweight + "</b></td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>" + document.soap.oilname1.value + "</td><td bgcolor='#FFFFFF'>" + document.soap.weight1.value + "</td><td bgcolor='#FFFFFF'>&nbsp;</td><td align='right' bgcolor='#FFFFFF'>" + document.soap.sfoilname1.value + "</td><td align='right' bgcolor='#FFFFFF'>" + document.soap.sfweight1.value + "</td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>" + document.soap.oilname2.value + "</td><td bgcolor='#FFFFFF'>" + document.soap.weight2.value + "</td><td bgcolor='#FFFFFF'>&nbsp;</td><td align='right' bgcolor='#FFFFFF'>" + document.soap.sfoilname2.value + "</td><td align='right' bgcolor='#FFFFFF'>" + document.soap.sfweight2.value + "</td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>" + document.soap.oilname3.value + "</td><td bgcolor='#FFFFFF'>" + document.soap.weight3.value + "</td><td bgcolor='#FFFFFF'>&nbsp;</td><td align='right' bgcolor='#FFFFFF'>" + document.soap.sfoilname3.value + "</td><td align='right' bgcolor='#FFFFFF'>" + document.soap.sfweight3.value + "</td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>" + document.soap.oilname4.value + "</td><td bgcolor='#FFFFFF'>" + document.soap.weight4.value + "</td><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF'>&nbsp;</td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>" + document.soap.oilname5.value + "</td><td bgcolor='#FFFFFF'>" + document.soap.weight5.value + "</td><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF' colspan=2><hr></td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>" + document.soap.oilname6.value + "</td><td bgcolor='#FFFFFF'>" + document.soap.weight6.value + "</td><td bgcolor='#FFFFFF'>&nbsp;</td><td align='right' bgcolor='#FFFFFF'><b>" + document.soap.liquid.value + ":</b></td><td bgcolor='#FFFFFF' align='right'>" + document.soap.totalwater.value + "</td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>" + document.soap.oilname7.value + "</td><td bgcolor='#FFFFFF'>" + document.soap.weight7.value + "</td><td bgcolor='#FFFFFF'></td><td align='right' bgcolor='#FFFFFF'><b>Totaal loog voor 0% resterende olie</b><font size='2'>("+lyetype+")</font></td><td  bgcolor='#FFFFFF' align='right'>" + document.soap.totallye.value + "</td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>" + document.soap.oilname8.value + "</td><td bgcolor='#FFFFFF'>" + document.soap.weight8.value + "</td><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF' align='right'><b>Totaal loog voor 5% resterende olie</b><font size='2'>("+lyetype+")</font></td><td bgcolor='#FFFFFF' align='right'>" + document.soap.totallye2.value + "</td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF' align='right'><b>Totaal loog voor 10% resterende olie</b><font size='2'>("+lyetype+")</font></td><td bgcolor='#FFFFFF' align='right'>" + document.soap.totallye3.value + "</td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF' align='right'><b>Olie:</b></td><td bgcolor='#FFFFFF' align='right'>" + document.soap.totaloils.value + "</td></tr>")
		  write("<tr><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF'>&nbsp;</td><td bgcolor='#FFFFFF' align='right'><b>Totaal gewicht:</b></td><td bgcolor='#FFFFFF' align='right'>" + document.soap.total.value + "</td></tr>")
		  write("<tr><td bgcolor='#FFFFFF' colspan=5><b>Geur/kleur/toevoegingen/notities:</b><pre>" + document.soap.notes.value + "</pre></td></tr>")
		  write("<tr><td colspan=5><hr><b>Om het recept te printen, klik met de rechtermuisknop, en kies Afdrukken.</b><br><input type='button' value='Sluiten' onClick='self.close();'></td></tr></table>")
          write("</BODY></HTML>")

       }
    }
