// function QtyDis(q,p) {
// // Quantity, Price . Fiqure discount for Quantities
// 	if((q<30)&(q>=10))		p = RoundToCent(p*.83);	// 83%
// 	if((q<100)&(q>=30))		p = RoundToCent(p*.73);	// 73%
// 	if(q>=100)				p = RoundToCent(p*.66);	// 66%
// 	return p;
// }

function MatPriceEst() {
	var compName = selectedTextById("CompPop");
	if (compName && compName != "None") {
		var c = selectedIndexById("CompPop");
		var ply = selectedIndexById("PlyPop");
		var color = selectedIndexById("ColorPop");

		var mat = Mat[c].ply[ply].matobs[color][0];
		var os = Mat[c].ply[ply].matobs[color][1];
		
		myFrame.setMatItemNumber(mat.itemno);
		myFrame.setMatOSItemNumber(os.itemno);
		
		myFrame.setMatPrice(mat.price);
		myFrame.setMatPriceOS(os.price);
		myFrame.setMatCutCharge(mat.matcutcharge);
		myFrame.setMatCutChargeOS(os.matcutcharge);
		
		myFrame.setBoardPrice(mat.price);
		myFrame.setBoardPriceOS(os.price);
		myFrame.setBoardCutCharge(mat.boardcutcharge);
		myFrame.setBoardCutChargeOS(os.boardcutcharge);
	} else {
		myFrame.setMatItemNumber("");
		myFrame.setMatOSItemNumber("");
		
		myFrame.setMatPrice(0);
		myFrame.setMatPriceOS(0);
		myFrame.setMatCutCharge(0);
		myFrame.setMatCutChargeOS(0);
		
		myFrame.setBoardPrice(0);
		myFrame.setBoardPriceOS(0);
		myFrame.setBoardCutCharge(0);
		myFrame.setBoardCutChargeOS(0);
	}

	var prc = myFrame.matSubtotal();
	setValueById("$" + CashFormat(myFrame.subTotal(prc)), "MatTotal");

	if (myFrame.hasMat) {
		updateRightSide();
	}
}


function AcrPriceEst() {
	setValueById("$" + CashFormat(myFrame.subTotal(myFrame.acrylicPrice())), "AcrylicTotal");
	if (myFrame.hasAcrylic) {
		updateRightSide();
	}
}

function FoamPriceEst() {
	setValueById("$" + CashFormat(myFrame.subTotal(myFrame.foamboardPrice())), "FoamTotal");
	if (myFrame.hasFoam) {
		updateRightSide();
	}
}

function SelectMat(isIt) {
	myFrame.setHasMat(isIt);
	if (elem = myGetElementById("MatSelect")) {
		if (isIt) {
			elem.className = "selected";
			elem.value = "Remove";
		} else {
			elem.className = "none";
			elem.value = "Add" + (myFrame.pnum ? " to Frame" : "");
		}
	}
	updateRightSide();
}

function SelectAcrylic(isIt) {
	myFrame.setHasAcrylic(isIt);
	if (elem = myGetElementById("AcrylicSelect")) {
		if (isIt) {
			elem.className = "selected";
			elem.value = "Remove";
		} else {
			elem.className = "none";
			elem.value = "Add" + (myFrame.pnum ? " to Frame" : "");
		}
	}
	updateRightSide();
}

function SelectFoam(isIt) {
	myFrame.setHasFoam(isIt);
	if (elem = myGetElementById("FoamSelect")) {
		if (isIt) {
			elem.className = "selected";
			elem.value = "Remove";
		} else {
			elem.className = "none";
			elem.value = "Add" + (myFrame.pnum ? " to Frame" : "");
		}
	}
	updateRightSide();
}

// called when one of the inside dimensions change
function InsideFunky() {
	outside_in = false;
	FigureDimensions();
}

// called when one of the outside dimensions change
function OutsideFunky() {
	outside_in = true;
	FigureDimensions();
}

function FigureDimensions() {
//	alert ("FigureDimensions()");
	
	myFrame.setMatTop(matTop());
	myFrame.setMatLeft(matLeft());
	myFrame.setMatBottom(matBottom());
	myFrame.setMatRight(matRight());

	if (outside_in) {
		myFrame.setHorizontal(parseFloat(valueById("OutsideH")) + 
			parseFloat(selectedValueById("OutsideHPop")));
		setInsideH(myFrame.horizontal - matLeft() - matRight());
		myFrame.setVertical(parseFloat(valueById("OutsideV")) + 
			parseFloat(selectedValueById("OutsideVPop")));
		setInsideV(myFrame.vertical - matTop() - matBottom());
	} else {
		var inH = parseFloat(valueById("InsideH")) + 
			parseFloat(selectedValueById("InsideHPop"));
		myFrame.setHorizontal(inH + matLeft() + matRight());
		setOutsideH(myFrame.horizontal);
		var inV = parseFloat(valueById("InsideV")) + 
			parseFloat(selectedValueById("InsideVPop"));
		myFrame.setVertical(inV + matTop() + matBottom());
		setOutsideV(myFrame.vertical);
	}
	
	MatPriceEst();
}

function matTop() {
	return parseFloat(valueById("Up")) + 
		parseFloat(selectedValueById("UpPop"));
}
function setTop(val) {
	var v = Math.floor(val);
	setValueById(v, "Up");
	setSelectedValueById(val - v, "UpPop");
}
function matBottom() {
	return parseFloat(valueById("Down")) + 
		parseFloat(selectedValueById("DownPop"));
}
function setBottom(val) {
	var v = Math.floor(val);
	setValueById(v, "Down");
	setSelectedValueById(val - v, "DownPop");
}
function matLeft() {
	return parseFloat(valueById("Left")) +
		parseFloat(selectedValueById("LeftPop"));
}
function setLeft(val) {
	var v = Math.floor(val);
	setValueById(v, "Left");
	setSelectedValueById(val - v, "LeftPop");
}
function matRight() {
	return parseFloat(valueById("Right")) +
		parseFloat(selectedValueById("RightPop"));
}
function setRight(val) {
	var v = Math.floor(val);
	setValueById(v, "Right");
	setSelectedValueById(val - v, "RightPop");
}

