// JavaScript Document
// Main javascript. Used with www.cantinarte.com
//author: Augusto Picciani

if (document.images)//CARICO IMMAGINI DURANTE IL LOAD DELLA PAGINA
   {
     vino= new Image(114,30);
     vino.src="images/menudown/WGvino.jpg";  

     olio= new Image(114,30);
     olio.src="images/menudown/WGolio.jpg";
	 
	 zafferano= new Image(114,30);
     zafferano.src="images/menudown/WGzafferano.jpg";  

     regali= new Image(114,30);
     regali.src="images/menudown/WGregali.jpg";
	 
	 riconoscimenti= new Image(114,30);
     riconoscimenti.src="images/menudown/WGriconoscimenti.jpg"; 
	 
	 tour= new Image(114,30);
     tour.src="images/menudown/WGtour.jpg"; 
	 
	 degusta= new Image(114,30);
     degusta.src="images/menudown/WGriconoscimenti.jpg"; 
	 
	 menuDown= new Image(114,30);
     menuDown.src="images/menudown/WGriconoscimenti.jpg"; 
	 
	 
	 cantinarte= new Image(114,30);
     cantinarte.src="images/menuup/WGCantinarte.jpg";
	 
	 prodotti= new Image(114,30);
     prodotti.src="images/menuup/WGProdotti.jpg";  
	 
	 museo= new Image(114,30);
     museo.src="images/menuup/WGMuseo.jpg";
	 
	 catalogo= new Image(114,30);
     catalogo.src="images/menuup/WGCatalogo.jpg";  
	 
	 carrello= new Image(114,30);
     carrello.src="images/menuUp/cart1B.jpg";  
	 
  }


function roll(img_name, img_src)//GESTISCO ROLLHOVER IMMAGINI MENU
   {
   document[img_name].src = img_src;
   //document[img_name].src = pic1on.src;
   }
   
   function showMenuInferiore(nomeMenu){ //ATTIVO MENU A SCOMPARSA INFERIORI
     menu=document.getElementById(nomeMenu);
	 menu.style.display='block';
	 
   }
   function hideMenuInferiore(){
   menu1=document.getElementById('menuDownHome');
   menu2=document.getElementById('menuDownProdotti');
   menu3=document.getElementById('menuDownTour');

      if(menu1.style.display=='block'){
         menu1.style.display='none';
       }else if(menu2.style.display=='block'){
	     menu2.style.display='none';
	   }else if(menu3.style.display=='block'){
	     menu3.style.display='none';
	   }
   }

function getXMLHTTP() { 
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e){
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}

return xmlhttp;
}

// CREO COOKIE SE NON ESISTE
function creaCookie() {

nocache=Math.random();
strURL="/newCantinarte/creaCookie.asp?nocache="+nocache;
//if (valore!=""){

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {

} else {
alert("Il cookie non e' stato creato");
 }
}
}
req.open("GET", strURL, true);
req.send(null);
}
 //}//if iniziale
}

//INSERISCE ARTICOLI NEL CARRELLO
function inserisciCarrello(codProd){
	identificatore=codProd+"quantita";
    quantita=document.getElementById(identificatore).value;
	if(!soloNumeri(quantita) || quantita==""){
	 alert("la quantita accetta solo numeri interi a partire da 1");
	 return;
 }else{
	location.href="/newCantinarte/carrello.asp?cod="+codProd+"&quant="+quantita;
 }
} 
  
  
//CANCELLAZIONE TOTALE CARRELLO AJAX
function deleteCarrello() {

nocache=Math.random();
strURL="/newCantinarte/deleteCarrello.asp?nocache="+nocache;
//if (valore!=""){

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById("ajaxUpdate").innerHTML=req.responseText;
} else {
alert("modifica non effettuata per problema di connessione.");
 }
}
}
req.open("GET", strURL, true);
req.send(null);
}
 //}//if iniziale
}

