/*
QUESTA FUNZIONE CONTROLLA CHE SIA PRESENTE UN PARAMETRO DI RICERCA ED IN SEGUITO EFFETTUA IL SUBMIT
PARAMETRI: nessuno
VALORE DI RITORNO: nessuno
*/
function go_search()
{
	if (document.ric_libera.key.value != "")
		document.ric_libera.submit();
	else alert('Inserire un termine valido da ricercare');
	document.ric_libera.key.focus();
	return;
}

/*
Controlla la maxlength delle textarea
PARAMETRI: l'id della textarea, lunghezza massima
VALORE DI RITORNO: nessuno
*/
function imposeMaxLength(Object, MaxLen)
	{
  	return (Object.value.length <= MaxLen);
	}

/*
Svuota un campo dal contenuto (serve per i campi readonly)
PARAMETRI: nomeCampo
VALORE DI RITORNO: nessuno
*/
function SvuotaCampo(nomeCampo)
{
	document.getElementById(nomeCampo).value = "";
	return;
}
/*
SEMPLICE FUNZIONE CHE EFFETTUA IL REDIRECT VERSO UNA PAGINA
PARAMETRI: string url |contenente l'url di destinazione
VALORE DI RITORNO: nessuno
*/
function redirect(url)
	{
	window.location=url;
	}
	
function submitSenzaSalva(idForm, campoDiscriminante)
	{
	document.getElementById(campoDiscriminante).value = "nessuna"
	document.getElementById(idForm).submit()
	}

/*
SEMPLICE FUNZIONE CHE APRE UNA PICCOLA PAGINA DI POPUP PER IL CALENDARIO
PARAMETRI: string url |contenente l'url di destinazione
VALORE DI RITORNO: nessuno
*/
function OpenFrm(Link)
	{
	windowFrm=window.open(Link,"","toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=0,width=300,height=200");
	}
function Apri(Link)
	{
	windowFrm=window.open(Link,"","toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=0,width=350,height=200");
	}
function ApriGenerico(Link,larg,alt)
	{
	windowFrm=window.open(Link,"","toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,width="+larg+",height="+alt+"");
	}
	
/*
QUESTA FUNZIONE SI OCCUPA DI RENDERE VISIBILE O INVISIBILE UN QUALSIASI ELEMENTO HTML (DIV PER ESEMPIO.). 
IN AUTOMATICO RICAVA LO STATO ATTUALE DELL'ELEMENTO E LO MODIFICA DI CONSEGUENZA
PARAMETRI: string sDiscriminante |contenente il nome dell'elemento da processare
VALORE DI RITORNO: nessuno
*/
function ShowSez(sDiscriminante)	
	{
	var ie4=document.all;
	var ns6=document.getElementById&&!document.all;
	
	 if(ie4)
		{
		if (document.getElementById("rigaAree").style.display  == "none")
			{
			document.getElementById("rigaAree").style.display="block";
			document.getElementById("rigaInHome").style.display="block";
			}
		else
			{
			document.getElementById("rigaAree").style.display="none";
			document.getElementById("area").selectedIndex='0'
			document.getElementById("inHome").checked =false;
			document.getElementById("rigaInHome").style.display="none";
			if (document.getElementById("rigaPagine") != null)
				{
				document.getElementById("rigaPagine").style.display="none";
				}
			
			}
		}
	if(ns6)
		{
		if (document.getElementById("rigaAree").style.display  == "none")
			{
			document.getElementById("rigaAree").style.display="table-row";
			document.getElementById("rigaInHome").style.display="table-row";
			}
		else
			{
			document.getElementById("rigaAree").style.display="none";
			document.getElementById("area").selectedIndex='0'
			document.getElementById("inHome").checked =false;
			document.getElementById("rigaInHome").style.display="none";
			if (document.getElementById("rigaPagine") != null)
				{
				document.getElementById("rigaPagine").style.display="none";
				}
			
			}
		}
	}
	
