accomodation=0;
//---------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------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.idVendor=0;
	this.tabCount;
	this.parameters;
	
	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-strutture'));
		
		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',this.localita));
			else if(mode==2)
				myForm.appendChild(newInput('provincia',this.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;
	}
	
	this.showDivResults = function(idVendor,trattamento){
		this.parameters = this.getParameters();
		if(idVendor!=0){
			this.idVendor = idVendor;
		}
		else{
			idVendor = this.idVendor;
		}
		if(idVendor==undefined)
			return false;
		idt='#results';
		if(this.tabCount){
			idt='#corpoprev_' + this.tabCount;
			this.parameters['count']=this.tabCount;
		}
		this.parameters['idvendor']=idVendor;
		if(trattamento)
			this.parameters['trattamento']=trattamento;
		$.post(".eshop?action=result-strutture&modejs=1", this.parameters,function(data){$(idt).html(data);});
		return false;
	}
	
	this.showScontrino = function(prSelected,trattamento,num){
		this.parameters = this.getParameters();
		var tmp_pr = this.parameters;
		prSelected.each(function(){tmp_pr[$(this).attr('name')]=$(this).val();});
		if(!trattamento)trattamento=0;
		this.parameters['trattamento']=trattamento;
		if(this.tabCount){
			this.parameters['count']=this.tabCount;
		}
		var id='#scontrino';
		if(num)id = id + num;
		
		$.post(".eshop?action=scontrino&modejs=1", this.parameters,function(data){$(id).html(data);});
		return false;
	}
	
	this.getParameters  = function(){
		var parameters = {};
		parameters['arrivo'] = this.dataArrivo;
		parameters['partenza'] = this.dataPartenza;
		parameters['stanze'] = this.fullProduct.length;
		for(i=1;i<=this.fullProduct.length;i=i+1){
			var fp = this.fullProduct[i-1];
			parameters['trattamento'] = fp.trattamento;
			parameters['adulti' + i]=fp.adulti;
			parameters['bambini' + i]=fp.etaBambini.length;
			for(j=1;j<=fp.etaBambini.length;j=j+1){
				parameters['etabambino' + i + j]=fp.etaBambini[j-1];
			}
		}
		return parameters;
	}
	
	this.saveEstimate = function(){
		try{
			var params = this.parameters;
			params['type']='1';
			var count='';
			if(this.tabCount){
				count=this.tabCount;
			}
			$.post(".trattative?action=salvapreventivo&modejs=1", params,
					function(data){$("#message_" + count).html(data);
					});
		}
		catch(e){alert(e.message);}
	}
	
	this.book = function(mode){
		
			
		var myForm = document.createElement("form");
		myForm.method='post';
		myForm.action = '.it.eshop';
		myForm.appendChild(newInput('act','bookacc'));
		if(mode && mode==1)
			myForm.appendChild(newInput('typeReq','info'));
		for(var key in this.parameters){
			myForm.appendChild(newInput(key,this.parameters[key]));
		}
		document.body.appendChild(myForm) ;
		myForm.submit() ;
		document.body.removeChild(myForm) ;
	}
}
//---------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------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);
	}
}
//--------------------
//---------------------
//-----BIND-------------------------------
$(document).ready(function() {
$('#resulttb a').click(
	function() {
		$('#resulttb tr').removeClass('active');
		$(this).parent().parent().addClass('active');
		accomodation.showDivResults($(this).attr('id'));
		return false;
	}
);
$('#results .trattamento').live('click',
		function() {
			var tr = $("input[name='trattamento']:checked").val();
			//var pr =  $(".checkproduct:checked");
			accomodation.showDivResults(0,tr);
			//accomodation.showScontrino(pr,tr);
		}
	);
$('#results .checkproduct:radio').live('click',
		function() {
			var tr = $("input[name='trattamento']:checked").val();
			var pr =  $(".checkproduct:checked");
			accomodation.showScontrino(pr,tr);
		}
	);
if(accomodation!=0)
	accomodation.showDivResults($('#resulttb a.first').attr('id'));

if($.isFunction($('#resulttb').dataTable)){
jQuery.fn.dataTableExt.oSort['numeric-comma-asc']  = function(a,b) {
	 	var x = (a == "-") ? 0 : a.replace( /,/, "." ).replace( ".", "" ).replace( ".", "" ).replace( "€", "" );
		var y = (b == "-") ? 0 : b.replace( /,/, "." ).replace( ".", "" ).replace( ".", "" ).replace( "€", "" );
		x = parseFloat( x );
		y = parseFloat( y );
		return ((x < y) ? -1 : ((x > y) ?  1 : 0));
	};

jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function(a,b) {
	var x = (a == "-") ? 0 : a.replace( /,/, "." ).replace( ".", "" ).replace( ".", "" ).replace( "€", "" );
	var y = (b == "-") ? 0 : b.replace( /,/, "." ).replace( ".", "" ).replace( ".", "" ).replace( "€", "" );
	x = parseFloat( x );
	y = parseFloat( y );
	return ((x < y) ?  1 : ((x > y) ? -1 : 0));
};

jQuery.fn.dataTableExt.aTypes.push(
		function ( sData )
		{
			var sValidChars = "0123456789-,";
			var Char;
			var bDecimal = false;
			
			/* Check the numeric part */
			for ( i=0 ; i<sData.length ; i++ )
			{
				Char = sData.charAt(i);
				if (sValidChars.indexOf(Char) == -1)
				{
					return null;
				}
				
				/* Only allowed one decimal place... */
				if ( Char == "," )
				{
					if ( bDecimal )
					{
						return null;
					}
					bDecimal = true;
				}
			}
			
			return 'numeric-comma';
		}
	);

tableSorter = $('#resulttb').dataTable({"aoColumns": [null,{ "sType": "html" },null,{ "sType": "numeric-comma" },{ "sType": "numeric-comma" }],
										"aaSorting": [[0,'asc']],
										"bPaginate": false,
                                		"bLengthChange": false,
                                		"bFilter": false,
                                		"bSort": true,
                                		"bInfo": false,
                                		"bAutoWidth": false                                         
}
);

idOrdinamenti = ['headdisp','headname','headloc','headgiudizi','headprice'];

/*
var count=0;
for( id in idOrdinamenti){
	var ordPrim = idOrdinamenti[id];
	var ordSec = 'desc';
	if(ordPrim=='desc')
		ordSec='asc';
	$('#' + id).toggle(
			function() {
				$('#headtb th').removeClass('asc').removeClass('desc');
				tableSorter.fnSort( [ [count,ordPrim]]);
				$('#headdisp').addClass(ordPrim);
			},
			function() {
				$('#headtb th').removeClass('asc').removeClass('desc');
				tableSorter.fnSort( [ [count,ordSec]]);
				$('#headdisp').addClass(ordSec);
			}	
	);
	count = count +1;
}
*/
$('#headdisp').toggle(
		function() {
			$('#headtb th').removeClass('asc').removeClass('desc');
			tableSorter.fnSort( [ [0,'asc']]);
			$('#headdisp').addClass('asc');
			$('#selorder').val(0);
		},
		function() {
			$('#headtb th').removeClass('asc').removeClass('desc');
			tableSorter.fnSort( [ [0,'desc']]);
			$('#headdisp').addClass('desc');
			$('#selorder').val(0);
		}
	);

$('#headname').toggle(
	function() {
		$('#headtb th').removeClass('asc').removeClass('desc');
		tableSorter.fnSort( [ [1,'asc']]);
		$('#headname').addClass('asc');
		$('#selorder').val(1);
	},
	function() {
		$('#headtb th').removeClass('asc').removeClass('desc');
		tableSorter.fnSort( [ [1,'desc']]);
		$('#headname').addClass('desc');
		$('#selorder').val(1);
	}
);

$('#headloc').toggle(
	function() {
		$('#headtb th').removeClass('asc').removeClass('desc');
		tableSorter.fnSort( [ [2,'asc']]);
		$('#headloc').addClass('asc');
		$('#selorder').val(2);
	},
	function() {
		$('#headtb th').removeClass('asc').removeClass('desc');
		tableSorter.fnSort( [ [2,'desc']]);
		$('#headloc').addClass('desc');
		$('#selorder').val(2);
	}
);


$('#headgiudizi').toggle(
		function() {
			$('#headtb th').removeClass('asc').removeClass('desc');
			tableSorter.fnSort( [ [3,'desc']]);
			$('#headgiudizi').addClass('desc');
			$('#selorder').val(3);
		},
		function() {
			$('#headtb th').removeClass('asc').removeClass('desc');
			tableSorter.fnSort( [ [3,'asc']]);
			$('#headgiudizi').addClass('asc');
			$('#selorder').val(3);
		}
	);
$('#headprice').toggle(
		function() {
			$('#headtb th').removeClass('asc').removeClass('desc');
			tableSorter.fnSort( [ [4,'asc']]);
			$('#headprice').addClass('asc');
			$('#selorder').val(4);
		},
		function() {
			$('#headtb th').removeClass('asc').removeClass('desc');
			tableSorter.fnSort( [ [4,'desc']]);
			$('#headprice').addClass('desc');
			$('#selorder').val(4);
		}
);

$('#selorder').change(
	function(){
		riordinaRicerca($(this).val(),tableSorter);
	});
}
});

