
var isNav4 = false, isNav5 = false, isIE4 = false
var strSeperatorOra = ":"; 
var vHourType = 3; // Global value for type of hour format
//                1 = mm/dd/yyyy
//                2 = yyyy/dd/mm  (Unable to do hour check at this time)
//                3 = hh:mm:ss
var err = 0; // Set the error code to a default of zero
if(navigator.appName == "Netscape") 
{
	if (navigator.appVersion < "5") 	
	{
		isNav4 = true;
		isNav5 = false;
	}
	else
		if (navigator.appVersion > "4") 
		{
			isNav4 = false;
			isNav5 = true;
   		}
}
else 
{
	isIE4 = true;
}



function HourFormat(vHourName, vHourValue, e, hourCheck, hourType) 
{
	vHourType = hourType;
	// vHourName = object name
	// vHourValue = value in the field being checked
	// e = event
	// hourCheck 
	// True  = Verify that the vHourValue is a valid hour
	// False = Format values being entered into vHourValue only
	// vHourType
	// 1 = mm/dd/yyyy
	// 2 = yyyy/mm/dd
	// 3 = dd/mm/yyyy
	//Enter a tilde sign for the first number and you can check the variable information.
	var whichCode = (window.Event) ? e.which : e.keyCode;
	// Check to see if a seperator is already present.
	// bypass the hour if a seperator is present and the length greater than 8
	if (vHourValue.length > 8 && isNav4) 
	{
		if ((vHourValue.indexOf("-") >= 1) || (vHourValue.indexOf("/") >= 1))
			return true;
	}
	//Eliminate all the ASCII codes that are not valid
	var alphaCheck = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-";
	if (alphaCheck.indexOf(vHourValue) >= 1) 
	{
		if (isNav4) 
		{
			vHourName.value = "";
			vHourName.focus();
			vHourName.select();
			return false;
		}
	else
	 {
		vHourName.value = vHourName.value.substr(0, (vHourValue.length-1));
		return false;
	  }
	}
	if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
		return false;
	else {
	//Create numeric string values for 0123456789/
	//The codes provided include both keyboard and keypad values
		var strCheck = '47,48,49,5454450,51,52,53,54,55,56,57,58,59,95,96,97,98,99,100,101,102,103,104,105';
		if (strCheck.indexOf(whichCode) != -1) 
		{
			if (isNav4) 
			{
				if (((vHourValue.length < 6 && hourCheck) || (vHourValue.length == 7 && hourCheck)) && (vHourValue.length >=1)) 
				{
					alert("Ora non valida");
					vHourName.value = "";
					vHourName.focus();
					vHourName.select();
					return false;
				}
			if (vHourValue.length == 6 && hourCheck) 
			{
				var mDay = vHourName.value.substr(2,2);
				var mMonth = vHourName.value.substr(0,2);
				var mYear = vHourName.value.substr(4,4)
    		var vHourValueCheck = mMonth+strSeperatorOra+mDay+strSeperatorOra+mYear;
  			if (!hourValid(vHourValueCheck)) 
      	{
        	alert("Ora non valida");
          vHourName.value = "";
  				vHourName.focus();
    			vHourName.select();
      		return false;
  			}
    	return true;
  	}
	else 
	{
		// Reformat the hour for validation and set hour type to a 1
		if (vHourValue.length >= 8  && hourCheck) 
		{
			if (vHourType == 1) // mmddyyyy
			{
				var mDay = vHourName.value.substr(2,2);
				var mMonth = vHourName.value.substr(0,2);
				var mYear = vHourName.value.substr(4,4)
				vHourName.value = mMonth+strSeperatorOra+mDay+strSeperatorOra+mYear;
			}
		if (vHourType == 2) // yyyymmdd
		{
			var mYear = vHourName.value.substr(0,4)
			var mMonth = vHourName.value.substr(4,2);
			var mDay = vHourName.value.substr(6,2);
			vHourName.value = mYear+strSeperatorOra+mMonth+strSeperatorOra+mDay;
		}
		if (vHourType == 3) // ddmmyyyy
		{
			var mMonth = vHourName.value.substr(2,2);
			var mDay = vHourName.value.substr(0,2);
			var mYear = vHourName.value.substr(4,4)
			vHourName.value = mDay+strSeperatorOra+mMonth+strSeperatorOra+mYear;
		}
		//Create a temporary variable for storing the HourType and change
		//the HourType to a 1 for validation.
		var vHourTypeTemp = vHourType;
		vHourType = 1;
		var vHourValueCheck = mMonth+strSeperatorOra+mDay+strSeperatorOra+mYear;
		if (!hourValid(vHourValueCheck)) 
		{
			alert("Ora non valida");
			vHourType = vHourTypeTemp;
			vHourName.value = "";
			vHourName.focus();
			vHourName.select();
			return false;
		}
		vHourType = vHourTypeTemp;
		return true;
	}
	else
	{
		if (((vHourValue.length < 8 && hourCheck) || (vHourValue.length == 9 && hourCheck)) && (vHourValue.length >=1)) 
		{
			alert("Ora non valida");
			vHourName.value = "";
			vHourName.focus();
			vHourName.select();
			return false;
	     }
	 }
 }
}
else 
{
// Non isNav Check
if (((vHourValue.length < 8 && hourCheck) || (vHourValue.length == 9 && hourCheck)) && (vHourValue.length >=1)) {
alert("Ora non valida");
vHourName.value = "";
vHourName.focus();
return true;
}

// Reformat hour to format that can be valihourd. mm/dd/yyyy
if (vHourValue.length >= 8 && hourCheck) {
// Additional hour formats can be entered here and parsed out to
// a valid hour format that the validation routine will recognize.
if (vHourType == 1) // mm/dd/yyyy
{
var mMonth = vHourName.value.substr(0,2);
var mDay = vHourName.value.substr(3,2);
var mYear = vHourName.value.substr(6,4)
}
if (vHourType == 2) // yyyy/mm/dd
{
var mYear = vHourName.value.substr(0,4)
var mMonth = vHourName.value.substr(5,2);
var mDay = vHourName.value.substr(8,2);
}
if (vHourType == 3) // dd/mm/yyyy
{
var mDay = vHourName.value.substr(0,2);
var mMonth = vHourName.value.substr(3,2);
var mYear = vHourName.value.substr(6,4)
}
// Create temp. variable for storing the current vHourType
var vHourTypeTemp = vHourType;
// Change vHourType to a 1 for standard hour format for validation
// Type will be changed back when validation is completed.
vHourType = 1;
// Store reformatted hour to new variable for validation.
var vHourValueCheck = mMonth+strSeperatorOra+mDay+strSeperatorOra+mYear;
if (!hourValid(vHourValueCheck)) {
alert("Ora non valida");
vHourType = vHourTypeTemp;
vHourName.value = "";
vHourName.focus();
return true;
}
vHourType = vHourTypeTemp;
return true;
}
else {
if (vHourType == 1) {
if (vHourValue.length == 2) {
vHourName.value = vHourValue+strSeperatorOra;
}
if (vHourValue.length == 5) {
vHourName.value = vHourValue+strSeperatorOra;
   }
}
if (vHourType == 2) {
if (vHourValue.length == 4) {
vHourName.value = vHourValue+strSeperatorOra;
}
if (vHourValue.length == 7) {
vHourName.value = vHourValue+strSeperatorOra;
   }
} 
if (vHourType == 3) {
if (vHourValue.length == 2) {
vHourName.value = vHourValue+strSeperatorOra;
}
if (vHourValue.length == 5) {
vHourName.value = vHourValue+strSeperatorOra;
   }
}
return true;
   }
}
if (vHourValue.length == 10&& hourCheck) {
if (!hourValid(vHourName)) {
// Un-comment the next line of code for debugging the hourValid() function error messages
//alert(err);  
alert("Ora non valida");
vHourName.focus();
vHourName.select();
   }
}
return false;
}
else {
// If the value is not in the string return the string minus the last
// key entered.
if (isNav4) {
vHourName.value = "";
vHourName.focus();
vHourName.select();
return false;
}
else
{
vHourName.value = vHourName.value.substr(0, (vHourValue.length-1));
return false;
         }
      }
   }
}

