///////////////////////////////////FUNCTIONS JS//////////////////////////////////////
function inviaqta(q,x,xf) { //q=qta, x=idprod, xf=num. del form
//alert(q+" - "+x+" - "+xf)
if ((! q=="") || (!isNaN(q)) || (!isNaN(x))) {
/*
//disattivato se si utilizza la select x selezionare la qta da 
//inserire nel carrello, attivato se si utilizza input text
df=document.forms["form"+xf]
df.elements["qta"+xf].value=""  
*/
//alert(df)
//ajax2(q,x)
//new Ajax.Updater('divcarrello',"ajax.asp?azione=1&qtaprod="+q+"&idprod="+x);
getajax("ajax.asp?azione=1&qtaprod="+q+"&idprod="+x,"divcarrello")
//new Ajax.Updater('divnumprodottikart',"ajaxscripts.asp?aggiornanumprodottikart=1");
getajax("ajaxscripts.asp?aggiornanumprodottikart=1","divnumprodottikart")
//new Ajax.Updater('divtest',"ajaxtest.asp?test=1");
}
else {
alert("Attenzione:quantità non valida")
}
}


///////////////////////////////////FUNCTIONS JS//////////////////////////////////////
  
   
function admin() 
   {
open("admin/login.asp","admin","width=900,height=700,status=0,scrollbars=0")
   }
   
   
function insert() {
new_w=window.open("ins2.asp","insert","width=600,height=650,status=1,scrollbars=1");
new_w.focus()
}
function modifica(par) {
window.open("mod2.asp?id_modello="+par,"modifica","width=600,height=650,status=1,scrollbars=1")
}
function mod_img(par) {
window.open("ins_img.asp?id="+par,"modifica","width=600,height=650,status=1,scrollbars=1")
}
function gestionefoto(x) {
open('ins_foto1.asp?id='+x,'stampa','width=600,height=650,status=0,scrollbars=1')
}


function fullsize(x) {
if (x!="") {
open("schedafoto.asp?foto="+x,"schedafoto","width=10,height=10,status=0,scrollbars=1")
}
}


function popnews(x) {
open("popnews.asp?idnews="+x,"popnews","width=450,height=450,status=0")
}
   
function privacy() {
open("util/privacy.htm","privacy","width=450,height=250,status=0,scrollbars=0")
}

//function stampa() {
//window.print()
//}

function confronta_print() {
open("confronta_print.asp","confronta_print","width=650,height=500,status=0,scrollbars=1")
}

function confronta_print_riv() {
open("confronta_print_riv.asp","confronta_print","width=650,height=500,status=0,scrollbars=1")
}

function park(x) {
if (document.all || document.getElementById) {
x.innerHTML="<p style='margin-top: 2; margin-bottom: 0' onmouseover='showlay(1)' onmouseout='showlay(0)'><img src=img/ListatoAuto/parcheggioSi.gif border='0'></p>"
}
}


function showlay(n) {
x=document.body.scrollLeft + event.clientX
y=document.body.scrollTop + event.clientY
lay1.style.posLeft=x;
lay1.style.posTop=y;
if ((document.all || document.getElementById) && n==1) {
lay1.style.visibility="visible";
}
if ((document.all || document.getElementById) && n==0) {
lay1.style.visibility="hidden";
}
}

function showlay2(n) {
x=document.body.scrollLeft + event.clientX
y=document.body.scrollTop + event.clientY
lay2.style.posLeft=x;
lay2.style.posTop=y;
if ((document.all || document.getElementById) && n==1) {
lay2.style.visibility="visible";
}
if ((document.all || document.getElementById) && n==0) {
lay2.style.visibility="hidden";
}
}


function setprov(x) {
	df=document.forms[x]
	if (df.nazione.value=="Italia" || df.nazione.value=="Italy" || df.nazione.value=="ITALIA") {
	df.provincia.disabled = false
	df.citta.disabled = false
}
else {
	df.provincia.disabled = true
	df.citta.disabled = true
}
}

//////////////////////////////SCHEDA   

