Y.namespace('CNA');
var CNA = Y.CNA;
CNA.lang=new Object();

MEB.MODULES_BASE='/modules/';
MEB.AJAX_BASE='/ajax/'; 
MEB.JSON_BASE='http://www.insiemecna.com/json/';

MEB.JS_BASE='http://www.insiemecna.com/js/';
MEB.IMG_BASE='http://www.insiemecna.com/img/';
MEB.SOUNDS_BASE='http://www.insiemecna.com/sounds/';

MEB.CSS_BASE='http://www.insiemecna.com/css/';
MEB.LANG_BASE='http://www.insiemecna.com/js/';
MEB.DOMAIN='insiemecna.com';
MEB.LOADING_IMG_URL= MEB.IMG_BASE+'loading.gif';

MEB.SITE_URL='http://www.insiemecna.com/';

/* CLIENT */
CNA.Client=function(configs)
{
	CNA.Client.superclass.constructor.call(this,configs);
	this.logged = configs.id;
}
Y.lang.extend(CNA.Client, MEB.CoreClient,{
	
});

CNA.Page=function(configs)
{
	if(configs)
	{
		this._country = configs.country;
		this._myAddressValue = configs.myAddress;
		this._baseURL = configs.baseURL?configs.baseURL:null;
	}
	
	this._btnShop = Y.util.Dom.get('btn-shop');
	this._btnServizi = Y.util.Dom.get('btn-servizi');
	this._shopCat = Y.util.Dom.get('overcont-shop');
	this._serviziCat = Y.util.Dom.get('overcont-servizi');
	
	Y.util.Event.addListener(this._btnShop,'mouseover',this._showShop,null,this);
	Y.util.Event.addListener(this._shopCat,'mouseover',this._showShop,null,this);
	Y.util.Event.addListener(this._btnShop,'mouseout',this._outShop,null,this);
	Y.util.Event.addListener(this._shopCat,'mouseout',this._outShop,null,this);
	Y.util.Event.addListener(this._btnServizi,'mouseover',this._showServizi,null,this);
	Y.util.Event.addListener(this._serviziCat,'mouseover',this._showServizi,null,this);
	Y.util.Event.addListener(this._btnServizi,'mouseout',this._outServizi,null,this);
	Y.util.Event.addListener(this._serviziCat,'mouseout',this._outServizi,null,this);
	
	var cat =  Y.util.Dom.getElementsByClassName('cat-icon','div',this._shopCat);	
	for(var i=0;i<cat.length;i++){
		Y.util.Event.addListener(cat[i],'mouseover',this._showSubCatShop,cat[i],this);
	}
	
	cat =  Y.util.Dom.getElementsByClassName('cat-icon','div',this._serviziCat);
	for(var i=0;i<cat.length;i++){
		Y.util.Event.addListener(cat[i],'mouseover',this._showSubCatShop,cat[i],this);
	}
	
	this._searchInput = Y.util.Dom.get('header-search-input');
	if(configs && configs.what)
		this._searchInput.value = configs.what;
	else
		this._searchInput.value = CNA.lang.common['header_input_search'];
	Y.util.Event.addListener(this._searchInput,'click',this._inputClick,null,this);
	var items = new Array();
	for(var i=0;i<CNA.lang.common['header_input_type'].length;i++)
		items.push({value: i, text:CNA.lang.common['header_input_type'][i]});
	this._searchType = Y.util.Dom.get('header-search-type');
	this._searchSelect = new MEB.Select({id:'header-search-select', items: items, selectedIndex: (configs && configs.type?configs.type:0)});
	this._searchSelect.onChange.subscribe(this._changeSearchType,null,this);
	
	this._searchNearBtn = Y.util.Dom.get('header-search-near');
	Y.util.Event.addListener(this._searchNearBtn,'click',this._searchNear,null,this);
	
	if(cl.logged){
		this._btnLogout = Y.util.Dom.get('user-logout');
		Y.util.Event.addListener(this._btnLogout,'click',this._logout,null,this);
	}else{
		this._btnAccess = Y.util.Dom.get('user-access');
		Y.util.Event.addListener(this._btnAccess,'click',this._loginPopup,null,this);
	}
	this._btnCart = Y.util.Dom.get('user-cart');
	Y.util.Event.addListener(this._btnCart,'click',this._cartPopup,null,this);
	
	this._headerLocaitonBtn = Y.util.Dom.get('header-location');
	Y.util.Event.addListener(this._headerLocaitonBtn,'click',this._openCnaLocation,null,this);
	this._headerLocaitonLink = Y.util.Dom.get('header-location-link');
	Y.util.Event.addListener(this._headerLocaitonLink,'click',this._openCnaLocation,null,this);
	this._headerLocaitonContainer = Y.util.Dom.get('header-location-container');
	
	new MEB.InitTips();
}

CNA.Page.prototype = {
	_timerShop:null,_timerServizi:null,_catActive:null,_subCatActive:null,_contActive:null,_headerLocationOpen:false,
	_showShop: function()
	{
		if(this._timerServizi){
			this._hideServizi();
		}
		if(this._timerShop){
			this._timerShop.cancel();
			this._timerShop = null;
		}else{
			var r = Y.util.Dom.getRegion(this._shopCat);
			var w = r.right-r.left;
			var vw = Y.util.Dom.getViewportWidth();
			Y.util.Dom.setStyle(this._shopCat,'left',((vw-w)/2)+'px' );
			Y.util.Dom.addClass(this._btnShop,'item-2-over');
		}
	},
	_outShop: function()
	{
		this._timerShop = Y.lang.later(300,this,this._hideShop);
	},
	_hideShop: function()
	{
		if(this._catActive)
		{
			Y.util.Dom.removeClass(this._catActive,'over');
			Y.util.Dom.removeClass(this._subCatActive,'over');
			Y.util.Dom.removeClass(this._contActive,'over');
			this._contActive = null;
		}
		this._timerShop = null;
		Y.util.Dom.setStyle(this._shopCat,'left','-9000px');
		Y.util.Dom.removeClass(this._btnShop,'item-2-over');
	},
	_showServizi: function()
	{	
		if(this._timerShop){
			this._hideShop();
		}
		if(this._timerServizi){
			this._timerServizi.cancel();
			this._timerServizi = null;
		}else{
			var r = Y.util.Dom.getRegion(this._serviziCat);
			var w = r.right-r.left;
			var vw = Y.util.Dom.getViewportWidth();
			Y.util.Dom.setStyle(this._serviziCat,'left',((vw-w)/2)+'px' );
			Y.util.Dom.addClass(this._btnServizi,'item-2-over');
		}
	},
	_outServizi: function()
	{
		this._timerServizi = Y.lang.later(300,this,this._hideServizi);
	},
	_hideServizi: function()
	{
		if(this._catActive)
		{
			Y.util.Dom.removeClass(this._catActive,'over');
			Y.util.Dom.removeClass(this._subCatActive,'over');
			Y.util.Dom.removeClass(this._contActive,'over');
			this._contActive = null;
		}
		this._timerServizi = null;
		Y.util.Dom.setStyle(this._serviziCat,'left','-9000px');
		Y.util.Dom.removeClass(this._btnServizi,'item-2-over');
	},
	_showSubCatShop:function(e,arg)
	{
		if(this._catActive)
		{
			Y.util.Dom.removeClass(this._catActive,'over');
			Y.util.Dom.removeClass(this._subCatActive,'over');
		}
		
		if(!this._contActive)
		{
			var type = arg.id.substring(0,3)=='cat'?'cat':'serv';
			this._contActive = Y.util.Dom.get('sub-'+type+'-container');
			Y.util.Dom.addClass(this._contActive,'over');
		}

		this._catActive = arg;
		this._subCatActive = Y.util.Dom.get('sub-'+arg.id);
		Y.util.Dom.addClass(this._catActive,'over');
		Y.util.Dom.addClass(this._subCatActive,'over');

		//ALtezza contenitore
		var region = Y.util.Dom.getRegion(this._subCatActive);
		this._contActive.style.height = (region.bottom-region.top)+'px';
		
		
		if(!this._subCatActive.style.marginLeft)
		{
			var region2 = Y.util.Dom.getRegion(this._catActive);
			if(Y.util.Dom.hasClass(this._subCatActive,'latest'))
				this._subCatActive.style.marginLeft = region2.right - region.right-42+'px';		
			else
				this._subCatActive.style.marginLeft = region2.left - region.left+42+'px';		
		}
	},
	_changeSearchType:function()
	{
		this._searchType.value = this._searchSelect.value;
	},
	_searchNear:function()
	{
		if(!this.checkHeaderSearch())
			return false;
		if(!this._myAddressValue)
		{
			var p = new CNA.ChangeMyaddressPopup();
			p.onClose.subscribe(this._closeMyaddress,null,this);
		}
		else
		{
			Y.util.Dom.get('header-search-near-input').value = 20;
			Y.util.Dom.get('header-search-form').submit();
		}
	},
	_closeMyaddress:function(arg)
	{
		if(arg)
		{
			Y.util.Dom.get('header-search-near-input').value = 20;
			Y.util.Dom.get('header-search-form').submit();
		}
	},
	checkHeaderSearch: function()
	{
		if(this._searchInput.value==CNA.lang.common['header_input_search'] || this._searchInput.value=='')
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target:this._searchInput,align:'b',type:'error',content:CNA.lang.common['header_input_search_error'],closeButton:false,closeOnChange:true});
			return false;
		}
		return true;
	},
	_inputClick:function()
	{
		if(this._searchInput.value==CNA.lang.common['header_input_search'])
			this._searchInput.value = '';
	},
	_loginPopup:function()
	{
		new CNA.LoginPopup();
	},
	_logout:function()
	{
		new MEB.Connect('POST', MEB.AJAX_BASE+'logout.php',{ ok:this._logoutOK, scope:this} ,'', {t:this._oDiv});
	},
	_logoutOK:function(a)
	{
		window.location.href=MEB.SITE_URL+'index.php';
	},
	_cartPopup:function()
	{
		new CNA.CartPopup({url: 'cart.php', baseURL: this._baseURL});
	},
	updateCart:function(str)
	{
		Y.util.Dom.get('user-cart-data').innerHTML = str;	
	},
	_openCnaLocation:function()
	{
		if(this._headerLocationOpen)
		{
			this._closeCnaLocation();
			return;
		}
		this._headerLocationOpen = true;
		var anim = new Y.util.Anim(this._headerLocaitonContainer, {width: {  to: 225 }}, 0.5, Y.util.Easing.easeOut	);
		anim.animate();
	},
	_closeCnaLocation:function()
	{
		this._headerLocationOpen = false;
		var anim = new Y.util.Anim(this._headerLocaitonContainer, {width: {  to: 25 }}, 0.5, Y.util.Easing.easeOut	);
		anim.animate();
	}
}

