var ajax_response_file = "do_ajax.php?";

function getE(id) {
	return document.getElementById(id);
}

/*
	Inserisce dinamicamente un filmato Flash.
	Per scavalcare il blocco di IE 6, che altrimenti ne richiede l'attivazione manuale.
*/
function print_flash_movie(nomefile, width, height, version) {
	var str;
	
	str = ('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version + '" WIDTH="' + width + '" HEIGHT="' + height + '">');
	str = str + ('<PARAM NAME=movie VALUE="' + nomefile + '">');
	str = str + ('<PARAM NAME=quality VALUE=high>');
	str = str + ('<embed src="' + nomefile + '" quality=high ');
	str = str + ('swLiveConnect="FALSE" WIDTH="' + width + '" HEIGHT="' + height + '" border="0" TYPE="application/x-shockwave-flash"');
	str = str + ('PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></embed></OBJECT>');
	
	document.write(str);
}

function doAjaxRequestTxt(url, type, presel_city, presel_loc) {
	//window.location = url;
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("get", url, true);
	oXmlHttp.onreadystatechange = function() {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				if (type=="get_cities") {
					var l = 2;
					var response = oXmlHttp.responseText.split("\n");
					var found_something = false;
					getE("id_regione_provincia_citta").length = 0;
					if (response.length>0) {
						for(var i=0; i<response.length; i++) {
							var this_elem = response[i].split("||");
							if (this_elem[0].length>0 && this_elem[1].length>0) {
								getE("id_regione_provincia_citta").options[getE("id_regione_provincia_citta").length] = new Option(this_elem[1].unescapeHTML().replace('&quot;', '"'), this_elem[0]);
								if (presel_city==this_elem[0]) {getE("id_regione_provincia_citta").selectedIndex = getE("id_regione_provincia_citta").length-1;}
								l = this_elem[2];
								found_something = true;
							}
						}
					}
					
					if (!found_something) {		// se non ha trovato nessuna citta'
						getE("id_regione_provincia_citta").disabled = true;
						getE("id_regione_provincia_citta_localita").disabled = true;
					}
					else {
						getE("id_regione_provincia_citta").disabled = false;
						ajax_get_cities(1, l, presel_city, presel_loc);		// ricarica le localita' della prima citta' estratta
					}
				}
				else if (type=="get_locations") {
					var response = oXmlHttp.responseText.split("\n");
					var found_something = false;
					getE("id_regione_provincia_citta_localita").length = 0;
					if (response.length>0) {
						for(var i=0; i<response.length; i++) {
							var this_elem = response[i].split("||");
							if (this_elem[0].length>0 && this_elem[1].length>0) {
								getE("id_regione_provincia_citta_localita").options[getE("id_regione_provincia_citta_localita").length] = new Option(this_elem[1].unescapeHTML().replace('&quot;', '"'), this_elem[0]);
								if (presel_loc==this_elem[0]) {getE("id_regione_provincia_citta_localita").selectedIndex = getE("id_regione_provincia_citta_localita").length-1;}
								found_something = true;
							}
						}
					}
					
					if (!found_something) {		// se non ha trovato nessuan citta'
						getE("id_regione_provincia_citta_localita").disabled = true;
					}
					else {
						getE("id_regione_provincia_citta_localita").disabled = false;
					}
				}
				else if (type=="timed_adv") {		// banner temporizzati
					var response = oXmlHttp.responseText.split("\n");
					
					if (presel_city==3 || presel_city==5) {		// colonna di destra
						getE('advbanner_1').innerHTML = response;
					}
					else if (presel_city==2 || presel_city==4) {		// colonna di destra
						getE('advbanner_2').innerHTML = response;
					}
					else if (presel_city==9 || presel_city==6) {		// colonna di destra
						getE('advbanner_3').innerHTML = response;
					}
					else if (presel_city==10 || presel_city==7) {		// colonna di destra
						getE('advbanner_4').innerHTML = response;
					}
					else if (presel_city==11 || presel_city==8) {		// colonna di destra
						getE('advbanner_5').innerHTML = response;
					}
				}
			}
		}
	};
	
	oXmlHttp.send(null);
}

/*
	restituisce la lista di citta' collegate alla provincia passata (type=0), oppure la lista di
	localita' collegate alla citta' passata (type=1). "l" e' la lingua.
	
	"presel_city" e "presel_loc" sono gli ID della citta' e della localita' da pre-selezionare. se
	la funzione viene richiamata per popolare le citta', si devono passare entrambi i parametri per
	fare in modo che "doAjaxRequestTxt", dopo aver popolato le citta', richiami "ajax_get_cities"
	per popolare le localita' pre-selezionando quella giusta.
*/
function ajax_get_cities(type, l, presel_city, presel_loc) {
	if (isObject(getE('rooms_search_form'))) {
		var ajax_type = "get_cities";
		var ajax_obj = "id_regione_provincia";
		if (type==1) {
			ajax_type = "get_locations";
			ajax_obj = "id_regione_provincia_citta";
		}
		doAjaxRequestTxt(ajax_response_file+'a=102&id='+getE(ajax_obj).value+"&type="+type+"&o=1", ajax_type, presel_city, presel_loc);
	}
}

