jQuery.fn.getCDATA = function() {
	var node = this[0].firstChild; 
		while ((node.nodeType != 4) && (node.nodeType != 8)) {
			 node = node.nextSibling; 
			 if (node==null) return "";
		}
	var ret=node.nodeValue;
	if (ret.substring(0,7)=="[CDATA["){
		ret=ret.substring(7);
		ret=ret.substr(0,ret.length-2);
	}
	return ret; 
};
/*************** Cerca Agenzie ***********************/
function activateCercaAgenzie() {
	_global_provincia=jQuery('#provincia').html();
	if (jQuery('#regione').val()!='') {
		jQuery('#provincia optgroup[label!='+jQuery('#regione').val()+']').remove();
	} else {
		jQuery('#provincia optgroup').remove();
	}
	jQuery('#regione').change(function(){
		jQuery('#provincia').html(_global_provincia);
		if (jQuery(this).val()!='') {
			jQuery('#provincia optgroup[label!='+jQuery(this).val()+']').remove();
		} else {
			jQuery('#provincia optgroup').remove();
		}
	});
}

/*************** Cruscotto viaggi ***********************/

function getCruscottoViaggi(){
	jQuery.get('/getCruscotto.php',
  		function(html){
  			jQuery('#trovaViaggio').append(html);
    		jQuery("#p_PERIOD_FROM").datepicker({ dateFormat: 'dd/mm/yy',
    			dayNamesMin: ['Do', 'Lu', 'Ma', 'Me', 'Gi', 'Ve', 'Sa'], 
    			monthNames: ['Gennaio','Febbraio','marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],
    			firstDay: 1,
    			nextText: 'Succ',
    			prevText: 'Prec',
    			minDate: 0
    		});
    		jQuery('#p_PERIOD_FROM').datepicker('option', jQuery.extend({
   				onSelect: function(dateText, inst) {
   					jQuery('#p_PERIOD_FROM').datepicker( 'hide' , 100 );
   				}
			}
    		));
    		jQuery("#p_PERIOD_FROM").unbind("click");
    		jQuery("#p_PERIOD_FROM").parent().find('a').hide();
    		
    		jQuery("#p_PERIOD_TO").datepicker({ dateFormat: 'dd/mm/yy',
    			dayNamesMin: ['Do', 'Lu', 'Ma', 'Me', 'Gi', 'Ve', 'Sa'], 
    			monthNames: ['Gennaio','Febbraio','marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],
    			firstDay: 1,
    			nextText: 'Succ',
    			prevText: 'Prec',
    			minDate: 0
    		});
    		jQuery('#p_PERIOD_TO').datepicker('option', jQuery.extend({
   				onSelect: function(dateText, inst) {
   					jQuery('#p_PERIOD_TO').datepicker( 'hide' , 100 );
   				}
			}
    		));
    		jQuery("#ui-datepicker-div").hide();
    		jQuery("#p_PERIOD_TO").unbind("click");
    		jQuery("#p_PERIOD_TO").parent().find('a').hide();
    		jQuery('#trovaViaggio a img').hide().parent().addClass('ui-state-default ui-corner-all ui-textbutton').append('<span class="cerca_btn">CERCA</span>'); // jQuery('#trovaViaggio img[src=http://81.29.193.77/robintur/leisure/appl/small_search//image/cerca.gif]')
  		}
	);
}
function showCalendar(){
}
function openSearchPage(){
    
    var channelList     = "5";  // ATTENZIONE: bisogna mettere il canale WEB
    var nation          = jQuery('#p_IDNATI').val();
    var zone            = jQuery('#p_IDZONE').val();
    var destination     = jQuery('#p_IDDEST').val();
    var period_from     = jQuery('#p_PERIOD_FROM').val();
    var period_to       = jQuery('#p_PERIOD_TO').val();
    
    var idx_sel_nati    = jQuery('#p_IDNATI').val();
    var nation_descr      = jQuery('#p_IDNATI option:selected').text();
    
    var idx_sel_zone    = jQuery('#p_IDZONE').val();
    var zone_descr        = jQuery('#p_IDZONE option:selected').text();
    
    var idx_sel_dest    = jQuery('#p_IDDEST').val();
    var dest_descr        = jQuery('#p_IDDEST option:selected').text();
    
    
    a_period_from = period_from.split("/");
    a_period_to   = period_to.split("/");
    
    format_period_from = String(a_period_from[2])+String(a_period_from[1])+String(a_period_from[0]);
    format_period_to   = String(a_period_to[2])+String(a_period_to[1])+String(a_period_to[0]);
    
        
    if(format_period_to < format_period_from){
    	alert("La data di fine periodo  antecedente a quella di inizio.");
    	
    } else {
	    
	    address = "/index.php?option=com_wrapper&view=wrapper&Itemid=103";
	    address += "&channelList="+channelList;
	    address += "&nation="+nation;
	    address += "&zone="+zone;
	    address += "&destination="+destination;
	    address += "&period_from="+period_from;
	    address += "&period_to="+period_to;
	    address += "&nation_descr="+nation_descr;
	    address += "&zone_descr="+zone_descr;
	    address += "&dest_descr="+dest_descr;
	    
	    //location.target = "_parent";
	    window.location = address;
	}
}

function natiChange(sender) {
    // Pulisco il combo delle zone
    jQuery('#p_IDZONE').empty().append(jQuery('<option></option').val('').html('Qualsiasi'));
    getStr = 'idnati='+sender.value;
    jQuery.post("/getExtXML.php", 
		{ url: "http://81.29.193.77/robintur/leisure/appl/small_search/ajax/ntb_get_only_zone.php?"+getStr, 
			mime: "Content-type:  application/text; charset=iso-8859-1"}, 
  		function(data){
  			eval(data); // Crea la variabile javascript "outcome_text" che contiene la dichiarazione dell'array
            eval(outcome_text);  
            jQuery(aZone).each(function(){
    			jQuery('#p_IDZONE').append(jQuery('<option></option').val(this[0]).html(this[1]));
   			});
  		});    
}

function zoneChange(sender) {
    jQuery('#p_IDDEST').empty().append(jQuery('<option></option').val('').html('Qualsiasi'));
    getStr = 'idzone='+sender.value;
    jQuery.post("/getExtXML.php", 
		{ url: "http://81.29.193.77/robintur/leisure/appl/small_search/ajax/ntb_get_only_dest.php?"+getStr, 
			mime: "Content-type:  application/text; charset=iso-8859-1"}, 
  		function(data){
  			eval(data); // Crea la variabile javascript "outcome_text" che contiene la dichiarazione dell'array
            eval(outcome_text);  
            jQuery(aDest).each(function(){
    			jQuery('#p_IDDEST').append(jQuery('<option></option').val(this[0]).html(this[1]));
   			});
  		});     
}



function popolaOfferteViaggi(catalogo, priorita){
	// url: "http://online.robintur.it/intranet/appl/ntb/catalog/rss/offer_hp/offer_hp_web.xml" }, /getExtXML.php
	if(priorita===undefined){
		var priorita_get="&where=priorita&oggetto_value=S";
	}else{
		var priorita_get="&where=1&oggetto_value=1";
	}
	
	if(catalogo===undefined){
		var catalogo_get="";
	} else {
		var catalogo_get="&where2=catalogo&oggetto_value2="+catalogo;
	}
	jQuery.getJSON("/argonautaviaggi/index.php?option=com_joodb&task=componiselect&format=raw&joobase=1&oggetto=*&raggruppa=titolo&ordina=  partenza ASC , prezzo_da ASC"+priorita_get+catalogo_get,
		function(data){

			//var xml=jQuery(data,xml);
			//var xml=data;
			rowstyle="pari";
			rowid=1;
			var reftable='#propostexte table';
			var titoloVecchia = '';
			jQuery.each(data,function(i,el){
				if (rowid<35) {
					var title = el.titolo;
					var continente = el.continente;
					var description = el.titolo;
					//var descDOM2= jQuery(description);
					var prezzo_da =	'&euro; '+el.prezzo;
					var partenza = el.partenza.split('-').reverse().join('-');
					var nazione = el.nazione;
					var titolo = el.titolo;
					
					/*var link = '/argonautaviaggi/index.php?option=com_joodb&Itemid=49&view=article&joobase=1&id='+el.id*/
					var link = '/argonautaviaggi/index.php?continente='+continente+'&nazione='+nazione+'&titolo='+titolo+'&id=&option=com_joodb&Itemid=49';
					
					if (rowid==19) {
						jQuery('#propostexte').append('<a href="#" class="trigger ui-has-icon"><span class="ui-icon ui-icon-triangle-1-e ui-icon-inline"></span>Visualizza altre offerte...</a>');
						jQuery('#propostexte').append('<div class="toggle_container" style="display:none;"><table border="0" cellpadding="2" cellspacing="2" width="100%"><tbody style="text-align: left;"></tbody></table></div>');
						reftable='#propostexte .toggle_container table';
					}
					/*if(titoloVecchia == '' || titolo != titoloVecchia ){*/			
						jQuery('<tr class="row'+rowstyle+'" id="rowid_'+rowid+'"></tr>').appendTo(reftable);
						rowstyle= (rowstyle=="pari")?"dispari":"pari";
						jQuery('<td width="80"></td>').html('<b>'+nazione+'</b>').appendTo('#rowid_'+rowid);
						jQuery('<td></td>').html('<a href="'+link+'" title="vedi tutte le offerte di questo viaggio">'+title+'</a>').appendTo('#rowid_'+rowid);
						jQuery('<td width="65"></td>').html('<small>'+partenza+'</small>').appendTo('#rowid_'+rowid);
						jQuery('<td align="right" width="60"></td>').html('<small>'+prezzo_da+'</small>').appendTo('#rowid_'+rowid);
						
					/*}else{
						jQuery('<tr class="row'+rowstyle+'" id="rowid_'+rowid+'"></tr>').appendTo(reftable);
						rowstyle= (rowstyle=="pari")?"dispari":"pari";
						jQuery('<td width="80"></td>').html('').appendTo('#rowid_'+rowid);
						jQuery('<td></td>').html('').appendTo('#rowid_'+rowid);
						jQuery('<td width="65"></td>').html('<small>'+partenza+'</small>').appendTo('#rowid_'+rowid);
						jQuery('<td align="right" width="60"></td>').html('<small>'+prezzo_da+'</small>').appendTo('#rowid_'+rowid);
					}*/
				}
				titoloVecchia = el.titolo;
				rowid++;	
			});
			if (rowid>18) {
				//Switch the "Open" and "Close" state per click
				jQuery("#propostexte .trigger").toggle(function(){
					jQuery(this).addClass("active");
					jQuery(this).children('span').removeClass("ui-icon-triangle-1-e");
					jQuery(this).children('span').addClass("ui-icon-triangle-1-s");
				}, function () {
					jQuery(this).removeClass("active");
					jQuery(this).children('span').removeClass("ui-icon-triangle-1-s");
					jQuery(this).children('span').addClass("ui-icon-triangle-1-e");
				});

				//Slide up and down on click
				jQuery("#propostexte .trigger").click(function(){
					jQuery(this).next(".toggle_container").slideToggle("slow");
				});

			}
   		},'xml');
}

/*************************** prese dal loro sistema ***************************/
function fillCombo(aData,OptionList) {
    // L'array aData deve essere nella forma aData[i][0] = ID aData[i][1] = DESCR
    var aRow, aField;
    var i,lng;
    
    for (i=0; i<aData.length; i++) {
    	OptionList.append(jQuery('<option></option').val(aData[i][0]).html(aData[i][1]));
    }
}


/****** ARGONAUTA *****/
function updateContinenti(continente,myselect,firstoption,firstvalue) {
	if (continente===undefined) nazione=0;
	if (firstoption===undefined) firstoption=false;
	if (firstvalue===undefined) firstvalue=0;
	var oldval=jQuery(myselect).val();
	jQuery(myselect).empty();
	if (firstoption) {
		jQuery(myselect).append('<option value="'+firstvalue+'" selected="selected">'+firstoption+'</option>');
	}
	if(continente != ''){
		catalogo=jQuery('#catalogo').val();
		catalogo = escape(catalogo);

		if (catalogo!='') 
			cattext='&catalogo='+catalogo;
		else 
			cattext='';
			
		sezione_soci=jQuery('#sezione_soci').val();	
		
		if (sezione_soci!=''){
			sezione_soci = escape(sezione_soci);
			sezione_sociext='&sezione_soci='+sezione_soci;
		}else{ 
			sezione_sociext='';
		}
		
		jQuery.getJSON("/argonautaviaggi/index.php?option=com_joodb&task=componiselect&format=raw&joobase=1&where=sezione_soci&oggetto=continente&oggetto_value="+sezione_soci+cattext+"&ordina=continente ASC",
			function(data){
				jQuery.each(data,function(i,el){
					jQuery(myselect).append('<option value="'+el+'" >'+el+'</option>');
				});
				if (oldval)
					jQuery(myselect+' option[value='+oldval+']').attr('selected','selected');
			}
		);
	}
}


function updateNazioni(continente,myselect,firstoption,firstvalue) {
	if (continente===undefined) nazione=0;
	if (firstoption===undefined) firstoption=false;
	if (firstvalue===undefined) firstvalue=0;
	
	var oldval=jQuery(myselect).val();
	jQuery(myselect).empty();
	if (firstoption) {
		jQuery(myselect).append('<option value="'+firstvalue+'" selected="selected">'+firstoption+'</option>');
	}
	if(continente != ''){
		catalogo=jQuery('#catalogo').val();
		catalogo = escape(catalogo);
		if (catalogo!='') 
			cattext='&catalogo='+catalogo;
		else 
			cattext='';
			
		sezione_soci=jQuery('#sezione_soci').val();	
		
		if (sezione_soci!='' && sezione_soci ){
			sezione_soci = escape(sezione_soci);
			sezione_sociext='&sezione_soci='+sezione_soci;
		}else{
			sezione_sociext='';
		}
		
		jQuery.getJSON("/argonautaviaggi/index.php?option=com_joodb&task=componiselect&format=raw&joobase=1&where=continente&oggetto=nazione&oggetto_value="+continente+cattext+sezione_sociext+"&ordina= nazione ASC",
			function(data){
				jQuery.each(data,function(i,el){
					jQuery(myselect).append('<option value="'+el+'" >'+el+'</option>');
				});
				if (oldval)
					jQuery(myselect+' option[value='+oldval+']').attr('selected','selected');
			}
		);
	}
}
/*aggiornare anche altri con campo sezionesoci*/
function updateDestinazioni(nazione,myselect,firstoption,firstvalue) {
	if (nazione===undefined) titolo=0;
	
	nazione = escape(nazione);
	
	if (firstoption===undefined) firstoption=false;
	if (firstvalue===undefined) firstvalue=0;
	var oldval=jQuery(myselect).val();
	jQuery(myselect).empty();
	if (firstoption) {
		jQuery(myselect).append('<option value="'+firstvalue+'" selected="selected">'+firstoption+'</option>');
	}
	if(nazione != ''){
		catalogo=jQuery('#catalogo').val();
		catalogo = escape(catalogo);
		if (catalogo!='') 
			cattext='&catalogo='+catalogo;
		else 
			cattext='';
			
		sezione_soci=jQuery('#sezione_soci').val();	
		
		if (sezione_soci!='' && sezione_soci ){
			sezione_soci = escape(sezione_soci);
			sezione_sociext='&sezione_soci='+sezione_soci;
		}else{
			sezione_sociext='';
		}

		jQuery.getJSON("/argonautaviaggi/index.php?option=com_joodb&task=componiselect&format=raw&joobase=1&where=nazione&oggetto=titolo&oggetto_value="+nazione+cattext+sezione_sociext+"&ordina= titolo ASC",
			function(data){
				jQuery.each(data,function(i,el){
					jQuery(myselect).append('<option value="'+el+'" >'+el+'</option>');
				});
				if (oldval)
					jQuery(myselect+' option[value='+oldval+']').attr('selected','selected');
			}
		);
	}
}
function scrivoMese(mese){
		switch (mese){
			case '01':
				mesep="gen";
			break;
			case '02':
				mesep="feb";
			break;
			case '03':
				mesep="mar";
			break;
			case '04':
				mesep="apr";
			break;
			case '05':
				mesep="mag";
			break;
			case '06':
				mesep="giu";
			break;
			case '07':
				mesep="lug";
			break;
			case '08':
				mesep="ago";
			break;
			case '09':
				mesep="set";
			break;
			case '10':
				mesep="ott";
			break;
			case '11':
				mesep="nov";
			break;
			case '12':
				mesep="dic";
			break;
			default:
				mesep="gen";
		}
		return mesep;

}

function updateDate(titolo,myselect,firstoption,firstvalue) {
	
	if (titolo===undefined) titolo=0;
	if (firstoption===undefined) firstoption=false;
	if (firstvalue===undefined) firstvalue=0;
	
	titolo=escape(titolo);
	
	var oldval=jQuery(myselect).val();
	jQuery(myselect).empty();
	if (firstoption) {
		jQuery(myselect).append('<option value="'+firstvalue+'" selected="selected">'+firstoption+'</option>');
	}
	if(titolo != ''){
		catalogo=jQuery('#catalogo').val();
		catalogo = escape(catalogo);
		if (catalogo!='') 
			cattext='&catalogo='+catalogo;
		else 
			cattext='';
			
		sezione_soci=jQuery('#sezione_soci').val();
		
		if (sezione_soci!='' && sezione_soci ){
			sezione_soci = escape(sezione_soci);
			sezione_sociext='&sezione_soci='+sezione_soci;
		}else{
			sezione_sociext='';
		}
		jQuery.getJSON("/argonautaviaggi/index.php?option=com_joodb&task=componiselect&format=raw&joobase=1&where=titolo&oggetto=id&oggetto1=partenza&oggetto2=rientro&ordina=partenza&ordina1=rientro&oggetto_value="+titolo+sezione_sociext+cattext,
		function(data){
				jQuery.each(data,function(i,el){
					var partenza = el.partenza.split('-');
					var annop = partenza[0];
					var mesep = scrivoMese(partenza[1]);
					var rientro = el.rientro.split('-');	
					var annor = rientro[0];	
					var meser = scrivoMese(rientro[1]);				
					partenza= partenza[2]+' '+mesep+' '+annop;
					rientro= rientro[2]+' '+meser+' '+annor;

					jQuery(myselect).append('<option value="'+el.id+'" >dal '+partenza+' al '+rientro+'</option>');
				});
				if (oldval)
					jQuery(myselect+' option[value='+oldval+']').attr('selected','selected');
			}
		);
	}
}
