function ControllaDipendente(){
	if (document.Dipendente.Cognome.value=="") {
		alert("Inserire il cognome!");
		document.Dipendente.Cognome.focus();
		return false;
	}
	if (document.Dipendente.Nome.value=="") {
		alert("Inserire il nome!");
		document.Dipendente.Nome.focus();
		return false;
	}	
	if (document.Dipendente.LuogoNascita.value=="") {
		alert("Inserire il luogo di nascita!");
		document.Dipendente.LuogoNascita.focus();
		return false;
	}	
	if (document.Dipendente.DataNascita.value=="") {
		alert("Inserire la data di nascita!");
		document.Dipendente.DataNascita.focus();
		return false;
	}	
	if (document.Dipendente.ProvNascita.value=="") {
		alert("Selezionare la provincia di nascita!");
		document.Dipendente.ProvNascita.focus();
		return false;
	}	
	if (document.Dipendente.CodiceFiscale.value=="") {
		alert("Inserire il Codice fiscale!");
		document.Dipendente.CodiceFiscale.focus();
		return false;
	}	
	
	if (confirm("Inviare il curriculum inserito?")) {
		document.Dipendente.submit();
	}
}

function OnlyNumeric() { 
 if ((event.keyCode < 48) || //Sono ammessi solo caratteri numerici
  (event.keyCode > 57)) event.returnValue = false;
}

function OnlyDecimal() { 
	//Sono ammessi solo caratteri numerici e "," o "."
	if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode != 44 && event.keyCode != 46))
	 event.returnValue = false;
}

function CF(){
	lwidth=screen.width;
	lheight=screen.height;
	stringa="location=no,toolbar=no,resizable=no,width=380,height=250,left=" + (lwidth -380)/2 + ",top=" + (lheight-250)/2;
	strlocation="CodiceFiscale.asp?";
	strlocation=strlocation + "Cognome=" + document.Dipendente.Cognome.value;
	strlocation=strlocation + "&Nome=" + document.Dipendente.Nome.value;
	strlocation=strlocation + "&Data=" + document.Dipendente.DataNascita.value;
	strlocation=strlocation + "&Luogo=" + document.Dipendente.LuogoNascita.value;
	strlocation=strlocation + "&Provincia=" + document.Dipendente.ProvNascita.options[document.Dipendente.ProvNascita.options.selectedIndex].value;
	var finestra=window.open(strlocation,"CodiceFiscale",stringa);
	finestra.focus();
}	

function ControllaCF(){
	if (document.Dipendente.Cognome.value=="") {
		alert("Inserire il cognome!");
		document.Dipendente.Cognome.focus();
		return false;
	}
	if (document.Dipendente.Nome.value=="") {
		alert("Inserire il nome!");
		document.Dipendente.Nome.focus();
		return false;
	}		
	if (document.Dipendente.LuogoNascita.value=="") {
		alert("Inserire il luogo di nascita!");
		document.Dipendente.LuogoNascita.focus();
		return false;
	}	
	if (document.Dipendente.DataNascita.value=="") {
		alert("Inserire la data di nascita!");
		document.Dipendente.DataNascita.focus();
		return false;
	}	
	if (document.Dipendente.ProvNascita.value=="") {
		alert("Selezionare la provincia di nascita!");
		document.Dipendente.ProvNascita.focus();
		return false;
	}		
	CF();
}

function validHour(fld) {
    var inp = fld.value
	var msg="";
	if (inp=="") return true
    // extract components of input hour
    var inpHour = parseInt(inp.substring(0, inp.indexOf(":")), 10)
    var inpMin = parseInt(inp.substring((inp.indexOf(":") + 1), inp.length), 10)
	if (isNaN(inpHour) || isNaN(inpMin)) msg = "Formato ora non valido. Inserire l'ora nel formato 'hh:mm'"
	if (inpHour>23) msg = "Formato ora non valido. Inserire l'ora nel formato 'hh:mm'" 
	if (inpMin>59) msg = "Formato ora non valido. Inserire l'ora nel formato 'hh:mm'"
	
	if (msg) {
		alert(msg);
		fld.focus();
        return false
	}
	else {
		return true
	}
}

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 CheckLen(fld,length) {
	if (fld.value.length>length) {
		alert("Il testo inserito non può superare i " + length + " caratteri.");
		fld.focus();
		return false;
	}
}

