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

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;
}

function confirmLink(theLink, confirmMsg) {
	if (typeof(window.opera) != 'undefined') {
		return true;
	}
	
	var is_confirmed = confirm(confirmMsg);
	
	return is_confirmed;
}

function checkemail(email_addr) {
	testresults = false;
	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;
	}
	
	return(testresults);
}

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

function is_valid_username(str, min_chars, max_chars) {
	var filter=/^[a-zA-Z0-9]{1,}[\.|\_|-]?[a-zA-Z0-9]{1,}$/i
	if (filter.test(str) && str.length>=min_chars) {
		if (max_chars>=min_chars) {
			if (str.length<=max_chars) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			return true;
		}
	}
	else {
		return false;
	}
}

/*
	Effettua la chiamata AJAX utilizzando JQuery e JSON.
	Autore:	Cristian Zuddas
	Data:	2007-11-19
	
	Params:
		query_str			string			Parametri da passare allo script che gestisce le risposte AJAX
		is_admin			bool			TRUE significa che questa funzione e' stata richiamata dal CMS
		func_success		function		Funzione da richiamare in caso di successo; deve avere un parametro nel quale viene inserito il risultato JSON della richiesta. Esempio: function(oResponse) {...}
		func_error			function		Opzionale, funzione da richiamare in caso di errore. Se viene passato un valore il cui tipo non e' "function", ne viene presa una di default
		func_complete		function		Opzionale, funzione da richiamare una volta completata la richiesta, sia in caso di successo sia in caso di errore. Se viene passato un valore il cui tipo non e' "function", ne viene presa una di default
		external_url		string			URL della risposta, esterno a quello locale (aggiunto per Applejack 0.5)
	
	Note:
		Deve essere definita una variabile "ajax_main_script" (string) che contiene il nome/URL
		dello script gestore delle risposte.
*/
function doAjax(query_str, is_admin, func_success, func_error, func_complete, external_url) {
	if (typeof func_success=='function') {
		var url = ajax_main_script;
		if (is_admin) {
			url = '../'+url;
		}
		
		if (typeof external_url=='string' && external_url!=null && external_url.length>0) {
			url = external_url;
		}
		//window.location = url+'?'+query_str;
		if (typeof func_error!='function') {
			func_error = function() {
										alert("Si e' verificato un errore durante il caricamento dei dati. Si consiglia di riprovare in un secondo momento.");
									};
		}
		
		if (typeof query_str!='string')
			query_str = '';
		
		$.ajax({
			type: "POST",
			url: url,
			data: query_str,
			dataType: "json",
			success: func_success,
			error: func_error,
			complete: func_complete
		});
	}
	else {alert('doAjax error: success function is not defined.');}
}

// CMS - visualizza gli "Aggiornamenti Applejack"
function admin__infobox(p) {
	if (isObject(getE('infobox_content'))) {
		getE('infobox_content').innerHTML = '<center><br><img src="img/loading.gif" title="Caricamento in corso" /><br><br></center>';
		
		if (getE('infobox_table').style.display=='none') {
			getE('infobox_table').style.display = '';
		}
		
		doAjax(
				'p='+p,
				false,
				function(oResponse) {
					if (oResponse.length>0) {
						if (oResponse.length==2) {
							var html = oResponse[1].date+'<br /><b>'+oResponse[1].title+'</b><br /><br /><p>'+oResponse[1].text+'</p>';
							
							if (parseInt(oResponse[1].btn)==1) {
								html += '<center><form><input type="button" class="infobox_btn" id="infobox_btn_send" value="Voglio essere contattato per maggiori informazioni" onclick="admin__infobox_email('+oResponse[1].id+');"></form></center>';
							}
							
							getE('infobox_content').innerHTML = html;
							
							
							// bottoni di paginazione
							html = '';
							if (parseInt(oResponse[0].p)>1) {
								html += '<a href="#" onclick="admin__infobox('+(parseInt(oResponse[0].p)-1)+'); return false;"><img src="icons/arrow_up_16.gif" border="0" align="absmiddle" title="Annuncio successivo"></a>';
							}
							
							if (parseInt(oResponse[0].p)<parseInt(oResponse[0].c)) {
								html += '<a href="#" onclick="admin__infobox('+(parseInt(oResponse[0].p)+1)+'); return false;"><img src="icons/arrow_down_16.gif" border="0" align="absmiddle" title="Annuncio precedente"></a>';
							}
							
							getE('infobox_buttons').innerHTML = html;
						}
						else {
							getE('infobox_table').style.display = 'none';
						}
					}
					else {
						getE('infobox_table').style.display = 'none';
					}
				}
				, null, null, infobox_ajax_url
		   );
	}
}

