//validate the whole form

function calculate(theForm) {
    var why = "";
    why += isEmpty(theForm.butter.value, "butter price");
    why += isEmpty(theForm.nonfatdrymilk.value, "non-fat dry milk price");
    why += isEmpty(theForm.cheese.value, "cheese price");
    why += isEmpty(theForm.drywhey.value, "dry whey price");
    if (why != ""){
       alert(why);
       return false;
    }

    alert(createCalculation(theForm.butter.value, theForm.nonfatdrymilk.value, theForm.cheese.value, theForm.drywhey.value));
    return false;
}

// textbox - check for emptiness

function isEmpty(strng, item){
var error = "";
  if (strng == "") {
     error = "Please enter a " + item + ".\n";
  }
return error;	  
}

function createCalculation(butter, nonfatdrymilk, cheese, drywhey) {
	var butterMA = 0.1202;
	var nonfatdrymilkMA = 0.157;
	var drywheyMA = 0.1956;
	var cheeseMA = 0.1682;
	var butterfatPRO = 1.17;
	var butterYF = 1.2;
	var nonfatdrymilkYF = 0.99;
	var drywheyYF = 1.03;
	var cheeseYF = 1.383;
	var butterfatYF = 1.572;
	var class3OSYF = 5.9;
	var class3PROYF = 3.1;
	var class3SKIMYF = 0.965;
	var class3BFATYF = 3.5;
	var class4NFSYF = 9;
	var class4SKIMYF = 0.965;
	var class4BFATYF = 3.5;
	var netButter = 0;
	var netNFDM = 0;
	var netWhey = 0;
	var netCheese = 0;
	var class3Price = 0;
	var class4Price = 0;
	var proteinPrice = 0;
	var L09 = 0;
	var L11 = 0;
	var L16 = 0;
	var L19 = 0;
	var L22 = 0;
	var C16 = 0;
	var C19 = 0;
	var C20 = 0;
	var C22 = 0;
	var C25 = 0;
	var F16 = 0;
	var F18 = 0;
	var F21 = 0;
	var thingToSend = "";

	netButter = (butter - butterMA) * butterYF;
	netNFDM = (nonfatdrymilk - nonfatdrymilkMA) * nonfatdrymilkYF;
	netWhey = (drywhey - drywheyMA) * drywheyYF;

	L09 = (cheese - cheeseMA);
	L11 = L09 * cheeseYF;
	L16 = L09 * butterfatYF;
	L19 = L16  - (netButter * .9)
	L22 = butterfatPRO * L19;

	proteinPrice = L22 + L11;
	C16 = Math.round((netWhey * class3OSYF) * 100) / 100;
	C19 = Math.round((proteinPrice * class3PROYF) * 100) / 100;
	C20 = C16 + C19;
	C22 = Math.round((C20 * class3SKIMYF) * 100) / 100;
	C25 = Math.round((class3BFATYF * netButter) * 100) / 100;
	class3Price = C25 + C22;

	F16 = Math.round((class4NFSYF * netNFDM) * 100) / 100;
	F18 = Math.round((class4SKIMYF * F16) * 100) / 100;
	F21 = Math.round((netButter * class4BFATYF) * 100) / 100;
	
	class4Price = F18 + F21;

	thingToSend = "Projected Class 3 Price: $" + class3Price + ".\n";
	thingToSend += "Projected Class 4 Price: $" + class4Price + ".\n";

return thingToSend;

}