function stampascheda(par,par2,par3) {
if (par3==3) {
	if (par2==1) {
		open('print_admin_prev.asp?id_modello='+par+'&foto=1','stampaprev','width=650,height=600,status=0,scrollbars=1')
	}
	else if (par2==0) {
		open('print_admin_prev.asp?id_modello='+par+'&foto=0','stampaprev','width=650,height=600,status=0,scrollbars=1')
	}
}
else if (par3==2) {
	if (par2==1) {
		open('riv_scheda_stampa.asp?id_modello='+par+'&foto=1','stampa','width=650,height=600,status=0,scrollbars=1')
	}
	else if (par2==0) {
		open('riv_scheda_stampa.asp?id_modello='+par+'&foto=0','stampa','width=650,height=600,status=0,scrollbars=1')
	}
}
else if (par3==1) {
	if (par2==1) {
		open('scheda_stampa.asp?id_modello='+par+'&foto=1','stampa','width=650,height=600,status=0,scrollbars=1')
	}
	else if (par2==0) {
		open('scheda_stampa.asp?id_modello='+par+'&foto=0','stampa','width=650,height=600,status=0,scrollbars=1')
	}
}
}



function info(par) {
open('pop_info.asp?id_modello='+par,'info','width=500,height=545,status=1,scrollbars=1')
}
function prova(par) {
open('pop_prova.asp?id_modello='+par,'prova','width=500,height=555,status=0,scrollbars=1')
}
function noleggio(par) {
open('pop_noleggio.asp?id_modello='+par,'noleggio','width=500,height=545,status=0,scrollbars=0')
}
function permuta(par) {
open('pop_permuta.asp?id_modello='+par,'permuta','width=500,height=500,status=0,scrollbars=1')
}
function finanziamento(par) {
open('pop_finanziamento.asp?id_modello='+par,'finanziamento','width=500,height=500,status=0,scrollbars=1')
}
function inviamico(par) {
open('pop_inviamico.asp?id='+par,'inviamico','width=500,height=450,status=0,scrollbars=1')
}
function confronto(id,modello) {
open('confronto.asp?id_modello='+id+'&modello='+modello,'conronto','width=650,height=535,status=0,scrollbars=0')
}
function pop_foto(x) {
open("img_scheda.asp?id="+x,"img_foto","width=580,height=610,status=0,scrollbars=0")
}

function parkscheda() {
if (document.all || document.getElementById) {
parcheggio.innerHTML="<span style='color:cccc00;padding-left:5px'>Veicolo parcheggiato</span>"
}
}


function info_prodotto(par) {
open('pop_prodottiinfo.asp?id_modello='+par,'info_prodotto','width=500,height=545,status=1,scrollbars=1')
}


/////////////////////////////////////////////////////////////////////////
function ctrlform(nomeform) {
df=document.forms[nomeform];
//alert(nomeform)
if (nomeform=="richiesta") {
	if (df.elements["modello"].value=="") {
	alert('Attenzione: modello non inserito')
	return false
	}
}
if (nomeform=="rivenditori") {
	if (df.elements["azienda"].value=="") {
	alert('Attenzione: nome azienda non inserito')
	return false
	}
	if (df.elements["piva"].value=="") {
	alert('Attenzione: P.IVA non inserita')
	return false
	}
}
if (nomeform=="contatti" || nomeform=="richiesta") {
	if (df.elements["nome"].value=="") {
	alert('Attenzione: Nome non inserito')
	return false
	}
	if (df.elements["cognome"].value=="") {
	alert('Attenzione: Cognome non inserito')
	return false
	}
}
if (nomeform=="contatti") {
	if (df.elements["citta"].value=="" || df.elements["citta"].value=="0") {
	alert('Attenzione: Citta\' non inserita')
	return false
	}
}
if (df.elements["tel"].value=="") {
alert('Attenzione: Numero di telefono non inserito')
return false
}
if (isNaN(df.elements["tel"].value)) {
alert('Attenzione: Numero di telefono non valido')
return false
}
if (df.elements["mail"].value=="") {
alert('Attenzione: eMail non inserita')
return false
}
if (df.elements["mail"].value.indexOf("@")==-1) {
alert('Attenzione: eMail non valida')
return false
}
if (nomeform=="rivenditori") {
	if ((df.elements["passw"].value=="") || (df.elements["passw"].value.length<6) || (df.elements["passw"].value.length>10)) {
	alert('Attenzione: password non valida')
	return false
	}
}
if (nomeform=="formnewsletter") {
	if (df.elements[0].value=="") {
	alert('Attenzione: campo \"OGGETTO\" vuoto.');
	return false
	}
	if (df.elements[1].value=="") {
	alert('Attenzione: campo \"TESTO\" vuoto.');
	return false
	}
}
if (!df.elements["privacy"].checked) {
alert('Attenzione: e\' necessario autorizzare il trattamento dei dati personali');
return false
}
}


