function lz(numero, cifre)
{
	n = String(numero);
	while (n.length<cifre)
	{ 
		n = "0" + n 
	}
	return n;
}

function CheckData(action)
{
	adesso = new Date();
	var aaaa = adesso.getFullYear();
	var mese = adesso.getMonth() + 1;
	var mm = lz(mese,2);

	if (document.usedmoto.U_Modello.value.length == 0)
	{
		alert("ATTENZIONE: Inserire il modello della moto.");
		document.usedmoto.U_Modello.focus();
		return ;
	}

	if (document.usedmoto.U_Colore.value.length == 0)
	{
		alert("ATTENZIONE: Inserire il colore della moto.");
		document.usedmoto.U_Colore.focus();
		return ;
	}
	
	if (document.usedmoto.U_Cilindrata.value.length == 0)
	{
		alert("ATTENZIONE: Inserire la cilindrata della moto.");
		document.usedmoto.U_Cilindrata.focus();
		return ;
	}
	
	if (ValidaNumeri(document.usedmoto.U_Cilindrata.value) == false)
	{
		alert("ATTENZIONE: Caratteri non validi.");
		document.usedmoto.U_Cilindrata.focus();
		return ;
	}

	if (document.usedmoto.U_Prezzo.value.length == 0)
	{
		alert("ATTENZIONE: Inserire il prezzo della moto.");
		document.usedmoto.U_Prezzo.focus();
		return ;
	}
	
	if (ValidaNumeri(document.usedmoto.U_Prezzo.value) == false)
	{
		alert("ATTENZIONE: Caratteri non validi.");
		document.usedmoto.U_Prezzo.focus();
		return ;
	}

	if (document.usedmoto.U_mese.value.length < 2)
	{
		alert("ATTENZIONE: Inserire il mese di immatricolazione ne formato mm.");
		document.usedmoto.U_mese.focus();
		return ;
	}
	
	if (document.usedmoto.U_mese.value == "00")
	{
		alert("ATTENZIONE: Inserire il mese di immatricolazione correttamente.");
		document.usedmoto.U_mese.focus();
		return ;
	}
	
	if (document.usedmoto.U_mese.value.substr() > 12)
	{
		alert("ATTENZIONE: Inserire il mese di immatricolazione correttamente.");
		document.usedmoto.U_mese.focus();
		return ;
	}
		
	if (ValidaNumeri(document.usedmoto.U_mese.value) == false)
	{
		alert("ATTENZIONE: Caratteri non validi.");
		document.usedmoto.U_mese.focus();
		return ;
	}
	
	if (document.usedmoto.U_anno.value.length < 4)
	{
		alert("ATTENZIONE: Inserire l'anno di immatricolazione nel formato aaaa.");
		document.usedmoto.U_anno.focus();
		return ;
	}
	
	if (ValidaNumeri(document.usedmoto.U_anno.value) == false)
	{
		alert("ATTENZIONE: Caratteri non validi.");
		document.usedmoto.U_anno.focus();
		return ;
	}
	
	var data1 = document.usedmoto.U_anno.value + document.usedmoto.U_mese.value
	var data2 = aaaa + mm
	
	d1=data1.substr() 
	d2=data2.substr()

	if (d1 > d2)
	{
		alert("ATTENZIONE: Data errata.");
		document.usedmoto.U_mese.focus();
		return ;
	}
	
	if (ValidaNumeri(document.usedmoto.U_My.value) == false)
	{
		alert("ATTENZIONE: Caratteri non validi.");
		document.usedmoto.U_My.focus();
		return ;
	}

	if (document.usedmoto.U_My.value.length < 4)
	{
		alert("ATTENZIONE: Inserire il model year della motonel formato aaaa.");
		document.usedmoto.U_My.focus();
		return ;
	}

	var data3 = document.usedmoto.U_My.value
	var data4 = aaaa + 1;
	
	d3 = data3.substr()

	if (d3 > data4)
	{
		alert("ATTENZIONE: Data errata.");
		document.usedmoto.U_My.focus();
		return ;
	}
	
	if (document.usedmoto.U_Km.value.length == 0)
	{
		alert("ATTENZIONE: Inserire i chilometri della moto.");
		document.usedmoto.U_Km.focus();
		return ;
	}

	if (ValidaNumeri(document.usedmoto.U_Km.value) == false)
	{
		alert("ATTENZIONE: Caratteri non validi.");
		document.usedmoto.U_Km.focus();
		return ;
	}
	
	
	if (document.usedmoto.U_Foto.value != ""){
	if (precarica(document.usedmoto.U_Foto.value) == false){
		alert("ATTENZIONE: Il file non è di tipo .JPG o .GIF, prova a selezionarne un altro.");
		document.usedmoto.U_Foto.focus();
		return ;
	}
	
	//if (misura() == false)
	//{
		//alert("ATTENZIONE: le dimensioni del file sono troppo grandi. La foto deve essere al massimo 500 x 350 pixel e non pesare più di 60kb.");
		//document.usedmoto.U_Foto.focus();
		//return ;
	//}
	}
	
	document.usedmoto.action = "gest_" + action + ".asp";
	document.usedmoto.submit();
}

