// 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;
// }

Object.prototype.size = function () {
  var len = this.length ? --this.length : -1;
    for (var k in this)
      len++;
  return len;
}


function MatPriceEst() {
	var compName = selectedTextById("CompPop");
	if (compName && compName != "None") {
		//var c = selectedIndexById("CompPop");
		//var c = selectedIndexById("MatStyle");
		var c = getValueByFormItem('MatStyle');
		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];

		
		var mat;
		
		if ( Mat[c].ply[ply] != undefined ){
			for (i=0; i < Mat[c].ply[ply].matobs[color].size(); i+=2){
				if (
					Mat[c].ply[ply].matobs[color][i].style == c
					|| Mat[c].ply[ply].matobs[color][i].style.search(','+c+',')
					|| Mat[c].ply[ply].matobs[color][i].style.search('^'+c+',')
					|| Mat[c].ply[ply].matobs[color][i].style.search(','+c+'$')
				)
					mat = Mat[c].ply[ply].matobs[color][i];
			}
			
			var os;
			for (i=1; i < Mat[c].ply[ply].matobs[color].size(); i+=2){
				if (
					Mat[c].ply[ply].matobs[color][i].style == c
					|| Mat[c].ply[ply].matobs[color][i].style.search(','+c+',')
					|| Mat[c].ply[ply].matobs[color][i].style.search('^'+c+',')
					|| Mat[c].ply[ply].matobs[color][i].style.search(','+c+'$')
				)
					mat = Mat[c].ply[ply].matobs[color][i];
			}
		}

		if ( mat == undefined ){
			mat = new buildArray();
			mat.itemno = '';
			mat.price = 0;
			mat.matcutcharge = 0;
			mat.boardcutcharge = 0;
			mat.matcutchargemin = 0;
			mat.boardcutchargemin = 0;
		}

		if ( os == undefined ){
			var os = new buildArray();
			os.itemno = '';
			os.price = 0;
			os.matcutcharge = 0;
			os.boardcutcharge = 0;
			os.matcutchargemin = 0;
			os.boardcutchargemin = 0;
		}
		
		myFrame.setMatItemNumber(mat.itemno);
		myFrame.setMatOSItemNumber(os.itemno);
		
		myFrame.setMatPrice(mat.price);
		myFrame.setMatPriceOS(os.price);
		myFrame.setMatCutCharge(mat.matcutcharge);
		myFrame.setMatCutChargeMin(mat.matcutchargemin);
		myFrame.setMatCutChargeOS(os.matcutcharge);
		
		myFrame.setBoardPrice(mat.price);
		myFrame.setBoardPriceOS(os.price);
		myFrame.setBoardCutCharge(mat.boardcutcharge);
		myFrame.setBoardCutChargeMin(mat.boardcutchargemin);
		myFrame.setBoardCutChargeOS(os.boardcutcharge);
	} else {
		myFrame.setMatItemNumber("");
		myFrame.setMatOSItemNumber("");
		
		myFrame.setMatPrice(0);
		myFrame.setMatPriceOS(0);
		myFrame.setMatCutCharge(0);
		myFrame.setMatCutChargeMin(0);
		myFrame.setMatCutChargeOS(0);
		
		myFrame.setBoardPrice(0);
		myFrame.setBoardPriceOS(0);
		myFrame.setBoardCutCharge(0);
		myFrame.setBoardCutChargeMin(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()");
	
	if ( isNaN(parseFloat(valueById("Up"))) || isNaN(parseFloat(valueById("Down"))) 
		 || isNaN(parseFloat(valueById("Left"))) || isNaN(parseFloat(valueById("Right")))
		 || ( outside_in && isNaN(parseFloat(valueById("OutsideH"))) )
		 || ( !outside_in && isNaN(parseFloat(valueById("InsideV"))) )
		 || ( outside_in && isNaN(parseFloat(valueById("OutsideV"))) )
	){
		alert('Please, set numeric value');
	}
	
	
	myFrame.setMatTop(matTop());
	myFrame.setMatLeft(matLeft());
	myFrame.setMatBottom(matBottom());
	myFrame.setMatRight(matRight());

	if (outside_in) {
		myFrame.setHorizontal(parseFloat(valueById("OutsideH")) + 
			parseFloat(selectedValueById("OutsideHPop")));
		var tempInsideHorizontal = myFrame.horizontal - matLeft() - matRight();
		if ( tempInsideHorizontal < 0 ){
			tempInsideHorizontal = 0;
			setOutsideH( matLeft() + matRight() );
		}	
		setInsideH( tempInsideHorizontal );
		myFrame.setVertical(parseFloat(valueById("OutsideV")) + 
			parseFloat(selectedValueById("OutsideVPop")));
		var tempInsideVertikal = myFrame.vertical - matTop() - matBottom();
		if ( tempInsideVertikal < 0 ){
			tempInsideVertikal = 0;
			setOutsideV( matTop() + matBottom() );
		}	
		setInsideV( tempInsideVertikal );
	} 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");
//	var c = selectedIndexById("MatStyle");
	var c = getValueByFormItem('MatStyle');

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

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

function PlyFunky() {
//	var c = selectedIndexById("CompPop");
//	var c = selectedIndexById("MatStyle");
	var c = getValueByFormItem('MatStyle');
	var p = selectedIndexById("PlyPop");

	myFrame.setMatPly(selectedTextById("PlyPop"));
	if ( 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");
	var acr = getValueByFormItem("AcrylicPop");
	//myFrame.setAcrylic(selectedTextById("AcrylicPop"));
	var txt = document.getElementById("AcrylicText_"+acr).innerHTML;
	if ( document.AcrylicImg != undefined ){
		var img = document.getElementById("AcrylicImage_"+acr).value;
		if ( img != '' ){
			document.AcrylicImg.style.display="block";
		}
		else{
			document.AcrylicImg.style.display="none";
		}
		var path = document.getElementById("AcrylicPath_"+acr).value;
		document.AcrylicImg.src = path + '/' + img;
	}	
	myFrame.setAcrylic( txt );
	if ( gAcrylic.cutcharge[acr] != undefined ) {
		myFrame.setAcrPrice(gAcrylic.price[acr]);
		myFrame.setAcrCutCharge(gAcrylic.cutcharge[acr]);
		myFrame.setAcrCutChargeMin(gAcrylic.cutchargemin[acr]);
		myFrame.setAcrItemNumber(gAcrylic.itemno[acr]);
 	} else {
 		myFrame.setAcrPrice(0);
 		myFrame.setAcrCutCharge(0);
		myFrame.setAcrCutChargeMin(0);
		myFrame.setAcrItemNumber("");
 	}
	AcrPriceEst();
}

function FoamFunky() {
	//var styl = selectedIndexById("FoamStyle");
	var styl = getValueByFormItem("FoamStyle");
	//var fidx = selectedIndexById("FoamPop");
	var fidx = getValueByFormItem("FoamPop");
	var txt = document.getElementById("FoamText_"+fidx).innerHTML;
	if ( document.FoamImg != undefined ){
		var img = document.getElementById("FoamImage_"+fidx).value;
		var path = document.getElementById("FoamPath_"+fidx).value;
		if ( img != '' ){
			document.FoamImg.style.display="block";
		}
		else{
			document.FoamImg.style.display="none";
		}
		document.FoamImg.src = path + '/' + img;
	}	
	myFrame.setFoamboard( txt );
 	if (gFoam.item[styl][fidx] != undefined) {
		var foam = gFoam.item[styl][fidx][0];
		var os = gFoam.item[styl][fidx][1];
		myFrame.setFoamPrice(gFoam.price[styl][fidx]);
		myFrame.setFoamPriceOS(gFoam.price[styl][fidx]);
		myFrame.setFoamCutCharge(foam.cutcharge);
		myFrame.setFoamCutChargeMin(foam.cutchargemin);
		myFrame.setFoamItemNumber(foam.itemno);
		if (os != undefined) myFrame.setFoamCutChargeOS(os.cutcharge);
 	} else {
 		myFrame.setFoamPrice(0);
 		myFrame.setFoamPriceOS(0);
 		myFrame.setFoamCutCharge(0);
 		myFrame.setFoamCutChargeOS(0);
		myFrame.setFoamCutChargeMin(0);
		myFrame.setFoamItemNumber("");
 	}
	FoamPriceEst();
}

function MatStyler() {
	//var styl = selectedIndexById("MatStyle");
	var styl = getValueByFormItem('MatStyle');
	myFrame.setMatStyle(styl);
	if ( Style.img[styl] != undefined ){
		document.StyleImg.src = "/Products/Matboard/images/" + Style.img[styl];
	}	
	setInnerHTMLById(styl, "rememberFloat");
	if ( Style.desc[styl] != undefined ){
		setInnerHTMLById(Style.desc[styl], "rememberFloat");
	}

	var a;
	buildPop(myGetElementById("CompPop"), a);	
	buildPop(myGetElementById("PlyPop"), a);
	buildPop(myGetElementById("ColorPop"), a);
	
	if ( Style.brand[styl] != undefined ){
		for (i=0; i < Style.brand[styl].size(); i++){
			if ( Style.brand[styl][i] != undefined ){
				sel = myGetElementById("CompPop");
				opt = new Option(Style.brand[styl][i].Name, Style.brand[styl][i].Number);
				if (i == 0 ) {
					opt.selected = true;
				}
				sel.options[i] = opt;
			}	
		}
	}
	
	

//	Mat = new Object();
//	var a;
//	Mat[0] = Cresc;
//	a = new buildArray("Crescent");
//	buildPop(myGetElementById("CompPop"), a, myFrame.matboard);
	
	CompFunky();
}

function FoamStyler() {
	//var styl = selectedIndexById("FoamStyle");
	var styl = getValueByFormItem("FoamStyle");
	//myFrame.setFoamStyle(styl);
	if ( document.getElementById('FoamStyleImg') != undefined ){
		if ( FoamStyle.img[styl] != undefined && FoamStyle.img[styl] != '' ){
			document.FoamStyleImg.style.display="inline";
			document.FoamStyleImg.src = "/Products/Other_products/images/" + FoamStyle.img[styl];
		}
		else{
			document.FoamStyleImg.style.display="none";
		}
	}
	setInnerHTMLById(styl, "rememberFoamFloat");
	if ( FoamStyle.desc[styl] != undefined ){
		setInnerHTMLById(FoamStyle.desc[styl], "rememberFoamFloat");
	}
	if (gFoam && gFoam.pop[styl]) {
		var fidx = getValueByFormItem("FoamPop");
		//buildPop(myGetElementById("FoamPop_"+fidx), gFoam.pop[styl], myFrame.foamboard);
		FoamFunky();
	}	
	
	FoamFunky();

}

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");
	FoamStyler();
	//var styl = selectedIndexById("FoamStyle");
	var styl = getValueByFormItem("FoamStyle");
	if (gFoam && gFoam.pop[styl]) {
		var fidx = getValueByFormItem("FoamPop");
		//buildPop(myGetElementById("FoamPop_"+fidx), gFoam.pop[styl], myFrame.foamboard);
		FoamFunky();
	}
	SelectFoam(myFrame.hasFoam);
}

function SelectMatStyle(el) {
	var n = el.name;
	var v = el.value;
	SetCookie( n, v );
	MatStyler();
}

function SelectAcrylicPop(el) {
	var n = el.name;
	var v = el.value;
	SetCookie( n, v );
	AcrylicFunky();
}

function SelectFoamPop(el) {
	var n = el.name;
	var v = el.value;
	SetCookie( n, v );
	FoamFunky();
}


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


