// functions_f

/* inserisce un elemento flash */
function printVFlash(divid, divclass, pureHtml, nomefile, width, height, version, fallback, commonmarkup) {
	var xclose = (pureHtml)? '' : ' /';
	var isMSIE = /*@cc_on!@*/false;
	var divtag;
	var str;
	var commonstr;

	if (!version) {
		version = '9,0,0,0';
	}
	if (!fallback) {
		fallback = 'Please install Flash Player plugin.'
	}

	divtag = '<div';
	if (divid.length > 0) { divtag += ' id="' + divid + '"'; }
	if (divclass.length > 0) { divtag += ' class="' + divclass + '"'; }
	divtag += '>';

	commonstr = ('<param name="quality" value="high"' + xclose + '>');
	if (commonmarkup) {
		commonstr += commonmarkup;
	}

	if (isMSIE) {
		str = ('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version);
		str += ('" width="' + width + '" height="' + height + '">');
		str += ('<param name="movie" value="' + nomefile + '"' + xclose + '>');
		str += (commonstr + '<\/object>');
	} else {
		str = ('<object data="' + nomefile + '" width="' + width + '" height="' + height);
		str += ('" type="application/x-shockwave-flash">' + commonstr);
		str += ('<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer"' + xclose + '>');
		str += (fallback + '<\/object>');
	}

	document.write(divtag + str + '<\/div>');
}

function initSpecialLinks()
{
	var i, lk, rel;

	if (document.getElementsByTagName) {
		lk = document.getElementsByTagName('a');
		if (lk) {
			for (i=0; i < lk.length; i++) {
				rel = lk[i].getAttribute('rel');
				if (rel && rel.indexOf('external') != -1) {
					lk[i].setAttribute('target', '_blank');
					if (!lk[i].getAttribute('title')) {
						lk[i].setAttribute('title', mytext[3]);
					}
				}
			}
		}
	}
}

function adjustQuantity(idProd, adjValue)
{
	if (isNaN(adjValue) || isNaN(idProd) || idProd < 1) { return; }

	var myinput = document.getElementById('prodqta_' + idProd);
	if (!myinput) { return; }

	var maxValue, idx;
	var inputTitle = myinput.getAttribute('title');
	if (inputTitle && inputTitle.length) {
		idx = inputTitle.lastIndexOf(': ');
		maxValue = parseInt(inputTitle.substring(idx + 1), 10);
	} else {
		maxValue = 0;
	}

	var currentValue = parseInt(myinput.value, 10);
	adjValue = parseInt(adjValue, 10);
	var newValue = currentValue + adjValue;
	if (isNaN(newValue) || (newValue < 0)) {
		newValue = 0;
	}
	if (maxValue > 0 && newValue > maxValue) {
		newValue = maxValue;
	}
	myinput.value = newValue;

	// aggiorna prezzo totale e visibilita' +/-
	var unitSpan = document.getElementById('prezzo_' + idProd);
	var subtotSpan = document.getElementById('subtot_' + idProd);
	var priceZero = '';
	var price, priceEuro, priceCent, newClass;

	if (unitSpan && subtotSpan) {
		if (newValue == 0) {
			subtotSpan.innerHTML = mytext[5];
			$('div#div_qtaadj_' + idProd).removeClass().addClass('quantita_adj_piu');

		} else {
			price = unitSpan.innerHTML
			price = parseFloat(price.replace(',', '.'));

			// approssimazione al centesimo piu' vicino
			price = Math.round(100 * price) / 100;

			// quantita'
			price *= newValue;

			// scrivi prezzo
			priceEuro = Math.floor(price);
			priceCent = Math.round((price - priceEuro) * 100);

			if (priceCent < 10) { priceZero = '0'; }
			subtotSpan.innerHTML = priceEuro + mytext[6] + priceZero + priceCent;

			newClass = (!maxValue || newValue < maxValue) ? 'quantita_adj' : 'quantita_adj_meno';
			$('div#div_qtaadj_' + idProd).removeClass().addClass(newClass);
		}
	} 
	return;
}

function updateAllPrices()
{
	$('div.prices_row').each( function() {
		var prod;
		var id = this.id;
		var c = id.lastIndexOf('_');
		if (c >= 0) {
			prod = parseInt(id.substring(1 + c), 10);
		}
		adjustQuantity(prod, 0);
	});

}

function setupUpdateAllPrices()
{
	$('input.quantita').removeClass('quantita').addClass('quantita_nobo').change(
		function() {
			updateAllPrices();
		}
	);
}

function selectAllCheckbox(idContainer, value)
{
	var oDiv = $('div#' + idContainer).get(0);
	$('input:checkbox', oDiv).attr('checked', (value)? 'checked':'');
}

function orderConfirmAction(action)
{
	var myform;
	var acttext;

	if (document.getElementById) {
		myform = document.getElementById('form_action_' + action);
		if (myform) {
			switch(action) {
				case 'cancel':
					acttext = mytext[14];
					break;

				case 'accept':
					acttext = mytext[15];
					break;

				default:
					return true;
			}

			if (window.confirm(acttext + mytext[16])) {
				myform.confirm.value = 1;
				return true;

			} else {
				return false;
			}
		}
	}
	return true;
}

function cartEmptyConfirm()
{
	return window.confirm(mytext[17] + mytext[16]);
}

function productAddToCart(inCart)
{
	var qta = 0;
	// se non ancora in carrello, blocca qta = 0	
	if (!inCart) {
		$('input.quantita_nobo').each(function() {
			qta += parseInt(this.value, 10);
		});

		if (!qta) {
			window.alert(mytext[18]);
			return false;
		}
	}

	return true;
}

// document ready comune

$(document).ready( function() {
	initSpecialLinks();

	$('input#search_keywords').click( function() {
		if (this.value == mytext[4]) { this.value = ''; }
	});

	$('a#link_logout').click( function() {
		var cart = parseInt($('span#cart_count').html(), 10);
		if (cart > 0) {
			return cartEmptyConfirm();
		} else {
			return true;
		}
	});
});