// CMS - invia l'email allo staff di Applejack quando un cliente richiede informazioni su un "Aggiornamento Applejack"
function admin__infobox_email(id) {
	getE('infobox_btn_send').disabled = true;
	getE('infobox_btn_send').value = 'Invio in corso... attendere.';
	
	if (parseInt(id)>0) {
		doAjax(
			'a_ajax=1&id='+id,
			true,
			function(oResponse) {
				if (oResponse==true) {
					alert("La comunicazione e' stata inviata in modo corretto, verrai ricontattato appena possibile da un nostro operatore. Lo staff di Applejack.it ti ringrazia.");
				}
				else {
					alert("Attenzione: ci sono stati dei problemi durante l'invio della tua richiesta; si consiglia di riprovare in un secondo momento.");
				}
			}
	   );
	}
	
	getE('infobox_btn_send').disabled = false;
	getE('infobox_btn_send').value = 'Voglio essere contattato per maggiori informazioni';
	return false;
}

function initSpecialLinks() {
	var i;
	var lk;
	if (document.getElementsByTagName) {
		lk = document.getElementsByTagName('a');
		if (lk) {
			for (i=0; i < lk.length; i++) {
				if (lk[i].rel.indexOf('external') != -1) {
					lk[i].target = '_blank';
					lk[i].title = lang_00002;
				}
			}
		}
	}
}

// caricamento di Google Maps
function location_map_load() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("goog_maps"));
		map.setMapType(G_HYBRID_MAP);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		map.setCenter(new GLatLng(39.213, 9.132724), 17);		// centramento della mappa un po' piu' un alto rispetto al marker, per evitare sovraposizione dei tasti sull'InfoWindow
		
		
		var marker = new GMarker(new GLatLng(39.211968, 9.132724));
		GEvent.addListener(marker, "click",	function() {
												this.openInfoWindowHtml(lang_00003);
											}
		);
		map.addOverlay(marker);
		marker.openInfoWindowHtml(lang_00003);
		marker = null;
	}
}

// motore di ricerca avanzato - selezione di tutte le sotto-tipologie di una particolare tipologia di immobile
function search_select_all_subtypes(id_tipo) {
	if (id_tipo>0) {
		var form = getE('f_search');
		for (var i=0; i<form.length; i++) {
			if (form.elements[i].type.toLowerCase()=='checkbox' && form.elements[i].id.indexOf('search_type_'+id_tipo+'_')==0) {
				form.elements[i].checked = true;
			}
		}

	}
	
	return false;
}

// mostra o nasconde un oggetto dom. "class_show" e' il nome della classe da applicare all'oggetto per visualizzarlo
function hide_dom(id, class_show) {
	if (isObject(getE(id))) {
		if (getE(id).className==class_show) {
			getE(id).className = 'hidden';
		}
		else {
			getE(id).className = class_show;
		}
	}
	
	return false;
}

// mostra o nasconde il box "richiedi informazioni" della scheda immobili
function structure_informer_box() {
	if (getE('informer_div').className=='informer_hide') {
		getE('informer_div').className = 'informer';
	}
	else {
		getE('informer_div').className = 'informer_hide';
	}
	
	return false;
}

