/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

// Carica un oggetto date da un input di data italiana

Date.prototype.setItalianDate = setItalianDate

function setItalianDate(day_month_fullYear, hours, minutes)
{
splitDate = day_month_fullYear.split("/")
if (splitDate.length != 3) splitDate = day_month_fullYear.split("-")

giorno = new Number(splitDate[0])
mese = new Number(splitDate[1]) - 1
anno = new Number(splitDate[2])

this.setFullYear(anno, mese, giorno)
this.setHours(hours)
this.setMinutes(minutes)
this.setSeconds(0)
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

// 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	telephone_type_control = 8
var	decimal_type_control = 9
var	euro_type_control = 10
var	user_type_control = 255

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function MCEtriggerSave(obj)
{
// trigger per output di tinyMCE
if (typeof tinyMCE != undefined)
	{
	tinyMCE.triggerSave()
	len =  obj.elements.length
	for (x = 0; x < len; x++)
		{
		if (obj.elements[x].type == "textarea")
			{
			var inp_v = obj.elements[x].value.toLowerCase()
			if (inp_v == "<p><br></p>" || inp_v == "<p></p>" || inp_v == "<p>&nbsp;</p>")
				obj.elements[x].value = ""
			else
				{
				re_a = new RegExp("<p[^>]*>", "i")
				re_b = new RegExp("<p>", "gi")
				re_c = new RegExp("<\/p>", "gi")
				obj.elements[x].value = obj.elements[x].value.replace(re_a, '')
				obj.elements[x].value = obj.elements[x].value.replace(re_b, '<p>')
				obj.elements[x].value = obj.elements[x].value.replace(re_c, '')
				}
			}
		}
	
	}
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function ControlInput(obj, tocheck_flag)
{
if (obj == undefined)
	{
	alert("This is not a form object")
	return
	}

switch (obj.type)
	{
	case "hidden":
	case "text":
	case "password":
	case "textarea":
		switch (tocheck_flag)
			{
			case 1: // date_type_control
				obj.onkeydown = _key_date_check_
				break;
			case 2: // alfa_type_control
				obj.onkeypress = _key_alfa_check_
				break;
			case 3: // alfa_numeric_type_control
				obj.onkeydown = _key_alfa_numeric_check_
				break;
			case 4: // numeric_type_control
				obj.onkeydown = _key_numeric_check_
				break;
			case 5: // ascii_type_control
				obj.onkeydown = _key_ascii_check_
				break;
			case 6: // email_type_control
				obj.onkeydown = _key_email_check_
				break;
			case 8: // telephone_type_control
				obj.onkeydown = _key_telephone_check_
				break;
			case 9: // decimal_type_control
				obj.onkeydown = _key_decimal_check_
				break;
			case 10: // euro_type_control
				obj.onkeydown = _key_euro_check_
				break;
			default:
				alert(tocheck_flag + " is not a type control parameter")
				return
			}
		break;
	default:
		alert(obj + " is not a form object to control")
	}
}


/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function CheckInput(obj, check_if_empty_flag, empty_msg_err, tocheck_flag, check_msg_err, user_checker)
{
var 	result = false

if (obj == undefined)
	{
	alert("This is not a form object")
	return true
	}

check_if_empty_flag = check_if_empty_flag == null ? false : check_if_empty_flag
empty_msg_err = empty_msg_err == null ? "" : empty_msg_err
tocheck_flag = tocheck_flag == null ? false : tocheck_flag
check_msg_err = check_msg_err == null ? "" : check_msg_err

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;
		default:
			alert(obj + " is not a form object")
			return true
		}
	
	if (result)
		{
		alert(empty_msg_err)
		if (obj.type != "hidden") obj.focus()
		return result
		}
		
	}

if (tocheck_flag)
	{
	var 	temp_msg = ""
	
	switch (tocheck_flag)
		{
		case 1: // date_type_control
			if (check_msg_err = _date_check_(obj, check_msg_err))
				result = true
			break;
		case 2: // alfa_type_control
			result = _alfa_check_(obj)
			break;
		case 3: // alfa_numeric_type_control
			result = _alfa_numeric_check_(obj)
			break;
		case 4: // numeric_type_control
			result = _numeric_check_(obj)
			break;
		case 5: // ascii_type_control
			result = _ascii_check_(obj)
			break;
		case 6: // email_type_control
			if (check_msg_err = _email_check_(obj, check_msg_err))
				result = true
			break;
		case 7: // web_type_control
			result = _web_check_(obj)
			break;
		case 8: // telephone_type_control
			result = _telephone_check_(obj)
			break;
		case 9: // decimal_type_control
			result = _decimal_check_(obj)
			break;
		case 10: // euro_type_control
			result = _euro_check_(obj)
			break;
		case 255: // user_type_control
			result = user_checker(obj)
			break;
		default:
			alert(tocheck_flag + " is not a type control parameter")
			return true
		}
		
		if (result)
			{
			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
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

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 Data(obj.value)
	if (ck_date.err)
		return err_msg += ck_date.getError()
	else
		obj.value = ck_date.format("%d"+ck_date.separator+"%m"+ck_date.separator+"%Y")
	}
	
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 += result_err_msg
	}
	
return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _alfa_check_(obj)
{
var str = 'ABCDEFGHJKILMNOPQRSTUVWXYZabcdefghjkilmnopqrstuwxyz'
var value = obj.value
var len = value.length

for (var x = 0; x < len; x++)
	{
	if (str.indexOf(value.charAt(x)) == -1)
		return true
	}

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _alfa_numeric_check_(obj)
{
var str = 'ABCDEFGHJKILMNOPQRSTUVWXYZabcdefghjkilmnopqrstuwxyz1234567890'
var value = obj.value
var len = value.length

for (var x = 0; x < len; x++)
	{
	if (str.indexOf(value.charAt(x)) == -1)
		return true
	}

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _decimal_check_(obj)
{
var str = '1234567890,.'
var value = obj.value
var len = value.length

for (var x = 0; x < len; x++)
	{
	if (str.indexOf(value.charAt(x)) == -1)
		return true
	}

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _euro_check_(obj)
{
var str = '1234567890,'
var value = obj.value
var len = value.length

for (var x = 0; x < len; x++)
	{
	if (str.indexOf(value.charAt(x)) == -1)
		return true
	}

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _ascii_check_(obj)
{
var len = obj.value.length

for (var x = 0; x < len; 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)
{
var len = obj.value.length

for (var x = 0; x < len; x++)
	{
	var ch = obj.value.charAt(x)
	if (ch  < '0' || ch > '9')
		return true
	}

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _telephone_check_(obj)
{
var len = obj.value.length

for (var x = 0; x < len; x++)
	{
	var ch = obj.value.charAt(x)
	if ((ch  < '0' || ch > '9'))
		return true
	}

return false
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function emailCheck(s)
{
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=s.match(emailPat)
if (matchArray==null) return "\nIndirizzo non valido."

var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) return "\nNome utente non valido."

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null)
	{
	  for (var i=1;i<=4;i++)
	  	{
	    if (IPArray[i]>255)
			return "\nIndirizzo IP non valido."
    	}
    return false;
	}

var domainArray=domain.match(domainPat)
if (domainArray==null) return "\nControllare il nome dominio."

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)
   return "\nL'indirizzo deve terminare con una estensione di due o piu' caratteri."
if (len < 2) return "\nHostname non specificato."

return false;
}


/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _key_alfa_check_(e)
{
var str = 'ABCDEFGHJKILMNOPQRSTUVWXYZabcdefghjkilmnopqrstuwxyz\t\r\b'

	if (str.indexOf(String.fromCharCode(e.which)) == -1)
		return false

return true
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _key_alfa_numeric_check_(e)
{
var str = 'ABCDEFGHJKILMNOPQRSTUVWXYZabcdefghjkilmnopqrstuwxyz1234567890\t\r\b'

	if (str.indexOf(String.fromCharCode(e.which)) == -1)
		return false

return true
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _key_numeric_check_(e)
{
var str = '1234567890\t\r\b'

	if (str.indexOf(String.fromCharCode(e.which)) == -1)
		return false

return true
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _key_decimal_check_(e)
{
var str = '1234567890.,\t\r\b'

	if (str.indexOf(String.fromCharCode(e.which)) == -1)
		return false

return true
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _key_euro_check_(e)
{
var str = '1234567890,\t\r\b'

	if (str.indexOf(String.fromCharCode(e.which)) == -1)
		return false

return true
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _key_ascii_check_(e)
{
var ch = String.fromCharCode(e.which)

	if (ch == '\t' || ch == '\r')
		return true
	if (ch  < 32 || ch > 126)
		return false

return true
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _key_telephone_check_(e)
{
var str = '1234567890 \t\r\b'

	if (str.indexOf(String.fromCharCode(e.which)) == -1)
		return false

return true
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

function _key_email_check_(e)
{
var str = 'ABCDEFGHJKILMNOPQRSTUVWXYZabcdefghjkilmnopqrstuwxyz1234567890@.-_\t\r\b'
	if (str.indexOf(String.fromCharCode(e.which)) == -1)
		return false

return true
}

/*->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->*/

/*
english
%a	Abbreviated name of day of the week
%A	Complete name of day of the week
%b	Abbreviated name of month (also obtained with Ô%hÕ)
%B	Complete name of month
%c	Date and time in standard format
%x	Date in standard format

italian 
%e	Abbreviated name of day of the week
%E	Complete name of day of the week
%f	Abbreviated name of month (also obtained with Ô%hÕ)
%F	Complete name of month
%g 	Date and time in standard format
%o	Date in standard format

%d	Day of the month as an integer number
%H	Time in 24-hour format (from 00 to 23)
%I	Time in 12-hour format (from 01 to 12)
%m	Month as an integer number (from 01 to 12)
%M	Minutes in decimal format
%p	The AM or PM string, depending on the time
%S	Seconds in decimal format
%w	Day of the year as an integer number (from 0 to 6)
%X	Time in standard format
%y	Year with two digits
%Y	Year with four digits
*/


function Data(date_str, ora, minuti, secondi)
{

this.date_str = date_str
this.err = 0
this.separator = ""

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.err_string = new Array("L'anno della data e' errato.",
	"E' richiesta una data con l'anno non inferiore al 1970.'",
	"Il mese della data e' errato.",
	"Il giorno della data e' errato.",
	"Mancano i separatori di data.",
	"Manca un separatore di data.",
	"I separatori di data devono essere due '/' o '-' .",
	"La data contiene caratteri errati.")


this.valueOf = data_method_valueOf
this.toString = data_method_toString
this.toSource = data_method_toSource
this.parse = data_method_parse
this.format = data_method_format
this.pad = data_method_pad
this.getError = data_method_getError

if (this.date_str)
	{
	if (!this.parse())
		{
		var splitDate = this.date_str.split(this.separator)
		var splitLen = splitDate.length
		if (splitLen == 3)
			{
			var day_limit = 31
			
			var day = new Number(splitDate[0])
			var month = new Number(splitDate[1])
			var year = new Number(splitDate[2])
			
			if (isNaN(splitDate[2]))
				this.err = 1
			else
				{
				if (year < 1970)
					this.err = 2
				else
					{
					if (isNaN(splitDate[1]) || month < 1 || month > 12 )
						this.err = 3
					else
						{
						if (month == 4 || month == 6 || month == 9 || month == 11)
							day_limit = 30
						else if (month == 2)
							{					
							if ( (((year & 3) == 0) && (year % 100)) || ((year % 400) == 0) )
								day_limit = 29
							else
								day_limit = 28
							}
						if (isNaN(splitDate[0]) ||day < 1 || day > day_limit)
							this.err = 4
						else
							this.date = new Date(year, month-1, + day, isNaN(ora)?0:ora, isNaN(minuti)?0:minuti, isNaN(secondi)?0:secondi)
						}
					}
				}
			}
		else
			{
			if (splitLen == 1)
				this.err = 5
			else if (splitLen == 2)
				this.err = 6
			else
				this.err = 7
			}
		}
	else
		this.err = 8
	}
else
	this.date = new Date()

return this
}

function data_method_pad(value)
{

return (value.toString().length == 1) ? "0" + value : value
}

function data_method_parse()
{
var result = false
var ch
var len = this.date_str.length

for (x = 0; x < len; x++)
	{
	ch = this.date_str.charAt(x)
	if (ch == "/" || ch == "-")
		{
		if (!this.separator)
			this.separator = ch
		continue
		}
	if (ch < "0" || ch > "9")
		{
		result = true
		break
		}
		
	}

if (!this.separator) this.separator = "/"
	
return result
}

function data_method_format(format_str)
{
result = ""
if (!this.err)
	{
	var ch
	var len = format_str.length
	
	for (x = 0; x < len; x++)
		{
		ch = format_str.charAt(x)
		if (ch == "%")
			{
			if (x < len)
				{
				ch = format_str.charAt(++x)
				switch (ch)
					{
					case "a":
						result += this.day_name[this.date.getDay()+1].substring(0,3)
						break
					case "A":
						result += this.day_name[this.date.getDay()+1]
						break
					case "b":
						result += this.month_name[this.date.getMonth()+1].substring(0,3)
						break
					case "B":
						result += this.month_name[this.date.getMonth()+1]
						break
					case "e":
						result += this.giorni_name[this.date.getDay()+1].substring(0,3)
						break
					case "E":
						result += this.giorni_name[this.date.getDay()+1]
						break
					case "f":
						result += this.mesi_name[this.date.getMonth()+1].substring(0,3)
						break
					case "F":
						result += this.mesi_name[this.date.getMonth()+1]
						break
					case "c":
						var ora = this.date.getHours()
						var sigla = ora >= 12 ? "PM" : "AM"
						ora = this.pad(ora > 12 ? ora-12 : ora)
						result += this.date.getFullYear() + this.separator + this.pad(this.date.getMonth()+1) + this.separator + this.pad(this.date.getDate()) + " " + ora + ":" + this.pad(this.date.getMinutes()) + ":" + this.pad(this.date.getSeconds()) + " " + sigla
						break
					case "g":
						result += this.pad(this.date.getDate()) + this.separator + this.pad(this.date.getMonth()+1) + this.separator + this.date.getFullYear() + " " + this.pad(this.date.getHours()) + ":" + this.pad(this.date.getMinutes()) + ":" + this.pad(this.date.getSeconds())
						break
					case "d":
						result += this.pad(this.date.getDate())
						break
					case "H":
						result += this.pad(this.date.getHours())
						break
					case "i":
						var ora = this.date.getHours()
						result += this.pad(ora > 12 ? ora-12 : ora)
						break
					case "m":
						result += this.pad(this.date.getMonth()+1)
						break
					case "M":
						result += this.pad(this.date.getMinutes())
						break
					case "p":
						result += this.date.getHours() >= 12 ? "PM" : "AM"
						break
					case "S":
						result += this.pad(this.date.getSeconds())
						break
					case "w":
						result += this.date.getDay()+1
						break
					case "x":
						result += this.date.getFullYear() + this.separator + this.pad(this.date.getMonth()+1) + this.separator + this.pad(this.date.getDate())
						break
					case "o":
						result += this.pad(this.date.getDate()) + this.separator + this.pad(this.date.getMonth()+1) + this.separator + this.date.getFullYear()
						break
					case "y":
						result += String(this.date.getFullYear()).substring(2)
						break
					case "Y":
						result += this.date.getFullYear()
						break
					default:
						result += "%" + ch
					}
				}
			else
				result += ch
			}
		else
			result += ch
		}
	}
else
	result = this.getError()

return result
}

function data_method_toString()
{

return this.date.toString()
}

function data_method_valueOf()
{

return this.date.valueOf()
}

function data_method_toSource()
{

return "object Data(string, int, int, int) {\n    [native code]\n}"
}

function data_method_getError()
{

return this.err ? this.err_string[this.err-1] : "no error"
}

