// decode encoded properties

function f_updatePrice () {
	$('price').update('$' + f_formatPrice(n_cardPrice + n_envelopePrice + n_borderPrice) + '<span>per ' + s_boxcount +  (s_envelopeBoxcount ? ' with matching ' + s_envelopeBoxcount + ' of Envelopes' : '') + '</span>');
}

var n_envelopePrice = 0;
var s_envelopeBoxcount = '';
function f_setEnvelope (e_img, s_code, n_price, s_boxcount) {

	if (e_img) {
		$$('#envelope-swatch img').each(function(e_curImg) {
			e_curImg.className = (e_img == e_curImg ? 'selected' : null);
		});
	}
	var e_imgcont = $('setPreview');
	var e_img = $('cardimg');
	if (s_code) {
		s_imgURL = 'http://www.lcipaper.com/catalog/graphics/prods/env-' + s_code + 'med.jpg';
		n_bottom = 5;
		n_right = 5;
		n_envelopePrice = n_price;
		s_envelopeBoxcount = s_boxcount;
	}
	else {
		s_imgURL = '/catalog/images/pixel.gif';
		if (!e_img.offsetWidth) {
			setTimeout("f_setEnvelope('-')", 200);
			return;
		}
		n_right  = (e_imgcont.offsetWidth  - (e_img.offsetWidth  ? e_img.offsetWidth  : 242)) / 2 - 2;
		n_bottom = (e_imgcont.offsetHeight - (e_img.offsetHeight ? e_img.offsetHeight : 330)) / 2 + 2;
		n_envelopePrice = 0;
		s_envelopeBoxcount = '';
	}
	e_img.setStyle({
		bottom: n_bottom + 'px',
		right: n_right + 'px',
		visibility: 'visible'
	});

	var e_bgBorder = $('bg-border');
	if (e_bgBorder) {
		e_bgBorder.setStyle({
			bottom: (n_bottom + 15) + 'px',
			right: (n_right + 15) + 'px',
			width: (e_img.offsetWidth - 30) + 'px',
			height: (e_img.offsetHeight - 30) + 'px'
		});
	}

	$('envimg').src = s_imgURL;
	$('addToCart').elements['product_code2'].value = (s_code ? s_code : '');
	f_updatePrice();
}

var n_borderPrice = 0;
function f_setBorder(e_img, s_code, n_price) {

	$$('#border-swatch img').each(function(e_curImg) {
		e_curImg.className = (e_img == e_curImg ? 'selected' : null);
	});

	$('border-color-value').value = s_code;
	$('cardimg').src = '/catalog/graphics/prods/' + window.s_productCode + ( s_code ? '_DB' : '' ) + 'med.png';
	
	$('bg-border').setStyle(s_code ? {
			backgroundColor: s_code,
			visibility: 'visible'
		} : {
			backgroundColor: 'transparent',
			visibility: 'hidden'
		}
	);
	n_borderPrice = n_price;
	f_updatePrice();
}

function f_formatPrice(n_value) {
	// validate input
	if (isNaN(Number(n_value)))
		return 'ERROR';

	// save the sign
	// round to 1/100 precision, add ending zeroes if needed
	var n_frac = Math.round(n_value * 1e2) % 1e2;
	var s_result = String((n_frac < 10 ? '0' : '') + n_frac + '00').substring(0, 2);
	
	// separate all orders
	var b_first = true;
	var s_subresult;
	while (n_value >= 1) {
		s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value % 1e3);
		s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') +  s_result;
		b_first = false;
		n_value = n_value / 1e3;
	}
	// add at least one integer digit
	if (b_first)
		s_result = '0.' +  s_result;

	// apply formatting and return
	return s_result;
}

function f_showSamples () {
	e_elem = $('sampleSelector');
	e_elem.setStyle({
		left: Math.max(10, (f_clientWidth() - e_elem.offsetWidth) / 2) + 'px',
		top:  Math.max(10, (f_clientHeight() - e_elem.offsetHeight) / 2)  + 'px',
		visibility: 'visible'
	});
}
function f_hideSamples () {
	$('sampleSelector').setStyle({ visibility: 'hidden' });
}
function f_selectAllSamples (b_select) {
	$$('#samplesTableContainer input[type=checkbox]').each(
		function (e_box) {
			e_box.checked = b_select;
		}
	);
}