// box "richiedi informazioni" della scheda immobili: cancella il contenuto dei campi data all'onFocus
function structure_informer_date_content(id, is_onfocus) {
	if (isObject(getE(id))) {
		if (is_onfocus && getE(id).value==lang_00006) {		// onFocus: cancella il contenuto
			getE(id).value = '';
		}
		else if (!is_onfocus && getE(id).value=='') {		// onFocus: cancella il contenuto
			getE(id).value = lang_00006;
		}
	}
	
	return false;
}

// scheda dell'immobile - caricamento di Google Maps
function structure_map_load(lat, lng, fullarea_marker) {
	if (GBrowserIsCompatible()) {
		var zoom = 18;
		var markerOptions = {};
		if (fullarea_marker) {
			zoom = 13;
			
			icon = new GIcon(G_DEFAULT_ICON);
			icon.image = 'images/googmaps_marker_area.png';
			icon.shadow = null;
			icon.iconAnchor = new GPoint(30, 30);
			icon.iconSize = new GSize(66, 66);
			markerOptions = {icon: icon};
			icon = null;
		}
		
		var map = new GMap2(document.getElementById("goog_maps"));
		
		/*if (!fullarea_marker) {
			map.setMapType(G_SATELLITE_MAP);
		}*/
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		map.setCenter(new GLatLng(lat, lng), zoom);
		
		var marker = new GMarker(new GLatLng(lat, lng), markerOptions);
		GEvent.addListener(marker, "click",	function() {
												map.setCenter(new GLatLng(lat, lng), zoom);
											}
		);
		map.addOverlay(marker);
		marker = null;
	}
}

// scheda dell'immobile - submit del modulo di richiesta informazioni
function structure_info_submit(id_tipo, id_prodotto) {
	if (id_tipo>0 && id_prodotto>0) {
		if (getE('inform_name').value.length>0 && checkemail(getE('inform_email').value) && getE('inform_consenso').checked) {
			var inform_data1 = '';
			var inform_data2 = '';
			if (isObject(getE('inform_data1'))) {
				inform_data1 = encodeURIComponent(getE('inform_data1').value)
			}
			
			if (isObject(getE('inform_data2'))) {
				inform_data2 = encodeURIComponent(getE('inform_data2').value)
			}
			
			
			doAjax(
					'a_ajax=2&id_tipo='+id_tipo+'&id_prodotto='+id_prodotto+'&name='+encodeURIComponent(getE('inform_name').value)+'&email='+encodeURIComponent(getE('inform_email').value)+'&data1='+inform_data1+'&data2='+inform_data2+'&text='+encodeURIComponent(getE('inform_text').value)+'&consenso=1',
					false,
					function(oResponse) {
						if (oResponse==0) {
							alert(lang_00009);
							getE('inform_name').value = '';
							getE('inform_email').value = '';
							getE('inform_data1').value = lang_00006;
							getE('inform_data2').value = lang_00006;
							getE('inform_text').value = '';
							getE('inform_consenso').checked = false;
						}
						else {
							alert(lang_00008);		// problemi nell'invio della richiesta
						}
						
						return false;
					},
					function() {
						alert(lang_00008);
					}
			   );
		}
		else {
			alert(lang_00007);
		}
	}
	
	return false;
}

// memorizzazione dell'immobile negli appunti
function structure_save(id_tipo, id_prodotto) {
	if (id_tipo>0 && id_prodotto>0) {
		//window.location = 'ajax.php?a_ajax=3&id_tipo='+encodeURIComponent(id_tipo)+'&id_prodotto='+encodeURIComponent(id_prodotto);
		doAjax(
			'a_ajax=3&id_tipo='+encodeURIComponent(id_tipo)+'&id_prodotto='+encodeURIComponent(id_prodotto),
			false,
			function(oResponse) {
				if (oResponse>=0) {
					getE('saved_structures_lnk').innerHTML = oResponse;
				}
				
				alert(lang_00010);
			},
			null, null
		);
	}
	
	return false;
}

