// JavaScript Document

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, '') }

<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->

	function JWM_swapImage() {
	  a = JWM_swapImage.arguments;
	  document.JWM_tmp = new Array;
	  j=0;
	  for (i=0;i<(a.length-1);i+=2) {
	    x = document.getElementById(a[i]);
	    document.JWM_tmp[j++] = x;
      if(!x.oSrc) x.oSrc=x.src;
      x.src = a[i+1];
	  }
	}

	function JWM_swapImgRestore() {
    a = document.JWM_tmp;
    for (i=0;a&&i<a.length&&a[i].oSrc;i++)
      a[i].src = a[i].oSrc;
	}

//select senza input vai link automatico sulla selezione della voce
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

//aggiunge il parametro specificato alla querystring
function addParameter(nome,valore) {
  for (var l=0; l<document.links.length; l++) {
    var abslink = document.links[l].href.indexOf("http://");
    var intlink = document.links[l].href.indexOf("#");
    var jslink = document.links[l].href.indexOf("javascript:");  //forse va trattato!!!!
    var jwmlink = document.links[l].href.indexOf("http://");   //forse è meglio aggiungere il nome dell'applicazione
    var isjsp = (document.links[l].href.indexOf(".jsp")!=-1)?true:false;
    var separatore = (document.links[l].href.indexOf("?")!=-1)?"&":"?";

    if (intlink==-1 && jslink==-1 && jwmlink!=-1 && isjsp)
      document.links[l].href += separatore+nome+"="+valore;
  }
}

//ritorna undefined se il parametro non è definito
function getParameter(nome) {

   var url = window.location.href;
   var paramsStart = url.indexOf("?");
   if (paramsStart != -1) {
      var paramString = url.substr(paramsStart + 1);
      var tokenStart = paramString.indexOf(nome);

      if (tokenStart != -1) {
         paramToEnd = paramString.substr(tokenStart + nome.length + 1);
         var delimiterPos = paramToEnd.indexOf("&");

         if (delimiterPos == -1) {
            return paramToEnd;
         } else {
            return paramToEnd.substr(0, delimiterPos);
         }
      }
   }
}

function getParameters() {
   var params = new Array();
   var url = window.location.href;
   var paramsStart = url.indexOf("?");
   var hasMoreParams = true;

   if (paramsStart != -1) {
     var paramString = url.substr(paramsStart + 1);
     var params = paramString.split("&");
     for (var i = 0 ; i < params.length ; i++) {
       var pairArray = params[i].split("=");
       if (pairArray.length == 2) {
         params[pairArray[0]] = pairArray[1];
       }
     }
     return params;
   }
}


function getAttributo(el,attr,isselect) {
  retval = document.getElementById(el)

  if (retval) {
    if (isselect)
      return eval("retval.options[retval.selectedIndex]."+attr)
    else
      return eval("retval."+attr)
  }
}

function vai(desturl,repl) {
  if (desturl && desturl!="") {
    if (repl)
      document.location.replace(desturl)
    else
      document.location = desturl
  }
}


function cerca(form,id,error) {
    if (trim(document.getElementById(id).value) == '' ) {
       alert(error);
			 return false;
    } else {
			document.forms[form].submit();
			return true;
    }
}

function trim(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

//stampa la data in formato gg/mm/aaaa
//o mm/gg/aaaa se passiamo 'eng'
function printUltimaModifica(lng) {
  var mydate = new Date(document.lastModified);
  var g = mydate.getDate();
  var gg = new String(g);
  gg = gg.length==1?"0"+g:g;
  var m = mydate.getMonth();
  var mm = new String(m);
  mm = mm.length==1?"0"+(m+1):(m+1);
  var aaaa = mydate.getFullYear();
  if (lng=="eng")
    document.write(mm+ "/" + gg + "/" + aaaa)
  else
    document.write(gg+ "/" + mm + "/" + aaaa)

}

//funzione per "nascondere" un layer
function hideLayers() {
  //compatibilità: Netscape 6.x, Explorer 5 e 6, Opera 5 e 6, Mozilla
  a = hideLayers.arguments;
  for (i=0;i<a.length;i++) {
    if (document.getElementById(a[i])) {
      //document.getElementById(a[i]).style.visibility="hidden";
			if (document.getElementById(a[i]))
				document.getElementById(a[i]).style.display="none";
    }
  }
}

//rende visibile un layer
function showLayers() {
  //compatibilità: Netscape 6.x, Explorer 5 e 6, Opera 5 e 6, Mozilla
  a = showLayers.arguments;
  for (i=0;i<a.length;i++) {
    if (document.getElementById(a[i])) {
      //document.getElementById(a[i]).style.visibility="visible";
			if (document.getElementById(a[i]))
				document.getElementById(a[i]).style.display="block";
    }
  }
}

//controlla che il testo inserito nel campo sia un numero
function isNumber(idcampo) {
  campo = document.getElementById(idcampo);
  if (isNaN(campo.value) && campo.value!="") {
    alert("valore numerico !!");
    campo.value = 0
  }
}

//controlla che il testo inserito sia un indirizzo email formalmente valido
function isEmail(idcampo) {
  campo = document.getElementById(idcampo);
  var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
  var val = campo.value
  var ok = val.search(email_reg_exp);
  if( ok == -1 && val != "" ) {
    alert("Email non valida");
    campo.value = "";
    campo.focus();
    return false;
  } else return true;
}

//controlla che il il testo inserito sia nel formato gg/mm/aaaa
function checkItaDate(idcampo) {
  campo = document.getElementById(idcampo);

  //regular expression per il formato della data (gg/mm/aaaa)
  var dateIta_reg_exp = /^((3[01])|([12][0-9])|(0[1-9]))[\/]((1[0-2])|(0[1-9]))[\/]((19|20)\d{2})$/;

  var val= campo.value;
  var error= 0;
  if(val!="") {
    var giorno= val.substr(0, 2);
    var mese= val.substr(3, 2) - 1; //in js i mesi partono da 0
    var anno= val.substr(6, 4);
    var ok= val.search(dateIta_reg_exp);
    if(ok==-1 ) {
      error= 1;
    } else {
      //controllo sui mesi che non hanno 31 giorni e sull'anno bisestile
      var mese_tmp = mese.toString();
      if( mese_tmp.substr(0, 1) == 0 ) { mese_tmp = mese_tmp.substr(1,1) } //tolgo l'eventuale 0 davanti al mese
      gm= new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
      if ( ( ( anno % 4 == 0 ) && ( anno % 100 != 0 ) ) || ( anno % 400 == 0 ) ) { gm[1] = 29; } //anno bisestile
      if (giorno>gm[mese_tmp]) { error = 1; } //30 giorni a novembre con april......
    }
  }
  if( error == 1 ) {
    alert("Data non valida");
    campo.value= "";
  }

//simo per gestione apri chiudi form si ricerca
function nobox(nome) {
document.getElementById(nome).style.display='none'
}
function box(nome) {
document.getElementById(nome).style.display='block'
}
}
//MAPPA
function updateLink(link) {
window.location.href = link;
}



//GESTIONE MENU A TENDINA
<!--
window.onload=montre;
function montre(id) {
var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
if (d) {d.style.display='block';}
}
//-->


//ritorna true se una variabile o una funzione è definita
function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}



