function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   }
   return this
}

// BOI, followed by one or more digits, followed by EOI.
var reInteger = /^\d+$/

// BOI, followed by an optional + or -, followed by one or more digits,
// followed by EOI.
var reSignedInteger = /^(\+\|\-)?\d+$/


var defaultEmptyOK = false

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 isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function checkIfInteger (obj, fldName, fldType, mandatory, minValue, maxValue)
{
  var retCode = false ;

  if (isEmpty(obj.value)){
    if (mandatory == 'm'){
      alert('Attenzione: Il campo "' + fldName + '" e\' obbligatorio');
	}else{
	  retCode = true ;
	}
  }else if (isInteger(obj.value)){
	if ((minValue != '') && (Number(obj.value) < Number(minValue))){
	  alert('Attenzione: Il valore minimo ammesso per il campo "' + fldName + '" e\' '+minValue);
	}else if ((maxValue != '') && (Number(obj.value) > Number(maxValue))){
	  alert('Attenzione: Il valore massimo ammesso per il campo "' + fldName + '" e\' '+maxValue);
	}else{
	  retCode = true ;
	}
  }else{
	alert('Attenzione: Il valore del campo "' + fldName + '" deve essere un numero intero');
  }

  if (!retCode){
	obj.focus();
  }
  return(retCode)
}

function isInteger (s)

{   
    if (isEmpty(s)) return defaultEmptyOK;

    return reInteger.test(s)
}

var reUsername = /^\w+$/ 

function isUsername(s)
{
  if (isEmpty(s)) return false ;
  return reUsername.test(s) ;
}

var rePassword = /^\w+$/ 

function isPassword(s)
{
  if (isEmpty(s)) return false ;
  return rePassword.test(s) ;
}

function isSignedInteger (s)

{   if (isEmpty(s))
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);


    else {
       return reSignedInteger.test(s)
    }
}

function isIntegerInRange (s, a, b)
{   if (isEmpty(s))
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
//
// Returns true if string s is an integer >= 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}


// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day
// form a valid date.
//
function isYear (s)
{   if (isEmpty(s))
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

function isMonth (s)
{   if (isEmpty(s))
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

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);
}



// isDay (STRING s [, BOOLEAN emptyOK])
//
// isDay returns true if string s is a valid
// day number between 1 and 31.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isDay (s)
{
    if (isEmpty(s))
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);
    f=isIntegerInRange (s, 1, 31);
    return f;
}


function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.


    //if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;
    if (! (isYear(year, false))) return false;
    if (! (isMonth(month, false)) ) return false;
    if (! (isDay(day, false) ) )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;
}


  // mandatory = M obbligatorio
  // formato = mdy mese/giorno/anno
function checkIfDate(obj, fldName, fldType, mandatory, formato)
{
	var retCode = false ;
    if (isEmpty(obj.value)){
      if (mandatory == 'm'){
        alert('Attenzione: Il campo "' + fldName + '" e\' obbligatorio');
	  }else{
	    retCode = true ;
	  }
	}else{
	  if (checkDate(obj, formato)){
	    retCode = true;
	  }else{
	    alert('Attenzione: Formato data per il campo "' + fldName + '" non riconosciuto');
	  }
	}
	if (! retCode){
	  obj.focus() ;
	}
	return(retCode);
}

function checkDate(obj, formato)
{
	var aDateString = obj.value ;
	var retCode = false ;

    if (isEmpty(aDateString)){
	  retCode = false ;
	}else{
      var aryElms = aDateString.split('/') ;
      var giorno  = parseInt(aryElms[0]) ;
      var mese    = parseInt(aryElms[1]) ;
      var anno    = parseInt(aryElms[2]) ;
      if (formato == 'mdy'){
        giorno  = parseInt(aryElms[1]) ;
        mese    = parseInt(aryElms[0]) ;
      }
      if (anno < 50) { anno += 2000 ;}
        if (isNaN(anno)) {
          var toDay = new Date() ;
            anno = toDay.getYear() ;
      }
      var d = new Date(mese+'/'+giorno+'/'+anno);
      // alert(giorno+'/'+mese+'/'+anno);
      // alert(d.getDate()+'/'+(d.getMonth()+1)+'/'+d.getYear())  ;
      if (isNaN(d) || ( giorno+'/'+mese+'/'+anno != d.getDate()+'/'+(d.getMonth()+1)+'/'+d.getYear())) {
        ;
      }else{
	    if (formato == 'mdy'){
		  obj.value = mese+'/'+giorno+'/'+anno ;
	    }else{
		  obj.value = giorno+'/'+mese+'/'+anno ;
	    }
	    retCode = true ;
	  }
    }
    return (retCode) ;
}

function checkIfString (obj, fldName, fldType, mandatory, multiLine, minLength, maxLength)
{
  var retCode = false ;

    if (isEmpty(obj.value)){
      if (mandatory == 'm'){
        alert('Attenzione: Il campo "' + fldName + '" e\' obbligatorio');
	  }else{
	    retCode = true ;
	  }
	}else{
      var isMultiline = /\r/ ;
	  isMultiline.multiline = true ;
	  if ((multiLine=='1') && isMultiline.test(obj.value)){
	    alert('Attenzione: Il campo "' + fldName + '" non puo\' essere multiriga');
	  }else if ((minLength != '') && (obj.value.length < Number(minLength))){
	    alert('Attenzione: Il campo "' + fldName + '" deve avere lunghezza minima di '+minLength+' caratteri');
	  }else if ((maxLength != '') && (obj.value.length > Number(maxLength))){
	    alert('Attenzione: Il campo "' + fldName + '" deve avere lunghezza massima di '+maxLength+' caratteri');
	  }else{
	    retCode = true ;
	  }
	}

	if (! retCode){
	  obj.focus() ;
	}
	return(retCode);
}