function setInsideH(hor) {
	var h = Math.floor(hor);
	setValueById(h, "InsideH");
	setSelectedValueById(hor - h, "InsideHPop");
}
function setInsideV(ver) {
	var v = Math.floor(ver);
	setValueById(v, "InsideV");
	setSelectedValueById(ver - v, "InsideVPop");
}

function setOutsideH(hor) {
	var h = Math.floor(hor);
	setValueById(h, "OutsideH");
	setSelectedValueById(hor - h, "OutsideHPop");
}
function setOutsideV(ver) {
	var v = Math.floor(ver);
	setValueById(v, "OutsideV");
	setSelectedValueById(ver - v, "OutsideVPop");
}

function CompFunky() {
	var c = selectedIndexById("CompPop");

	myFrame.setMatboard(selectedTextById("CompPop"));

	buildPop(myGetElementById("PlyPop"), Mat[c].plypop, myFrame.matPly);
	PlyFunky();
}

function PlyFunky() {
	var c = selectedIndexById("CompPop");
	var p = selectedIndexById("PlyPop");

	myFrame.setMatPly(selectedTextById("PlyPop"));

	buildPop(myGetElementById("ColorPop"), Mat[c].ply[p].colpop, myFrame.matColor);
	ColorFunky();
}

function ColorFunky() {
	myFrame.setMatColor(selectedTextById("ColorPop"));
	MatPriceEst();
}

function AcrylicFunky() {
	var acr = selectedIndexById("AcrylicPop");
	myFrame.setAcrylic(selectedTextById("AcrylicPop"));
// 	if (acr > 0) {
		myFrame.setAcrPrice(gAcrylic.price[acr]);
		myFrame.setAcrCutCharge(7);
// 	} else {
// 		myFrame.setAcrPrice(0);
// 		myFrame.setAcrCutCharge(0);
// 	}
	AcrPriceEst();
}

function FoamFunky() {
	var fidx = selectedIndexById("FoamPop");
	myFrame.setFoamboard(selectedTextById("FoamPop"));
// 	if (fidx > 0) {
		var foam = gFoam.item[fidx][0];
		var os = gFoam.item[fidx][1];
		myFrame.setFoamPrice(gFoam.price[fidx]);
		myFrame.setFoamPriceOS(gFoam.price[fidx]);
		myFrame.setFoamCutCharge(foam.cutcharge);
		myFrame.setFoamCutChargeOS(os.cutcharge);
// 	} else {
// 		myFrame.setFoamPrice(0);
// 		myFrame.setFoamPriceOS(0);
// 		myFrame.setFoamCutCharge(0);
// 		myFrame.setFoamCutChargeOS(0);
// 	}
	FoamPriceEst();
}

function MatStyler() {
	var styl = selectedIndexById("MatStyle");
	myFrame.setMatStyle(styl);
	document.StyleImg.src = "/Products/Matboard/images/" + Style.img[styl];
	if (styl == 2) { // both
		setInnerHTMLById(
		"Remember: The horizontal and vertical measurements <br />" +
		"should include the size of the artwork and the size <br />" +
		"of the float around the artwork.", "rememberFloat");
	} else {
		setInnerHTMLById("  ", "rememberFloat");
	}
	Mat = new Object();
	var a;
// 	if (styl == 1) {	// Sized Board
// 		Mat[0] = NoBoard;
// 		Mat[1] = Cresc;
// 		a = new buildArray("None", "Crescent");
// 		buildPop(myGetElementById("CompPop"), a, myFrame.matboard);
// 	} else {
		Mat[0] = Cresc;
		a = new buildArray("Crescent");
		buildPop(myGetElementById("CompPop"), a, myFrame.matboard);
// 	}
	CompFunky();
}

function MatStartUp() {
	setOutsideH(myFrame.horizontal);
	setOutsideV(myFrame.vertical);

	setTop(myFrame.matTop);
	setLeft(myFrame.matLeft);
	setBottom(myFrame.matBottom);
	setRight(myFrame.matRight);

	OutsideFunky();
	
	setValueById(myFrame.quantity, "Qty");
	setSelectedIndexById(myFrame.matStyle, "MatStyle");
	MatStyler();

	SelectMat(myFrame.hasMat);
	
// 	window.location.hash = "#MatAcrylicFoam";
}

function AcrStartUp() {
	setHor(myFrame.horizontal);
	setVer(myFrame.vertical);
	setValueById(myFrame.quantity, "Qty");
	if (gAcrylic && gAcrylic.pop) {
		buildPop(myGetElementById("AcrylicPop"), gAcrylic.pop, myFrame.acrylic);
		AcrylicFunky();
	}
	SelectAcrylic(myFrame.hasAcrylic);
}

function FoamStartUp() {
	setHor(myFrame.horizontal);
	setVer(myFrame.vertical);
	setValueById(myFrame.quantity, "Qty");
	if (gFoam && gFoam.pop) {
		buildPop(myGetElementById("FoamPop"), gFoam.pop, myFrame.foamboard);
		FoamFunky();
	}
	SelectFoam(myFrame.hasFoam);
}

var outside_in = true;	 /* true means calculate the sizing from the outside dimension
	in to the inside, false means caluate the outside based on the inside */

