sm = 1;

function calcshipping () {
	
	zipcodev = document.Forminfo.zipc.value;
	ziptot = zipcodev.substring(0,5);
	zippre = zipcodev.substring(0,3);
	ratea = new Array(40);
	var CurrentIndex = document.Forminfo.shipgroup.selectedIndex;	// check the value of the selected index
	var CurrentValue = document.Forminfo.shipgroup.options[CurrentIndex].value;	// check the value of the selected value

	if( zippre == "967" ||  zippre == "968" )
	{
		file2speczone(CurrentValue);
	}
	else if( zippre == "995" ||  zippre == "996" || zippre == "997" ||  zippre == "998" || zippre == "999" )
	{
		file3speczone(CurrentValue);
	}
	else
	{
		filezone ();
	}
								
	gwgt = (PA[sizepos] [paperpos] [0] * document.Forminfo.quant.value) - .0001;
	gwgt = gwgt + ((gwgt / 40) * 1);
	wgt = (gwgt / 40);
	npack =  Math.ceil(wgt);

	if (npack < 1) {
		npack = 1;
	}

	forties = npack - 1;
	overs = Math.round(gwgt - ((npack - 1) * 40));
	rate = 0;
	rate1 = 0;
	rate2 = 0;

	if (npack > 1) {
		fullbox = ratea[39];
		rate1 = ((npack - 1) * fullbox);
	}

	if (overs > 0) {
		rate2 = (ratea[overs -1]);
	}
		
	if (gwgt <= SDAL[0]) {
		addrate = SDA[0]
	} else {
		if (gwgt > SDAL[0] && gwgt <= SDAL[1]) {
			addrate = SDA[0] - ((SDA[0] - SDA[1]) * ((gwgt - SDAL[0]) / (SDAL[1] - SDAL[0])));
		} else {
			addrate = SDA[1];
		}
	}
	//store box and weight  info:
	document.Forminfo.shipbox.value =  format_num(gwgt)+'_'+npack+'_'+overs;
 	
	rate = Math.round((((rate1 + rate2 + 1.4) * addrate) * SHIPMU) * 100) / 100;

	document.Forminfo.hshipcost.value = format_num(rate);
	document.getElementById("real_shipping_cost").innerHTML = "$" + format_num(rate);
	document.getElementById("summary_shipping_cost").innerHTML = "$" + format_num(rate);
	
}

function calcprintprice () {
	if (document.Forminfo.quant.value < BASEQ) {
		document.Forminfo.quant.value = BASEQ;
	}
	//if (document.Forminfo.zipc.value > 0) {
		quote_status("not_saved"); //to reset the save status whenever user re-calculate the print price
		
		paperpos = document.Forminfo.paperpos.value;
		binkpos = document.Forminfo.binkpos.value;
		foldpos = document.Forminfo.foldpos.value;
		proofpos = document.Forminfo.proofpos.value;
		sizepos = document.Forminfo.sizepos.value;
		rushrate = document.Forminfo.rushrate.value;

		var varnish_pos = document.Forminfo.varnpos.value;
		
		sm = document.Forminfo.sm.value;
		apos = 0;

		if (document.Forminfo.zipc.value != "")
		{
			calcshipping();
			oldrate = rate;
		}

		quant = document.Forminfo.quant.value;
		document.Forminfo.hquant.value = quant;

		if ( quant < DRV[7] ) {
			for (i=0;i<8;i++) {
				if ( quant >= DRV[i] && quant < DRV[i+1] ) {
					sprd = DRV[i + 1] - DRV[i]
					bpd = DR[sizepos] [binkpos] [i + 1] - DR[sizepos] [binkpos] [i];
					aunitp = bpd / sprd;
					aunits = quant - DRV[i];
					totpprc = DR[sizepos] [binkpos] [i] + (aunitp * aunits);
					i = 8;
				}
			}
		//since the pricing is limited to quantity 20000, so if the quantity > 20000, the availage will be made in this else case:
		} else {
			sprd = DRV[7] - DRV[6];		//quantity 20,000 - 15,000, get the quantity different
			bpd = DR[sizepos] [binkpos] [7] - DR[sizepos] [binkpos] [6];	//get the price different for 20,000 & 15,000
			aunitp = bpd / sprd;	//get the price for quantity = 1 (unit price)
			aunits = quant - DRV[7];	//get the quantity different between the real quantity and the biggest quantity 20,000
			totpprc = DR[sizepos] [binkpos] [7] + (aunitp * aunits);	//price of 20,000 + (extra quantity X unit price)
		} 

		totpprc = totpprc * $_adjuctment[this_product]; //print price % adjustment

		bp = (totpprc + (PA[sizepos] [paperpos] [1] * quant)) + calc_varnish(parseInt(varnish_pos), sizepos, quant);

		proofprc = PR [binkpos] [sizepos] [proofpos];
		
		document.Forminfo.hproofcost.value = format_num(proofprc);

		if (paperpos >= 5) {
			fi = 1;
		} else {
			fi = 0;
		}
				
		if (foldpos > 0) {
			FOLDPRC = (FA[fi] [0] * quant) + FA[fi] [1];
		} else {
			FOLDPRC = 0;
		}

		foldprc = (FOLDPRC * rushrate) * UPRATE;

		document.Forminfo.hfoldcost.value = format_num(foldprc);
					
		baseprc = (bp * rushrate);

		totpprc = baseprc * UPRATE;

		if (QDISRATE = "Y") { 
			qda = calcdis(totpprc + foldprc);
		} else {
			qda = 0;
		}
		//document.Forminfo.price.value = format_num(totpprc - qda);

		//to get the rush price different:
		var rushDiff = totpprc - (totpprc / rushrate);

		totpprc = totpprc - qda;

		document.Forminfo.hrushcost.value = format_num(rushDiff);
		document.Forminfo.hprintcost.value = format_num(totpprc);

		gtotal = ((totpprc + foldprc) + proofprc);
		
		//------ calc promo ------
		gtotal = calcpromo(7,gtotal,quant);
		promo_saving = document.Forminfo.promoda.value;
		document.getElementById("summary_promo_saving").innerHTML = "$" + format_num(promo_saving);
		
		//------ fill subtotal info. (both with and without zip situation) ------
		document.getElementById("real_sub_total").innerHTML = "$" + format_num(gtotal);	//sub-total
		document.getElementById("summary_sub_total").innerHTML = "$" + format_num(gtotal);	//sub-total summary

		u_subtotal = gtotal / quant;	//calculate subtotal unit price
		document.getElementById("real_unit_price").innerHTML = "$" + format_num(u_subtotal) + " ea.";//sub-total unit price
		document.getElementById("summary_quantity").innerHTML = quant;	//quantity summary
		
		if (document.Forminfo.zipc.value == "")
		{
			document.getElementById("real_sub_total_top").innerHTML = "$" + format_num(gtotal);	//sub-total price at top
		}
		
		else
		{
			gtotal = gtotal + rate;
			utotal = gtotal / quant;	//total unit price
			document.getElementById("real_sub_total_top").innerHTML = "$" + format_num(gtotal);	//total price at top
			document.getElementById("real_total_price").innerHTML = "$" + format_num(gtotal);	//total
			document.getElementById("real_unit_price_total").innerHTML = "$" + format_num(utotal) + " ea.";	//total unit price
			document.getElementById("summary_total_price").innerHTML = "$" + format_num(gtotal);	//total summary
			document.getElementById("summary_unit_price").innerHTML = "$" + format_num(utotal) + " ea.";//total unit price summary
			document.Forminfo.htotalcost.value = format_num(gtotal);
			
		}
		
		calculateAdditionalQuantity(quant, "increase_quantity");
}