function riordinaRicerca(fieldRicerca,tableSorter){
	var ord = 'asc';
	if(fieldRicerca==3)
		ord ="desc";
	
	$('#headtb th').removeClass('asc').removeClass('desc');
	tableSorter.fnSort( [ [fieldRicerca,ord]]);
	$('#' + idOrdinamenti[fieldRicerca]).addClass(ord);
}

//------------------------------------------------------------------
//---------------METODI VIEW PLACE----------------------------------
//------------------------------------------------------------------
numPrev=1;
function addPreventivo(){
	$('#btn_preventivo').show();
	$("#navdetail li").removeClass();
	$("#btn_preventivo").addClass("active");
	$(".infostruttura").hide();
	$("#preventivi").show();
	$('#preventivi #navprev li' ).removeClass('active');
	$("#preventivi #navprev").append("<li id=\"prev_" + numPrev + "\" class=\"active\"><a href=\"#\" onclick=\"return showPreventivo(this);\"><span>&nbsp;&nbsp;" + numPrev + "&nbsp;&nbsp;</span></a></li>");
	$("#preventivi div.datiprev").removeClass('last').hide();
	$("#preventivi").append("<div id=\"corpoprev_" + numPrev + "\" class=\"datiprev last\"></div>");
	params=$("#ricercaEsperienza").serialize();
	params+='&count=' + numPrev+'&modejs=1';
	$.post(".eshop", params,
		function(data){
			$("#preventivi .last").html(data);
		}
	);
	
	numPrev = numPrev + 1;
	return false;
}