CNA.PageList=function(configs)
{
	CNA.PageList.superclass.constructor.call(this,configs);
	this.nearcity=configs.nearcity;
	this.tot=configs.tot;
	this.cityURL=configs.cityURL;
	this.nearURL=configs.nearURL;
	this._searchCityBtn=document.getElementById('search-city-btn');
	if (this._searchCityBtn)
	Y.util.Event.addListener(this._searchCityBtn,'click',this._searchCity,null,this);
	
	this._searchNearBtn=document.getElementById('search-near-btn');
	if (this._searchNearBtn)
		Y.util.Event.addListener(this._searchNearBtn,'click',this._searchNear,null,this);
	
	this._searchSliderCont=document.getElementById('search-slider-cont');
	if (this._searchSliderCont)
	{
		this._searchNearValue=document.getElementById('search-near-value');
		this._searchNearValue.innerHTML=configs.near+' km';
		this._sslider=new MEB.Slider({container:this._searchSliderCont,axis:'x',length:90,values:[10,20,30,50,100,150,200],value:(configs.near?configs.near:false)});
		this._sslider.onChange.subscribe(this._searchSliderChange,null,this);
		this._sslider.onDragStop.subscribe(this._searchSliderStop,null,this);
	}
	if (this.tot>0 && this.nearcity)
		new MEB.InnerTooltip({target:document.getElementById('search-tip'),content:MEB.sprintf(CNA.lang.common.tip_nearcity,this.nearcity),closeButton:false});
	new MEB.InitImagePopup();
}
Y.lang.extend(CNA.PageList, CNA.Page,{
	_searchCity:function()
	{
		this._searchCityBtn.style.display='none';
		this._searchCityCtrl=new MEB.City({container:this._searchCityBtn.parentNode,country:'IT',noflag:true,countries:false});
		this._searchCityCtrl.onChange.subscribe(this._searchCityGo,null,this);
		
		this._searchCityGoBtn=new MEB.Button({container:this._searchCityBtn.parentNode,content:'ok'});
		this._searchCityGoBtn.onClick.subscribe(this._searchCityGo,null,this);
	},
	_searchCityGo:function()
	{
		if (this._searchCityCtrl.value)
			window.location.href=MEB.sprintf(this.cityURL,this._searchCityCtrl.text.city.replace(/ /g,'-'),this._searchCityCtrl.value.city); 
		else if (this._searchCityCtrl.text && this._searchCityCtrl.text.city)
			window.location.href=MEB.sprintf(this.cityURL,this._searchCityCtrl.text.city.replace(/ /g,'-'),'0');
	},
	_searchNear:function()
	{
		if (this._searchNearBtn.tagName.toLowerCase()=='a' && this._myAddressValue)
			this._searchNearGo(true);
		else
		{
			var p = new CNA.ChangeMyaddressPopup({value: this._myAddressValue});
			p.onClose.subscribe(this._searchNearGo,null,this);
		}
	},
	_searchNearGo:function(ok)
	{
		if (!ok)
			return true;
		window.location.href=this.nearURL+'near=20';
	},
	_searchSliderStop:function()
	{
		var l = new MEB.Loading(Y.util.Dom.get('search-near-l'),'inside');
		l.show();
		
		window.location.href=this.nearURL+'near='+Math.round(this._sslider.value);
	},
	_searchSliderChange:function(v)
	{
		this._searchNearValue.innerHTML=Math.round(v)+' km';
	}
});
CNA.SearchPage=function(configs)
{
	CNA.SearchPage.superclass.constructor.call(this,configs);
	if (configs.tipNoType)
	{
		this._st=new MEB.InnerTooltip({target:document.getElementById('search-tip'),content:MEB.sprintf(CNA.lang.search.tip_notype,configs.typeURL,configs.typeURL)});
		this._st.onClose.subscribe(this._stClosed,null,this);
	}
	if (configs.tipHL)
	{
		this._hlt=new MEB.InnerTooltip({target:document.getElementById('search-tip'),content:MEB.sprintf(CNA.lang.search.tip_hl,configs.itaURL)});
		this._hlt.onClose.subscribe(this._hlClosed,null,this);
	}
}
Y.lang.extend(CNA.SearchPage, CNA.PageList,{
	_hlClosed:function()
	{
		var day = new Date();
		day.setDate(day.getDate()+1);
	 	Y.util.Cookie.set("tipHL", 'no', { expires: day,domain: ".insiemecna.com" });
	},
	_stClosed:function()
	{
		var day = new Date();
		day.setDate(day.getDate()+1);
	 	Y.util.Cookie.set("tipNoType", 'no', { expires: day,domain: ".insiemecna.com" });
	},
	_searchCityGo:function()
	{
		if (this._searchCityCtrl.value || (this._searchCityCtrl.text && this._searchCityCtrl.text.city))
			window.location.href=this.cityURL+'city='+(this._searchCityCtrl.value?this._searchCityCtrl.value.city:encodeURIComponent(this._searchCityCtrl.text.city));
	}
});
CNA.LoginPopup=function(configs)
{
	if(configs)
	{
		this._loginRequired = configs.loginRequired;
		this._currentPopup = configs.currentPopup;	
		this._currentPopupParam = configs.currentPopupParam?configs.currentPopupParam:null;
	}
	else
	{
		configs = new Object();
	}
	this._oDiv = document.createElement('DIV');
	this._oDiv.className = 'login-popup';
	this._loginHTML();
	
	configs.body = this._oDiv;
	CNA.LoginPopup.superclass.constructor.call(this,configs);
	this.render();
	
	this.startPooling();
	this._loginHTMLBind();
}
Y.lang.extend(CNA.LoginPopup, MEB.Popup,{
	_tooltip:null,_loginRequired:null,
	close:function(a)
	{
		if(this._tooltip)
			this._tooltip.close();
		CNA.LoginPopup.superclass.close.call(this,a);	
	},
	_register:function()
	{
		parent.location.href = MEB.SITE_URL+'registrazione.php';
	},
	_loginHTML: function(){
		str='';
		if(this._loginRequired)
			str = '<div class="action">'+MEB.sprintf(CNA.lang.common['login_necessary'],this._loginRequired)+'</div>';
		this._oDiv.innerHTML =	str+'<form id="login-form" action="javascript:void(0);"><div class="field"><div class="field-label">'+MEB.lang.fields['email']+':</div><div class="field-input"><input id="login-mail" type="text" value=""/></div></div>'+
							'<div class="field"><div class="field-label">'+MEB.lang.fields['password']+':</div><div class="field-input"><input id="login-pass" type="password" value=""/></div></div>'+
							'<div id="forget-pass-btn" class="field center">'+CNA.lang.common['forget_pass']+'</div>'+
							'<div class="field-btn center"><button id="btn-login"></button></div></form>'+
							'<div id="register-btn" class="field center">'+CNA.lang.common['if_no_register']+'</div>';
	},
	_loginHTMLBind: function(){
		this._forgetBtn = Y.util.Dom.get('forget-pass-btn');
		Y.util.Event.addListener(this._forgetBtn,'click',this._forgetForm,null,this);
		this._registerBtn = Y.util.Dom.get('register-btn');
		Y.util.Event.addListener(this._registerBtn,'click',this._register,null,this);
		this._loginBtn = new MEB.Button({id:'btn-login',content: CNA.lang.common['user_panel_access']});
		this._loginBtn.onClick.subscribe(this._login,null,this);
	},
	_forgetHTML: function(){
		this._oDiv.innerHTML ='<div id="forget-back-btn" class="right">'+CNA.lang.common['btn_back']+'</div>'+
									'<div class="clear forget-text">'+CNA.lang.common['forget_text']+'</div>'+
									'<form id="pswreq-form" action="javascript:void(0);"><div class="field"><div class="field-label">'+MEB.lang.fields['email']+':</div><div class="field-input"><input id="login-mail" type="text" value=""/></div></div>'+
									'<div class="field-btn center"><button id="btn-fogetSend"></button></div></form>'+
									'<div id="forget-mail-btn" class="field center">'+CNA.lang.common['forget_mail']+'</div>';
	},
	_forgetHTMLBind: function(){
		this._forgetBackBtn = Y.util.Dom.get('forget-back-btn');
		Y.util.Event.addListener(this._forgetBackBtn,'click',this._back,null,this);
		this._forgetMailBtn = Y.util.Dom.get('forget-mail-btn');
		Y.util.Event.addListener(this._forgetMailBtn,'click',this._forgetMail,null,this);
		this._forgetSendBtn = new MEB.Button({id:'btn-fogetSend',content: CNA.lang.common['forget_send']});
		this._forgetSendBtn.onClick.subscribe(this._forgetSend,null,this);
	},
	_login: function()
	{
		if(this._tooltip)
			this._tooltip.close();
		
		var mail = Y.util.Dom.get('login-mail');
		if(!mail.value){
			this._loginError(200,'mail');
			return;
		}
		var pass = Y.util.Dom.get('login-pass');
		if(!pass.value){
			this._loginError(200,'pass');
			return;
		}
			
		str='mail='+mail.value+'&pass='+pass.value;
		if(this._currentPopup){
			str+='&currentPopup='+this._currentPopup;
			if(this._currentPopupParam)
				str+='&currentPopupParam='+this._currentPopupParam;
		}
	
		new MEB.Connect('POST', MEB.AJAX_BASE+'login.php',{ ok:this._loginOK, error: this._loginError, scope:this} ,str, {t:this._loginBtn, p:'r'});
	},
	_loginOK: function()
	{
		window.location.href = window.location.href;
	},
	_loginError: function(e,txt)
	{
		switch(e)
		{
			case 300:
				this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target:Y.util.Dom.get('login-mail'),align:'t',type:'error',content:MEB.lang.errors[e],closeButton:false,closeOnChange:[Y.util.Dom.get('login-mail'),Y.util.Dom.get('login-pass')]});
				return true;
			case 313:
				this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target:Y.util.Dom.get('login-mail'),align:'t',type:'error',content:MEB.lang.errors[e],closeButton:false,closeOnChange:true});
				return true;
			case 200:
				var target = Y.util.Dom.get('login-'+txt);
				if(target)
					this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: target,align:'t',type:'error',content:MEB.lang.errors[e],closeButton:false,closeOnChange:true});
				return true;
		}
	},
	_forgetForm:function(){
		if(this._tooltip)
			this._tooltip.close();
			
		this._forgetHTML();
		this._forgetHTMLBind();	
	},
	_back:function()
	{
		if(this._tooltip)
			this._tooltip.close();
		
		this._loginHTML();
		this._loginHTMLBind();
	},
	_forgetSend:function(){
		if(this._tooltip)
			this._tooltip.close();
		
		var mail = Y.util.Dom.get('login-mail');
		if(!mail.value){
			this._loginError(200,'mail');
			return;
		}
					
		str='mail='+mail.value;
		new MEB.Connect('POST', MEB.AJAX_BASE+'forgetPass.php',{ ok:this._forgetSendOK, error: this._loginError, scope:this} ,str, {t:this._forgetSendBtn, p:'r'});
	},
	_forgetSendOK:function(){
		this.switchContest('ok');
		this._oDiv.innerHTML = CNA.lang.common['forget_text_ok'];
	},
	_forgetMail:function(){
		window.location.href= MEB.SITE_URL+"contatti.php";
	}
});

