function checkTel(x){
//var x=document.checknum.pnum.value
//var anum=/(^\d+$)|(^\d+\.\d+$)/
var anum = /^\d+$/;
if (anum.test(x))
testresult=true
else{
alert("Inserire il numero di telefono senza spazi, punti(.), trattini (-) o barre (/).")
testresult=false
}
return (testresult)
}


function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c < "0") || (c > "9"))
			{
			alert("pippo")
			return false
			}
    }
    // All characters are numbers.
    return true;
}

var reDate = /^(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2,4})$/;
var reSignedInteger = /^(\+|\-)?\d+$/;
var reEmail = /^.+\@.+\..+$/;
var iDatePrefix = "Il campo ";
var iDateSuffix = " data non è corretto. \nRiprovare nuovamente.";
var reInteger = /^\d+$/;
var defaultEmptyOK = false;
var DateDelimiter = "/";
var iEmail = "Questo campo deve essere un indirizzo email valido (es. raf@planetvillage.it). \nRiprovare nuovamente.";
var iDay = "Questo campo deve essere un giorno valido (numero da 1 a 31). \nRiprovare nuovamente.";
var iMonth = "Questo campo deve essere un mese valido (numero da 1 a 12). \nRiprovare nuovamente.";
var iYear = "Questo campo deve essere un anno valido (4 cifre). \nRiprovare nuovamente.";
var iWorldZIPCode = "Questo campo deve essere un CAP di 5 cifre valido (es. 35100). \nRiprovare nuovamente.";
var iWorldPhone = "Questo campo deve essere un numero (5 cifre almeno) di telefono valido (es. 0429875623). \nRiprovare nuovamente.";
var iWorldCellPhone = "Questo campo deve essere un numero (9 cifre  almeno) di cellulare valido (es. 3807545256) \nRiprovare nuovamente.";
var ZIPCodeDelimiters = "-";
var ZIPCodeDelimeter = "-";
var validZIPCodeChars = digits + ZIPCodeDelimiters;
var digitsInWorldZIPCode = 5;
var digits = "0123456789";
var whitespace = " \t\n\r";
var digitsInIntlPhoneNumber = 5;
var digitsInIntlCellNumber = 9;

var daysInMonth = makeArray(12);
	daysInMonth[1] = 31;
	daysInMonth[2] = 29;   
	daysInMonth[3] = 31;
	daysInMonth[4] = 30;
	daysInMonth[5] = 31;
	daysInMonth[6] = 30;
	daysInMonth[7] = 31;
	daysInMonth[8] = 31;
	daysInMonth[9] = 30;
	daysInMonth[10] = 31;
	daysInMonth[11] = 30;
	daysInMonth[12] = 31;



function isEmail(s)
{
	if (isEmpty(s))
	{
		if (isEmail.arguments.length == 1)
			return defaultEmptyOK;
		else
			return (isEmail.arguments[1] == true);
	}

	return reEmail.test(s);
}


function checkEmail(theField, emptyOK)
{
	if (checkEmail.arguments.length == 1)
		emptyOK = defaultEmptyOK;

	if ((emptyOK == true) && (isEmpty(theField.value)))
		return true;
    
    if (!isEmail(theField.value, false))
       return warnInvalid(theField, iEmail);

	return true;
}


function stripCharsInBag(s, bag)
{
    var returnString = "";

    for (var i = 0; i < s.length; i++)
    {
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1)
			returnString += c;
	}

	return returnString;
}

function warnInvalid(theField, s)
{
	theField.focus();
    theField.select();
    alert(s);
    return false;
}

function checkIntlZIPCode(theField, emptyOK)
{
	if (checkIntlZIPCode.arguments.length == 1)
		emptyOK = defaultEmptyOK;

    if ((emptyOK == true) && (isEmpty(theField.value)))
		return true;

	var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters);
	if (!isIntlZIPCode(normalizedZIP, false))
		return warnInvalid(theField, iWorldZIPCode);

	theField.value = normalizedZIP;
	return true;
}

function isIntlZIPCode(s)
{
	if (isEmpty(s))
	{
		if (isIntlZIPCode.arguments.length == 1)
			return defaultEmptyOK;
		else
			return (isIntlZIPCode.arguments[1] == true);
	}

	return (isInteger(s) && (s.length == digitsInWorldZIPCode));
}

function makeArray(n)
{
	for (var i = 1; i <= n; i++)
		this[i] = 0;
	return this;
}

function isPositiveInteger(s)
{
	var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    return (isSignedInteger(s, secondArg) && ( (isEmpty(s) && secondArg) || (parseInt(s, 10) > 0) ) );
}

