
// **************************************************************
// This page lives in two locations:
// - local webserver: /CommonWeb/JavaScript/InstPrice/
// - tiera.net webserver: /htdocs/JavaScript/InstaPrice/
// Please distribute in both locations.
// **************************************************************
// This file contains functions used for product pages only
// StandardValidation.js should be called prior to this script

// Pricing arrays
var minimumQuantityArray        = new Array();
var minimumPriceArray           = new Array();
var minimumPrice2ndVersionArray = new Array();
var multiplyEachArray           = new Array();
var multiplyEachArray5000       = new Array();
var multiplyEachArray10000      = new Array();
var multiplyEachArray20000      = new Array();
var aqueousPriceArray           = new Array();
var aqueousEachArray            = new Array();
var sealArray       	        = new Array();
var shipArray                   = new Array();
var shipTypeArray               = new Array();
var handleArray                 = new Array();
var shipPaperArray              = new Array();
var paperTypeBaseArray          = new Array();
var paperTypeArray              = new Array();
var insidepaperTypeArray        = new Array();
var secondSheetsArray           = new Array();
var envelopePriceArray 	        = new Array();
var roundcornersArray           = new Array();

// Mailing arrays
var gMailingMethod = null;
var mailingMinimumPriceArray = new Array();
var mailingMultiplyEachArray = new Array();
var mailingMultiplyEachArray5000 = new Array();
var mailingMultiplyEachArray10000 = new Array();
var mailingPostageCostArray = new Array();
var mailingMinQtyArray = new Array();
var tabbingEachArray = new Array();
var tabbingEachArray5000 = new Array();
var tabbingEachArray10000 = new Array();
var paperWeightArray = new Array();
var numOfPagesArray = new Array();
var mailingHalfFoldSurchargeBM = 0;
var mailingHalfFoldSurcharge1CP = 0;
var mailingHalfFoldSurcharge1C = 0;
var mailingNoFoldSurchargeBM = 0;
var mailingNoFoldSurcharge1CP = 0;
var mailingNoFoldSurcharge1C = 0;
var mailingTriFoldSurchargeBM = 0;
var mailingTriFoldSurcharge1CP = 0;
var mailingTriFoldSurcharge1C = 0;
var mailingTriThenHalfFoldSurchargeBM = 0;
var mailingTriThenHalfFoldSurcharge1CP = 0;
var mailingTriThenHalfFoldSurcharge1C = 0;
var mailingFourPanelFoldSurchargeBM = 0;
var mailingFourPanelFoldSurcharge1CP = 0;
var mailingFourPanelFoldSurcharge1C = 0;
var mailingCoverStockSurchargeBM = 0;
var mailingCoverStockSurcharge1CP = 0;
var mailingCoverStockSurcharge1C = 0;

// Channel arrays
var channelID = null;
var minimumPriceArrayC = new Array();
var minimumPrice2ndVersionArrayC = new Array();
var multiplyEachArrayC = new Array();
var multiplyEachArray5000C = new Array();
var multiplyEachArray10000C = new Array();
var multiplyEachArray20000C = new Array();
var aqueousPriceArrayC = new Array();
var aqueousEachArrayC = new Array();
var sealArrayC = new Array();
var shipPaperArrayC = new Array();
var paperTypeBaseArrayC = new Array();
var paperTypeArrayC = new Array();
var insidepaperTypeArrayC = new Array();
var secondSheetsArrayC = new Array();
var handleArrayC = new Array();
var envelopetypeint = 0;
var roundcornersArrayC = new Array();
var pricingPercentArrayC = new Array();

var versionCt = 1;

//-----------------------------------------------------------------------------

var AdjustPricesCount = 0;
function AdjustPrices(){
	var channelID = null;
	if (document.price.channelID && document.price.channelID.value != '' && document.price.DisplayChannelPricing) {
		channelID = parseInt(document.price.channelID.value);
	}
	AdjustBasePrice(channelID);
	AdjustPaperPrice(channelID);
	AdjustAquaPrice(channelID);
	if (gProduct == 22 || gProduct == 23) { //booklet envelopes
		AdjustSealPrice(channelID);
	}	
	AdjustFoldPrice();
     if (document.price.roundcornersPrice) {
	   AdjustRoundCornersPrice();
	}
	if (gProduct == 27 || gProduct == 28 || gProduct == 12 || gProduct == 13) { //calendar & catalog
		AdjustInsidePagesPrice(channelID);
		AdjustInsidePaperPrice(channelID);
	}
	AdjustSubTotalPrice(channelID);
	AdjustPPCopyPrice();
	AdjustEnvelopePrice(channelID);
	if (gProduct == 11 || gProduct == 17) { //letterhead and letterhead/envelopes
		AdjustSecondSheetsPrice(channelID);
	}
	AdjustShipPrice(channelID);
	AdjustRushPrice(channelID);
	if (site == "Channel" || site == "LW" || site == "OM" || site == "ST") {
			AdjustMarkup();
	}
	if (site != "DO" && site != "MN" && site != "LW") {
		AdjustMailingPrice();
	}
	//AdjustTotalPrice();
	if (document.price.pp2copyPrice) {
		AdjustPP2CopyPrice(channelID);
	}
	//by Vu
	if (document.price.discountedPrice) {
		AdjustDiscountedPrice();
				
	}
	AdjustTotalPrice();
	
	if (document.price.finalTotalPrice && document.price.finalUnitCost) {
	    AdjustFinalPrice();
	}
	if (window.AdjustChangePrices) AdjustChangePrices();
	
	if (document.price.couponDiscount && document.price.couponDiscount != '')
	{
		var discountedPrice = document.price.totalPrice.value - document.price.couponDiscount.value;
		document.price.discountedPrice.value = MakeCents(discountedPrice*100);
	}
	
	//check that totalPrice is a number (This MUST be the last call in AdjustPrices())
	if (isNaN(document.price.totalPrice.value) && (AdjustPricesCount < 10))	{
		AdjustPricesCount++;
		AdjustPrices();
	}
	else { //reset global variable
		AdjustPricesCount = 0;
	}
	//by Vu
	//calcAlternatePrice(document.price.quantity.value);
}

function GetBasePrice(inkIdx, forInsideInk, channelID) {
	var baseP;
	if (forInsideInk && (gProduct == 12 || gProduct == 13) && (gMultiple == 0)) { //catlogsfive or catalogseight
		baseP = 0;
	}
	else {
		var baseP = (channelID && minimumPriceArrayC[channelID]) ? minimumPriceArrayC[channelID][gProduct][inkIdx] : minimumPriceArray[gProduct][inkIdx];
		var qty = parseInt(document.price.quantity.value);
		var minQty = minimumQuantityArray[gProduct];
		var qtyDiff = qty - (minQty*versionCt);
		if (versionCt >= 2) {
			baseP += (versionCt-1) * ((channelID && minimumPrice2ndVersionArrayC[channelID]) ? minimumPrice2ndVersionArrayC[channelID][gProduct][inkIdx] : minimumPrice2ndVersionArray[gProduct][inkIdx]);
		}
		if (qtyDiff > 0) {
			if (qty >= 20000)  {
				baseP += qtyDiff * ((channelID && multiplyEachArray20000C[channelID]) ? multiplyEachArray20000C[channelID][gProduct][inkIdx] : multiplyEachArray20000[gProduct][inkIdx]);
			}
			else if (qty >= 10000) {
				baseP += qtyDiff * ((channelID && multiplyEachArray10000C[channelID]) ? multiplyEachArray10000C[channelID][gProduct][inkIdx] : multiplyEachArray10000[gProduct][inkIdx]);
			}
			else if (qty >= 5000) {
				baseP += qtyDiff * ((channelID && multiplyEachArray5000C[channelID]) ? multiplyEachArray5000C[channelID][gProduct][inkIdx] : multiplyEachArray5000[gProduct][inkIdx]);
			}
			else {
				baseP += qtyDiff * ((channelID && multiplyEachArrayC[channelID]) ? multiplyEachArrayC[channelID][gProduct][inkIdx] : multiplyEachArray[gProduct][inkIdx]);
			}
		}
		if (forInsideInk && gMultiple) {
			baseP *= gMultiple;
		}
	}
	return Math.round(baseP);
}

