//Mostra nascondi righe di tabella
function mostra_nascondi(id_riga){
	var IE=(window.navigator.userAgent.indexOf('MSIE')>-1)?1:0;
	document.getElementById(id_riga).style.display=(document.getElementById(id_riga).style.display=='none')?((IE)?'block':'table-row'):'none';
}

//ricerca
function mostra(sel){
if(sel=='3')
{
document.getElementById('search_prod').style.display = 'block';
document.getElementById('search_prod').style.visibility = 'visible';
}
else
{
document.getElementById('search_prod').style.visibility = 'hidden';
document.getElementById('search_prod').style.display = 'none';
}
}

//funzione per selezionare tutti/nessuno gli elementi di una checkbox
var NumeroCheck=0;
function seleziona(selID) {
    for(i=1 ; i<=NumeroCheck; i++)
    {
    document.getElementById(selID+i).checked=true;
    }
}

function deseleziona(selID) {
    for(i=1 ; i<=NumeroCheck ; i++)
    {
    document.getElementById(selID+i).checked=false;
    }
}

//funzione per aprire i documenti  in una nuova finestra
function nuova_finestra(pagina) {
	window.open(pagina,'Document','scrollbars=yes,resizable=yes,status=no,width=780,height=560,location=no,toolbar=no');
	}

//funzione per aprire i siti esterni in una nuova finestra
function link_esterno(pagina) {
	window.open(pagina,'Site','scrollbars=yes,resizable=yes,status=yes,width=780,height=560,location=yes,toolbar=yes');
	}

//Funzione per popup
function popup(pagina,wdt,hgt,scr,loc,tool,menu) {
	var wdt = 800;
	var hgt = 600;
	window.open(pagina,'popup','scrollbars='+scr+',resizable=no,left='+parseInt((screen.availWidth-wdt)/2)+',top='+parseInt((screen.availHeight-hgt)/2)+',width='+wdt+',height='+hgt+',status=no,location='+loc+',toolbar='+tool+',menubar='+menu);
}

function refreshit() {
//		var frm = document.getElementById('form');
//		if(frm.id_categoria){
//			if(frm['id_categoria'].options[frm['id_categoria'].selectedIndex]){
//				var id_categoria = frm['categoria_id'].options[frm['categoria_id'].selectedIndex];
//			}
//		}
	document.form1.target="_self";
	document.form1.action="new_articolo.asp";
	document.form1.submit();
}

//funzione per l'ingrandimento delle immagini in una pop up 		
function apri(url_foto,des_foto){
	addr="/include/fullsize.htm?"+url_foto+"&"+escape(des_foto)
	alt=150
	lar=150
	rszb=(document.layers)?1:0;
	props='height='+alt+',width='+lar+',top='+parseInt((screen.availHeight-alt)/2)+',left='+parseInt((screen.availWidth-lar)/2)+',scrollbars=0,resizable='+rszb+',toolbar=0,menubar=0,location=0,status=0';
	window.open(addr,'_blank',props);
}

/*---------------------------------------------------------------------
Questa funzione accetta una variabile stringa e verifica se e una data.
La data deve essere nel formato giorno mese anno. Il giorno e il mese
devono essere di 1 o 2 cifre, l'anno deve essere di 2 o 4 cifre.
Il delimitatore deve essere / o - o .
Restituisce un oggetto con le proprieta:
	.isdate = vero o falso
	.error	= messaggio errore
	.year	= anno
	.month	= mese (1 - 12)
	.day	= giorno (1 - 31)
	.data   = oggetto data
---------------------------------------------------------------------*/
function isdate(dateStr) {
	
	var ret = new Object();
	ret.isdate = false;
	ret.error = "";
	ret.year = 0;
	ret.month = 0;
	ret.day = 0;
	ret.data = null;
	
	
    // inizio stringa
    // 1 o 2 cifre
    // / o - o .
    // 1 o 2 cifre
    // / o - o .
	// 4 cifre
	// fine stringa    
    //var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{4})$/;
    var datePat = /^\s*(\d{1,2})(\/|-|\.)(\d{1,2})(\2)(\d{2}|\d{4})\s*$/
    
    //restituisce, allo stesso modo di una matrice, i risultati di una ricerca
    //di una stringa utilizzando un oggetto Regular Expression
    var matchArray = dateStr.match(datePat); 

    if (matchArray == null) {
        ret.error = "Per favore, introduci la data nei formati gg/mm/aaaa o gg-mm-aaaa o gg.mm.aaaa";
        return ret;
    }

    day = matchArray[1];
    month = matchArray[3]; 
    year = matchArray[5];
    if(year.length == 2)
		if(parseInt(year,10) < 30)
			year = parseInt(year,10) + 2000;
		else
			year = parseInt(year,10) + 1900;

    if (month < 1 || month > 12) { // il mese deve essere compreso tra 1 e 12
        ret.error = "Il mese deve essere compreso tra 1 e 12.";
        return ret;
    }

    if (day < 1 || day > 31) {//il giorno non puo essere < 1 e > 31
        ret.error = "Il giorno deve essere compreso tra 1 e 31";
        return ret;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //i mesi aprile, giugno, settembre, novembre, hanno 30 giorni
        ret.error = "Il mese " + month + " non ha 31 giorni!";
        return ret;
    }

    if (month == 2) { // verifica se l'anno e bisestile: febbraio puo avere 29 giorni
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
			ret.error = "Febbraio " + year + " non ha " + day + " giorni!";
			return ret;
        }
    }
    isDateError = "";
    // se sono qui, la data e valida!
	ret.isdate = true;
	ret.error = "";
	ret.year = year;
	ret.month = month;
	ret.day = day;
	ret.data = new Date(year, (month - 1), day);
    
    return ret;
}

function calcola_giorni()
{
    if(document.getElementById("arrivo").value.length == 0 || document.getElementById("partenza").value.length == 0) return;
    var d1 = isdate(document.getElementById("arrivo").value);
    var d2 = isdate(document.getElementById("partenza").value);
    if((d1.isdate && d2.isdate) && (d2.data.getTime() - d1.data.getTime() > 0))
    {
        var differenza = d2.data.getTime() - d1.data.getTime();
        var giorni = Math.floor(differenza /(1000*60*60*24));
        document.getElementById("nnotti").value = giorni;
    }
    else 
    {
        document.getElementById("nnotti").value = "";
        alert("Date non valide");
    }
    
}

window.onload = function()
{
    if ((document.getElementById("arrivo") !== null) && (document.getElementById("partenza") !== null)) {
    document.getElementById("arrivo").onchange = calcola_giorni;
    document.getElementById("partenza").onchange = calcola_giorni;
    }
}