function ShowAndHide2(sDiscriminante,action)	
	{
	var ie4=document.all;
	var ns6=document.getElementById&&!document.all;
	if(ie4)
		{
		if (action == "hide")
			{
			document.getElementById(sDiscriminante).style.display="none";
			}
		else
			{
			document.getElementById(sDiscriminante).style.display="block";
			}
		}
	if(ns6)
		{
		if (action == "hide")
			{
			document.getElementById(sDiscriminante).style.display="none";
			}
		else
			{
			document.getElementById(sDiscriminante).style.display="table-row";
			}
		}
	}
/*
QUESTA E' UNA FUNZIONE SPECIFICA PER FAR APPARIRE LE FORM PER ACQUISTARE
PARAMETRI: string sDiscriminante |contenente il nome della form da mostrare
VALORE DI RITORNO: nessuno
*/
function ShowForm(sDiscriminante)	
	{
	if (sDiscriminante == "kgmt")
		{
			antagonista = "conf"
			for (i=1;i<=2;i++)
				{
				document.getElementById(sDiscriminante+i).style.display="block";
				}
			for (i=1;i<=2;i++)
				{
				document.getElementById(antagonista+i).style.display="none";
				}
		}
    else
		{
			antagonista = "kgmt"
			for (i=1;i<=2;i++)
				{
				document.getElementById(sDiscriminante+i).style.display="block";
				}
			for (i=1;i<=2;i++)
				{
				document.getElementById(antagonista+i).style.display="none";
				}
     	}
		
 
	}
/*
QUESTA E' UNA FUNZIONE SELEZIONA TUTTI I CHECKBOX
*/	
function seleziona()
	{
	var totaleNews;
	totaleNews = document.getElementById("totaleNews").value;
	
	if (document.getElementById("selezionaTutto").checked == true)
		{
		for (i=1; i<totaleNews; i++) 
			{	
			box = eval("document.form.flag" + i); 
			if (box.checked == false) box.checked = true;
			}
		}
	else
		{
		for (i=1; i<totaleNews; i++) 
			{
			box = eval("document.form.flag" + i); 
			if (box.checked == true) box.checked = false;
			}
		}
	}
/*
QUESTA E' UNA FUNZIONE EFFETTUA LA CONVERSIONE E CAMBIA I VALORI NEI TEXTBOX
PARAMETRI: string myfield, event, bho
VALORE DI RITORNO: true o false
NB se ritorna false il carattere non viene scritto e nulla succede
*/	
function ConversioneValori(myfield, e, dec)
	{
	var key;
	var keychar;
	
	//ricavo la key premuta ed il codice relativo ad essa
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	//Se premo backspace entro qua
	if (key == 8)
		{
		//estraggo il contenuto in MT
		contenutoMt = document.getElementById("MT").value
		//visto che ho premuto il back devo eliminare l'ultimo carattere del contenuto MT
		contenutoMtAdesso = contenutoMt.substring(0,contenutoMt.length-1);
		//effettuo il calcolo
		qta = contenutoMtAdesso * document.getElementById("KGMT").value;
		//sostituisco il valore all'interno del box QTA arrotondato con 2 decimali
		document.getElementById("QTA").value = qta.toFixed(2);
	   	return true;
		}
		
	//Se premo un numero entro qua
	else if ((("0123456789.").indexOf(keychar) > -1))
		{
		//estraggo il contenuto in MT e lo concateno alla key appena premuta
		contenutoMtAdesso = document.getElementById("MT").value + keychar;
		//effettuo il calcolo
		valoreConvertito =contenutoMtAdesso * document.getElementById("KGMT").value;
		//se il risultato è numerico lo accetto senno lo setto a zero
		if (!isNaN(valoreConvertito))
			qta = valoreConvertito
		else
			qta = 0
		//sostituisco il valore all'interno del box QTA arrotondato con 2 decimali
		document.getElementById("QTA").value = qta.toFixed(2);
		return true;
		}	
	else
	   return false;
	}
	
