// JavaScript Document
function vota_canzone(event, id_canzone) {
	new Ajax.Request('/ajax/vota.php?id_canzone='+id_canzone, {
		parameters: event.memo,
		onSuccess: function(transport) {
			var response = transport.responseText;
			if(response != "") {
				alert("Hai già votato questa canzone, grazie. Questo voto non vale!");
		  	}
		  	location.href = "/grazie_del_voto_"+id_canzone+"-"+event.memo.rated+".htm";
		}
	});	
}

function vota_album(event, id_album) {
	new Ajax.Request('/ajax/vota.php?id_album='+id_album, {
		parameters: event.memo,
		onSuccess: function(transport) {
			var response = transport.responseText;
			if(response != "") {
				alert("Hai già votato quest'album, grazie. Questo voto non vale!");
			}
			location.href = "/grazie_del_voto_album_"+id_album+".htm";
		}
	});
}

function init(id_elemento,tipo_elemento) {
	/*new Ajax.Request('/ajax/conta.php', {
		parameters: 'id_elemento='+id_elemento+'&tipo_elemento='+tipo_elemento
	});*/
}

/*function invia_commento(denominazione, url) {
    if($('nome').value && $('commento').value && $('userSecurityCode').value) {
        $('commenta').request({
            onSuccess: function(req) {
                if(req.responseText!='') {
                    if($('status')) {
                        $('status').innerHTML='Messaggio inviato. Collegati al <a href=\'forum_' + url + '.htm\'>forum dedicato a ' + denominazione + ' per vederlo</a>';
                    }
                    $('commenta').disable();
                    $('riabilita').appear();
                    $('riabilita').enable();
                    //window.location='forum_'+url+'.htm';
                }
            }
        });
    } else {alert('Compila correttamente tutti i campi. \nVerifica il codice di sicurezza (anti-spam).')};
}
*/
function invia_commento() {
	var emailFilter=/^([^ ][a-zA-Z0-9_\.\-]+)\@(([a-zA-Z0-9\-]+)\.)+([a-zA-Z0-9]{2,4})$/;

	if($('nome').value && $('commento').value && $('userSecurityCode').value && (!$('email').value || emailFilter.test($('email').value))) {
        $('commenta').request({
            onSuccess: function(req) {
				if($('action_link')) {
					var action_link_url = $('action_link').value;
				} else {
					var action_link_url = location.href;
				}
				
                if(req.responseText.match(/Già inserito/)) {
                	if($('status')) {
						$('status').innerHTML = 'Hai già inviato il tuo commento! Collegati al <a href=\'' + action_link_url + '\'>forum</a> per vederlo (se non lo vedi, <strong>aggiorna la pagina nel tuo browser</strong>). Grazie!';
					} else {
						alert('Hai già inviato il tuo commento!\nSe non lo vedi, prova ad aggiornare la pagina nel tuo browser (tasto F5 del tuo browser).\nGrazie!');
					}
					//$('commenta').disable();
					location.href = action_link_url;
				} else if(req.responseText=='') {
                	alert('Verifica che il campo con il codice di sicurezza sia corretto!');
					$('userSecurityCode').focus();
                } else {
					if($('lista_commenti')) {
						location.reload(true);
					} else if($('niente')) {
						$('niente').replace('<ul id="lista_commenti">'+req.responseText+'</ul>');
						location.href = action_link_url;
					} else if($('status')) {
						$('status').innerHTML = 'Messaggio inviato. Collegati al <a href=\'' + action_link_url + '\'>forum</a> per vederlo. Grazie!';
						location.href = action_link_url;
					}
				}
            }
        });
    } else {
		warning_message = "Compila correttamente tutti i campi: \n";
		if(!$('nome').value) {
			warning_message += "\t- inserisci il nome\n";
		}
		if(!$('commento').value) {
			warning_message += "\t- inserisci il commento\n";
		}
		if(!$('userSecurityCode').value) {
			warning_message += "\t- inserisci il codice di sicurezza (anti-spam)\n";
		}
		if($('email').value && !emailFilter.test($('email').value)) {
			warning_message += "\t- inserisci un indirizzo e-mail valido. Se non vuoi ricevere gli avvisi di risposta via e-mail puoi lasciare questo campo vuoto";
		}
		alert(warning_message);
	}
}

function invia_commento_beta(denominazione, url) {
	var emailFilter=/^([^ ][a-zA-Z0-9_\.\-]+)\@(([a-zA-Z0-9\-]+)\.)+([a-zA-Z0-9]{2,4})$/;

	if($('nome').value && $('commento').value && $('userSecurityCode').value && (!$('email').value || emailFilter.test($('email').value))) {
        $('commenta').request({
            onSuccess: function(req) {
                if(req.responseText.match(/Già inserito/)) {
                	if($('status')) {
						$('status').innerHTML='Hai già inviato il tuo commento! Collegati al <a href=\'forum_' + url + '.htm\'>forum dedicato a ' + denominazione + '</a> per vederlo (se non lo vedi, <strong>aggiorna la pagina nel tuo browser</strong>). Grazie!';
						if ($('action_link')) {
							location.href = $('action_link').value;
						} else {
							location.href = "forum_" + url + ".htm";
						}
					} else {
						alert('Hai già inviato il tuo commento!\nSe non lo vedi, prova ad aggiornare la pagina nel tuo browser (tasto F5 del tuo browser).\nGrazie!');
					}
					$('commenta').disable();
				} else if(req.responseText=='') {
                	alert('Verifica che il campo con il codice di sicurezza sia corretto!');
					$('userSecurityCode').focus();
                } else {
					if($('lista_commenti')) {
						//$('lista_commenti').innerHTML=req.responseText+$('lista_commenti').innerHTML;
						location.reload(true);
					} else if($('niente')) {
						$('niente').replace('<ul id="lista_commenti">'+req.responseText+'</ul>');
					} else if($('status')) {
						$('status').innerHTML='Messaggio inviato. Collegati al <a href=\'forum_' + url + '.htm\'>forum dedicato a ' + denominazione + '</a> per vederlo. Grazie!';
						if ($('action_link')) {
							location.href = $('action_link').value;
						} else {
							location.href = "forum_" + url + ".htm";
						}
					}
					//$('commenta').disable();
					//$('riabilita').appear();
					//$('riabilita').enable();
					//self.location.hash='discussioni';
				}
            }
        });
    } else {
		warning_message = "Compila correttamente tutti i campi: \n";
		if(!$('nome').value) {
			warning_message += "\t- inserisci il nome\n";
		}
		if(!$('commento').value) {
			warning_message += "\t- inserisci il commento\n";
		}
		if(!$('userSecurityCode').value) {
			warning_message += "\t- inserisci il codice di sicurezza (anti-spam)\n";
		}
		if($('email').value && !emailFilter.test($('email').value)) {
			warning_message += "\t- inserisci un indirizzo e-mail valido. Se non vuoi ricevere gli avvisi di risposta via e-mail puoi lasciare questo campo vuoto";
		}
		alert(warning_message);
	}
}

