var vendor=[];
var divNoSelection;

addProduct=function(searchCount, _numV, idprodotto, disponibili, prenotabili, prezzo, _stanza, _num, statoDisponibilita,altro){
var numprodotto=_num - 1;
var numstanza=_stanza - 1;

var numV=''+searchCount+'_'+_numV;

var partid=''+numV +'_'+_stanza+'_'+_num;

var vendor=getVendor(searchCount, _numV);

var boxprodotto=vendor.getStanza(numstanza).getBoxProdotto(numprodotto);
boxprodotto.box=boxprodotto.getBox(partid, vendor, numstanza, numprodotto);
boxprodotto.radio=document.getElementById('radio_'+ partid);
boxprodotto.prezzo=1*(prezzo.replace('.','').replace(',', '.'));
boxprodotto.prezzononscontato=altro.pns;
boxprodotto.spese=altro.spese;
boxprodotto.nome=altro.nome;
vendor.dotx=altro.dotx;
vendor.doty=altro.doty;
vendor.prov=altro.prov;

var prodotto=vendor.getProdotto(idprodotto);
prodotto.prenotabili=prenotabili;
prodotto.disponibili=disponibili;
prodotto.statoDisponibilita = statoDisponibilita;
prodotto.id=idprodotto;
prodotto.chiesti=0;
prodotto.deposito=altro.deposito;
boxprodotto.prodotto=prodotto;
boxprodotto.refresh();
}

getVendor=function(search, _n){
var n=search +'_'+ _n;
if(! vendor[n]){vendor[n]=new VendorResult(n); vendor[n].searchCount=search;}
vendor[n].searchCount=search;
return vendor[n];
}