MEB.ErrorPopup=function(configs)
{
	configs.contest = 'error';
	configs.body='<div class="modal-default-body">'+configs.body+'</div>';
	MEB.ErrorPopup.superclass.constructor.call(this,configs);
	this.render();
}
Y.lang.extend(MEB.ErrorPopup, MEB.Popup);

MEB.OkPopup=function(configs)
{
	configs.contest = 'ok';
	configs.body='<div class="modal-default-body">'+configs.body+'</div>';
	MEB.OkPopup.superclass.constructor.call(this,configs);
	this.render();
}
Y.lang.extend(MEB.OkPopup, MEB.Popup);

MEB.Connect.handleError=function(err,txt)
{
   if (err)
    {
	    switch(err)
	    {
	         default:
			    new MEB.ErrorPopup({body:MEB.lang.errors[err]});
	    }
	}
}

CNA.OffersTpl=function(configs)
{
	this._container = Y.util.Dom.get(configs.container);
	this._tot = configs.tot;
	this._active = 0;
	for(var i=0;i<this._tot;i++){
		Y.util.Event.addListener('tpl-product-offers-product_'+i,'mouseover',this._over,i,this);
	}
}
CNA.OffersTpl.prototype = {
	_over:function(e,arg)
	{
		Y.util.Dom.addClass(Y.util.Dom.get('tpl-product-offers-title_'+this._active),'hidden');
		Y.util.Dom.removeClass(Y.util.Dom.get('tpl-product-offers-product_'+this._active),'selected');
		this._active = arg;
		Y.util.Dom.removeClass(Y.util.Dom.get('tpl-product-offers-title_'+this._active),'hidden');		
		Y.util.Dom.addClass(Y.util.Dom.get('tpl-product-offers-product_'+this._active),'selected');
	}
}

CNA.ProductListTpl=function(configs)
{
	this.id = configs.id;
	this._noBtn = configs.noBtn;
	this._baseURL = configs.baseURL?configs.baseURL:null;
	
	if(this._noBtn)
	{
		this._buyBtn = new MEB.Button({id: "buy-btn_"+this.id, content: CNA.lang.common['buy']});
		this._buyBtn.onClick.subscribe(this._openCart,null,this);
	}
	
	this._dispShop = Y.util.Dom.get('disp-shop_'+this.id);
	if(this._dispShop)
		Y.util.Event.addListener(this._dispShop,'click',this._openDispLegend,null,this);	
		
	this._dispYes = Y.util.Dom.get('disp-yes_'+this.id);
	if(this._dispYes)
		Y.util.Event.addListener(this._dispYes,'click',this._openDispLegend,null,this);
	
	this._dispOrder = Y.util.Dom.get('disp-order_'+this.id);
	if(this._dispOrder)
		Y.util.Event.addListener(this._dispOrder,'click',this._openDispLegend,null,this);

	this._dispNo = Y.util.Dom.get('disp-no_'+this.id);
	if(this._dispNo)
		Y.util.Event.addListener(this._dispNo,'click',this._openDispLegend,null,this);
}
CNA.ProductListTpl.prototype = {
	_openDispLegend: function()
	{
		new CNA.DisponibilityPopup();
	},
	_openCart: function()
	{
		new CNA.CartPopup({url: 'edit_cart.php',param: 'act=ins&qta=1&prod='+this.id, baseURL: this._baseURL});
	}
}

CNA.DisponibilityPopup=function()
{
	configs = new Object();
	configs.iframe= { url: MEB.MODULES_BASE+'disponibilita.php', width: 600 ,height: 400};
	
	CNA.DisponibilityPopup.superclass.constructor.call(this,configs);
	this.render();
	this.startPooling();
}
Y.lang.extend(CNA.DisponibilityPopup, MEB.Popup);

CNA.PrivacyPopup=function()
{
	configs = new Object();
	configs.iframe= { url: MEB.MODULES_BASE+'privacy.php', width: 600 ,height: 200};
	
	CNA.PrivacyPopup.superclass.constructor.call(this,configs);
	this.render();
	this.startPooling();
}
Y.lang.extend(CNA.PrivacyPopup, MEB.Popup);

CNA.CartPopup=function(configs)
{
	this.url = configs.url;
	this.baseURL = configs.baseURL?configs.baseURL:'';
	this.param = configs.param?configs.param:'';
	if(this.baseURL)
	{
		this.param += this.param?'&':'';
		this.param += 'baseURL='+this.baseURL;
		this.baseURL = 'http://'+this.baseURL+'.insiemecna.com';
	}
	configs.iframe= { url: this.baseURL+MEB.MODULES_BASE+this.url+(this.param?'?'+this.param:''), width: 800 ,height: 230 };

	CNA.CartPopup.superclass.constructor.call(this,configs);
	this.render();
	this.startPooling();
}
Y.lang.extend(CNA.CartPopup, MEB.Popup);

CNA.Cart=function(configs)
{
	this._orderLabel = configs.orderLabel;
	this._prd = configs.prd;
	this._neg = configs.neg;
	this.baseURL = configs.baseURL?configs.baseURL:'';
	
	for(var i=0;i<this._prd.length;i++)
	{
		new MEB.IntInput({id: "qta"+this._prd[i]});
		Y.util.Event.addListener('qta_btn'+this._prd[i],'click',this._checkQta,this._prd[i],this);
	}	
	for(var i=0;i<this._neg.length;i++)
	{
		var b = new MEB.Button({id: "order_btn_"+this._neg[i], content: this._orderLabel});
		b.onClick.subscribe(this._order,this._neg[i],this);
	}	

	new MEB.InitTitle();
}
CNA.Cart.prototype = {
	_checkQta:function(e,arg)
	{
		if (Y.util.Dom.get("qta"+arg).value=="" || Y.util.Dom.get("qta"+arg).value==0)
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get("qta"+arg),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			Y.util.Event.stopEvent(e);
			return false;
		}
	},
	_order:function(e,arg)
	{
		if(!cl.logged)
		{
			new parent.CNA.LoginPopup({loginRequired: CNA.lang.common['order_action'], currentPopup: 'CartPopup', currentPopupParam: 'negozio='+arg});
			window.frameElement.popup.close();
		}
		else
		{	
			location.href = (this.baseURL?'http://'+this.baseURL+'.insiemecna.com/modules/':'')+'ordina.php?negozio='+arg+(this.baseURL?'&baseURL='+this.baseURL:'');
		}
	}
}