function br2nl(str) {
	mod_str = str.replace(/[ ]*<br>[ ]*/g,"\r\n");
	return mod_str;
}

function copia_testo() {
	testo_attuale = $('testo').innerHTML;
	testo_attuale = testo_attuale.replace(/<(|\/)p>/g,"");
	testo_attuale_no_tags = br2nl(testo_attuale);
	
	$('testo_corretto').value = testo_attuale_no_tags;
}

function copia_testo_(id_testo) {
	new Ajax.Request('/ajax/get_testo.php', {
			parameters: 'id_testo='+id_testo,
			onSuccess: function(transport) {
				$('testo_corretto').value = transport.responseText;
			}
		}
	);
}

function invia_testo_corretto(id_canzone) {
	var emailFilter=/^([a-zA-Z0-9_\.\-]+)\@(([a-zA-Z0-9\-]+)\.)+([a-zA-Z0-9]{2,4})$/;

	if($('nome').value && $('testo_corretto').value && $('userSecurityCode').value && (!$('e-mail').value || emailFilter.test($('e-mail').value))) {
        $('form_correggi_testo').request({
            onSuccess: function(req) {
                if(req.responseText.match(/Correzione canzone inserita/)) {
					//$('status').innerHTML = "<h2>Grazie "+$('nome').value+",</h2><p>abbiamo ricevuto la tua correzione che dovrà essere approvata. Grazie!</p>";
					//$('status').appear();
					//setTimeout("$('correggi_testo').fade()",5000);
					location.href = '/correzione_testo_'+id_canzone+'.htm';
				} else if(req.responseText.match(/Verifica codice/)) {
                	alert('Verifica che il campo con il codice di sicurezza sia corretto!');
					$('userSecurityCode').focus();
                }
            }
        });
    } else {
		warning_message = "Compila correttamente tutti i campi: \n";
		if(!$('nome').value) {
			warning_message += "\t- inserisci il nome\n";
		}
		if(!$('testo_corretto').value || $('testo_corretto').textLength < 160) {
			warning_message += "\t- inserisci il tuo testo corretto\n";
		}
		if(!$('userSecurityCode').value) {
			warning_message += "\t- inserisci il codice di sicurezza (anti-spam)\n";
		}
		if($('e-mail').value && !emailFilter.test($('e-mail').value)) {
			warning_message += "\t- inserisci un indirizzo e-mail valido. Se non vuoi inserirlo puoi lasciare questo campo vuoto";
		}
		alert(warning_message);
	}
}

function show_and_hide(id) {
	var elemento = $(id);
	if(elemento.style.display == 'none') {
		elemento.blindDown();
	} else {
		elemento.blindUp();
	}
}

function apri_e_stampa(indirizzo) {
	window.open(indirizzo, 'stampa', 'width=800,height=700')
}

function apri_video(video_id) {
	//player.addModelListener("TIME","getVideoTime"); 
	var posizione = $('posizione').innerHTML;
	player.sendEvent("STOP");
	w = window.open('/player.php?video_id='+video_id+'&pos='+posizione, 'video_player', 'width=672,height=285,scrollbars=no,resizable=no,location=no,status=no')
	//player.removeModelListener("TIME","getVideoTime"); 
	w.focus();
}

function getVideoTime(obj) {
	posizione = obj.position;
	if($('posizione')) {
		$('posizione').innerHTML = posizione;
	}
};
var player;
function playerReady(obj) {
	//alert('the videoplayer '+obj['id']+' has been instantiated');
	player = document.getElementById(obj['id']);
	player.addModelListener("TIME","getVideoTime");
};

function chiudi(){
	window.close()
}
function stampa(){
	browserVersion = parseInt(navigator.appVersion)
	if (browserVersion >= 4) window.print()
}

function rispondi_a_commento(parent_id) {
	$('rispondi_a_commento_'+parent_id).request({
        onSuccess: function(req) {
            if($('risposte_a_'+parent_id)) {
                //$('risposte_a_'+parent_id).innerHTML=$('risposte_a_'+parent_id).innerHTML+req.responseText;
            } else {
                //$('rispondi'+parent_id).innerHTML='<ul id=\'risposte_a_'+parent_id+'\'>'+req.responseText+'</ul>';
            }
            Effect.BlindUp('rispondi_a_commento_'+parent_id);
            location.reload(true);
        }
    });
}

function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function tw_click(href) {
	window.open(href,'sharer_tw','toolbar=0,status=0,width=626,height=436');
	return false;
}