function Show(sDiscriminante, e)	
	{
	var ie4=document.all;
	var ns6=document.getElementById&&!document.all;
	
	if(ie4)
		{
		x=event.clientX + document.body.scrollLeft + 20;
		y=event.clientY + document.body.scrollTop - 50; 
		}
	if(ns6)
		{
		x= e.clientX + window.pageXOffset;
		y= e.clientY + window.pageYOffset + 15;
		}
	document.getElementById(sDiscriminante).style.display="block";                         		
	document.getElementById(sDiscriminante).style.left = x;                                	
	document.getElementById(sDiscriminante).style.top = y;
	}	

/* this function hides the pop-up when user moves the mouse out of the link */
function Hide(sDiscriminante)
	{
	document.getElementById(sDiscriminante).style.display="none";
	}	
	
	
/*QUA DI SEGUITO CI SONO DELLE FUNZIONI AJAX PER POPOLARE DINAMICAMENTE LE FOMR SENZA RICARICARE LE PAGINE*/
function cercaModelli(marca) 
{
if (window.XMLHttpRequest) // branch for native XMLHttpRequest object
	{
	liveSearchReq3 = new XMLHttpRequest();
	liveSearchReq3.onreadystatechange = popolaModelli;
	liveSearchReq3.open("GET", "lstMod.asp?marca="+marca, true);
	liveSearchReq3.send(null);
	} 
else if (window.ActiveXObject)// branch for IE/Windows ActiveX version 
	{
	liveSearchReq3 = new ActiveXObject("Microsoft.XMLHTTP");
	if (liveSearchReq3)
		{
		liveSearchReq3.onreadystatechange = popolaModelli;
		liveSearchReq3.open("GET", "lstMod.asp?marca="+marca, true);
		liveSearchReq3.send();
		}
	}
}	

function popolaModelli() 
{   
var strRes3;       
var arrValori3;
if (liveSearchReq3.readyState == 4)
	{	  
	strRes3=liveSearchReq3.responseText;
	arrValori3=strRes3.split("||");
	
	document.form.modello.options.length=0;		    
	for(i=0;i<arrValori3.length;i++)
		{
		document.form.modello.options[document.form.modello.options.length]= new Option(arrValori3[i].split("|")[1],arrValori3[i].split("|")[0]); 
		}
	}
	
//cercaAllestimenti(0);
}	

function cercaAllestimenti(modello) 
{
if (window.XMLHttpRequest) // branch for native XMLHttpRequest object
	{
	liveSearchReq4 = new XMLHttpRequest();
	liveSearchReq4.onreadystatechange = popolaAllestimenti;
	liveSearchReq4.open("GET", "lstAlles.asp?modello="+modello,true);
	liveSearchReq4.send(null);
	
	} 
else if (window.ActiveXObject) // branch for IE/Windows ActiveX version
	{
	liveSearchReq4 = new ActiveXObject("Microsoft.XMLHTTP");
	if (liveSearchReq4) 
		{
		liveSearchReq4.onreadystatechange = popolaAllestimenti;
		liveSearchReq4.open("GET", "lstAlles.asp?modello="+modello,true);
		liveSearchReq4.send();
		}
	}
}

function popolaAllestimenti() 
{   
var strRes4;       
var arrValori4;      
if (liveSearchReq4.readyState == 4) 
	{	    
	strRes4=liveSearchReq4.responseText;
	arrValori4=strRes4.split("||");
	document.form.allestimento.options.length=0;		    
	for(i=0;i<arrValori4.length;i++)
		{
		document.form.allestimento.options[document.form.allestimento.options.length]= new Option(arrValori4[i].split("|")[1],arrValori4[i].split("|")[0]); 
		}
	}
}	
