var ajax_main_script = "ajax.php";


/*
	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
	
	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) {
	if (typeof func_success=='function') {
		var url = ajax_main_script;
		if (is_admin) {
			url = '../'+url;
		}
		
		if (typeof func_error!='function') {
			func_error = function() {
										alert(lng00003);
									};
		}
		
		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.');}
}

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

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

/*
	paginazione della photogallery.
	il parametro "verso" e' un booleano che indica se si deve scorrere di una foto in avanti (TRUE)
	oppure indietro (FALSE) rispetto a quella attuale, che e' registrata nel DIV con id "photogallery_page".
*/
function photogallery(verso) {
	if (isObject(getE('tgallery__image'))) {
		var p = parseInt(getE('photogallery_page').innerHTML)+1;		// se "verso = true"
		if (!verso) {
			p = parseInt(getE('photogallery_page').innerHTML)-1;
		}
		//window.location = 'ajax.php?a_ajax=1&page_id='+encodeURIComponent(getE('insert_code').innerHTML)+'&p_scroll='+p;
		//alert('a_ajax=1&pagina='+encodeURIComponent(getE('insert_code').innerHTML)+'&p_scroll='+p);
		doAjax(
			'a_ajax=1&pagina='+encodeURIComponent(getE('insert_code').innerHTML)+'&p_scroll='+p,
			false,
			function(oResponse) {
					if (oResponse['act']!=undefined) {
					getE('photogallery_page').innerHTML = parseInt(oResponse['act'].p);		// aggiorna il valore della pagina attuale
					egal.skipTo(parseInt(oResponse['act'].p));
					
					// visualizza la foto attuale
					getE('tgallery__image').src = oResponse['act'].zoom;
					getE('tgallery__thumb2').src = oResponse['act'].thumb;
					getE('numero_foto').innerHTML = 'Foto '+(parseInt(oResponse['act'].p)+1)+' di '+oResponse['act'].pics_num;
					
					
					// didascalia
					if (oResponse['act'].txt!=null && oResponse['act'].txt.length>0) {
						getE('tgallery__capt').innerHTML = oResponse['act'].txt;
					}
					else {
						getE('tgallery__capt').innerHTML = '';
					}
					
					
					// thumb della foto precedente
					if (oResponse['prev']!=undefined) {
						getE('box_thumb1').innerHTML = '<a id="tgallery__link1" href="#" onclick="return photogallery(false);"><img id="tgallery__thumb1" src="'+oResponse['prev']+'" alt="miniatura" title="Fotografia precedente" /></a>';
					}
					else {
						getE('box_thumb1').innerHTML = '';
					}
					
					
					// thumb della foto successiva
					if (oResponse['next']!=undefined) {
						getE('box_thumb3').innerHTML = '<a id="tgallery__link3" href="#" onclick="return photogallery(true);"><img id="tgallery__link3" src="'+oResponse['next']+'" alt="miniatura" title="Fotografia successiva" /></a>';
					}
					else {
						getE('box_thumb3').innerHTML = '';
					}
				}
			},
			null, null
		);
	}
	
	return false;
}

/* inserisce un elemento flash */
function printVFlash(divid, divclass, pureHtml, nomefile, width, height, version, fallback, commonmarkup) {
	var xclose = (pureHtml)? '' : ' /';
	var isMSIE = /*@cc_on!@*/false;
	var divtag;
	var str;
	var commonstr;

	if (!version) {
		version = '8,0,0,0';
	}
	if (!fallback) {
		fallback = 'Please install Flash Player plugin.'
	}

	divtag = '<div';
	if (divid.length > 0) { divtag += ' id="' + divid + '"'; }
	if (divclass.length > 0) { divtag += ' class="' + divclass + '"'; }
	divtag += '>';

	commonstr = ('<param name="quality" value="high"' + xclose + '>');
	if (commonmarkup) {
		commonstr += commonmarkup;
	}

	if (isMSIE) {
		str = ('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version);
		str += ('" width="' + width + '" height="' + height + '">');
		str += ('<param name="movie" value="' + nomefile + '"' + xclose + '>');
		str += (commonstr + '<\/object>');
	} else {
		str = ('<object data="' + nomefile + '" width="' + width + '" height="' + height);
		str += ('" type="application/x-shockwave-flash">' + commonstr);
		str += ('<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer"' + xclose + '>');
		str += (fallback + '<\/object>');
	}

	document.write(divtag + str + '<\/div>');
}

// caricamento di Google Maps
function google_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.285, 9.124765), 13);		// 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.270448, 9.124765));
		GEvent.addListener(marker, "click",	function() {
														this.openInfoWindowHtml(lang_00001);
													}
		);
		map.addOverlay(marker);
		marker.openInfoWindowHtml(lang_00001);
		marker = null;
	}
}

function SetUrl(url) {		// CMS - usato per leggere l'URL dei files selezionati dal Gestore delle Risorse quando viene usato esternamente alla HTML Area
	if (isObject(getE('biz_editor_file'))) {
		url = url.substr(url.indexOf('biz_editor/'));
		getE('biz_editor_file').value = url;
	}
}