CNA.Spedizione=function(configs)
{
	this._sp = configs.hasSpedizione;
	this._spType = configs.spedizione;
	this._payType = configs.pagamento;
	this._to = configs.to;
	this._country = configs.country;
	this._negID = configs.negID;
	
	this._setBtn = new MEB.Button({id: 'set', content: 'ok'});
	this._setBtn.onClick.subscribe(this._set,null,this);
		
	if(this._sp)
	{
		if(Y.util.Dom.get('contrassegno_check')){
			this._contrassegnoCh = new MEB.Checkbox({id : 'contrassegno_check'});
			this._contrassegnoCh.onChange.subscribe(this.selezionaPagamento,'contrassegno',this);
			if(this._payType==2)
				this._contrassegnoCh.activate(true);
		}
		if(Y.util.Dom.get('cc2_check')){
			this._cc2Ch= new MEB.Checkbox({id : 'cc2_check'});
			this._cc2Ch.onChange.subscribe(this.selezionaPagamento,'cc2',this);
			if(this._payType==1)
				this._cc2Ch.activate(true);
		}
	}

	this._inNegozioCH = new MEB.Checkbox({id : 'in_negozio_check'});	
	this._inNegozioCH.onChange.subscribe(this.selezionaPagamento,'in_negozio',this);
	if(this._payType==3)
		this._inNegozioCH.activate(true);
	if(Y.util.Dom.get('cc1_check')){
		this._cc1Ch = new MEB.Checkbox({id : 'cc1_check'});
		this._cc1Ch.onChange.subscribe(this.selezionaPagamento,'cc1',this);
		if(this._payType==1)
			this._cc1Ch.activate(true);
	}
	
	if(this._sp)
	{
		this._ritiroCh = new MEB.Checkbox({id : 'ritiro_check'});
		this._ritiroCh.onChange.subscribe(this.dispPagamento,'ritiro',this);
		if(this._spType==1)
			this._ritiroCh.activate(true);
		this._corriereCh = new MEB.Checkbox({id : 'corriere_check'});
		this._corriereCh.onChange.subscribe(this.dispPagamento,'corriere',this);
		if(this._spType==2)
			this._corriereCh.activate(true);
		this._addressCont = Y.util.Dom.get('shipment-address');
		this._addressBtn = Y.util.Dom.get('address-change');
		if(this._addressBtn)
			Y.util.Event.addListener(this._addressBtn,'click',this._addressChange,null,this);
	}
}
CNA.Spedizione.prototype = {
	_inNegozioCH:null,_cc1Ch:null,_contrassegnoCh:null,_cc2Ch:null,_ritiroCh:null,_corriereCh:null,
	_addressChange: function()
	{
		this._oldAddress = this._addressCont.innerHTML;
		Y.util.Dom.setStyle(this._addressBtn,'display','none');
		this._addressCont.innerHTML = '<br/>'+
							'<div class="field"><div class="field-label">'+MEB.lang.fields['name']+' '+MEB.lang.fields['lastname']+'</div><div class="field-input left"><input id="address-change-input-name" type="text" value=""/></div></div>'+
							'<div id="address-change-location"></div>'+
							'<div class="field-btn"><button id="address-change-ok" type="button"></button><button id="address-change-back" type="button"></button></div>';
		
		this._map = new MEB.Location({container:document.getElementById('address-change-location'),id:'location',country:this._country, zipcodeField:true});
		this._btnChangeOk = new MEB.Button({id: 'address-change-ok', content: CNA.lang.common['change']});
		this._btnChangeOk.onClick.subscribe(this._sendNewAddress,null,this);
		this._btnChangeBack = new MEB.Button({id: 'address-change-back', content: MEB.ucfirst(CNA.lang.common['btn_back'])});
		this._btnChangeBack.onClick.subscribe(this._sendBackAddress,null,this);
	},
	_sendBackAddress:function()
	{
		this._map.destroy();
		this._addressCont.innerHTML = this._oldAddress;
		Y.util.Dom.setStyle(this._addressBtn,'display','block');
	},
	_sendNewAddress: function()
	{
		var name = Y.util.Dom.get('address-change-input-name').value;
		if(name=="")
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('address-change-input-name'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		var str = 'nome='+name+'&negozioID='+this._negID;
		if(!this._map.check({geo:true}))
			return false;
			
		str+='&country='+this._map.country;
		if(this._map.value.city)
			str += '&city='+this._map.value.city+'&city_name='+encodeURIComponent(this._map.value.city_name);
		else 
			str += '&city=&city_name='+encodeURIComponent(this._location.value.city_name);
		str += '&admin1='+this._map.value.admin1+'&admin2='+this._map.value.admin2+'&zipcode='+this._map.value.zipcode+'&address='+encodeURIComponent(this._map.value.address);			
		
		new MEB.Connect('POST', MEB.AJAX_BASE+'set_shipment_address.php',{ ok:this._sendOK, scope:this} ,str, {t:this._btnChangeOk, p:'l'});
	},
	_sendOK:function(o)
	{
		this._map.destroy();
		this._addressCont.innerHTML = o.firstChild.nodeValue;
		Y.util.Dom.setStyle(this._addressBtn,'display','block');
	},
	_set:function()
	{
		document.getElementById('settaSpese').submit();
	},
	dispPagamento: function(e,arg)
	{
		if(e && arg=='ritiro')
		{
			this._corriereCh.deactivate(true);
			document.getElementById('ritiro').value = 'on';
			document.getElementById('corriere').value = 'off';
			document.getElementById('pagamenti_0').style.display='none';
			document.getElementById('pagamenti_1').style.display='block';
			document.getElementById('pagamenti_2').style.display='none';	
		}
		else if(e && arg=='corriere')
		{
			this._ritiroCh.deactivate(true);
			document.getElementById('ritiro').value = 'off';
			document.getElementById('corriere').value = 'on';
			document.getElementById('pagamenti_0').style.display='none';
			document.getElementById('pagamenti_1').style.display='none';
			document.getElementById('pagamenti_2').style.display='block';
		}
		else
		{
			this._ritiroCh.deactivate(true);
			this._corriereCh.deactivate(true);
			document.getElementById('ritiro').value = 'off';
			document.getElementById('corriere').value = 'off';
			document.getElementById('pagamenti_0').style.display='block';
			document.getElementById('pagamenti_1').style.display='none';
			document.getElementById('pagamenti_2').style.display='none';
		}
		this.aggiornaSpese();
	},
	selezionaPagamento: function(e,arg)
	{
		if(document.getElementById('cc2') && arg!='cc2'){
			document.getElementById('cc2').value = 'off';
			this._cc2Ch.deactivate(true);
		}
		if(document.getElementById('contrassegno') && arg!='contrassegno'){
			document.getElementById('contrassegno').value = 'off';
			this._contrassegnoCh.deactivate(true);
		}
		if(document.getElementById('cc1') && arg!='cc1'){
			document.getElementById('cc1').value = 'off';
			this._cc1Ch.deactivate(true);
		}
		if(document.getElementById('in_negozio') && arg!='in_negozio'){
			document.getElementById('in_negozio').value = 'off';
			this._inNegozioCH.deactivate(true);
		}
		
		if(e)
			document.getElementById(arg).value = 'on';
		else
			document.getElementById(arg).value = 'off';
		this.aggiornaSpese();
	},
	aggiornaSpese: function()
	{
		var spesa=0;
		if(this._sp)
		{
			if(this._ritiroCh.active){
				spesa+=parseFloat(document.getElementById('ritiro').getAttribute('costo'));
			}
			else if(this._corriereCh.active)
			{
				spesa+=parseFloat(document.getElementById('corriere').getAttribute('costo'));
				
				if (this._contrassegnoCh.active)
					spesa+=parseFloat(document.getElementById('contrassegno').getAttribute('costo'));
				else if (this._cc2Ch.active)
					spesa+=parseFloat(document.getElementById('cc2').getAttribute('costo'));
				else if (this._cc1Ch.active)
					spesa+=parseFloat(document.getElementById('cc1').getAttribute('costo'));
				else if (this._inNegozioCH.active)
					spesa+=parseFloat(document.getElementById('in_negozio').getAttribute('costo'));
			}
		}
		document.getElementById('totaleSpese').innerHTML=spesa.toFixed(2).replace('.',',');
	}
}

CNA.Ordina=function(configs)
{
	this._btnID = configs.btnID;
	this._btnLabel = configs.btnLabel;
	this._negID = configs.negID;
	this._negCheck = configs.negCheck;
	this._btn = new MEB.Button({id: this._btnID,content: this._btnLabel});
	this._btn.onClick.subscribe(this._send,null,this);
	this._country = configs.country;
	this._addressCont = Y.util.Dom.get('shipment-address');
	this._addressBtn = Y.util.Dom.get('address-change');
	if(this._addressBtn)
		Y.util.Event.addListener(this._addressBtn,'click',this._addressChange,null,this);
}
CNA.Ordina.prototype = {
	_addressChange: function()
	{
		this._oldAddress = this._addressCont.innerHTML;
		Y.util.Dom.setStyle(this._addressBtn,'display','none');
		this._addressCont.innerHTML = '<br/>'+
							'<div class="field"><div class="field-label">'+MEB.lang.fields['name']+' '+MEB.lang.fields['lastname']+'</div><div class="field-input left"><input id="address-change-input-name" type="text" value=""/></div></div>'+
							'<div id="address-change-location"></div>'+
							'<div class="field-btn"><button id="address-change-ok" type="button"></button><button id="address-change-back" type="button"></button></div>';
		
		this._map = new MEB.Location({container:document.getElementById('address-change-location'),id:'location',country:this._country, zipcodeField:true});
		this._btnChangeOk = new MEB.Button({id: 'address-change-ok', content: CNA.lang.common['change']});
		this._btnChangeOk.onClick.subscribe(this._sendNewAddress,null,this);
		this._btnChangeBack = new MEB.Button({id: 'address-change-back', content: MEB.ucfirst(CNA.lang.common['btn_back'])});
		this._btnChangeBack.onClick.subscribe(this._sendBackAddress,null,this);
	},
	_sendBackAddress:function()
	{
		this._map.destroy();
		this._addressCont.innerHTML = this._oldAddress;
		Y.util.Dom.setStyle(this._addressBtn,'display','block');
	},
	_sendNewAddress: function()
	{
		var name = Y.util.Dom.get('address-change-input-name').value;
		if(name=="")
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('address-change-input-name'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		var str = 'nome='+name+'&negozioID='+this._negID;
		if(!this._map.check({geo:true}))
			return false;
			
		str+='&country='+this._map.country;
		if(this._map.value.city)
			str += '&city='+this._map.value.city+'&city_name='+encodeURIComponent(this._map.value.city_name);
		else 
			str += '&city=&city_name='+encodeURIComponent(this._location.value.city_name);
		str += '&admin1='+this._map.value.admin1+'&admin2='+this._map.value.admin2+'&zipcode='+this._map.value.zipcode+'&address='+encodeURIComponent(this._map.value.address);			
		
		new MEB.Connect('POST', MEB.AJAX_BASE+'set_shipment_address.php',{ ok:this._returnAddressOk, scope:this} ,str, {t:this._btnChangeOk, p:'l'});
	},
	_returnAddressOk:function(o)
	{
		this._map.destroy();
		this._addressCont.innerHTML = o.firstChild.nodeValue;
		Y.util.Dom.setStyle(this._addressBtn,'display','block');
	},
	_send:function()
	{
		str='neg_id='+this._negID+'&neg_check='+encodeURIComponent(this._negCheck);
		new MEB.Connect('POST', MEB.AJAX_BASE+'conferma_ordine.php',{ ok:this._sendOK, scope:this} ,str, {t:this._btn, p:'r'});
	},
	_sendOK:function()
	{
		parent.location.href = MEB.SITE_URL+'conferma.php';
	}
}

CNA.Contacts=function(configs)
{
	this._btnID = configs.btnID;
	this._id = configs.id?configs.id:null;
	this._ajaxURL = configs.ajaxURL;
	this._btnLabel = CNA.lang.common['contacts_shop_form_send'];
	this._okLabel = CNA.lang.common['contacts_shop_form_send_ok'];
	this._btn = new MEB.Button({id: this._btnID,content: this._btnLabel});
	this._btn.onClick.subscribe(this._send,null,this);
	this._sediArray = configs.sediArray?configs.sediArray:null;
	if(this._sediArray)
	{
		this._sediSelect = new MEB.Select({id:'contacts-sedi-select', items: this._sediArray, selectedIndex: (configs.selectedSede?configs.selectedSede:0)});
	}
	
	new MEB.Textarea({id: 'testo', max:300});
}
CNA.Contacts.prototype = {
	_innerTooltip:null,
	_send:function()
	{
		if(this._innerTooltip)
			this._innerTooltip.close();
		if(Y.util.Dom.get('email').value=="")
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('email'),align:'r',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		if(!MEB.isValidEmail(Y.util.Dom.get('email').value))
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('email'),align:'r',type:'error',content:MEB.lang.errors[201],closeButton:false,closeOnChange:true});
			return;
		}
		if(Y.util.Dom.get('oggetto').value=="")
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('oggetto'),align:'r',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		if(Y.util.Dom.get('testo').value=="")
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('testo'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		str = 'email='+encodeURIComponent(Y.util.Dom.get('email').value)+'&oggetto='+encodeURIComponent(Y.util.Dom.get('oggetto').value)+'&testo='+encodeURIComponent(Y.util.Dom.get('testo').value);
		if(this._id)
			str += '&id='+this._id;
		if(this._sediSelect)
			str += '&sede='+this._sediSelect.value;
		new MEB.Connect('POST', MEB.AJAX_BASE+this._ajaxURL,{ ok:this._sendOK, scope:this} ,str, {t:this._btn, p:'r'});
		this._btn.disable();
	},
	_sendOK:function()
	{
		this._innerTooltip = new MEB.InnerTooltip({target: Y.util.Dom.get('formContatti-ok'),align:'inner',content:this._okLabel,closeButton:false});
		Y.util.Dom.get('email').value="";
		Y.util.Dom.get('oggetto').value="";
		Y.util.Dom.get('testo').value="";
		this._btn.enable();
	}
}

