function trim(stringa){
	return jQuery.trim(stringa);
}

function jConfirm(text,c)
{
	Boxy.confirm(
		text,
		c,
		{title: ''}
	);
}

function jAlert(text)
{
	Boxy.alert(
		text, null, {title:''}
	);
}

function jPrompt(text, c)
{
	var txt = prompt(text);
	if( txt != '' )
		c(txt);
}

function jConfirmLink(text, link)
{
	jConfirm(text,
		function(){
				window.location.href=link;
		}
	);
}

function in_array( needle, haystack )
{
	if( !haystack )
		return false;

	for( var i in haystack )
		if( haystack[i] == needle )
			return true;

	return false;
}

function roundTo(decimalpositions)
{
    var i = this * Math.pow(10,decimalpositions);
    i = Math.round(i);
    return i / Math.pow(10,decimalpositions);
}
Number.prototype.roundTo = roundTo; 

function safeSubmit(id_submit_button, id_text_input)
{
	$('#'+id_submit_button)[0].disabled = true;
	if(trim($('#'+id_text_input)[0].value)=="" || $('#'+id_text_input)[0].modificato == false)
	{
		$('#'+id_submit_button)[0].disabled = false;
		return false;
	}
	return true;
}

function isset( variable )
{
	return( typeof( variable ) != 'undefined' );
}

function addJsFromUrl(url)
{
	scrptE = document.createElement("script");
	scrptE.setAttribute("type", "text/javascript");
	scrptE.setAttribute("src", url);
	document.getElementsByTagName("head")[0].appendChild(scrptE);
}

$(document).ready(function() {
	//Limita la grandezze delle textarea (non esiste in html standard)
	$('textarea[maxlength]').keyup(function(){  
		//get the limit from maxlength attribute  
		var limit = parseInt($(this).attr('maxlength'));  
		//get the current text inside the textarea  
		var text = $(this).val();  
		//count the number of characters in the text  
		var chars = text.length;
		//check if there are more characters then allowed  
		if(chars > limit){
			//and if there are use substr to get the text before the limit
			var new_text = text.substr(0, limit);
			//and change the current text with the new text  
			$(this).val(new_text);
		}  
	});
	//Aggiunge i tooltip dove serve
	$('.tooltip').each(function()
	{
		var tooltipStyle = 'cream';
		if($(this).attr('rel'))
			tooltipStyle = $(this).attr('rel');		
		$(this).qtip({
			content: $(this).attr('title'), // Use the tooltip attribute of the element for the content
			style: {
				border: {
					width: 5,
                    radius: 10
                },
                padding: 10, 
                textAlign: 'center',
                tip: true,
                name: tooltipStyle
            },
			position: {
				corner: {
					tooltip: 'topMiddle',
					target: 'bottomMiddle'
				},
				adjust: { 
					x: 0, 
					y: 10
				}
			},
			hide: { when: 'mouseout', fixed: true }
		});
		$(this).removeAttr('title');
		$(this).removeAttr('alt');
	});
});