//////////////////////////////////////////////////////////////
// Hughes Make Ready Calculator functions
// v1.00 (Budd Wright)
//
// Description:
//////////////////////////////////////////////////////////////
// Contains Make Ready calculation functions
//////////////////////////////////////////////////////////////
//
// Usage:
//////////////////////////////////////////////////////////////
// <script language="javascript" src="js/[name].js"></script>
//////////////////////////////////////////////////////////////
//
// v1.00 Notes
// -----------
//
// Known Issues
// ------------
// 
//////////////////////////////////////////////////////////////




// Calculator object
function Calculator()
{
	this.pressCost = 0;
	this.impressionMinutes = 0;
	this.registrationMinutes = 0;
	this.plateCareMinutes = 0;
	this.perColorCost = 0;
	this.colorsPerJob = 0;
	this.costPerJob = 0;
	this.savingsPercentage = .15;
	this.savings = 0;
	this.jobsPerDay = 0;
	this.savingsPerDay = 0;

	this.fields = new function()
	{
		this.pressCost = document.getElementById("pressCost");
		this.impressionMinutes = document.getElementById("impressionMinutes");
		this.registrationMinutes = document.getElementById("registrationMinutes");
		this.plateCareMinutes = document.getElementById("plateCareMinutes");
		this.perColorCost = document.getElementById("perColorCost");
		this.colorsPerJob = document.getElementById("colorsPerJob");
		this.costPerJob = document.getElementById("costPerJob");
		this.savings = document.getElementById("savings");
		this.jobsPerDay = document.getElementById("jobsPerDay");
		this.savingsPerDay = document.getElementById("savingsPerDay");
	}





	// Object methods //
	////////////////////


	// ClearValues method
	this.ClearValues = function()
	{
		this.fields.perColorCost.innerHTML = "&nbsp;";
		this.fields.costPerJob.innerHTML = "&nbsp;";
		this.fields.savings.innerHTML = "&nbsp;";
		this.fields.savingsPerDay.innerHTML = "&nbsp;";

		this.perColorCost = 0;
		this.costPerJob = 0;
		this.savings = 0;
		this.savingsPerDay = 0;
	}



	// IsNumeric method
	this.IsNumeric = function( value )
	{
		if( value.length == 0 )
			return false;
		if( isNaN( value ) )
			return false;

		return true;			
	}




	// Update method
	this.Update = function()
	{
		this.ClearValues();


		// Can we calculate the per color cost yet?
		if
		(
			this.IsNumeric( this.fields.pressCost.value ) &&
			this.IsNumeric( this.fields.impressionMinutes.value ) &&
			this.IsNumeric( this.fields.registrationMinutes.value ) &&
			this.IsNumeric( this.fields.plateCareMinutes.value )
		)
		{
			// Update the values of the calculator
			this.pressCost = this.fields.pressCost.value;
			this.impressionMinutes = this.fields.impressionMinutes.value;
			this.registrationMinutes = this.fields.registrationMinutes.value;
			this.plateCareMinutes = this.fields.plateCareMinutes.value;

			// Caclulate the per color cost
			var perMinuteCost = this.pressCost / 60;
			this.perColorCost = ( this.impressionMinutes * perMinuteCost ) +
								( this.registrationMinutes * perMinuteCost ) +
								( this.plateCareMinutes * perMinuteCost );
			this.perColorCost = Math.round( this.perColorCost * 100 ) / 100;


			this.fields.perColorCost.innerHTML = "$" + this.perColorCost.toFixed(2);
		}
		else
		{
			alert("Some of your values are either blank or not numeric. Please make sure each field has a numeric value and try again.");
			return;
		}
		//- Per color cost


		// Can we calculate the cost per job / savings yet?
		if
		(
			this.IsNumeric( this.fields.colorsPerJob.value ) &&
			this.perColorCost > 0
		)
		{
			// Update the values of the calculator
			this.colorsPerJob = this.fields.colorsPerJob.value;

			// Calculate the cost per job
			this.costPerJob = this.perColorCost * this.colorsPerJob;
			this.costPerJob = Math.round( this.costPerJob * 100 ) / 100;

			// Calculate the savings
			this.savings = this.costPerJob * this.savingsPercentage;
			this.savings = Math.round( this.savings * 100 ) / 100;


			this.fields.costPerJob.innerHTML = "$" + this.costPerJob.toFixed(2);
			this.fields.savings.innerHTML = "$" + this.savings.toFixed(2);
		}
		else
		{
			alert("Some of your values are either blank or not numeric. Please make sure each field has a numeric value and try again.");
			return;
		}
		//- Per job cost / savings


		// Can we calculate the savings per day yet?
		if
		(
			this.IsNumeric( this.fields.jobsPerDay.value ) &&
			this.savings > 0
		)
		{
			// Update the values of the calculator
			this.jobsPerDay = this.fields.jobsPerDay.value;

			// Calculate the savings per day
			this.savingsPerDay = this.savings * this.jobsPerDay;
			this.savingsPerDay = Math.round( this.savingsPerDay * 100 ) / 100;


			this.fields.savingsPerDay.innerHTML = "$" + this.savingsPerDay.toFixed(2);
		}
		else
		{
			alert("Some of your values are either blank or not numeric. Please make sure each field has a numeric value and try again.");
			return;
		}
		//- Savings per day


	}




}
