/*
	This gets the price for a throw pillow.
	@serverPage the Ajax page that is called
	@pid        the product id
	@optp       upcharge price (based on site, 0.00 is default)
*/
function am_throw_pillow(thread, serverPage, optp) {
	optp = opt_prices;

	mess = '';
	if($F('Option' + calcparam.get('d')) < 4) {
		mess += "Pillow height must be between 4 and 25 inches.\n";
		$('Option' + calcparam.get('d')).setValue(4);
	}

	if($F('Option' + calcparam.get('d')) > 25) {
		mess += "Pillow height must be between 4 and 25 inches.\n";
		$('Option' + calcparam.get('d')).setValue(25);
	}
	$('Option' + calcparam.get('d')).setValue(Math.round($F('Option' + calcparam.get('d'))*2)/2);

	if($F('Option' + calcparam.get('w')) < 12) {
		mess += "Pillow width must be between 12 and 25 inches.\n";
		$('Option' + calcparam.get('w')).setValue(12);
	}

	if($F('Option' + calcparam.get('w')) > 25) {
		mess += "Pillow width must be between 12 and 25 inches.\n";
		$('Option' + calcparam.get('w')).setValue(25);
	}
	$('Option' + calcparam.get('w')).setValue(Math.round($F('Option' + calcparam.get('w'))*2)/2);

	sid = '';
	if($('Option' + calcparam.get('sid')).disabled === false) sid = $F('Option' + calcparam.get('sid'));

	sid2='';
	if (calcparam.get('sid2')) sid2 = $F('Option' + calcparam.get('sid2'));

	// Even if there is an error, let's recalc prices based on the updated configuration ...
	if(mess != '') displayMessage(mess);

	sPage = serverPage + '?pid=' + product_id + '&sid=' + sid + '&sid2=' + sid2 + '&cid=' + $F('Option' + calcparam.get('cid')) + '&d=' +
		$F('Option' + calcparam.get('d')) + '&w=' + $F('Option' + calcparam.get('w')) + '&f=' + $F('Option' + calcparam.get('f')) + '&e=' +
		$F('Option' + calcparam.get('e')) + '&optp=' + optp;
	serverPage = sPage + '&price=1';
	doAjaxCall(serverPage, thread);
}

function displayMessage(mess) {
	alert(mess);
}

/*
	This gets the price for a bolster pillow.
	@serverPage the Ajax page that is called
	@pid        the product id
	@optp       upcharge price (based on site, 0.00 is default)
*/
function am_bolster_pillow(thread, serverPage, pid, optp) {
	optp = opt_prices;

	mess = '';

	if($F('Option' + calcparam.get('w')) < 12) {
		mess += "Pillow width must be between 12 and 25 inches.\n";
		$('Option' + calcparam.get('w')).setValue(12);
	}

	if($F('Option' + calcparam.get('w')) > 52) {
		mess += "Pillow width must be between 12 and 52 inches.\n";
		$('Option' + calcparam.get('w')).setValue(52);
	}
	$('Option' + calcparam.get('w')).setValue(Math.round($F('Option' + calcparam.get('w'))*2)/2);

	sid = '';
	if($('Option' + calcparam.get('sid')).disabled === false) sid = $F('Option' + calcparam.get('sid'));

	if(mess != '') displayMessage(mess);

	sPage = serverPage + '?pid=' + product_id + '&sid=' + sid + '&d=' + $F('Option' + calcparam.get('d')) + '&w=' + $F('Option' + calcparam.get('w')) + '&f=' + $F('Option' + calcparam.get('f')) + '&e=' + $F('Option' + calcparam.get('e')) + '&optp=' + optp;
	serverPage = sPage + '&price=1';
	doAjaxCall(serverPage, thread);
}

