// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;
var value_nr_numerico = '99';
var value_nr_alfabetico = 'XX';

//script per il menu collassabile
// da 0 a n livelli
// 0 = padre
// 1 = primo figlio....
var livelli = 50;
var chiudi_red=false;
var ultimo = new Array(livelli);
for(i=0;i<=livelli;i++){
	ultimo[i] = "";
}

var ultimoimg = new Array(livelli);
for(i=0;i<=livelli;i++){
	ultimoimg[i] = "";
}



function visualizzaon(domid,imgid,tipo) {
var nodeObj = document.getElementById(domid);
var imgObj = document.getElementById(imgid);
if(nodeObj == null) {
	return;
}
nodeObj.style.display = 'block';
if (tipo) {imgObj.src = '/immagini/meno_red.gif';}
else {imgObj.src = '/immagini/meno.gif';}
}

function visualizzaoff(domid,imgid,tipo) {
var nodeObj = document.getElementById(domid);
var imgObj = document.getElementById(imgid);
if(nodeObj == null) {
	return;
}
nodeObj.style.display = 'none';
if (tipo) {imgObj.src = '/immagini/piu_red.gif';}
else {imgObj.src = '/immagini/piu.gif';}
}

function aprichiudi(nome,i,idimg,imgtipo){
if (ultimo[i] == nome) { // sto chiudendo quello aperto
	visualizzaoff(ultimo[i],ultimoimg[i],imgtipo);
	ultimo[i] = "";
	ultimoimg[i] = "";
	}
else if (ultimo[i] == "") { // devo aprire quello dove ho cliccato ed erano tutti chiusi
	visualizzaon(nome,idimg,imgtipo); 
	ultimo[i] = nome;
	ultimoimg[i] = idimg;
	}
else { // devo chiudere quello aperto ed aprire quello dove ho cliccato
if (ultimo[i] =='item8-1') {
		visualizzaoff(ultimo[i],ultimoimg[i],true);
	}
	else {
		visualizzaoff(ultimo[i],ultimoimg[i],false);
	}
	visualizzaon(nome,idimg,imgtipo);
	ultimo[i] = nome;
	ultimoimg[i] = idimg;
	}
}
// FINE MENU
//********************************************
// IS INTEGER
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;
}

//********************************************
// stripCharsInBag
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;
}
//********************************************
// daysInFebruary
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 );
}
//********************************************
// DaysArray
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
}
//********************************************
// isDate
function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)	
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.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("La data deve essere nel formato: gg-mm-aaaa.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Prego inserire un giorno valido")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Prego inserire un mese valido.")
		return false
	}

	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Prego inserire un anno di 4 cifre tra "+minYear+" e "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Prego inserire una data valida.")
		return false
	}
return true
}
//********************************************
// ValidateForm
function ValidateForm(name, required, msg){
	var dt = eval("document.forms[0]." + name);
	if(!required && dt.value.replace(/^\s*|\s*$/g,"") == "") {
		return true;
	}
	if (required && dt.value.replace(/^\s*|\s*$/g,"") == ""){
		alert('Il campo "' + msg + '" è obbligatorio.');
		dt.focus();
		return false;
	}
    return true;
 }
//********************************************
// isSelected
function isSelected(name, msg){
	var dt = eval("document.forms[0]." + name);
	if(dt.value == value_nr_numerico || dt.value == value_nr_alfabetico) {
		alert('Il campo "' + msg + '" è obbligatorio.');
		dt.focus();
		return false;
	}
    return true;
 }
