
var isOk_checkAll = false;
var  ecAddToCartMess = 0;

function ecAddToCart(objId, htmlLabel)
{
	var messId = 'ecAddToCartMess_'+ecAddToCartMess++;
	var obj = document.getElementById(objId);

	var htmlMessaggio = '';
	htmlMessaggio += '<div class="testo">Prodotto: <strong>'+htmlLabel+'</strong></div>';
	htmlMessaggio += '<div class="testo">'+(obj.value == 1 ? 'Aggiunto: <strong>1</strong> articolo' : 'Aggiunti: <strong>'+obj.value+'</strong> articoli')+' al carrello</div>';
	messageInfo_add('pulsante_v', htmlMessaggio, 6, messId);

	divSpinnerRefill('boxECommerce_spinner', 'aggiornamento in corso...');
	loadHTML(ecUrlAction["addToCart"]+'&objId='+objId+'&size='+obj.value+'&idiv=true', storage='boxECommerce', jsAfterOpen='');
	return false;
}

function ecViewCart()
{
	scrollTop();
	openInside(ecUrlAction["carrello"]+'&idiv=true', "checkAll_onchange('ecCart');");
	return false;
}

function ec_cartRemove(idProdotto)
{
	loadHTML_execJs(ecUrlAction["carrelloRemoveProdotto"]+'&idProdotto='+idProdotto, storage='', jsAfterOpen='');
	return false;
}

function ecEmptyCart()
{
	scrollTop();
	loadHTML_execJs(ecUrlAction["carrelloSvuota"]+'&idiv=true', storage='', jsAfterOpen='');
	return false;
}

function ecCambiaPerPagina(url, value)
{
	scrollTop();
	openInside(url+'&perPagina='+value+'&idiv=true');
}

function ecRetriveAccount(formName)
{
	var objVal = document.forms[formName].info;
	var val = objVal.value;

	if (val == '')
	{
		var idParent = objVal.parentNode.id;
		jq_blink(idParent);
		objVal.style.backgroundColor = '#FFFF99';
		objVal.focus();
	}
	else
	{
		var idAction = document.forms[formName].idAction.value;
		var retriveType = document.forms[formName].retriveType.value;
		scrollTop();
		openInside(ecUrlAction['passwordRecoverySend']+'&retriveType='+retriveType+'&idiv=true&val='+val);
	}
	return false;
}

function ecRetriveAccountConfirm()
{
	var p1 = document.getElementById('ecF_passwd');
	var p2 = document.getElementById('ecF_verifica');

	if (p1.value == ''){
		p1.className ='ec_errorReport';
		jq_blink('ecF_passwd');
	} else if ((p2.value == '') || (p1.value != p2.value))
	{
		p1.className = '';
		p2.className = 'ec_errorReport';
		jq_blink('ecF_verifica');
	}
	else
	{
		p1.className = '';
		p2.className = '';

		var form = $('#ecPRE');
		form.submit();
	}
	return false;
}

function ecAccountNew(objId)
{
	scrollTop();
	objForm = document.getElementById(objId);

	isOk_checkAll = true;
	checkAll_onchange(objForm);

	return (isOk_checkAll ? true : false);
}


function ecEmptyCart()
{
	scrollTop();
	loadHTML_execJs(ecUrlAction["carrelloSvuota"]+'&idiv=true', storage='', jsAfterOpen='');
	return false;
}

function ecUpdateCart()
{
	isOk_checkAll = true;
	checkAll_onchange('ecCart');
	if (isOk_checkAll)
	{
		var form = $('#ecCart');
		url = ecUrlAction["carrelloUpdate"]+'&idiv=true&'+form.serialize();

		loadHTML_execJs(url);
		scrollTop();
	}
	return false;
}

function ecCart_step2()
{
	isOk_checkAll = true;
	checkAll_onchange('ecCart');
	if (isOk_checkAll)
	{
		var form = $('#ecCart');
		url = ecUrlAction["step2"]+'&idiv=true&'+form.serialize();
		loadHTML(url, 'idivMain', "scrollTop();");
		divSpinnerAdd('idivMain', "position:fixed; top:0px; left:0px; background-color: #efefef; border: solid 1px white;", 'caricamento in corso...');
	}
	return false;
}

