// JavaScript Document
/*************
Author: Toby Jackson
Date: 19-21st July 2005
Note: This script must be included after the table associated with it
as the update is called as soon as the page loads and if the script
is loaded before the form it cannot find the form to work upon.
*/
update();
function update(){
	f = document.getElementById("calculator");
	setBuyCosts(f);
	costOfBottles = f.numBottles.value * f.costBottles.value;
	costOfBoxes = f.numBoxes.value * f.costBoxes.value;
	costOfSingles = f.numSingles.value * f.costSingles.value;
	costOfSanitation = f.costSanitation.value * 1;//*1 to turn into an int rather than a string... dont ask why, just go with it
	costOfRacks = f.numRacks.value * f.costRacks.value;
	
	var costOfCooler = new Array();
	totalCostOfCoolers = 0;
	for (i = 0; i < f.cooler.length; i++){
		costOfCooler[i] = f.numCooler[i].value * f.cooler[i].value;
		totalCostOfCoolers = totalCostOfCoolers + costOfCooler[i];
	}
	if (totalCostOfCoolers == 0){
		f.costSanitation.value=0;
		costOfSanitation = 0; //No Coolers then no Sanitation
	}
	cost = (costOfBottles + costOfBoxes + costOfSingles + totalCostOfCoolers + costOfSanitation)* f.duration.value;
	extras = costOfRacks;
	f.extrasTotal.value = "£" + Math.round(extras*100)/100;
	f.subtotal.value = "£" + Math.round(cost*100)/100;
	if (f.addExtras.checked){
		value = "£" + Math.round((cost+extras)*100)/100;
	}else{
		value = "£" + Math.round((cost)*100)/100;
	}
	f.finalTotal.value = value;
 switch(f.duration.value)
        {
        case '0.142857':   dur = "Day"; break
        case '1':   dur = "Week"; break
        case '4':   dur = "Month"; break
		case '52':	dur = "Year"; break
        default: dur = "Week";
        }
	 document.getElementById("durationLabel").innerHTML=dur;
}
function setBuyCosts(f){
	/*********************
	Sort out the bottles first */
	numBottles = f.numBottles.value;
	w = f.costBottles;
	if (numBottles == 1){
		w.value = 5.95;
	}else if (numBottles == 2 || numBottles == 3){
		w.value = 5.75;
	}else if (numBottles > 3){
		w.value = 5.50;
	}else{
		w.value = 0;
	}

	/*********************
	Sort out the boxes of cups */
	numBoxes = f.numBoxes.value;
	x = f.costBoxes;
	if (numBoxes == 1){
		x.value = 23.50;
	}else if (numBoxes == 2 ){
		x.value = 22.00;
	}else if (numBoxes >= 3){
		x.value = 20.00;
	}else{
		x.value = 0;
	}

	/*********************
	Singles are always the same price*/
	if (f.numSingles.value > 0)
		f.costSingles.value = 2.45;
	else
		f.costSingles.value = 0;
	
		/*********************
	The cost of a cooler is held in the drop down value */
	for (i = 0; i < f.cooler.length; i++){
		if (f.numCooler[i].value > 0)
			f.costCooler[i].value = f.cooler[i].value;
		else
			f.costCooler[i].value =0;
	}
	f.costSanitation.value = 1.25
	f.costRacks.value = 29.50
}
function showCooler(x){
	addbutton = document.getElementById("addCooler" + x);
	nextButton = document.getElementById("addCooler" + (x+1));
	coolerRow = document.getElementById("Cooler" + x);
	
	addbutton.style.display = "none";
	coolerRow.style.display = "";
	if (nextButton != null)
		nextButton.style.display = "";
}

function addCooler(){
	/*This is out of date, its a perliminary version
	to automatically add the form elements */
  div=document.getElementById("coolerCombo");
	tr = document.createElement("tr");
	td1 = document.createElement("td");
	td2 = document.createElement("td")
	td3 = document.createElement("td");
	td4 = document.createElement("td");
	txt = document.createTextNode("This is row 1, cell 1");
	selectBox = document.createTextNode("This is row 1, cell 1");
			selectBox.nodeValue = "<select name='cooler' class='numField' id='cooler' onChange='update()'><option value='10'>Ebac E-Max</option><option value='11'>Ebac Desktop</option><option value='12'>Ebac Easycooler</option></select>";
		td1.appendChild(selectBox);
	tr.appendChild(td1);
			txt.nodeValue = "Testing";
		td2.appendChild(txt);
	tr.appendChild(td2);
			txt.nodeValue = "Testing";
		td3.appendChild(txt);
	tr.appendChild(td3);
			txt.nodeValue = "Testing";
		td4.appendChild(txt);
	tr.appendChild(td4);
	div.appendChild(tr);
}
function filter(i) {
	//only allows int values and . being entered into the supplied input field
	if(i.value.length>0) {
		i.value = i.value.replace(/[^\d]+/g, ''); 
	}
	//Update the calculations
	update();
}
function dec(x){
	if (x.value == 0)
		return;
	x.value=(x.value*1) - 1;
	filter(x);
}
function inc(x){
	x.value=(x.value*1) + 1;
	filter(x);
}