var NUMBER_OF_STARS = 5;

if (/msie/i.test (navigator.userAgent)) //only override IE
{
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function(id)
	{
		var elem = document.nativeGetElementById(id);
		if(elem)
		{
			//make sure that it is a valid match on id
			if(elem.id == id)
			{
				return elem;
			}
			else
			{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++)
				{
					if(document.all[id][i].id == id)
					{
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	};
}

//FUNZIONE PER IL CONTROLLO DELLA CORRETTEZZA DELLA MAIL
	function validate_email(field)
	{
		with (field)
		{
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) 
		  {return false}
		else {return true}
		}
	}
	
//Funzione di controllo per il form
function controlla(e){
	
	var ret = true;
	
	//Controllo il campo nome
	if(Field.getValue('nome') == ""){
		new Effect.Highlight('nome');
		ret = false;
		return;
	}
	//Controllo il campo cognome
	if(Field.getValue('cognome') == ""){
		new Effect.Highlight('cognome');
		ret = false;
		return;
	}
	//Controllo il campo email
	if(Field.getValue('email') == "" || !validate_email($('email'))){
		new Effect.Highlight('email');
		ret = false;
		return;
	}
	//Controllo il campo email
	if(Field.getValue('file') == ""){
		new Effect.Highlight('file');
		ret = false;
		return;
	}
	
	//Controllo la licenza
	if(Field.getValue('auth') != "SI"){
		window.alert("Attenzione, devi accettare le condizioni per proseguire!");
		ret = false;
		return;
	}
	
	if(ret){
		$('modulo').submit();
	}
}

function init_rating()
{
	Event.observe('btInvio', 'click', controlla, false);
	
    var ratings = document.getElementsByTagName('div');
    for (var i = 0; i < ratings.length; i++)
    {
			
        if (ratings[i].className != 'rating')
            continue;

        var rating = ratings[i].firstChild.nodeValue;
        ratings[i].removeChild(ratings[i].firstChild);
        if (rating > NUMBER_OF_STARS || rating < 0)
            continue;
        for (var j = 0; j < NUMBER_OF_STARS; j++)
        {
            var star = document.createElement('img');
            if (rating >= 1)
            {
                star.setAttribute('src', './images/stars/rating_on.gif');
                star.className = 'on';
                rating--;
            }
            else if(rating > 0.1 && rating < 1)
            {
                star.setAttribute('src', './images/stars/rating_half.gif');
                star.className = 'half';
                rating = 0;
            }
            else
            {
                star.setAttribute('src', './images/stars/rating_off.gif');
                star.className = 'off';
            }
            var widgetId = ratings[i].getAttribute('id').substr(7);
            star.setAttribute('id', 'star_'+widgetId+'_'+j);
            star.onmouseover = new Function("evt", "displayHover("+widgetId+", "+j+");");
            star.onmouseout = new Function("evt", "displayNormal("+widgetId+", "+j+");");
            ratings[i].appendChild(star);
        } 
    }

    //Registrazione azione per le immagini 
    $$("div.rating img").each(function(p){
	Event.observe(p.id, 'click', vote, false);
    });
	
	$$("input.si").each(function(p){
		Event.observe(p.id, 'click', pub, false);
    });
	$$("input.no").each(function(p){
		Event.observe(p.id, 'click', depub, false);
    });
		
}

function init_rating_no()
{
    var ratings = document.getElementsByTagName('div');
    for (var i = 0; i < ratings.length; i++)
    {
        if (ratings[i].className != 'rating')
            continue;
            
        var rating = ratings[i].firstChild.nodeValue;
	
        ratings[i].removeChild(ratings[i].firstChild);
        if (rating > NUMBER_OF_STARS || rating < 0)
            continue;
        for (var j = 0; j < NUMBER_OF_STARS; j++)
        {
            var star = document.createElement('img');
            if (rating >= 1)
            {
                star.setAttribute('src', './images/stars/rating_on.gif');
                star.className = 'on';
                rating--;
            }
            else if(rating > 0.1 && rating < 1)
            {
                star.setAttribute('src', './images/stars/rating_half.gif');
                star.className = 'half';
                rating = 0;
            }
            else
            {
                star.setAttribute('src', './images/stars/rating_off.gif');
                star.className = 'off';
            }
            var widgetId = ratings[i].getAttribute('id').substr(7);
            star.setAttribute('id', 'star_'+widgetId+'_'+j);
            ratings[i].appendChild(star);
        } 
    }
		
}

function pub(evt){
	var idp = evt.target.getAttribute('id').substr(5);

		var data = $H({
			'id': idp
		});
		new Ajax.Request('public.php', {
			method: 'post',
			parameters: data.toQueryString(),
			onComplete: function(requester){
				new Effect.Highlight('adv_' + idp);
			}
		});
	
}

function depub(evt){
	var idp = evt.target.getAttribute('id').substr(7);
	var data = $H({'id' : idp});

		new Ajax.Request('unpublic.php', {
			method: 'post',
			parameters: data.toQueryString(),
			onComplete: function(requester){
				new Effect.Highlight('adv_' + idp);
			}
		});
	
}

function agg_rating(id)
{
    	var ratings = $("rating_" + id);
           
        var rating = ratings.firstChild.nodeValue;
	
        ratings.removeChild(ratings.firstChild);

        for (var j = 0; j < NUMBER_OF_STARS; j++)
        {
            var star = document.createElement('img');
            if (rating >= 1)
            {
                star.setAttribute('src', './images/stars/rating_on.gif');
                star.className = 'on';
                rating--;
            }
            else if(rating > 0.1 && rating < 1)
            {
                star.setAttribute('src', './images/stars/rating_half.gif');
                star.className = 'half';
                rating = 0;
            }
            else
            {
                star.setAttribute('src', './images/stars/rating_off.gif');
                star.className = 'off';
            }
            var widgetId = ratings.getAttribute('id').substr(7);
            star.setAttribute('id', 'star_'+widgetId+'_'+j);
            star.onmouseover = null;
            star.onmouseout = null;
            ratings.appendChild(star);
    } 
	
}

function displayHover(ratingId, star)
{
    for (var i = 0; i <= star; i++)
    {
        document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './images/stars/rating_over.gif');
    }
}

function displayNormal(ratingId, star)
{
    for (var i = 0; i <= star; i++)
    {
        var status = document.getElementById('star_'+ratingId+'_'+i).className;
        document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './images/stars/rating_'+status+'.gif');
    }
}

function vote(evt){
	   		var tmp = Event.element(evt).id.substr(5);
            var widgetId = tmp.substr(0, tmp.indexOf('_'));
            var starNbr = tmp.substr(tmp.indexOf('_')+1);
	    	var data = $H({'id' : widgetId, 'vote' : starNbr});
            new Ajax.Updater('rating_' + widgetId, 'vote.php', {
				postBody: data.toQueryString(),
				onComplete: function(requester){
					agg_rating(widgetId);
					new Effect.Highlight('adv_' + widgetId);
					new Insertion.Bottom( $('rating_' + widgetId), '<img src="./images/stars/ok.gif">' );
					var vot = document.getElementById('percent_' + widgetId).innerHTML * 1 + 1;
					$('percent_' + widgetId).update(vot);
					
				}
            });
}
