function	autoUrl(name, dest)
{
	var loc;
	var id_list;

	id_list = document.getElementById(name);
	loc = id_list.options[id_list.selectedIndex].value;
	if (loc != 0)
		location.href = dest+loc;
	return ;
}

function	autoUrlNoList(name, dest)
{
	var loc;

	loc = document.getElementById(name).checked;
	location.href = dest + (loc == true ? 1 : 0);
	return ;
}

/*
** show or hide element e depending on condition show
*/
function toggle(e, show)
{
	e.style.display = show ? '' : 'none';
}

function toggleMultiple(tab)
{
    var len = tab.length;

    for (var i = 0; i < len; i++)
        if (tab[i].style)
            toggle(tab[i], tab[i].style.display == 'none');
}

/**
* Show dynamicaly an element by changing the sytle "display" property
* depending on the option selected in a select.
*
* @param string $select_id id of the select who controls the display
* @param string $elem_id prefix id of the elements controlled by the select
*   the real id must be : 'elem_id'+nb with nb the corresponding number in the
*   select (starting with 0).
*/
function showElemFromSelect(select_id, elem_id)
{
	var select = document.getElementById(select_id);
	for (var i = 0; i < select.length; ++i)
	{
	    var elem = document.getElementById(elem_id + select.options[i].value);
		if (elem != null)
			toggle(elem, i == select.selectedIndex);
	}
}

/**
* Get all div with specified name and for each one (by id), toggle their visibility
*/
function openCloseAllDiv(name, option)
{
	var tab = $('*[name='+name+']');
	for (var i = 0; i < tab.length; ++i)
		toggle(tab[i], option);
}

/**
* Toggle the value of the element id_button between text1 and text2
*/
function toggleElemValue(id_button, text1, text2)
{
	var obj = document.getElementById(id_button);
	if (obj)
		obj.value = ((!obj.value || obj.value == text2) ? text1 : text2);
}

function addBookmark(url, title)
{
	if (window.sidebar)
		return window.sidebar.addPanel(title, url, "");
	else if ( window.external )
		return window.external.AddFavorite( url, title);
	else if (window.opera && window.print)
		return true;
	return true;
}

function writeBookmarkLink(url, title, text, img)
{
	var insert = '';
	if (img)
		insert = writeBookmarkLinkObject(url, title, '<img src="' + img + '" alt="' + escape(text) + '" title="' + escape(text) + '" />') + '&nbsp';
	insert += writeBookmarkLinkObject(url, title, text);
	document.write(insert);
}

function writeBookmarkLinkObject(url, title, insert)
{
	if (window.sidebar || window.external)
		return ('<a href="javascript:addBookmark(\'' + escape(url) + '\', \'' + escape(title) + '\')">' + insert + '</a>');
	else if (window.opera && window.print)
		return ('<a rel="sidebar" href="' + escape(url) + '" title="' + escape(title) + '">' + insert + '</a>');
	return ('');
}

function checkCustomizations()
{
	var tmp;
	var pattern = new RegExp(' ?filled ?');
	for (var i = 0; i < customizationFields.length; i++)
		/* If the field is required and empty then we abort */
		if (parseInt(customizationFields[i][1]) == 1 && $('#' + customizationFields[i][0]).val() == '' && !pattern.test($('#' + customizationFields[i][0]).attr('class')))
			return false;
	return true;
}

function ceilf(value, precision)
{
	if (typeof(precision) == 'undefined')
		precision = 0
	var precisionFactor = precision == 0 ? 1 : Math.pow(10, precision);
	var tmp = value * precisionFactor;
	var tmp2 = tmp.toString();
	// If the current value has already the desired precision
	if (tmp2.indexOf('.') === false)
		return (value);
	if (tmp2.charAt(tmp2.length - 1) == 0)
		return value;
	return Math.ceil(tmp) / precisionFactor;
}

function floorf(value, precision)
{
	if (typeof(precision) == 'undefined')
		precision = 0
	var precisionFactor = precision == 0 ? 1 : Math.pow(10, precision);
	var tmp = value * precisionFactor;
	var tmp2 = tmp.toString();
	// If the current value has already the desired precision
	if (tmp2.indexOf('.') === false)
		return (value);
	if (tmp2.charAt(tmp2.length - 1) == 0)
		return value;
	return Math.floor(tmp) / precisionFactor;
}
/*#################################################################################*/
/*#################################################################################*/
/*#################################################################################*/
function checkNewsletterPrivacy() {
	var emailDaReg = trim($('#emailfastreg').val());
	var action = $('#action').val()
	if (action == 0) {
		if (emailDaReg!='' && indirizzoEmailValido(emailDaReg)) {
			if (document.getElementById('pr_si').checked == true) {
				return true;
			} else {
				alert('Devi acconsentire al trattamento dei dati personali.');
				return false
			}
		} else {
			alert('Devi inserire un indirizzo email valido');
			return false;
		}
	} else {
		return true;
	}
};

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
        fromIndex = 0;
    } else if (fromIndex < 0) {
        fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex, j = this.length; i < j; i++) {
        if (this[i] === obj)
            return i;
    }
    return -1;
  };
};