function precarica(myFile)
{
	nomeFile = myFile;
	if (nomeFile == "") return;
	// isolo l'estensione
	extFile = nomeFile.substr(nomeFile.lastIndexOf(".")+1);
	extFile = extFile.toLowerCase();
	// verifico che sia fra quelle permesse
	permesse = "#jpg#jpe#jpeg#gif#";
	if (permesse.indexOf("#" + extFile + "#") == -1) {
		//alert(extFile + " non e' un file permesso");
		return false;
	}  
	//myImg.src = document.usedmoto.U_Foto.value; 
	return true;
}

var volte=0;
function misura()
{
	if (myImg.complete) {
		largo = myImg.width; 
		alto = myImg.height; 
		grosso = myImg.fileSize;
		if (largo > 500) {
			//alert(largo);
			return false;
		}
		if (alto > 350) {
			//alert(alto);
			return false;
		}
		if (grosso > 60000) {
			//alert(grosso);
			return false;
		}
	} else { 
		volte++;
		if (volte>10) {
			alert("Errore sul file: " + myImg.src)
		} else {
			window.setTimeout("misura()",100); 
		}
	}
}

function CheckConc()
{
	if (document.concessionario.C_Ragsoc.value.length == 0)
	{
		alert("ATTENZIONE: Inserire la ragione sociale.");
		document.concessionario.C_Ragsoc.focus();
		return ;
	}

	if (document.concessionario.C_Indirizzo.value.length == 0)
	{
		alert("ATTENZIONE: Inserire l'indirizzo.");
		document.concessionario.C_Indirizzo.focus();
		return ;
	}
	
	if (document.concessionario.C_Cap.value.length < 5)
	{
		alert("ATTENZIONE: Inserire il CAP.");
		document.concessionario.C_Cap.focus();
		return ;
	}
	
	if (ValidaNumeri(document.concessionario.C_Cap.value) == false)
	{
		alert("ATTENZIONE: Il CAP non è corretto. Inserire solo numeri.");
		document.concessionario.C_Cap.focus();
		return ;
	}

	if (document.concessionario.C_Citta.value.length == 0)
	{
		alert("ATTENZIONE: Inserire la città.");
		document.concessionario.C_Citta.focus();
		return ;
	}
	
	if (document.concessionario.C_Prov.value == "XX")
	{
		alert("ATTENZIONE: Inserire la provincia.");
		document.concessionario.C_Prov.focus();
		return ;
	}
	
	if (ValidaNumeri(document.concessionario.C_Tel.value) == false)
	{
		alert("ATTENZIONE: Il numero di telefono non è corretto. Inserire solo numeri.");
		document.concessionario.C_Tel.focus();
		return ;
	}
	
	if (document.concessionario.C_Tel.value.length == 0)
	{
		alert("ATTENZIONE: Inserire il numero di telefono.");
		document.concessionario.C_Tel.focus();
		return ;
	}
	
	if (ValidaNumeri(document.concessionario.C_Fax.value) == false)
	{
		alert("ATTENZIONE: Il numero di fax non è corretto. Inserire solo numeri.");
		document.concessionario.C_Fax.focus();
		return ;
	}
	
	if (document.concessionario.C_Fax.value.length == 0)
	{
		alert("ATTENZIONE: Inserire il numero di fax.");
		document.concessionario.C_Fax.focus();
		return ;
	}
			
	if (ValidaEmail(document.concessionario.C_Mail.value) == false)
	{
		alert("ATTENZIONE: L'indirizzo e-mail non è corretto. Prova a reinserirlo.");
		document.concessionario.C_Mail.focus();
		return ;
	}				

	if (document.concessionario.C_Mail.value.length == 0)
	{
		alert("ATTENZIONE: Inserire l'idirizzo e-mail.");
		document.concessionario.C_Mail.focus();
		return ;
	}
	
	if (document.concessionario.C_Referente.value.length == 0)
	{
		alert("ATTENZIONE: Inserire il nome della persona di riferimento.");
		document.concessionario.C_Referente.focus();
		return ;
	}
	
		document.concessionario.submit();
}