function openMenuItem(idLivello) {

	if (document.getElementById('level_'+idLivello).className == "voce_categoria menuItemOpen")
	{
		if (document.getElementById('sublevel_'+idLivello))
			document.getElementById('sublevel_'+idLivello).style.display = "none";

		document.getElementById('level_'+idLivello).className = "voce_categoria";
	}
	else
	{
		if (document.getElementById('sublevel_'+idLivello))
			document.getElementById('sublevel_'+idLivello).style.display = "block";

		document.getElementById('level_'+idLivello).className = "voce_categoria menuItemOpen";
	}

}

function closeMenuItem(idLivello) {

	if (document.getElementById('sublevel_'+idLivello))
		document.getElementById('sublevel_'+idLivello).style.display = "none";

	if (document.getElementById('level_'+idLivello))
		document.getElementById('level_'+idLivello).className = "voce_categoria";

}






	function doCatalogSearch() {

		if (document.getElementById('idFinituraValoreDefaultSearch').value == '')
		{
			// TODO: resource bundle
			alert("Nessuna caratteristica selezionata")
		}
		else
		{
			document.forms['searchProd'].submit();
		}

	}

	function finestra(pagina,w,h,scroll,resize) {
  if (scroll=="") scroll="no"
  if (resize=="") resize="yes"
  window.open(pagina,'','width='+w+',height='+h+',scrollbars='+scroll+',resizable='+resize)
}



function isFilled(_form,_el) {

  var filled = false;

  for (var i=0;_form.elements[i];i++)
  {
    var campo = _form.elements[i];
    if (campo && campo.name == _el)
    {
      switch (campo.type) {
	case "select-one":
          if (campo.selectedIndex!=-1 && campo.options[campo.selectedIndex].value!="") {
            filled = true;
          }
	  break;
        case "select-multiple":
          for (var x=0; x < campo.length; x++)
          {
            if (campo[x].selected == true)
            {
              filled = true;
              break;
            }
          }
	  break;
	case "radio":
          if (campo.checked) return true;
          break;
	case "checkbox":
          if (campo.checked) return true;
	  break;
/*
case "file":
case "text":
case "textarea":
*/
	default:
	  //if (campo.value!="") {
          if (trim(campo.value)!="") {
            filled = true;
          }
	  break;
      }
    }
  }

  return filled;
}


//funzione per il controllo lato client sui campi obbligatori
// - da aggiungere nell'attributo 'onsubmit' della form
// - richiede un campo hidden con id='requiredfields' con
//   l'elenco dei nomi dei campi obbligatori separati da virgola (AND)
// - supporta anche parentesi e || per creare diverse combinazioni di campi
//   obbligatori
// - se viene specificato un campo hidden con id='requiredfieldsmsg',
//   questo viene visualizzato in caso non siano compilati tutti i
//   campi obbligatori.
function validateForm(_form) {
  var reqfields = document.getElementById('requiredfields');
  var reqfieldsmsg = document.getElementById('requiredfieldsmsg');

  if (reqfields && (trim(reqfields.value)).length>0) {

    var rf = reqfields.value;

    var re = new RegExp(",","gi");
    rf = rf.replace(re,'&&');
    re = new RegExp("\s","gi");
    var rf2 = rf.split(/[\s&&\(\)||]/gi);

    for (var i=0;i<rf2.length;i++)
		{
      var token = trim(rf2[i]);
      if (token.length>0) {
        var valcampo = false;
        if (isFilled(_form,token))
          valcampo = true;

        rf = rf.replace(token,valcampo)
      }
    }

    re = null;
    rf2 = null;

    var err = false;
    eval("if(!("+rf+")) err=true")

    if (err)
    {
      alert(reqfieldsmsg && reqfieldsmsg.value != '' ? reqfieldsmsg.value : "ATTENZIONE: Per procedere e' necessario compilare tutti i campi obbligatori");

      return false;
    }
    else
    {
      return true;
    }

  } //end if ci sono campi obbligatori

  return true;
}







