//################################################
// Coating_Calulator_class.js
// written by Geoff Corriere / CPE
// created 19 -August-2002
// last modified 19 -August-2002
//################################################

//	this class takes 5 parameters
//		system - 'eng' or 'metric' depending on which system you want to use
//		fieldsToCheck - the name of the form fields that are required and need data checking
//		userFieldNames - user-readable names for the fileds in fieldsToCheck
//		bad_data_alert_message - the message that displays if the data the user entered is incorrect
//		form_name - the name of the form


//################################################
// constuctor
//################################################
function Calculator( system, fieldsToCheck, userFieldNames, bad_data_alert_message, form_name )
{
	this.measurement_system = system;	// english or metric
	this.fieldsToCheck = fieldsToCheck;		//	the names of the fields that need to have their data checked
	this.userFieldNames = userFieldNames;	// user-readable names for the fields in fieldsToCheck
	this.bad_data_alert_message = bad_data_alert_message;		// the message the user sees when the data entered is wrong
	this.form_name = form_name;
	
	this.chooseMeasurementSystem();
	//alert( 'leave constructor' );
}


//################################################
// private data members (that are not initialized in the constructor )
//################################################
Calculator.prototype.avg_coating_app_weight_eng = 0.85;
Calculator.prototype.avg_coating_app_weight_metric = 0.0415;
Calculator.prototype.avg_coating_app_weight = 0;	//	will get value from 1 of 2 vars above

Calculator.prototype.sq_in_to_sq_ft = 144;	// convert square inches to square feet
Calculator.prototype.sq_cm_to_sq_m = 100;	// convert square cm to square meters
Calculator.prototype.sq_to_sq_conversion = 0;	//	will get value from 1 of 2 vars above

Calculator.prototype.coating_weight_gallon = 8.6;	
Calculator.prototype.coating_weight_liter = 1.0337;	
Calculator.prototype.coating_weight_volume = 0;	//	will get value from 1 of 2 vars above
Calculator.prototype.total_in_thousands = 1000;	// divide number of sheets to print by 1000 

// measurement system conversion factors
Calculator.prototype.in_to_cm = 2.54;
Calculator.prototype.cm_to_in = 0.393701;
Calculator.prototype.lb_to_kg = 0.453592;
Calculator.prototype.kg_to_lb = 2.20462;
Calculator.prototype.gal_to_ltr = 3.78541;
Calculator.prototype.ltr_to_gal = 0.264172;

Calculator.prototype.query_string = new Array();
Calculator.prototype.form_fields = new Object();

// these vars get their values during calculations
Calculator.prototype.total_sq_in_cm_sheet = 0;
Calculator.prototype.total_sq_ft_m_sheet = 0;
Calculator.prototype.total_sq_ft_m_coated = 0;
Calculator.prototype.total_thousand_sqft_coated = 0;
Calculator.prototype.coating_weight_total = 0;
Calculator.prototype.coating_volume_total = 0;
Calculator.prototype.required_fields = 0;	// this gets set in the checkData() function. 1 = all fields have been set. 0 = some field(s) missing data


//################################################
// private member functions
//################################################

Calculator.prototype.chooseMeasurementSystem = chooseMeasurementSystem;
function chooseMeasurementSystem()
{
	//alert('the system being used is ' + this.measurement_system );
	if( this.measurement_system == 'eng' )
	{
		this.avg_coating_app_weight = this.avg_coating_app_weight_eng;
		this.sq_to_sq_conversion = this.sq_in_to_sq_ft;
		this.coating_weight_volume = this.coating_weight_gallon;
	}
	else
	{
		this.avg_coating_app_weight = this.avg_coating_app_weight_metric;
		this.sq_to_sq_conversion = this.sq_cm_to_sq_m;
		this.coating_weight_volume = this.coating_weight_liter;
	}
	//alert( this.avg_coating_app_weight + "\n" + this.sq_to_sq_conversion + "\n" + this.coating_weight_volume );
}


//================================================

Calculator.prototype.checkData = checkData;						
function checkData()
{			
	// use the "d" instead of typing "document[ this.form_name ]" each and every time
	var d = document[ this.form_name ];
	
	var missingFields = new Array();		// array that holds the names of the form fields that have no value
				
	var message = "";		// the variable that will llist the missing fields in the alert sent to the user
	var z = 0;
				
	// check values of elements in array
	for( var x=0; x < this.fieldsToCheck.length; x++ )
	{
		if( d[ this.fieldsToCheck[x] ].value < 0 || isNaN( d[ this.fieldsToCheck[x] ].value )  )
		{
			missingFields[z++] = this.userFieldNames[x];
		}
	}
				
	if( missingFields.length > 0 )
	{
		// create alert message
		for( x=0; x < missingFields.length; x++ )
		{
			message += missingFields[x] + "\n";
		}
					
		alert( bad_data_alert_message + "\n" + message );
		this.required_fields = 0;
	}
	else
	{
		this.required_fields = 1;
	}
}
		
//================================================
	
