
function del(url){
	if(confirm("Sicuro di voler eliminare l'oggetto selezionato?")){
		document.location=url;		
	}
}

function goto(url){
	document.location=url;		
}

function check_date(field,verbose){

if (typeof verbose == 'undefined' ) verbose = true;

var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var step = 0;
var err = 0;
var i;

   err = 0;
   DateValue = DateField.value;
   /* cancello tutti i dati tranne 0-9 */
   for (i = 0; i < DateValue.length; i++) {
              if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
                 DateTemp = DateTemp + DateValue.substr(i,1);
              }
   }
   DateValue = DateTemp;

  /* se l'anno � inserito con 2 valori lo cambio sempre con  20xx */

   if (DateValue.length == 6) {

      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }

   if (DateValue.length != 8) {

      err = 19;}

   /* anno sbagliato 0000 */

   year = DateValue.substr(4,4);

   if (year == 0) {

      err = 20;

   }

   /* validazione del mese*/

   month = DateValue.substr(2,2);

   if ((month < 1) || (month > 12)) {

      err = 21;

   }

   /* validazione del giorno */

   day = DateValue.substr(0,2);

   if (day < 1) {

     err = 22;

   }

   /* Validazione anno / febbraio / gg */

   if ((year % 4 == 0)) {

      step = 1;

   }

   if ((month == 2) && (step == 1) && (day > 29)) {

      err = 23;

   }

   if ((month == 2) && (step != 1) && (day > 28)) {

      err = 24;

   }

   /* validazione dei mesi */

   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {

      err = 25;

   }

   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {

      err = 26;

   }

   /* se � inserito 00 cancello i dati */

   if ((day == 0) && (month == 0) && (year == 00)) {

      err = 0; day = ""; month = ""; year = ""; seperator = "";

   }

   /* Se non ci sono errori inserisco la data (esempio 13.12.2001) */

   if (err == 0) {

      DateField.value = day + seperator + month + seperator + year;

   }

   /* Messaggio di errore, avviso di data corretta se err diverso da 0 */

   else {
    if(verbose){
        alert("Data non corretta. n\nI formati supportati sono:\n  - ggmmaa\n  - ggmmaaaa\n  - gg/mm/aa\n  - gg/mm/aaaa\n\n\n...e non tutti i mesi hanno piu' di 28 giorni!!!");
        DateField.select();
        DateField.focus();
    }else{
        return false;
    }

   }
    if(!verbose) return true;
}


function format_number(p,d) 
{
  var r;
  if(p<0){p=-p;r=format_number2(p,d);r="-"+r;}
  else   {r=format_number2(p,d);}
  return r;
}
function format_number2(pnumber,decimals) 
{
  var strNumber = new String(pnumber);
  var arrParts = strNumber.split('.');
  var intWholePart = parseInt(arrParts[0],10);
  var strResult = '';
  if (isNaN(intWholePart))
    intWholePart = '0';
  if(arrParts.length > 1)
  {
    var decDecimalPart = new String(arrParts[1]);
    var i = 0;
    var intZeroCount = 0;
     while ( i < String(arrParts[1]).length )
     {
       if( parseInt(String(arrParts[1]).charAt(i),10) == 0 )
       {
         intZeroCount += 1;
         i += 1;
       }
       else
         break;
    }
    decDecimalPart = parseInt(decDecimalPart,10)/Math.pow(10,parseInt(decDecimalPart.length-decimals-1)); 
    Math.round(decDecimalPart); 
    decDecimalPart = parseInt(decDecimalPart)/10; 
    decDecimalPart = Math.round(decDecimalPart); 

    //If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
    //then we need to add 1 to the 'intWholePart' and set the decDecimalPart to 0. 

    if(decDecimalPart==Math.pow(10, parseInt(decimals)))
    { 
      intWholePart+=1; 
      decDecimalPart="0"; 
    } 
    var stringOfZeros = new String('');
    i=0;
    if( decDecimalPart > 0 )
    {
      while( i < intZeroCount)
      {
        stringOfZeros += '0';
        i += 1;
      }
    }
    decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart); 
    var dot = decDecimalPart.indexOf('.');
    if(dot == -1)
    {
      decDecimalPart += '.'; 
      dot = decDecimalPart.indexOf('.'); 
    } 
    var l=parseInt(dot)+parseInt(decimals); 
    while(decDecimalPart.length <= l) 
    {
      decDecimalPart += '0'; 
    }
    strResult = decDecimalPart;
  }
  else
  {
    var dot; 
    var decDecimalPart = new String(intWholePart); 

    decDecimalPart += '.'; 
    dot = decDecimalPart.indexOf('.'); 
    var l=parseInt(dot)+parseInt(decimals); 
    while(decDecimalPart.length <= l) 
    {
      decDecimalPart += '0'; 
    }
    strResult = decDecimalPart;
  }
  return strResult;
}



// A utility function that returns true if a string contains only 
// whitespace characters.
function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