function calc_ark_drape(thread, serverPage, pid, optp) {
	optp = opt_prices;

	mess = '';
	if($F('Option' + calcparam.get('l')) < 12) {
		mess += "Drape length must be longer than 12 inches.\n";
		$('Option' + calcparam.get('l')).setValue(12);
	}
	$('Option' + calcparam.get('l')).setValue(Math.round($F('Option' + calcparam.get('l'))*2)/2);

	sid = '';
	if($('Option' + calcparam.get('sid')).disabled === false) sid = $F('Option' + calcparam.get('sid'));

	if(mess != '') displayMessage(mess);

	sPage = serverPage + '?pid=' + product_id + '&sid=' + sid + '&w=' + $F('Option' + calcparam.get('w')) + '&l=' + $F('Option' + calcparam.get('l')) + '&inner=' + $F('Option' + calcparam.get('inner')) + '&optp=' + optp + '&type=' + $F('Option' + calcparam.get('type'));
	serverPage = sPage + '&price=1';
	doAjaxCall(serverPage, thread);
}

/*
	This gets the price for a drape
	@serverPage the Ajax page that is called
	@pid        the product id
	@optp       upcharge price (based on site, 0.00 is default)
*/
function calc_pr_drape(thread, serverPage, pid, optp) {
	optp = opt_prices;

	mess = '';
	if($F('Option' + calcparam.get('l')) < 12) {
		mess += "Drape length must be longer than 12 inches.\n";
		$('Option' + calcparam.get('l')).setValue(12);
	}
	$('Option' + calcparam.get('l')).setValue(Math.round($F('Option' + calcparam.get('l'))*2)/2);

	if($F('Option' + calcparam.get('sid')) == '') {
		clearTimeout(newPriceThread);
		mess += "Drape fabric must be selected.\n";
	}

	if(mess != '') displayMessage(mess);

	sPage = serverPage + '?pid=' + product_id + '&sid=' + $F('Option' + calcparam.get('sid')) + '&w=' + $F('Option' + calcparam.get('w')) + '&l=' + $F('Option' + calcparam.get('l')) + '&optp=' + optp + '&type=' + $F('Option' + calcparam.get('type')) + '&ftype=' + $F('Option' + calcparam.get('ftype'));
	serverPage = sPage + '&price=1';
	doAjaxCall(serverPage, thread);
}

/*
	This gets the price for a bench pillow
	@serverPage the Ajax page that is called
	@pid        the product id
	@optp       upcharge price (based on site, 0.00 is default)
*/
function am_bench_OLD(thread, serverPage, pid, optp) {
	optp = opt_prices;

	mess = '';
	if($F('Option' + calcparam.get('d')) < 10) {
		mess += "Cushion depth must be between 10 and 40 inches.\n";
		$('Option' + calcparam.get('d')).setValue(10);
	}

	if($F('Option' + calcparam.get('d')) > 40) {
		mess += "Cushion depth must be between 10 and 40 inches.\n";
		$('Option' + calcparam.get('d')).setValue(40);
	}
	$('Option' + calcparam.get('d')).setValue(Math.round($F('Option' + calcparam.get('d'))*2)/2);

	if ($F('Option' + calcparam.get('w'))>0) {
		if($F('Option' + calcparam.get('w')) < 22) {
			mess += "Cushion width must be between 22 and 25 inches.\n";
			$('Option' + calcparam.get('w')).setValue(22);
		}

		if($F('Option' + calcparam.get('w')) > 205) {
			mess += "Cushion width must be between 22 and 205 inches.\n";
			$('Option' + calcparam.get('w')).setValue(205);
		}
		$('Option' + calcparam.get('w')).setValue(Math.round($F('Option' + calcparam.get('w'))*2)/2);
		w = $F('Option' + calcparam.get('w'));
	}
	if($F('Option' + calcparam.get('fw'))>0) {
		if($F('Option' + calcparam.get('fw')) < 22) {
			mess += "Cushion width must be between 22 and 25 inches.\n";
			$('Option' + calcparam.get('fw')).setValue(22);
		}

		if($F('Option' + calcparam.get('fw')) > 205) {
			mess += "Cushion width must be between 22 and 205 inches.\n";
			$('Option' + calcparam.get('fw')).setValue(205);
		}
		$('Option' + calcparam.get('fw')).setValue(Math.round($F('Option' + calcparam.get('fw'))*2)/2);
		w = $F('Option' + calcparam.get('fw'));

		if($F('Option' + calcparam.get('bw')) < 22) {
			mess += "Cushion width must be between 22 and 25 inches.\n";
			$('Option' + calcparam.get('bw')).setValue(22);
		}

		if($F('Option' + calcparam.get('bw')) > 205) {
			mess += "Cushion width must be between 22 and 205 inches.\n";
			$('Option' + calcparam.get('bw')).setValue(205);
		}
		$('Option' + calcparam.get('bw')).setValue(Math.round($F('Option' + calcparam.get('bw'))*2)/2);
		if($F('Option' + calcparam.get('bw')) > w) w=$F('Option' + calcparam.get('bw'));
	}

	if($F('Option' + calcparam.get('sid')) == '') {
		clearTimeout(newPriceThread);
		mess += "Cushion fabric must be selected.<br />";
	}

	if(mess != '') displayMessage(mess);

	sPage = serverPage + '?pid=' + product_id + '&sid=' + $F('Option' + calcparam.get('sid')) + '&cid=' + $F('Option' + calcparam.get('cid')) + '&d=' + $F('Option' + calcparam.get('d')) + '&w=' + w + '&t=' + $F('Option' + calcparam.get('t')) + '&cord=' + $F('Option' + calcparam.get('cord')) + '&edge=' + $F('Option' + calcparam.get('edge')) + '&optp=' + optp;
	serverPage = sPage + '&price=1';
	doAjaxCall(serverPage, thread);
}