CNA.Register=function(configs)
{
	CNA.Register.superclass.constructor.call(this,configs);
	
	this._btnPrivacy = Y.util.Dom.get('btn-privacy');
	Y.util.Event.addListener(this._btnPrivacy,'click',this._openPrivacy,null,this);
	this._btnReg = new MEB.Button({id: "btn-register", content: CNA.lang.common['register_btn']});
	this._btnReg.onClick.subscribe(this._send,null,this);
	this._map = new MEB.Location({container:document.getElementById('location-container'),id:'location',country:this._country, zipcodeField:true});
	this._map.initMap(true);
	
	new MEB.PhoneInput({id: 'phone'});
}
Y.lang.extend(CNA.Register, CNA.Page,{
	_send:function()
	{
		var email = Y.util.Dom.get('email').value;
		if(email=="")
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('email'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		if(!MEB.isValidEmail(email))
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('email'),align:'t',type:'error',content:MEB.lang.errors[201],closeButton:false,closeOnChange:true});
			return;
		}
		var pwd = Y.util.Dom.get('pwd').value;
		if(pwd=="")
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('pwd'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		if(pwd.length<6)
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('pwd'),align:'t',type:'error',content:MEB.lang.errors[202],closeButton:false,closeOnChange:true});
			return;
		}
		
		var nome = Y.util.Dom.get('first-name').value;
		if(nome=="")
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('first-name'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		var cognome = Y.util.Dom.get('last-name').value;
		if(cognome=="")
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('last-name'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		
		if(!this._map.check({geo:true}))
			return false;
			
		var str='country='+this._map.country;
		if(this._map.value.city)
			str += '&city='+this._map.value.city+'&city_name='+encodeURIComponent(this._map.value.city_name);
		else 
			str += '&city=&city_name='+encodeURIComponent(this._location.value.city_name);
		str += '&admin1='+this._map.value.admin1+'&admin2='+this._map.value.admin2+'&zipcode='+this._map.value.zipcode+'&address='+encodeURIComponent(this._map.value.address)+'&latitude='+this._map.value.latitude+'&longitude='+this._map.value.longitude+'&accuracy='+this._map.value.accuracy;
		
		var tel = Y.util.Dom.get('phone').value;
		if(tel=="")
		{
			new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('phone'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		
		str += '&email='+encodeURIComponent(email)+'&pwd='+encodeURIComponent(pwd)+'&nome='+encodeURIComponent(nome)+'&cognome='+encodeURIComponent(cognome)+'&tel='+tel;		

		new MEB.Connect('POST', MEB.AJAX_BASE+'register.php',{ ok:this._sendOK, error:this._sendERROR, scope:this} ,str, {t:this._btnReg, p:'r'});
	},
	_sendOK:function()
	{
		window.location.href = MEB.SITE_URL+'index.php'; 
	},
	_sendERROR:function(e,txt)
	{
		if(e==218 || e==202 || e==208)
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target:Y.util.Dom.get(txt),align:'t',type:'error',content:MEB.lang.errors[e],closeButton:false,closeOnChange:true});
			return true;
		}
		return false;
	},
	_openPrivacy:function()
	{
		new CNA.PrivacyPopup();
	}
});