function AdjustBasePrice(channelID){
	var baseP = GetBasePrice(gInk,null,channelID);
	baseP *= GetPricingPercent(channelID);
	document.price.basePrice.value = MakeCents(baseP);
}

function GetPaperPrice(channelID) {
	var paperP = document.price.quantity.value * ((channelID && paperTypeArrayC[channelID]) ? paperTypeArrayC[channelID][gProduct][gPaper] : paperTypeArray[gProduct][gPaper]);
	if (gProduct == 2 || gProduct == 16) { //Posters
		paperP += ((channelID && paperTypeBaseArrayC[channelID]) ? paperTypeBaseArrayC[channelID][gProduct][gPaper] : paperTypeBaseArray[gProduct][gPaper]);
	}
	return Math.round(paperP);
}

function AdjustPaperPrice(channelID){
	var paperP = GetPaperPrice(channelID);
	paperP *= GetPricingPercent(channelID);
	document.price.paperPrice.value = MakeCents(paperP);
}

function GetAquaPrice(channelID) {
	var aquaP = 0;
	if (gAqua > 0) {
		var qty = parseInt(document.price.quantity.value);
		aquaP = ((channelID && aqueousPriceArrayC[channelID]) ? aqueousPriceArrayC[channelID][gProduct][gInk] : aqueousPriceArray[gProduct][gInk]) + (qty * ((channelID && aqueousEachArrayC[channelID]) ? aqueousEachArrayC[channelID][gProduct][gInk] : aqueousEachArray[gProduct][gInk]));
	}
	if ( (gProduct == 8 && site != 'MN') || (gProduct == 33) ) { //paper choice hidden for MN
		if ((document.price.paperType[2].checked)) {
			aquaP = 0;
		}
	}
	return Math.round(aquaP);
}

function AdjustAquaPrice(channelID){
	var aquaP = GetAquaPrice(channelID);
	aquaP *= GetPricingPercent(channelID);
	document.price.aquaPrice.value = MakeCents(aquaP);
}

//booklet envelopes only
function GetSealPrice(channelID){
	var sealP = 0;
	if (gSeal > 0) {
		var qty = parseInt(document.price.quantity.value);
		sealP = qty * ((channelID && sealArrayC[channelID]) ? sealArrayC[channelID][gProduct][gInk] : sealArray[gProduct][gInk]);
	}
	return Math.round(sealP);
}

//booklet envelopes only
function AdjustSealPrice(channelID){
	var sealP = GetSealPrice(channelID);
	sealP *= GetPricingPercent(channelID);
	document.price.sealPrice.value = MakeCents(sealP);
}

function GetFoldPrice(channelID) {
	var foldP = 0;
	var isCatalogOrCalendar = (gProduct == 27 || gProduct == 28 || gProduct == 12 || gProduct == 13);

	if (gFold > 0 || isCatalogOrCalendar) {
    	var foldCover;
    	var foldEach;

		// Paper is cover and exclude greeting and note cards & holiday cards
		if (IsPaperCover(GetCheckedValue(document.price.paperType)) && gProduct != 8 && gProduct != 33 && gProduct != 47 && gProduct != 48 && gProduct != 49 && gProduct != 50 ) {
			foldCover = 2000;
            foldEach = (isCatalogOrCalendar) ? 3.5 : 2;
		}
		else {
		    foldCover = 0;
            foldEach = (isCatalogOrCalendar && !(gProduct == 13 && gMultiple == 0)) ? 2.5 : 1;
		}

        var qty = document.price.quantity.value;
        foldP = (qty * foldEach) + foldCover;
		if (isCatalogOrCalendar) {
			foldP += (qty * foldEach * gMultiple);
		}
	}

	return Math.round(foldP);
}

function AdjustFoldPrice(){
	var foldP = GetFoldPrice();
	foldP *= GetPricingPercent(channelID);
	document.price.foldPrice.value = MakeCents(foldP);
}

//LogoWorks business cards only
function GetRoundCornersPrice(channelID){
	var roundcornersP = 0;
	if (document.price.roundcorners) {
		if (gRoundCorners > 0) {
		  var qty = parseInt(document.price.quantity.value);
		  roundcornersP = qty * ((channelID && roundcornersArrayC[channelID]) ? roundcornersArrayC[channelID][gProduct][gInk] : roundcornersArray[gProduct][gInk]);
		}
	}
	return Math.round(roundcornersP);
}

function AdjustRoundCornersPrice(){
	if (document.price.roundcornersPrice) {
		var roundcornersP = GetRoundCornersPrice(channelID);
		roundcornersP *= GetPricingPercent(channelID);
		document.price.roundcornersPrice.value = MakeCents(roundcornersP);
	}
}

//for calendars and catalogs only
function AdjustInsidePagesPrice(channelID){
	var insidepagesP = GetBasePrice(gInsideInk, true,channelID);
	insidepagesP *= GetPricingPercent(channelID);
	document.price.insidepagesPrice.value  = MakeCents(insidepagesP);
}

//for calendars and catalogs only
function GetInsidePaperPrice(channelID) {
	var insidepaperP;
	if ((gProduct == 12 || gProduct == 13) && (gMultiple == 0)) { //catlogsfive or catalogseight
		insidepaperP = 0;
	}
	else {
		insidepaperP = document.price.quantity.value * ((channelID && insidepaperTypeArrayC[channelID]) ? insidepaperTypeArrayC[channelID][gProduct][gInsidePaper] : insidepaperTypeArray[gProduct][gInsidePaper]) * gMultiple;
	}
	return Math.round(insidepaperP);
}

//for calendars and catalogs only
function AdjustInsidePaperPrice(channelID) {
	var insidepaperP = GetInsidePaperPrice(channelID);
	insidepaperP *= GetPricingPercent(channelID);
	document.price.insidepaperPrice.value = MakeCents(insidepaperP);
}

function GetSubTotalPrice(channelID) {
	var baseP = GetBasePrice(gInk, false, channelID);
	var aquaP = GetAquaPrice(channelID);
	var paperP = GetPaperPrice(channelID);
	var foldP = GetFoldPrice(channelID);
     
	var forCatalogs = (gProduct == 27 || gProduct == 28 || gProduct == 12 || gProduct == 13);
	var insidepagesP = (forCatalogs) ? GetBasePrice(gInsideInk, true, channelID) : 0;
	var insidepaperP = (forCatalogs) ? GetInsidePaperPrice(channelID) : 0;
	var forBooklets = (gProduct == 22 || gProduct == 23);
     var forRoundCorners = (document.price.roundcornersPrice);
	var sealP = (forBooklets) ? GetSealPrice(channelID) : 0;
     var roundcornersP = (forRoundCorners) ? GetRoundCornersPrice(channelID) : 0;
	return Math.round(baseP + aquaP + paperP + foldP + roundcornersP + insidepagesP + insidepaperP + sealP);
}

function AdjustSubTotalPrice(channelID){
	var subtotalP = GetSubTotalPrice(channelID);
	subtotalP*= GetPricingPercent(channelID);
	document.price.subPrice.value = MakeCents(subtotalP);
}

function AdjustPPCopyPrice(){	
	var ppcopy = document.price.subPrice.value / document.price.quantity.value*100;
	document.price.ppcopyPrice.value  = MakeCents( Math.round( ppcopy ) );		
}

function AdjustEnvelopePrice(){
	if (document.price.envelopes != null) {
		var EnvQ = 0;
		if (document.price.envelopes.value != '')
			EnvQ = document.price.envelopes.value;
		var envelopeP
		if (EnvQ == 0)
		{
			envelopeP = 0;
		}
		else if ( gEnvelope == 0 ) 
			envelopeP = (document.price.envelopes.value) * envelopePriceArray[gProduct][envelopetypeint];
		else
			envelopeP = envelopePriceArray[gProduct][1] + (document.price.envelopes.value) * envelopePriceArray[gProduct][2];
		envelopeP *= GetPricingPercent(channelID);
		document.price.envelopePrice.value = MakeCents(Math.round(envelopeP));
	}
}

