function openpop(url, title, param){

	title = title.replace(" ","");

	new2=window.open(url, title, param);

}
var NUMBER_OF_STARS = 5;
function submitRating(evt)
{
//	alert (evt.target.getAttribute('id'));
	
	var tmp = evt.target.getAttribute('id').substr(5);
	
	var tipoRate = tmp.substr(0, 1);
	var tmp2 = tmp.substr(2);
//	alert (tipoRate); 
	var widgetId = tmp2.substr(0, tmp2.indexOf('_'));
//	alert (widgetId); 
	var starNbr = tmp2.substr(tmp2.indexOf('_')+1);
//	alert (starNbr); 
//	alert ('rated_'+tipoRate+''+widgetId+'=1');
//	alert (eval('rated_'+tipoRate+''+widgetId));
	
	if (!eval('rated_'+tipoRate+widgetId)){  //controllo lato client che impedisce di rivotare senza ricaricare la pagina
		eval('rated_'+tipoRate+widgetId+'=1');
		$.get("submitRating.php?tipo="+tipoRate+"&maia="+Math.floor(Math.random()*10000),
		   { ratingID: widgetId, value: starNbr},
		   function(data){
			 document.getElementById('backFromCall_'+tipoRate+widgetId).innerHTML=data;
			 document.getElementById('rating_'+tipoRate+widgetId).innerHTML=parseInt(starNbr)+1;
			 single_init_rating(tipoRate+widgetId);
			 if (tipoRate!=1){
				userNumVotes=userNumVotes+1;
			 	textNumVotes=userNumVotes + " votes to date";
			 	document.getElementById('voteInfo_'+tipoRate+widgetId).innerHTML=textNumVotes;
			 }
		   }
		 );
	} else {
		document.getElementById('backFromCall_'+widgetId).innerHTML=textAlreadyVoted;	
	}
}

function jqueryInit()
{
	init_rating();
	if (pBind){
		$('div.rating/img').bind('click', submitRating);
	}
}

function init_rating()
{
    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', './img/icone/rating_on.gif');
                star.className = 'on';
                rating--;
            }
            else if(rating == 0.5)
            {
                star.setAttribute('src', './img/icone/rating_half.gif');
                star.className = 'half';
                rating = 0;
            }
            else
            {
                star.setAttribute('src', './img/icone/rating_off.gif');
                star.className = 'off';
            }

			var widgetId = ratings[i].getAttribute('id').substr(8);
			var tipoRate = ratings[i].getAttribute('id').substr(7,1);
			
			star.setAttribute('id', 'star_'+tipoRate + '_' + widgetId+'_'+j);
            star.onmouseover = new Function("evt", "displayHover('"+tipoRate+ '_' + widgetId+"', "+j+");");
            star.onmouseout = new Function("evt", "displayNormal('"+tipoRate+ '_' + widgetId+"', "+j+");");
            ratings[i].appendChild(star);
		
        } 
    }
}


function single_init_rating(refreshId)
{
	var ratingToRefresh = document.getElementById('rating_'+refreshId);
            
	var rating = ratingToRefresh.firstChild.nodeValue;

	ratingToRefresh.removeChild(ratingToRefresh.firstChild);
	
	for (var j = 0; j < NUMBER_OF_STARS; j++)
	{
		var star = document.createElement('img');
		if (rating >= 1)
		{
			star.setAttribute('src', './img/icone/rating_on.gif');
			star.className = 'on';
			rating--;
		}
		else if(rating == 0.5)
		{
			star.setAttribute('src', './img/icone/rating_half.gif');
			star.className = 'half';
			rating = 0;
		}
		else
		{
			star.setAttribute('src', './img/icone/rating_off.gif');
			star.className = 'off';
		}

		var widgetId = ratingToRefresh.getAttribute('id').substr(8);
		var tipoRate = ratingToRefresh.getAttribute('id').substr(7,1);
			
		star.setAttribute('id', 'star_'+tipoRate + '_' + widgetId+'_'+j);
		
		star.onmouseover = new Function("evt", "displayHover('"+tipoRate+ '_' + widgetId+"', "+j+");");
		star.onmouseout = new Function("evt", "displayNormal('"+tipoRate+ '_' + widgetId+"', "+j+");");
		ratingToRefresh.appendChild(star);
		
	} 
}

function displayHover(ratingId, star)
{	
    for (var i = 0; i <= star; i++)
    {
        document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './img/icone/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', './img/icone/rating_'+status+'.gif');
    }
}



function toggleDisplay(me){
 	
	if (document.getElementById(me).style.display=="none"){
		document.getElementById(me).style.display="block";
		
	}else {
		document.getElementById(me).style.display="none";			
	}
}

	
function showDiv(d){
	if (document.getElementById(d).style.display=="none"){
		document.getElementById(d).style.display="block";
	}
}		
function hideDiv(d){
	document.getElementById(d).style.display="none";
}


function setFormValues(id){		
	document.formwt.reset();
	
	document.forms['formwt'].testo.value = document.forms[id].commento.value ;

    //da sostituire con parte ajax
	document.formwt.submit(); 
}

function checkDiv(d,flag,text){
	
	if(flag==0){//onblur
		if(document.forms['form_'+d].commento.value == ""){//set message
			document.forms['form_'+d].commento.value = text;
			document.forms['form_'+d].commento.style.color="rgb(119, 119, 119)";
			
			hideDiv('button_'+d);
		}else if(document.forms['form_'+d].commento.value == text){//hide button
			hideDiv('button_'+d);
		}
	}else{//onfocus
		if(document.forms['form_'+d].commento.value == text){
			document.forms['form_'+d].commento.value = "";
			document.forms['form_'+d].commento.style.color="#000";
			
		}
		showDiv('button_'+d);
	}
}

function checkTopDiv(flag,text){

	if(flag==0){//onblur
		if(document.forms['formwt'].testo.value == ""){//set message
			document.forms['formwt'].testo.value = text;
			document.forms['formwt'].testo.style.color="rgb(119, 119, 119)";
			
//			hideDiv('button_'+d);
		}else if(document.forms['formwt'].testo.value == text){//hide button
//			hideDiv('button_'+d);
		}
	}else{//onfocus
		if(document.forms['formwt'].testo.value == text){
			document.forms['formwt'].testo.value = "";
			document.forms['formwt'].testo.style.color="#000";
			
		}
//		showDiv('button_'+d);
	}
}