//-----------------------------------------------------------------
// function VERIFY(f)
//
//  ----------!!!  ATTENZIONE COI CHECKBOX forse non funziona!!!!!------------------
//
// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.

//esempio di FORM per capire come usare questo validatore:

/*
<FORM onSubmit="
    this.firstname.optional = true;   'notare questi attributi: escludono il campo dalla verifica
    this.phonenumber.optional = true;
    this.zip.min = 0;
    this.zip.max = 99999;
    return validate_fields(this);
">

First name: <INPUT TYPE=text NAME="firstname">
Last name: <INPUT TYPE=text NAME="lastname"><BR>
Address:<BR><TEXTAREA NAME="address" ROWS=4 COLS=40></TEXTAREA><BR>
Zip Code: <INPUT TYPE=text NAME="zip"><BR>
Phone Number: <INPUT TYPE=text NAME="phonenumber"><BR>
<INPUT TYPE=submit>
</FORM>
*/

function validate_fields(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";
   
   
    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        
        var e = f.elements[i];
        e.style.backgroundColor=""; //resetta il background come da CSS
        
        // era: 
        // if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
        // ma a noi interessano TUTTI i campi tranne "submit" e "button", non solo quelli textarea e text
        // di contro, se un campo � stato ESPLICITAMENTE settato come invisibile , non va controllato
        
       // if (!e.optional && (e.style.visibility =="visible" || e.style.visibility =="") ) {
            
       if (!e.optional ) {
            
            //non si controllano i pulsanti e i campi nascosti
            if (e.type == "submit" ) continue;
            if (e.type == "button" ) continue;
            if (e.type == "hidden" ) continue;
            if (e.name == "scheda-rilievo" ) continue;
            
            // vewrifica se il campo ha un parent non visibile (visibility=hidden o display=none)
            // in questo caso non controlla il campo      
                
            var nascosto =false;
            var par =e; 
            
            //risale la gerarchia DOM fino all'elemento pi� alto (HTML)
            while (par.tagName!="HTML") {
                
                 if (par.style.visibility=="hidden" || par.style.display=="none") nascosto=true;
                 par=par.parentNode;
            }
           
            if (nascosto==true) continue;
                         
            
            // first check if the field is empty
           
            // se il campo � vuoto,  riempie la stringa empty_fields (opzionalmente
            // ci mette anche il valore TITLE del campo, che pu� essere pi� descrittivo),
            // e cambia il colore dello sfondo del campo per evidenziarlo
            // altrimenti annulla il colore dello sfondo (torna come come da CSS)
            // ATTENZIONE: questa parte della colorazione potrebbe essere poco adatta in certe pagine
            // se � il caso, modificare la funzione per renderla opzionale.
             
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n         " + e.name 
                if (e.title) empty_fields += " (" + e.title + ")";
                e.style.backgroundColor="#D9D9FF";
                continue;
            } else {
            
              e.style.backgroundColor=""; //resetta il background come da CSS
            }

            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- Il campo " + e.name;
                    if (e.title) errors += " (" + e.title + ")";
                    errors += " deve essere un numero";
                    
                    if (e.min != null) 
                        errors += " e' maggiore di " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " e minore di " + e.max;
                    else if (e.max != null)
                        errors += " e' minore di " + e.max;
                    errors += ".\n";
                }
            }
        }
    }

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "Il modulo non e' stato salvato per i motivi descritti qui sotto.\n\n";
  
    if (empty_fields) {
        msg += "- I seguenti campi obbligatori non sono stati riempiti:\n" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    msg += "______________________________________________________\n\n"

    alert(msg);
    return false;
}


/**
*	@param title_to_append il testo da aggiungere al title del link
	@author Paolo Battino battino@ldpassociati.it
	@requires getElementsByClass(theClass)
	
note: serve per far aprire un link in una nuova finestra pur restando conformi a XHTML strict.
	Per far questo, il link deve essere identificato con la classe "target_blank".
	l'aggiunta di title_to_append server per avvertire l'utente dell'apertura della finestra. 
	Aggiungendo questa scritta alla fine del title si e' sicuri che appare solo in caso che
	lo script sia eseguito davvero.
	
	Modifica anche la visibilita' degli elementi di classe "ico_pop_up". Serve per far apparire le icone "apri in una nuova fienstra" in caso di js abilitato.
*/

function target_blank_solution(title_to_append) {

	if(document.getElementsByTagName) {
		
		var msg = title_to_append;
		var links = document.getElementsByTagName("a");
		for (i = 0; i < links.length; i++) {
			var link = links[i];
			if (link.className.indexOf("target_blank") != -1) {
				link.title += msg;
				var fn_click = function () {window.open(this.href); return false; }
				link.onclick = fn_click;
				var fn_key = function () {k = (e) ? e.keyCode : window.event.keyCode;
					if(k==13) {
					window.open(this.href);
					return false;
					}
				}
				
			}
		}
	}
	
}


function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