//CANCELLAZIONE SINGOLA CARRELLO

//CANCELLAZIONE SINGOLA CARRELLO AJAX
function deleteSingleCarrello(codProd) {

nocache=Math.random();
strURL="script/deleteSingleCarrello.asp?cod="+codProd+"&nocache="+nocache;
//if (valore!=""){

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById("ajaxUpdate").innerHTML=req.responseText;
} else {
alert("modifica non effettuata per problema di connessione.");
 }
}
}
req.open("GET", strURL, true);
req.send(null);
}
 //}//if iniziale
}
  
//MODIFICA QUANTITA AJAX

function cambiaDisplayQuantita(codProd,operazione){
	identificatore=codProd+"quantita";
	quantitaInput=identificatore;
	quantita=document.getElementById(quantitaInput).value;
	 if (operazione=="incremento"){
	 quantitaX=parseInt(quantita)+1
	   document.getElementById(quantitaInput).value=quantitaX;	   
	 }else if(operazione=="decremento" && parseInt(quantita)>0){
		 
		quantitaX=parseInt(quantita)-1
	   document.getElementById(quantitaInput).value=quantitaX; 
	   checkZero(quantitaInput);
	 }
		   updateQuantitaCarrello(codProd);
  
}
function checkZero(quantitaInput){
		quantita=document.getElementById(quantitaInput).value;
		if(parseInt(quantita)==0){
			document.getElementById(quantitaInput).value=1;
		}

}


//AGGIORNA QUANTITA'CARRELLO CON I PULSANTI INCREMENTA E DECREMENTA
function updateQuantitaCarrello(codProd) {
	
	identificatore=codProd+"quantita";
    quantita=document.getElementById(identificatore).value;

nocache=Math.random();
strURL="script/updateQuantitaCarrello.asp?cod="+codProd+"&quant="+quantita+"&nocache="+nocache;
//if (valore!=""){

var req = getXMLHTTP();

if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById("ajaxUpdate").innerHTML=req.responseText;

//REGOLO POSIZIONE E ALTEZZA IMMAGINE CONTENITORE
divHeightCarrello();

} else {
alert("modifica non effettuata per problema di connessione.");
 }
}
}
req.open("GET", strURL, true);
req.send(null);
}
 //}//if iniziale
}  


//CONTROLLO CARRELLO VUOTO
function checkCarrelloVuoto(){
	marker=document.getElementById("checkCarrelloVuoto");
	if(marker!=null){
		alert("Il tuo carrello e' vuoto. Aggiungi almeno un articolo prima di proseguire.")
	}else{
		location.href="datiFatturazione.asp"

	}
}

//VALIDAZIONE DATI FATTURAZIONE
function validaFatturazione(){
	
	nome=document.fatturazione.nome;
	cognome=document.fatturazione.cognome;
	via=document.fatturazione.via;
	civico=document.fatturazione.civico;
	citta=document.fatturazione.citta;
	provincia=document.fatturazione.provincia;
	cap=document.fatturazione.cap;
	email=document.fatturazione.email;
	codiceFiscale=document.fatturazione.codiceFiscale;
	note=document.fatturazione.note;

    var fatturazioneOk=1;
	
	if(nome.value==""){
		alert("Inserisci nome");
		nome.focus();
		fatturazioneOk=0;
	}else if(cognome.value==""){
		alert("Inserisci cognome");
		cognome.focus();
		fatturazioneOk=0;
	}else if(via.value==""){
		alert("Inserisci Via");
		via.focus();
		fatturazioneOk=0;
	}else if(civico.value==""){
		alert("Inserisci numero civico");
		civico.focus();
		fatturazioneOk=0;
	}else if(citta.value==""){
		alert("Inserisci Citta");
		citta.focus();
		fatturazioneOk=0;
	}else if(provincia.value==""){
		alert("Inserisci Provincia");
		provincia.focus();
		fatturazioneOk=0;
	}else if(cap.value==""){
		alert("Inserisci Cap");
		cap.focus();
		fatturazioneOk=0;
	}else if(nome.value==""){
		alert("Inserisci nome");
		nome.focus();
		fatturazioneOk=0;
	}else if(email.value==""){
		alert("Inserisci email");
		email.focus();
		fatturazioneOk=0;
	}
    
	if(fatturazioneOk==1){
		
		checkbox=document.getElementById("checkbox")
		if(checkbox.checked==1){
			if(validaSpedizione()==1){
				riempiCampiSpedizione();
			}
		}else{
			//campo di controllo per verificare se prendere anche i dati di spedizione nella pagina asp
        	document.fatturazione.controllo.value=0;
			document.fatturazione.submit();//INVIO FORM FATTURAZIONE
		}
		//location.href="riepilogo.asp"
	}

}

