//sleeve wrapping calculator by Marius Liebenberg written for ITT Jetpak (c) 2008

function calcRollCost(){
	var kgC = document.CALC.kgc.value;
	var rollW = document.CALC.rollw.value;
	
	if(! rollW==""){

	var result =  parseFloat(rollW) * parseFloat(kgC);
	document.CALC.rollc.value = result.toFixed(2);

	}

}


function calcRollW(){
	var canW=document.CALC.canw.value;
	var canD=document.CALC.cand.value;
	var itemsW=document.CALC.itemsw.value;
	var itemsD=document.CALC.itemsd.value;
	var canH=document.CALC.canh.value;
	var shrinkW=document.CALC.shrinkw.value;

	for (var i=0; i < document.CALC.group1.length; i++)
	   {
	   if (document.CALC.group1[i].checked)
	      {
	      var ex = document.CALC.group1[i].value;
	      }
	   }	

	if(! canW=="" && ! itemsW=="" && ! canH=="" && ! shrinkW ==""){

	if(ex==1){
	var result = ((parseFloat(canW) * parseFloat(itemsW)) + (parseFloat(canH) * 0.2) + (parseFloat(canD) * parseFloat(itemsD) * 0.52 ));


	}
	else if(ex==2){
	var result = ((parseFloat(canW) * parseFloat(itemsW)) + (parseFloat(canH) * 0.2) + (parseFloat(canD) * parseFloat(itemsD) * 0.52 )) / (1 + (parseFloat(shrinkW)*0.01) * 0.333);
	}
	else{
	var result = ((parseFloat(canW) * parseFloat(itemsW)) + (parseFloat(canH) * 0.2) + (parseFloat(canD) * parseFloat(itemsD) * 0.52 )) / (1 + (parseFloat(shrinkW)*0.01) * 0.666);
	}
	

	
	
	document.CALC.resultwidth.value = Math.round(result);
	
	}
}


function calcLengthPerPack(){
	var canD=document.CALC.cand.value;
	var canH=document.CALC.canh.value;
	var itemsD=document.CALC.itemsd.value;


	if(! canD=="" && ! canH=="" && ! canH=="" && ! itemsD ==""){

	var lengthPerPack = (((parseFloat(canD)*parseFloat(itemsD)) + parseFloat(canH)) * 2) + 60;
	document.CALC.packl.value = Math.round(lengthPerPack);


	}

}

function calcLengthPerRoll(){
	var filmG = document.CALC.filmg.value;
	var filmS = document.CALC.films.value;
	var rollWth = document.CALC.resultwidth.value;
	var rollW = document.CALC.rollw.value;

	if(! filmG=="" && ! rollWth=="" && ! rollW=="" && ! filmS==""){


	var rollLength = ((((1000000 / parseFloat(filmS)) / parseFloat(rollWth)) / parseFloat(filmG)) * parseFloat(rollW));

	document.CALC.rolll.value = Math.round(rollLength);
	}
	
}


function calcCostPerPack(){
	var rollL= document.CALC.rolll.value;
	var packL = document.CALC.packl.value;
	var rollC = document.CALC.rollc.value;

	if(! rollL=="" && ! packL=="" && ! rollC==""){

		var costPerPack = ((parseFloat(rollC) /  parseFloat(rollL)) * (parseFloat(packL) / 1000 ));
		document.CALC.packc.value = costPerPack.toFixed(3);
	}
}

function calcPacksPerRoll(){
	var packL = document.CALC.packl.value;
	var rollL= document.CALC.rolll.value;

	if(! rollL=="" && ! packL==""){
		var packsPerRoll = ((parseFloat(rollL)*1000)/parseFloat(packL));
		document.CALC.packq.value = Math.round(packsPerRoll);
	}
}

function calcRollDia(){
	var rollWidth = document.CALC.resultwidth.value;
	var filmS = document.CALC.films.value;
	var rollW = document.CALC.rollw.value;

	if(! rollWidth=="" && ! filmS=="" && ! rollW==""){
	var filmCC = 1 / (parseFloat(filmS) * (1 / (parseFloat(rollW)*1000)));
	var coreCC= 78.5398 * (parseFloat(rollWidth)/10);
	//core diameter fixed at 100mm
	
	var totalCC = parseFloat(filmCC) + parseFloat(coreCC);
	
	var rollDiameter = (Math.sqrt((parseFloat(totalCC) / (3.14159 * (parseFloat(rollWidth) /10))))*20);


	//alert (rollRadius);
	document.CALC.rolld.value = Math.round(rollDiameter);

	}
}

function calcPackWidth  (){

	if(! document.CALC.canw.value=="" && ! document.CALC.itemsw.value==""){
		document.CALC.packw.value = parseFloat(document.CALC.canw.value) * parseFloat(document.CALC.itemsw.value);
	}
}

function calcPackDepth  (){

	if(! document.CALC.cand.value=="" && ! document.CALC.itemsd.value==""){

		document.CALC.packd.value = parseFloat(document.CALC.cand.value) * parseFloat(document.CALC.itemsd.value);
	}
}


function costPerCarton(){
	if(! document.CALC.cartonSqm.value=="" && ! document.CALC.boxH.value=="" && ! document.CALC.boxW.value=="" && ! document.CALC.boxL.value==""){
		var boxAreaSide = (parseFloat(document.CALC.boxH.value)/1000) * (parseFloat(document.CALC.boxW.value)/1000);
		var boxAreaFront = (parseFloat(document.CALC.boxH.value)/1000) * (parseFloat(document.CALC.boxL.value)/1000);
		var boxAreaTop = (parseFloat(document.CALC.boxW.value)/1000) * (parseFloat(document.CALC.boxL.value)/1000);
 		var boxAreaTotal = (boxAreaSide *2) + (boxAreaFront *2) + (boxAreaTop * 4);

		var costPerCarton = parseFloat(document.CALC.cartonSqm.value) * boxAreaTotal;	
		document.CALC.cartonCost.value = costPerCarton.toFixed(3);
	
	}
}


function costVariance(){


	if(! document.CALC.itemsw.value=="" && ! document.CALC.itemsd.value=="" ){
		var itemsPerFilm = (parseFloat(document.CALC.itemsw.value) * parseFloat(document.CALC.itemsd.value));

		if(! document.CALC.packc.value==""){

			var filmPerItemCost = (parseFloat(document.CALC.packc.value) / itemsPerFilm);

			if(! document.CALC.itemsPerCarton.value=="" && ! document.CALC.cartonCost.value=="" ){
				var cartonPerItemCost = (parseFloat(document.CALC.cartonCost.value) / parseFloat(document.CALC.itemsPerCarton.value));
				var filmSaving = (100-((filmPerItemCost/cartonPerItemCost)*100));
				document.CALC.filmSaving.value = filmSaving.toFixed(2);

			}

		}

	}
	

	
}



function checkval(cval, obj){
	if(isNaN(cval)) {
	alert("'" + cval + "' - is not a number, please correct.");
	}
	else{
	calcRollW();
	calcLengthPerPack();
	calcRollCost();
	calcLengthPerRoll();
	calcCostPerPack();
	calcPacksPerRoll();
	calcRollDia();
	calcPackWidth  ();
	calcPackDepth  ();
	costPerCarton();
	costVariance();

	}
}