function control_chk(nomeform) {
df=document.forms[nomeform];
if (!df.elements["privacy"].checked) {
alert('Attenzione: disabilitando questo campo la richiesta non verra\' inviata')
}
}


//controlla se sessione iscritto attiva, in caso negativo appare il div
function controlcarrellosession(x,p) {
if (x==0) {
//alert("nn log")
divdisplay(0,'alert_sfondo',0);
divdisplay(0,'alert',0);
return false
}
else {
location.href=p
}
}


function delcarrello(x) {
if (confirm("Eliminare l'elemento selezionato?")) {
location.replace("areapriv_carrelli.asp?delcarrello="+x)
}
}


//aumenta o diminuisce qta del carrello
function kartaddqta(q,inpt,idprod,prz) {
	//alert("yy")
	//divtest=document.getElementById("divtest")
	//hidcarrellototale=document.getElementById("hidcarrellototale")
	//df=document.forms["carrello"]
	formqta=document.getElementById(inpt)
	//hidcarrellototale=document.getElementById("hidcarrellototale")
	//alert(hidcarrellototale.value)
	//alert(hidcarrellototale.value)
	if (q==1) {
	//formqta=document.getElementById(inpt)
	//newqta=parseInt(formqta.value)+1
	//formqta.value=newqta  //parseInt(formqta.value)+1 //df.qta.value+1
	//ajax2(1,idprod)
	//divcarrellototale.innerHTML=hidcarrellototale.value+prz
	//alert(idprod)
	//ajax3(1,idprod)
	newqta=1
	//new Ajax.Updater('divpagcarrello',"ajaxcarrello.asp?azione=1&qtaprod="+newqta+"&idprod="+idprod);
	getajax("ajaxcarrello.asp?azione=1&qtaprod="+newqta+"&idprod="+idprod,"divpagcarrello")
	}
	if (q==0) {
	//formqta=document.getElementById(inpt)
	if (parseInt(formqta.value)>1) {
		//newqta=parseInt(formqta.value)-1
		//formqta.value=newqta  //parseInt(formqta.value)-1 //df.qta.value+1
		//ajax3(-1,idprod)
		//divcarrellototale.innerHTML=hidcarrellototale.value-prz
		newqta=-1
		//new Ajax.Updater('divpagcarrello',"ajaxcarrello.asp?azione=1&qtaprod="+newqta+"&idprod="+idprod);
		getajax("ajaxcarrello.asp?azione=1&qtaprod="+newqta+"&idprod="+idprod,"divpagcarrello");
		}
	}
	//new Ajax.Updater('divnumprodottikart',"ajaxscripts.asp?aggiornanumprodottikart=1");
	getajax("ajaxscripts.asp?aggiornanumprodottikart=1","divnumprodottikart");
}

function canccarrelloprod(id) {
	if (confirm("Cancellare il prodotto selezionato?")) {
		//new Ajax.Updater('divpagcarrello',"ajaxcarrello.asp?azione=1&delprod="+id);
		getajax("ajaxcarrello.asp?azione=1&delprod="+id,"divpagcarrello");
		//new Ajax.Updater('divnumprodottikart',"ajaxscripts.asp?aggiornanumprodottikart=1");
		getajax("ajaxscripts.asp?aggiornanumprodottikart=1","divnumprodottikart");
	}
}
////////////////////////////////////////////////////////////////////////////////////////////

function messaggio() {
open("messaggio.asp","messaggio","width=400,height=400,status=0")
}

////////////////////////////////////////////////////////////////////////////////////

function linkhome(x) {
	if(x=="usatosicuro"){
	open("pop_usato.htm","_blank","width=500,height=400,scrollbars=1")
	}
	if(x=="assistenza"){
	open("pop_assistenza.htm","_blank","width=500,height=550,scrollbars=1")
	}
	if(x=="garanzia"){
	open("pop_garanzia.htm","_blank","width=550,height=600,scrollbars=1")
	}
	if(x=="domicilio"){
	open("pop_domicilio.htm","_blank","width=400,height=400,scrollbars=1")
	}
	if(x=="finanziamenti"){
	open("pop_finanziamenti.htm","_blank","width=500,height=600,scrollbars=1")
	}
}

/////////////////////////////////POP INS/MOD ADM//////////////////////////////////////////
function showdiv(par) {
if (document.all || document.getElementById) {
if (par==1) div1.style.visibility="visible";
if (par==0) div1.style.visibility="hidden";
}
}