function IntervalloDate(fldfrom,fldto) {
	if (fldfrom.value=="") return true
	if (fldto.value=="") return true

    // extract components of from data
    fromDay = parseInt(fldfrom.value.substring(0, fldfrom.value.indexOf("/")), 10)
    fromMo = parseInt(fldfrom.value.substring((fldfrom.value.indexOf("/") + 1), fldfrom.value.lastIndexOf("/")), 10)
    fromYr = parseInt(fldfrom.value.substring((fldfrom.value.lastIndexOf("/") + 1), fldfrom.value.length), 10)

	// extract components of to data
    toDay = parseInt(fldto.value.substring(0, fldto.value.indexOf("/")), 10)
    toMo = parseInt(fldto.value.substring((fldto.value.indexOf("/") + 1), fldto.value.lastIndexOf("/")), 10)
    toYr = parseInt(fldto.value.substring((fldto.value.lastIndexOf("/") + 1), fldto.value.length), 10)
	
	var fromDate = new Date(fromMo + "/" + fromDay + "/" + fromYr)
	var toDate = new Date(toMo + "/" + toDay + "/" + toYr)
	
	if (fromDate>toDate) return false;
	return true;
}

function check_email(campo) {
/*
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 email=campo.value;
var errors=""
var i
if (email=="") return true

// 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
if (errors!="") {
	alert(errors);
	campo.focus();
	return false
} else
	return true
}

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

function CheckHex(value) {
	if (value.length!=6) return false;
    for (xx=0;xx<=value.length-1;xx++) {
		car=value.charAt(xx);
		if ((car< "a" || car> "f") && (car< "A" || car> "F") && (car< "0" || car> "9")) 
			return false;
    } 
	return true;
}

// Argomento: campo IBAN da verificare
// Restituisce true se il codice è corretto e false se è errato
// Dà un messaggio popup per commentare l'errore o indicare l'esattezza dell'IBAN
function IBANChk(campo)
{
  var b=campo.value;
  if (b=="") return true; //non ha scritto niente nel campo
  
  // Controllo della lunghezza e del codice di controllo
  if (b.length < 5)
  {
    alert("La lunghezza è minore di 5 caratteri");
	campo.focus();
    return false;
  }

  // Scambio dei primi quattro caratteri con il resto
  s = b.substring(4) + b.substring(0, 4);

  // Ciclo tra caratteri della stringa
  for (i = 0, r = 0; i < s.length; i++ )
  {
    // Estrae il prossimo carattere c
    c = s.charCodeAt(i);

    // Calcola il codice k da 0 a 35
    if (48 <= c && c <= 57) // per cifra 0-9
    {
      if (i == s.length-4 || i == s.length-3)
      {
        alert("Posizioni 1 e 2 non possono contenere cifre");
		campo.focus();
        return false;
      }
      k = c - 48;
    }
    else if (65 <= c && c <= 90) // per lettera A-Z
    {
      if (i == s.length-2 || i == s.length-1)
      {
        alert("Posizioni 3 e 4 non possono contenere lettere");
		campo.focus();
        return false;
      }
      k = c - 55;
    }
    else
    {
      alert("Sono ammesse solo cifre e lettere maiuscole");
	  campo.focus();
      return false;
    }

    // Controllo del codice di controllo
    if (b.substring(2, 4) > "96")
    {
      alert("Il codice d controllo non può superare 96");
	  campo.focus();
      return false;
    }

    // Cumula il resto della divisione per 97
    if (k > 9)
      r = (100 * r + k) % 97;
    else
      r = (10 * r + k) % 97;
  }

  // Il resto della divisione deve essere 1
  if (r != 1)
  {
    alert("Il codice di controllo è errato");
	campo.focus();
    return false;
  }

  // Return OK
  //alert("L'IBAN risulta corretto");
  return true;
}

function ImpostaData(campoFrom,campoTo) {
	if (validDate(campoFrom)) campoTo.value=campoFrom.value;
}

function check_cf(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 e' "
        +"corretta: il codice fiscale dovrebbe essere lungo\n"
        +"esattamente 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) +
                "'.\nI 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 e' corretto:\n"+
            "il codice di controllo non corrisponde.\n";
    return "";
}