function hourValid(objName) {
var strHour;
var strHourArray;
var strOra;
var strMinuti;
var strSecondi;
var intora;
var intminuti;
var intsecondi;
var booFound = false;
var hourfield = objName;
var strSeparatorArray = new Array(":");
var intElementNr;
// var err = 0;
strHour = objName;
if (strHour.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strHour.indexOf(strSeparatorArray[intElementNr]) != -1) {
strHourArray = strHour.split(strSeparatorArray[intElementNr]);
if (strHourArray.length != 3) {
err = 1;
return false;
}
else {
strMinuti = strHourArray[0];
strOra = strHourArray[1];
strSecondi = strHourArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strHour.length>5) {
strOra = strHour.substr(0, 2);
strMinuti = strHour.substr(2, 2);
strSecondi = strHour.substr(4);
   }
}
/*
strTemp = strOra;
strOra = strMinuti;
strMinuti = strTemp;
*/
intora = parseInt(strOra, 10);
if (isNaN(intora)) {
err = 2;
return false;
}
if (!(intora >= 0 && intora <= 23))
{
err = 2;
return false;
}

intminuti = parseInt(strMinuti, 10);
if (isNaN(intminuti)) {
err = 3;
return false;
}
if (!(intminuti >= 0 && intminuti <= 59))
{
err = 2;
return false;
}

intsecondi = parseInt(strSecondi, 10);
if (isNaN(intsecondi)) {
err = 4;
return false;
}
if (!(intsecondi >= 0 && intsecondi <= 59))
{
err = 2;
return false;
}
return true;
}
	
	function seleziona_ora(tipoOra,oggettoOra)
	{
		if (tipoOra == 1) // mm/dd/yyyy
		{
		var mMonth = oggettoOra.value.substr(0,2);
		var mDay = oggettoOra.value.substr(3,2);
		var mYear = oggettoOra.value.substr(6,4)
		}
		if (tipoOra == 2) // yyyy/mm/dd
		{
		var mYear = oggettoOra.value.substr(0,4)
		var mMonth = oggettoOra.value.substr(5,2);
		var mDay = oggettoOra.value.substr(8,2);
		}
		if (tipoOra == 3) // dd/mm/yyyy
		{
		var mDay = oggettoOra.value.substr(0,2);
		var mMonth = oggettoOra.value.substr(3,2);
		var mYear = oggettoOra.value.substr(6,4)
		}	
				
		var strSeperatorOra = "-";
		var oggettoOra_r = mMonth+strSeperatorOra+mDay+strSeperatorOra+mYear;		
		
		var risultato_ora = hourValid(oggettoOra_r);		
		
		return risultato_ora;
	}
		
//  End -->