function CheckUsrPwd()
{
	if (document.usrpwd.C_Usr.value.length < 5)
	{
		alert("ATTENZIONE: Lo username deve essere di almeno 5 caratteri.");
		document.usrpwd.C_Usr.focus();
		return ;
	}

	if (document.usrpwd.C_Pwd.value.length < 5)
	{
		alert("ATTENZIONE: La password deve essere di almeno 5 caratteri.");
		document.usrpwd.C_Pwd.focus();
		return ;
	}
	
	if (document.usrpwd.C_ConfPwd.value.length == 0)
	{
		alert("ATTENZIONE: Confermare la nuova password.");
		document.usrpwd.C_ConfPwd.focus();
		return ;
	}
	
	if (document.usrpwd.C_ConfPwd.value != document.usrpwd.C_Pwd.value)
	{
		alert("ATTENZIONE: Riscrivere la nuova password correttamente.");
		document.usrpwd.C_ConfPwd.focus();
		return ;
	}
			document.usrpwd.submit();
}

function ValidaEmail(stringa)
{ 
  if (stringa=="")
	return(true)
  var carrattere=""
  var testo =0
  var vannobene = "abcdefghijklmnopqrstuvwxyz_@.ABCDEFGHIJKLMNOPQRSTUVWXYZƒŠŒšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789";
  var checkStr = stringa;
  var aposto = true;
  var chiocciola = 0
  var punto = 0
  for (i = 0;  i < checkStr.length;  i++)
  {
	 carattere = checkStr.charAt(i);
	 for (j = 0;  j < vannobene.length;  j++)
	 {
		if (carattere == vannobene.charAt(j))
		{
		  if (carattere=="@")
		  {
			 if (testo==0)
				aposto=false;
			 chiocciola = chiocciola +1;
			 testo = -1;
		  }
		  if (carattere==".")
		  {
			 if (testo==0)
				aposto=false;
			 punto= punto +1
			 testo = -1;
		  }
		  testo = testo + 1;
		  break;
		}

		if (j == vannobene.length)
		{
		  aposto = false;
		  break;
		}
	 }
   }
	if (!aposto || chiocciola !=1 || punto ==0 || testo < 2 || testo >3 )
   {
		return (false);
   }
return (true);
}

