function chkDate(oggi,limite_inf,limite_sup, msg_limite_inf, msg_limite_sup) {


if (Number(date2ISO(oggi)) < limite_inf)
	{
	window.alert(msg_limite_inf);
	return false;
	}

if (Number(date2ISO(oggi)) > limite_sup)
	{
	window.alert(msg_limite_sup);
	return false;
	}

return true
}


function chkDateInf(oggi, limite_inf, msg_limite_inf) {


if (Number(date2ISO(oggi)) < limite_inf)
	{
	window.alert(msg_limite_inf);
	return false;
	}

return true
}

//Cambia sezione
function quantianni(annop,annoa){
        //app=0;
        //for (i=annop;i!=annoa;i++){
        //        app++;}
        //return(app);
        return (annoa-annop)
}

function indietro() {
 history.go(-1)
}



function printPrev(formName,pageTo)
{
//alert(pageTo);
f = document.forms[formName];
openpage('','Risultati',700,500);
f.action='/contents/printresaltri_veicoli.cfm?page='+pageTo;
f.target='Risultati';
f.submit();
}


function menuSection(arg_list)
{
offset=0;
url="";
url_end="";

if (arguments[0].toString().toUpperCase()=="TOOLBAR")
        {
        //url_end="&toolbar=yes";
        //offset++;
        }

if (arguments[0].toString().toUpperCase()=="TOOLBAR_MINI")
        {
        //url_end="&toolbar_mini=yes";
        offset++;
        }


if (arguments.length==(1+offset))
        {
        id=queryString("section");
        url=url+arguments[0+offset];
        gotoSection(url+url_end);
        //gotoSection(url+"&section="+id+url_end);
	}
if (arguments.length>=(2+offset))
	{
        id=arguments[offset+0];
        if (id=='') id=queryString("section");
        //if (id!='') id="&section="+id;

        url=url+arguments[offset+1];
        if (arguments.length==(2+offset)) {gotoSection(url+id+url_end);return;}
	}
if (arguments.length==(3+offset))
	{
        fname=arguments[offset+2];
        gotoSection(url+id+url_end,'',fname);
        }

}