function ins_options() {
ins_val="";
df1=document.forms["div-opts"]
for (i=0;i<df1.elements.length-2;i++) {
if (df1.elements[i].checked) {
ins_val=ins_val+df1.elements[i].value+"\n"
}
}
document.forms["form1"].elements["optionals"].value=ins_val
document.all.div1.style.visibility="hidden"
}

function popscheda(x) {
open("popscheda.asp?id="+x,"popscheda","width=350,height=300,status=0")
}
//////////////////////////////////////RIVENDITORI////////////////////////////////////////////
function del_rivenditore(x) {
if(confirm("Eliminare il rivenditore selezionato?")) {
location.href="gestione_rivenditori.asp?del=" + x
}
}
//////////////////////////////////////DATI AZIENDA///////////////////////////////////////////
function ins_aziendadati() {
new_w=window.open("gestione_aziendadati.asp","ins_aziendadati","width=550,height=590,status=1,scrollbars=1");
new_w.focus()
}
/////////////////////////////////////NEWSLETTER//////////////////////////////////////////////
function valid_f() {
df=document.forms[0];
if (df.elements[0].value=="") {
alert('Attenzione: campo \"OGGETTO\" vuoto.');
return false
}
if (df.elements[1].value=="") {
alert('Attenzione: campo \"TESTO\" vuoto.');
return false
}
}

function anteprima(x) {
open("nl_anteprima-txt.asp?txta="+x,"anteprima_txt","width=600,height=450,status=0,scrollbars=1")
}

function anteprima2(par,txta) {
for (i=0;i<=txta.length;i++) {
txta=txta.replace("'","\'")
txta=txta.replace('"','\"')
txta=txta.replace('\n','<br>')
}
window.open("nl_anteprima.asp?id="+par+"&txta="+txta,"anteprima","width=600,height=600,status=0,scrollbars=1")
}


function del_iscritti(x) {
df=document.forms[x]
if (!confirm("eliminare gli iscritti selezionati?"))  {
return false
}
else
{return true}
}

function nl_insmod(x) {
window.open("nl_insmod.asp?id="+x,"nl_insmod","width=550,height=400,status=0")
}
/////////////////////////////////////////////////////////////////////////////////////////////

function mostraordine(x) {
wo=open("areapriv_ordine.asp?idordine="+x,"ordine","width=600,height=600,scrollbars=1,status=0")
wo.focus()
}

//////////////////////////////////RICAMBI///////////////////////////////////////////
function prodottifullsize(x) {
if (x!="") {
open("schedaprodottifoto.asp?foto="+x,"schedaprodottifoto","width=10,height=10,status=0,scrollbars=1")
}
}


function scheda_stampa_prodotto(x) {
wo=open("scheda_stampa_prodotto.asp?idprod="+x,"scheda_stampa_prodotto","width=675,height=600,status=0,scrollbars=1")
wo.focus()
}


var ricambinstato=1

function mostraprodotti(nomediv,nst) {
//alert(nomediv.style.display)
//nomediv="subcats" + id
//nomediv=document.getElementById(ndiv);
if ((document.all || document.getElementById)) {
if (nomediv.style.display=="none") {
nomediv.style.display="inline";
}
else {
//alert(nst)
nomediv.style.display="none";
ricambinstato=1
//return nstato
}
}
}


function fullsizediv(ndiv,img,wid) {
//alert(ndiv)
imgw=wid/2
nomediv=document.getElementById(ndiv)
//sfondodiv=document.getElementById(bgdiv)
if (wid<900) {
	marginleftnumero=-(imgw+28)
	nomediv.style.marginLeft=marginleftnumero+"px"
}
else {
	nomediv.style.marginLeft="-476px"
}
image = document.getElementById('fullimg');
image.src=img   //'fotoprodotti/' + img
		if (nomediv.style.display="inline") {
			nomediv.style.display="inline";
			bg_fullsize.style.display="inline";
		}
		else {
			nomediv.style.display="none"
			bg_fullsize.style.display="none";
		}
}



function showdivajax(nomediv,url) {
	if (url=="") {
		//new Ajax.Updater(nomediv,"ajax.asp");
		getajax("ajax.asp",nomediv)
	}
	else {
		//new Ajax.Updater(nomediv,url);
		getajax(url,nomediv)
	}
}