CNA.Profile=function(configs)
{
	CNA.Profile.superclass.constructor.call(this,configs);
	
	this._btnModifyData = Y.util.Dom.get('modify-data-btn');
	Y.util.Event.addListener(this._btnModifyData,'click',this._modifyData,null,this);
	this._btnChangePwd = Y.util.Dom.get('change-pwd-btn');
	Y.util.Event.addListener(this._btnChangePwd,'click',this._changePwd,null,this);
	
	this._btnChangeMyaddress = Y.util.Dom.get('modify-myaddress-btn');
	Y.util.Event.addListener(this._btnChangeMyaddress,'click',this._changeMyAddress,null,this);
}
Y.lang.extend(CNA.Profile, CNA.Page,{
	_data: null,_btnOK:null,_btnBack:null,_newData:null,_innerTooltip:null,
	_modifyData:function()
	{
		if(this._innerTooltip)
			this._innerTooltip.close();
		if(!this._data)
		{
			this._data = new Object();	
			this._data.nomeHtml = Y.util.Dom.get('data-nome');
			this._data.cognomeHtml = Y.util.Dom.get('data-cognome');	
			this._data.emailHtml = Y.util.Dom.get('data-email');
			this._data.locatorHtml = Y.util.Dom.get('locator-container');
			this._data.telHtml = Y.util.Dom.get('data-tel');
			this._data.btnContHtml = Y.util.Dom.get('modify-btn-container');
		}
		this._data.cityHtml = Y.util.Dom.get('data-city');
		this._data.city_nameHtml = Y.util.Dom.get('data-city-name');
		this._data.zipcodeHtml = Y.util.Dom.get('data-zipcode');
		this._data.admin1Html = Y.util.Dom.get('data-admin1');
		this._data.admin1TextHtml = Y.util.Dom.get('data-admin1-text');
		this._data.admin2Html = Y.util.Dom.get('data-admin2');
		this._data.admin2TextHtml = Y.util.Dom.get('data-admin2-text');
		this._data.addressHtml = Y.util.Dom.get('data-address');
		this._data.latitudeHtml = Y.util.Dom.get('data-latitude');
		this._data.longitudeHtml = Y.util.Dom.get('data-longitude');
		this._data.accuracyHtml = Y.util.Dom.get('data-accuracy');
		
		this._data.nome = this._data.nomeHtml.innerHTML;	
		this._data.cognome = this._data.cognomeHtml.innerHTML;
		this._data.email = this._data.emailHtml.innerHTML;
		this._data.locator = this._data.locatorHtml.innerHTML;
		this._data.city = this._data.cityHtml.innerHTML;
		this._data.city_name = this._data.city_nameHtml.innerHTML;
		this._data.zipcode = this._data.zipcodeHtml.innerHTML;
		this._data.admin1 = this._data.admin1Html.innerHTML;
		this._data.admin2 = this._data.admin2Html.innerHTML;
		this._data.address = this._data.addressHtml.innerHTML;
		this._data.latitude = this._data.latitudeHtml.innerHTML;
		this._data.longitude = this._data.longitudeHtml.innerHTML;
		this._data.accuracy = this._data.accuracyHtml.innerHTML;
		this._data.tel = this._data.telHtml.innerHTML;
		
		Y.util.Dom.removeClass(this._data.nomeHtml,"field-data");
		Y.util.Dom.addClass(this._data.nomeHtml,"field-input");
		this._data.nomeHtml.innerHTML = '<input id="first-name-input" name="first-name-input" type="text" value="'+this._data.nome+'" />';
		
		Y.util.Dom.removeClass(this._data.cognomeHtml,"field-data");
		Y.util.Dom.addClass(this._data.cognomeHtml,"field-input");
		this._data.cognomeHtml.innerHTML = '<input id="last-name-input" name="last-name-input" type="text" value="'+this._data.cognome+'" />';
		
		Y.util.Dom.removeClass(this._data.emailHtml,"field-data");
		Y.util.Dom.addClass(this._data.emailHtml,"field-input");
		this._data.emailHtml.innerHTML = '<input id="email-input" name="email-input" type="text" value="'+this._data.email+'" />';
		
		this._data.locatorHtml.innerHTML = '';
		this._map = new MEB.Location({container:this._data.locatorHtml,id:'location',country:this._country, zipcodeField:true, value: {city_name: this._data.city_name, city: this._data.city, admin1: this._data.admin1, admin2: this._data.admin2,  address: this._data.address, zipcode: this._data.zipcode, latitude: this._data.latitude, longitude: this._data.longitude, accuracy: this._data.accuracy}});
		this._map.initMap(true);
		
		Y.util.Dom.removeClass(this._data.telHtml,"field-data");
		Y.util.Dom.addClass(this._data.telHtml,"field-input");
		this._data.telHtml.innerHTML = '<input id="phone-input" name="phone-input" type="text" value="'+this._data.tel+'" />';
		new MEB.PhoneInput({id: 'phone-input'});
		
		Y.util.Dom.removeClass(this._data.btnContHtml,"hidden");
		if(!this._btnOK)
		{
			this._btnOK = new MEB.Button({id: 'btn-moodify-ok', content: 'Modifica'});
			this._btnOK.onClick.subscribe(this._modifySend,null,this);
			this._btnBack = new MEB.Button({id: 'btn-moodify-back', content: 'Annulla'});
			this._btnBack.onClick.subscribe(this._modifyBack,null,this);
		}
	},
	_modifySend:function()
	{
		if(!this._newData){
			this._newData = new Object();
		}
		this._newData.nome = Y.util.Dom.get('first-name-input').value;
		if(this._newData.nome=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('first-name-input'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		this._newData.cognome = Y.util.Dom.get('last-name-input').value;
		if(this._newData.cognome=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltipr',target: Y.util.Dom.get('last-name-input'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		this._newData.email = Y.util.Dom.get('email-input').value;
		if(this._newData.email=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('email-input'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		if(!MEB.isValidEmail(this._newData.email))
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('email-input'),align:'t',type:'error',content:MEB.lang.errors[201],closeButton:false,closeOnChange:true});
			return;
		}
		
		if(!this._map.check({geo:true}))
			return false;
			
		var str='country='+this._map.country;
		if(this._map.value.city)
			str += '&city='+this._map.value.city+'&city_name='+encodeURIComponent(this._map.value.city_name);
		else 
			str += '&city=&city_name='+encodeURIComponent(this._location.value.city_name);
		str += '&admin1='+this._map.value.admin1+'&admin2='+this._map.value.admin2+'&zipcode='+this._map.value.zipcode+'&address='+encodeURIComponent(this._map.value.address)+'&latitude='+this._map.value.latitude+'&longitude='+this._map.value.longitude+'&accuracy='+this._map.value.accuracy;
		
		this._newData.tel = Y.util.Dom.get('phone-input').value;
		if(this._newData.tel=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('phone-input'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		
		this._country = this._map.country;
		this._newData.city =this._map.value.city;
		this._newData.city_name =this._map.value.city_name;
		this._newData.admin1 =this._map.value.admin1;
		this._newData.admin2 =this._map.value.admin2;
		this._newData.zipcode =this._map.value.zipcode;
		this._newData.address =this._map.value.address;
		this._newData.latitude =this._map.value.latitude;
		this._newData.longitude =this._map.value.longitude;
		this._newData.accuracy =this._map.value.accuracy;
		
		str += '&email='+encodeURIComponent(this._newData.email)+'&nome='+encodeURIComponent(this._newData.nome)+'&cognome='+encodeURIComponent(this._newData.cognome)+'&tel='+this._newData.tel;		
	
		new MEB.Connect('POST', MEB.AJAX_BASE+'edit_profile.php',{ ok:this._sendOK, scope:this} ,str, {t:this._btnOK, p:'l'});
	},
	_modifyBack:function(e,arg)
	{
		if(this._tooltip)
			this._tooltip.close();
			
		Y.util.Dom.removeClass(this._data.nomeHtml,"field-input");
		Y.util.Dom.addClass(this._data.nomeHtml,"field-data");
		this._data.nomeHtml.innerHTML = arg?this._newData.nome:this._data.nome;
		
		Y.util.Dom.removeClass(this._data.cognomeHtml,"field-input");
		Y.util.Dom.addClass(this._data.cognomeHtml,"field-data");
		this._data.cognomeHtml.innerHTML = arg?this._newData.cognome:this._data.cognome;
		
		Y.util.Dom.removeClass(this._data.emailHtml,"field-input");
		Y.util.Dom.addClass(this._data.emailHtml,"field-data");
		this._data.emailHtml.innerHTML = arg?this._newData.email:this._data.email;
		
		this._map.destroy();
		this._data.locatorHtml.innerHTML = this._data.locator;
		if(arg)
		{
			Y.util.Dom.get('data-city').innerHTML = this._newData.city;
			Y.util.Dom.get('data-city-name').innerHTML = this._newData.city_name;
			Y.util.Dom.get('data-zipcode').innerHTML = this._newData.zipcode;
			Y.util.Dom.get('data-admin1').innerHTML = this._newData.admin1;
			Y.util.Dom.get('data-admin1-text').innerHTML = arg.admin1;
			Y.util.Dom.get('data-admin2').innerHTML = this._newData.admin2;
			Y.util.Dom.get('data-admin2-text').innerHTML = arg.admin2;
			Y.util.Dom.get('data-address').innerHTML = this._newData.address;
			Y.util.Dom.get('data-latitude').innerHTML = this._newData.latitude;
			Y.util.Dom.get('data-longitude').innerHTML = this._newData.longitude;
			Y.util.Dom.get('data-accuracy').innerHTML = this._newData.accuracy;
		}
		
		Y.util.Dom.removeClass(this._data.telHtml,"field-input");
		Y.util.Dom.addClass(this._data.telHtml,"field-data");
		this._data.telHtml.innerHTML = arg?this._newData.tel:this._data.tel;
		
		Y.util.Dom.addClass(this._data.btnContHtml,"hidden");
		if(arg)
		{
			Y.util.Dom.get('user-panel-nominativo').innerHTML = this._newData.nome+' '+this._newData.cognome;
			if(this._innerTooltip)
				this._innerTooltip.close();
			this._innerTooltip = new MEB.InnerTooltip({target: Y.util.Dom.get('profile-innertooltip'),align:'inner',content:CNA.lang.common['profile_change_ok'],closeButton:false});
		}
	},
	_sendOK:function(o)
	{
		var arg = new Object();
		arg.admin1 = o.getElementsByTagName('admin1')[0].firstChild.data;
		arg.admin2 = o.getElementsByTagName('admin2')[0].firstChild.data;
		this._modifyBack(null,arg);
	},
	_changePwd:function()
	{
		if(this._innerTooltip)
			this._innerTooltip.close();
		var p = new CNA.ChangePwdPopup();
		p.onClose.subscribe(this._openInnerTooltip,null,this);
	},
	_openInnerTooltip: function(text)
	{
		if(text)
		{
			if(this._innerTooltip)
				this._innerTooltip.close();
			this._innerTooltip = new MEB.InnerTooltip({target: Y.util.Dom.get('profile-innertooltip'),align:'inner',content:CNA.lang.common['profile_change_pwd_ok'],closeButton:false});
		}
	},
	_changeMyAddress:function()
	{
		if(this._innerTooltip)
			this._innerTooltip.close();
		var p = new CNA.ChangeMyaddressPopup({value: this._myAddressValue});
		p.onClose.subscribe(this._changeHtmlMyaddress,null,this);
	},
	_changeHtmlMyaddress:function(arg)
	{
		if(arg)
		{
			this._myAddressValue.country = arg.value.country;
			this._myAddressValue.city = arg.value.city;
			this._myAddressValue.city_name =arg.value.city_name;
			this._myAddressValue.admin1 =arg.value.admin1;
			this._myAddressValue.admin2 =arg.value.admin2;
			this._myAddressValue.zipcode =arg.value.zipcode;
			this._myAddressValue.address =arg.value.address;
			this._myAddressValue.latitude =arg.value.latitude;
			this._myAddressValue.longitude =arg.value.longitude;
			this._myAddressValue.accuracy =arg.value.accuracy;
			
			Y.util.Dom.get('myaddress-container').innerHTML = arg.display;
			if(this._innerTooltip)
				this._innerTooltip.close();
			this._innerTooltip = new MEB.InnerTooltip({target: Y.util.Dom.get('profile-innertooltip-myaddress'),align:'inner',content:CNA.lang.common['profile_change_myaddress_ok'],closeButton:false});
		}
	}
});

CNA.ChangePwdPopup=function(configs)
{
	configs = new Object();
	this._oDiv = document.createElement('DIV');
	this._oDiv.className = 'change-pwd-popup';
	this._oDiv.innerHTML =	'<form id="login-form" action="javascript:void(0);">'+
							'<div class="field"><div class="field-label">'+MEB.lang.fields['password']+':</div><div class="field-input"><input id="change-pwd-old" type="password" value=""/></div></div>'+
							'<div class="field"><div class="field-label">'+MEB.lang.fields['new_password']+':</div><div class="field-input"><input id="change-pwd-new" type="password" value=""/></div></div>'+
							'<div class="field-btn center"><button id="btn-change"></button></div></form>';
	
	configs.body = this._oDiv;
	CNA.ChangePwdPopup.superclass.constructor.call(this,configs);
	this.render();
	
	this._changeBtn = new MEB.Button({id:'btn-change',content: CNA.lang.common['modify']});
	this._changeBtn.onClick.subscribe(this._changeSend,null,this);
}
Y.lang.extend(CNA.ChangePwdPopup, MEB.Popup,{
	_tooltip:null,
	close:function(a)
	{
		if(this._tooltip)
			this._tooltip.close();
		CNA.LoginPopup.superclass.close.call(this,a);	
	},
	_changeSend: function()
	{
		if(this._tooltip)
			this._tooltip.close();
		
		var oldPwd = Y.util.Dom.get('change-pwd-old');
		if(!oldPwd.value){
			this._loginError(200,'old');
			return;
		}
		var newPwd = Y.util.Dom.get('change-pwd-new');
		if(!newPwd.value){
			this._loginError(200,'new');
			return;
		}
			
		str='oldPwd='+encodeURIComponent(oldPwd.value)+'&newPwd='+encodeURIComponent(newPwd.value);	
		new MEB.Connect('POST', MEB.AJAX_BASE+'change_pwd.php',{ ok:this._loginOK, error: this._loginError, scope:this} ,str, {t:this._changeBtn, p:'r'});
		this._changeBtn.disable();
	},
	_loginOK: function()
	{
		this.close(true);
	},
	_loginError: function(e,txt)
	{
		this._changeBtn.enable();
		switch(e)
		{
			case 217:
				this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target:Y.util.Dom.get('change-pwd-old'),align:'t',type:'error',content:MEB.lang.errors[e],closeButton:false,closeOnChange:true});
				return true;
			case 202:
				this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target:Y.util.Dom.get('change-pwd-new'),align:'t',type:'error',content:MEB.lang.errors[e],closeButton:false,closeOnChange:true});
				return true;
			case 200:
				var target = Y.util.Dom.get('change-pwd-'+txt);
				if(target)
					this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: target,align:'t',type:'error',content:MEB.lang.errors[e],closeButton:false,closeOnChange:true});
				return true;
		}
	}
});