//VALIDAZIONE FORM SPEDIZIONE
function validaSpedizione(){
	
	nome=document.spedizione.nomeSped;
	cognome=document.spedizione.cognomeSped;
	via=document.spedizione.viaSped;
	civico=document.spedizione.civicoSped;
	citta=document.spedizione.cittaSped;
	provincia=document.spedizione.provinciaSped;
	cap=document.spedizione.capSped;
	

    var spedizioneOk=1;
	
	if(nome.value==""){
		alert("Inserisci nome");
		nome.focus();
		spedizioneOk=0;
	}else if(cognome.value==""){
		alert("Inserisci cognome");
		cognome.focus();
		spedizioneOk=0;
	}else if(via.value==""){
		alert("Inserisci Via");
		via.focus();
		spedizioneOk=0;
	}else if(civico.value==""){
		alert("Inserisci numero civico");
		civico.focus();
		spedizioneOk=0;
	}else if(citta.value==""){
		alert("Inserisci Citta");
		citta.focus();
		spedizioneOk=0;
	}else if(provincia.value==""){
		alert("Inserisci Provincia");
		provincia.focus();
		spedizioneOk=0;
	}else if(cap.value==""){
		alert("Inserisci Cap");
		cap.focus();
		spedizioneOk=0;
	}
	    return spedizioneOk;
}

//RIEMPIO CAMPI SPEDIZIONE HIDDEN NEL FORM FATTURAZIONE PER L' INVIO
function riempiCampiSpedizione(){
	
	//campo di controllo per verificare se prendere anche i dati di spedizione nella pagina asp
	document.fatturazione.controllo.value=1;
	
	nome=document.spedizione.nomeSped.value;
	cognome=document.spedizione.cognomeSped.value;
	via=document.spedizione.viaSped.value;
	civico=document.spedizione.civicoSped.value;
	citta=document.spedizione.cittaSped.value;
	provincia=document.spedizione.provinciaSped.value;
	cap=document.spedizione.capSped.value;
	
	document.fatturazione.nomeSped.value=nome;
	document.fatturazione.cognomeSped.value=cognome;
	document.fatturazione.viaSped.value=via;
	document.fatturazione.civicoSped.value=civico;
	document.fatturazione.cittaSped.value=citta;
	document.fatturazione.provinciaSped.value=provincia;
	document.fatturazione.capSped.value=cap;
	
	//INVIO FORM
	document.fatturazione.submit();
}

//VALIDAZIONE FORM PAGAMENTO
function validaPagamento(){
	radioButton=document.metodoPagamento.pagamento;
	 validazione=0;
	  for(i=0;i<radioButton.length;i++){
		  if(radioButton[i].checked==true){
			  validazione=1;
			    marker=i;
		  }
	  }
	  if(validazione==0){
		  alert("Seleziona un metodo di pagamento");
	  }else{
		    if(radioButton[marker].value=="paypal"){// se e' selezionato paypal alloro invio ad altra pagina
				location.href="paypal.asp"
			}else{
				document.metodoPagamento.submit();
			}
	  }
}

