// Controllo sull'input di typo data
/*
 il parametro date_str se passato deve avere l'anno a quattro cifre
*/


ItalianDate.prototype.valueOf = valueOf
ItalianDate.prototype.toString = toString

ItalianDate.prototype.SECONDS_COUNT = 1
ItalianDate.prototype.DAY_COUNT = 2
ItalianDate.prototype.MONTH_COUNT = 3
ItalianDate.prototype.YEAR_COUNT = 4

function check_form()
{
 var f = document.searchForm
 var msg = "Impossibile effettuare la ricerca:\n"
 if (CheckInput(f.from, null, null, date_type_control, msg)) return
 if (CheckInput(f.to, null, null, date_type_control, msg)) return
 f.submit()
}

function ItalianDate(date_str)
{
this.user_date = (date_str == null) ? "" : date_str
this.day = 0
this.month = 0
this.year = 0
this.weekDay = 0
this.usa_date = 0
this.separator = ""
this.bad_date = false
this.date_err_string = "Data corretta"

this.giorni_name = new Array("Domenica","Lunedi'","Martedi'","Mercoledi'","Giovedi'","Venerdi'","Sabato")
this.day_name = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
this.mesi_name = new Array("gennaio","febbraio","marzo","aprile","maggio","giugno","luglio","agosto","settembre","ottobre","novembre","dicembre")
this.month_name = new Array("January","February","March","April","May","June","July","August","September","October","November","December")

this.recalculateDate = recalculateDate
this.parseDateString = parseDateString
this.isBadDate = isBadDate
this.getDateErrString = getDateErrString
this.increment = increment

this.getDay = getDay
this.getMonth = getMonth
this.getYear = getYear
this.setDay = setDay
this.setMonth = setMonth
this.setYear = setYear

this.getDayName = getDayName
this.getMonthName = getMonthName
this.getFullDayName = getFullDayName
this.getFullMonthName = getFullMonthName

this.get_usa_DayName = get_usa_DayName
this.get_usa_MonthName = get_usa_MonthName
this.get_usa_FullDayName = get_usa_FullDayName
this.get_usa_FullMonthName = get_usa_FullMonthName

this.toTime = toTime

this.toShortDate = toShortDate
this.toAbbrevDate = toAbbrevDate
this.toLongDate = toLongDate

this.to_usa_ShortDate = to_usa_ShortDate
this.to_usa_AbbrevDate = to_usa_AbbrevDate
this.to_usa_LongDate = to_usa_LongDate

this.getUsaDate = getUsaDate
this.toPadString = toPadString

if (this.user_date.length == 0)
 {
 this.usa_date = new Date()
 this.recalculateDate()
 }
else
 {
 if (this.parseDateString())
 {
 this.bad_date = true
 this.date_err_string = "La data contiene caratteri errati."
 }
 else
 {
 var splitDate = this.user_date.split(this.separator = "/")

 if (splitDate.length != 3)
 {
 if (splitDate.length == 1)
 splitDate = this.user_date.split(this.separator = "-")
 if (splitDate.length != 3)
 {
 this.bad_date = true
 if (splitDate.length == 1)
 this.date_err_string = "Macano i separatori di data."
 else if (splitDate.length == 2)
 this.date_err_string = "Manca un separatore di data."
 else
 this.date_err_string = "I separatori di data devono essere due '/' o '-' ."
 }
 }

 if (this.bad_date != true)
 {
 var year_length = splitDate[2].length

 if (year_length != 4 /*&& year_length != 2*/)
 {
 this.bad_date = true
 //this.date_err_string = "E' richiesta una data con l'anno a quattro o due cifre."
 this.date_err_string = "E' richiesta una data con l'anno a quattro cifre."
 }
 else
 {

 var day_limit = 31

 this.day = new Number(splitDate[0])
 this.month = new Number(splitDate[1])
 this.year = new Number(splitDate[2])

 if (year_length == 2)
 {
 if (base_century == 0)
 {
 var proposed_year = 2000 + this.year

 if (new Date().getFullYear() < proposed_year)
 {
 proposed_year = 1900 + this.year
 intended_year = 2000 + this.year
 }
 else
 intended_year = 1900 + this.year


 if (confirm("E' stata immessa una data a due cifre.\nPremere OK se intendete il " + proposed_year + ".\nPremere Annulla (Cancel) se intendete il " + intended_year + "."))
 this.year = proposed_year
 else
 this.year = intended_year
 base_century = (this.year < 2000) ? 1900 : 2000
 }
 else
 this.year += base_century
 }

 if (this.month < 1 || this.month > 12)
 {
 this.bad_date = true
 this.date_err_string = "Il mese della data e' errato."
 }
 else
 {
 if (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11)
 day_limit = 30
 else if (this.month == 2)
 {
 if ( (((this.year & 3) == 0) && (this.year % 100)) || ((this.year % 400) == 0) )
 day_limit = 29
 else
 day_limit = 28
 }
 if (this.day < 1 || this.day > day_limit)
 {
 this.bad_date = true
 this.date_err_string = "Il giorno della data e' errato."
 }
 else
 {
 this.usa_date = new Date(this.year, this.month-1, + this.day)
 this.recalculateDate()
 }
 }
 }
 }
 }
 }
return this
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function valueOf()
{
return this.usa_date.valueOf()
}


/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function toString()
{

return this.toShortDate()
}


/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function recalculateDate()
{

this.day = this.usa_date.getDate()
this.month = this.usa_date.getMonth() + 1
this.year = this.usa_date.getFullYear()
this.weekDay = this.usa_date.getDay()
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function getDay()
{

return this.day
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function getMonth()
{

return this.month
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function getYear()
{

return this.year
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function setDay(day)
{

this.usa_date.setDate(day)
this.recalculateDate()
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function setMonth(month)
{

this.usa_date.setMonth(month - 1)
this.recalculateDate()
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function setYear(year)
{

this.usa_date.setFullYear(year)
this.recalculateDate()
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function getDayName()
{

return this.giorni_name[this.weekDay].subStr(0,3)
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function get_usa_DayName()
{

return this.day_name[this.weekDay].subStr(0,3)
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function getMonthName()
{

return this.mesi_name[this.month-1].subStr(0,3)
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function get_usa_MonthName()
{

return this.month_name[this.month-1].subStr(0,3)
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function getFullDayName()
{

return this.giorni_name[this.weekDay]
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function get_usa_FullDayName()
{

return this.day_name[this.weekDay]
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function getFullMonthName()
{

return this.mesi_name[this.month-1]
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function get_usa_FullMonthName()
{

return this.month_name[this.month-1]
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function toShortDate()
{
var result_date = "Il formato della data e' errato"

if (this.bad_date != true)
 result_date = this.toPadString(this.day) + this.separator + this.toPadString(this.month) + this.separator + this.year.toString()

return result_date
}


/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function toLongDate()
{
var result_date = "Il formato della data e' errato"

if (this.bad_date != true)
 result_date = this.getFullDayName() + ", " + this.toPadString(this.day) + " " + this.getFullMonthName() + " " + this.year.toString()

return result_date
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function toAbbrevDate()
{
var result_date = "Il formato della data e' errato"

if (this.bad_date != true)
 result_date = this.getDayName() + ", " + this.toPadString(this.day) + " " + this.getMonthName() + " " + this.year.toString()

return result_date
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function to_usa_ShortDate()
{
var result_date = "Il formato della data e' errato"

if (this.bad_date != true)
 result_date = this.toPadString(this.month) + this.separator + this.toPadString(this.day) + this.separator + this.year.toString()

return result_date
}


/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function to_usa_LongDate()
{
var result_date = "Il formato della data e' errato"

if (this.bad_date != true)
 result_date = this.get_usa_FullDayName() + ", " + this.get_usa_FullMonthName() + " " + this.toPadString(this.day) + ", " + this.year.toString()

return result_date
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function to_usa_AbbrevDate()
{
var result_date = "Il formato della data e' errato"

if (this.bad_date != true)
 result_date = this.get_usa_DayName() + ", " + this.get_usa_MonthName() + " " + this.toPadString(this.day) + ", " + this.year.toString()

return result_date
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function toTime(wantSeconds)
{
var result_time = "Il formato della data e' errato"

if (this.bad_date != true)
 {
 result_time = this.toPadString(this.usa_date.getHours()) + ":" + this.toPadString(this.usa_date.getMinutes())
 if (wantSeconds)
 result_time += ":" + this.toPadString(this.usa_date.getSeconds())
 }

return result_time
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function isBadDate()
{

return this.bad_date
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function getDateErrString()
{

return this.date_err_string
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function increment(inc_dec, flag)
{

if (this.bad_date != true)
 {
 switch(flag)
 {
 case 1:
 this.usa_date.setSeconds(this.usa_date.getSeconds() + inc_dec)
 break;
 case 2:
 this.usa_date.setDate(this.usa_date.getDate() + inc_dec)
 break;
 case 3:
 this.usa_date.setMonth(this.usa_date.getMonth() + inc_dec)
 break;
 case 4:
 this.usa_date.setYear(this.usa_date.getYear() + inc_dec)
 break;
 default:
 this.usa_date.setSeconds(this.usa_date.getSeconds() + inc_dec)
 break;
 }

 this.recalculateDate()
 }

}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function parseDateString()
{
var result = false
var ch

for (x = 0; x < this.user_date.length; x++)
 {
 ch = this.user_date.charAt(x)
 if ((ch < "0" || ch > "9") && (ch != "-" && ch != "/"))
 {
 result = true
 break
 }

 }
return result
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function toPadString(value)
{

return (value.toString().length == 1) ? "0" + value : value
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
function getUsaDate()
{

return this.usa_date
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/
// Funzioni pubbliche

// Globali flag per tipo di check

var date_type_control = 1
var alfa_type_control = 2
var alfa_numeric_type_control = 3
var numeric_type_control = 4
var ascii_type_control = 5
var email_type_control = 6
var web_type_control = 7
var user_type_control = 255

var domain_length = 4

var base_century = 0

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

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. */
 return "Indirizzo e-mail non valido."

var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid
if (user.match(userPat)==null)
 // user is not valid
 return "Nome utente non valido."

/* 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)
 return "Indirizzo IP non valido."
 }
 return false;
 }

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null)
 return "Controllare il nome dominio."

/* 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
var world = domArr[domArr.length-1].toLowerCase()
if (world.length < 2 || world.length > domain_length || (world.length == domain_length && (world != "name" || world != "info")))
 // the address must end in a two letter or three letter word or four letter for name and info.
 return "L'indirizzo deve terminare con una estensione di due, tre o quattro caratteri."

// Make sure there's a host name preceding the domain.
if (len < 2)
 return "Hostname non specificato."

// If we've gotten this far, everything's valid!
return false;
}
// Funzioni private

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _checkTextValue_(obj)
{

return (obj.value == "")
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _checkSelectValue_(obj)
{

return (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].value == "")
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _checkSelectMultipleValue_(obj)
{
var result = true

if (obj.selectedIndex != -1)
 {
 for (var x = 0; x < obj.options.length; x++)
 {
 if (obj.options[x].selected && obj.options[x].value != "")
 {
 result = false
 break
 }
 }
 }

return result
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _date_check_(obj, err_msg)
{
if (obj.value != "")
 {
 var ck_date = new ItalianDate(obj.value)
 if (ck_date.isBadDate())
 return err_msg += ("\n" + ck_date.getDateErrString())
 else
 obj.value = ck_date.toString()
 }

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _email_check_(obj, err_msg)
{

if (obj.value != "")
 {
 var result_err_msg
 if (result_err_msg = emailCheck(obj.value))
 return err_msg += ("\n" + result_err_msg)
 }

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _alfa_check_(obj)
{
var str = 'ABCDEFGHJKILMNOPQRSTUVWXYZabcdefghjkilmnopqrstuwxyz'

for (var x = 0; x < obj.value.length; x++)
 {
 if (str.indexOf(obj.value.charAt(x) == -1))
 return true
 }

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _alfa_numeric_check_(obj)
{
var str = 'ABCDEFGHJKILMNOPQRSTUVWXYZabcdefghjkilmnopqrstuwxyz1234567890'

for (var x = 0; x < obj.value.length; x++)
 {
 if (str.indexOf(obj.value.charAt(x) == -1))
 return true
 }

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _ascii_check_(obj)
{

for (var x = 0; x < obj.value.length; x++)
 {
 var ch = obj.value.charCodeAt(x)
 if (ch < 32 || ch > 126)
 return true
 }

return false
}


/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _web_check_(obj)
{

if (obj.value.substr(0, 7) != "http://")
 return true

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _numeric_check_(obj)
{

for (var x = 0; x < obj.value.length; x++)
 {
 var ch = obj.value.charAt(x)
 if (ch < '0' || ch > '9')
 return true
 }

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function CheckInput(obj, check_if_empty_flag, empty_msg_err, tocheck_flag, check_msg_err, user_checker)
{
var result = false

if (check_if_empty_flag)
 {
 switch (obj.type)
 {
 case "hidden":
 result = _checkTextValue_(obj)
 break;
 case "text":
 result = _checkTextValue_(obj)
 break;
 case "password":
 result = _checkTextValue_(obj)
 break;
 case "textarea":
 result = _checkTextValue_(obj)
 break;
 case "file":
 result = _checkTextValue_(obj)
 break;
 case "select-one":
 result = _checkSelectValue_(obj)
 break;
 case "select-multiple":
 result = _checkSelectMultipleValue_(obj)
 break;
 }

 if (result == true)
 {
 alert(empty_msg_err)
 if (obj.type != "hidden") obj.focus()
 return result
 }

 }

if (tocheck_flag)
 {
 var temp_msg = ""

 switch (tocheck_flag)
 {
 case 1:
 if (temp_msg = _date_check_(obj, check_msg_err))
 {
 check_msg_err = temp_msg
 result = true
 }
 break;
 case 2:
 result = _alfa_check_(obj)
 break;
 case 3:
 result = _alfa_numeric_check_(obj)
 break;
 case 4:
 result = _numeric_check_(obj)
 break;
 case 5:
 result = _ascii_check_(obj)
 break;
 case 6:
 if (temp_msg = _email_check_(obj, check_msg_err))
 {
 check_msg_err = temp_msg
 result = true
 }
 break;
 case 7:
 result = _web_check_(obj)
 break;
 case 255:
 result = user_checker(obj)
 break;
 }

 if (result == true)
 {
 alert(check_msg_err)
 if (obj.type != "hidden") obj.focus()
 return result
 }
 }

return result
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function IsEmptyInput(obj)
{
var result = false

switch (obj.type)
 {
 case "hidden":
 result = _checkTextValue_(obj)
 break;
 case "text":
 result = _checkTextValue_(obj)
 break;
 case "password":
 result = _checkTextValue_(obj)
 break;
 case "textarea":
 result = _checkTextValue_(obj)
 break;
 case "file":
 result = _checkTextValue_(obj)
 break;
 case "select-one":
 result = _checkSelectValue_(obj)
 break;
 case "select-multiple":
 result = _checkSelectMultipleValue_(obj)
 break;
 }

return result
}