/*VENDOR RESULT*/
function VendorResult(numV){
this.getInfo=function(){
if(!this.infoV)this.infoV=document.getElementById('dett_'+numV);
return this.infoV;
}

this.getStanza=function(numstanza){
if(! this.stanze[numstanza])this.stanze[numstanza]=new Stanza(numstanza);
return this.stanze[numstanza];
}

this.getProdotto=function(id){
if(! this.prodotti[id])this.prodotti[id]=new Prodotto(id);
return this.prodotti[id];
}
this.autoSelect=function(){for(var i=0; i < this.stanze.length; i++)this.stanze[i].autoSelect();}
this.refresh=function(){for(var i=0; i < this.stanze.length; i++)this.stanze[i].refresh();this.update();}
this.reset=function(){for(var i=0; i < this.stanze.length; i++)this.stanze[i].reset();}
this.update=function(){
this.resetProdotti();
var s=getScontrino(this.searchCount);
s.numStanze=this.stanze.length;
s.refresh(this);
}

this.getSelezionati=function(){
var r=[];var o;
for(var i=0; i < this.stanze.length; i++){o=this.stanze[i].getSelezionato();if(o)r[r.length]=o;}
return r;
}
this.resetProdotti=function(){for(i in this.prodotti)if(this.prodotti[i].chiesti)this.prodotti[i].chiesti=0;}
this.numero=numV;
this.prodotti=[];
this.nome='';
this.dotx=0;this.doty=0;
this.prov='';
this.stanze=[];
this.divTotale=document.getElementById('totalevendor_'+numV);
this.searchCount=0;
this.infoV;
this.foto;
this.url='';
}
/*STANZA*/
function Stanza(numstanza){
this.getBoxProdotto=function(numbox){
if(! this.boxprodotti[numbox])this.boxprodotti[numbox]=new BoxProdotto(numbox);
return this.boxprodotti[numbox];
}
this.reset=function(){for(var i=0; i < this.boxprodotti.length; i++)this.boxprodotti[i].reset();}
this.autoSelect=function(){
var selected=false;
for(var i=0; i < this.boxprodotti.length; i++){
if(this.boxprodotti[i].prodotto.isPrenotabile()){this.boxprodotti[i].setSelected(true);selected=true;}
if(!selected)this.boxprodotti[0].setSelected(true);
}}
this.refresh=function(){for(var i=0; i < this.boxprodotti.length; i++)this.boxprodotti[i].refresh();}
this.getSelezionato=function(){
for(var i=0; i < this.boxprodotti.length; i++){
if(this.boxprodotti[i].isSelected())return this.boxprodotti[i];
}}
this.numero=numstanza;
this.boxprodotti=[];	
}
/*BOX PRODOTTO*/
function BoxProdotto(num){
this.num=num;this.box;this.radio;
this.prodotto;this.prezzo=0;
this.prezzononscontato;
this.spese=[];this.nome;
this.setSelected=function(b){this.radio.checked=b;this.refresh();}
this.isSelected=function(){return this.radio.checked;}
this.refresh=function(){var p=this.prodotto;
try{
if(p.isDisponibile())this.className='disponibile';
else if(p.isPrenotabile())this.className='daverificare';
else { this.className='nondisponibile';  }
this.box.className =(this.radio.checked)?'selected':'';
}catch(e){if(document.debug){alert('BoxProdotto.refresh \n'+e.message);}}
}
this.reset=function(){this.setSelected(false);}
this.getBox=function(partid, vendor, stanza, numprodotto){
var el=document.getElementById('box_'+partid);
el.style.cursor ='pointer';
var myself=this;
el.onclick=function(){myself.clicked(vendor, stanza, numprodotto);};
return el;
}
this.clicked=function(vendor, stanza, numprodotto){
try{
vendor.getStanza(stanza).reset(); var p=0;
this.setSelected(true);p++;
vendor.refresh();
}catch(e){if(document.debug)alert('BoxProdotto.clicked('+p+'):\n'+e.message);}}
};
/*PRODOTTO*/
function Prodotto(){
this.idvendor=0;this.disponibili=0;
this.prenotabili=0;this.chiesti=0; this.deposito=0;this.statoDisponibilita=0;
this.isDisponibile=function(){return(this.statoDisponibilita!=4 && this.disponibili > this.chiesti);}
this.isPrenotabile=function(){return(this.statoDisponibilita==1 && this.prenotabili > 0 && this.prenotabili > this.chiesti);}
}
/*SCONTRINO*/
var scontrini=[];
function getScontrino(searchCount){
if(!scontrini[searchCount])scontrini[searchCount]=new Scontrino(searchCount);
return scontrini[searchCount];
}
function Scontrino(searchCount){
this.mainDiv=document.getElementById('scontrino_'+searchCount);
this.tblBody=document.getElementById('tblScontrino_'+searchCount);
this.rigaTotale=document.getElementById('tblScontrino_'+searchCount).firstChild;
this.numStanze=0;
this.clear=function(){while(this.tblBody.rows.length > 1)this.tblBody.deleteRow(0);}

this.addRiga=function(head, val, cssClass, thClass){
var tr,th,td;tr=document.createElement('TR');td=document.createElement('TD');
if(val){
th=document.createElement('TH');th.innerHTML=head+'&nbsp;:';
if(thClass)th.className=thClass;
td.innerHTML=val+'&nbsp;&euro;';tr.appendChild(th);
}
else{td.innerHTML=head;td.colSpan=2;td.className='note';}
if(cssClass)td.className=cssClass;
tr.appendChild(td);
try{this.tblBody.insertBefore(tr, this.rigaTotale);}catch(e){}
}
this.refresh=function(vendor){try{var p=0;
var selezionati=vendor.getSelezionati();p++;
if(this.numStanze > selezionati.length)vendor.autoSelect();
var prezzo=0;var prod,box;p++;
var disponibile=true; var prenotabile=true;p++;
this.clear();p++;
if(vendor.nome)this.addRiga(vendor.nome, '' ,'nomeVendor');p++;
if(vendor.foto)this.addRiga('<div class="imgContainer"><img src="'+vendor.foto+'" /></div><div id="mappaProvinceSmall" class="prov-'+vendor.prov+'" style="left:0;float:right;position:relative;top 2px;border:none;"><ul id="mapsmall"><li id="dot" style="left:'+vendor.dotx+'px; top:'+vendor.doty+'px;"></li></ul></div><div class="dettStrutt">'+vendor.getInfo().innerHTML+'<div style="padding-top:4px;text-align:right;"><a href="'+vendor.url+'" target="_blank"><img src="'+pathImages+'/dett_struttura_'+siglaLingua+'.gif" /></a></div></div>','','');
for(var i=0; i<selezionati.length;i++){
box=selezionati[i];prod=box.prodotto;
if(selezionati.length>0 && box.nome)this.addRiga(box.nome,'','nomeStanza');
if(prod && prod.isDisponibile){
prezzo += box.prezzo;
disponibile=disponibile && prod.isDisponibile();
prenotabile=prenotabile && prod.isPrenotabile();
prod.chiesti++;
for(s in box.spese)if(box.spese[s])this.addRiga(s, box.spese[s]);
if(prod.deposito)this.addRiga(deposito1+' <b>'+prod.deposito +'&nbsp;&euro;</b> '+deposito2);
}}
if(prezzo<sogliaSpeseAperturaPratica){this.addRiga(txtSpeseAperturaPratica,speseAperturaPratica,'','spese');prezzo+=speseAperturaPratica;}
if(!divNoSelection)divNoSelection=document.getElementById('noSelection');
if(this.numStanze == selezionati.length){
if(this.mainDiv)this.mainDiv.style.display='block';
if(divNoSelection)divNoSelection.style.display='none';
}else{
if(this.mainDiv)this.mainDiv.style.display='none';
if(divNoSelection)divNoSelection.style.display='block';
}p++;
var btn=document.getElementById('continuaBtn_'+searchCount);
var btn_info = document.getElementById('newInfo_'+searchCount);
var txt_info = document.getElementById('txtInfo_'+searchCount);
if(btn){p++;
if(prenotabile){btn.className='disp';btn.value=btn_disponibile;btn_info.className='newinfo';txt_info.className='';}
else if(disponibile){btn.className='daverif';btn.value=btn_daverificare;btn_info.className='hidden';txt_info.className='hidden';}
else { btn.className='nondisponibile'; btn.value=btn_nondisponibile;btn_info.className='hidden';txt_info.className='hidden';}
document.getElementById('totale_'+searchCount).innerHTML=formatEuro(prezzo)+' &euro;';
}}
catch(e){if(document.debug)alert('Scontrino.refresh('+p+')\n'+e.message);}
}
}