function getajax(strurl,ndiv) {
	//alert(strurl)
                $.ajax({
                  type: "GET",
				  cache: false,
                  url: strurl,
                  success: function(msg){
                           $("#"+ndiv).html(msg);
                  }
                });
	}
	


function divdisplay(x,ndiv,n) {
	if (document.all || document.getElementById) {
		//alert(nomediv.style.display.value)
		nomediv=document.getElementById(ndiv)
		if (x==1) { 
			if (n==1) {
				nomediv.style.display="inline";
			}
			else {
				nomediv.style.display="none"
			}
		}
		else if (x==0) {
			if (nomediv.style.display!="inline") {
				nomediv.style.display="inline";
			}
			else {
				nomediv.style.display="none"
			}
		}
	}
}


function ajaxform(strurl,formdata,div1,div2) {
        //alert(formdata)

        $.ajax({
            //this is the php file that processes the data and send mail
            url: strurl, //"test.asp",
            //GET method is used
            type: "POST",
            //pass the data        
            data: formdata,    
            //Do not cache the page
            cache: false,
            //success
            success: function (html) {
				//alert(html)
                //if process.php returned 1/true (send mail success)
				//$("#textareatest").html(html);
                if (html==1) {       
                    //hide the form
					$("#divformerror").hide();
                    $('#'+div1).hide(); //$('#'+div1).fadeOut('slow'); 
					$('#divloading').hide();
                    //show the success message
                    $('#'+div2).fadeIn('fast');
					} 
				else if (html==2)
					{
						//alert(html)
						$("#divformerror2").show();
						$('#divloading').hide();
					}              
				else 
					{
						//alert(html)
						$("#divformerror").show();
						$('#divloading').hide();
					}              
            }      
        });
         
    //cancel the submit button default behaviours
    return false;
}



function del_elemento(id,url) {
	if (confirm("Eliminare l'elemento selezionato?")) {
		location.href=url + "delelemento=" + id
		}
	}
	


//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
	$(document).ready(function() {
							   
		$('#formprodottoinfosubmit').click(function() {
								  
			$("#formprodottoinfo").validate({
				//submitHandler:function(form) {
					//SubmittingForm();
				//},
				rules: {
					nominativo: "required",		
					tel: "required",		
					mail: {
						required:true, 
						email:true
					},		
					privacy: "required" 
				},
				messages: {
					nominativo: "",
					tel: "",
					mail: "",
					privacy: ""
				}

			});
			
			if ($("#formprodottoinfo").valid())	 {
				//alert("ok");
				var dati=$("#formprodottoinfo").serialize();
				//alert(dati);
				$('#formprodottoinfosubmit').hide();
				$('#divloading').show();
				ajaxform("ajaxscripts.asp?formprodottoinfo=1",dati,"scheda_richiedi_info","scheda_richiedi_info_submit")
				return false
			}
		});
		
		
		
		
		$('#formrecpasswsubmit').click(function() {
								  
			$("#formrecpassw").validate({
				//submitHandler:function(form) {
					//SubmittingForm();
				//},
				rules: {
					mail: {
						required:true, 
						email:true
					}
				},
				messages: {
					mail: ""
				}

			});
			
			if ($("#formrecpassw").valid())	 {
				//alert("ok");
				var dati=$("#formrecpassw").serialize();
				//alert(dati);
				//$('#formprodottoinfosubmit').hide();
				$('#divloading').show();
				ajaxform("ajaxscripts.asp?formformrecpassw=1",dati,"scheda_richiedi_info","divform_ok")
				return false
			}
		});
		
	});
	
//////////////////////////////////////////////////////////////////////
////////////////////////E-COMMERCE ADMIN//////////////////////////////


function mod_cat(lev,id,modcat,idcat,nomecat) {
location.href="mod_cat.asp?lev="+lev+"&id="+id+"&modcat="+modcat+"&idcat="+idcat+"&nomecat="+nomecat
}

function del_cat(lev,id,nomecat,idcat) {
if (confirm("Eliminare la categoria prodotti selezionata?\n\nATTENZIONE: verranno cancellate tutte le subcategorie collegate\n e i prodotti ivi contenuti.")) {
location.href="del_cat.asp?lev="+lev+"&id="+id+"&nomecat="+nomecat+"&idcat="+idcat
}
}

function del_prodotto(id,idcat) {
if (confirm("Cancellare il prodotto selezionato?")) {
location.href='gestione_servizi.asp?delprod='+id+'&idcat='+idcat
}
}

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////