// cancellazione di una struttura dagli appunti
function saved_structures_delete(id_tipo, id_prodotto) {
	if (id_tipo>0 && id_prodotto>0 && confirm(lang_00011)) {
		doAjax(
			'a_ajax=4&id_tipo='+encodeURIComponent(id_tipo)+'&id_prodotto='+encodeURIComponent(id_prodotto),
			false,
			function(oResponse) {
				if (oResponse>=0) {
					getE('saved_structures_lnk').innerHTML = oResponse;
					getE('immobili_list').removeChild(getE('structure_data_'+id_tipo+'_'+id_prodotto));
					
					if (oResponse==0) {
						getE('generic_msg').innerHTML = lang_00013;
					}
				}
			},
			null, null
		);
	}
	else if (id_tipo==-1 && id_prodotto==-1 && confirm(lang_00012)) {
		doAjax(
			'a_ajax=4&all=1',
			false,
			function(oResponse) {
				getE('saved_structures_lnk').innerHTML = '0';
				getE('immobili_list').innerHTML = '';
				getE('generic_msg').innerHTML = lang_00013;
			},
			null, null
		);
	}
	
	return false;
}

// stampa della pagina
function print_page() {
	window.print();
	return false;
}

// scheda dell'immobile - invio per email
function structures_email_send() {
	if (parseInt(getE('inform_t').value)>0 && parseInt(getE('inform_page_id').value)>0) {
		if (checkemail(getE('inform_email').value)) {
			doAjax(
					'a_ajax=5&t='+getE('inform_t').value+'&page_id='+getE('inform_page_id').value+'&inform_name='+encodeURIComponent(getE('inform_name').value)+'&inform_email='+encodeURIComponent(getE('inform_email').value),
					false,
					function(oResponse) {
						if (oResponse==0) {
							alert(lang_00014);
							getE('inform_name').value = '';
							getE('inform_email').value = '';
						}
						else {
							alert(lang_00008);		// problemi nell'invio della richiesta
						}
						
						return false;
					},
					function() {
						alert(lang_00008);
					}
			   );
		}
		else {
			alert(lang_00007);
		}
	}
	
	return false;
}

// Proponi un immobile - mostra o nasconde i radiobuttons per la selezione della transazione in base al tipo di immobile
function suggest_structure_transaction(types_data) {
	if (parseInt(getE('ct_tipologia').value)>0) {
		if (types_data[getE('ct_tipologia').value]==0) {
			getE('div_transazione').innerHTML =	'<input id="ct_transazione_1" name="ct_transazione" value="1" type="radio" /> '+
												'<label for="ct_transazione_1"><span style="margin-left: 5px;">'+lang_transaction_00001+'</span></label><br /> '+
												
												'<input id="ct_transazione_2" name="ct_transazione" value="2" type="radio" /> '+
												'<label for="ct_transazione_2"><span style="margin-left: 5px;">'+lang_transaction_00002+'</span></label> ';
		}
		else {
			getE('div_transazione').innerHTML = lang_transaction_00003;
		}
	}
}

// scheda dell'immobile - apertura delle tab
function structure_open_tab(tab, lat, lng, fullarea_marker) {
	tab = tab.toLowerCase();
	if (tab.length==1 && isObject(getE('tab_content_'+tab+'_div'))) {
		getE('tab_selected_div').className = 'tab_selected_'+tab;
		
		if (lat!==null && lng!==null) {
			structure_map_load(lat, lng, fullarea_marker);
		}
	}
	
	return false;
}

// apertura delle tab delle tariffe - scheda immobili
function structure_seasontab(tab) {
	if (isObject(getE('div_seasontab_content_'+tab))) {
		getE('div_seasontab_selected').className = 'seasontab_selected_'+tab;
	}
	
	return false;
}