//letterhead and letterhead/envelopes only
function GetSecondSheetsPrice(channelID){
	var secondsheetsP = 0;
	if (document.price.secondsheets != null) {
		secondsheetsP = (document.price.secondsheets.value) * ((channelID && secondSheetsArrayC[channelID]) ? secondSheetsArrayC[channelID][gProduct][0] : secondSheetsArray[gProduct][0]);
	}
	return Math.round(secondsheetsP);
}

//letterhead and letterhead/envelopes only
function AdjustSecondSheetsPrice(channelID){
	if (document.price.secondsheets != null) {
		var secondsheetsP = GetSecondSheetsPrice(channelID);
		secondsheetsP *= GetPricingPercent(channelID);
		document.price.secondsheetsPrice.value = MakeCents(secondsheetsP);
	}
}

function AdjustShipPrice(){
	var shipPB = shipArray[gProduct][0];
	var shipPV = (document.price.quantity.value) * (shipArray[gProduct][1]/100);
	if (document.price.MailingQuantity && document.price.MailingQuantity.value != "" && !isNaN(document.price.MailingQuantity.value) && gMailingMethod != null) {
		if (document.price.quantity.value == document.price.MailingQuantity.value) {
			shipPB = 0;
		}
		shipPV -= parseInt(document.price.MailingQuantity.value) * (shipArray[gProduct][1]/100);
	}
	var shipP = shipPB + shipPV;
	if (gProduct == 27 || gProduct == 28 || gProduct == 12 || gProduct == 13) { //calendar & catalog
		shipP *= (shipPaperArray[gProduct][gPaper]) + ((shipPaperArray[gProduct][gInsidePaper]) * gMultiple);
	}
	else if (gProduct == 11 || gProduct == 17) { //letterhead and letterhead/envelopes
		shipP += (document.price.secondsheets.value/2) * (shipArray[gProduct][1]/100);
	}
	else {
		shipP *= (shipPaperArray[gProduct][gPaper]);
	}
	shipP *= (shipTypeArray[gProduct][gShipType]);
	
	if ( document.price.SignatureRequired )
	{
		if ( document.price.SignatureRequired.checked )
		{
			shipP += 300;
		}
	}
	
	document.price.shipPrice.value = MakeCents( Math.round(shipP) );
}

function GetRushPrice(channelID) {
	var rushP = (document.price.subPrice.value) * ((channelID && handleArrayC[channelID]) ? handleArrayC[channelID][gProduct][gHandle] : handleArray[gProduct][gHandle]);
	return Math.round(rushP);
}

function AdjustRushPrice(channelID){
	var rushP = GetRushPrice(channelID);
	//rushP *= GetPricingPercent(channelID);
	document.price.rushPrice.value = MakeCents(rushP);
}

function AdjustMarkup(){ //channel (OfficeMax) only
	var markupP = document.price.subPrice.value*100;
	markupP += document.price.shipPrice.value*100;
	markupP += document.price.rushPrice.value*100;
	if (gProduct == 11 || gProduct == 17) { //letterhead and letterhead/envelopes 
		markupP += document.price.secondsheetsPrice.value*100;
	}
	if (gProduct == 8 || gProduct == 33) { //greeting cards
		markupP += document.price.envelopePrice.value*100;
	}
	markupP *= (markupArray[gProduct][gMarkup]);
	document.price.markupPrice.value = MakeCents( Math.round( markupP ) );
}

function AdjustTotalPrice(){
	var totalP = (document.price.subPrice.value*100);
	//by Vu
	totalP -= (document.price.discountedPrice.value*100);
	totalP += (document.price.shipPrice.value*100);
	totalP += (document.price.rushPrice.value*100);
	if (document.price.FreeOffer) {
		document.price.FreeOffer.value = -49.95;
		totalP += (document.price.FreeOffer.value*100)
	}
	if (site == "Channel" || site == "LW" || site == "OM"  || site == "ST") {
		totalP += (document.price.markupPrice.value*100);
	}
	if (gProduct == 8 || gProduct == 33) { //greeting cards
		totalP += (document.price.envelopePrice.value*100);
	}
	if (gProduct == 11 || gProduct == 17) { //letterhead and letterhead/envelopes
		totalP += (document.price.secondsheetsPrice.value*100);
	}
	if (document.price.mailingPrice) {
		totalP += (document.price.mailingPrice.value*100);
	}
	document.price.totalPrice.value = MakeCents( Math.round(  totalP ) );
	
	if (site == "default" && !isChangeOrderPage) { //no High Volume discount for Channel or DO
		strCookie=document.cookie
		
		var priceMinusMailing;

		if (document.price.mailingPrice) {
			var mailingPrice = MakeCents( Math.round(document.price.mailingPrice.value*100) );
			priceMinusMailing = document.price.totalPrice.value - mailingPrice;
		} else {
			priceMinusMailing = document.price.totalPrice.value;
		}
		if (priceMinusMailing >= 5000 && strCookie.indexOf("volumeDiscount=yes") < 0 )
		{ 
			document.cookie = "volumeDiscount=yes";
			win = window.open('highvolumepopup.html','HighVolume','toolbar=0,location=0,' +
			'directories=0,status=0,scrollbars=0,menubar=0,height=160,width=480,top=150,left=15');
		}
		if (priceMinusMailing < 5000) { 
		    expireCookie();
		}
	}
	if (site == "PB" || site == "MBA") {
	   CheckCoupon();
	}
	
	//by Vu
	return totalP;
}

function AdjustPP2CopyPrice(){	//Channel only
	var pp2copy = document.price.totalPrice.value / document.price.quantity.value*100;
	document.price.pp2copyPrice.value  = MakeCents( Math.round( pp2copy ) );
}

