// JScript source code

function imposta_data_normalizzata(campo,formatodata,formname)
{
  if ((formatodata == 'DD/MM/YYYY') || (formatodata == 'DD/MM/YY')) {
	var valore=document.forms[formname][campo].value;
	if (valore != '') {
		var risultato = normalizza_data(valore,formatodata);
		if (risultato == '') {
			alert('Formato data errato!');
			document.forms[formname][campo].value = '';
			document.forms[formname][campo].focus();
		}else{
			document.forms[formname][campo].value = risultato;
		}
	}
  }	
}


function normalizza_data(dtStr)
{
    var strDay;
    var strMonth;
    var strYear;
    var prosegui;

    prosegui = '';
    
    if (dtStr.length == 6) {
        strDay=dtStr.substr(0,2);
	    strMonth=dtStr.substr(2,2);
	    strYear=dtStr.substr(4,2);
	    if (strYear<='50'){
	        dtStr = strDay + '/' + strMonth + '/20' + strYear;
	    }else{
		    dtStr = strDay + '/' + strMonth + '/19' + strYear;	
	    }
	    prosegui = '1';
    }
    
    if ((dtStr.indexOf("/") != -1) & (dtStr.length == 8)) {
        strYear=dtStr.substr(6,2);
	    if (strYear<='50'){
		    dtStr= dtStr.substr(0,6) + '20' + strYear;
	    }else{
	        dtStr = dtStr.substr(0,6) + '19' + strYear;	
	    }
	    prosegui = '2';
    }
    
    if ((dtStr.indexOf("/") == -1) & (dtStr.length == 8)) {
        strDay=dtStr.substr(0,2);
	    strMonth=dtStr.substr(2,2);
	    strYear=dtStr.substr(4,4);
	    dtStr = strDay + '/' + strMonth + '/' + strYear;
	    prosegui = '3';
    }
    
    if (dtStr.length == 10){
        prosegui = '4';
    }
    
    if (prosegui != '') {
        strDay=dtStr.substr(0,2);
        strMonth=dtStr.substr(3,2);
        strYear=dtStr.substr(6,4);
 
 	/*
        numDay=parseInt(strDay);
        numMonth=parseInt(strMonth);
        numYear=parseInt(strYear);

	alert(strDay);
	alert(numDay);
	alert(strMonth);
	alert(numMonth);
	alert(strYear);
	alert(numYear);
	
	*/
        numDay=(strDay);
        numMonth=(strMonth);
        numYear=(strYear);

        primoslash=dtStr.substr(2,1);
        secondoslash=dtStr.substr(5,1);

        primalunghezza=strDay.length;
        secondalunghezza=strMonth.length;
        terzalunghezza=strYear.length;

        if ((primalunghezza == 2) & (primoslash == "/") & (numDay >= 1) & (numDay <= 31) & (secondalunghezza ==2 ) & (secondoslash == "/") & (numMonth >= 1) & (numMonth <= 12) & (terzalunghezza == 4) & (numYear >= 1800) & (numYear <= 3000))
        {
            prosegui = '5';
        }
        else
        {
            prosegui = '';
        }
    }
    
	if (prosegui == '') {
		return '';
	}else{
		return dtStr;	
	}
	
alert(dtStr);
alert(prosegui);

return true
}

function normalizza_dataOLD(dtStr){
	var strDay;
	var strMonth;
	var strYear;
	
	var reg = /[0-3][0-9][/][0-1][0-9][/][1-2][0-9]{3}/i;
    var isok = reg.test(dtStr);
	if (!((isok) & (dtStr.length == 10))) {
		reg = /[0-3][0-9][/][0-1][0-9][/][0-9]{2}/i;
		isok = reg.test(dtStr);
		if ((isok) & (dtStr.length == 8)) {
			strYear=dtStr.substr(6,2);
			if (strYear<='50'){
			  dtStr= dtStr.substr(0,6) + '20' + strYear;
			}else{
			  dtStr = dtStr.substr(0,6) + '19' + strYear;	
			}
		}else{
			reg = /[0-3][0-9][0-1][0-9]{3}/i;
			isok = reg.test(dtStr);
			if ((isok) & (dtStr.length == 6)) {
				strDay=dtStr.substr(0,2);
				strMonth=dtStr.substr(2,2);
				strYear=dtStr.substr(4,2);
				if (strYear<='50'){
					dtStr = strDay + '/' + strMonth + '/20' + strYear;
				}else{
					dtStr = strDay + '/' + strMonth + '/19' + strYear;	
				}
			}else{
				reg = /[0-3][0-9][0-1][0-9][1-2][0-9]{3}/i;
				isok = reg.test(dtStr);
				if ((isok) & (dtStr.length == 8)) {
					strDay=dtStr.substr(0,2);
					strMonth=dtStr.substr(2,2);
					strYear=dtStr.substr(4,4);
					dtStr = strDay + '/' + strMonth + '/' + strYear;
				} else {
					isok = false;
				}
			}
		}
	}
	if (!(isok)) {
		return '';
	}else{
		return dtStr;	
	}

return true
}

