var numeroProdottiPrimaVis = 10;

$(document).ready(function(){
	$('#xmenu').treeonic({
		exclusive:	true
	});

	$('#apriSito').click(function(e){
		apriSito();
	});

	$('#apriCatalogo').click(function(e){
		apriCatalogo();
	});

	$('#gallery a').lightBox();

	$("a").click(function() {
		id = $(this).attr('id');

		if ( id == 'strutturaEsternaID'){
			$("#strutturaEsternaTesto").slideToggle("slow");
			return false;
		}
		else if ( id == 'strutturaInternaID'){
			$("#strutturaInternaTesto").slideToggle("slow");
			return false;
		}
		return true;
	});
});

function apri( obj ){
	
}


function apriSito(){
	window.open("sito.php", "sito", "width=800, height=400, top=150, left=120, scrollbars=yes, resizable=yes, toolbar=no");
}

function apriCatalogo(){
	window.open("catalogo.php", "sito", "width=800, height=400, top=150, left=120, scrollbars=yes, resizable=yes, toolbar=no");
}


function showLink( titolo )
{
	document.getElementById("txtLoading").innerHTML="<br><center><img src='images/loading.gif'></center>";
	
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
	document.getElementById("txtLoading").innerHTML="";
	
    }
  }
xmlhttp.open("GET","getLink.php?titolo="+titolo,true);
xmlhttp.send();


}

function showHome()
{
	document.getElementById("txtLoading").innerHTML="<br><center><img src='images/loading.gif'></center>";
	location.href="sito.php";
}

function showCatalogo(){
	document.getElementById("txtLoading").innerHTML="<br><center><img src='images/loading.gif'></center>";
	location.href="catalogo.php";
}

function inviaMail()
{
	document.getElementById("txtHintMail").innerHTML="";
	document.getElementById("txtLoadingMail").innerHTML="<br><center><img src='images/loading.gif'></center>";
	
	nome = document.getElementById("nome").value;
	cognome = document.getElementById("cognome").value;
	societa = document.getElementById("societa").value;
	tel = document.getElementById("tel").value;
	email = document.getElementById("email").value;
	messaggio = document.getElementById("messaggio").value;
	privacy = document.getElementById("privacy").checked;

	if (nome == "" || cognome == "" || tel == "" || email == "" || messaggio == "" )
	{
		document.getElementById("txtHintMail").innerHTML="<br><b><font style='color:#FF0000'>Compilare tutti i campi.</font></b>";
		document.getElementById("txtLoadingMail").innerHTML="";
		return;
	}

	if (!validateEmail(email))
	{
		document.getElementById("txtHintMail").innerHTML="<br><b><font style='color:#FF0000'>Email non corretta.</font></b>";
		document.getElementById("txtLoadingMail").innerHTML="";
		return;
	}

	if (!privacy)
	{
		document.getElementById("txtHintMail").innerHTML="<br><b><font style='color:#FF0000'>E' necessario acconsentire all'utilizzo dei dati personali.</font></b>";
		document.getElementById("txtLoadingMail").innerHTML="";
		return;
	}
	
	
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }

	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		document.getElementById("txtHintMail").innerHTML="";
		document.getElementById("txtLoadingMail").innerHTML="";
		
		}
	  }
	xmlhttp.open("GET","inviaMail.php?nome="+nome+"&cognome="+cognome+"&societa="+societa+"&tel="+tel+"&email="+email+"&messaggio="+messaggio,true);
	xmlhttp.send();


}