function ecCheckQuant_minMaxUni(obj, oldValue, min, max, unit)
{
	var btnRicalcola = document.getElementById('btnRicalcola');
	if (btnRicalcola)
	{
		btnRicalcola.className = 'displayInline ecBtn_selected';
	}
	var btnAvanti = document.getElementById('btnAvanti');
	if (btnAvanti)
	{
		btnAvanti.className = 'displayInline ecBtn_normal';
		btnAvanti.disabled	= 'disabled';
	}

	if (obj.oldValue)
	{
		oldValue = obj.oldValue;
	}
	obj.value = parseInt(obj.value);
	if (obj.value == 'NaN')
	{
		obj.value = min;
	}

	var error_1 = (obj.value < 0);
	var error_2 = (min && (obj.value < min));
	var error_3 = (max && (obj.value > max));
	var error_4 = ((obj.value%unit) != 0);

	var problemi ='';
	if (error_1 || error_2)
	{
		problemi += 'la quantità minima consentita è di <strong>'+min+'</strong>\n';
	}
	else if (error_3)
	{
		problemi += 'la quantità massima consentita è di <strong>'+max+'</strong>\n';
	}
	else if (error_4)
	{
		prossimaQuantita = parseInt(obj.value) + unit - parseInt(obj.value % unit);
		problemi += 'si possono acquistare solo multipli di <strong>'+unit+'</strong><br/>la quantità suggerita è di <strong>'+prossimaQuantita+'</strong>\n';
	}

	isOk = (problemi == '');
	if (!isOk)
	{
		// obj.value = oldValue;
		
		if (!obj.className_before)
		{
			obj.className_before = obj.className;
		}
		obj.className = obj.className_before+' errorReport';
		obj.title = 'la quantità indicata non è valida';

		var messageId = addPopup_fadeout(obj.parentNode, 'ec_errorReport_PUF displayInline', nl2br(trim(problemi), false), 5000*1000, 100);

		if (!obj.messaggiAppesi)
		{
			obj.messaggiAppesi = new Array();
		}
		messaggiAppesi_hiddeAll(obj);
		obj.messaggiAppesi.push(messageId);

		jq_blink(obj.id);
	}
	else
	{
		messaggiAppesi_hiddeAll(obj);
		if (obj.className_before)
		{
			obj.className = obj.className_before+' okReport';
			obj.title = 'quantità corretta';
		}
		else
		{
			obj.title = '';
		}
	}
	obj.oldValue = obj.value;

	isOk_checkAll &= isOk;
	return isOk;
}

function messaggiAppesi_hiddeAll(obj)
{
	if (obj.messaggiAppesi)
	{
		while (obj.messaggiAppesi.length)
		{
			var idAppeso = obj.messaggiAppesi.pop();
			var objAppeso = document.getElementById(idAppeso);
			if (objAppeso)
			{
				objAppeso.style.display = 'none';
			}
		}
	}
}

function ecProdotto_ckeck(idProdotto)
{
	obj = document.getElementById('cartQuantita_'+idProdotto);
	if (obj)
	{
		fireEvent(obj, 'change');
	}
}

function ecAccountAddress_manager(type, tipoIndirizzo)
{
	if (type == 'apply')
	{
		var url = ecUrlAction["accountEdit_indirizzo"]+'&'+$('#ecFormEditAddress').serialize();
		loadHTML_execJs(url);
	}
	else if (type == 'modify')
	{
		var url = ecUrlAction["accountEdit_indirizzo"]+'&tipoIndirizzo='+tipoIndirizzo;
		loadHTML(url, 'ecDiv_accountEdit_indirizzo', "");
	}
	else
	{
		var url = ecUrlAction["accountView_indirizzo"]+'&actionType='+type+'&tipoIndirizzo='+tipoIndirizzo;
		loadHTML(url, 'ecDiv_accountEdit_indirizzo', "");
	}
}

function ecUpdate_modalitaSpedizione(obj)
{
	var url = ecUrlAction["step3_update"]+'&idModalitaSpedizione='+obj.value+'&idiv=true';
	openInsideAddIDIV(url);
}

function ecUpdate_modalitaPagamento(obj)
{
	var url = ecUrlAction["step3_update"]+'&idModalitaPagamento='+obj.value+'&idiv=true';
	openInsideAddIDIV(url);
}

function ecConfermaAcquisto()
{
	var obj = document.getElementById('ec_consensoPrivacy');
	if (obj.checked)
	{
		// loadHTML_execJs(ecUrlAction['step5'], storage='', jsAfterOpen='');
		openLocation(ecUrlAction['step5']);
	}
	else
	{
		jq_blink('ec_boxCondizioniContrattuali');
	}
}


function ecStampaRicevuta()
{
	
}


