// ====================================================================
//                      FUNZIONI DI VALIDAZIONE FORM
//                  copyright Antares di Sbrana Saverio
// ====================================================================
function menuSelezionati()
{
	var path = window.location.pathname; 
	var file = path.substring(path.lastIndexOf("/")+1);
	file = file.substring(0,file.lastIndexOf("."));
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];	
		if (anchor.getAttribute("rel") == "menu"){
			//alert(anchor.childNodes.length);
			for (var j=0; j<anchor.childNodes.length; j++){
				var nodi = anchor.childNodes[j];
				if(nodi.nodeName == "IMG" && nodi.getAttribute("id") == file)
				{
					nodi.getAttribute("src");
					var mouseOver = nodi.getAttribute("onmouseover");
					//alert(typeof(mouseOver));
					if(typeof(mouseOver) == "function" )
					{
						mouseOver = String(mouseOver);
						//alert(mouseOver+"1")
						mouseOver = mouseOver.split("{");
						//alert(mouseOver[1]+"2")
						mouseOver = mouseOver[1].split("}");
						//alert("diventa"+mouseOver[0]);
						mouseOver = mouseOver[0];
					}

					
					mouseOverSplit = mouseOver.split(",");
					
					var valore  = mouseOverSplit[2].replace("'","");
					valore  = valore.replace("'","");
					nodi.setAttribute("src",valore);

				}
			}
		}
	}
}

function mostraInfo()
{
	var display = document.getElementById("contenutoInfo").style.display;
	if (display == "none" || display == "")
	{
		document.getElementById("contenutoDocumenti").style.display = "none"; 
		document.getElementById("contenutoInfo").style.display = "block"; 	
		
	}
	else
		document.getElementById("contenutoInfo").style.display = "none"; 	
}

function mostraDocumenti()
{
	var display = document.getElementById("contenutoDocumenti").style.display;
	if (display == "none" || display == "")
	{
		document.getElementById("contenutoInfo").style.display = "none"; 
		document.getElementById("contenutoDocumenti").style.display = "block"; 	
	}
	else
		document.getElementById("contenutoDocumenti").style.display = "none"; 	
}


function checkEmail(element)
{
	if(conSeVuoto(element._user))
	{
		erroreMsg("Inserire la username",element._user)
		return false;
	}
	if(conSeVuoto(element._pass))
	{
		erroreMsg("Inserire la password",element._pass)
		return false;
	}
}
function checkFormContatti(form)
{	
	if(conSeVuoto(form.nome))
	{
		erroreMsg("Inserire il nome",form.nome)
		return false;
	}
	if(conSeVuoto(form.cognome))
	{
		erroreMsg("Inserire il cognome",form.cognome)
		return false;
	}
	if(conSeVuoto(form.email))
	{
		erroreMsg("Inserire l'email",form.email)
		return false;
	}
	if(conEmail(form.email))
	{
		erroreMsg("Inserire l'email correttamente",form.email)
		return false;
	}
	if(conSeVuotoTextArea(form.messaggio))
	{
		erroreMsg("Inserire il messaggio",form.messaggio)
		return false;
	}
	if(form.privacy[1].checked)
	{
		erroreMsg("E' necessario accettare la legge sulla privacy",form.privacy[1])
		return false;
	}	
	inviaEmail();
	return false;
}

// Invio email

function inviaEmail()
{
	url = 'script/invioEmail.php';
	var form1 = document.contatti1;
	var form2 = document.contatti2;
	
	variabili = 'nome=' + form1.nome.value + '&cognome=' + form1.cognome.value ;
	variabili += '&indirizzo=' + form1.indirizzo.value + '&ragSociale=' + form1.ragSociale.value ;
	variabili += '&telefono=' + form1.telefono.value;
	variabili += '&email=' + form1.email.value + '&messaggio=' + form2.messaggio.value ;

	new Ajax.Request(url,{method:'post', postBody:variabili , onSuccess: function(transport)
	{
		var risposta = '';
		if (transport.responseText != "errore" && transport.responseText == "ok")
		{
			ShowTip(this, "<b>Invio eseguito con successo</b>:<br/> Email inviata correttamente.",true);
			
		}
		else
			ShowTip(this, "<b>Errore</b>:<br/> Email <b>NON</b> inviata!<br/>Riprovare pił tardi. ",true);
		
	}
	, onUninitialized: function(transport)
	{
		ShowTip(this,"Invio in corso. <img src=\"immagini/loading.gif\"/>",false);
	}				
	, onLoading: function(transport)
	{
		ShowTip(this,"Invio in corso.. <img src=\"immagini/loading.gif\"/>",false);
	}		
	
	} );
}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

addLoadEvent(menuSelezionati);

var activeImage = 0;

function avviaNavigazioneEdiliza(nFoto,arrayFoto)
{
	
	//alert("ci sono n="+nFoto+" foto");
Element.hide('edilizaBoxNavNext');
Element.hide('edilizaBoxNavPrev');

	
		

		// if not first image in set, display prev image button
		if(activeImage != 0){
			Element.show('edilizaBoxNavPrev');
				document.getElementById('edilizaBoxNavPrev').onclick = function() {
				//alert("test  precedente ora="+arrayFoto[activeImage]+" dopo ="+arrayFoto[activeImage-1]); 
				cambiaFoto(arrayFoto[activeImage-1]);
				activeImage--;
				avviaNavigazioneEdiliza(nFoto,arrayFoto);
				return false;
			}
		}

		// if not last image in set, display next image button	
		//  imageArray.length - 1 
			
		if(activeImage != (arrayFoto.length-1)){
			Element.show('edilizaBoxNavNext');
			document.getElementById('edilizaBoxNavNext').onclick = function() {
				//alert("test  successivo ora="+arrayFoto[activeImage]+" dopo ="+arrayFoto[activeImage+1]);
				cambiaFoto(arrayFoto[activeImage+1]);
				activeImage++;
				avviaNavigazioneEdiliza(nFoto,arrayFoto);
				return false;
			}
		}

}

function cambiaFoto(id){
	
	document.getElementById("lightboxImage").src = "script/visualizzaEdilizia.php?id="+id+"&ridimensiona=true&width=390&height=260";
}

function aggiornaLink()
{
	// if not first image in set, display prev image button
		if(activeImage != 0){
			Element.show('edilizaBoxNavPrev');
				document.getElementById('edilizaBoxNavPrev').onclick = function() {
				alert("test  precedente ora="+arrayFoto[activeImage]+" dopo ="+arrayFoto[activeImage-1]); 
				cambiaFoto(arrayFoto[activeImage-1]);
				activeImage--;
				aggiornaLink();
				return false;
			}
		}

		// if not last image in set, display next image button	
		//  imageArray.length - 1 
			
		if(activeImage != (arrayFoto.length-1)){
			Element.show('edilizaBoxNavNext');
			document.getElementById('edilizaBoxNavNext').onclick = function() {
				alert("test  successivo ora="+arrayFoto[activeImage]+" dopo ="+arrayFoto[activeImage+1]);
				cambiaFoto(arrayFoto[activeImage+1]);
				activeImage++;
				aggiornaLink();
				return false;
			}
		}
	
}
