﻿if(Prototype.Browser.IE)
{
	addEvent(window, 'load', function() {
		var el = document.getElementsByTagName("input");
		for (i=0;i<el.length;i++)
		{
			var type = el[i].getAttribute("type");
			if((type=="checkbox")||(type=="radio"))
				el[i].style.border = "none";
		}
	});
}
// Common javascript funtionality
function roundTo(decimalpositions)
{
    var i = this * Math.pow(10,decimalpositions);
    i = Math.round(i);
    return i / Math.pow(10,decimalpositions);
}
Number.prototype.roundTo = roundTo;

var mousex = mousey = 0;
function _setMousePosition(e) {
	if (!e) e = window.event;
	if (e) {
		mousex = Event.pointerX(e);
		mousey = Event.pointerY(e);
	}
}
function SV(v)
{
	var k;
	if(v==null)return "";
	if(!v.toString)return "";
	v=v.toString();
	v=v.replace(/&/g,"&amp;").replace(/"/g,"&quot;");
	for(k=0; k<32; k++)
		while(v.indexOf(String.fromCharCode(k))>=0)
			v=v.replace(String.fromCharCode(k),"&#"+k+";");
	return v;
}
function SH(v)
{
	if(v==null)return "";
	if(typeof(v)!="string")
	{
		if(!v.toString)return "";
		v=v.toString();
	}
	return v.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\n/g,"<br/>");
}
function SHT(s)
{
	var o,k;
	if(s==null)return "";
	s=s.toString();
	if(!s.length)return "";
	o=document.createElement("div");
	s=s.replace(/\r\n/g,"\n").replace(/\r/g,"\n").split("\n");
	for(k=0;k<s.length;k++)
	{
		if(k)
			o.appendChild(document.createElement("br"));
		o.appendChild(document.createTextNode(s[k]));
	}
	return o.innerHTML;
}
function DateToText(d,TwoYears)
{
	var s="",n;
	if(d.getDate()<10)s+="0";
	s+=d.getDate()+"/";
	if(d.getMonth()<9)s+="0";
	s+=(d.getMonth()+1)+"/";
	if(TwoYears)
	{
		n=d.getFullYear()%100;
		s+=((n<10)?"0":"")+n;
	}
	else
		s+=d.getFullYear();
	return s;
}
function TimeToText(t)
{
	var s="";
	if(t.getHours()<10)s+="0";
	s+=t.getHours()+":";
	if(t.getMinutes()<10)s+="0";
	s+=t.getMinutes();
	return s;
}
function DatetimeToText(dt)
{
	return DateToText(dt)+" "+TimeToText(dt);
}
function MoneyToHtml(v)
{
	var s;
	if(v==null)return "";
	v=Math.round(v*100)
	if(!v)return "0,00";
	s=v.toString();
	while(s.length<3)s="0"+s;
	return s.substr(0,s.length-2)+","+s.substr(s.length-2) + " " + money;
}
function fmtZip(s)
{
	return s.replace(/[ \t\r\n]/g,"").toUpperCase();
}
function fmtCounty(s)
{
	var s=s.replace(/[ \t\r\n]/g,"");
	if(s.length==2)
		return s.toUpperCase();
	else
		return belStrS(s);
}
function fmtEmail(s)
{
	return s.replace(/[ \t\r\n]/g,"").toLowerCase();
}
function fmtVatFC(s)
{
	return s.replace(/[ \t\r\n]/g,"").toUpperCase();
}
function popupWindow(mypage, myname, w, h, scroll, tools) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+', width='+w+', top='+wint+', left='+winl+', scrollbars='+scroll+', toolbar='+tools+', resizable=1';
	if(tools == 1 && !Prototype.Browser.IE)
		winprops += ", menubar=1";
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}
function trim(str) {
   return rtrim(ltrim(str));
}
function onlyNumbers(evt) {
   var charCode = (evt.which) ? evt.which : evt.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;
   return true;
}
function onlyMoney(evt)
{
   var charCode = (evt.which) ? evt.which : evt.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode != 44 && charCode != 46))
      return false;
   return true;
}
function onlyDecimal(evt, allowNegative) {
   var charCode = (evt.which) ? evt.which : evt.keyCode
   if(allowNegative)
   {
	   if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode != 44 && charCode != 45 && charCode != 46))
		   return false;
	}
   else
   {
	   if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode != 44 && charCode != 46))
		   return false;
	}
   return true;
}
function GetTextContent(o)
{
	if(o==null)return "";
	if(typeof(o.innerText)=="string")return o.innerText;
	if(typeof(o.textContent)=="string")return o.textContent;
	return "";
}
function isEmail(s)
{
	var rx;
	if(s==null)return false;
	if(typeof(s)!="string")return false;
	rx=new RegExp("^([a-z0-9_\\.\\-])+\\@(([a-z0-9\\-])+\\.)+([a-z0-9]{2,4})+$", "gi");
	return rx.test(s);
}
function isCodFisPartIVA(s)
{
	if((s!=null)&&(s=="-----------"))return true;
	return isCodFis(s)||isPartIVA(s);
}
function isCodFis(s)
{
	var S=0,i,A="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",B="ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ",P="ABCDEFGHIJKLMNOPQRSTUVWXYZ",D="BAKPLCQDREVOSFTGUHMINJWZYX",p;
	if(s==null)return false;
	if(typeof(s)!="string")s=s.toString();
	if(s.length!=16)return false;
	s=s.toUpperCase().replace(/\W/g,"");
	if(s.length!=16)return false;
	for(i=0;;i+=2)
	{
		if((p=A.indexOf(s.charAt(i)))<0)return false;
		S+=D.indexOf(B.charAt(p));
		if(i==14)break;
		if((p=A.indexOf(s.charAt(i+1)))<0)return false;
		S+=P.indexOf(B.charAt(p));
	}
	return (S%26)==(s.charCodeAt(15)-"A".charCodeAt(0));
}
function isPartIVA(s)
{
	var S=0,k,v;
	if(s==null)return false;
	if(typeof(s)!="string")s=s.toString();
	if(s.length!=11)return false;
	s=s.replace(/\D/g,"");
	if(s.length!=11)return false;
	for(k=0;k<9;k+=2)
	{
		S+=parseInt(s.charAt(k),10);
		if((v=2*parseInt(s.charAt(k+1),10))>9)
			v-=9;
		S+=v;
	}
	return ((10-(S%10))%10)==parseInt(s.charAt(10),10);
}
function toMoney(n)
{
	var s,n2,p;
	n2=Math.round(n*100)/100;
	s=(""+n2).replace(/\./g,",");
	p=s.lastIndexOf(",");
	if(p==0)
		s="0"+s;
	else if(p<0)
		s+=",00";
	else if(p==s.length-2)
		s+="0";
	return s;
}
function firstToUpper(word) {
	firstLetter = word.substring(1, 0);
	restOfWord = word.substring(1);
	return (firstLetter.toUpperCase()+restOfWord);
}
// Top menu rollover
function topLinkOver(iId) {
	var Td = $(iId);
	Td.style.backgroundPosition = 'center 0';
}
function topLinkOut(iId) {
	var Td = $(iId);
	Td.style.backgroundPosition = 'center -26px';
}
function topLinkHOver(iId) {
	var Td = $(iId);
	Td.style.backgroundPosition = 'right 0';
}
function topLinkHOut(iId) {
	var Td = $(iId);
	Td.style.backgroundPosition = 'right -38px';
}
// Init check
var WebChatText = null, RegInfoTitle = null, RegInfoText = null, SiteName = null;
function initCheck() {
	if($('trLayPreBotHome') && $('trLayPreBotHome').childNodes[0] && $('trLayPreBotHome').childNodes[0].innerHTML.length == 0)
		Element.hide('trLayPreBotHome');
	if($F('hfScriptName').toLowerCase().indexOf('default') < 0 ) {
		if(Element.getHeight('tdLayMidSx') < 700)	$('tdLayMidSx').style.height = '700px';
		if(($F('hfScriptName').toLowerCase().indexOf('product') >= 0)) {
			new Tip('ctl00_tdLayRightChat', WebChatText, {
				title: 'Live Support',
				hideAfter: true,
				stem: 'rightTop',
				hook: { target: 'leftTop', tip: 'topRight' },
				width: 170,
				style: 'protogrey'
			});
		}
		if($F('hfScriptName').toLowerCase().indexOf('login') < 0 ) {
			if($('linkRegister')) {
				if(readCookie('shopperRegInfoTipReaded') == null) {
					new Tip('linkRegister', RegInfoText, {
						title: RegInfoTitle.replace('{0}', SiteName),
						closeButton: true,
						hideOn: { element: 'closeButton', event: 'click' },
						stem: 'rightTop',
						hook: { tip: 'topRight', mouse: false },
						width: 170,
						style: 'redtopten'
					});
					$('linkRegister').prototip.show();
					$('linkRegister').observe('prototip:hidden', function() {
						createCookie('shopperRegInfoTipReaded', '1', 30);
					});
					if($F('hfScriptName').toLowerCase().indexOf('default') >= 0 )
						createCookie('shopperRegInfoTipReaded', '1', 30);
				}
				else {
					new Tip('linkRegister', RegInfoText, {
						title: RegInfoTitle.replace('{0}', SiteName),
						closeButton: false,
						hideOn: 'mouseleave',
						stem: 'rightTop',
						hook: { tip: 'topRight', mouse: false },
						width: 170,
						style: 'redtopten'
					});
				}
				$('linkRegister').observe('prototip:shown', function() {
					setTipOpacity('redtopten', 0.9);
				});
			}
		}
	}
	if($F('hfScriptName').toLowerCase().indexOf('carrello') >= 0 ) {
		Element.hide('linkGoToCart');
	}
	Event.observe('tbMainSearch', 'keydown', mainSearchKD);
	Event.observe('tbMainSearch', 'blur', mainSearchBlur);
	Event.observe('tbMainSearch', 'focus', mainSearchFocus);
}
// Init login
var tbLogin = null, tbPassword = null;
function checkLogin() {
	if($F(tbLogin).length == 0 || $F(tbPassword).length == 0) return false;
	return true;
}
// Init caddie promo
var jsonObjCaddie = null;
var caddieBaseProdsList = null, promoProdsList = null;
function refreshCP(cartSpan) {
	if(!Object.isUndefined(cartSpan) && cartSpan != null && cartSpan.length > 0)
		$(cartSpan).className = 'AddToCartPresent';
	jsonObjCaddie.loadCartProds(initCaddiePromo);
}
function initCaddiePromo(response) {
	caddieBaseProdsList = response.result;
	if(caddieBaseProdsList.length == 0) {
		if($F('hfScriptName').toLowerCase().indexOf('default') > 0 )
		{ }
		else
			loadWeeklyPromoHandler();
	}
	else {
		if($F('hfScriptName').indexOf('carrello') > 0 || $F('hfScriptName').indexOf('ordine') > 0 || $F('hfScriptName').indexOf('ordini') > 0 || $F('hfScriptName').indexOf('login') > 0) {
			loadWeeklyPromoHandler();
		}
		else {
			if($F('hfScriptName').indexOf('default') > 0 )
			{ }
			else
				loadCaddieProds();
		}
	}
}
// Caddie
function loadCaddieProds() {
	Element.hide('cntPromo');
	Element.show('cntCaddie');

	var Tb = $('tbCaddieProds');
	var n = Tb.rows.length;
		while(n>0) Tb.deleteRow(--n);

	var R, Td, Th, I;
	if(caddieBaseProdsList.length == 0) {
		return;
	}
	var caddieTots = jsonObjCaddie.loadCart();

	if(WHCS != null && WHCS.timer != null) {
		WHCS.stop();
		Element.hide(WHCS.UI.Container);
	}
	for(var i=0; i<caddieBaseProdsList.length; i++) {
		var tipContent = document.createElement('div');
		var tipContentHTML = 'Cod.: <b>' + caddieBaseProdsList[i].barCode + '</b><br />';
		tipContentHTML += caddieBaseProdsList[i].price + '<br />';
		if(!Object.isUndefined(caddieBaseProdsList[i].featvalsDescr))
			tipContentHTML += '<b>' + caddieBaseProdsList[i].featvalsDescr + '</b><br />';
		tipContentHTML += caddieBaseProdsList[i].descrShort.replace(/\"/g,'\'');

		tipContent.innerHTML = tipContentHTML;
		tipContent.id = 'cellMiniCaddieCnt_' + caddieBaseProdsList[i].prodId + '_' + i;
		tipContent.style.display = 'none';

		R = document.createElement('tr');
		R.id = 'rowMiniCaddie_' + caddieBaseProdsList[i].prodId;
		R.idx = (i%2);
		R.className = 'row'+R.idx;

		Td = document.createElement('td');
		Td.style.paddingLeft = '2px';
		Td.appendChild(tipContent);
		R.appendChild(Td);

		Td = document.createElement('td');
		var pcode = '';
		if(caddieBaseProdsList[i].barCode.length > 6)
			pcode = caddieBaseProdsList[i].barCode.substr(0, 5) + '<span class="small">&hellip;</span>';
		else
			pcode = caddieBaseProdsList[i].barCode;
		Td.innerHTML = pcode;
		R.appendChild(Td);

		Td = document.createElement('td');
		Td.appendChild(document.createTextNode(caddieBaseProdsList[i].quantity));
		Td.className = 'price';
		R.appendChild(Td);

		Td = document.createElement('td');
		Td.appendChild(document.createTextNode(toMoney(caddieBaseProdsList[i].totTot)));
		Td.className = 'price';

		R.appendChild(Td);

		Tb.appendChild(R);

		new Tip(R, $(tipContent.id).cloneNode(true), {
			title: caddieBaseProdsList[i].name.replace(/\"/g,'\''),
			hideAfter: true,
			stem: 'rightTop',
			hook: { target: 'leftTop', tip: 'topRight' },
			width: 300,
			style: 'darkgrey'
		});

		$(R).observe('prototip:shown', function() {
			setTipOpacity('darkgrey', 0.9);
		});
		R.onmouseover = function() {
			$(this).removeClassName(this.className);
			$(this).addClassName('row2');
		}
		R.onmouseout = function() {
			$(this).removeClassName(this.className);
			$(this).addClassName('row'+this.idx);
		}
	}
	R = document.createElement('tr');
	Th = document.createElement('th');
	Th.style.paddingLeft = '2px';
	R.appendChild(Th);
	Th = document.createElement('th');
	Th.appendChild(document.createTextNode('Tot:'));
	R.appendChild(Th);
	Th = document.createElement('th');
	Th.appendChild(document.createTextNode(caddieTots.totQuantity));
	Th.className = 'price';
	R.appendChild(Th);
	Th = document.createElement('th');
	Th.className = 'price';
	Th.appendChild(document.createTextNode(caddieTots.totGoods));
	R.appendChild(Th);

	Tb.appendChild(R);
}
// Weekly promo slider
var WHCS = null;
function loadWeeklyPromoHandler() {
	Element.hide('cntCaddie');
	Element.show('cntPromo');

	Element.hide('promoCnt');
	var colH = Element.getHeight('tdLayMidDx');
	if(colH < 250) { colH = 300; } else if(colH > 500) { colH = 500; } else { colH = (colH - 165); }
	$('promoCnt').style.height = colH + 'px';
	Element.show('promoCnt');
	WHCS = new WeeklyHilightsColScroller();
	WHCS.init();
}
function WeeklyHilightsColScroller() {
	this.scrollerYi	= 0;
	this.speed			= 2;
	this.timer			= null;
	this.availHeight	= null;
	this.totalHeight	= null;
	this.UI =
	{
		Container:	$('promoCnt'),
		Table:		$('tblPromo')
	};
}
WeeklyHilightsColScroller.prototype=
{
	init:function() {
		Event.observe(this.UI.Container, 'mouseover', WHCS.stop);
		Event.observe(this.UI.Container, 'mouseout', WHCS.start);
		Event.observe(this.UI.Container, 'mousewheel', WHCS.scroll);
		Event.observe(this.UI.Container, 'DOMMouseScroll', WHCS.scroll);
		this.availHeight = parseInt(this.UI.Container.offsetHeight);
		this.totalHeight = parseInt(this.UI.Table.offsetHeight);
		this.loop();
	},
	loop:function() {
		this.scrollerYi = this.scrollerYi + this.speed;
		this.UI.Container.scrollTop = this.scrollerYi;
		if (this.scrollerYi > this.UI.Container.scrollHeight - 160) { this.scrollerYi = 0; }
		this.timer = setTimeout("WHCS.loop()", 100);
		return false;
	},
	stop:function(evt) {
		if(!Object.isUndefined(evt)) Event.stop(evt);
		clearTimeout(WHCS.timer);
		return false;
	},
	start:function(evt) {
		if(mouseIsInDiv(evt, WHCS.UI.Table)) { Event.stop(evt); return false; }
		Event.stop(evt);
		WHCS.loop();
		return false;
	},
	scroll:function(evt) {
		Event.stop(evt);
		var d=0;
		if(!Object.isUndefined(evt))
			d = Event.wheel(evt);
		if(!d) return true;

		var ScrollTop = WHCS.UI.Container.scrollTop;
		if(d > 0) ScrollTop -= (d * 15);
		else		 ScrollTop += (d * -15);

		if (ScrollTop >= (WHCS.totalHeight - WHCS.availHeight))
			ScrollTop = (WHCS.totalHeight - WHCS.availHeight - 1);
		else if (ScrollTop < 0)
			ScrollTop = 0;

		WHCS.UI.Container.scrollTop = WHCS.scrollerYi = ScrollTop;
		return false;
	}
};
function mouseIsInDiv(e, container) {
	Position.prepare();
	return Position.within($(container), Event.pointerX(e), Event.pointerY(e));
}
Object.extend(Event, {
	wheel:function (event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120;
			if (window.opera) delta = -delta;
		} else if (event.detail) { delta = -event.detail/3; }
		return Math.round(delta); //Safari Round
	}
});
// Main search
function mainSearch(wath) {
	wath = trim(wath).toString().toLowerCase();
	if(wath.length == 0) return false;
	if($F('hfScriptName').toLowerCase().indexOf('cerca') >= 0 ) {
		$('wath').value = wath;
		$('tbMainSearch').value = '';
		search();
	}
	else
		document.location = 'cerca.aspx?t=' + encodeURIComponent(wath);
}
function mainSearchKD(evt) {
   var charCode = (evt.which) ? evt.which : evt.keyCode
	if(charCode == Event.KEY_RETURN) {
		Event.stop(evt);
		mainSearch($F('tbMainSearch'));
	}
	return false;
}
function mainSearchBlur(evt) {
	Element.removeClassName('tbMainSearch', 'mainSearchActive')
	Element.addClassName('tbMainSearch', 'mainSearchIdle')
}
function mainSearchFocus(evt) {
	Element.removeClassName('tbMainSearch', 'mainSearchIdle')
	Element.addClassName('tbMainSearch', 'mainSearchActive')
}
// Warnings tip
var WarningTitle = null;
function loadWarningsTip() {
	setTimeout('showWarningsTip()', 2500);
}
function showWarningsTip() {
	Lightview.show({ href: '#hiddenwarnings', rel: 'inline', title: WarningTitle, options: { autosize: true }})
	var tt = document.getElementsByClassName('lv_Title'); tt[0].style.color = '#D01D1D';
	tt = document.getElementsByClassName('warnTitle');
	for(var x=0; x<tt.length; x++) {
		tt[x].style.backgroundColor = '#434542';
		tt[x].style.border = '1px solid #434542';
	}
	tt = document.getElementsByClassName('warnBody');
	for(var x=0; x<tt.length; x++) {
		tt[x].style.backgroundColor = '#F7F7F7';
		tt[x].style.border = '1px solid #434542';
	}
	setTimeout('hideWarningsTip()', 15000);
}
function hideWarningsTip() {
	Lightview.hide();
}
// Cookies functions
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
// Other functions
function openNewsLetter() {
	if(!$('newsLetterEmail')) return;
	var email = $F('newsLetterEmail').replace(/\s/g,"").toLowerCase();
	popupWindow("newsletter_iscrizione.aspx?e=" + escape(email), "newsletterIscrizione", 450, 400, 1, 0);
}
function setTipOpacity(tipstyle, opacity) {
	var tt = document.getElementsByClassName('prototip');
	for(var t=0; t<tt.length; t++) {
		if($(tt[t]).innerHTML.indexOf(tipstyle) >= 0)
			$(tt[t]).setStyle({ opacity: opacity });
	}
}