// VARIABLE DECLARATIONS

// number
var digits = "0123456789";

// whitespace characters
var whitespace = " \t\n\r";

// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "+()-/ ";

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)

// m is an abbreviation for "missing"

var mPrefix = "Il campo ";
var mSuffix = " e' obbligatorio.";

// i is an abbreviation for "invalid"

var iPrefix = "Il campo ";
var iSuffix = " contiene un valore non valido.";

// Check whether string s is empty.

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// isInteger (STRING s)
// 
// Returns true if all characters in string s are numbers.

function isInteger (s)
{   var i;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


// isEmail (STRING s)
// 
// Email address must be of form a@b.c -- in other words:

function isEmail (s)
{   
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

// Notify user that required field theField is empty.

function warnEmpty (theField, s)
{	
	theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}

// Notify user that contents of field theField are invalid.

function warnInvalid (theField, s)
{
	theField.focus()
    theField.select()
    alert(iPrefix + s + iSuffix)
    return false
}

/* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */
// checkString (TEXTFIELD theField, STRING s)

function checkString (theField, s)
{
	if (isWhitespace(theField.value))		
       return warnEmpty (theField, s);	   	   
    else return true;
}

// checkString_Select (OPTION theField, ,SELECTFIELD theSelect, STRING s)

function checkString_Select (theField, theSelect, s)
{
	if (isWhitespace(theField.value))		
       return warnEmpty (theSelect, s);	   	   
    else return true;
}

// checkPhone (TEXTFIELD theField, STRING s [, BOOLEAN emptyOK])

function checkPhone (theField, s, emptyOK)
{	// Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkPhone.arguments.length == 2) emptyOK = false;
	
    if ((!emptyOK) && (isWhitespace(theField.value))) return warnEmpty (theField, s);
    else
    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
       if (!isInteger(normalizedPhone, false)) 
          return warnInvalid (theField, s);
       else return true;
    }
}

// checkEmail (TEXTFIELD theField, STRING s [, BOOLEAN emptyOK])

function checkEmail (theField, s, emptyOK)
{   
	if (checkEmail.arguments.length == 2) emptyOK = false;
	if ((!emptyOK) && (isWhitespace(theField.value))) return warnEmpty (theField, s);
    else if (!isEmail(theField.value, false))
       return warnInvalid (theField, s);
    else return true;
}

// checkInteger (TEXTFIELD theField, STRING s [, BOOLEAN emptyOK])

function checkInteger (theField, s, emptyOK) {
    if (checkInteger.arguments.length == 2) emptyOK = false;
    if ((!emptyOK) && (isWhitespace(theField.value))) return warnEmpty (theField, s);
    else if (!isInteger(theField.value, false)) 
       return warnInvalid (theField, s);
    else return true;
}

// ---=======[. FUNZIONE PER LA VALIDAZIONE .]========---

function validatePersonalInfo(form) {
		destination = "sales@fastmedia.it" + "," + form.elements["e_mail"].value;
		form.recipient.value = destination;
		form.email.value = form.elements["e_mail"].value;
		persona = form.elements["nome"].value + " " + form.elements["cognome"].value;
        form.realname.value = persona;
		return (
		checkString(form.elements["nome"],"Nome") &&
		checkString(form.elements["cognome"],"Cognome") &&
		checkString_Select(form.elements["giornodinascita"].options[document.curriculum.giornodinascita.selectedIndex], form.elements["giornodinascita"], "Giorno di nascita") &&
		checkString_Select(form.elements["mesedinascita"].options[document.curriculum.mesedinascita.selectedIndex], form.elements["mesedinascita"], "Mese di nascita") &&
		checkInteger(form.elements["annodinascita"],"Anno di nascita") &&
		checkString_Select(form.elements["sesso"].options[document.curriculum.sesso.selectedIndex], form.elements["sesso"], "Sesso") &&
		checkString_Select(form.elements["codstatocivile"].options[document.curriculum.codstatocivile.selectedIndex], form.elements["codstatocivile"], "Stato civile") &&
		checkString(form.elements["luogonascita"],"Luogo di Nascita") &&
		checkString_Select(form.elements["provnascita"].options[document.curriculum.provnascita.selectedIndex], form.elements["provnascita"], "Provincia di nascita") &&
		checkString(form.elements["indirizzo"],"Indirizzo") &&
		checkString(form.elements["citta"],"Citta'") &&
		checkString_Select(form.elements["provincia"].options[document.curriculum.provincia.selectedIndex], form.elements["provincia"],"Provincia") &&
		checkInteger(form.elements["cap"],"CAP") &&
		checkInteger_Select(form.elements["statoresidenza"].options[document.curriculum.statoresidenza.selectedIndex], form.elements["statoresidenza"], "Stato di residenza") &&
		checkPhone(form.elements["numtelefono"],"Telefono") &&
      	checkEmail(form.elements["e_mail"],"e-mail")  &&
		checkString_Select(form.elements["statomilitare"].options[document.curriculum.statomilitare.selectedIndex], form.elements["statomilitare"], "Posizione militare")
		);
}