function inviaRichiesta(id,marca, modello)
{
	document.getElementById("txtHintMail").innerHTML="";
	document.getElementById("txtLoadingMail").innerHTML="<br><center><img src='images/loading.gif'></center>";
	
	nome = document.getElementById("nome").value;
	cognome = document.getElementById("cognome").value;
	societa = document.getElementById("societa").value;
	tel = document.getElementById("tel").value;
	email = document.getElementById("email").value;
	messaggio = document.getElementById("messaggio").value;

	if (nome == "" || cognome == "" || tel == "" || email == "" || messaggio == "" )
	{
		document.getElementById("txtHintMail").innerHTML="<br><b><font style='color:#FF0000'>Compilare tutti i campi.</font></b>";
		document.getElementById("txtLoadingMail").innerHTML="";
		return;
	}

	if (!validateEmail(email))
	{
		document.getElementById("txtHintMail").innerHTML="<br><b><font style='color:#FF0000'>Email non corretta.</font></b>";
		document.getElementById("txtLoadingMail").innerHTML="";
		return;
	}
	
	
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }

	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		document.getElementById("txtHintMail").innerHTML="";
		document.getElementById("txtLoadingMail").innerHTML="";
		
		}
	  }
	xmlhttp.open("GET","inviaRichiesta.php?nome="+nome+"&cognome="+cognome+"&societa="+societa+"&tel="+tel+"&email="+email+"&messaggio="+messaggio+"&marca="+marca+"&modello="+modello+"&id="+id,true);
	xmlhttp.send();


}

function validateEmail(email) {
		var at = email.lastIndexOf("@");

		// Make sure the at (@) sybmol exists and  
		// it is not the first or last character
		if (at < 1 || (at + 1) === email.length)
			return false;

		// Make sure there aren't multiple periods together
		if (/(\.{2,})/.test(email))
			return false;

		// Break up the local and domain portions
		var local = email.substring(0, at);
		var domain = email.substring(at + 1);

		// Check lengths
		if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
			return false;

		// Make sure local and domain don't start with or end with a period
		if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
			return false;

		// Check for quoted-string addresses
		// Since almost anything is allowed in a quoted-string address,
		// we're just going to let them go through
		if (!/^"(.+)"$/.test(local)) {
			// It's a dot-string address...check for valid characters
			if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
				return false;
		}

		// Make sure domain contains only valid characters and at least one period
		if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
			return false;	

		return true;
	}

function apriScheda( id ){
	window.open("catalogo/"+id+"/scheda.pdf", "PDF", "width=800, height=400, top=150, left=120, scrollbars=yes, resizable=yes, toolbar=no");
}

function cercaIE(){
	var browserName=navigator.appName; 
	if (browserName=="Microsoft Internet Explorer")
	 {
		pagina = document.getElementById("pagina").value;
		prodottiPerPagina = document.getElementById("prodottiPerPagina").value;
		
		if ( pagina != "")
		{
			cercaProdottiPagina(pagina, prodottiPerPagina);
		}
	 }
}

function cercaProdotti(){
	document.getElementById("txtLoadingCat").innerHTML="<center><img src='images/loading.gif'></center>";
	
	testo = document.getElementById("birds").value;
	categoria = document.getElementById("categoria").value;

	prodottiPerPagina = document.getElementById("prodottiPerPagina").value;

	if ( prodottiPerPagina != "" )
	{
		numeroProdottiPrimaVis = document.getElementById("prodottiPerPagina").value;
	}

	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }

	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		document.getElementById("txtLoadingCat").innerHTML="";
		document.getElementById("pagina").value=1;
		document.getElementById("prodottiPerPagina").value=numeroProdottiPrimaVis;

		}
	  }
	xmlhttp.open("GET","getBeniCatalogo.php?testo="+testo+"&categoria="+categoria+"&pagina=1&prodottiPerPagina="+numeroProdottiPrimaVis,true);
	xmlhttp.send();
	
}

function cercaProdottiPagina( pagina, prodottiPerPagina ){
	document.getElementById("txtLoadingCat").innerHTML="<center><img src='images/loading.gif'></center>";
	
	testo = document.getElementById("birds").value;
	categoria = document.getElementById("categoria").value;

	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }

	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		document.getElementById("txtLoadingCat").innerHTML="";
		document.getElementById("pagina").value=pagina;
		document.getElementById("prodottiPerPagina").value=prodottiPerPagina;
		}
	  }
	xmlhttp.open("GET","getBeniCatalogo.php?testo="+testo+"&categoria="+categoria+"&pagina="+pagina+"&prodottiPerPagina="+prodottiPerPagina,true);
	xmlhttp.send();
	
}
