// ********************************************************************************************
// * File: calc.js
// * Project: 48HrBooks.com
// * Revision History:
// * 15DEC2005 - KMC - Imported from 48HrBooks.com homepage, added CalculateAndDisplay function
// ********************************************************************************************

// ********************************************************************************************
// * GetDiscount
// * Purpose:	Calculate and return the appropriate discount multiplier maintained in the
// *			formulae database.
// *
// * Variables
// * lngQty: Quantity of books
// * arrSelected: The array containing formulae data
// ********************************************************************************************
function GetDiscount( lngQty, arrSelected )
{
	if ( ( lngQty > 0 ) && ( lngQty < 500 ) )
	{
		return parseFloat( arrSelected[ FRM_DISC1 ] );
	}
	else if ( ( lngQty >= 500 ) && ( lngQty < 1000 ) )
	{
		return parseFloat( arrSelected[ FRM_DISC500 ] );
	}
	else if ( ( lngQty >= 1000 ) && ( lngQty < 2500 ) )
	{
		return parseFloat( arrSelected[ FRM_DISC1000 ] );
	}
	else if ( ( lngQty >= 2500 ) && ( lngQty < 5000 ) )
	{
		return parseFloat( arrSelected[ FRM_DISC2500 ] );
	}
	else if ( lngQty >= 5000 )
	{
		return parseFloat( arrSelected[ FRM_DISC5000 ] );
	}
	else
	{
		alert( 'Please enter a valid quantity' );
		return -1;
	}
}

// ********************************************************************************************
// * GetColorPages
// * Purpose:	Calculate and return the appropriate color pages multiplier maintained in the
// *			formulae database.
// *
// * Variables
// * lngNumPagesColor: Number of color pages set by the user
// * lngQty: Quantity of books set by the user
// * arrSelected: The array containing formulae data
// ********************************************************************************************
function GetColorPages( lngNumPagesColor, lngQty, arrSelected )
{
	var lngColorPagesTotal = lngNumPagesColor * lngQty;
	
	if ( lngColorPagesTotal > 0 && lngColorPagesTotal <= 100 )
	{
		return parseFloat( arrSelected[ FRM_COLOR100 ] );
	}
	else if ( lngColorPagesTotal >= 101 && lngColorPagesTotal <= 250 )
	{
		return parseFloat( arrSelected[ FRM_COLOR250 ] );
	}
	else if ( lngColorPagesTotal >= 251 && lngColorPagesTotal <= 500 )
	{
		return parseFloat( arrSelected[ FRM_COLOR500 ] );
	}
	else if ( lngColorPagesTotal >= 501 && lngColorPagesTotal <= 1000 )
	{
		return parseFloat( arrSelected[ FRM_COLOR1000 ] );
	}
	else if ( lngColorPagesTotal >= 1001 && lngColorPagesTotal <= 2500 )
	{
		return parseFloat( arrSelected[ FRM_COLOR2500 ] );
	}
	else if ( lngColorPagesTotal >= 2501 && lngColorPagesTotal <= 5000 )
	{
		return parseFloat( arrSelected[ FRM_COLOR5000 ] );
	}
	else if ( lngColorPagesTotal > 5000 )
	{
		return parseFloat( arrSelected[ FRM_COLOROVER5000 ] );
	}
	else
	{
		return 0.0;
	}
}

// ********************************************************************************************
// * CalculateAndDisplay
// * Purpose:	Determine the estimated order price and price per book... display results
// *			to user via the input box object parameters.
// *
// * Variables
// * objQty: The "Qty" input box object
// * objPrice: The "Price" input box object
// * objPricePer: The "Price Per" input box object
// * lngQty: Quantity of books set by the user
// * lngNumPages: Number of pages set by the user
// * lngNumPagesColor: Number of color pages set by the user
// * arrSelected: The array containing formulae data
// * dblRushMultiplier: The appropriate multiplier for rush 24-48-72 hour pricing
// ********************************************************************************************
function CalculateAndDisplay( objQty, objPrice, objPricePer, lngQty, lngNumPages, lngNumPagesColor, arrSelected, dblRushMultiplier )
{
	var dblDiscount = 0.0;
	var dblColor = 0.0;
	var dblTotal = 0.0;
	var dblResultPer = 0.0;
	
	// Calculate Total
	dblDiscount = GetDiscount( lngQty, arrSelected );
	dblColor = GetColorPages( lngNumPagesColor, lngQty, arrSelected );
	dblTotal = ( ( ( parseInt( arrSelected[ FRM_SETUP ] ) + ( parseFloat( arrSelected[ FRM_PRINT ] ) * lngNumPages * lngQty ) ) + ( lngQty * parseFloat( arrSelected[ FRM_COVERBIND ] ) ) ) * dblDiscount ) + ( ( lngNumPagesColor * lngQty ) * dblColor );
	dblTotal = ( dblRushMultiplier > 0 ) ? dblTotal + ( dblTotal * dblRushMultiplier ) : dblTotal;
	
	// Display results to user -- toFixed works with JavaScript 1.5+
	if ( dblTotal.toFixed )
	{
		// Calculate values
		dblResultPer = dblTotal / lngQty;
		objPrice.value = dblTotal.toFixed(2);
		
		// Display values
		objQty.value = lngQty;
		objPricePer.value = dblResultPer.toFixed(2);
	}
	else
	{
		// Calculate values
		dblResultPer = dblTotal / lngQty;
		dblResultPer = Math.round( dblResultPer * 100 ) / 100;
		objPrice.value = Math.round( dblTotal * 100 ) / 100;
		
		// Display values
		objQty.value = lngQty;
		objPricePer.value = dblResultPer;
	}
}