/******************************************************************************
 *
 *	File:			updateprices.js
 *
 *	Function:		gets values from viweprod form and calculates new prices 
 *					based on user input. Updates HTML fields 'subtotal', 
 *					'total', 'unitprice'.
 *
 *	Author(s):		Carlton M. Remy
 *
 *	Copyright:		Copyright (c) 2002 
 *					All Rights Reserved.
 *
 *	Source:			Started anew.
 *
 *	Notes:			
 *
 *	Change History:
 *			2002-08-31	Started source
 *			2004-04-07  Added turn_cost window
 *			2004-05-19	Rush charges no longer calc'd on proofs
 *			2004-11-03	Bush won, and I added salepricing to the module		
 *	
 *****************************************************************************/
var msgshown = false;
function updatePrices() {


		//numpages gets its value from the form unless it's a pull-down
		if (theForm.numpages != 'hidden') {

				numPages = theForm.numpages.value
	
			} else {
	
				numPages = numPages
		}

		//initialize dynamic vars. static vars are set up in the viewprod file.
		quantity = parseFloat(theForm.quantity.value.split("|")[0]);
		colorConfig = parseFloat(theForm.colors.value.split("|")[0]);
		paperStock = parseFloat(theForm.paperstock.value.split("|")[0]);		
		proofType = parseFloat(theForm.prooftype.value.split("|")[0]);
		turnAround = parseFloat(theForm.turnaround.value.split("|")[0]);
		
		//set up the bindery options values
		var i;
		for(i = 1;i < 9;i++){
			eval('bindery'+i+' = parseFloat(theForm.bindery'+i+'.value.split("|")[0]);bindery'+i+'Thresh = parseFloat(theForm.bindery'+i+'_thresh.value);bindery'+i+'Setup = (bindery'+i+' != 0) ? parseFloat(theForm.bindery'+i+'_setup.value):0 ;');
		}
		
		
		//do the deal
		subTotal = new Number;
		subTotal = baseprice * colorConfig 
		+ (((qprice / 1000) * quantity) * colorConfig)
		+ (paperarea * (paperStock / 1000) * quantity)
		+ bindery1Setup + bindery2Setup + bindery3Setup + bindery4Setup + bindery5Setup + bindery6Setup + bindery7Setup + bindery8Setup
		+ ((bindery1 / bindery1Thresh) * Math.abs(quantity - bindery1Thresh))
		+ ((bindery2 / bindery2Thresh) * Math.abs(quantity - bindery2Thresh))
		+ ((bindery3 / bindery3Thresh) * Math.abs(quantity - bindery3Thresh))
		+ ((bindery4 / bindery4Thresh) * Math.abs(quantity - bindery4Thresh))
		+ ((bindery6 / bindery6Thresh) * Math.abs(quantity - bindery6Thresh))
		+ ((bindery7 / bindery7Thresh) * Math.abs(quantity - bindery7Thresh))
		+ ((bindery8 / bindery8Thresh) * Math.abs(quantity - bindery8Thresh))
		//+ (proofType * numpages);

		
		theForm.subtotal.value = "$" + numToCurr(subTotal + proofType) //.toFixed(2);unfortunately, the .toFixed() method does not yet work for most browsers
		theForm.unitprice.value = "$" + numToFloatFour((subTotal + proofType) / quantity)
		theForm.turn_cost.value = "$" + numToCurr(subTotal * turnAround - subTotal);

		
		if (theForm.saleprice) {
			//if the item is on sale, the sale price goes into total and the original price goes into saleprice
			
			var discount_mult = ((subTotal * turnAround + proofType) > min) ? discount:1;
			var regularprice = subTotal * turnAround + proofType;
			var discounted = regularprice * discount_mult;
			var savings = (regularprice - (discounted));
			if(max != null){
				discounted = (savings < max) ? discounted:regularprice - max;
			}

			theForm.total.value = "$" + numToCurr(discounted);
			theForm.saleprice.value = "$" + numToCurr(regularprice);

		} else {
			theForm.total.value = "$" + numToCurr(subTotal * turnAround + proofType);//(totPrice + (totPrice * turnAround)) //.toFixed(2);		
		}
		//var msgshown = false;
		if((parseInt(theForm.total.value.replace('$','')) >= 4000) && (!msgshown)){
			alert('Jobs over $4,000 qualify for a discount off the website pricing.\n\nCall customer service at 888-894-2810.\n\nYou may continue to place your order, upload files and release your job to print. Please contact us so that your discount may be applied before your credit card is charged.');
			msgshown = true;
		}
		
		
} // update prices
	
function numToCurr(num) {

//formats a number (as string) to currency (a float with 2 point precision)

num = Math.round(num * 100) / 100 + .00001;
num = num.toString();
num = num = num.split(".")[0] + "." + num.split(".")[1].substring(0,2);
return num;


}

function numToFloatFour(num) {

num = Math.round(num * 10000) / 10000 + .0000001;
num = num.toString();
num = num = num.split(".")[0] + "." + num.split(".")[1].substring(0,5);
return num;

}