function calcaddprintprice (addQuantity) {

		paperpos1 = document.Forminfo.paperpos.value;
		binkpos1 = document.Forminfo.binkpos.value;
		foldpos1 = document.Forminfo.foldpos.value;
		proofpos1 = document.Forminfo.proofpos.value;
		sizepos1 = document.Forminfo.sizepos.value;
		rushrate1 = document.Forminfo.rushrate.value;
		
		var varnish_pos = document.Forminfo.varnpos.value;
		
		quant1 = addQuantity;

		if ( quant1 < DRV[7] ) {
			for (i=0;i<8;i++) {
				if ( quant1 >= DRV[i] && quant1 < DRV[i+1] ) {
					sprd1 = DRV[i + 1] - DRV[i]
					bpd1 = DR[sizepos1] [binkpos1] [i + 1] - DR[sizepos1] [binkpos1] [i];
					aunitp1 = bpd1 / sprd1;
					aunits1 = quant1 - DRV[i];
					totpprc1 = DR[sizepos1] [binkpos1] [i] + (aunitp1 * aunits1);
					i = 8;
				}
			}
		} else {
			sprd1 = DRV[7] - DRV[6];
			bpd1 = DR[sizepos1] [binkpos1] [7] - DR[sizepos1] [binkpos1] [6];
			aunitp1 = bpd1 / sprd1;
			aunits1 = quant1 - DRV[7];
			totpprc1 = DR[sizepos1] [binkpos1] [7] + (aunitp1 * aunits1);
		}
		
		// online pricing use this part, but since the this_product get set on xyz_general.js, this need to be here

		this_product = 'brochures';
		
		totpprc1 = totpprc1 * $_adjuctment[this_product]; //print price % adjustment

		bp1 = (totpprc1 + (PA[sizepos1] [paperpos1] [1] * quant1)) + calc_varnish(parseInt(varnish_pos),sizepos1,quant1);

		proofprc1 = PR [binkpos1] [sizepos1] [proofpos1];
		
		if (paperpos1 >= 5) {
			fi = 1;
		} else {
			fi = 0;
		}
				
		if (foldpos1 > 0) {
			FOLDPRC1 = (FA[fi] [0] * quant1) + FA[fi] [1];
		} else {
			FOLDPRC1 = 0;
		}

		foldprc1 = (FOLDPRC1 * rushrate1) * UPRATE;
					
		baseprc1 = (bp1 * rushrate1);

		totpprc1 = baseprc1 * UPRATE;

		if (QDISRATE = "Y") { 
			qda1 = calcdis(totpprc1 + foldprc1);
		} else {
			qda1 = 0;
		}
		
		
		gtotal1 = ((totpprc1 + foldprc1) + proofprc1) - qda1;

		utotall = gtotal1 / quant1;
				
		var quan_unit_total = Array(format_num_vary(gtotal1, 0), format_num(utotall));
		
		return quan_unit_total;
}