function getElementsByAttribute(aName, aValue) {
		var allTags = document.getElementsByTagName('*') || document.all;
		var results = new Array();
		for(var i=0; i < allTags.length; i++) {
			if(allTags[i].tagName != "TABLE" && allTags[i].getAttribute(aName) &&  aValue == allTags[i].getAttribute(aName)) {
				results.push(allTags[i])
			}
		}
		return results;
	}
	
function checkSegnalaForm() {
	var fields = new Array;
						
	fields[fields.length] = "from_mail";
	fields[fields.length] = "to_mail";

	
	var theform = document.getElementById("segnala");
	
	if(!isEmail(theform["from_mail"].value)) {
		window.alert("La tua email non sembra essere corretta!");
		return false;
	}
	
	if(!isEmail(theform["to_mail"].value)) {
		window.alert("L'email del destinatario non sembra essere corretta!");
		return false;
	}
	
	if(!isEmail(theform["from_mail"].value)) {
		window.alert("La tua mail non sembra essere corretta!");
	}
	
	
	if(theform["segnalaprivacy"].checked != true) {
		window.alert("Per inviare la segnalazione occorre accettare i termini della legge sulla privacy!");
		return false;
	}

	return true;
}	
	
	
function Segnala() {
	if($("jimmyrotella_segnala").style.visibility == "visible") {
		alert("Sto gia' inviando un messaggio!");
		return false;
	}
	if(checkSegnalaForm()) {
		
		var params = Form.serialize($("segnala"));
		$("jimmyrotella_segnala").style.visibility = "visible";
		
		var myAjax = new Ajax.Updater($("response"), $("segnala").action, { 
			parameters:
				params,
			onSuccess:
				function () {
					$("response").style.paddingTop = "3px";
				},
			onFailure:
				function() {
					alert("Siamo spiacenti\nSi e' verificato un errore durante l'invio dei dati!");
				},
			onComplete:
				function () {
					$("jimmyrotella_segnala").style.visibility = "hidden";
				}
		});
	}
}

function showSegnala() {
	var theheight;
	if ($("segnala").offsetHeight == 0) {
		$("segnala").style.height = "100%";
		theheight = $("segnala").offsetHeight;
		$("segnala").style.height = "0%";
	
		var counter = 0;
		var it = setInterval(function() {
				if($("segnala").offsetHeight >= theheight && counter != 0) {
					clearInterval(it);
				}
				counter+=5;
				$("segnala").style.height = counter+"px";
		},5);
	} else {
		theheight = $("segnala").offsetHeight;
		var counter = theheight;
		var it = setInterval(function() {
				counter-=5;
				if($("segnala").offsetHeight <= 0 && counter != theheight) {
					counter = 0;
					clearInterval(it);
				}
				
				$("segnala").style.height = counter+"px";
		},5);
	}
}


function activateVoters() {
	var voters = getElementsByAttribute("tnes:widgetType","voter");
	for (var i = 0; i < voters.length; i++) {
		var btnvote = voters[i].getElementsByTagName("dt");
		for (var j = 0; j < btnvote.length; j++) {
			btnvote[j].setAttribute("idx",j+1);
			btnvote[j].onmouseover = function() {
				if (this.parentNode.voted) return false;
				this.title = "voto "+this.getAttribute("idx")+" per questa frase";
				this.parentNode.style.backgroundPosition = -200+40*(1*this.getAttribute("idx"))+"px";
			}
			btnvote[j].onmouseout = function() {
				if (this.parentNode.voted) return false;
				this.parentNode.style.backgroundPosition = "-200px";
			}
			btnvote[j].onclick = function() {
				var thedl = this.parentNode;
				if (thedl.voted) return false;
				var article_id = thedl.getAttribute("tnes:id")
				thedl.setAttribute("voted",true);
				thedl.style.cursor = "default";
				
				//cookie check - controlla se ha gia' votato
				var votedarray = new Array();
				if(document.cookie.indexOf("voted") != -1) {
					var votedstring = document.cookie.match("voted="+"(.*?)(;|$)")[0];
					votedarray = votedstring.substring(6,votedstring.length-1).split(",");	
					for (var i = 0; i < votedarray.length; i++) {
						if(votedarray[i]==article_id) {
							thedl.style.background = "#fff";
							thedl.innerHTML = "<dd class='err'>Avevi gia' votato<br>precedentemente per questa frase,<br/>Spiacenti: non puoi votare 2 volte!</dd>";
							return;
						}
					}
				} 
				//end cookie check
				
				var vote = this.getAttribute("idx");
				thedl.innerHTML = "<dd><img src='images/ajax-loader.gif' width='35' height='35' /></dd>";
				thedl.style.background = "#fff";
				var myAjax = new Ajax.Updater(thedl, "/latte_stremato_score/latscore-0.save", { 
					parameters:
						"lat_entry_id="+article_id+"&score="+vote+"&_tnes_redirect_ok=http://www.lattestremato.it/response_vote.view",
					onSuccess:
						function () {
							votedarray.push(article_id);
							document.cookie = "voted="+votedarray+";expires =06/06/2666 00:00:00;"
						},
					onFailure:
						function() {
							alert("Siamo spiacenti\nSi e' verificato un errore durante l'invio dei dati!");
						}
				});					
			}
		}
	}
}
function wopen(url) {
	window.open(url,"privacy","width=300,height=300,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
}
function isEmail(par) {
	var e = par;
	e = e.replace(/^\s*(\S*)\s*$/, "$1");
	var pattern = /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,}$/;
	return (e == e.match(pattern));
}
function checkSearch() {
	var theform = $("searchform");
	if ($("searchstring").value.replace(/\s*/g,"") == "") {
		alert("Non hai inserito alcuna parola da cercare!");
		return;
	}
	theform.submit();
}
function checkForm() {
	var fields = new Array;
						
	fields[fields.length] = "messaggio";
	fields[fields.length] = "nome";
	fields[fields.length] = "email";
	
	var theform = document.getElementById("phraseform");
	
	for(var i = 0;i < fields.length;i++) {
		var elem = theform[fields[i]];
		if (elem == null || elem.value == "") {
			window.alert("E' obbligatorio compilare i campi segnati con un asterisco!");
			return false;
		} else if (fields[i] == "email" && !isEmail(elem.value)) {
			window.alert("L'email inserita non e' valida!");
			return false;
		}
	}
	
	if(theform["privacy"].checked != true) {
		window.alert("Per inviare il messaggio occorre accettare i termini della legge sulla privacy!");
		return false;
	}

	return true;
	//document[formName].submit();
}	
function addArticle() {
	if($("jimmyrotella_article").style.visibility == "visible") {
		alert("Sto gia' inviando un messaggio!");
		return false;
	}
	if($("email_check").value != ""){
		return false;
	}
		
	if(checkForm()) {
		var usr = document.createElement('div');
		usr.className = "article";
		var params = Form.serialize($("phraseform"));
		$("contents").insertBefore(usr, $("contents").childNodes[0]);
		$("jimmyrotella_article").style.visibility = "visible";
		var myAjax = new Ajax.Updater(usr, "/latte_stremato_not_approved/latentry-0.savelatentry", { 
			parameters:
				params,
			onSuccess:
				function () {
					setTimeout("activateVoters()",1000);
					usr.style.backgroundImage = "url(images/aniquots.gif)";
				},
			onFailure:
				function() {
					alert("Siamo spiacenti\nSi e' verificato un errore durante l'invio dei dati!");
				},
			onComplete:
				function () {
					$("jimmyrotella_article").style.visibility = "hidden";
				}
		});
	}
}
function init() {
	activateVoters();
}
window.onload = init;