Calculator.prototype.doTheMath = doTheMath;				
function doTheMath()
{				
	//alert( 'enter doTheMath' );
	
	// use the "d" instead of typing "document.forms[this.form_name]" each and every time
	var d = document.forms[this.form_name];	
	
	//alert('line 147');
				
	var select_index = d.num_coated_sides.selectedIndex;
	var coated_sides = d.num_coated_sides.options[select_index].text;
	
	//alert('line 152');
				
	// BEGIN MATH CALCULATIONS	
	// get total square inches per sheet
	total_sq_in_cm_sheet = d.sheet_width.value * d.sheet_length.value * coated_sides;
	//alert('total_sq_in_cm_sheet = ' + total_sq_in_cm_sheet );
	// get total square feet per sheet
	total_sq_ft_m_sheet = total_sq_in_cm_sheet / this.sq_to_sq_conversion;
	//alert( 'total_sq_ft_m_sheet = ' + total_sq_ft_m_sheet );
	// get total square feet of all the sheets that will be coated
	total_sq_ft_m_coated = total_sq_ft_m_sheet * d.num_impressions.value;
	//alert( 'total_sq_ft_m_coated = ' + total_sq_ft_m_coated );
	// convert the total square feet of sheet run to 1000s
	total_thousand_sq_ft_m_coated = total_sq_ft_m_coated / this.total_in_thousands;
	//alert( 'total_thousand_sq_ft_m_coated = ' + total_thousand_sq_ft_m_coated );
	// get the number of pounds of coating needed
	coating_weight_total = total_thousand_sq_ft_m_coated * this.avg_coating_app_weight;
	//alert( 'coating_weight_total = ' + coating_weight_total );
	// convert pounds to gallons
	coating_volume_total = coating_weight_total / this.coating_weight_volume;
				
	// PLACE VALUES IN FORM
	d.coating_weight_total.value = formatNumber( coating_weight_total );
	d.coating_volume_total.value = formatNumber( coating_volume_total );
	
	//alert( 'leave doTheMath' );
}
			
//================================================
Calculator.prototype.convertValues = convertValues;
function convertValues()
{
	//alert( 'enter convertValues');
	
	if(this.measurement_system == 'eng')
	{
		//alert( 'convert to eng' );
		this.form_fields["sheet_width"] *= this.cm_to_in;
		this.form_fields["sheet_length"] = this.form_fields["sheet_length"] * this.cm_to_in;
		this.form_fields["coating_weight_total"] = this.form_fields["coating_weight_total"] * this.kg_to_lb;
		this.form_fields["coating_volume_total"] = this.form_fields["coating_volume_total"] * this.ltr_to_gal;
	}
	else
	{
		//alert('convert to metric');
		this.form_fields["sheet_width"] = this.form_fields["sheet_width"] * this.in_to_cm;
		this.form_fields["sheet_length"] = this.form_fields["sheet_length"] * this.in_to_cm;
		this.form_fields["coating_weight_total"] = this.form_fields["coating_weight_total"] * this.lb_to_kg;
		this.form_fields["coating_volume_total"] = this.form_fields["coating_volume_total"] * this.gal_to_ltr;
	}
	//alert( 'leave convertValues');
}

//================================================

Calculator.prototype.displayValuesInForm = displayValuesInForm;
function displayValuesInForm()
{
	document[this.form_name].sheet_width.value = formatNumber( this.form_fields["sheet_width"] );
	document[this.form_name].sheet_length.value = formatNumber( this.form_fields["sheet_length"] );
	document[this.form_name].num_impressions.value = this.form_fields["num_impressions"];
	document[this.form_name].num_coated_sides.selectedIndex = this.form_fields["num_coated_sides"] - 1;
	
	//alert( 'leave displayValuesInForm');
}

//================================================

Calculator.prototype.formatNumber = formatNumber;
function formatNumber( number )	// formats the number to two decimal places. if number = 52.6789...
{
	number *= 100;								// number = 5267.89
	number = Math.round( number );		// number = 5268
	number /= 100;								// number = 52.68
	return number;
}



//################################################
// public member functions
//################################################

Calculator.prototype.calculateTotal = calculateTotal;
function calculateTotal()
{
	this.checkData();
	
	if( this.required_fields == 1 )
	{
		this.doTheMath();
	}
	else
	{
		document.forms[this.form_name].coating_weight_total.value = "";
		document.forms[this.form_name].coating_volume_total.value = "";		
	}
	
}

//================================================

Calculator.prototype.getFormValues = getFormValues;
function getFormValues(  )
{
	//alert( 'enter getFormValues');
	
	if( location.search.length > 0 )
	{
		var the_query = location.search;	// location.search is the section of the url including and after the "?"
		this.query_string = the_query.split( '&' );
		// enter loop
		for( var q = 0; q < this.query_string.length; q++ )	// cycle through this.query_string array
		{
			if( this.query_string[q].indexOf('?') == 0 )		// if '?' is the first character in the string...
			{
				this.query_string[q] = this.query_string[q].slice(1);	// ...remove the '?'
			}
			//alert( this.query_string[q] );
		
			var query_pair = this.query_string[q].split( '=' );	// change string "var_name=value" into array[0]="var_name" array[1]="value"
			
			// compare query_pair[0] to each element in the form this.form_name to see if they match
			for( var s=0; s < document[this.form_name].elements.length; s++ )
			{
				if( query_pair[0] == document[this.form_name].elements[s].name )	// check the name of the var from the query string (e.g. "sheet_width") to the field name in the form
				{
					// set the value of form_fields[ query_pair[0] ] = query_pair[1] 
					this.form_fields[ query_pair[0] ] = query_pair[1];
				}
			}
		}	// end loop
		
		this.convertValues();
		this.displayValuesInForm();
		this.calculateTotal();
	}
	
	//alert( 'leave getFormValues' );
}

//================================================