CNA.ChangeMyaddressPopup=function(configs)
{
	if(configs)
		this._value = configs.value;
	else
		configs = new Object();

	this._country = 'IT';
	this._oDiv = document.createElement('DIV');
	this._oDiv.className = 'change-myaddress-popup'; 
	this._oDiv.innerHTML =	'<form id="login-form" action="javascript:void(0);">'+
							'<div class="text">'+CNA.lang.common['change_myaddress_text']+'</div>'+
							'<div id="myaddress-location-container"></div>'+
							'<div class="field-btn center"><button id="btn-change"></button></div></form>';
	
	configs.body = this._oDiv;
	CNA.ChangeMyaddressPopup.superclass.constructor.call(this,configs);
	this.render();
	this.startPooling();
		
	this._map = new MEB.Location({container:Y.util.Dom.get('myaddress-location-container'),id:'location-myaddress',country: this._country, value: (this._value?this._value:null),countries: false});
	this._map.initMap({autoShowMap:true,addressNotRequired:true});
	this._changeBtn = new MEB.Button({id:'btn-change',content: CNA.lang.common['modify']});
	this._changeBtn.onClick.subscribe(this._changeSend,null,this);
}
Y.lang.extend(CNA.ChangeMyaddressPopup, MEB.Popup,{
	_tooltip:null,
	close:function(a)
	{
		if(this._tooltip)
			this._tooltip.close();
		CNA.LoginPopup.superclass.close.call(this,a);	
	},
	_changeSend: function()
	{
		if(!this._map.check({geo:true,address:true}))
			return false;
			
		var str='country='+this._map.country;
		if(this._map.value.city)
			str += '&city='+this._map.value.city+'&city_name='+encodeURIComponent(this._map.value.city_name);
		else 
			str += '&city=&city_name='+encodeURIComponent(this._location.value.city_name);
		str += '&admin1='+this._map.value.admin1+'&admin2='+this._map.value.admin2+'&address='+encodeURIComponent(this._map.value.address)+'&latitude='+this._map.value.latitude+'&longitude='+this._map.value.longitude+'&accuracy='+this._map.value.accuracy;
		
		new MEB.Connect('POST', MEB.AJAX_BASE+'change_myaddress.php',{ ok:this._loginOK, error: this._loginError, scope:this} ,str, {t:this._changeBtn, p:'r'});
		this._changeBtn.disable();
	},
	_loginOK: function(o)
	{
		var value = new Object();
		value.city = o.getAttribute('city');
		value.country = o.getAttribute('country');
		value.admin1 = o.getAttribute('admin1');
		value.admin2 = o.getAttribute('admin2');
		value.latitude = o.getAttribute('latitude');
		value.longitude = o.getAttribute('longitude');
		value.accuracy = o.getAttribute('accuracy');
		value.city_name = o.childNodes[0].firstChild.nodeValue;
		if (o.childNodes[1].firstChild)
			value.address = o.childNodes[1].firstChild.nodeValue;
		
		var display = o.childNodes[2].firstChild.nodeValue;
		this._map.destroy();
		this.close({value : value, display: display});
	},
	_loginError: function(e,txt)
	{
		this._changeBtn.enable();
	}
});