//AGGIORNAMENTO QUANTITA' E PREZZO PAGINE OLIO-EXTRAVERGINE.ASP E VINO-MONTEPULCIANO.ASP
function updateQuantitaProdotti(quantita,codProd) {
	
 if(quantita==0){
	 alert("la quantita non puo' essere Zero");
	 document.getElementById(codProd+"quantita").value='1';
	 updateQuantitaProdotti('1',codProd);
	 return;
 }
 if (soloNumeri(quantita)==false){//se vi sono altri caratteri oltre ai numeri
	 document.getElementById(codProd+"quantita").value='1';
	 updateQuantitaProdotti('1',codProd);
	 return;
 }
 
nocache=Math.random();
strURL="script/updateQuantitaProdotti.asp?cod="+codProd+"&quant="+quantita+"&nocache="+nocache;
//if (valore!=""){

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById(codProd).innerHTML=req.responseText;
} else {
alert("modifica non effettuata per problema di connessione.");
 }
}
}
req.open("GET", strURL, true);
req.send(null);
}
 //}//if iniziale
}

//CONTROLLO INSERIMENTO SOLO NUMERI IN QUANTITA BOX - PAGINE:VINO E OLIO.ASP
function soloNumeri(value) {
var strNumber = isNaN (value);
if (strNumber == true) {
	alert ("La quantita accetta solo numeri interi!");
	return false;
 }else{
	 return true;
 }
}

//AGGIORNAMENTO QUANTITA' E PREZZO CATALOGO
function updateQuantitaCatalogo(quantita,codProd) {
 if(!soloNumeri(quantita) || quantita==""){
	 alert("la quantita accetta solo numeri interi a partire da 1");
	 return;
 }
 
nocache=Math.random();
strURL="script/updateQuantitaCatalogo.asp?cod="+codProd+"&quant="+quantita+"&nocache="+nocache;
//if (valore!=""){

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById(codProd).innerHTML=req.responseText;
divHeight(codProd);
} else {
alert("modifica non effettuata per problema di connessione.");
 }
}
}
req.open("GET", strURL, true);
req.send(null);
}
 //}//if iniziale
}

//INVIO EMAIL CONTATTI - validazione e invio form - pagina Contatti.asp
function validazioneFormContatti(){
	
	nome=document.contatti.nome;
	cognome=document.contatti.cognome;
	email=document.contatti.email
	messaggio=document.contatti.note;
	
	validazione=1;
	
	if(nome.value==""){
		alert("Inserisci il tuo nome.");
		nome.focus();
		validazione=0;
	}else if(cognome==""){
		alert("inserisci il tuo cognome");
		cognome.focus();
		validazione=0;
	}else if(email==""){
		alert("inserisci la tua email per la risposta");
		email.focus();
		validazione=0;
	}else if(messaggio==""){
		alert("inserisci messaggio da inviare");
		messaggio.focus();
		validazione=0;
	}
	
	   if(validazione==1){
		   document.contatti.submit();
	   }
}

//AGGIORNAMENTO QUANTITA' E PREZZO PAGINE REGALI AZIENDALI
function updateQuantitaRegali(quantita,codProd) {
	
 if(quantita==0){
	 alert("la quantita non puo' essere Zero");
	 document.getElementById(codProd+"quantita").value='1';
	 updateQuantitaRegali('1',codProd);
	 return;
 }
 if (soloNumeri(quantita)==false){//se vi sono altri caratteri oltre ai numeri
	 document.getElementById(codProd+"quantita").value='1';
	 updateQuantitaRegali('1',codProd);
	 return;
 }
 
nocache=Math.random();
strURL="script/updateQuantitaRegali.asp?cod="+codProd+"&quant="+quantita+"&nocache="+nocache;
//if (valore!=""){

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById(codProd).innerHTML=req.responseText;
} else {
alert("modifica non effettuata per problema di connessione.");
 }
}
}
req.open("GET", strURL, true);
req.send(null);
}
 //}//if iniziale
}


