// Input : String containing useless spaces
// Output : 'Trimmed' string
function sTrim(inputString) {

   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
 }

// Restituire true se sEmail è un indirizzo e-mail valido
function checkemail(sEmail) {
	return (sEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

// Restituisce true se il valore passato è numerico
function checkNumber(sVar) {
	return (sVar.search(/^[0-9]{1,16}$/) != -1);
}	

// Restituisce true se la stringa passata contiene lettere o numeri
function checkAlfaNum(sVar) {
	return (sVar.search(/^([a-zA-Z0-9])+$/) != -1);
}	

// Restituisce true se la stringa passata contiene lettere, numeri o trattino
function checkRagSoc(sVar) {
	return (sVar.search(/^([a-zA-Z0-9_\.\-\s\'\à?\è\à?\ù\ò\ì\/])+$/) != -1);
}	

// Restituisce stringa vuota se il codice fiscale passato in input è valido.
function checkCodFisc(cf)
{
    var validi, i, s, set1, set2, setpari, setdisp;
    if( cf == '' )
		return ('');
		
    cf = cf.toUpperCase();
    if( cf.length != 16 )
		return ("La lunghezza del codice fiscale non è corretta, il codice fiscale deve essere lungo 16 caratteri.\n");
		
    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for( i = 0; i < 16; i++ ){
        if( validi.indexOf( cf.charAt(i) ) == -1 )
			return ("Il codice fiscale contiene un carattere non valido '" + cf.charAt(i) + "'. I caratteri validi sono le lettere e le cifre.\n");
    }
    set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
    for( i = 1; i <= 13; i += 2 )
        s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    for( i = 0; i <= 14; i += 2 )
        s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
        return ("Il codice fiscale non è corretto, il codice di controllo non corrisponde.\n");
    return ('');
}

// Restituisce stringa vuota se la partita iva passata in input è valida
function checkP_IVA(pi)
{
    if( pi == '' )  
		return ('');
		
    if( pi.length != 11 )
        return "La lunghezza della partita IVA non è corretta: la partita IVA deve essere lunga 11 caratteri.\n";
		
    validi = "0123456789";
    for( i = 0; i < 11; i++ ){
        if( validi.indexOf( pi.charAt(i) ) == -1 )
            return "La partita IVA contiene un carattere non valido '" + pi.charAt(i) + "'. Sono ammesse solamente le cifre.\n";
    }
    s = 0;
    for( i = 0; i <= 9; i += 2 )
        s += pi.charCodeAt(i) - '0'.charCodeAt(0);
    for( i = 1; i <= 9; i += 2 ){
        c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
        if( c > 9 )  c = c - 9;
        s += c;
    }
    if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
        return "La partita IVA non è valida, il codice di controllo non corrisponde.\n";
    return '';
}