function indirizzoEmailValido(indirizzo) {
    if (window.RegExp) {
        var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
        var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        var regnv = new RegExp(nonvalido);
        var regv = new RegExp(valido);
        if (!regnv.test(indirizzo) && regv.test(indirizzo))
            return true;
        return false;
    }
    else {
        if(indirizzo.indexOf("@") >= 0)
            return true;
        return false;
    }
};
//-----------------------------------------------------------------------------
document.getElementsByClassName = function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = this.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) {
         var classes = elem[i].className;
         if (myclass.test(classes)) retnode.push(elem[i]);
         }
    return retnode;
};
//-----------------------------------------------------------------------------
function insertAfter(node, referenceNode){
    if(referenceNode.nextSibling) {
        referenceNode.parentNode.insertBefore(node, referenceNode.nextSibling);
    }
    else {
        referenceNode.parentNode.appendChild(node);
    }
};
//-----------------------------------------------------------------------------
function showElementsById(idToShow,idToHide) {
    if (document.getElementById(idToHide) != null) {
        document.getElementById(idToHide).style.display = 'none';
    }
    if (document.getElementById(idToShow) != null) {
        document.getElementById(idToShow).style.display = 'block';
    }
};
//-----------------------------------------------------------------------------
function hideMe(idToHide) {
    if (document.getElementById(idToHide) != null) {
        document.getElementById(idToHide).style.display = 'none';
    }
};
//-----------------------------------------------------------------------------
function hideAll(initIdToHide,numberOfElement) {
    for (i=1; i<=numberOfElement;i++) {
        if (document.getElementById(initIdToHide+''+i) != null) {
            document.getElementById(initIdToHide+''+i).style.display = 'none';
        }
    }
};
function hideAllExcept(initIdToHide,numberOfElement,idExcept) {
    for (i=1; i<=numberOfElement;i++) {
        if (document.getElementById(initIdToHide+''+i) != null && (initIdToHide+''+i) != idExcept) {
            document.getElementById(initIdToHide+''+i).style.display = 'none';
        }
    }
};
function showByClass(classeToShow) {
    $('.'+classeToShow).each(function() {if ($(this).css('display')=='none') {$(this).fadeIn();}});
};
function showByClass(classeToShow,classeToHide) {
    $('.'+classeToHide).each(function() {if ($(this).css('display')!='none') {$(this).fadeOut();}});
    $('.'+classeToShow).each(function() {if ($(this).css('display')=='none') {$(this).fadeIn();}});
};
//-----------------------------------------------------------------------------
function setOpacity(obj,opacity){
    opacity=(opacity==100) ? 99.999 : opacity;
    obj.style.filter = "alpha(opacity:"+opacity+")";  // IE/Win
    obj.style.KHTMLOpacity = opacity/100; // Safari<1.2, Konqueror
    obj.style.MozOpacity = opacity/100;   // Older Mozilla and Firefox
    obj.style.opacity = opacity/100;      // Safari 1.2, newer Firefox and Mozilla, CSS3
};
//-----------------------------------------------------------------------------
var timerOpacity;
//-----------------------------------------------------------------------------
function getRequestParameter(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
};
function showElementByIdJQuery(id) {
    $('#'+id).css('display','block');
}
function toogleElementByIdJQuery(id) {
    $('#'+id).toggle();
};
//-----------------------------------------------------------------------------
function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
};
function Trim() {return this.replace(/\s+$|^\s+/g,"");};
function LTrim() {return this.replace(/^\s+/,"");};
function RTrim() {return this.replace(/\s+$/,"");};
    
String.prototype.Trim=Trim; 
String.prototype.RTrim=RTrim;   
String.prototype.LTrim=LTrim;   
//-----------------------------------------------------------------------------
function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);
}
//-----------------------------------------------------------------------------
function fadeInJQ(id) {
    fadeInJQ(id,'normal');
};
function fadeInJQ(id,vel) {
    $('#'+id).fadeIn(vel);
};
function fadeInJQByClass(classe,vel) {
    $('.'+classe).fadeIn(vel);
};
function fadeOutJQ(id) {
    fadeOutJQ(id,'normal');
};
function fadeOutJQ(id,vel) {
    $('#'+id).fadeOut(vel);
};
function fadeOutJQByClass(classe,vel) {
    $('.'+classe).fadeOut(vel);
};
function swapInJQ(id,vel) {
    if ($('#'+id).css("display")!="none") {
        fadeOutJQ(id,vel);
    }
    else {
        fadeInJQ(id,vel);
    }
};
function swapInJQByClass(classe,vel) {
    if ($('.'+classe).css("display")!="none") {
        fadeOutJQByClass(classe,vel);
    }
    else {
        fadeInJQByClass(classe,vel);
    }
};
function swapSlide(id) {
    $("#"+id).slideToggle('fast');
};
//-----------------------------------------------------------------------------
function setSrc(id,src) {
    $('#'+id).attr("src",src);
};
//-----------------------------------------------------------------------------
function cambiaImm(idImg,src) {
    setSrc(idImg,src);
};
//-----------------------------------------------------------------------------
function swapSrc(id,src1,src2) {
    if ($('#'+id).attr("src")==src1) {
        setSrc(id,src2);
    }
    else if ($('#'+id).attr("src")==src2) {
        setSrc(id,src1)
    }
};
//-----------------------------------------------------------------------------
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 roundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
}
//------------------------------------------------------------------------------
$(document).ready(function() {	
	
	$('.sparkling').each(function() {
		var obj = $(this);
		var dl = Math.floor(Math.random()*10);
		setTimeout(function(){lampeggia(obj);},dl*223);
	});
	
	function lampeggia(obj) {
		obj.fadeIn(2000);
		obj.fadeOut(2000);
		obj.fadeIn(2000);
		obj.fadeOut(2000);
		obj.fadeIn(2000);
	};
	
});