function setAnchor(pagina)
{
 if (pagina.search(/#/gi) != -1)
        {
          pagina=pagina.replace(/#(\w*)(&|$)/gi,"$2");
          pagina=pagina+"#"+RegExp.$1;
        }


return pagina;
}

//Per caricare qualsiasi sezione del sito
function gotoSection(pagina)
{
offset=0;
page="main.cfm?page="+arguments[0];

/* io dico che si può togliere...
if (arguments.length>=2 && arguments[1]=="WAIT")
	{
		offset=offset+1;
                page=setAnchor(page+"&wait=1");
	}

*/
//Controlla se è stato richiesto un form particolare

if (arguments.length >=(3+offset)){
	sFormName=(arguments[2+offset]!="" ? arguments[2+offset] : 0);
        meth=arguments[1+offset];
        if (meth!='')
                document.forms[sFormName].method=meth;
        document.forms[sFormName].action=setAnchor(page);
	document.forms[sFormName].submit();
        return;
}

// Nessun nome di form specificato
if (arguments.length==(1+offset) || arguments[1+offset]=="GET")
        location.href=setAnchor(page);
else{
	document.forms[0].method="POST";
        document.forms[0].action=setAnchor(page);
	document.forms[0].submit();
	}
}


function isalpha(n) {
  var nn=""+n;
  // Elimino segni di interpunzione o negativi, che sono ammessi
  nn = nn.replace(/\./gi, "");
  nn = nn.replace(/\,/gi, "");
  nn = nn.replace(/\-/gi, "");
  return ((nn.search(/[^0-9]/gi)!=-1) && nn.length>0);
}//endfunction


function undot(vv) {
  var vx=""+vv;
  if (typeof vv=='string')
    return (vx.replace(/\./gi, "").replace(/\,/gi, "."));
  else {
    if (isNaN(vv)) vv=0;
    return vv;
  }//end else
}//end function

function dot(name) {
  negative = "";
  decimal = "";
  var v=""+undot(name);
  // elimino l'eventuale segno negativo
  if (v.charAt(0) == "-") {
    v = v.substring(1, v.length);
    negative="-";
  }//end if
  // considero solo la parte intera
  decpos = v.search(/\./gi);
  if (decpos != -1) {
    decimal = ","+v.substring(decpos+1, v.length);
    v = v.substring(0, decpos);
    //alert("Posizione virgola: " + decpos + "\nParte intera: " + v + "\nParte decimale: "+decimal);
  }//end if
  var l=v.length-1;
  var vd=""
  var vd2=""

  for (i=0,d=1;i<=l;i++,d++) {
    if (d>3) {vd=vd+".";d=1;}
    vd=vd+v.substr(l-i,1);
  }//end for

  for (i=vd.length;i>=0;i--)
    vd2=vd2+vd.substr(i,1);
  return negative+vd2+decimal;
}//end function

function validateNum(numstr) {
    retstr = "";
    negative="";
    if (numstr.length == 0) {
      retstr = "0";
    }//end if
    else {
      // controllo se il numero dovrà essere negativo
      negative = numstr.charAt(0) == "-" ? "-" : "";
      // sostituisco le virgole con i punti
      retstr = numstr.replace(/,/gi, ".");
      // elimino caratteri estranei
      retstr = retstr.replace(/[^0-9.]/gi, "")
      // cerco il primo punto
      dotpos = retstr.search(/\./gi);
      switch (dotpos) {
        case -1:
          // non ci sono punti -> non faccio nulla
          // mi serve specificare questo caso
          // altrimenti finirebbe nel default
          break;
        case 0:
          // il punto è all'inizio -> aggiungo lo 0 davanti ed elimino gli altri punti
          retstr = "0."+retstr.replace(/\./gi,"");
          break;
        default:
          // il punto è in mezzo -> elimino gli altri punti
          substr1 = retstr.substr(0, dotpos+1);
          substr2 = (retstr.substring(dotpos+1, retstr.length)).replace(/\./gi,"");
          retstr = substr1+substr2;
      }//end switch
    }//end else
    retval = parseFloat(negative+retstr,10);
    if(!isNaN(retval)) return retval;
    else return 0;
  }//end function


// dato un numero reale ne norma la stringa in formato "EURO"
//-----------------------------------------------------------
function numToEuro(numero)
        {

        numero=validateNum(String(numero));

        //arrotondo
        numero=(Math.round(numero*100))/100;

        //intero
        intero=parseInt(numero);

        //reale
        decimals=Math.round((numero-intero)*100);
        if (decimals<10) decimals=decimals+'0';

        intero=dot(intero);

        return intero+","+decimals
        }





//--------------------------------------
// checkField()
//--------------------------------------
// Controlla che il campo non sia vuoto
function checkField(oField,sMessage,bSetFocus){
	var regexp = /^ *$/

	if(regexp.test(oField.value)){
		if (!regexp.test(sMessage)){
			alert(sMessage);
			if(bSetFocus){
				oField.focus();
			}
		}
		return 0;
	}

	return 1;
}

//--------------------------------------
// checkMail()
//--------------------------------------
// Controlla la correttezza sintattica
// della mail
function checkMail(oField){
	var nome,dominio2,dominio1;
	var sValue=oField.value;

	rxSplitMail = /^(.+)@(.+)\.(.+)$/;

	//Controlla la struttura del campo email
	if(!rxSplitMail.test(sValue)){
		return false
	}
	nome			= sValue.replace(rxSplitMail,"$1");
	dominio2	= sValue.replace(rxSplitMail,"$2");
	dominio1	= sValue.replace(rxSplitMail,"$3");

	rxChNonPermessi=/[àèéìòù\s@]+/;

	//---------------------------
	//Dominio di primo livello
	//---------------------------
	if(dominio1.length<2 || rxChNonPermessi.test(dominio1)){
		return false;
	}

	//---------------------------
	//Dominio di secondo livello
	//---------------------------
	if(dominio2.length<2 || rxChNonPermessi.test(dominio2)){
		return false;
	}

	//---------------------------
	//Nome
	//---------------------------
	if(nome.length<2 || rxChNonPermessi.test(nome)){
		return false;
	}

	// Non esistono errori
	return true;
}// end function

function cancella(objForm)
{
 objForm.reset();
}



function trim(myString)
{
myString=myString.replace( /^\s*(\.*)/ , '$1') ;
myString=myString.replace( /(\.*)\s*$/ , '$1') ;
return (myString)
}





/* -----------------------------------------------------------------------------
 * Torna un valore numerico valido (eventualmente decimale e negativo) data una
 * stringa di testo. Se la stringa non rappresenta un numero valido il valore di
 * ritorno sarà 0.
 *
 * @param numstr: stringa che rappresenta un numero;
 * @return:       valore numerico eventualmente decimale e negativo;
 * ----------------------------------------------------------------------------- */
function validateReal(numstr) {
  var retstr = "";
  var negative="";
  if (numstr.length == 0) {
    retstr = "0";
  }//end if
  else {
    // controllo se il numero dovrà essere negativo
    negative = numstr.charAt(0) == "-" ? "-" : "";
    // sostituisco i punti con il nulla
    retstr = numstr.replace(/\./gi, "");
    // elimino caratteri estranei
    retstr = retstr.replace(/[^0-9.,^,.]/gi, "");
    // cerco la prima virgola
    var dotpos = retstr.search(/,/gi);
    switch (dotpos) {
      case -1:
        // non ci sono virgole -> non faccio nulla
        // mi serve specificare questo caso
        // altrimenti finirebbe nel default
        break;
      case 0:
        // la virgola è all'inizio -> aggiungo lo 0 davanti ed elimino altre virgole
        retstr = "0."+retstr.replace(/,/gi,"");
        break;
      default:
        // la virgola è in mezzo -> elimino le altre virgole
        var substr1 = retstr.substr(0, dotpos);
        var substr2 = (retstr.substring(dotpos+1, retstr.length)).replace(/,/gi,"");
        retstr = substr1+'.'+substr2;

    }//end switch
  }//end else
  var retval = parseFloat(negative+retstr,10);
  if(!isNaN(retval)) return retval;
  else return 0;
}//end function


function getExt(filename)
{
ext=filename.substr(filename.length-3,3);
return (ext);
}

function isPictureFile(filename)
{
ext=getExt(filename).toUpperCase();
return (ext=="JPG" || ext=="GIF");
}



//--------------------------------
// formatDenaro()
//--------------------------------
function formatDenaro(n,dec)
{
	//arrotondo l'intero!
	// arriva giusto! alert(n);

	rf=Math.pow(10,dec);
	n=Math.round(n*rf)/rf;
	n_str=String(n);

	primavirgola=parseInt(n);
	vpos=n_str.search(/\./gi);
	if (vpos>=0)
		dopovirgola=n_str.substr(vpos+1,dec);
	else
		dopovirgola="";

	//alert("Intero:"+primavirgola+" decs :"+dopovirgola);

	//scritto: 0.xxxx
	var i=0;
	intero="";

	primavirgola	= String(primavirgola);
	//dopovirgola_str	= String(dopovirgola);

	intero=dot(primavirgola);
	if (dec>0 && dopovirgola.length>0)
		reale=","+dopovirgola;
	else
		reale=""

	denaro=intero+reale;

	return (denaro);
}


//--------------------------------
// eurocalc()
//--------------------------------
function eurocalc(lire)
{
euro = lire / 1936.27;
return euro;
}

// Ritorna il valore del parametro name
function queryString(name) {
  var i;
  query=location.search;
  query=query.substr(1);
  params=new Array();
  params=query.split("&");
  for (valore="",i=0;i<params.length;i++) {
    v=params[i];
    pos=v.indexOf("=");
    if (v.substring(0,pos)==name) {
      valore=unescape(v.substr(pos+1).replace(/\+/g, " "));
      break;
    }//end if
  }//end if
  return valore;
}//end function


function addPreferiti(title)
{
window.external.AddFavorite(location.href, title);
return true
}

function back()
{
history.go(-1);
}

function prePadNum(str, n) {
    returnstr = str;
    stringlen = str.length;
    if (stringlen < n) {
      topad = n - stringlen;
      for (var i = 0; i < topad; i ++) {
        returnstr = "0" + returnstr;
      }//end for
    }//end if
    return returnstr;
  }//end function




//--------------------------------------
// checkMail()
//--------------------------------------
// Controlla la correttezza sintattica
// della mail
function checkMail(oField){
	var nome,dominio2,dominio1;
	var sValue=oField.value;

	rxSplitMail = /^(.+)@(.+)\.(.+)$/;

	//Controlla la struttura del campo email
	if(!rxSplitMail.test(sValue)){
		return false
	}
	nome			= sValue.replace(rxSplitMail,"$1");
	dominio2	= sValue.replace(rxSplitMail,"$2");
	dominio1	= sValue.replace(rxSplitMail,"$3");

	rxChNonPermessi=/[àèéìòù\s@]+/;

	//---------------------------
	//Dominio di primo livello
	//---------------------------
	if(dominio1.length<2 || rxChNonPermessi.test(dominio1)){
		return false;
	}

	//---------------------------
	//Dominio di secondo livello
	//---------------------------
	if(dominio2.length<2 || rxChNonPermessi.test(dominio2)){
		return false;
	}

	//---------------------------
	//Nome
	//---------------------------
	if(nome.length<2 || rxChNonPermessi.test(nome)){
		return false;
	}

	// Non esistono errori
	return true;
}// end function



//--------------------------------------------------
//	CheckDate
//--------------------------------------------------
// Controlla la correttezza dei campi che contengono date
// [object] inField		- Campo da controllare
// string alertText	- Testo da utilizzare per l'alert box
// bool isMandatory	- Campo obbligatorio
// ritorna 1 su OK, 0 su error

function CheckDate(inField, alertText, isMandatory){

	if( (!isMandatory) && (inField.value<1))
		return 1;

	err=0;
	if (alertText.length<=0)
		alertText="Verifica la correttezza dei campi";

	strField=String(inField.value);

	if (isMandatory && (strField.length<=0)){
		alert('Attenzione! Il campo '+alertText+' deve essere inserito correttamente.');
		return err;
	}

	// Controlla la correttezza della data
	theArray=strField.split("/");
	if ((theArray.length!=3) || (theArray[0].length!=2) || (theArray[1].length!=2) || (theArray[2].length!=4)){
		alert("Attenzione! Il campo "+alertText+" non è corretto.");
		return err;
	}

	// Cerca caratteri non consentiti
	for(i=0; i<theArray.length; i++){
		if (theArray[i].search(/\D'/i)!=-1){
			alert("Attenzione! Il campo "+alertText+" contiene caratteri non validi.");
			return err;
		}
	}

	// Controlla validità della data
	arrMesi=new Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	arrMesi[2]+=Bisestile(theArray[2]);
	// Controlla il mese
	if ((1>theArray[1] || theArray[1]>12)){
			alert("Attenzione! Il mese del campo "+alertText+" non è valido.");
			return err;
	}

	// Controlla il giorno
	if ((1>theArray[0] || theArray[0]>arrMesi[theArray[1]/1])){
			alert("Attenzione! Il giorno del campo "+alertText+" non è valido.");
			return err;
	}

	return 1;
}


//Funzione per controllare se un anno è bisestile.
//Gli anni bisestili sono quelli divisibili per 4, ad eccezione di quelli divisibili per 100 e non per 400
// Si passa l'anno come argomento (year)
// Restituisce:
// 1 se è bisestile
// 0 se NON è bisestile

function Bisestile(year){
	return ( ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 1 : 0 );
}


function openpage(Page, Name, w, h)
{
	mywidth = w;
    myheight = h;
    x = (screen.width - mywidth)/2;
    y = (screen.height - myheight)/2;
    newwin = window.open(Page, Name, "scrollbars=yes,resizable=yes,left="+x+",top="+y+",width=" + w + ",height=" + h );
	newwin.creator=self;
	return newwin;
}



function date2ISO(strdate)
{
	var isodate;
if ((strdate.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})/gi))!=null)
				{
				isodate= "" + RegExp.$3 + RegExp.$2 + RegExp.$1 + "";
        return isodate;
        }
else
        return false;
}