function AdjustMailingPrice(){
	if (document.price.MailingQuantity && document.price.mailingtype) {
		var paperWeightIdx = GetPaperWeightIdx();
		var numOfPagesIdx = GetNumOfPagesIdx(paperWeightIdx);
		var mailingFoldSurcharge = 0;
		
		//brochurestwentyfive and newslettertwentyfive
		if (gProduct == 52 || gProduct == 53) {
			if (gFold == 2) { //tri-fold
				mailingFoldSurcharge = (gMailingMethod == 0) ? mailingTriFoldSurchargeBM : (gMailingMethod == 1) ? mailingTriFoldSurcharge1CP : (gMailingMethod == 2) ? mailingTriFoldSurcharge1C : 0;
			} else if (gFold == 3) { //four-panel
				mailingFoldSurcharge = (gMailingMethod == 0) ? mailingFourPanelFoldSurchargeBM : (gMailingMethod == 1) ? mailingFourPanelFoldSurcharge1CP : (gMailingMethod == 2) ? mailingFourPanelFoldSurcharge1C : 0;	
			} else if (gFold == 4) { //tri-then-half
				mailingFoldSurcharge = (gMailingMethod == 0) ? mailingTriThenHalfFoldSurchargeBM : (gMailingMethod == 1) ? mailingTriThenHalfFoldSurcharge1CP : (gMailingMethod == 2) ? mailingTriThenHalfFoldSurcharge1C : 0;				
			}
		} // all other brochures and newsletters
		// No fold
		else if (gFold == 0) {
			mailingFoldSurcharge = (gMailingMethod == 0) ? mailingNoFoldSurchargeBM : (gMailingMethod == 1) ? mailingNoFoldSurcharge1CP : (gMailingMethod == 2) ? mailingNoFoldSurcharge1C : 0;
		}
		// Half fold
		else if (gFold == 1) {
			mailingFoldSurcharge = (gMailingMethod == 0) ? mailingHalfFoldSurchargeBM : (gMailingMethod == 1) ? mailingHalfFoldSurcharge1CP : (gMailingMethod == 2) ? mailingHalfFoldSurcharge1C : 0;
		}
		//determine if paper is cover stock; if so add coverStockSurcharge to mailingFoldSurcharge
		var stockName = "";
		for (var i = 0; i < document.price.paperType.length; i++) {
			if (document.price.paperType[i].checked) {
				stockName = document.price.paperType[i].value.toLowerCase();
				break;
			}
		}
		if (stockName.indexOf("cover") > -1) {
		   mailingFoldSurcharge += (gMailingMethod == 0) ? mailingCoverStockSurchargeBM : (gMailingMethod == 1) ? mailingCoverStockSurcharge1CP : (gMailingMethod == 2) ? mailingCoverStockSurcharge1C : 0;
		}

		if (document.price.MailingQuantity.value != "" && document.price.MailingQuantity.value != 0 && gMailingMethod != null) {
			var qtyMailing = parseInt( document.price.MailingQuantity.value );
			var mailingP = mailingMinimumPriceArray[gProduct][paperWeightIdx][numOfPagesIdx][gMailingMethod];
			if (qtyMailing >= 10000) {
				mailingP += qtyMailing * (mailingMultiplyEachArray10000[gProduct][paperWeightIdx][numOfPagesIdx][gMailingMethod] + tabbingEachArray10000[gProduct][paperWeightIdx][numOfPagesIdx][gMailingMethod] + mailingFoldSurcharge);
			}
			else if (qtyMailing >= 5000) {
				mailingP += qtyMailing * (mailingMultiplyEachArray5000[gProduct][paperWeightIdx][numOfPagesIdx][gMailingMethod] + tabbingEachArray5000[gProduct][paperWeightIdx][numOfPagesIdx][gMailingMethod] + mailingFoldSurcharge);
			}
			else {
				mailingP += qtyMailing * (mailingMultiplyEachArray[gProduct][paperWeightIdx][numOfPagesIdx][gMailingMethod] + tabbingEachArray[gProduct][paperWeightIdx][numOfPagesIdx][gMailingMethod] + mailingFoldSurcharge);
			}
			document.price.mailingPrice.value = MakeCents( Math.round( mailingP ) );
			
			var postageP = qtyMailing * (mailingPostageCostArray[gProduct][paperWeightIdx][numOfPagesIdx][gMailingMethod] + mailingFoldSurcharge);
			document.price.postageFee.value = MakeCents( Math.round( postageP ) );
			
		}
		else {
			document.price.mailingPrice.value = "0.00";
			document.price.postageFee.value = "0.00";
		}
	}
}
//by Vu
//function AdjustDiscountedPrice() {
//	if (document.price.channelID && document.price.channelID.value != '') {
//		var channelID = parseInt(document.price.channelID.value);
//		var subtotalP = GetSubTotalPrice(channelID);
//		var secondsheetsP = GetSecondSheetsPrice(channelID);
//		var rushP = GetRushPrice(channelID);
//		var shipP = Math.round(100 * document.price.shipPrice.value);
//		if (document.price.subPriceC) {
//			document.price.subPriceC.value = MakeCents(subtotalP);
//		}
//		if (document.price.secondsheetsPriceC) {
//			document.price.secondsheetsPriceC.value = MakeCents(secondsheetsP);
//		}
//		if (document.price.rushPriceC) {
//			document.price.rushPriceC.value = MakeCents(rushP);
//		}
//		if (document.price.shipPriceC) {
//			document.price.shipPriceC.value = MakeCents(shipP);
//		}
//		document.price.discountedPrice.value = MakeCents(subtotalP + secondsheetsP + rushP + shipP);
//	}
//}

//by Vu

function AdjustDiscountedPrice() {
	var discountPercent;
	
	//discountPercent = getDiscount(document.price.productTypeInt.value);

	if (document.price.magneticCard)
		discountPercent = getDiscount("55");	
	else 
		discountPercent = getDiscount(document.price.productTypeInt.value);
	
	var subP = document.price.subPrice.value;
	var discount = (subP*discountPercent);
	var ie=document.all
	var ns6=document.getElementById && !document.all
	var discountCell=document.all? document.all["discountField"] : document.getElementById? document.getElementById("discountField") : "";
	discountCell.innerHTML = (discountPercent*100) + '% Discount';
	
	document.price.discountedPrice.value = MakeCents(discount*100);
	
}

function AdjustFinalPrice() {
    document.price.finalTotalPrice.value = MakeCents(Math.round((100.0 * document.price.totalPrice.value) - (100.0 * document.price.couponAmount.value)));
    document.price.finalUnitCost.value = MakeCents(Math.round(document.price.finalTotalPrice.value / document.price.quantity.value * 100));
}

function ChangeProduct(product){
	orgProduct = gProduct;
	//statement stuffers fold check
	if (gProduct == 34 || gProduct == 35 || gProduct == 36 || gProduct == 37 || gProduct == 38 || gProduct == 39 || gProduct == 40) {
		//statement stuffers (3.5 x 8.5, 3.5 x 7 & 5.25 x 8.5 cannot be folded, must be No fold)
		if ( (product == 34 || product == 38 || product == 37) && !gFold == 0) {
			alert('This size statement stuffer cannot be folded');
			SetProductRadio(orgProduct); //set back to previous product
		}
		//statement stuffers (7 x 8.5 & 7 x 7 cannot select tri or z fold)
		else if ( (product == 35 || product == 39) && (gFold == 2 || gFold == 3)) {
			alert('You cannot select a tri or z fold with this size statement stuffer');
			SetProductRadio(orgProduct); //set back to previous product
		}
		else { //no folding checks - AdjustPrices
			gProduct = product;
			document.price.productTypeInt.value = gProduct;
			AdjustPrices();
		}
	}
	else { //all other products (non statement stuffers)
		gProduct = product;
		document.price.productTypeInt.value = gProduct;
		if (gProduct == 27 || gProduct == 28) { //calendar only
			ChangeMultiples();
		}
		//remove Bulk Mail in mailing services default for 4 x 6 Postcards and 4.25 x 5.5 Postcards
		if (document.price.mailingtype) {
			if (gProduct == 3 || gProduct == 20) {

				if (document.price.mailingtype[0].checked) 
				{
					if (gProduct == 3) alert('Bulk Mail is not available for 4 x 6 Postcards.');
					if (gProduct == 20) alert('Bulk Mail is not available for 4.25 x 5.5 Postcards.');
					document.price.mailingtype[1].checked = true;  //first class pre-sort
					ChangeMailingType(1,true);
				}
				document.price.mailingtype[0].disabled = true;
			} else {
				if (document.price.mailingtype[0].disabled) {
					document.price.mailingtype[0].disabled = false;
				}
			}
		}
		AdjustPrices(); 
	}
}

function SetProductRadio(product) {
	if (product == 34) { document.price.papersize[0].checked = true; }
	if (product == 35) { document.price.papersize[1].checked = true; }
	if (product == 36) { document.price.papersize[2].checked = true; }
	if (product == 37) { document.price.papersize[3].checked = true; }
	if (product == 38) { document.price.papersize[4].checked = true; }
	if (product == 39) { document.price.papersize[5].checked = true; }
	if (product == 40) { document.price.papersize[6].checked = true; }
}

