function $(i){
  return document.getElementById(i);
}
function lasciaFeedBack(){
	hide("tf");
	show("feedback");
}
function inviafeedback(){
	var nome = $("nome").value;
	var email = $("email").value;
	var testo = $("testo").value;
	
	if(!parolaMinMax(nome, 3, 20)){
		alert("Errore. Il nome dev'essere compreso fra i 3 ed i 20 caratteri");
		return;
	}
	if(!isEmail(email)){
		alert("L'indirizzo mail inserito non é valido");
		return;
	}
	if(!parolaMinMax(testo, 3, 1000)){
		alert("Errore. Il commento dev'essere compreso fra i 3 caratteri ed i 1000");
	}
	
	var req = new Ajax("inviafeedback.php");
	req.data.push("nome=" + nome);
	req.data.push("email="+email);
	req.data.push("testo="+testo);
	req.method="post";
	req.onload = "feedback";
	req.send();
}
function showhide(id){
	if($(id).style.display == "none"){
		$(id).style.display = "block";
	}
	else 
	$(id).style.display = "none";
}
function addBookmark(title,url) {
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,""); 
	}
	else if( document.all ) {
		window.external.AddFavorite( url, title);
	} 
	else if( window.opera && window.print ) {
		return true;
	}
}
function hos(i){
  if($(i).style.display == 'block'){
    $(i).style.display = 'none';
  }
  else $(i).style.display = 'block';
}
function show(ias){
	$(ias).style.display = 'block';
}

function hide(i){
	$(i).style.display = 'none';
}
function hideLoader(){
	
}
function showLoader(){
	
}
function isEmail(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;
 	}
}

// Restituisce un errore se la parola é minore o maggiore
function parolaMinMax(parola, min, max){
	if(parola.length >= min && parola.length <= max){
		return true;
	}
	else return false;
}

function Ajax(address, xml) {
	this.address = address;
	this.client =  richiestaXML();
	this.data = new Array();
	this.method = 'get';
	this.showloading = true;
	this.onload = false;
	this.onerror = false;
	this.json = false;
	this.send = function() {
		var self = this;
		this.client.onreadystatechange = function(){
			if(self.client.readyState  == 4) {
				if(self.showloading){
					hideLoader();
				}
				try { var status = self.client.status }
				catch (e) { var status = 500 }

				if(status == 200) {
					if(typeof self.onload == 'function'){
						if(xml){
								self.onload(self.client.responseXML);
						}
						else {
							if(self.json){
								self.onload(self.client.responseText.parseJSON());
							}
							else{
								self.onload(self.client.responseText);
							}
						}
					}
					
					if(self.onload == 'refresh')				// refresh page
						window.location.reload(false);
					else {											// innerHTML 
						var output = $(self.onload);
						if(output)
							output.innerHTML = self.client.responseText;
					}
				}
				else{
					if(status == 404){
						alert("Error: " + status);
					}
				}
			}
		};

		var c = this.data.length;
		if(c > 0) {
			var args = this.data[0];
			for(var i=1; i<c; i++) {
				var name = this.data[i].substring(0,this.data[i].indexOf('='));
				var value = this.data[i].substring(this.data[i].indexOf('=') + 1);

				args = args + '&' + name + '=' + encodeURIComponent(value);
			}
		} else
			var args = false;

		if(this.showloading){
			showLoader();
		}
		if(this.method == 'get') {
			this.client.open('GET', this.address + (args ? '?' + args : ''), true);
			this.client.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
			this.client.setRequestHeader('AJAX-Request', 'true');
			this.client.send(null);
		}
		if(this.method == 'post') {
			this.client.open('POST', this.address, true);
			this.client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			this.client.setRequestHeader('AJAX-Request', 'true');
			this.client.send(args?args:null);
		}
	};
}


function richiestaXML() {
	var http_request = false;

	if (window.XMLHttpRequest) { 
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	return http_request;
}