// CheckDate.js

//Aggiunta di VicenzoM del 17/11/2001
function aduecifre(numero){
	lunghezza=numero.length
	if (lunghezza==1) {
		numero='0'+numero
	}			
	return numero
} 

function Data1campo(datanow){
	if (!(datanow=="")) {
		cifra=new Array (3)
		cifra=datanow.split("/")
		if ((cifra[1]==null)||(cifra[2]==null))  {
				//alert("Attenzione! Data errata!!!")
				return false
		}
		
		cifra[0]=aduecifre(cifra[0])
		cifra[1]=aduecifre(cifra[1])
		if (cifra[2] < 100) {
			cifra[2] = (20 + cifra[2])
		}
		if (! isDate(cifra[0], cifra[1], cifra[2])) {
				//alert("Attenzione! Data errata!!!")
				return false
		}
	}
	return true
}

function Data3campi(gg,mm,aa){
	datatemp=gg+"/"+mm+"/"+aa
	if (!Data1campo(datatemp)) {
				return false
		}
	return true
}

function ConfrontaDate(prima,seconda){
	//prima di chiamare questa funzione lanciare Data1campo(prima) e Data1campo(seconda)
	
	// Preparo la prima data
	cifra=new Array (3)
	cifra=prima.split("/")
	if ((cifra[1]==null)||(cifra[2]==null))  {
			alert("Attenzione! Prima Data errata!!!")
			return false
	}
	
	cifra[0]=aduecifre(cifra[0])
	cifra[1]=aduecifre(cifra[1])
	if (cifra[2] < 100) {
			cifra[2] = (20 + cifra[2])
	}
	
	// Preparo la seconda data
	cifra2=new Array (3)
	cifra2=seconda.split("/")
	if ((cifra2[1]==null)||(cifra2[2]==null))  {
			alert("Attenzione! Seconda Data errata!!!")
			return false
	}

	cifra2[0]=aduecifre(cifra2[0])
	cifra2[1]=aduecifre(cifra2[1])
	if (cifra2[2] < 100) {
		cifra2[2] = (20 + cifra2[2])
	}

	//confronto fra date
	 if (cifra2[2] < cifra[2]) { 
				alert("La seconda data è antecedente alla prima data.")
	            return false 
	            }
	 else if (cifra2[2] == cifra[2])
	            if (cifra2[1] < cifra[1]) {
						alert("La seconda data è antecedente alla prima data.")
						return false 
	                    }
				else if ((cifra2[1] == cifra[1]) && (cifra2[0] < cifra[0])) {
							alert("La seconda data è antecedente alla prima data.")
							return false
						}
	return true
}

//Fine aggiunta di Vicenzom del 17/11/2001

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isInteger (s)

{   if (isNaN(s)) return false;
 
    return true;
}

function isIntegerInRange (s, a, b)
{
    if (!isInteger(s)) return false;

    var num = parseInt(s, 10);
//    var num = s - 0;
// alert("s: "+s+" num: "+num);
    return ((num >= a) && (num <= b));
}