function ChangePaper(paper){
     //do not allow Aqueous coating on 80# dull/matte for brochures
     if ( (gProduct == 0 || gProduct == 14 || gProduct == 1 || gProduct == 53) && paper == 4 && gAqua == 2) {
		alert('Aqueous coating is not available on 80# dull/matte cover stock.');
		document.price.aqueous[1].checked=true;
		gAqua = 0;
     }
     //do not allow Aqueous coating on 70# uncoated text for newsletters
     else if ( (gProduct == 9 || gProduct == 10 || gProduct == 52) && paper == 2 && gAqua == 2) {
		alert('Aqueous coating is not available on 70# uncoated text.');
		document.price.aqueous[1].checked=true;
		gAqua = 0;
     }
	//do not allow Aqueous coating on 80# matte text for brochures and newsletters
    else if ( (gProduct == 0 || gProduct == 1 || gProduct == 9 || gProduct == 10 || gProduct == 14 || gProduct == 52 || gProduct == 53) && paper == 1 && gAqua == 2) {
		alert('Aqueous coating is not available on 80# matte text.');
		document.price.aqueous[1].checked=true;
		gAqua = 0;
	}
	else if ( (gProduct == 53) && (gFold == 4) && (paper == 3 || paper == 4) ){
	    alert('You cannot select a cover stock paper with a tri-then-half fold.');
	    document.price.paperType[gPaper].checked = true;
	    paper = gPaper;
	}
	gPaper = paper;
	AdjustPrices();
}
function ChangeFold(fold){
	//statement stuffers (3.5 x 8.5, 3.5 x 7 & 5.25 x 8.5 cannot be folded, must be No fold)
	if ( (gProduct == 34 || gProduct == 38 || gProduct == 37) && !fold == 0) {
		alert('This size statement stuffer cannot be folded');
		document.price.folds[gFold].checked = true;
	}
	//statement stuffers (7 x 8.5 & 7 x 7 cannot select tri or z fold)
	else if ( (gProduct == 35 || gProduct == 39) && (fold == 2 || fold == 3)) {
		alert('You cannot select a tri or z fold with this size statement stuffer');
		document.price.folds[gFold].checked = true;
	}
	else if ( (gProduct == 53) && (fold == 4) && (gPaper == 3 || gPaper == 4) ){
		alert('You cannot select a tri-then-half fold with cover stock paper');
		document.price.folds[gFold-2].checked = true;
	}
	else {
		gFold = fold;
		changeImage("foldImage", foldImageArray[fold]);
		CheckFoldMailingRules();
		CheckMailingQuantity();		
		AdjustPrices();
	}
}
function ChangeInk(ink){
	gInk = ink;
	changeImage("inkImage", inkColorsArray[gInk]);
	AdjustPrices();
}
//for catalogs and calendars only
function ChangeInsideInk(insideink){
	gInsideInk = insideink;
	changeImage("insideinkImage", insideinkColorsArray[insideink]);
	AdjustPrices();
}
//for catalogs and calendars only
function ChangeInsidePaper(insidepaper){
	gInsidePaper = insidepaper;
	AdjustPrices();
}
function ChangeAqua(aqua){
     //do not allow Aqueous coating on 80# dull/matte for brochures
     if ( (gProduct == 0 || gProduct == 14 || gProduct == 1 || gProduct == 53) && gPaper == 4 && aqua == 2) {
		alert('Aqueous coating is not available on 80# dull/matte cover stock.');
		document.price.aqueous[1].checked=true;
		gAqua = 0;
     }
     //do not allow Aqueous coating on 70# uncoated text for newsletters
     else if ( (gProduct == 9 || gProduct == 10 || gProduct == 52) && gPaper == 2 && aqua == 2) {
		alert('Aqueous coating is not available on 70# uncoated text.');
		document.price.aqueous[1].checked=true;
		gAqua = 0;
     }
	//do not allow Aqueous coating on 80# matte text for brochures and newsletters
    else if ( (gProduct == 0 || gProduct == 1 || gProduct == 9 || gProduct == 10 || gProduct == 14 || gProduct == 52 || gProduct == 53) && gPaper == 1 && aqua == 2) {
		alert('Aqueous coating is not available on 80# matte text.');
		document.price.aqueous[1].checked=true;
		gAqua = 0;
	}
     else {
	    gAqua = aqua;
	    AdjustPrices();
     }
}
function ChangeRoundCorners(roundcorners){
    gRoundCorners = roundcorners;
    AdjustPrices();
}
//booklet envelopes only
function ChangeSeal(seal){
	gSeal = seal;
	AdjustPrices();
}
function ChangeEnvelopes(){
	var e = parseInt(document.price.envelopes.value);
	if (isNaN(e)) document.price.envelopes.value = "";
	else document.price.envelopes.value = e;
	CheckMailingQuantity();
	AdjustPrices();
}
//letterhead and letterhead/envelopes
function ChangeSecondSheets(){
	var e = parseInt(document.price.secondsheets.value);
	if (isNaN(e)) document.price.secondsheets.value = "";
	else document.price.secondsheets.value = e;

	AdjustPrices();
}
function ChangeHandling(handle){
	gHandle = handle;
	AdjustPrices();
}
function ChangeShipType(shiptype){
	gShipType = shiptype;
	AdjustPrices();
}
function ChangeMarkup(markup){ //Channel only
	gMarkup = markup;
	AdjustPrices();
}

function ChangeEnvelopeImage(envelope, validate){
	gEnvelope = envelope;
	changeImage("envelopeImage", envelopeImageArray[envelope]);
	if (validate) {
		CheckMailingQuantity();
		AdjustPrices();
	}
}

