/*
	Autore: Danilo Cicognani
	Script: chkform.js
	Applicazione: bnt
	Versione: 1.00
	Data: 11/02/2009
	Scopo: Validazione dati
	Copyright (c) 2009 Danilo Cicognani
*/

// Verifica la validita' di un campo data
function ValidateDate(theinput) {
	var str;
	str = trim(theinput.value);
	if (str.length == 0) {
		theinput.value = str;
		return true;
	}
	switch (ChkDate(str)) {
	case 0:
		return true;
	case 1:
		alert('Inserire una data nel formato gg/mm/aaaa');
		theinput.focus();
		return false;
	case 2:
		alert('Inserire un giorno compreso tra 01 e 31');
		theinput.focus();
		return false;
	case 3:
		alert('Inserire un mese compreso tra 01 e 12');
		theinput.focus();
		return false;
	case 4:
		alert('Inserire un anno superiore a 0000');
		theinput.focus();
		return false;
	case 28:
		alert('Inserire un giorno compreso tra 01 e 28');
		theinput.focus();
		return false;
	case 29:
		alert('Inserire un giorno compreso tra 01 e 29');
		theinput.focus();
		return false;
	case 30:
		alert('Inserire un giorno compreso tra 01 e 30');
		theinput.focus();
		return false;
	case 31:
		alert('Inserire un giorno compreso tra 01 e 31');
		theinput.focus();
		return false;
	}
}

// Verifica la validita' di un campo obbligatorio
function ValidateMandatory(theinput, errorlist) {
	if (!compiled(theinput)) {
		theinput.className += ' required';
		if (typeof(errorlist) != 'undefined')
			errorlist.innerHTML += '<li>' + theinput.getAttribute('title') + '</li>';
		return false;
	}
	else {
		if (theinput.className.indexOf('required') >= 0)
			theinput.className = trim(theinput.className.substring(0, theinput.className.indexOf('required')));
	}
	return true;
}

function ValidateAlternate(firstlist, secondlist, errorlist) {
	blnfirsterror = false;
	blnseconderror = false;
	for (i = 0; i < firstlist.length; i++) {
		if (!compiled(firstlist[i]))
			blnfirsterror = true;
	}
	for (i = 0; i < secondlist.length; i++) {
		if (!compiled(secondlist[i]))
			blnseconderror = true;
	}
	if (blnfirsterror && blnseconderror) {
		strfirsterror = '';
		for (i = 0; i < firstlist.length; i++) {
			if (!compiled(firstlist[i])) {
				firstlist[i].className += ' required';
				if (strfirsterror.length) strfirsterror += ', ';
				strfirsterror += '"' + firstlist[i].getAttribute('title') + '"';
			}
			else {
				if (firstlist[i].className.indexOf('required') >= 0)
					firstlist[i].className = trim(firstlist[i].className.substring(0, firstlist[i].className.indexOf('required')));
			}
		}
		strseconderror = '';
		for (i = 0; i < secondlist.length; i++) {
			if (!compiled(secondlist[i])) {
				secondlist[i].className += ' required';
				if (strseconderror.length) strseconderror += ', ';
				strseconderror += '"' + secondlist[i].getAttribute('title') + '"';
			}
			else {
				if (secondlist[i].className.indexOf('required') >= 0)
					secondlist[i].className = trim(secondlist[i].className.substring(0, secondlist[i].className.indexOf('required')));
			}
		}
		if (typeof(errorlist) != 'undefined')
			errorlist.innerHTML += '<li>' + strfirsterror + ' o in alternativa ' + strseconderror + '</li>';
		return false;
	}
	else {
		for (i = 0; i < firstlist.length; i++) {
			if (firstlist[i].className.indexOf('required') >= 0)
				firstlist[i].className = trim(firstlist[i].className.substring(0, firstlist[i].className.indexOf('required')));
		}
		for (i = 0; i < secondlist.length; i++) {
			if (secondlist[i].className.indexOf('required') >= 0)
				secondlist[i].className = trim(secondlist[i].className.substring(0, secondlist[i].className.indexOf('required')));
		}
	}
	return true;
}

// Torna true se un campo e compilato, false altrimenti
function compiled(theinput) {
	var str = '';

	if (theinput.type == 'select-one' || theinput.type == 'select-multiple') {
		if (theinput.selectedIndex > -1)
			str = theinput.options[theinput.selectedIndex].value;
	}
	else
		str = trim(theinput.value);
	if (!str.length)
		return false;
	else
		return true;
}

// Elimina gli spazi prima e dopo in una stringa
function trim(str) {
	if (!str)
		return str;
	while (str.length && (str.substring(0, 1) == " " || str.substring(0, 1) == "\t"))
		str = str.substring(1, str.length);
	while (str.length && (str.substring(str.length - 1, str.length) == " " || str.substring(str.length - 1, str.length) == "\t"))
		str = str.substring(0, str.length - 1);
	return str;
}

// Verifica se un carattere e' numerico
function IsDigit(chr) {
	if (chr < '0' || chr > '9')
		return false;
	else
		return true;
}

function ValidElement(stringa, begin, end) {
	var val;

	for (val = ""; begin <= end; begin++) {
		if (!IsDigit(stringa.charAt(begin)))
			return false;
		val += stringa.charAt(begin);
	}
	return val;
}

// Controlla la validita' di una stringa contenente una data
// Ritorna:	0 OK
//			1 'Data non nel formato gg/mm/aaaa'
//			2 'Giorno non compreso tra 01 e 31'
//			3 'Mese non compreso tra 01 e 12'
//			4 'Anno non superiore a 0000'
//			28 'Il giorno dovrebbe essere compreso tra 01 e 28'
//			29 'Il giorno dovrebbe essere compreso tra 01 e 29'
//			30 'Il giorno dovrebbe essere compreso tra 01 e 30'
//			31 'Il giorno dovrebbe essere compreso tra 01 e 31'
function ChkDate(str) {
	var day, month, year;
	var MaxDay;

	if (str.length != 10) return 1;

	day = ValidElement(str, 0, 1);
	month = ValidElement(str, 3, 4);
	year = ValidElement(str, 6, 9);
	
	if (day == 0 || month == 0 || year == 0 || str.charAt(2) != '/' || str.charAt(5) != '/') return 1;
	if (day <= 0) return 2;
	if (month < 1 || month > 12) return 3;
	if (year < 1) return 4;
	if (month == 11 || month == 4 || month == 6 || month == 9)
		MaxDay = 30;
	else if (month == 2) {
		MaxDay = 28;
		if (year % 400 == 0 || ((year % 4) == 0 && (year % 100) != 0))
			MaxDay++;
	}
	else
		MaxDay = 31;
	if (day > MaxDay) return MaxDay;

	return 0;
}