//CATALOGO ==> POSIZIONAMENTO IMMAGINE ARTICOLO
function divHeight(codProd){
	altezzaPartenza=document.getElementById(codProd).offsetHeight;
	containerImmagine="imm"+codProd
	divImm=document.getElementById(containerImmagine);
	partenza=divImm.offsetHeight;
	altezzaFinale=altezzaPartenza-20;
	if(altezzaPartenza>207){//207 equivale all' altezza standard
	divImm.style.height=altezzaFinale+"px";//setto altezza finale
	}
}

//CARRELLO ==> POSIZIONAMENTO IMMAGINE ARTICOLO
function divHeightCarrello(codProd){
	oColl = document.getElementsByTagName('INPUT');
 for (i=0;i<oColl.length;i++) { //RECUPERO TUTTI I VALORI DELLE INPUT HIDDEN CREATE IN REFRESH CARRELLO E LI PASSO A divHeight(codProd)
  curInp = oColl.item(i);
	if (curInp.type == 'hidden') { 
	 codProd=curInp.value;
       divHeight(codProd);
     }
 }
}


function validateRecipes(){
	isValidate=true;
	titoloIT=document.contenuto.titoloIT;
	 	titoloEN=document.contenuto.titoloEN;
			ingredientiIT=document.contenuto.ingredientiIT;
				ingredientiEN=document.contenuto.ingredientiEN;
					contenutoIT=document.contenuto.contenutoIT;
					 	contenutoEN=document.contenuto.contenutoEN;
if(titoloIT.value==""){
	titoloIT.focus();
	alert("titoloIT ");
	isValidate=false;
}else if(titoloEN.value==""){
	titoloEN.focus();
	alert("titoloEN");
	isValidate=false;
}else if(ingredientiIT.value==""){
	ingredientiIT.focus();
	alert("ingredientiIT");
	isValidate=false;
}else if(ingredientiEN.value==""){
	ingredientiEN.focus();
	alert("ingresientiEN");
	isValidate=false;
}else if(contenutoEN.value==""){
	contenutoEN.focus();
	alert("contenutoEN");
	isValidate=false;
}else if(contenutoIT.value==""){
	contenutoIT.focus();
	alert("contenutoIT");
	isValidate=false;
}

return isValidate;


}


function inserisciRecipes() {


titoloIT=document.contenuto.titoloIT.value;
	 	titoloEN=document.contenuto.titoloEN.value;
			ingredientiIT=document.contenuto.ingredientiIT.value;
				ingredientiEN=document.contenuto.ingredientiEN.value;
					contenutoIT=document.contenuto.contenutoIT.value;
					 	contenutoEN=document.contenuto.contenutoEN.value;
						 image=document.contenuto.image.value;
						 
						
nocache=Math.random();
strURL="/newCantinarte/common/pannelloControllo/ajax/inserimentoRecipes.asp?titoloIT="+titoloIT+"&titoloEN="+titoloEN+"&ingredientiIT="+ingredientiIT+"&ingredientiEN="+ingredientiEN+"&contenutoIT="+contenutoIT+"&contenutoEN="+contenutoEN+"&image="+image+"&nocache="+nocache;
//if (valore!=""){

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById("resultInsert").style.display='block';
} else {
alert("modifica non effettuata. Errore.");
 }
}
}
req.open("GET", strURL, true);
req.send(null);
}
 //}//if iniziale
}

function refreshRecipe() {
					
nocache=Math.random();
strURL="/newCantinarte/common/pannelloControllo/ajax/recipesEditor.asp?nocache="+nocache;
//if (valore!=""){

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById("ajaxUpdate").innerHTML=req.responseText;

} else {
alert("modifica non effettuata. Errore.");
 }
}
}
req.open("GET", strURL, true);
req.send(null);
}
 //}//if iniziale
}