function ChangeQuantity() {
     //Mathnasium Greeting Cards must be ordered in bundles of 25
     if (site == "MN" && gProduct == 8) {
	   if (document.price.quantity.value % 25 != 0) { //mod 25
		  alert("You must order in increments of 25.");
		  document.price.quantity.value = 25;
		  document.price.quantity.focus;	   
	   }
     }
	// Business cards handling
	if (gProduct == 7) { ChangeNameQuantity(); return }

	//var q = parseInt(document.price.quantity.value);
     var q = document.price.quantity.value;
     q = q.replace(",", "");
     q = parseInt(q);
	if (q < minimumQuantityArray[gProduct] || isNaN(q)) {
		document.price.quantity.value = minimumQuantityArray[gProduct];
	}
	else {
		document.price.quantity.value = q;
	}
	CheckEnvelopeQuantity();
	CheckMailingQuantity();
	AdjustPrices();
}
function ChangeNameQuantity() {
    //Validate Vertical Lend 2 box limit (revalidated in validateDesignOnline in StandardValidation.js
    if (site == "VL" && document.price.name1qty.value > 2) {
		alert("You are limited to two boxes.");
	     document.price.name1qty.value = 2;
		document.price.name1qty.focus();
	}

	// The following is for business cards where there can be more than
	// one name, and orders are in fixed increments, i.e. boxes of 500.
	with (document.price) {
		var q = parseInt(quantity.value);
		if (isNaN(q)) q = minimumQuantityArray[gProduct];
		// Use minimum quantity value as the increment value
		var increment = minimumQuantityArray[gProduct];

		var sum = 0;
		var q1  = 0;
		var q2  = 0;
		versionCt = 0;
		if (name1qty.value != "") {
			q1 = parseInt(name1qty.value,10);
			q2 = name1qty.value-0
			if (q1!=q2) name1qty.value = ""; 
			else {
				sum += q1 * increment;
				name1qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name2qty.value != "") {
			q1 = parseInt(name2qty.value,10);
			q2 = name2qty.value-0
			if (q1!=q2) name2qty.value = "";
			else {
				sum += q1 * increment;
				name2qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name3qty.value != "") {
			q1 = parseInt(name3qty.value,10);
			q2 = name3qty.value-0
			if (q1!=q2) name3qty.value = "";
			else {
				sum += q1 * increment;
				name3qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name4qty.value != "") {
			q1 = parseInt(name4qty.value,10);
			q2 = name4qty.value-0
			if (q1!=q2) name4qty.value = "";
			else {
				sum += q1 * increment;
				name4qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name5qty.value != "") {
			q1 = parseInt(name5qty.value,10);
			q2 = name5qty.value-0
			if (q1!=q2) name5qty.value = "";
			else {
				sum += q1 * increment;
				name5qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name6qty.value != "") {
			q1 = parseInt(name6qty.value,10);
			q2 = name6qty.value-0
			if (q1!=q2) name6qty.value = "";
			else {
				sum += q1 * increment;
				name6qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name7qty.value != "") {
			q1 = parseInt(name7qty.value,10);
			q2 = name7qty.value-0
			if (q1!=q2) name7qty.value = "";
			else {
				sum += q1 * increment;
				name7qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name8qty.value != "") {
			q1 = parseInt(name8qty.value,10);
			q2 = name8qty.value-0
			if (q1!=q2) name8qty.value = "";
			else {
				sum += q1 * increment;
				name8qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name9qty.value != "") {
			q1 = parseInt(name9qty.value,10);
			q2 = name9qty.value-0
			if (q1!=q2) name9qty.value = "";
			else {
				sum += q1 * increment;
				name9qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name10qty.value != "") {
			q1 = parseInt(name10qty.value,10);
			q2 = name10qty.value-0
			if (q1!=q2) name10qty.value = "";
			else {
				sum += q1 * increment;
				name10qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name11qty.value != "") {
			q1 = parseInt(name11qty.value,10);
			q2 = name11qty.value-0
			if (q1!=q2) name11qty.value = "";
			else {
				sum += q1 * increment;
				name11qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (name12qty.value != "") {
			q1 = parseInt(name12qty.value,10);
			q2 = name12qty.value-0
			if (q1!=q2) name12qty.value = "";
			else {
				sum += q1 * increment;
				name12qty.value = q1;
				if (q1 > 0) versionCt++;
			}
		}
		if (versionCt < 1) versionCt = 1;
		// Assure quantity is at least the sum of individual name orders
		if (sum > 0) {
			q = sum;
		} 
		else {
			if (q <= 0) q = 1;	// set default

			// Assure quantity is an even increment (rounded up)
			q = Math.floor( (q + (increment - 1)) / increment) * increment;
		}
		quantity.value = q;
	}
	AdjustPrices();
}

function ChangePrice() {
	// Price fields are readonly, so recompute if user changes them.
	AdjustPrices();
}

function ChangeMailingType(mailingMethod,donotcheckmailingqty) {
	gMailingMethod = mailingMethod;
	
	if (CheckFoldMailingRules()) {
	if (!donotcheckmailingqty) {
	CheckMailingQuantity();
	}
	AdjustPrices();
}
}

function ChangeMailingQuantity() {
    if (document.price.MailingQuantity.value != "" && (isNaN(document.price.MailingQuantity.value) || (document.price.MailingQuantity.value.indexOf(" ") != -1))) {
        alert("Quantity to be mailed must be numeric.");
	    document.price.MailingQuantity.value = "";
		HandleUncheckMailingType();
    }
    else {
	    CheckMailingQuantity();
	}
	var divuploadtextelem = document.getElementById('divuploadtext');
	if (divuploadtextelem){
		divuploadtextelem.style.display = (document.price.MailingQuantity.value == "") ? 'none' : '';
	}
    AdjustPrices();
}

function CheckFoldMailingRules(){
    if ((gProduct == 52 || gProduct == 53 )) {
		var mailingQty = (document.price.MailingQuantity.value != "");
		var mailingChecked = (document.price.mailingtype[0].checked || document.price.mailingtype[1].checked || document.price.mailingtype[2].checked);
		if ((gFold == 5) && (mailingChecked || mailingQty)) {
			alert('Mailing Services are not available for a Gate Fold');
			gFold = 2
			document.price.folds[gFold-2].checked = true;
			return (false);
		}
    }
    return (true); //okay to go!
}

function CheckEnvelopeQuantity() {
	if ( gProduct == 8 || gProduct == 33 )
	{
		if (document.price.envelopes.value != '' && document.price.envelopes.value != 0)
		{
			document.price.envelopes.value = document.price.quantity.value;
		}
	}
}

function CheckMailingQuantity() {
	if (document.price.MailingQuantity) {
		var q = parseInt(document.price.quantity.value);
		if (document.price.MailingQuantity.value > q) {
			alert("Quantity to be mailed cannot be greater than order quantity.");
			document.price.quantity.value = document.price.MailingQuantity.value;
			document.price.quantity.focus();
		}

		if (gMailingMethod != null) {
			var paperWeightIdx = GetPaperWeightIdx();
			var numOfPagesIdx = GetNumOfPagesIdx(paperWeightIdx);
			var mq = mailingMinQtyArray[gProduct][paperWeightIdx][numOfPagesIdx][gMailingMethod];
			if (document.price.MailingQuantity.value != "" && document.price.MailingQuantity.value != 0 && document.price.MailingQuantity.value < mq) {
				var bmMinQty = mailingMinQtyArray[gProduct][paperWeightIdx][numOfPagesIdx][0];
				if (document.price.mailingtype[1].checked) //1st Class-Presorted
				{
					alert("The minimum quantity for 1st Class-Presorted rate is " + mq + ".");
					if (document.price.MailingQuantity.value >= bmMinQty) {

						if (gProduct == 3 || gProduct == 20) {
							//bulk mail is not an option for 4x6 postcards and 4.25x5.5 postcards
							document.price.mailingtype[2].checked = true; //single rate - 1st Class
							ChangeMailingType(2,true);
						} else {
						document.price.mailingtype[0].checked = true; //Bulk Mail
						ChangeMailingType(0,true);
						}
					} else {
						document.price.mailingtype[2].checked = true; //single rate - 1st Class
						ChangeMailingType(2,true);
					}
				}
				else if (document.price.mailingtype[0].checked) //Bulk Mail
				{
					alert("The minimum quantity for Bulk Mail rate is " + bmMinQty + ".");
					document.price.mailingtype[2].checked = true; //single rate - 1st Class
					ChangeMailingType(2,true);
				}
			}

			// for Greeting Cards and Note Cards we must have enough printed envelopes to mail the cards
			if ((gProduct == 8 || gProduct == 33) && document.price.MailingQuantity.value != '' && document.price.MailingQuantity.value > 0 )
			{
				var msg;
				msg = "";
				if ( gEnvelope == 0 )
				{
					msg = "Printed Envelopes are required for mailing.";
					gEnvelope = 1;
					document.price.envelope[1].checked = true;
					ChangeEnvelopeImage(1,false);
				}
				if ( document.price.envelopes.value == '' || parseInt(document.price.envelopes.value) < parseInt(document.price.MailingQuantity.value) )
				{
					msg += "\nMailing quantity cannot be greater than the envelope quantity";
					document.price.envelopes.value = document.price.MailingQuantity.value;	
				}
				if ( gFold == 0 )
				{
					msg += "\nHalf fold is required for mailing.";
					document.price.folds[1].checked = true;
					gFold = 1;
					changeImage("foldImage", foldImageArray[gFold]);
				}
				if ( msg != "" )
				{
					alert(msg);
				}
			}
		}

		if (document.price.shiptype && document.price.shiptype.length > 0) {
			var allMailing = (document.price.quantity.value == document.price.MailingQuantity.value && gMailingMethod != null);
			for (var i = 0, lenIter = document.price.shiptype.length; i < lenIter; i++) {
				if (allMailing && document.price.shiptype[i].checked) {
					document.price.shiptype[i].checked = false;
				}
				if (document.price.shiptype[i].disabled != allMailing) {
					document.price.shiptype[i].disabled = allMailing;
				}
			}
			//determine if a ship type is chosen.
			var shiptypeCheck = false;
			for (i = 0; i < lenIter; i++) {
				if (!(allMailing) && document.price.shiptype[i].checked) {
					shiptypeCheck = true;
					break;
				}
			}
			//if this order contains both mailing and shipping and no ship type is checked, default is ground
			if (!(allMailing) && !(shiptypeCheck)) {
				document.price.shiptype[0].checked = true;
			}
		}
		
		HandleUncheckMailingType();
	}
}

function HandleUncheckMailingType() {
	//if the mailing quantity is empty, uncheck all mailing type choices
	if (document.price.mailingtype && document.price.MailingQuantity.value == '') {
		for (var i = 0, loopCnt = document.price.mailingtype.length; i < loopCnt; i++) {
			document.price.mailingtype[i].checked = false;
		}
		ChangeMailingType(null, true);
	}
}

//-----------------------------------------------------------------------------
//for business cards only
function DefaultNameQuantity(name, nameqty) {
	if(name.value != "") {
		nameqty.value=1
	}
	else {
		nameqty.value=0
	}
	ChangeNameQuantity();
}

//-----------------------------------------------------------------------------
//for catalogs and calendars only
function ChangeMultiples(){
	if (gProduct == 27 || gProduct == 28) { //calendars
		if (gProduct == 27) gMultiple = 2.75;
		else gMultiple = 6;
		document.price.multiplesamount.value = gMultiple;
	}
	else if (gProduct == 12) { //catlogsfive
		var f = document.price.c5multiplesamount;
		gMultiple = f.options[f.selectedIndex].value;
		if (!gMultiple) { gMultiple = 1; }

		CheckMultiplesValidity();

		changeImage("firstPages", firstPagesArray[f.selectedIndex]);
		AdjustPrices();	
	}
	else if (gProduct == 13) { //catalogseight
		var f = document.price.multiplesamount;
		gMultiple = f.options[f.selectedIndex].value;
		if (!gMultiple) { gMultiple = 1; }

		CheckMultiplesValidity();

		AdjustPrices();
	}
}

function CheckMultiplesValidity() {
	// Have the Inside Pages buttons or section grayed-out until a page count other than the first option in Number of Pages dropdown is selected
	var firstNumOfPagesOption = (gMultiple == 0);
	if (firstNumOfPagesOption) {
		document.price.insideink[0].checked = true;
		ChangeInsideInk(0);
		document.price.insidepaperType[0].checked = true;
		ChangeInsidePaper(0);
	}
	SetCollectionStatus(document.price.insideink, firstNumOfPagesOption);
	SetCollectionStatus(document.price.insidepaperType, firstNumOfPagesOption);
}
//-----------------------------------------------------------------------------

function checkPaperType() {
	var papertypeGroup = document.price.paperType;
	//booklet envelopes
	if (gProduct == 22 || gProduct == 23) {
		if ((papertypeGroup[1].checked) && (document.price.sealType[1].checked)) {
			alert('Peel & Seel is not available on 24 lb stock.');
			document.price.sealType[0].checked=true;
			ChangeSeal(0);
		}
	}
}

function checkSealType() { //booklet envelopes only
	var sealtypeGroup = document.price.sealType
	if ((sealtypeGroup[1].checked) && (document.price.paperType[1].checked)) { //check seal type for paper
		alert('Peel & Seel is not available on 24 lb stock.');
		document.price.paperType[0].checked=true;
		ChangePaper(0);
	}
	else if ((gProduct == 23) && (document.price.paperType[1].checked)) {  //check paper type for product
		alert('9 1/2 x 12 5/8 envelopes are not available in 24 lb stock.');
		document.price.paperType[0].checked=true;
		ChangePaper(0);
	}
}

function checkPaperType2() { //booklet envelopes only
	var papertypeGroup = document.price.paperType
	if ((papertypeGroup[1].checked) && (gProduct == 23)) {
		alert('9 1/2 x 12 5/8 envelopes are not available in 24 lb stock.');
		if (document.price.papersize) {
			document.price.papersize[0].checked=true;
			ChangePaper(0);
		}
	}
}

function checkAqueous() {
	var aqueousGroup = document.price.aqueous;
	//business cards, bookmarks, rack cards, calendars, postcards, doorhangers, 8x11Catalog, 5x8 Catalog
	if (gProduct == 7 || gProduct == 24 || gProduct == 25 || gProduct == 6 || gProduct == 27 || gProduct == 28 || gProduct == 3 || gProduct == 4 || gProduct == 5 || gProduct == 20 || gProduct == 32 || gProduct == 41 || gProduct == 30 || gProduct == 31 || gProduct == 13 || gProduct == 12) {
		if (	((gProduct == 7 || gProduct == 24 || gProduct == 25 || gProduct == 6 || gProduct == 3 || gProduct == 4 || gProduct == 5 || gProduct == 20 || gProduct == 32 || gProduct == 41 || gProduct == 27 || gProduct == 28) && (aqueousGroup[0].checked) && (document.price.paperType[1].checked)) 
		||	( (gProduct == 30 || gProduct == 31) &&  aqueousGroup[0].checked && document.price.paperType[2].checked )
		 ){
			if (gProduct == 27 || gProduct == 28) {
				alert('Aqueous coating is not available on 70# uncoated stock.');
			}
			else {
				alert('Aqueous coating is not available on 100# uncoated cover stock.');
			}
			document.price.aqueous[1].checked=true;
			gAqua = 0;
			if (gProduct == 27 || gProduct == 28) {
				ChangePaper(4); //match pricing array
			}
			else { 
				ChangePaper(1); 
			}
		}
		//business cards, postcards, bookmarks, rackcards, doorhangers
		//do not allow Aqueous on 120# dull/matte
		else if ( 
			 ( (gProduct == 7 || gProduct == 3 || gProduct == 4 || gProduct == 5 || gProduct == 20 || gProduct == 32 || gProduct == 41 || gProduct == 24 || gProduct == 25 || gProduct == 6)
				 && document.price.paperType[2].checked && aqueousGroup[0].checked ) 
			|| ( (gProduct == 30 || gProduct == 31) && document.price.paperType[3].checked && (aqueousGroup[0].checked)) ) {
			
			alert('Aqueous coating is not available on 120# dull/matte cover stock.');
			document.price.aqueous[1].checked=true;
			gAqua = 0;
			ChangePaper(2); 
		}
		//Catalogs and Calendars
		//do not allow Aqueous on 80# dull/matte
		else if ( ( ((gProduct == 13 || gProduct == 12) && document.price.paperType[4].checked) 
				|| ((gProduct == 27 || gProduct == 28) && document.price.paperType[5].checked) ) 
				&& (aqueousGroup[0].checked)) {
			 alert('Aqueous coating is not available on 80# dull/matte cover stock.');
			 document.price.aqueous[1].checked=true;
			 gAqua = 0;
			 if (gProduct == 13 || gProduct == 12) { //8x11 Catalog
				ChangePaper(4); //match pricing array
			 }
			 else { //5x8 or 8x11 Calendars
				ChangePaper(5); 
			 }			
		}
		//Catalogs and Calendars
		//do not allow Aqueous on 80# matte text
		else if ( (gProduct == 13 || gProduct == 12 ||gProduct == 27 || gProduct == 28) 
						&& document.price.paperType[2].checked && (aqueousGroup[0].checked) ) {
			 alert('Aqueous coating is not available on 80# matte text.');
			 document.price.aqueous[1].checked=true;
			 gAqua = 0;
			 ChangePaper(2);			
		}
		else if (gProduct != 27 && gProduct != 28) { //do not need to reset paper for calendars
			for (var i = 0; i < document.price.paperType.length; i++)  { 
				if (document.price.paperType[i].checked)  {
					ChangePaper(i);
					break;
				}
			}
		}
	}
	//statement stuffers
	if (gProduct == 34 || gProduct == 35 || gProduct == 36 || gProduct == 37 || gProduct == 38 || gProduct == 39 || gProduct == 40) {
		if ( aqueousGroup[0].checked && (document.price.paperType[0].checked || document.price.paperType[2].checked || document.price.paperType[4].checked) ){
			if (document.price.paperType[0].checked) {
				alert('Aqueous coating is not available on 70# uncoated stock.');
			}
			else if (document.price.paperType[2].checked) {
				alert('Aqueous coating is not available on 100# uncoated cover stock.');
			}
			else {//if (document.price.paperType[4].checked) {
				alert('Aqueous coating is not available on 120# dull/matte cover stock.');
			}
			document.price.aqueous[1].checked=true;
			gAqua = 0;
			if (document.price.paperType[0].checked) {
				ChangePaper(1);
			}
			else if (document.price.paperType[2].checked) {
				ChangePaper(3);
			}
			else if (document.price.paperType[4].checked) {
				ChangePaper(4);
			}
		}
		else { //reset paper
			for (var i = 0; i < document.price.paperType.length; i++)  { 
				if (document.price.paperType[i].checked)  {
					if (i == 0) { ChangePaper(1); }
					else if (i == 1) { ChangePaper(0); }
					else if (i == 2) { ChangePaper(3); }
					else if (i == 3) { ChangePaper(2); }
					else if (i == 4) { ChangePaper(4); }
					break;
				}
			}
		}
	}
	if (gProduct == 27 || gProduct == 28) { //calendars
		for (var i = 0; i < document.price.paperType.length; i++)  { 
			if (document.price.paperType[i].checked)  {
				if (i == 0) { gPaper = 0; }
				else if (i == 1) { gPaper = 4; }
				else if (i == 2) { gPaper = 1; }
				else if (i == 3) { gPaper = 2; }
				else if (i == 4) { gPaper = 3; }
				else if (i == 5) { gPaper = 5; }
				break;
			}
		}
	}
	else if (gProduct == 13 || gProduct == 12) { //catalogs
		for (var i = 0; i < document.price.paperType.length; i++)  { 
			if (document.price.paperType[i].checked)  {
				if (i == 0) { gPaper = 0; }
				else if (i == 1) { gPaper = 2; }
				else if (i == 2) { gPaper = 1; }
				else if (i == 3) { gPaper = 3; }
				else if (i == 4) { gPaper = 4; }
				break;
			}
		}
	}
	else { //set gPaper
		for (var i = 0; i < document.price.paperType.length; i++)  { 
			if (document.price.paperType[i].checked)  {
				gPaper = i;
				break;
			}
		}

	}
	//set gAqua
	for (var i = 0; i < aqueousGroup.length; i++)  { 
	    if (aqueousGroup[i].checked)  {
		    if (i == 0) { gAqua = 2; }
		    else if (i == 1) { gAqua = 0; }
		    break;
	    }
	}
	AdjustPrices();  //update pricing
}

//-----------------------------------------------------------------------------

function changeImage(locName, imageName) {
	//change the image for all but IE3 and NS2
	if ((navigator.userAgent.indexOf("Mozilla/2"))) {
		document.images[locName].src= imageName;
	}
}

//-----------------------------------------------------------------------------
//filefolders and presentation folders only gProduct = 29, 21
function checkPocket() {
	var pocketsGroup = document.price.PocketType;
	if ((pocketsGroup[0].checked) && (document.price.SlitType[1].checked || document.price.SlitType[2].checked)) {
		alert('Please select a slit option to match your pocket choice.');
		document.price.SlitType[3].checked=true;
	}
	else if ((pocketsGroup[1].checked) && (document.price.SlitType[0].checked || document.price.SlitType[2].checked)) {
		alert('Please select a slit option to match your pocket choice.');
		document.price.SlitType[3].checked=true;
	}
	AdjustPrices();
}

//set all global variables called from body onload 
function setGlobalVariables() {
	//gProduct (only dynamic for posters, calendars, boodmarks, doorhangers, postcards, booklet envelopes, greeting and note cards, statement stuffers)
	if (gProduct == 2 || gProduct == 16 || gProduct == 24 || gProduct == 30 || gProduct == 31 || gProduct == 3 || gProduct == 4 || gProduct == 5 || gProduct == 20 || gProduct == 32 || gProduct == 41 || gProduct == 22 || gProduct == 23 || gProduct == 27 || gProduct == 28 || gProduct == 8 || gProduct == 33 || gProduct == 34 || gProduct == 35 || gProduct == 36 || gProduct == 37 || gProduct == 38 || gProduct == 39 || gProduct == 40) {
		Click(document.price.papersize);
	}

	//gPaper (exclude cdcovers, filefolders, presentation folders, envelopes only, letterhead/envelopes, letterhead only)
	if (gProduct != 15 && gProduct != 29 && gProduct != 21 && gProduct != 19 && gProduct != 11 && gProduct != 17) {
		Click(document.price.paperType);
	}
	
	//gFold (only dynamic for brochureseight, brochureseleven, brochuresfourteen, greeting and note cards, newsletterseight, newsletterseleven, newslettertwentyfive, statement stuffers)
	if (gProduct == 0 || gProduct == 1 || gProduct == 14 || gProduct == 8 || gProduct == 33 || gProduct == 9 || gProduct == 10 || gProduct == 34 || gProduct == 35 || gProduct == 36 || gProduct == 37 || gProduct == 38 || gProduct == 39 || gProduct == 40 || gProduct == 52 || gProduct == 53) {
		Click(document.price.folds);
	}

	//gRoundCorners (only for LogoWorks businesscards)
	if (document.price.roundcorners) {
		Click(document.price.roundcorners);
	}
	
	//gInk (exclude posters, calendars, envelopes only, letterhead/envelopes, letterhead only, booklet envelopes)
	if (gProduct != 2 && gProduct != 16 && gProduct != 27 && gProduct != 28 && gProduct != 19 && gProduct != 11 && gProduct != 17 && gProduct != 22 && gProduct != 23) {
		Click(document.price.back);
	}
	
	//gAqua (exclude presentation folders, envelopes only, letterhead/envelopes, letterhead only, booklet envelopes)
	if (gProduct != 21 && gProduct != 19 && gProduct != 11 && gProduct != 17 && gProduct != 22 && gProduct != 23) {
		Click(document.price.aqueous);
	}
	
	//gInsideInk (only for catalogseight, catalogsfive)
	if (gProduct == 13 || gProduct == 12) {
		Click(document.price.insideink);
	}
	
	//gInsidePaper (only for calendars, catalogseight, catalogsfive)
	if (gProduct == 27 || gProduct == 28 || gProduct == 13 || gProduct == 12) {
		Click(document.price.insidepaperType);
	}
	
	//gSeal (only for booklet envelopes)
	if (gProduct == 22 || gProduct == 23) {
		Click(document.price.sealType);
	}
		
	//gHandle (exclude door hangers, envelopes only)
	if (gProduct != 30 && gProduct != 31 && gProduct != 19) {
		Click(document.price.shipspeed);
	}
		
	//gShipType (for all products)
	Click(document.price.shiptype);
	
	//gMuliple (only for catalogseight, catalogsfive)
	if (gProduct == 13 || gProduct == 12) { 
		ChangeMultiples(); //dropdown
	}

	//gMarkup
	if (site == "Channel" || site == "LW"  || site == "ST") {
			Click(document.price.ChannelMarkupPercent);
	}

	// gMailingMethod
	if (document.price.mailingtype) {
		Click(document.price.mailingtype);
		
		if (gProduct == 3 || gProduct == 20) {
			document.price.mailingtype[0].disabled = true;
		}
	}
}


function GetPaperWeightIdx() {
	if (paperWeightArray[gProduct].length > 0 && document.price.insidepaperType) {
		for (var i = 0, loopCnt = paperWeightArray[gProduct].length; i < loopCnt; i++) {
			if (document.price.insidepaperType[gInsidePaper].value.indexOf(paperWeightArray[gProduct][i] + '#') != -1) {
				return i;
			}
		}
	}
	else {
		return 0;
	}
}

function GetNumOfPagesIdx(paperWeightIdx) {
	var numOfPagesElem = (document.price.c5multiplesamount) ? document.price.c5multiplesamount : (document.price.multiplesamount) ? document.price.multiplesamount : null;
	if (numOfPagesElem && numOfPagesArray[gProduct].length > 0 && document.price.insidepaperType) {
		var numOfPages = parseInt(numOfPagesElem.options[numOfPagesElem.selectedIndex].text);
		for (var i = 0, loopCnt = numOfPagesArray[gProduct][paperWeightIdx].length; i < loopCnt; i++) {
			if (numOfPagesArray[gProduct][paperWeightIdx][i] >= numOfPages) {
				return i;
			}
		}
	}
	else {
		return 0;
	}
}

function SetCollectionStatus(objColl, setDisabled) {
	for (var i = 0, loopCnt = objColl.length; i < loopCnt; i++) {
		objColl[i].disabled = setDisabled;
	}
}

// Return checked value of a collection
function GetCheckedValue(elemObj) {
	if (elemObj) {
		if (elemObj.type == "radio" || elemObj.type == "hidden") {
			return elemObj.value;
		}
		else {
			for (var i = 0; i < elemObj.length; i++) {
				if (elemObj[i].checked) {
					return elemObj[i].value;
				}
			}
		}
	}
}

// Return whether given paper type is cover
function IsPaperCover(paperType) {
    return (typeof(paperType) != 'undefined' && paperType.toLowerCase().indexOf('cover') != -1);
}


function GetPricingPercent(channelID) {
	// by Vu
//	if (channelID && typeof(pricingPercentArrayC[channelID]) != 'undefined' && typeof(pricingPercentArrayC[channelID][gProduct]) != 'undefined') {
//		return pricingPercentArrayC[channelID][gProduct];
//	}
//	else {
//		return 1.0;
//	}

var priceAdjust;
	
	if (document.price.magneticCard)
		priceAdjust = getPriceAdjust("55");	
	else 
		priceAdjust = getPriceAdjust(document.price.productTypeInt.value);

	return priceAdjust;
}