function ValidaNumeri(checkStr)
{ 
  if (checkStr=="")
	return(true)

  var carrattere="";
  var vannobene = "0123456789,";
  var bolAposto = true;
  var bolTrovato = false;

  for (i = 0;  i < checkStr.length;  i++)
  {
		carattere = checkStr.charAt(i);
		bolTrovato = false;

	    for (j = 0;  j < vannobene.length;  j++)
		{
			if (carattere == vannobene.charAt(j))
			{		
				bolTrovato = true;
				break;
			}		
		}
		
		if (!bolTrovato ) 
		{
			bolAposto = false;
			break;
		}
  }		

  if (!bolAposto )
  {
    return (false);
  }
return (true);
}

function cancella(selezionato)
{
	riempi();
	for (j = 0; j <= selezionato - 1; j++)  
	{
		document.U_Ricerca.U_Emax.options[0]=null;
	}
}

function riempi()
{	
	nObj=6
	for (i = 0; i < nObj; i++) 
{
	document.U_Ricerca.U_Emax.options[0]=null;
}
	NewOpt = new Array;
	
	NewOpt[0] = new Option("10.000 euro");	NewOpt[0].value = '10000';
	NewOpt[1] = new Option("15.000 euro");	NewOpt[1].value = '15000';
	NewOpt[2] = new Option("20.000 euro");	NewOpt[2].value = '20000';
	NewOpt[3] = new Option("25.000 euro");	NewOpt[3].value = '25000';
	NewOpt[4] = new Option("30.000 euro");	NewOpt[4].value = '30000';
	NewOpt[5] = new Option("qualsiasi prezzo");	NewOpt[5].value = '';
	for (i = 0; i < nObj; i++) 
	{
	document.U_Ricerca.U_Emax.options[i]=NewOpt[i];
	}
	numero=document.U_Ricerca.U_Emax.length-1
	document.U_Ricerca.U_Emax.options[numero].selected=true;
}

function FormCambia()
{
	document.U_Ricerca.action = "default.asp";
	document.U_Ricerca.submit(); 
	return false;
}

function FormInvia()
{
	document.U_Ricerca.action = "showused.asp";
	document.U_Ricerca.submit(); 
	return false;
}

function informazioni(id, head)
{
	newswin = window.open('informazioni.asp?headsel=' + head + '&usedid=' + id ,'info','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=470')
	newswin.focus();
}

function concessionario(id, head)
{
	newswin = window.open('concessionario.asp?headsel=' + head + '&concid=' + id ,'concessionario','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=315')
	newswin.focus();
}
function ingrandisci(id, head)
{
	newswin = window.open('foto.asp?headsel=' + head + '&usedid=' + id ,'foto','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=582,height=450')
	newswin.focus();
}

function CheckInfo()
{
	if (document.I_conta.I_nome.value.length == 0)
	{
		alert("ATTENZIONE: Inserire il nome");
		document.I_conta.I_nome.focus();
		return(false) ;
	}

	if (document.I_conta.I_cognome.value.length == 0)
	{
		alert("ATTENZIONE: Inserire il cognome");
		document.I_conta.I_cognome.focus();
		return(false) ;
	}
	
	if (ValidaEmail(document.I_conta.I_email.value) == false)
	{
		alert("ATTENZIONE: L'indirizzo E-mail non è corretto, provare a reinserirlo.");
		document.I_conta.I_email.focus();
		return(false) ;
	}

	if (document.I_conta.I_email.value.length == 0)
	{
		alert("ATTENZIONE: Inserire l'indirizzo E-mail");
		document.I_conta.I_email.focus();
		return(false) ;
	}
	
	if (ValidaNumeri(document.I_conta.I_telefono.value) == false)
	{
		alert("ATTENZIONE: Il numero di telefono non è corretto, provarlo a reinserirlo.");
		document.I_conta.I_telefono.focus();
		return(false) ;
	}
				
	if (document.I_conta.I_telefono.value.length == 0)
	{
		alert("ATTENZIONE: Inserire il numero di telefono");
		document.I_conta.I_telefono.focus();
		return(false) ;
	}
				
	return(true);
}