function am_bench(thread, serverPage, pid, optp) {
	optp = opt_prices;

	var empty;
	fnb = false;
	passed = true;
	mess = "";

	d = $F('Option' + calcparam.get('d'));
	if($('Option' + calcparam.get('w')).disabled === false) w = $F('Option' + calcparam.get('w'));
		else w = '';
	if(calcparam.get('bw')) bw = $F('Option' + calcparam.get('bw'));
	if(calcparam.get('fw')) fw = $F('Option' + calcparam.get('fw'));

	if (d < 10) {
		passed = false;
		mess += "Cushion Depth must be between 10 and 40 inches.\n";
		d = 10;
	}
	if (d > 40 ) {
		passed = false;
		mess += "Cushion Depth must be between 10 and 40 inches.\n";
		d = 40;
	}
	d = Math.round(d*2)/2;
	$('Option' + calcparam.get('d')).setValue(d);

	if (w == '') {
		fnb = true;
	}
	if (!fnb) {
		if (w < 10 ) {
			passed = false;
			mess += "Cushion Width must be between 10 and 205 inches.\n";
			w = 22;
		}
		if (w > 205 ) {
			passed = false;
			mess += "Cushion Width must be between 10 and 205 inches.\n";
			w = 205;
		}
		w = Math.round(w*2)/2;
		$('Option' + calcparam.get('w')).setValue(w);
	} else {
		if (fw < 10 ) {
			passed = false;
			mess += "Cushion Front Width must be between 10 and 205 inches.\n";
			fw = 10;
		}
		if (fw > 205 ) {
			passed = false;
			mess += "Cushion Front Width must be between 22 and 205 inches.\n";
			fw = 205;
		}
		fw = Math.round(fw*2)/2;
		$('Option' + calcparam.get('fw')).setValue(fw);
		if (bw < 10 ) {
			passed = false;
			mess += "Cushion Back Width must be between 10 and 205 inches.\n";
			bw = 10;
		}
		if (bw > 205 ) {
			passed = false;
			mess += "Cushion Back Width must be between 22 and 205 inches.\n";
			bw = 205;
		}
		bw = Math.round(bw*2)/2;
		$('Option' + calcparam.get('bw')).setValue(bw);

		w = fw;
		if (bw > fw) w = bw;
	}

	if(mess != '') displayMessage(mess);

	if($('Option' + calcparam.get('t')).disabled == true) t = $F('Option' + calcparam.get('bt'));
		else t = $F('Option' + calcparam.get('t'));

	sid = '';
	if($('Option' + calcparam.get('sid')).disabled === false) sid = $F('Option' + calcparam.get('sid'));

	sPage = serverPage + '?pid=' + product_id + '&sid=' + sid + (calcparam.get('cid') ? '&cid=' + $F('Option' + calcparam.get('cid')) : '') + '&d=' + d + '&w=' + w + '&t=' + t + '&cord=' + $F('Option' + calcparam.get('cord')) + '&edge=' + $F('Option' + calcparam.get('edge')) + '&optp=' + optp;
	serverPage = sPage + '&price=1';
	doAjaxCall(serverPage, thread);
}

