function OnlyNumeric(evt) { 
	var keyCode = (window.event) ? event.keyCode : evt.which;
	if ((keyCode < 48 || keyCode > 57) && keyCode !=8 && keyCode !=0) {
		if (window.event)
			event.returnValue = false;
	 	else
	 		evt.preventDefault();
	}
}
function OnlyDecimal(evt,obj) { 
	//Sono ammessi solo caratteri numerici e "."
	var keyCode = (window.event) ? event.keyCode : evt.which;
	if ((keyCode < 48 || keyCode > 57) && keyCode != 46 && keyCode !=8 && keyCode !=0) {
		if (window.event)
			event.returnValue = false;
	 	else
	 		evt.preventDefault();
	}
	else{
		//se l'oggetto è passato alla funzione.
		//viene effettuato il controllo per evitare l'inserimento di più di un "."
		if(obj!=undefined){
			existPoint = obj.value.split(".")
			if((existPoint.length >1) && (keyCode == 46)) {
				if (window.event)
					event.returnValue = false;
	 			else
	 				evt.preventDefault();
			}
		}
	}
}

function validDate(fld) {
    var testMo, testDay, testYr, inpMo, inpDay, inpYr, msg
    var inp = fld.value
	
	if (inp=="") return true
	if (inp.length<10) {
		msg = "Formato data non valido. Inserire la data nel formato 'gg/mm/aaaa'"
	}
    // extract components of input data
    inpDay = parseInt(inp.substring(0, inp.indexOf("/")), 10)
    inpMo = parseInt(inp.substring((inp.indexOf("/") + 1), inp.lastIndexOf("/")), 10)
    inpYr = parseInt(inp.substring((inp.lastIndexOf("/") + 1), inp.length), 10)
	
    // attempt to create date object from input data
	//devo cambiare il formato in mm/dd/yyyy
    //var testDate = new Date(inp)
	var testDate = new Date(inpMo + "/" + inpDay + "/" + inpYr)
    // extract pieces from date object
    testMo = testDate.getMonth() + 1
    testDay = testDate.getDate()
    testYr = testDate.getFullYear()
    // make sure parseInt() succeeded on input components

    if (isNaN(inpMo) || isNaN(inpDay) || isNaN(inpYr)) {
         msg = "Formato data non valido. Inserire la data nel formato 'gg/mm/aaaa'"
    }
    // make sure conversion to date object succeeded
    if (isNaN(testMo) || isNaN(testDay) || isNaN(testYr)) {
         msg = "Formato data non valido. Inserire la data nel formato 'gg/mm/aaaa'"
    }
    // make sure values match
    if (testMo != inpMo || testDay != inpDay || testYr != inpYr) {
         msg = "Formato data non valido. Inserire la data nel formato 'gg/mm/aaaa'"
    }
    if (msg) {
         // there's a message, so something failed
         alert(msg)
         // work around IE timing problem with alert by
         // invoking a focus/select function through setTimeout();
         // must pass along reference of fld (as string)
         //setTimeout("doSelection(document.forms['" + 
         // fld.form.name + "'].elements['" + fld.name + "'])", 0)
		 fld.focus();
         return false
    } else {
         // everything's OK; if browser supports new date method,
         // show just date string in status bar
         //status = (testDate.toLocaleDateString) ? testDate.toLocaleDateString() : "Date OK"
         return true
    }
}

function check_email(email) {
/*
LEGENDA DEGLI ERRORI:

1) La chiocciola e' presente: come primo o ultimo carattere o ne sono state digitate piu' di una;
2) L'e-mail contiene uno o piu' caratteri non ammessi contenuti nella variabile nochar;
3) Il punto e' presente: come primo, ultimo o penultimo carattere, prima o dopo la chiocciola;
4) Ci sono 2 punti (..) oppure due trattini (--) vicini;
5) Non c'e' nessun punto dopo la chiocciola
*/

var errors=""
var i

// Posizione della chiocciola.
var chiocPos=email.indexOf("@")

// Insieme dei caratteri non ammessi in un e-mail.
var nochar="\\/^,;:+àèìòù'<>()%=?!| " + '"'

// Prima lettera dell'e-mail.
var first_letter=email.substring(0,1)

// Ultima lettera dell'e-mail.
var last_letter=email.substring(email.length-1,email.length)

// Penultima lettera dell'e-mail.
var Penultima_letter=email.substring(email.length-2,email.length-1)

// Lettera a sinistra della chiocciola.
var sx_chioc=email.substring(chiocPos-1,chiocPos)

// Lettera a destra della chiocciola.
var dx_chioc=email.substring(chiocPos+1,chiocPos+2)

if ((chiocPos<"1") || (chiocPos==(email.length-1)) || (chiocPos!=(email.lastIndexOf("@")))) {
errors+="\n- Il carattere chiocciola (@) non e' presente \no si trova in una posizione in posizione errata!"
}
else {
  for (var i=0; i<=nochar.length-1; i++) {
    if (email.indexOf(nochar.substring(i,i+1))!="-1") {
     errors+="\n- Hai digitato dei caratteri non ammessi!"
     break
    }
  }
}

if (errors=="") {
  if ((first_letter==".") || (sx_chioc==".") || (dx_chioc==".") || (last_letter==".") || (Penultima_letter==".") ) {
     errors+="\n- Il punto (.) e' in posizione errata!"
  }  
  else {

    for (var i=0; i<=email.length-1; i++) {
      if ((email.substring(i,i+1)==".") && (email.substring(i+1,i+2)==".")) {
        errors+="\n- Ci sono due caratteri (.) vicini!"
        break
      }
      if ((email.substring(i,i+1)=="-") && (email.substring(i+1,i+2)=="-")) {
        errors+="\n- Ci sono due caratteri (-) vicini!"
        break
      }
    }
  }
}
PuntoDopoChioc = 0
if (errors=="") {
  for (var i=chiocPos+1; i<=email.length-3; i++) {
    if (email.substring(i,i+1)==".") {
      PuntoDopoChioc = 1
      break
    }
  }
  if (PuntoDopoChioc == 0) {
    errors+="\n- Non hai indicato il dominio (.it .com .net ...)!"
  }
}
return errors
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   //while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
   //   retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   //}
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function