//********************************************
// isNumeric
function isNumeric(name, required, msg) {
	var dt = eval("document.forms[0]." + name);
	dt.value = dt.value.replace(",",".");
	if(!required && dt.value.replace(/^\s*|\s*$/g,"") == "") {
		return true;
	}
	if(required && dt.value.replace(/^\s*|\s*$/g,"") == "") {
		alert('Il campo "' + msg + '" è obbligatorio.');
		dt.focus();
		return false;
	}
	if(isNaN(dt.value)) {
		alert('Il campo "' + msg + '" deve essere numerico.');
		dt.focus();
		return false;
	}
    
	return true;
}
//********************************************
// ControllaMail
function ControllaMail(EmailAddr){
 var Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
 if (Filtro.test(EmailAddr))
    return true;
 else
    return false;
}
//********************************************
// ControllaPIVA
function ControllaPIVA(pi)
{
    //if( pi == '' )  return '';
    if( pi.length != 11 )
        return "La lunghezza della partita IVA non è " +
            "corretta: la partita IVA dovrebbe essere lunga\n" +
            "esattamente 11 caratteri.\n";
    validi = "0123456789";
    for( i = 0; i < 11; i++ ){
        if( validi.indexOf( pi.charAt(i) ) == -1 )
            return "La partita IVA contiene un carattere non valido `" +
                pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
    }
    s = 0;
    for( i = 0; i <= 9; i += 2 )
        s += pi.charCodeAt(i) - '0'.charCodeAt(0);
    for( i = 1; i <= 9; i += 2 ){
        c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
        if( c > 9 )  c = c - 9;
        s += c;
    }
    if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
        return "La partita IVA non è valida:\n" +
            "il codice di controllo non corrisponde.\n";
    return '';
}
//********************************************
// check_text_area
function check_text_area(name, msg) {
	var dt = eval("document.forms[0]." + name);
	if(dt.value.length > 255) {
		alert('Il campo "' + msg + '" non deve superare 255 caratteri.');
		dt.focus();
		return false;
	}
	return true;
}
//********************************************
// toggle
function toggle(obj) {
	var el = document.getElementById(obj);
	if (el) {
		if ( el.style.display != 'none' ) {
			el.style.display = 'none';
		} else {
			el.style.display = '';
		}
	}
}
//********************************************
// cambiaImg
function cambiaImg(obj) {
	var el = document.getElementById(obj);
	if (el) {
		if ( el.style.backgroundImage != 'url(/immagini/piu.gif)' ) {
			el.style.backgroundImage = 'url(/immagini/piu.gif)';
		} else {
			el.style.backgroundImage = 'url(/immagini/meno.gif)';
		}
	}
}
//********************************************
// controllaHomepage
function controllaHomepage(){
	if (!ValidateForm('piva', true, 'P.IVA Impresa')) {
		return false;
	}
	if (!ValidateForm('pwd', true, 'Password')) {
		return false;
	}

	return document.aris.submit;
}
//********************************************
// MM
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
/* Functions that swaps images. */
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

/* Functions that handle preload. */
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//********************************************
// creapopup
function creapopup(pagina,nome,larghezza,altezza,scrbar,tolbar) {
fine=window.open(pagina, nome, 'width=' + larghezza + ',height=' + altezza + ',scrollbars=' + scrbar + ',toolbar=' + tolbar +'\'');
}
//********************************************
// confirmRequest
function confirmRequest() {
	if(!confirm('Sei sicuro di eliminare la voce?')) {
		return false;
	}
	return true;
}
//********************************************
// confirmDeleteQst
function confirmDeleteQst() {
	if(!confirm('Sei sicuro di voler eliminare il questionario relativo all\' impresa?')) {
		return false;
	}
	if(!confirm('Confermi l\' operazione?')) {
		return false;
	}
	return true;
}
//********************************************
// mod_stato_qst -- nell'elenco questionari validati e da validare
function mod_stato_qst(field_name, field_value) {
	var msg = "";
	if(eval("document.forms[0]." + field_name + ".checked")) {
		msg = 'Sei sicuro di validare il questionario?';
		document.forms[0].validato.value = 1;
	}
	else {
		msg = 'Sei sicuro di togliere la validazione al questionario?';
		document.forms[0].validato.value = 0;
	}
	if(!confirm(msg)) {
		if(eval("document.forms[0]." + field_name + ".checked")) eval("document.forms[0]." + field_name).checked = false;
		else eval("document.forms[0]." + field_name).checked = true;
		return false;
	}
	document.forms[0].id_questionario.value = field_value;
	return document.forms[0].submit();
}