function makeArray(n) {
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function isYear (s)
{   if (parseInt (s) <= 0) return false;
    return ((s.length == 2) || (s.length == 4));
}


function isMonth (s)
{
    return isIntegerInRange (s, 1, 12);
}


function isDay (s)
{
    return isIntegerInRange (s, 1, 31);
}

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 isDate (day, month, year)
{
// alert((isYear(year, false) && isMonth(month, false) && isDay(day, false)))
//alert(isYear(year, false) +' '+ isMonth(month, false)  +' '+  isDay(day, false))

    if (! ((isYear(year) && isMonth(month) && isDay(day)))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

///////////////////////////////////////////////////////////////////////////////
// Michele - 05/07/2003

function dateCheck(date, dateName, required) {
	if (date == "") {
		if (required) {
			alert("Campo Obbligatorio: " + dateName);
			return false;
		} else { 
			return true;
		}
	}

	cifraE = new Array (3)
	cifraE = date.split("/")
	if ((cifraE[1] == null)||(cifraE[2] == null))  {
		alert(dateName + ": Formato non corretto.\nIl formato deve essere gg/mm/aa");
		return false;
	}
// Fede - 12/05/2005 ////////////////////////////////////////////////////////////////
	if ( isNaN(cifraE[0]) || isNaN(cifraE[1]) || isNaN(cifraE[2]) ){
		alert(dateName + ": Formato non corretto.\nIl formato deve essere gg/mm/aa");
		return false;	
	}
/////////////////////////////////////////////////////////////////////////////////////

	C0 = parseInt(cifraE[0],10)
	C1 = parseInt(cifraE[1],10)
	if (! isDate(C0, C1, cifraE[2])) {
		alert(dateName + ": Formato non corretto.\nIl formato deve essere gg/mm/aa");
		return false;
	}
	
	return true;
}
/*
function dateOrder(prima, seconda) {
	if (prima == '') prima = '01/01/1900';
	if (seconda == '') seconda = '31/12/2099';
	d_prima = new Date(prima);
	d_seconda = new Date(seconda)
	return (d_prima <= d_seconda);
}
*/
function dateItalian2English(d) {
	var campiData = null;
	var delimitatori = new Array('/', '\\', '-');
	for (var delim in delimitatori) {
		if (d.indexOf(delimitatori[delim]) > -1) {
			campiData = d.split(delimitatori[delim]);
		}
	}
	if (campiData && campiData.length == 3) {
		return new Date(campiData[1] + '/' + campiData[0] + '/' + campiData[2]);
	}else {
		return false;
	}
}

function dateEnglish2Italian(d) {
	var campiData = null;
	var delimitatori = new Array('/', '\\', '-');
	for (var delim in delimitatori) {
		if (d.indexOf(delimitatori[delim]) > -1) {
			campiData = d.split(delimitatori[delim]);
		}
	}
	if (campiData && campiData.length == 3) {
		return new Date(campiData[1] + '/' + campiData[0] + '/' + campiData[2]);
	}else {
		return false;
	}
}

function dateOrder(prima, seconda) {
	if (prima == '') prima = '01/01/1900';
	if (seconda == '') seconda = '31/12/2099';
	return (dateItalian2English(prima) <= dateItalian2English(seconda));
}

//---------------------------------------------------------------------------//
// Controlla se un anno e' bisestile o meno
function bisesto(yy)
{
	return ((yy % 100 != 0 && yy % 4 == 0) || (yy % 400 == 0));
}
//---------------------------------------------------------------------------//

//---------------------------------------------------------------------------//
// Restituisce il numero della settimana corrispondente alla data richiesta
function DefWeekNum(yy,mm,dd)
{
	numd = 0;
	numw = 0;
	for (n=1; n<mm; n++)
	{
		numd += MonthLength[n];
	}
	numd = numd + dd - (9 - DefDateDay(yy,1,1));
	numw = Math.floor(numd / 7) + 1;

	if (DefDateDay(yy,1,1) == 1) { numw++; }
	return numw;
}
//---------------------------------------------------------------------------//

//---------------------------------------------------------------------------//
// Restituisce il numero del giorno della settimana corrispondente alla data richiesta
// Lun = 1; Dom = 7;
function DefDateDay(yy, mm, dd)
{
	var dw = new Date(mm + "/" + dd + "/" + yy).getDay();
	return (dw == 0) ? 7 : dw;
}
//---------------------------------------------------------------------------//

//---------------------------------------------------------------------------//
// Implementa la funzione dateAdd
function dateAdd(dp, num, date) {
	if (dp == "y") {
		return new Date(date.setFullYear(date.getFullYear() + num));
	}
	if (dp == "m") {
		return new Date(date.setMonth(date.getMonth() + num));
	}
	if (dp == "d") {
		return new Date(date.setDate(date.getDate() + num));
	}
	if (dp == "h") {
		return new Date(date.setHours(date.getHours() + num));
	}
	if (dp == "mm") {
		return new Date(date.setMinutes(date.getMinutes() + num));
	}
	if (dp == "s") {
		return new Date(date.setSeconds(date.getSeconds() + num));
	}
	if (dp == "ms") {
		return new Date(date.setMilliseconds(date.getMilliseconds() + num));
	}
}
//---------------------------------------------------------------------------//

//---------------------------------------------------------------------------//
// Implementa la funzione dateDiff
function dateDiff(dp, start, end) {
	var milliSecondi	= end - start;
	var secondi			= Math.floor(milliSecondi / 1000);
	var minuti			= Math.floor(secondi / 60);
	var ore				= Math.floor(minuti / 60);
	var giorni			= Math.floor(ore / 24);
	var anni			= end.getFullYear() - start.getFullYear();
	var mesi			= end.getMonth() - start.getMonth() + (anni * 12);

	if (dp == "y") { return anni }
	else if (dp == "m") { return mesi }
	else if (dp == "d") { return giorni }
	else if (dp == "h") { return ore }
	else if (dp == "mm") { return minuti }
	else if (dp == "s") {  return secondi }
	else if (dp == "ms") { return milliSecondi }
	else return false;

	/* CONTROLLO
	var s = '';
	s += 'Data Inizio = ' + start + '\n';
	s += 'Data Fine = ' + end + '\n';
	s += '\n';
	s += 'secondi = ' + secondi + '\n';
	s += 'milliSecondi = ' + milliSecondi + '\n';
	s += 'secondi = ' + secondi + '\n';
	s += 'minuti = ' + minuti + '\n';
	s += 'ore = ' + ore + '\n';
	s += 'giorni = ' + giorni + '\n';
	s += 'mesi = ' + mesi + '\n';
	s += 'anni = ' + anni + '\n';
	alert(s);
	*/
}
//---------------------------------------------------------------------------//

///////////////////////////////////////////////////////////////////////////////