function am_chaise(thread, serverPage, pid, optp) {
	optp = opt_prices;

	passed = true;mess = "";
	if ($F('Option' + calcparam.get('d')) < 10 ) {
		passed = false;
		mess += "Cushion Width must be between 10 and 40 inches";
		$('Option' + calcparam.get('d')).setValue(10);
	}

	if ($F('Option' + calcparam.get('d')) > 80 ) {
		passed = false;
		mess += "Cushion Width must be between 10 and 80 inches";
		$('Option' + calcparam.get('d')).setValue(80);
	}

	$('Option' + calcparam.get('d')).setValue(Math.round($F('Option' + calcparam.get('d'))*2)/2);
	$('Option' + calcparam.get('b1')).setValue(Math.round($F('Option' + calcparam.get('b1'))*2)/2);
	$('Option' + calcparam.get('b2')).setValue(Math.round($F('Option' + calcparam.get('b2'))*2)/2);
	$('Option' + calcparam.get('b3')).setValue(Math.round($F('Option' + calcparam.get('b3'))*2)/2);
	$('Option' + calcparam.get('b4')).setValue(Math.round($F('Option' + calcparam.get('b4'))*2)/2);

	sid = '';
	if($('Option' + calcparam.get('sid')).disabled === false) sid = $F('Option' + calcparam.get('sid'));

	if (!passed) {
		if(mess != '') displayMessage(mess);
	} else {
		if($('Option' + calcparam.get('t')).disabled == true) t = $F('Option' + calcparam.get('bt'));
			else t = $F('Option' + calcparam.get('t'));

		sPage = serverPage+"?pid="+product_id+"&sid="+sid+"&cid="+$F('Option' + calcparam.get('cid'))+"&d="+$F('Option' + calcparam.get('d'))+"&t="+ t +"&cord=" + $F('Option' + calcparam.get('cord')) + '&edge=' + $F('Option' + calcparam.get('edge')) + "&optp="+optp+"&b1="+$F('Option' + calcparam.get('b1'))+"&b2="+$F('Option' + calcparam.get('b2'))+"&b3="+$F('Option' + calcparam.get('b3'))+"&b4="+$F('Option' + calcparam.get('b4'));
		serverPage = sPage+"&price=1";
		doAjaxCall(serverPage, thread);
	}
}

function anchor_pool_cover(thread, serverPage, pid, optp) {
	optp = opt_prices;

	passed = true;mess = "";
	if ($F('Option' + calcparam.get('l')) < 10 ) {
		passed = false;
		mess += "Pool Length must be at least 10 feet";
		$('Option' + calcparam.get('l')).setValue(10);
	}

	if ($F('Option' + calcparam.get('w')) < 5 ) {
		passed = false;
		mess += "Pool Width must be at least 5 feet";
		$('Option' + calcparam.get('w')).setValue(5);
	}

	$('Option' + calcparam.get('l')).setValue(Math.round($F('Option' + calcparam.get('l'))*2)/2);
	$('Option' + calcparam.get('w')).setValue(Math.round($F('Option' + calcparam.get('w'))*2)/2);

	if (!passed) {
		if(mess != '') displayMessage(mess);
	} else {
		sPage = serverPage+"?pid="+product_id+"&type="+$F('Option' + calcparam.get('type'))+"&ps="+$F('Option' + calcparam.get('ps'))+"&l="+$F('Option' + calcparam.get('l'))+"&w=" + $F('Option' + calcparam.get('w')) + '&s1=' + $F('Option' + calcparam.get('s1')) + '&s2=' + $F('Option' + calcparam.get('s2')) + '&hw=' + $F('Option' + calcparam.get('hw')) + '&optp='+optp;
		serverPage = sPage+"&price=1";
		doAjaxCall(serverPage, thread);
	}
}

