/******************************************************************************
 *
 *	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		
 *	
 *****************************************************************************/

function updatePricesMulti() {


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

				var numPages = parseInt(theForm.numpages.value);
	
			} else {
	
				var numPages = parseInt(numPages);
		}

		//initialize dynamic vars. static vars are set up in the viewprod file.
		
		var coverColorConfig = parseFloat(theForm.covercolors.value.split("|")[0]);
		var coverPaperStock = parseFloat(theForm.coverpaperstock.value.split("|")[0]);
		if(theForm.coverpaperstock.value.split("|")[2]=='SELF_COVER'){
			coverPaperStock = parseFloat(theForm.paperstock.value.split("|")[0]);
		}
		var coverProoftype = parseFloat(theForm.coverprooftype.value.split("|")[0]);
		
		var quantity = parseFloat(theForm.quantity.value.split("|")[0]);
		var colorConfig = parseFloat(theForm.colors.value.split("|")[0]);
		var paperStock = parseFloat(theForm.paperstock.value.split("|")[0]);		
		var turnAround = parseFloat(theForm.turnaround.value.split("|")[0]);
		
		//set up the bindery options values

		var i;
		for(i = 1;i < 9;i++){
			eval('var 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 ;');
		}
		
		var j;
		for(j = 1;j < 9;j++){
			eval('var coverbindery'+j+' = parseFloat(theForm.coverbindery'+j+'.value.split("|")[0]);coverbindery'+j+'Thresh = parseFloat(theForm.coverbindery'+j+'_thresh.value);coverbindery'+j+'Setup = (coverbindery'+j+' != 0) ? parseFloat(theForm.coverbindery'+j+'_setup.value):0 ;');
		}
		
		
		//do the deal

		var cover = new Number();
		cover = baseprice * coverColorConfig;
		cover += (((qprice / 1000) * quantity) * coverColorConfig);
		cover += (paperarea * (coverPaperStock / 1000) * quantity);
		cover += coverbindery1Setup + coverbindery2Setup + coverbindery3Setup + coverbindery4Setup + coverbindery5Setup + coverbindery6Setup + coverbindery7Setup + coverbindery8Setup;
		cover += ((coverbindery1 / coverbindery1Thresh) * Math.abs(quantity - coverbindery1Thresh));
		cover += ((coverbindery2 / coverbindery2Thresh) * Math.abs(quantity - coverbindery2Thresh));
		cover += ((coverbindery3 / coverbindery3Thresh) * Math.abs(quantity - coverbindery3Thresh));
		cover += ((coverbindery4 / coverbindery4Thresh) * Math.abs(quantity - coverbindery4Thresh));
		cover += ((coverbindery6 / coverbindery6Thresh) * Math.abs(quantity - coverbindery6Thresh));
		cover += ((coverbindery7 / coverbindery7Thresh) * Math.abs(quantity - coverbindery7Thresh));
		cover += ((coverbindery8 / coverbindery8Thresh) * Math.abs(quantity - coverbindery8Thresh));
	
		var subTotal = new Number();
		subTotal = baseprice * colorConfig;
		subTotal += (((qprice / 1000) * quantity) * colorConfig);
		subTotal += (paperarea * (paperStock / 1000) * quantity);
		subTotal += bindery1Setup + bindery2Setup + bindery3Setup + bindery4Setup + bindery5Setup + bindery6Setup + bindery7Setup + bindery8Setup;
		subTotal += ((bindery1 / bindery1Thresh) * Math.abs(quantity - bindery1Thresh));
		subTotal += ((bindery2 / bindery2Thresh) * Math.abs(quantity - bindery2Thresh));
		subTotal += ((bindery3 / bindery3Thresh) * Math.abs(quantity - bindery3Thresh));
		subTotal += ((bindery4 / bindery4Thresh) * Math.abs(quantity - bindery4Thresh));
		subTotal += ((bindery6 / bindery6Thresh) * Math.abs(quantity - bindery6Thresh));
		subTotal += ((bindery7 / bindery7Thresh) * Math.abs(quantity - bindery7Thresh));
		subTotal += ((bindery8 / bindery8Thresh) * Math.abs(quantity - bindery8Thresh));
		
		numPages -= 4;
		//var pd = {1:1, 2:.88, 3:.87, 4:.86, 5:.85, 6:.84, 7:.83, 8:.82, 9:.81, 10:.80, 11:.79, 12:.78, 13:.77, 14:.76, 15:.75 };
		/*
		var ip = document.getElementById('insideprice');
		if(ip) ip.innerHTML = 'before discount: $' + numToCurr(subTotal  * (numPages * .25));
		*/		
		subTotal = (subTotal * (numPages * .25)) * parseFloat(pd[numPages * .25]) ;// - (quantity / (quantity - 1000)); //numPages / 
		
		//add the cover to the subpages
		/*
		var cp = document.getElementById('coverprice');
		if(cp) cp.innerHTML = 'cover: $'+ numToCurr(cover) + '<br />';
		var ip = document.getElementById('insideprice');
		var discnt = 100 - (parseFloat(pd[numPages * .25]*100));
		if(ip) ip.innerHTML += '<br />after discount: $'+ numToCurr(subTotal) + '<br />discount: %' + discnt;
		*/
		subTotal += cover;
		
		theForm.subtotal.value = "$" + numToCurr(subTotal + coverProoftype); //.toFixed(2);unfortunately, the .toFixed() method does not yet work for most browsers
		theForm.unitprice.value = "$" + numToFloatFour((subTotal + coverProoftype) / 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 + coverProoftype) > min) ? discount:1;
			var regularprice = subTotal * turnAround + coverProoftype;
			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 + coverProoftype);//(totPrice + (totPrice * turnAround)) //.toFixed(2);		
		}

		//updateSpecs();
} // update prices multi
	
function numToCurr(num) {

//formats a number (as string) to currency (a float with 2 point precision)
if(isNaN(num)) return num;
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) {

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

}

function updateSpecs(){
	var d = document;
	var f = d.getElementById('features');
	var n = d.getElementById('add-item-form');
	
	//alert(n[2]);
	f.innerHTML = '';
	
	f.innerHTML = '<h2 class="black">Total Price: <span style="color:#c00;">' + n.total.value + '</span></h2>\n';
	for(i=0;i<n.length;i++){
		
		if((n[i].type == 'select-one')){
			//alert(n[i].name+':'+ eval(n[i].name)[n[i].selectedIndex][2]);
			
			f.innerHTML += '<p style="margin:0 0 3px 0;padding:0;color:#369;">'+ n[i].id.replace('-',' ') + '</p><p style="margin:0 0 0 12px;padding:0;color:#666;">'+ n[i].value.split('|')[0]+ ' ' + n[i].value.split('|')[1] + '</p>\n';
		}
	}

	test_weight(n,f);	

}


function test_weight(n,f){
	var coverpaper = (n.coverpaperstock.value.indexOf('SELF_COVER')>-1) ? escape(n.paperstock.value):escape(n.coverpaperstock.value);
	var qs = 'numpages='+parseInt(n.numpages.value)+'&paperstock='+escape(n.paperstock.value)+'&width='+n.width.value+'&height='+n.height.value+'&coverpaperstock='+coverpaper+'&quantity='+n.quantity.value
	
	AjaxRequest.get(
		{
			'url':'/ajax/weight_beta.php?'+qs,
			'onSuccess':function(req){f.innerHTML += '<p style="margin:0 0 3px 0;padding:0;color:#369;">Calculated Weight</p>\n<p style="margin:0 0 0 12px;padding:0;color:#666;">' + req.responseText + '</p>\n' ;} 
		}
	);
}