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=113";
	    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(){
	// url: "http://online.robintur.it/intranet/appl/ntb/catalog/rss/offer_hp/offer_hp_web.xml" }, /getExtXML.php
	jQuery.post("/getExtXML.php", 
		{ url: "http://online.robintur.it/intranet/appl/ntb/catalog/rss/offer_hp/offer_hp_web.xml", 
			mime: "Content-type: application/xml; charset=iso-8859-1" }, 
  		function(data){
			//var xml=jQuery(data,xml);
			var xml=data;
			rowstyle="pari";
			rowid=1;
			var reftable='#propostexte table';
			jQuery(xml).find('item').each(function(){
				var title = jQuery(this).find('title').text().split(",")[1];
				var link = jQuery(this).find('link').text().replace("http://www.robintur.it/vacanze.asp?", "/index.php?option=com_wrapper&view=wrapper&Itemid=113&");
				var image = jQuery(this).find('image').text();
				var description = jQuery(this).find('description').text();
				//var descDOM2= jQuery(description);
				var prezzo_da =	jQuery(this).find('prezzo_da').text().split(",")[0]+' &euro;';
				
				var partenza = jQuery(this).find('partenza').text();
				var nazione = jQuery(this).find('nazione').text();
				if (rowid==9) {
					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';
				}			
				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+'</a>').appendTo('#rowid_'+rowid);
				jQuery('<td width="65"></td>').html(partenza).appendTo('#rowid_'+rowid);
				jQuery('<td align="right" width="60"></td>').html(prezzo_da).appendTo('#rowid_'+rowid);
				rowid++;	
			});
			if (rowid>8) {
				//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');
}

function ajaxModacajoomForm() {
	jQuery('.modacajoomForm').ajaxSubmit({
		success: function(res){
			jQuery('.modacajoomForm').replaceWith('<b>Grazie per esserti registrato alla nostra newsletter</b>');
		}
	});
}

/*************************** 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]));
    }
}
