var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var checkobj;
//Si occupa di validare tutti i campi del form
function valida(num)
{
	if (typeof num=="undefined")
	{
		num = 0;
	}
	valido = true;
	msg = "";

	//scorro tutti gli elementi del form
	for (i=0; i<document.forms[num].length; i++)
	{	
		//controllo se esiste un attributo validazione
		if (is_valid(document.forms[num].elements[i].getAttribute("validazione")))
		{
			//esamino un carattere alla volta
			str = new String(document.forms[num].elements[i].getAttribute("validazione"));
			for (j = 0; j < str.length; j++)
			{
				chr = str.charAt(j);
				switch(chr)
				{			
					case "n":	//numeric						
						if (!is_empty(document.forms[num].elements[i].value) && isNaN(document.forms[num].elements[i].value))
						{
							msg = msg + "Il campo " + document.forms[num].elements[i].title.toUpperCase() + " deve essere numerico\n";
							valido = false;
						}						
						break;
					case "d":	//date
						if (!is_empty(document.forms[num].elements[i].value) && !is_date(document.forms[num].elements[i].value))
						{
							msg = msg + "Il campo " + document.forms[num].elements[i].title.toUpperCase() + " deve contenere una data valida\n";
							valido = false;
						}
						break;
					case "t":	//time
						if (!is_empty(document.forms[num].elements[i].value) && !is_time(document.forms[num].elements[i].value))
						{
							msg = msg + "Il campo " + document.forms[num].elements[i].title.toUpperCase() + " deve contenere un orario valido\n";
							valido = false;
						}
						break;
					case "r":	//required
						if (is_empty(document.forms[num].elements[i].value))
						{
							msg = msg + "Il campo " + document.forms[num].elements[i].title.toUpperCase() + " non deve essere vuoto\n";
							valido = false;
						}
						break;
					case "c":	//currency
						if (!is_currency(document.forms[num].elements[i].value))
						{
							msg = msg + "Il campo " + document.forms[0].elements[i].title.toUpperCase() + " deve contenere una valuta valida\n";
							valido = false;
						}
						break;
					case "l":	//listing
						if (!is_listing(document.forms[num].elements[i].value))
						{
							msg = msg + "Il campo " + document.forms[num].elements[i].title.toUpperCase() + " deve contenere un elenco di numeri\n";
							valido = false;
						}
						break;
					case "e":	//eta
					if (!is_empty(document.forms[num].elements[i].value) && !eta_consentita(document.forms[num].elements[i].value))
						{
							msg = msg + "Non devi avere compiuto 21 anni entro la data di scadenza del bando (16 settembre)\n";
							valido = false;
						}
						break;		
					case "x":	//checkbox
					if (!(document.forms[num].elements[i].checked))
						{
							msg = msg + "Il campo " + document.forms[0].elements[i].title.toUpperCase() + " deve essere contrassegnato\n";
							valido = false;
						}
						break;		
					default:	//altri caratteri di validazione vengono ignorati
						break;
				} //fine switch
			} //fine for
		} //fine if

		// se il campo è valido controllo gli attributi min e max
		if(is_valid(document.forms[num].elements[i].value) && (!isNaN(document.forms[num].elements[i].value)))
		{
			// controllo il campo min
			if ((is_valid(document.forms[num].elements[i].getAttribute("min"))) && (eval(document.forms[num].elements[i].value) < eval(document.forms[0].elements[i].getAttribute("min"))))
			{
				msg = msg + "Il campo " + document.forms[num].elements[i].title.toUpperCase() + " non può essere minore di " + document.forms[0].elements[i].getAttribute("min") + "\n";
				valido = false;
			}

			// controllo il campo max
			if ((is_valid(document.forms[num].elements[i].getAttribute("max"))) && (eval(document.forms[num].elements[i].value) > eval(document.forms[0].elements[i].getAttribute("max"))))
			{
				msg = msg + "Il campo " + document.forms[num].elements[i].title.toUpperCase() + " non può essere maggiore di " + document.forms[0].elements[i].getAttribute("max") + "\n";
				valido = false;
			}			
		}
	} //fine for

	if (msg!="")
		alert(msg);
	return valido;
}