function modificaRicerca(tabCount,dataArrivo,dataPartenza,trattamento,durata){
	if(dataArrivo && dataArrivo.length > 0){
		var arrivo = document.getElementById('arrivo');
		arrivo.value=dataArrivo;
	}
	var partenza = document.getElementById('partenza');
	if(partenza && dataPartenza && dataPartenza.length > 0){		
		partenza.value=dataPartenza;
	}
	else if(durata ){
		var settimane = document.getElementById('settimane');
		if(settimane){
			var tmp_d = durata/7;
			settimane.value = tmp_d;
		}
	}
	if(trattamento && trattamento > 0){
		var selectTr = document.getElementById('trattamento');
		var i;
		var el;
		for(i=0;i < selectTr.options.length;i=i+1){
			el = selectTr.options[i];
			if(el.value == trattamento){
				selectTr.selectedIndex = i;
				break;
			}
		}
	}
	params=$("#ricercaEsperienza").serialize();
	params+='&count=' + numPrev+'&modejs=1';
	$.post(".eshop", params,
		function(data){
			$("#corpoprev_" + tabCount).html(data);
		}
	);
	return false;
}

function showPreventivo(tag){
	var idNav = $(tag).parent().attr('id');
	var i = idNav.replace('prev_','');
	$('#preventivi div.datiprev').hide();
	$('#preventivi #navprev li' ).removeClass('active');
	$('#preventivi #navprev #prev_' + i).addClass('active');
	$('#preventivi #corpoprev_' + i).show();
	return false;
}

function closePreventivo(tabCount){
	$('#prev_' + tabCount).remove();
	$('#corpoprev_' + tabCount).remove();
	tabCount = tabCount - 1;
	while(tabCount > 0){
		var prev = $('#preventivi #navprev #prev_' + tabCount);
		if(prev.length > 0){
			prev.addClass('active');
			$('#preventivi #corpoprev_' + tabCount).show();
			return false;
		}
		tabCount = tabCount - 1;
	}
	while(tabCount < 100 ){
		var prev = $('#preventivi #navprev #prev_' + tabCount);
		if(prev.length > 0){
			prev.addClass('active');
			$('#preventivi #corpoprev_' + tabCount).show();
			return false;
		}
		tabCount = tabCount + 1;
	}
	$('#preventivi').hide();
	$('#btn_preventivo').hide();
	$('#btn_descrizione').addClass('active');
	$('#dettagliStruttura').show();
	return false;
}

function showWindowViewPlace(idNotizia,titolo_url,path){
	var w = screen.availWidth ;
	var h = screen.availHeight - 100 ;
	var ww = 780 ; if(w < 780) ww = w ;
	var l = Math.round((w - 780) / 2);
	var opt = 'toolbar=no,scrollbar=auto,resizable=no,status=no,scrollbars=yes,top=40,width='+ww+',height=' + h + ',left='+l ;
	var link = '/' + path + idNotizia + '-' + titolo_url + '.html' ;
	window.open(link, '', opt) ;
	return false;
}


