/**
 * 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 = "";
	}
}


function number_format (number, decimals, dec_point, thousands_sep) {
		// Formats a number with grouped thousands
		//
		// version: 906.1806
		// discuss at: http://phpjs.org/functions/number_format
		// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
		// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +     bugfix by: Michael White (http://getsprink.com)
		// +     bugfix by: Benjamin Lupton
		// +     bugfix by: Allan Jensen (http://www.winternet.no)
		// +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
		// +     bugfix by: Howard Yeend
		// +    revised by: Luke Smith (http://lucassmith.name)
		// +     bugfix by: Diogo Resende
		// +     bugfix by: Rival
		// +     input by: Kheang Hok Chin (http://www.distantia.ca/)
		// +     improved by: davook
		// +     improved by: Brett Zamir (http://brett-zamir.me)
		// +     input by: Jay Klehr
		// +     improved by: Brett Zamir (http://brett-zamir.me)
		// +     input by: Amir Habibi (http://www.residence-mixte.com/)
		// +     bugfix by: Brett Zamir (http://brett-zamir.me)
		// *     example 1: number_format(1234.56);
		// *     returns 1: '1,235'
		// *     example 2: number_format(1234.56, 2, ',', ' ');
		// *     returns 2: '1 234,56'
		// *     example 3: number_format(1234.5678, 2, '.', '');
		// *     returns 3: '1234.57'
		// *     example 4: number_format(67, 2, ',', '.');
		// *     returns 4: '67,00'
		// *     example 5: number_format(1000);
		// *     returns 5: '1,000'
		// *     example 6: number_format(67.311, 2);
		// *     returns 6: '67.31'
		// *     example 7: number_format(1000.55, 1);
		// *     returns 7: '1,000.6'
		// *     example 8: number_format(67000, 5, ',', '.');
		// *     returns 8: '67.000,00000'
		// *     example 9: number_format(0.9, 0);
		// *     returns 9: '1'
		// *     example 10: number_format('1.20', 2);
		// *     returns 10: '1.20'
		// *     example 11: number_format('1.20', 4);
		// *     returns 11: '1.2000'
		// *     example 12: number_format('1.2000', 3);
		// *     returns 12: '1.200'
		var n = number, prec = decimals;

		var toFixedFix = function (n,prec) {
				var k = Math.pow(10,prec);
				return (Math.round(n*k)/k).toString();
		};

		n = !isFinite(+n) ? 0 : +n;
		prec = !isFinite(+prec) ? 0 : Math.abs(prec);
		var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
		var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

		var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

		var abs = toFixedFix(Math.abs(n), prec);
		var _, i;

		if (abs >= 1000) {
				_ = abs.split(/\D/);
				i = _[0].length % 3 || 3;

				_[0] = s.slice(0,i + (n < 0)) +
							_[0].slice(i).replace(/(\d{3})/g, sep+'$1');
				s = _.join(dec);
		} else {
				s = s.replace('.', dec);
		}

		var decPos = s.indexOf(dec);
		if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
				s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
		}
		else if (prec >= 1 && decPos === -1) {
				s += dec+new Array(prec).join(0)+'0';
		}
		return s;
}