function formatEuro(val){
var v=new String(Math.floor(100 * val));var ret='';
for(var i=0; i < v.length -2; i++){ret += v.charAt(i);if((v.length -2 - i)% 4 == 0)ret += '.';}
ret += ',';ret += v.charAt(v.length -2);ret += v.charAt(v.length -1);
return ret;
}

function preSubmit(btn){
if(canSubmit(btn)){
var typeReqField =document.getElementById('idTypeReq');
if(typeReqField && btn.className=='newinfo'){typeReqField.value='info';}
else if(typeReqField){typeReqField.value='';}
return true;
}	
return false;
}
function canSubmit(btn){return(btn.className != 'nondisponibile');}

function saveEstimate(form,num){try{
var act='.sardegne/.trattative';
$('actionsearch_'+num).setAttribute('value','salvapreventivo');
var params=$(form+'_'+num).serialize()+'&modejs=1';
var tab='message_'+num;
var opt={evalScripts:true,method:'post',postBody:params}
new Ajax.Updater(tab, act, opt);
}
catch(e){alert(e.message);}
}

function ordina(idOrder,searchCount,isDesc,escludiOfferte){
var form = document.getElementById('formPlace');
if(!form){//BO
form = document.getElementById('formPlace_' +searchCount);form.action='.it.eshop';	
}
var fieldAction = document.getElementById('action_'+searchCount);
if(!fieldAction ){//BO
fieldAction = document.getElementById('actionsearch_'+searchCount);
}
fieldAction.value='search-place';
document.getElementById('idOrder_'+searchCount).value=idOrder;
document.getElementById('isDesc_'+searchCount).value=isDesc;
if(escludiOfferte)
	document.getElementById('tipoOfferta_'+searchCount).value='';
form.submit();
return false;
}
function goToBoxRicerca(searchCount){
	var form = document.getElementById('formPlace');
	//Se qua esiste già il form significa che sono su IE
	if(!form){//BO
		form = document.getElementById('formPlace_' +searchCount);	
		form.action='.it.eshop';			
	}
	var fieldAction = document.getElementById('action_'+searchCount);
	if(!fieldAction ){//BO
	fieldAction = document.getElementById('actionsearch_'+searchCount);
	}
	fieldAction.value='new-search';form.submit();
}
function changeLanguage(select,nomesito,webroot){
	var form = document.getElementById('formPlace');
	//Se qua esiste già il form significa che sono su IE
	if(!form){//BO
		form = document.getElementById('formPlace_' +searchCount);
	}
	var sigla = select.options[select.selectedIndex].value;
	form.action='http://' + sigla + '.' + nomesito  + webroot+ '/.' + sigla +'.eshop';	
	return false;
}
//---------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------ACCOMODATION---------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------
function Accomodation(arrivo,partenza,categoria,idPreventivo,localita,provincia){
	this.fullProduct=[];
	this.provincia=provincia;
	this.localita=localita;
	this.dataArrivo=arrivo;
	this.dataPartenza=partenza;
	this.categoria=categoria;
	this.idPreventivo=idPreventivo;
	
	this.addFullProduct= function(fp){
		this.fullProduct.push(fp);
	}
	/**
	*@param mode vale 1 per le ricerche per localita e 2 per le ricerche per provincia
	*
	*/
	this.goToSearch=function(mode,categoria){
		var cat = this.categoria;
		if(categoria)
			cat = categoria;
		var myForm = document.createElement("form");
		myForm.method='post';
		myForm.action = '.it.eshop';
		myForm.appendChild(newInput('action','search-place'));
		
		myForm.appendChild(newInput('idPreventivo',this.idPreventivo));
		myForm.appendChild(newInput('orderBy','5'));
		myForm.appendChild(newInput('tipoalloggio',cat));
		myForm.appendChild(newInput('trattamento',this.fullProduct[0].trattamento ));
		myForm.appendChild(newInput('arrivo',this.dataArrivo));
		myForm.appendChild(newInput('partenza',this.dataPartenza));
		myForm.appendChild(newInput('stanze',this.fullProduct.length));
		
		if(mode){
			if(mode == 1)
				myForm.appendChild(newInput('localita',localita));
			else if(mode==2)
				myForm.appendChild(newInput('provincia',provincia));
		}
		var i=0;
		for(i=0;i<this.fullProduct.length;i=i+1){
			var lbl_ad = 'adulti' + (i+1);
			var lbl_bb = 'bambini' + (i+1);
			myForm.appendChild(newInput(lbl_ad,this.fullProduct[i].adulti));
			myForm.appendChild(newInput(lbl_bb,this.fullProduct[i].etaBambini.length));
			var j=0;
			for(j=0;j<this.fullProduct[i].etaBambini.length;j=j+1){
				var lbl_etabb = 'etabambino' + (i + 1) + (j + 1);
				myForm.appendChild(newInput(lbl_etabb,this.fullProduct[i].etaBambini[j]));
			} 
		}
		
		document.body.appendChild(myForm) ;
		myForm.submit() ;
		document.body.removeChild(myForm) ;
		return true;
	}
	
	function newInput(attribute,value){
		var myInput = document.createElement("input") ;
		myInput.setAttribute("name", attribute) ;
		myInput.setAttribute("value", value);
		return myInput;
	}
}
//---------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------FULL PRODUCT---------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------
function FullProduct(adulti,prezzo,trattamento){
	this.adulti=adulti;
	this.etaBambini=[];
	this.prezzo=prezzo;
	this.trattamento=trattamento;
	
	this.addBambino=function(eta){
		this.etaBambini.push(eta);
	}
}
//--------------------
//---------------------