function isInteger(s)
{
    if (isEmpty(s))
    {
		if (isInteger.arguments.length == 1)
			return defaultEmptyOK;
		else
			return (isInteger.arguments[1] == true);
	}

    return reInteger.test(s);
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0));
}

function isIntegerInRange(s, a, b)
{
	if (isEmpty(s))
	{
		if (isIntegerInRange.arguments.length == 1)
			return defaultEmptyOK;
		else
			return (isIntegerInRange.arguments[1] == true);
	}
	if (!isInteger(s, false))
		return false;

    var num = parseInt(s, 10);

    return ((num >= a) && (num <= b));
}

function isMonth(s)
{
	if (isEmpty(s))
	{
		if (isMonth.arguments.length == 1)
			return defaultEmptyOK;
		else
			return (isMonth.arguments[1] == true);
	}

    return isIntegerInRange(s, 1, 12);
}

function isDay(s)
{
	if (isEmpty(s))
	{
		if (isDay.arguments.length == 1)
			return defaultEmptyOK;
		else
			return (isDay.arguments[1] == true);
	}
    return isIntegerInRange(s, 1, 31);
}

function isEmail(s)
{
	if (isEmpty(s))
	{
		if (isEmail.arguments.length == 1)
			return defaultEmptyOK;
		else
			return (isEmail.arguments[1] == true);
	}

	return reEmail.test(s);
}

function isYear(s)
{
	if (isEmpty(s))
	{
		if (isYear.arguments.length == 1)
			return defaultEmptyOK;
		else
			return (isYear.arguments[1] == true);
	}
	if (!isNonnegativeInteger(s))
		return false;
    return ((s.length == 2) || (s.length == 4));
}

function isDate (year, month, day)
{
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false)))
		return false;

	var intYear = parseInt(year, 10);
	var intMonth = parseInt(month, 10);
	var intDay = parseInt(day, 10);

    if (intDay > daysInMonth[intMonth])
		return false;

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear)))
		return false;

    return true;
}

function isSignedInteger(s)
{
	if (isEmpty(s))
	{
		if (isSignedInteger.arguments.length == 1)
			return defaultEmptyOK;
		else
			return (isSignedInteger.arguments[1] == true);
	}

	return reSignedInteger.test(s);
}

function isNonnegativeInteger(s)
{
	var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
		secondArg = isNonnegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg) && ( (isEmpty(s) && secondArg) || (parseInt(s, 10) >= 0) ) );
}

function daysInFebruary(year)
{
	return ( ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function reformatDate(year, month, day, INTLformat)
{
    if (reformatDate.arguments.length < 4)
		INTLformat = false;

	year = "000" + parseInt(year, 10).toString();
	year = year.substr(year.length-4);

	month = "00" + parseInt(month, 10).toString();
	month = month.substr(month.length-2);

	day = "000" + parseInt(day, 10).toString();
	day = day.substr(day.length-2);

	if (INTLformat)
		return day + DateDelimiter + month + DateDelimiter + year;
	else
		return month + DateDelimiter + day + DateDelimiter + year;
}



function checkDateEx(dateField, labelString, OKtoOmitDay, INTLformat, emptyOK)
{
    if (checkDateEx.arguments.length < 3)
		OKtoOmitDay = false;

    if (checkDateEx.arguments.length < 4)
		INTLformat = false;

    if (checkDateEx.arguments.length < 5)
		emptyOK = defaultEmptyOK;

    if ((emptyOK == true) && (isEmpty(dateField.value)))
		return true;

	var im, id;

	if (INTLformat)
	{
		im = 2;
		id = 1;
	}
	else
	{
		im = 1;
		id = 2;
	}

	var a = reDate.exec(dateField.value);
	if (a)	// if not null -> a[0]:full-date, a[1]:nn, a[2]:nn, a[3]:nnnn
	{
		if (!isYear(a[3]))
			return warnInvalid(dateField, iYear);

		if (!isMonth(a[im]))
			return warnInvalid(dateField, iMonth);

		if ( (OKtoOmitDay == true) && isEmpty(a[id]) )
			return true;

		if (!isDay(a[id]))
			return warnInvalid(dateField, iDay);

		if (isDate(a[3], a[im], a[id]))
		{
			dateField.value = reformatDate(a[3], a[im], a[id], INTLformat);
			return true;
		}
	}

	dateField.focus();
    dateField.select();

    alert(iDatePrefix + labelString + iDateSuffix);
    return false;
}
function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Campo Email errato: inserisci un indirizzo e-mail corretto.")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("Campo Email errato: il nome utente non è valido.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Campo Email errato: la destinazione IP non è valida")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Campo Email errato: non hai inserito un dominio valido")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("Campo Email errato: l'indirizzo deve finire con tre lettere o due lettere di dominio locale.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="Campo Email errato: devi inserire l'hostname!"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}
//  End -->