function is_date(dt_str)
{
	var daysInMonth = DaysArray(12)
	var pos1=dt_str.indexOf(dtCh)
	var pos2=dt_str.indexOf(dtCh,pos1+1)
	var strDay=dt_str.substring(0,pos1)
	var strMonth=dt_str.substring(pos1+1,pos2)
	var strYear=dt_str.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1)
	{
		//alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12)
	{
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dt_str.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dt_str, dtCh))==false)
	{
		//alert("Please enter a valid date")
		return false
	}
	return true;
}

function is_time(str) 
{
	var strTime = /^(\d{1,2})(\:\d{1,2})?$/;
	var strFormat = str.match(strTime);

	if (strFormat == null) 
	{
		return false;
	}
	else
	{	// Validate for this format: hh:mm
		
		if (strFormat[1] > 23 || strFormat[1] < 00) 
		{
			return false;
		}
		
		if (strFormat[3] > 59 || strFormat[3] < 00) 
		{
			return false;	
		}
	}
	return true;
}

function is_currency(str)
{
	filtro = "^((0|[1-9][0-9]{0,8})(\\.\\d{1,2})?)?$";
	var regexp = new RegExp(filtro);

	if (str.match(regexp)) 
	{
		return true;
	}
	else
	{
		return false;
	}
}

function is_listing(str)
{
	filtro = "^\\d+(\\,\\d+)*$";
	var regexp = new RegExp(filtro);

	if (str.match(regexp)) 
	{
		return true;
	}
	else
	{
		return false;
	}
}

function is_empty(text) 
{
	if ((text.length==0) || (text==null)) 
	{
		return true;
	}
	else 
		return false;
}

function is_valid(obj) 
{
	if (null == obj) 
	{
		return false;
	}
	if ("undefined" == typeof(obj)) 
	{
		return false;
	}
	if (obj == "")
	{
		return false;
	}
	return true;
}


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"))) return false;
    }
    // All characters are numbers.
    return true;
}

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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n)
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this;
}

function popup_comuni(comune) 
{
	pagina = 'PopupComuni.php?comune=' + comune;
	window.open(pagina,'pagina_cerca_prodotto','width=250,height=300,toolbar=no')
}

function conferma_eliminazione()
{
	return confirm("CONFERMI L'ELIMINAZIONE?");
}

function conferma_annullamento()
{
	return confirm("CONFERMI L'ANNULLAMENTO?");
}

function aggiungi_separatore(textbox)
{
	if ((textbox.value.length==2) || (textbox.value.length==5))
		textbox.value = textbox.value + "/";

	var r = textbox.createTextRange();
    r.moveStart('character', textbox.value.length);
    r.collapse();
    r.select();
}

function valida_scheda()
{
	if (valida())		//effettuo prima la normale validazione
	{
		valido = true;
		msg = "";

		//se il form è valido controllo che la fonte e la categoria siano ammessi
		if(document.forms[0].Categoria.value != document.forms[0].CodiceCategoria.value)
		{
			msg = msg + "La CATEGORIA non è valida\n";
			valido = false;
		}
		if(document.forms[0].Fonte.value != document.forms[0].CodiceFonte.value)
		{
			msg = msg + "La FONTE non è valida\n";
			valido = false;
		}
		
		if (msg!="")
			alert(msg);
		return valido;
	}
	else
		return false;
}

function eta_consentita(dt_str)
{
	var data_limite_inf = new Date(1988,8,15);
	var daysInMonth = DaysArray(12);
	var pos1=dt_str.indexOf(dtCh);
	var pos2=dt_str.indexOf(dtCh,pos1+1);
	var strDay=dt_str.substring(0,pos1);
	var strMonth=dt_str.substring(pos1+1,pos2);
	var strYear=dt_str.substring(pos2+1);
	var data_inserita = new Date(strYear,strMonth-1,strDay);
	
	if (data_inserita.getTime() > data_limite_inf.getTime())
		return true;
	else
		return false;
}


function is_cheched(chkbx) 
{
	checkobj=chkbx
	if (window.checkobj&&checkobj.checked)
		return true
	else
		return false
}



var checkobj

function accetta(el){
checkobj=el
	if (document.all||document.getElementById){		  
	if (window.checkobj&&checkobj.checked)
		return true
		else{
			alert(msg + "Per favore accetta i termini del contratto")
			return false
			}
		}						
	}