function controllafile(nome)
{
	if (nome == "")
		return(true);
	nome = nome.toLowerCase();
	lunghezza = nome.length;
	estensione = nome.substring(lunghezza-4,lunghezza);
	if ((estensione == ".jpg") || (estensione == ".gif") || (estensione == ".jpeg") || (estensione == ".jpe"))
		return(true);
	return(false);
}

window.onerror=null;
var win = null;

function PrivacyWindow(mypage, myname, w, h, scroll) {
closePopWin();

var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',noresize';

win = window.open(mypage, myname, winprops);
//if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function closePopWin() {
	if(win != null) { 
		if(!win.closed) { 
			win.close();
			win = null;
		} else {
			win = null;
		}
	}
}

function CheckModify()
{
	if (document.correction.U_Colore.value.length == 0)
	{
		alert("ATTENZIONE: Inserire il colore della moto.");
		document.correction.U_Colore.focus();
		return ;
	}
	document.correction.submit();
}

function CambiaUsed(pagina)
{
	document.usedmoto.action = "gest_" + pagina + ".asp";
	document.usedmoto.submit(); 
	return false;
}

function CambiaUsed_in()
{
	document.usedmoto.action = "gest_insert.asp";
	document.usedmoto.submit(); 
	return false;
}

//http://ricca2000/HDI/private

function Esempio(pagina,altezza,larghezza,scrolling)
{
	linkin = pagina + ".asp";
	Eswin = window.open( linkin ,'esempio','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + scrolling +',resizable=no,width=' + larghezza + ',height=' + altezza)
	Eswin.focus();
}

function CheckNewPhoto(){
	if (document.newPh.foto.value != ""){
		if (precarica(document.newPh.foto.value) == false){
			alert("ATTENZIONE: Il file non è di tipo .JPG o .GIF, prova a selezionarne un altro.");
			document.newPh.foto.focus();
			return(false);
		}
	
		//if (misura() == false){
		//	alert("ATTENZIONE: le dimensioni del file sono troppo grandi. La foto deve essere al massimo 500 x 350 pixel e non pesare più di 60kb.");
		//	document.newPh.foto.focus();
		//	return(false);
		//}
	} else if (document.newPh.foto.value.replace(/^ +/, "") == ""){
			alert("ATTENZIONE!\nInserire una foto al posto dell'attuale.");
			document.newPh.foto.focus();
			return(false);
		}
}

function precarica2()
{
	nomeFile = document.newPh.foto.value;
	if (nomeFile == "") return;
	// isolo l'estensione
	extFile = nomeFile.substr(nomeFile.lastIndexOf(".")+1);
	extFile = extFile.toLowerCase();
	// verifico che sia fra quelle permesse
	permesse = "#jpg#jpe#jpeg#gif#";
	if (permesse.indexOf("#" + extFile + "#") == -1) {
		//alert(extFile + " non e' un file permesso");
		return false;
	}  
	//myImg.src = nomeFile; 
	return true;
}

function OpenNewImg(nuovo, vecchio, marca, modello, id, conc){
	var sinistra = (screen.width - 520) / 2
	var alto	 = (screen.height - 450) / 2
	
	newPhoto = window.open('gest_judgePhoto.asp?img1=' + nuovo + '&img2=' + vecchio + '&brand=' + marca + '&model=' + modello + '&motoid=' + id + '&conc=' + conc, 'cambiaFoto', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=540,height=450,left=' + sinistra + ',top=' + alto);
	newPhoto.focus();
}

function CambiaStato(stato){
	if (stato == "1"){
		document.change.noMotivo.disabled = true;
		document.change.noMotivo.value = "Campo disabilitato perchè non necessario.";
	} else if (stato == "0"){
		document.change.noMotivo.disabled = false;
		document.change.noMotivo.value = "";
	}
}

function CheckMotivo(){
	if (document.change.modifica.value = "N"){
		if (document.change.noMotivo.value.replace(/^ +/, "") == ""){
			alert("ATTENZIONE!\nMotivare la scelta di non accettare la modifica della foto per questa moto.");
			document.change.noMotivo.focus();
			return(false);
		}
	}	
}
