
// 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
}

function checkInvalid (theField, s) {
	theField.focus()
	theField.select()
	alert(iPrefix + s + ' non è stato selezionato!')
	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;
}

// 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;
}

function checkBox (theField,s) {
	if (theField.checked==false) return checkInvalid (theField, s);
		else return true;
}

// ---=======[. FUNZIONE PER LA VALIDAZIONE .]========---
function validatePersonalInfo(form) {
	return (
		checkString(form.elements["userid"], "userid") &&
		checkString(form.elements["nome"], "nome")  &&
		checkString(form.elements["cognome"], "cognome") &&
		checkString(form.elements["indirizzo"], "indirizzo") &&
		checkString(form.elements["telefono"], "telefono") &&
		checkEmail(form.elements["email"], "email") &&
		checkString(form.elements["citta"], "citta'") &&
		checkString(form.elements["provincia"], "provincia") &&
		checkBox(form.elements["accetto"], "Privacy"));
}


	function open_win(pagina,nomepopup,scrollbars,width,height)
	{
	  larghezza=screen.width;
	  altezza=screen.height;
	  spazio_oriz=(larghezza-width)/2;
	  spazio_vert=(altezza-height)/2;
	  parametri = "resizable=no,scrollbars="+scrollbars+",width="+width+",height="+height+",left="+spazio_oriz+",top="+spazio_vert;
	  window.open(pagina, nomepopup ,parametri);
	}