function select_boxPay(idSelected)
{
	var boxPay = document.getElementById('boxPay');
	if (boxPay)
	{
		if (boxPay.hasChildNodes())
		{
			var children = boxPay.childNodes;
			for (var i = 0; i < children.length; i++) 
			{
				if (children[i].nodeName == 'FORM')
				{
					isSelected = (children[i].id == idSelected);
					isHidden = (children[i].style.display == 'none');

					if (!isSelected)
					{
						$(children[i]).slideUp();
					}
					else
					{
						$(children[i]).slideDown();
					}

					scrollTopPixel(450);
				}
			}
		}
	}
}

function ecPaySendForm(formObj, maxImporto)
{
	var isOk = true;
	isOk &= (formObj.importo.value <= maxImporto);
	isOk &= (formObj.importo.value >0);
	if (!isOk)
	{
		jq_blinkObj(formObj.importo);
		formObj.importo.value = maxImporto;
	}
	else
	{
		var url = formObj.action+'?'+$(formObj).serialize()+'&idiv=true';
		openInside(url, 'scrollTop();');
	}
	return false;
}

function ecPayAction(idEcOrdine, idEcPagamento, azione)
{
	if (azione == 'delete')
	{
		var url = ecUrlAction["pagamento"]+'&idEcOrdine='+idEcOrdine+'&idEcPagamento='+idEcPagamento+'&idiv=true&ecAct=delete';
		openInside(url, 'scrollTop();');
	}
	else if (azione == 'modify')
	{
		var url = ecUrlAction["pagamento"]+'&idEcOrdine='+idEcOrdine+'&idEcPagamento='+idEcPagamento+'&idiv=true&ecAct=modifyBox';
		openInside(url, 'scrollTopPixel(420);');
	}
	else if (azione == 'view')
	{
		var url = ecUrlAction["pagamento"]+'&idEcOrdine='+idEcOrdine+'&idEcPagamento='+idEcPagamento+'&idiv=true&ecAct=viewBox';
		openInside(url, 'scrollTopPixel(420);');
	}
}

function ecAction_open(ecAction, extraUrl, notStoreBack)
{
	url = ecUrlAction[ecAction]+extraUrl;

	if (!notStoreBack)
	{
		callBack_add(url);
		scrollTop();
	}
	openLocation(url);
}

function ecAction_openInside(ecAction, extraUrl)
{
	url = ecUrlAction[ecAction]+extraUrl;
	callBack_add(url);
	openInside(url+'&idiv=true');
	scrollTop();
}

function changeFiltroCatalogo(formId)
{
	var formObj = document.forms[formId];

	var isOkFormato = true;
	isOkFormato &= (formObj.formato.value != '-E1');
	isOkFormato &= (formObj.formato.value != '-E2');
	isOkFormato &= (formObj.formato.value != '-I1');
	isOkFormato &= (formObj.formato.value != '-I2');
	if (!isOkFormato)
	{
		formObj.formato.value = '';
	}

	if (isOkFormato)
	{
		formObj.submit();
	}

	return false;
}

var boxAperti = 0;
var boxApertiTime = new Array();
function ecOpenPriceOpen(idDiv)
{
	// $('#'+idDiv).animate({width: 'toggle'}, 'fast', 'linear', function() { changeMargin(idDiv) } );
	var obj = document.getElementById(idDiv);
	if (obj.style.display != 'block')
	{
		$('#'+idDiv).fadeIn('fast', function() { changeMargin(idDiv, +1) } );
	}
	ecOpenPriceOver(idDiv);
}

function ecOpenPriceOver(idDiv)
{
	// BugTRACK_info('verde', 'ecOpenPriceOver');
	boxApertiTime[idDiv] = false;
}

function ecOpenPriceClose(idDiv)
{
	// BugTRACK_info('rosso', 'ecOpenPriceClose');
	$('#'+idDiv).fadeOut('fast', function() { changeMargin(idDiv, -1) } );
}

function ecOpenPriceOut(idDiv)
{
	// BugTRACK_info('rosso', 'ecOpenPriceOut');
	boxApertiTime[idDiv] = true;
	setTimeout("ecOpenPriceOut__('"+ idDiv +"');", 2000);
}

function ecOpenPriceOut__(idDiv)
{
	// BugTRACK_info('rosso', 'ecOpenPriceOut__');
	if (boxApertiTime[idDiv])
	{
		ecOpenPriceClose(idDiv);
		boxApertiTime[idDiv] = false;
	}
}


function changeMargin(idDiv, verso)
{
	var obj = document.getElementById(idDiv);

	boxAperti += verso;

	if (!boxAperti)
	{
		$('#mainConteiner01').animate({paddingRight: '0'}, 'fast');
	}
	else
	{
		$('#mainConteiner01').animate({paddingRight: '130px'}, 'fast');
	}

}
