/**
 * Switcha il generatore di captcha sulla versione testo
 */
function recaptchaText() {
	if(typeof(Recaptcha) != 'undefined') {
		Recaptcha.switch_type('image');
	}
}

/**
 * Switcha il generatore di captcha sulla versione audio
 */
function recaptchaAudio() {
	if(typeof(Recaptcha) != 'undefined') {
		Recaptcha.switch_type('audio');
	}
}
/**
 * Nasconde l'helptip quando il puntatore del mouse non si trova pi� sul punto interrogativo.
 */
function hidehelptip() {
	$('#helptip').hide('fast');
}
/**
 * Sostituisce l'html contenuto nell'elemento con classe hreplace con l'immagine fornita nei metadata messi come classe.
 */
function hReplace() {
	$('.hreplace').each(function(){
		var data = $.metadata.get(this);
		$(this).css({'background-image':'url(\''+data.imgurl+'\')', 'width':data.width+'px', 'height':data.height+'px'});
		$(this).html('');
	});
}
/**
 * Funzione richiamata all'hover dei punti interrogativi.
 * Il messaggio da mostrare viene reperito tramite l'attributo rel impostato
 * nel link tramite smarty.
 */
function showhelptip(obj) {
	log('showhelptip');
	var html = $('span', $(obj)).html();
	if(typeof(html) != 'undefined') {
		if($('#helptip').size() == 0) {
			$div = $('<div></div>').appendTo($('body'));
			$div.addClass('helptip_box');
			$div.addClass('notice');
			$div.attr({'id':'helptip'});
		} else {
			$div = $('#helptip');
		}

		$div.css({'top':$(obj).offset().top+'px',
							'left':($(obj).offset().left+20)+'px'
						 });
		$div.show('fast');
		$div.html(html);
	}
}
/**
 * Passato l'evento del keyup la funzione verifica se il valore fornito è tra quelli
 * accettati
 *
 * @param event e
 * @param string allowed_chars contiene una regex sulla quale viene confrontato il carattere
 */
function restricted(e, allowed_chars) {
	log('restricted(e, allowed_chars)');
	log('e:'+e);
	key = (e.charCode) ? e.charCode : e.which;

	log("key_code:"+key);

	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) return true;

	keychar = String.fromCharCode(key);
	log("keychar:"+keychar);

	re = new RegExp(allowed_chars+'{1}');

	if(re.test(keychar)) {
		if(debug) log('OK');
		return true;
	}

	if(debug) log('KO');
	return false;
}
/**
 * Funzione che fa da wrapper al componente ui.dialog.js (sostituisce il confirm)
 */
function modal_confirm(title, question, okfunct, cancelfunct) {
	if(typeof(cancelfunct) == 'undefined') {
		cancelfunct = function() {
			$(this).dialog('close').remove();
		}
	}

	$('<div>'+question+'</div>').dialog({
		title: title,
		modal : true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Ok : okfunct,
			Cancel : cancelfunct
		}
	});
}

/**
 * Funzione che fa da wrapper al componente ui.dialog.js (sostituisce l'alert)
 */
function modal_dialog(title, contenuto) {
	$('<div>'+contenuto+'<br /><br /></div>').dialog({
		title: title,
		modal : true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Ok': function() {
				$(this).dialog('close').remove();
			}
		}
	});
}

/**
 * Recupera la chiave di traduzione corrispondente a modulo.chiave forniti.
 */
function getLang(module, key) {
	if(typeof(Lang[module]) == 'undefined') {
		str = 'Lang Module '+module+' not found ('+module+"."+key+')';
		if(typeof(console) != 'undefined') {
			error(str);
		}
		return module+"."+key;
	} else {
		if(typeof(Lang[module][key]) == 'undefined') {
			str = 'Key '+key+' not found in module '+module;
			if(typeof(console) != 'undefined') {
				error(str);
			}
			return module+"."+key;
		} else {
			return Lang[module][key];
		}
	}
}
/**
 * Wrapper per firebug, viene mostrato un errore in rosso in console
 */
function error(str) {
 if(typeof(console) != 'undefined') {
	 console.error(str);
 }
}
/**
 * Wrapper per firebug, viene usato per i log, viene attivato solo se la variabile
 * globale "debug" è settata a true
 */
function log(str) {
 if(debug) {
	 if(typeof(console) != 'undefined') {
		 console.info(str);
	 }
 }
}
/**
 * Sistema dopo il caricamento l'attributo tabindex per i campi input, saltando i link di help.
 */
function tabindexinit() {
	$elementi = $(':input');
	var i = 0
	$elementi.each(function() {
		$(this).attr({'tabindex':i});
		i = i+1;
	});
	i = $elementi.size()
	$elementi = $('a');
	$elementi.each(function() {
		$(this).attr({'tabindex':i});
		i++;
	});
}

function preloadImages() {
	k = 0;
	$('.rollover').each(function() {
		i = new Image();
		i.src = $(this).attr('rel');
		img_array[k] = i;
		k++;
	});
}
function rolloverimg(obj) {
	$link_over = $(obj);
	new_img_url = $link_over.attr('rel');
	$img_obj = $('img', $link_over);
	swap_img = $img_obj.attr('src');
	$img_obj.attr({'src':new_img_url});
}

function rolloutimg(obj) {
	// questo if evita il baco che si verifica nel caso in cui
	// al caricamento della pagina ci si trovi già su un area sensibile
	if(swap_img != "") {
		$link_over = $(obj);
		$img_obj = $('img', $link_over);
		$img_obj.attr({'src':swap_img});
		swap_img = "";
	}
}