function ecu_umbrellas(thread, serverPage, optp) {
	optp = opt_prices;

	mess = '';
	if($F('Option' + calcparam.get('msid')) == '') {
		clearTimeout(newPriceThread);
		mess += "Main Umbrella fabric must be selected.<br />";
	}

	// Even if there is an error, let's recalc prices based on the updated configuration ...
	if(mess != '') displayMessage(mess);

	sPage = serverPage + '?pid=' + product_id + '&msid=' + $F('Option' + calcparam.get('msid')) + '&asid=' + $F('Option' + calcparam.get('asid')) + '&optp=' + optp;
	serverPage = sPage + '&price=1';
	doAjaxCall(serverPage, thread);
}

function tg_spa_cover(thread, serverPage, optp) {

	while(ready == false) {}
	optp = opt_prices;

	mess = '';
	if ($F('Option' + calcparam.get('shp')) != 4049) {
		if($F('Option' + calcparam.get('a')) == '') {
			$('Option' + calcparam.get('a')).setValue(96);
			mess += "Side A must be selected.\n";
		}

		if(isNaN($F('Option' + calcparam.get('a')))) {
			$('Option' + calcparam.get('a')).setValue(96);
			mess += "Side A must be a number.\n";
		}

		if ($F('Option' + calcparam.get('a')) > 102 ) {
			$('Option' + calcparam.get('a')).setValue(102);
			mess += "Side A cannot exceend 102.\n";
		}
	}

	if($F('Option' + calcparam.get('b')) == '') {
		$('Option' + calcparam.get('b')).setValue(96);
		mess += "Side B must be selected.\n";
	}

	if(isNaN($F('Option' + calcparam.get('b')))) {
		$('Option' + calcparam.get('b')).setValue(96);
		mess += "Side B must be a number.\n";
	}

	if ($F('Option' + calcparam.get('b')) > 102 ) {
		$('Option' + calcparam.get('b')).setValue(102);
		mess += "Side B cannot exceend 102";
	}

	// Even if there is an error, let's recalc prices based on the updated configuration ...
	if(mess != '') displayMessage(mess);

	sPage = serverPage + '?pid=' + product_id + '&a=' + $F('Option' + calcparam.get('a')) + '&b=' + $F('Option' + calcparam.get('b')) + '&shp=' + $F('Option' + calcparam.get('shp')) + '&optp=' + optp;
	serverPage = sPage + '&price=1';
	doAjaxCall(serverPage, thread);
}

/*
	This opens the color picker
	@root        the Ajax page that is called
	@vendor      the vendor
	@group       no idea ...
	@jsret       the return function for the JS (4 variables are returned, view the 'jsReturn' function in functions.js for more info
	@showup      no idea ...
	@addArgs     Any additional variables needed to pass to the fabric tool
*/
function open_color_pick(root, vendor, group, jsret, showup, addArgs) {
	newFabricTool(root, vendor, group, jsret, showup);
}

function open_color_pick_any(root, vendor, group, jsret, showup) {
	newFabricToolAny(root, vendor, group, jsret, showup);
}

function newFabricTool(root, vendor, group, jsret, showup) {
	var difftype = (group == ',Fringe' || group == ',Braids' || group == ',Tassel' || group == ',FringeCording') ? 'Trim' : 'Fabric';
	if (document.viewport.getDimensions().width > 1024 && document.viewport.getDimensions().height > 768 && typeof(Overlay) != 'undefined') {
		if ($('fabseeFrame') == null) {
			var overLayMe = new Element('iframe', {'id' : 'fabseeFrame', 'style' : 'border: 0px; background-color: #FFF; height: 635px; width: 950px; overflow:hidden;','src' : '/popup-page/fabrictool#jsfunc=' + jsret + '&vendor=' + vendor + '&groups=' + group + '&types=' + difftype + '&anyvend=false', 'name' : 'fabSelector', 'scrolling' : 'no', 'frameborder' : '0'});
			document.body.appendChild(overLayMe);
		}
		if (typeof(oler) == 'undefined') {
			oler = new Overlay();
		}
		oler.show(overLayMe, {position : 'center', modal : true, animate : true, opacity : 1, bckg_cls : 'bigbgwin', bckg_opacity : 0.7, auto_hide : false, click_hide : false, afterhide : function () {$('fabseeFrame').remove();}});
	} else {
		var furl = '/popup-page/fabrictool#jsfunc=' + jsret + '&vendor=' + vendor + '&groups=' + group + '&types=' + difftype + '&anyvend=false';
		var fabWin = window.open(furl, 'fabSelector', 'height=635, width=950');
		fabWin.focus();
	}
}