function isFunction(a) {
	return typeof a == 'function';
}

function isObject(a) {
	return (a && typeof a == 'object') || isFunction(a);
}

/*
Apertura di una pagina in una popup.
Utilizzo: <a href="my_file.est" onclick="mw=openWindowDimBars('my_file.est',370,200,0,mw); return false;" target="_blank">...</a>
*/
mw=null;
function openWindowDimBars(FileName,w,h,barre) {
	myWindow=window.open(FileName,'','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+barre+',resizable=0,top=10,left=10,width='+w+',height='+h);
	return myWindow;
}

var testresults;
function checkemail(email_addr) {
	var filter=/^[\'+\\./0-9A-Z^_\`a-z{|}~\-]+@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+){1,3}$/i;
	if (filter.test(email_addr)) {
		testresults = true;
	}
	else {
		testresults = false;
	}
	
	return(testresults);
}

// area riservata - prenotazioni negli appunti - seleziona/deseleziona tutte le prenotazioni negli appunti
function reserved_area__check_reservations(is_link) {
	if (is_link) {		// se si sta richiamando la funzione dall'onClick su un link, prima cambia il settaggio del checkbox e poi esegue i controlli
		getE('prodsel_all').checked = !getE('prodsel_all').checked;
	}
	
	for (i=0; i<getE('reserv_form').length; i++) {
		if (getE('reserv_form').elements[i].type.toLowerCase()=='checkbox' && getE('reserv_form').elements[i].id.indexOf('prenotazione_')==0) {
			getE('reserv_form').elements[i].checked = getE('prodsel_all').checked;
		}
	}
}

// area riservata - prenotazioni negli appunti - cancella tutte quelle selezionate
function reserver_area__delete_reservations() {
	if (confirm(transl_00008))	{return true;}
	else						{return false;}
}

// dettaglio camera - finestra "verifica la disponibilita'" - tasto "prenota nelle date selezionate"
function room__check__reserve_in_dates(s_id) {
	window.opener.location = "reservation.php?s_id="+s_id+"&search=1&a=1&data_arrivo_content="+getE('data_arrivo_content').value+"&data_partenza_content="+getE('data_partenza_content').value;
	window.opener.focus();
	window.close();
}

// inserimento feedback - nasconde i campi SELECT e mostra le stelline nel modulo. "max_id" e' il numero totale di campi di valutazione presenti
function feedback_show_stars(max_id) {
	if (max_id>0) {
		for (var i=1; i<=max_id; i++) {
			getE('stars_row_'+i).className = 'stars_0';
			getE('stars_select_'+i).className = 'stars_hide';
		}
	}
}

// inserimento feedback - evento onClick sulle stelline di valutazione: aggiorna il campo SELECT nascosto e blocca la selezione sul gruppo di stelline. "stars_group_id" e' l'ID progressivo dell'opzione di valutazione; "star_value" e' il valore numerico della singola stella cliccata
function feedback_stars_click(stars_group_id, star_value) {
	if (stars_group_id>0 && star_value>0) {
		getE('stars_row_'+stars_group_id).className = 'stars_'+star_value;
		getE('feedback_val_'+stars_group_id).selectedIndex = star_value;
	}
	
	return false;
}

// inserimento feedback - evento onSubmit del form
function feedback_onsubmit() {
	if ((!getE('feedback_sign_plus').checked && !getE('feedback_sign_minus').checked) || getE('feedback_text').value=="") {
		alert(transl_00011);
		return false;
	}
	
	return true;
}

// banner a rotazione in AJAX
function timed_adv(l) {
	if (l==1 || l==2 || l==3) {
		if (isObject(getE('advbanner_1'))) {
			var position = 5;
			if (l==2) { position = 3; }
			doAjaxRequestTxt(ajax_response_file+'a=103&id='+position, "timed_adv", position, null);
		}
		/*
		if (isObject(getE('advbanner_2'))) {
			var position = 4;
			if (l==2) { position = 2; }
			doAjaxRequestTxt(ajax_response_file+'a=103&id='+position, "timed_adv", position, null);
		}
		*/
		
		if (isObject(getE('advbanner_3'))) {
			var position = 9;
			if (l==2) { position = 6; }
			doAjaxRequestTxt(ajax_response_file+'a=103&id='+position, "timed_adv", position, null);
		}
		
		if (isObject(getE('advbanner_4'))) {
			var position = 10;
			if (l==2) { position = 7; }
			doAjaxRequestTxt(ajax_response_file+'a=103&id='+position, "timed_adv", position, null);
		}
		
		if (isObject(getE('advbanner_4'))) {
			var position = 11;
			if (l==2) { position = 8; }
			doAjaxRequestTxt(ajax_response_file+'a=103&id='+position, "timed_adv", position, null);
		}
	}
	
	setTimeout('timed_adv('+l+');', 30000);
}