CNA.ShopMap=function(configs)
{
	CNA.ShopMap.superclass.constructor.call(this,configs);
	this._directionService = new google.maps.DirectionsService();
	this._directionRender = new google.maps.DirectionsRenderer();
	if(MEB.LANGUAGE=='en')
		this._unitSystem = google.maps.DirectionsUnitSystem.IMPERIAL;
	else
		this._unitSystem = google.maps.DirectionsUnitSystem.METRIC;
	this._mapType = google.maps.MapTypeId.ROADMAP;
	this._disableMapUI = true;
	this._zoom = 14;
	this._viewportRounds = 0.012;
	this._markerImage = configs.markerImage?configs.markerImage:MEB.IMG_BASE+'marker_sedi.png';
	this._mapHtml = Y.util.Dom.get('map');
	this._routeHtml = Y.util.Dom.get('route-panel-container');
	this._btnChangeMyaddress = Y.util.Dom.get('btn-change-myaddress');
	Y.util.Event.addListener(this._btnChangeMyaddress,'click',this._changeMyAddress,null,this);
	
	this._points = configs.points;
	this._data = configs.data;
	this._address = configs.address;
	this._markers = new Array();
	this._load();
}
Y.lang.extend(CNA.ShopMap, CNA.Page,{
	_bounds:null,_win:null,_nowRoute:null,_hideMarker:null,
	_load: function()
	{
		var option = {zoom: this._zoom, mapTypeId: this._mapType, disableDefaultUI: this._disableMapUI, navigationControl: true, navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL}  };
		this._map = new google.maps.Map(this._mapHtml, option);
		this._directionRender.setMap(this._map);
		this._directionRender.setPanel(Y.util.Dom.get('route-panel'));
		
		var i=0;
		for(i;i<this._points.length;i++)
			this._setMarker(this._points[i].lat,this._points[i].lng,i);

		if(i>1)
		{
			var ne = new google.maps.LatLng(this._bounds.getNorthEast().lat()+this._viewportRounds,this._bounds.getNorthEast().lng()+this._viewportRounds);
			var sw = new google.maps.LatLng(this._bounds.getSouthWest().lat()-this._viewportRounds,this._bounds.getSouthWest().lng()-this._viewportRounds);
			var bounds = new google.maps.LatLngBounds(sw,ne);
			this._map.fitBounds(bounds);
		}
	},
	_setMarker: function(lat,lng,i)
	{
		var point = new google.maps.LatLng(lat,lng);
		this._markers[i] = new google.maps.Marker({position: point,	map: this._map, icon: this._markerImage});
		var self = this;
		google.maps.event.addListener(this._markers[i], 'click', function() {self._openWindow(i);});
		if(!this._bounds)
		{
			this._map.setCenter(point);
			this._bounds = new google.maps.LatLngBounds(point,point);
		}
		else
		{
			this._bounds.extend(point);
		}		
	},
	_openWindow: function(i)
	{
		if(this._win)
			this._win.close();
		else
			this._win = new google.maps.InfoWindow();
		
		this._win.setContent(this._data[i]);
		this._win.open(this._map,this._markers[i]);
	},
	_changeMyAddress:function(e)
	{
		if(e)
			Y.util.Event.stopEvent(e);
		if(this._innerTooltip)
			this._innerTooltip.close();
		var p = new CNA.ChangeMyaddressPopup({value: this._myAddressValue});
		p.onClose.subscribe(this._closeMyaddressDirection,null,this);
	},
	calcolateDirection:function(i)
	{
		if(this._timerRoute)
			return;
		if(this._innerTooltip)
			this._innerTooltip.close();
		if(!this._myAddressValue)
		{
			var p = new CNA.ChangeMyaddressPopup();
			p.onClose.subscribe(this._closeMyaddressDirection,i,this);
		}
		else
		{
			this._nowRoute = i;
			Y.util.Dom.addClass(this._routeHtml,"hidden");
			//if(!Y.util.Dom.hasClass(this._routeHtml,"hidden"))
			//{
			//	new Y.util.Anim(this._routeHtml, {opacity: { from: 1, to: 0 }}, 1, Y.util.Easing.easeOut	).animate();
			//}
			this._loading = new MEB.Loading(Y.util.Dom.get('route-loading'));
			this._loading.show();
			var start = new google.maps.LatLng(this._myAddressValue.latitude,this._myAddressValue.longitude);
			var end = new google.maps.LatLng(this._points[i].lat,this._points[i].lng);
			var request = { origin:start,  destination:end, travelMode: google.maps.DirectionsTravelMode.DRIVING, unitSystem: this._unitSystem };
			var self=this;
   			this._directionService.route(request, function(response, status){self._directionResults(response, status);});
		}
	},
	_directionResults: function(response, status) 
	{
 	   	if (status == google.maps.DirectionsStatus.OK)
 	   	{
 	   		this._timerRoute = Y.lang.later(1000,this,this._showDirection,response);
   	  	}
   	  	else
   	  	{
   	  		this._timerRoute = Y.lang.later(1000,this,this._showError,null);
   	  	}
    },
    _showError:function()
    {
    	this._innerTooltip = new MEB.InnerTooltip({target: Y.util.Dom.get('inner-tooltip'),align:'inner',content:CNA.lang.common['search_route_error'],closeButton:false,type: 'error'});
 	  	this._loading.hide();
 	  	this._timerRoute = null;
    },
    _showDirection: function(response)
    {
    	if(this._hideMarker!=null)
    		this._markers[this._hideMarker].setVisible(true);
    	Y.util.Dom.removeClass(this._routeHtml,"hidden");
		//Y.util.Dom.setStyle(this._routeHtml, 'opacity', 1.0);
		var route = response.trips[0].routes[0];
		route.start_geocode.formatted_address= this._myAddressValue.address+', '+this._myAddressValue.city_name+' ('+this._myAddressValue.admin2+')';
		route.end_geocode.formatted_address=this._address[this._nowRoute];
		this._markers[this._nowRoute].setVisible(false);
		this._hideMarker = this._nowRoute;
    	this._directionRender.setDirections(response);
    	
    	Y.util.Dom.get('route-panel-distance').innerHTML = route.distance.text;
    	this._loading.hide();
    		this._timerRoute = null;
		if(this._win)
			this._win.close();
    },
	_closeMyaddressDirection:function(arg,i)
	{
		if(arg)
		{
			if(!this._myAddressValue)
				this._myAddressValue = new Object();
			this._myAddressValue.country = arg.value.country;
			this._myAddressValue.city = arg.value.city;
			this._myAddressValue.city_name =arg.value.city_name;
			this._myAddressValue.admin1 =arg.value.admin1;
			this._myAddressValue.admin2 =arg.value.admin2;
			this._myAddressValue.zipcode =arg.value.zipcode;
			this._myAddressValue.address =arg.value.address;
			this._myAddressValue.latitude =arg.value.latitude;
			this._myAddressValue.longitude =arg.value.longitude;
			this._myAddressValue.accuracy =arg.value.accuracy;
			if(i!=null)
			{
				this.calcolateDirection(i);
			}
			else if(this._nowRoute!=null)
			{
				this.calcolateDirection(this._nowRoute);
			}
		}
	}
});

CNA.Partecipa=function(configs)
{
	CNA.Partecipa.superclass.constructor.call(this,configs);
	
	this._btnPrivacy = Y.util.Dom.get('btn-privacy');
	Y.util.Event.addListener(this._btnPrivacy,'click',this._openPrivacy,null,this);

	this._btnReg = new MEB.Button({id: "btn-partecipa", content: CNA.lang.common['iscriviti']});
	this._btnReg.onClick.subscribe(this._send,null,this);
	this._map = new MEB.Location({container:document.getElementById('location-container'),id:'location',country:this._country, zipcodeField:true,markerImage:MEB.IMG_BASE+'marker_sedi.png'});
	this._map.initMap(true);
	this._logoUploader=new MEB.Uploader({container: Y.util.Dom.get('logo-upload'),name: 'logo',action: MEB.JSON_BASE+'upload_logo.php'});
	this._logoUploader.onChange.subscribe(this._picUploadComplete,null,this);
	
	this._configsCkPagina = { resize_enabled : false,
	toolbar :  [ ['Undo','Redo'],['Bold','Italic','Underline','-','Subscript','Superscript'],['NumberedList','BulletedList','-','Outdent','Indent'] ] };

	this._editorPagina = CKEDITOR.replace( 'ckPagina', this._configsCkPagina);
	new MEB.PhoneInput({id: 'phone'});
}
Y.lang.extend(CNA.Partecipa, CNA.Page,{
	_tooltip:null,
	_picUploadComplete: function(obj)
	{
		if(obj.err){
			Y.util.Dom.get('logo-preview').innerHTML = '';
			if (obj.err==210)
			{
				this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target:this._logoUploader,align:'r',type:'error',content:MEB.lang.errors[210],closeButton:false,closeOnChange:true});
				return true;
			}
			else
				return false;
		}
		Y.util.Dom.get('logo-preview').innerHTML = '<img src="'+obj.src+'" />';
		return true;
	},
	_send:function()
	{
		if(this._tooltip)
			this._tooltip.close();
		var title = Y.util.Dom.get('title').value;
		if(title=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('title'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		var username = Y.util.Dom.get('username').value;
		if(username=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('username'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		var company = Y.util.Dom.get('company').value;
		if(company=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('company'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		var piva = Y.util.Dom.get('piva').value;
		if(piva=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('piva'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		
		if(!this._map.check({geo:true}))
			return false;	
			
		var email = Y.util.Dom.get('email').value;
		if(email=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('email'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		if(!MEB.isValidEmail(email))
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('email'),align:'t',type:'error',content:MEB.lang.errors[201],closeButton:false,closeOnChange:true});
			return;
		}		
		var phone = Y.util.Dom.get('phone').value;
		if(phone=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('phone'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:false,closeOnChange:true});
			return;
		}
		var website = Y.util.Dom.get('website').value;
		var pagina = CKEDITOR.instances.ckPagina.getData();
		if(pagina=="")
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target: Y.util.Dom.get('ck-pagina-container'),align:'t',type:'error',content:MEB.lang.errors[200],closeButton:true});
			return;
		}
		
		var str='country='+this._map.country;
		if(this._map.value.city)
			str += '&city='+this._map.value.city+'&city_name='+encodeURIComponent(this._map.value.city_name);
		else 
			str += '&city=&city_name='+encodeURIComponent(this._location.value.city_name);
		str += '&admin1='+this._map.value.admin1+'&admin2='+this._map.value.admin2+'&zipcode='+this._map.value.zipcode+'&address='+encodeURIComponent(this._map.value.address)+'&latitude='+this._map.value.latitude+'&longitude='+this._map.value.longitude+'&accuracy='+this._map.value.accuracy;
		
		str += '&title='+encodeURIComponent(title)+'&username='+encodeURIComponent(username)+'&company='+encodeURIComponent(company)+'&piva='+encodeURIComponent(piva)+'&email='+encodeURIComponent(email)+'&phone='+encodeURIComponent(phone)+'&website='+encodeURIComponent(website);		
		str += '&pagina='+encodeURIComponent(pagina);

		new MEB.Connect('POST', MEB.AJAX_BASE+'partecipa.php',{ ok:this._sendOK, error:this._sendERROR, scope:this} ,str, {t:this._btnReg, p:'r'});
	},
	_sendOK:function()
	{
		window.location.href = MEB.SITE_URL+'index.php'; 
	},
	_sendERROR:function(e,txt)
	{
		if(e==210)
		{
			this._tooltip = new MEB.Tooltip({code:'cna-tooltip',target:Y.util.Dom.get('logo-upload'),align:'t',type:'error',content:MEB.lang.errors[e],closeButton:false,closeOnChange:true});
			return true;
		}
		return false;
	},
	_openPrivacy:function()
	{
		new CNA.PrivacyPopup();
	}
});
