// 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';
	return (prima <= seconda);
}
///////////////////////////////////////////////////////////////////////////////