function newFabricToolAny(root, vendor, group, jsret, showup) {
	var difftype = (group == ',Fringe' || group == ',Braids' || group == ',Tassel' || group == ',FringeCording') ? 'Trim' : 'Fabric';
	if (document.viewport.getDimensions().width > 1024 && document.viewport.getDimensions().height > 768 && typeof(Overlay) != 'undefined') {
		if ($('fabseeFrame') == null) {
			var overLayMe = new Element('iframe', {'id' : 'fabseeFrame', 'style' : 'border: 0px; background-color: #FFF; height: 635px; width: 950px; overflow: hidden;','src' : '/popup-page/fabrictool#jsfunc=' + jsret + '&vendor=' + vendor + '&groups=' + group + '&types=' + difftype + '&anyvend=true', 'name' : 'fabSelector', 'scrolling' : 'no', 'frameborder' : '0'});
			document.body.appendChild(overLayMe);
		}
		if (typeof(oler) == 'undefined') {
			oler = new Overlay();
		}
		oler.show(overLayMe, {position : 'center', modal : true, animate : true, opacity : 1, bckg_cls : 'bigbgwin', bckg_opacity : 0.7, auto_hide : false, click_hide : false, afterhide : function () { $('fabseeFrame').remove(); } });
	} else {
		var furl = '/popup-page/fabrictool#jsfunc=' + jsret + '&vendor=' + vendor + '&groups=' + group + '&types=' + difftype + '&anyvend=true';
		var fabWin = window.open(furl, 'fabSelector', 'height=635, width=950');
		fabWin.focus();
	}

}

// Is this old?
function childcolor(OLD, ID, Name, Upcharge) {
	alert("returned with "+ID+" and "+Name+" and "+Upcharge);
}

var cart_calculate = (function(thread, serverPage, optp) {
	var addProduct = $('addProduct');

	doAjaxCall(serverPage + '?price', thread, addProduct.serialize(true), 'updateReviewStep');
});

/**
 * Updates the steps
 * @param {Object} jsreturn
 * @return {Boolean}
 */
function updateReviewStep(jsreturn, product_options) {
	if($('reviewOptionCon') != null && typeof jsreturn != 'undefined' && typeof jsreturn.desc != 'undefined') {
		optionObj = strToArr(jsreturn.desc.options, "\n");
		if(!optionObj) return false;
		var optionList = new Element('ul', {'class' : 'reviewOptionList'});
		var counter = 0;
		var dclass;
		optionObj.each (function (x) {
			dclass = (counter % 2 == 0) ? 'row' : 'rowalt';
			optionList.insert(new Element('li', {'class' : 'reviewOptionItem ' + dclass}).update(x));
			counter++;
		});
		$('reviewOptionCon').update(optionList);
	}

	//"option_prices":{"2":{"51":9.63},"18":{"4606":8,"4608":14.28}}}

	if(jsreturn.option_prices) {
		Object.keys(jsreturn.option_prices).each(function(option_id) {
			Object.keys(jsreturn.option_prices[option_id]).each(function(value_id) {
				product_options.setExtraPrice(option_id, value_id, jsreturn.option_prices[option_id][value_id]);
			});
		});
	}

	return true;
}

function strToArr (obj, joiner) {
	if (typeof obj == 'undefined' || typeof joiner == 'undefined')
			return false;
	var str = obj.split(joiner);
	return str;
}
