function msieversion()
// return Microsoft Internet Explorer (major) version number, or 0 for others.
// This function works by finding the "MSIE " string and extracting the version number
// following the space, up to the decimal point for the minor version, which is ignored.
{
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf ( "MSIE " );
    if ( msie > 0 )        // is Microsoft Internet Explorer; return version number
        return parseInt ( ua.substring ( msie+5, ua.indexOf ( ".", msie ) ) );
    else
        return 0;    // is other browser
}

function netscapeVersion()
// returns netscape browser version
{
    if ( window.navigator.appName != "Netscape" )
        return 0 ;
    var nsver = window.navigator.appVersion;
    return parseInt ( nsver.substring ( 0, nsver.indexOf ( ".") ) );
}

// Error message produced by input validation routines
var FieldValidationErr ;    

function IsDigitEx(c)
{
    if ((c=='0') || (c=='1') || (c=='2') || (c=='3') || (c=='4') || (c=='5') || (c=='6') || (c=='7') || (c=='8') || (c=='9'))
    {
	return true;
	}
    else {
	return false;
	}


}

function IsDigit(c)
{
    return (c >= '0' && c <= '9');
}


function IsLetter(c)
{
    return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}

function IsNumber(s)
{
    var i = 0;
    var fdot = false;
    var fsign = false ;
    var fdigit = false ;

    var len = s.length ;

    // skip spaces    
    while(i < len && s.charAt(i) == ' ') i++ ;
    
    // look for sign
    if (i < len && (s.charAt(i) == '+' || s.charAt(i) == '-')){
        i++ ;
        fsign = true ;
    }

    for (; i < s.length; i++) {
        var c = s.charAt(i);
        if (c == '.') {
            if (fdot) return false; 
            fdot = true;
        }
        else if ( IsDigit(c)) 
            fdigit = true ;
        else {
            // check if only blanks are left
            while(i < len && s.charAt(i) == ' ') i++ ;
            if (i < len)
                return false ;
            else
                break;
        }
    }
    
    if ( (fsign || fdot) && fdigit == false )
        return false ;
    return true;
}

function myParseInt(str)
{
    // we need this routine becaues parseInt("08") returns 0
    // as "08" is an invalid octal number
    
    // skip zero
    var i = 0;    

    while(i < str.length && str.charAt(i) == '0' )i++ ;
    
    if ( i < str.length )
        return parseInt(str.substring(i)); ;
    
    return 0 ;
    
}


function IsLiteral(anArray, aString)
{
  var k
  if (aString.length < 3) return -1;
  for (k = 0; k < anArray.length; k++)
    if (anArray[k].substring(0, aString.length) == aString.toLowerCase()) return k;
  return -1;
}

function encodeHTML(aForm, src, dest, bUseNBSP)
{
  var srcField = GetFormField(aForm, src)
  if ( srcField == 0 ) return 

  var destField = GetFormField(aForm, dest)
  if ( destField == 0 ) return 
  
  destField.value =  encodeHTMLChars(srcField.value, bUseNBSP) 
}

function encodeHTMLChars(src, bUseNBSP)
{
  if ( src == null)
    return src;
    
  var i, c, sResult='';
  for (i = 0; i < src.length; i++){
    c = src.charAt(i)  
    switch (c)
    {
      case '\'':
          sResult += "&#39;";
          break;

      case '\"':
          sResult += "&quot;";
          break;

      case '<':
          sResult += "&lt;";
          break;

      case '>':
          sResult += "&gt;";
          break;

      case '&':
          sResult += "&amp;";
          break;

      case ' ':
          if (bUseNBSP)
            sResult += "&nbsp;";
          else
            sResult += c;
          break;

      case '\r':
          sResult += '\r';
          if ( (i+1) < src.length) {
            if ( src.charAt(i+1) == '\n' )  {
              sResult += '\n';            
              i++ ;
            }
          }
          sResult += "<BR>";          
          break;
            
      case '\n':
          sResult += "\n<BR>";      
          break;
          
      default:
          sResult += c;
          break;
      }
   }
   return sResult
}

function stripCRLFChars(src)
{
  if ( src == null)
    return src;
    
  var i, c, sResult='';
  for (i = 0; i < src.length; i++){
    c = src.charAt(i)  
    switch (c)
    {
      case '\r':
          sResult += "";
          break;
            
      case '\n':
          sResult += "";
          break;
          
      case '\'':
          sResult += "&#39;";
          break;

      default:
          sResult += c;
          break;
      }
   }
   return sResult
} 

//-------------------------------------
// DATE FORMAT VALIDATION ROUTINES
//-------------------------------------

// Date-Time related specification

function mFormDateOnly(month, day, year)
{
    var i = 0
    var format = this.sShortDate
    var ch     
    var resultStr = ""    
    var actualName = ""
    
    if ( !format || format.length == 0 )
        return mFormDateOnlyDefault(month, day, year)

    while ( i < format.length ) {
        if ( format.charAt(i).toLowerCase() == 'd'){
            if ( format.substr(i, 4).toLowerCase() == 'dddd') {
                i += 4        
                var date1 = new Date(year, month-1, day, 9, 0, 0)                
                actualName = this.sDayNames[5+date1.getDay()] ;
                resultStr += actualName.charAt(0).toUpperCase() + actualName.substring(1)                   
            }
            else if ( format.substr(i, 3).toLowerCase() == 'ddd') {
                i += 3
                var date1 = new Date(year, month-1, day, 9, 0, 0)
                actualName = this.sDayNames[5+date1.getDay()] ;
                resultStr += actualName.charAt(0).toUpperCase() + actualName.substr(1,2)                   
            }
            else if ( format.substr(i, 2).toLowerCase() == 'dd') {
                i += 2            
                if ( day < 10 )
                    resultStr += "0" 
                resultStr += day                 
            }
            else {
                resultStr += day 
                i += 1
            }
        }
        else if ( format.charAt(i).toLowerCase() == 'm'){        
            if ( format.substr(i, 4).toLowerCase() == 'mmmm') {
                i += 4        
                actualName = this.sMonthNames[month-1] ;
                resultStr += actualName                   
            }
            else if ( format.substr(i, 3).toLowerCase() == 'mmm') {
                i += 3
                actualName = this.sMonthNames[month-1] ;
                resultStr += actualName.substr(0,3)                   
            }
            else if ( format.substr(i, 2).toLowerCase() == 'mm') {
                i += 2            
                if ( month < 10 )
                    resultStr += "0" 
                resultStr +=  month                 
            }
            else {
                resultStr +=  month 
                i += 1
            }
        }
        else if ( format.charAt(i).toLowerCase() == 'y'){        
            if ( format.substr(i, 4).toLowerCase() == 'yyyy') {
                i += 4        
                if (year <= 50) 
                    year = 2000 + year;
                else if (year < 100) 
                    year = 1900 + year;
                resultStr += year                
            }
            else if ( format.substr(i, 2).toLowerCase() == 'yy') {
                i += 2
                if ( year%100 < 10 )
                    resultStr += "0"
                resultStr += year%100                            
            }
            else {
                i += 1
                resultStr += year%10                
            }
        
        }
        else if ( format.charAt(i).toLowerCase() == 'g'){        
            break ;
        }
        while( i < format.length && 
                format.charAt(i).toLowerCase() != 'd' && 
                format.charAt(i).toLowerCase() != 'm' &&
                format.charAt(i).toLowerCase() != 'y' &&                
                format.charAt(i).toLowerCase() != 'g' ) {
            resultStr += format.charAt(i)
            i++ ;
        }                                      
    }
    return resultStr ;
}


function mFormDateOnlyDefault(month, day, year)
{
    if ( this.iDate == 0 ) 
        return "" + month + this.sDate + day + this.sDate + year ;
    else if ( this.iDate == 1 ) 
        return "" + day + this.sDate + month + this.sDate + year ;
    else  if ( this.iDate == 2 ) 
       return "" + year + this.sDate + month + this.sDate + day ;

    alert("The date format ordering specified in 'iDate' is not supported") ;
    return "" ;
}

function mFormDateObject(dateInStr)
{
    var obj  = IsDate(dateInStr, true);  // may return true, false or a new date object
    if ( obj == false || obj == true)
        return new Date() ;
    return obj;
}


// iPM can be 0->AM, 1->PM or 2->Not specified 
// This routine does not validate the data

function mFormTimeStr(hours, minutes, seconds, iPM, ignoreSec)
{
    var i = 0
    var format = this.sTimeFormat
    var ch     
    var resultStr = ""    
    
    if ( !format || format.length == 0 )
        return mFormTimeStrDefault(hours, minutes, seconds, iPM)
                                                             
    if ( i < format.length && format.charAt(i).toLowerCase() == 'h'){
        if ( format.charAt(i) == 'H' ) {   
            if ( iPM == 1 && hours < 12 )
                hours += 12 
        }
        else {
            if ( hours > 12 ) {
                hours -= 12
                iPM = 1        
            } 
        }
        
        if ( ((i+1) <  format.length) && format.charAt(i) ==  format.charAt(i+1) ) {
            // always two digit hours        
            if ( hours < 10 )
                resultStr = "0"                
            i += 2 
        }
        else
            i++ 
            
        resultStr += hours 
        
    }
    while( i < format.length && 
            format.charAt(i).toLowerCase() != 'm' && 
            format.charAt(i).toLowerCase() != 's' &&
            format.charAt(i).toLowerCase() != 't' ) {
        resultStr += format.charAt(i)
        i++ ;
    }                                      
    if ( i < format.length && format.charAt(i).toLowerCase() == 'm'){
        
        if ( format.charAt(i) ==  format.charAt(i+1) ) {
            // always two digit minutes        
            if ( minutes < 10 )
                resultStr += "0"                
            i += 2 
        }
        else
            i++ 
            
        resultStr += minutes 
        
    }
    while( i < format.length && (
            format.charAt(i).toLowerCase() != 's' &&
            format.charAt(i).toLowerCase() != 't' )) {
            
        if ( ignoreSec && (format.indexOf(this.sTime, i) == i))
            i += this.sTime.length;
        else {
            resultStr += format.charAt(i);
            i++ ;
        }
    }                                      
    if ( i < format.length && format.charAt(i).toLowerCase() == 's'){
        
        if ( ((i+1) <  format.length) &&  format.charAt(i) ==  format.charAt(i+1) ) {
            // always two digit minutes        
            if ( ignoreSec == false && seconds < 10 )
                resultStr += "0"                
            i += 2 
        }
        else
            i++ 
        if ( ignoreSec == false)            
          resultStr += seconds
    }
    while( i < format.length && (format.charAt(i).toLowerCase() != 't' )) {
        resultStr += format.charAt(i)
        i++ ;
    }                                      
    if ( i < format.length && format.charAt(i).toLowerCase() == 't'){

        if ( hours > 12 || (hours == 12 && iPM == 2))
            iPM = 1 ;
        else if ( iPM == 2 )
            iPM = 0 ;
    
        
        if (  ((i+1) <  format.length) && format.charAt(i) ==  format.charAt(i+1) ) {
            // full am/pm string
            i += 2 
            resultStr +=  ((iPM == 0) ?  this.s1159 : this.s2359)            
        }
        else {
            i++ 
            resultStr +=  ((iPM == 0) ?  this.s1159.charAt(0): this.s2359.charAt(0))            
        }        
    }
    while( i < format.length ) {
        resultStr += format.charAt(i)
        i++ ;
    }                                      
    
    return resultStr ;     
}


function mFormTimeStrDefault(hours, minutes, seconds, iPM)
{
    if ( this.iTime == 0 ) {    // 12 hour clock
        if ( hours > 12 || (hours == 12 && iPM == 2)) {
            iPM = 1 ;
            if ( hours > 12 )
                hours -= 12 ;
        }
        else if ( iPM == 2 )
            iPM = 0 ;
    }
    else { // this.iTime == 1     .. 24 hour clock                         
        if ( iPM == 1 && hours < 12 )
            hours += 12 
    }

    var resultStr = ""
    
    if ( hours < 10 )
        resultStr += " " 
    
    resultStr += hours + this.sTime
    
    if ( minutes < 10 )            
        resultStr += "0"

    resultStr += minutes
    
    if ( seconds > 0 ) 
        resultStr +=  this.sTime + (seconds < 10) ? "0" : "" + seconds

    if ( this.iTime == 0 )
        resultStr +=  " " + ((iPM == 0) ?  this.s1159 : this.s2359)
    
    return resultStr ;     
}

function mFormatAWDate(src)
{
  if ( src == null)
    return ''
  if ( src.length != 15)    // not a valid AWDate
    return src

  var i
  for(i=0; i < 14; i++) {
    if ( !IsDigit(src.charAt(i)) )     // not a valid AWDate
      return src
  }

  var dateType = src.charAt(14).toUpperCase()  
  if ( dateType == 'N' )
    return ''  
  if ( dateType != 'A' && dateType != 'O' )
    return src         // not a valid AWDate
    
  
  var year, month, date  
  if ( dateType == 'A' ){
    year = myParseInt(src.substr(0, 4))
    month = myParseInt(src.substr(4, 2))
    date = myParseInt(src.substr(6, 2))
  }
  else {
    // offset type
    var days = myParseInt(src.substr(0, 8))
    var now = new Date()
    now.setTime(now.getTime() + days*24*60*60*1000)
    year = now.getFullYear()
    month = now.getMonth()+1
    date = now.getDate()
  }
  
  return  this.FormDateString(month, date, year) + ' ' + this.FormTimeString(myParseInt(src.substr(8, 2)), myParseInt(src.substr(10, 2)), myParseInt(src.substr(12, 2)), 2, true)
}


// OBJECT used to preserve date time specification read from registry
// By default these are the values

function DateTimeSpecs (sD, iD, sT, sAM, sPM, iT, sShortDate, sTimeFormat)
{
    this.sDate       = sD         // "/" 
    this.iDate       = iD         // 0 (M/D/Y), 1 (D/M/Y), 2 (Y/M/D)
    this.sTime       = sT         // ":" 
    this.s1159       = sAM        // "AM"
    this.s2359       = sPM        // "PM"
    this.iTime       = iT         // 0 -> AM/PM 12 hr format; 1 -> 24 hour format
    this.sShortDate  = sShortDate // M/d/yy
    this.sTimeFormat = sTimeFormat // h:mm:ss tt    

    // the names of days should come from registry but ES/METRO does not support that yet
    
    // the meaning of these shortcust are hard coded in IsDate routine.. so if you change the ordering, change the IsDate too
    this.sDayNames = new Array('today', 'tomorrow', 'urgent', 'asap', 'whenever', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');        
    this.sTimeShortcuts = new Array('morning', 'noon', 'afternoon') ;
    this.sMonthNames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    
    this.FormDateString = mFormDateOnly ;
    this.FormDateObject = mFormDateObject ;
    this.FormTimeString = mFormTimeStr ;
    this.FormatAWDate   = mFormatAWDate ;  
}

// ONE OF THESE TWO LINES MUST BE IN THE FORM
// var DateTimeFormat = new DateTimeSpecs ("##RegionalSettings.sDate##",  "##RegionalSettings.iDate##", "##RegionalSettings.sTime##",  "##RegionalSettings.s1159##", "##RegionalSettings.s2359##", ##RegionalSettings.iTime##, "##RegionalSettings.sShortDate##", "##RegionalSettings.sTimeFormat##");
// var DateTimeFormat = new DateTimeSpecs ("/", 0, ":", "AM", "PM", 0, "M/d/yy", "h:mm:ss tt);

function IsDate(s, returnDateObj)
{
    var i = 0;
    var j;
    var year=month=day=hour=minute=second=0 ;

    hour = -1 ;
    am = -1 ;
    
    FieldValidationErr = "" ;

    // date part

    while (s.charAt(i) == ' ') i++;
    if ( i == s.length ) return true ;

    j = i; while (IsLetter(s.charAt(i))) i++;
    pred = s.substring(j, i);

    if ( (dayShortcut = IsLiteral(DateTimeFormat.sDayNames, pred)) == -1) {
        if ( pred.toLowerCase() == "no" ) {
            // check if it is 'No Time' string
            while (s.charAt(i) == ' ') i++;
            j = i; while (IsLetter(s.charAt(i))) i++;
            pred = s.substring(j, i);
            if ( pred.toLowerCase() != "time" ) 
                return false ;
            while (s.charAt(i) == ' ') i++;
            if ( i < s.length ) {			
                FieldValidationErr = "Please check the extra text '" + s.substr(i) + "' after the valid value '" + s.substr(0, i) + "'." ;
                return false ;
            }
            else
                return true ;
        }
        else {
            i = 0;
            while (s.charAt(i) == ' ') i++;
            j = i; while (IsDigit(s.charAt(i))) i++;

            if ( j == i ) return false ;

            val1 = myParseInt(s.substring(j, i));
            
            if (DateTimeFormat.iDate == 0) {
                if ( val1 < 1 || val1 > 12 ) {
                    FieldValidationErr = "'" + val1 + "' is not a valid month." ;                            
                    return false;
                }
            }
            else if (DateTimeFormat.iDate == 1) {
                if (val1 < 1 || val1 > 31) {
                    FieldValidationErr = "'" + val1 + "' is not a valid day of the month." ;                                            
                    return false;
                }
            }            

            while (s.charAt(i) == ' ') i++;

            if ( s.substr(i, DateTimeFormat.sDate.length) != DateTimeFormat.sDate ) {
                FieldValidationErr = "Expecting a date separator '" +
                DateTimeFormat.sDate + "' after the text '" + s.substr(0, i) + "'.";

                return false;
            }
            i += DateTimeFormat.sDate.length;

            while (s.charAt(i) == ' ') i++;
            j = i; while (IsDigit(s.charAt(i))) i++;

            if ( i == j ) {
                FieldValidationErr =  "Expecting a number after the text '" +
                    s.substr(0, i) + "'.";
                return false ;
            }

            val2 = myParseInt(s.substring(j, i));
            
            if (DateTimeFormat.iDate == 0) {
                if (val2 < 1 || val2 > 31) {
                    FieldValidationErr = "'" + val2 + "' is not a valid day of the month." ;            
                    return false;
                }
            }
            else if (DateTimeFormat.iDate == 1) {
                if ( val2 < 1 || val2 > 12 ) {
                    FieldValidationErr = "'" + val2 + "' is not a valid month." ;
                    return false;
                }
            }            

            while (s.charAt(i) == ' ') i++;

            if ( s.substr(i, DateTimeFormat.sDate.length) != DateTimeFormat.sDate ) {
                FieldValidationErr = "Expecting a date separator '" + 
                    DateTimeFormat.sDate + "' after the text '" + s.substr(0, i) + "'.";

                return false;
            }

            i += DateTimeFormat.sDate.length;

            while (s.charAt(i) == ' ') i++;
            j = i; while (IsDigit(s.charAt(i))) i++;

            if ( i == j ) {
                FieldValidationErr =  "Expecting a number after the text '" +
                    s.substr(0, i) + "'.";
                return false ;
            }
            
            val3 = myParseInt(s.substring(j, i));

            if ( DateTimeFormat.iDate == 0 )
                month = val1, day = val2, year = val3 ;
            else if ( DateTimeFormat.iDate == 1 )      
                month = val2, day = val1, year = val3 ;          
            else if ( DateTimeFormat.iDate == 2 )      
                month = val2, day = val3, year = val1 ;          

            if (month < 1 || month > 12) {
                FieldValidationErr = "'" + month + "' is not a valid month." ;
                return false;
            }
            if (day < 1 || day > 31) {
                FieldValidationErr = "'" + day + "' is not a valid day of the month." ;            
                return false;
            }
		
		if (year<1000) return false;

            if (year <= 50) 
                year = 2000 + year;
            else if (year < 100) 
                year = 1900 + year;

            if (month==2){ // feb
                if (day > 29) {
                    FieldValidationErr = "Februray does not have " + day + "days.";
                    return false;
                }

                if (day == 29) {
                    if ((year/400) == parseInt(year/400))
                        ;
                    else if ((year/100) == parseInt(year/100)) {
                        FieldValidationErr = "Februray does not have 29 days in the year " + year + ".";                                        
                        return false;
                    }    
                    else if ((year/4) == parseInt(year/4))
                        ;
                    else {
                        FieldValidationErr = "Februray does not have 29 days in the year " + year + ".";                    
                        return false ;
                    }
                }
            }

            else if ( day == 31 && (month == 4 || month == 6 || month == 9 ||
                        month == 11) ) {
                FieldValidationErr = "The date 31 is not valid for the specified month.";                     
                return false ;
            }
        }
    }
    else {
        // case of day shortcuts
        if ( returnDateObj == true )  {
            var thisDay = new Date() ;

            if ( dayShortcut == 0 )   // case of 'today'
                ;
            else if ( dayShortcut == 1 ) // 'tomorrow'
                thisDay.setDate( thisDay.getDate() + 1) ;
            else if ( dayShortcut == 2 ) // 'urgent'
                thisDay.setHours( thisDay.getHours() + 3) ;
            else if ( dayShortcut == 3 ) // 'asap'
                thisDay.setDate( thisDay.getDate() + 3) ;
            else if ( dayShortcut == 4 ) // 'whenever'
                thisDay.setDate( thisDay.getDate() + 30) ;
            else {  // case of sunday, monday, tuesday etc.
                var today = thisDay.getDay() ;
                var advBy = (dayShortcut-5) - today ;
                if ( advBy <= 0 )
                    advBy += 7 ;
                thisDay.setDate( thisDay.getDate() + advBy) ;                    
            }
            year = thisDay.getFullYear() ;
            month = thisDay.getMonth()+1 ;
            day = thisDay.getDate();
            
            if ( dayShortcut == 2 ) {// 'urgent'
                hour = thisDay.getHours();
                minute = thisDay.getMinutes();
                second = thisDay.getSeconds();
            }
            else {
                hour = 17;
                minute = 30;
                second = 0;
            }
            am = -1 ;
        }
    }

    // time part
    while (s.charAt(i) == ' ') i++;
    j = i; while (IsLetter(s.charAt(i))) i++;
    pred = s.substring(j, i);
    if ( (timeShortcut = IsLiteral(DateTimeFormat.sTimeShortcuts, pred)) == -1) {
        i = j; while (IsDigit(s.charAt(i))) i++;
        tmpHour= myParseInt(s.substring(j, i));
        if (i > j && !isNaN(tmpHour)) {
            hour = tmpHour
            if (hour > 23) {
                FieldValidationErr = "'" + hour + "' is not a valid hour.";                             
                return false;
            }

            while (s.charAt(i) == ' ') i++;

            if ( s.substr(i, DateTimeFormat.sTime.length) != DateTimeFormat.sTime ){
                FieldValidationErr = "Expecting a time separator '" + 
                    DateTimeFormat.sTime + "' after the text '" + s.substr(0, i) + "'.";
                return false;
            }
            i += DateTimeFormat.sTime.length;

            while (s.charAt(i) == ' ') i++;
            j = i; while (IsDigit(s.charAt(i))) i++;
            minute= myParseInt(s.substring(j, i));
            if ( i==j || isNaN(minute) )
                minute = 0 ;
            
            else if (minute > 59){
                FieldValidationErr = "The value '" + minute + "' is not valid for minutes.";                                     
                return false;
            }

            while (s.charAt(i) == ' ') i++;

            if ( s.substr(i, DateTimeFormat.sTime.length) == DateTimeFormat.sTime ) { 
                // optional seconds
                i += DateTimeFormat.sTime.length;
                while (s.charAt(i) == ' ') i++;
                j = i; while (IsDigit(s.charAt(i))) i++;
                second= s.substring(j, i);
                if ( i== j || isNaN(second))
                    second = 0 ;
                else if (second > 59) {
                    FieldValidationErr = "The value '" + seconds + "' is not valid seconds.";                                       
                    return false;
                }

                while (s.charAt(i) == ' ') i++;
            }
            j = i; 
            var s1159Len = DateTimeFormat.s1159.length;
            var s2359Len = DateTimeFormat.s2359.length;            
            if ( s1159Len > 0 && s.substr(i, s1159Len).toLowerCase() == DateTimeFormat.s1159.toLowerCase() ) {
                i += s1159Len;
                if (hour > 12) {
                    "The time marker '" + DateTimeFormat.s1159 + "' is not correct with the hour as '" + hour + "'.";          
                    return false;
                }
                am = 1 ;
            }
            else if ( s2359Len > 0 && s.substr(i, s2359Len).toLowerCase() == DateTimeFormat.s2359.toLowerCase() ) {
                i += s2359Len;
                am = 0 ;
            }
            else 
              am = ((hour < 12) ? 1 : 0);

        }
    }

    //ignore the rest of the blanks too
    while (s.charAt(i) == ' ') i++;
    if ( i < s.length ) {			
        FieldValidationErr = "Please check the extra text '" + s.substr(i) + "' after the valid value '" + s.substr(0, i) + "'." ;
        return false ;
    }

    if ( returnDateObj == true )  {
    
        // case of day shortcuts
        if ( timeShortcut >= 0 )  {
            // case of 'morning', 'noon', 'afternoon'
            
            if (timeShortcut == 0)    // morning
                return new Date(year, month-1, day, 9, 0, 0) ;
            else if (timeShortcut == 1)  // noon
                return new Date(year, month-1, day, 12, 0, 0) ;
            
            // if (timeShortcut == 2)  afternoon as default
            return new Date(year, month-1, day, 17, 30, 0) ;
        }
        else {
            if ( hour == -1 )
                hour = 17 , minute = 30 , second = 0;
            else if ( hour < 12 && am == 0 ) 
                hour = hour+12
        }
        return ( new Date(year, month-1, day, hour, minute, second) ) ;
    }
    return true ;
}

function GetFormField(aForm, aName)
{
    var i ;
    for (i=0; i<aForm.elements.length; i++) {
        if (aForm.elements[i].name == aName)
            return aForm.elements[i];
    }
    return 0;
}


function CheckMustFill(aField, aFieldName)
{
    if (aField.value == "") {
        alert("'"+aFieldName + "'"+ " è un campo obbligatorio");
        aField.focus();
        return false;
    }
    return true;
}

// Check if any of the radio buttons in a group is selected
function CheckMustSelect(aForm, aFieldName, aFieldLbl)
{
    var i;
    var aField;
    var aFirstField;
    var bChecked;

    bChecked = false;
    aFirstField = null;

    for (i = 0; i < aForm.elements.length; i++) {
      aField = aForm.elements[i];
      if (aField.name == aFieldName) {
          if (aFirstField == null)
              aFirstField = aField;
          if (aField.checked) {
              bChecked = true;
              break;
          }
      }
    }

    if (!bChecked && aFirstField != null) {
        alert("'" + aFieldLbl + "'"+ " is a required field.  Please select one of its options.");
        aFirstField.focus();
        return false;
    }
    return true;
}

function CheckNumber(aField, aFieldName)
{
    if (aField.value == "") aField.Value = '0';
    if (!IsNumber(aField.value)) {
        alert("'"+aFieldName + "'"+ " is not a number.  Please check it.");
        aField.focus();
        return false;
    }
    return true;
}

function CheckDateTime(aField, aFieldName)
{
    if (aField == 0)
        return true;

    if (IsDate(aField.value, false)) 
        return true;
        
    if ( FieldValidationErr.length == 0 ) 
        alert("'"+aFieldName + "'"+ " non ha un formato corretto. Verificare il formato della data.");        
    else
        alert("'"+aFieldName + "'"+ " non ha un formato corretto. " + FieldValidationErr);
    aField.focus();
    return false;
}

//-------------------------------------
// END OF date format validation routines
//-------------------------------------


//-------------------------------------
// CURRENCY FORMAT VALIDATION ROUTINES 
//-------------------------------------

// Currency related settings

// Object to hold the currency related specification
// There is only one object created 

function CurrencyFormatSpecs(sC, iC, iN, sMDS, iCD, sMTS, sMG)
{
    this.sCurrency = sC ;     // "$"
    this.iCurrency = iC ;     // 0 Positive currency mode
        // 0 -> Prefix, no seprataion           $123.45
        // 1 -> Suffix, no seprataion           123.45$
        // 2 -> Prefix, one char seprataion   $ 123.45
        // 3 -> Suffix, one char seprataion     123.45 $
        
    this.iNegCurr = iN ;        // 0 negative currency mode
        // 0 -> ($1.1)
        // 1 -> -$1.1
        // 2 -> $-1.1
        // 3 -> $1.1-
        // 4 -> (1.1$)
        // 5 -> -1.1$
        // 6 -> 1.1-$
        // 7 -> 1.1$-
        // 8 -> -1.1 $
        // 9 -> -$ 1.1
        // 10-> 1.1 $-
        // 11 -> $ 1.1-
        // 12 -> $ -1.1
        // 13 -> 1.1- $
        // 14 -> ($ 1.1)    
        // 15 -> (1.1 $)
        
    this.sMonDecimalSep = sMDS ;    // "."      Monetary decimal separator
    this.iCurrDigits = iCD ;        // 2        no. of fractional digits
    this.sMonThousandSep = sMTS ;   // ","      monetary group separator
    this.sMonGrouping = sMG ;       // "3;0"    monetary grouping size(s)
}

// ONE OF THESE TWO LINES MUST BE IN FORM
// var CurrencyFormat = new CurrencyFormatSpecs("##RegionalSettings.sCurrency##", ##RegionalSettings.iCurrency##, ##RegionalSettings.iNegCurr##, "##RegionalSettings.sMonDecimalSep##", ##RegionalSettings.iCurrDigits##, "##RegionalSettings.sMonThousandSep##", "##RegionalSettings.sMonGrouping##") ;
// var CurrencyFormat = new CurrencyFormatSpecs("$", 0, 0, ".", 2, ",", "3;0") ;
    
function CurrencyToken(tokenType, startCharInSource, nextCharInSource)
{
    this.Type = tokenType; 
        // 'none'   -> No Tokens left
        // 'digits' -> sequence of digits
        // 'blanks' -> blanks (other than what is requierd for currency separator
        // 'currency' -> Currency separator
        // 'decimal' -> decimal separator
        // 'group' -> thousand (or group) separator
        // '(' 
        // ')' 
        // '-' 
        // 'bad'   -> invalid token
    this.NextChar = nextCharInSource ;
    this.StartChar = startCharInSource ;
    //alert("type = " +  tokenType);
}

function GetNextToken(s, i)
{
    var startChar = i ;

    if ( s.charAt(i) == ' ' ) {
        while( s.charAt(i) == ' ' ) i++ ;
        // Forget about spaces.. return new CurrencyToken('blanks', startChar, i);
    }
    
    if ( s.length == i )
        return new CurrencyToken('none', startChar, i);
    
    if ( IsDigit(s.charAt(i)) ) {
        while ( IsDigit(s.charAt(i))) i++ ;
        return new CurrencyToken('digits', startChar, i);
    }

    if ( CurrencyFormat.sCurrency.length > 0 && s.substr(i, CurrencyFormat.sCurrency.length) == CurrencyFormat.sCurrency)
        return new CurrencyToken('currency', startChar, i+CurrencyFormat.sCurrency.length);        

    if ( CurrencyFormat.sMonThousandSep.length > 0 && s.substr(i, CurrencyFormat.sMonThousandSep.length) == CurrencyFormat.sMonThousandSep)
        return new CurrencyToken('group', startChar, i+CurrencyFormat.sMonThousandSep.length);        
    
    if ( CurrencyFormat.sMonDecimalSep.length > 0 && s.substr(i, CurrencyFormat.sMonDecimalSep.length) == CurrencyFormat.sMonDecimalSep)
        return new CurrencyToken('decimal', startChar, i+CurrencyFormat.sMonDecimalSep.length);        
    
    if ( s.charAt(i) == '(' || s.charAt(i) == ')' || s.charAt(i) == '-') 
        return new CurrencyToken(s.substr(i,1), startChar, i+1);                

    return new CurrencyToken('bad', startChar, s.length);                
}

function checkNumberPartOfCurrency(s, Tokens, tok)
{
    // we must get digits now
    if ( Tokens[tok].Type != 'digits' && Tokens[tok].Type != 'decimal') {
    
        if ( tok == 0 )
            FieldValidationErr = "A digit or a decimal is expected in the beginning" ; 
        else
            FieldValidationErr = "A digit or a decimal is expected after '" + s.substr(0, Tokens[tok].StartChar) + "'."; 
        return -1 ;
    }
    iNumberStart = tok ;
    iDecimalTokIndex = -1 ;
    if ( Tokens[tok].Type == 'decimal')
        iDecimalTokIndex = tok ;            

    var foundGroupSeps = false ;        
    while ( Tokens[tok].Type == 'digits' || Tokens[tok].Type == 'group' ) {
        if ( Tokens[tok].Type == 'group') {
            if (tok > 0 && Tokens[tok-1].Type == 'group') {
                FieldValidationErr = "Please remove the extra group separator in the portion '" + s.substr(Tokens[tok].StartChar) + "' at the position '"+ Tokens[tok].StartChar + "'." ;                        
                return -1 ;
            }
            if (iDecimalTokIndex > -1) {
                FieldValidationErr = "Group separator is not permitted after the decimal. Please remove it after the text '" + s.substr(0,Tokens[tok].StartChar)+ "'." ;                        
                return -1 ;
            }
            foundGroupSeps = true ;
        }
        tok++ ;
    }

    // check for grouping of digits
    
    if (foundGroupSeps == true) {
        var iCnt = tok-1;
        if ( Tokens[iCnt].Type == 'group' ) {
            FieldValidationErr = "Group separator is not permitted at the end of the number. Please check the group separator after the text " + s.substr(0, Tokens[iCnt].StartChar);                        
            return -1 ;
        }
        var iActualGroupSize = 0 ;
        var iGroupSpec = 0 ;
        var indexToGroupSpecs = 0 ;
        var iKeepRepeating = false ;
        while(iCnt >= iNumberStart) {
            // we must get a series of digit and group separator
            if ( Tokens[iCnt].Type == 'digits' ) 
                iActualGroupSize = Tokens[iCnt].NextChar-Tokens[iCnt].StartChar ;
            else if ( Tokens[iCnt].Type == 'group' ) {
                if ( iKeepRepeating == false ) {
                    var newPos = CurrencyFormat.sMonGrouping.indexOf(";", indexToGroupSpecs) ;
                    if ( newPos == -1 )
                        newPos = CurrencyFormat.sMonGrouping.length ;
                    var newGroupSpec = myParseInt(CurrencyFormat.sMonGrouping.substr(indexToGroupSpecs, newPos-indexToGroupSpecs)) ;
                    if ( isNaN(newGroupSpec) )
                        break ; // stop testing
                    if ( newGroupSpec == 0 ) {
                        iKeepRepeating = true ;
                    }
                    else {
                        indexToGroupSpecs = newPos+1;
                        iGroupSpec = newGroupSpec;
                    }
                }
                
                if ( iGroupSpec > 0 && iActualGroupSize != iGroupSpec) {
                    FieldValidationErr = "Group separator after the text '"+ s.substr(0, Tokens[iCnt].StartChar) + "' is not at right place.";                        
                    return -1 ;
                }
            }
            else
                break ;
            iCnt-- ;                
        }
    }

    
    if ( Tokens[tok].Type == 'decimal' ) {
        if ( iDecimalTokIndex > -1 && iDecimalTokIndex < tok ) {
            FieldValidationErr = "Please check the extra decimal in the portion '" + s.substr(Tokens[tok].StartChar) + "' at the position '"+ Tokens[tok].StartChar + "'." ;                        
            return -1;
        }
        iDecimalTokIndex = tok ;
        tok++ ;
        // get the right part
        if ( Tokens[tok].Type == 'digits' ) {
            if ( (Tokens[tok].NextChar - Tokens[tok].StartChar) > CurrencyFormat.iCurrDigits ) {
                FieldValidationErr = "Only " + CurrencyFormat.iCurrDigits + " digits are permitted after the decimal. Please remove extra digits after '" + s.substr(0, Tokens[tok].StartChar+CurrencyFormat.iCurrDigits) + "'." ;                        
                return -1;
            }
            tok++ ;
        }
    }
    
    
    return tok ;
}

function IsCurrency(s)
{
    FieldValidationErr = '' ;
    if (IsNumber(s)) {
        //alert("DEBUG: Verified ok as a number") ; // REMOVE THIS IN FINAL CODE
        return true ;
    }

    // ignore blanks
    var i = 0 ;
    while (s.charAt(i) == ' ') i++;
	if ( i == s.length ) return true ;
    
    var Tokens = new Array(10) ;
    var chToken ;

    // just get all the tokens
    var iTokCount = 0 ;
    while (s.length > i) {
        Tokens[iTokCount] = GetNextToken(s, i);
        i = Tokens[iTokCount++].NextChar ;
    }
    Tokens[iTokCount] = new CurrencyToken('none', s.length, s.length); 
    
    // guess if it is a negative currency by looking for ( ) or -
    i = 0 ;
    var iNegativeFormat = false ;
    while ( iTokCount > i ) {
        if ( Tokens[i].Type.length == 1 ) {
            chToken = Tokens[i].Type.charAt(0);
            if ( chToken == '(' ||  chToken == ')' ||  chToken == '-' ){
                iNegativeFormat = true ;
                break ;
            }
        }
        i++;
    }

    var tok = 0 ;   
    if ( iNegativeFormat == true ) {
        
        if (CurrencyFormat.iNegCurr == 0 || CurrencyFormat.iNegCurr == 4 || CurrencyFormat.iNegCurr == 14 || CurrencyFormat.iNegCurr == 15 ){
            if ( Tokens[tok].Type != '(' ) {
                FieldValidationErr = "A negative value must start with '('." ; 
                return false ;
            }
            tok++
        }
        else if (CurrencyFormat.iNegCurr == 1 || CurrencyFormat.iNegCurr == 5 || CurrencyFormat.iNegCurr == 8 || CurrencyFormat.iNegCurr == 9 ){        
            if ( Tokens[tok].Type != '-' ) {
                FieldValidationErr = "A negative value must start with '-'." ; 
                return false ;
            }
            tok++
        }

        if (CurrencyFormat.iNegCurr == 2 || CurrencyFormat.iNegCurr == 3 || CurrencyFormat.iNegCurr == 11 || CurrencyFormat.iNegCurr == 12 ){        
            if ( Tokens[tok].Type == 'currency' )
                tok++;
            
            if ( CurrencyFormat.iNegCurr == 2 ) {
                if ( Tokens[tok].Type != '-' ) {
                    FieldValidationErr = "A negative value must have '-' after the text '" + s.substr(0, Tokens[tok].StartChar) + "'." ; 
                    return false ;
                }
                tok++
            }
            else if ( CurrencyFormat.iNegCurr == 12) {
                if ( Tokens[tok].Type != '-' ) {
                    FieldValidationErr = "A negative value must have '-' after the text '" + s.substr(0, Tokens[tok].StartChar) + "'." ; 
                    return false ;
                }
                tok++
            }
        }
        else if (CurrencyFormat.iNegCurr == 0 || CurrencyFormat.iNegCurr == 1 || CurrencyFormat.iNegCurr == 9 || CurrencyFormat.iNegCurr == 14) {
            if ( Tokens[tok].Type == 'currency' )
                tok++ ;
        }
        
        // now we should get the digits and group chars and decimal
        tok = checkNumberPartOfCurrency(s, Tokens, tok) ;
        
        if ( tok == -1 )
            return false ;
        
        // got the number part correctly
        
        // check for currency sign immediately after digits
        if (CurrencyFormat.iNegCurr == 4 || CurrencyFormat.iNegCurr == 5 || 
            CurrencyFormat.iNegCurr == 7 || CurrencyFormat.iNegCurr == 8 || 
            CurrencyFormat.iNegCurr == 10 || CurrencyFormat.iNegCurr == 15 ){        
            if ( Tokens[tok].Type == 'currency' )
                tok++
        }
        // check the negative value indicator just after the digits
        else if (CurrencyFormat.iNegCurr == 3 || CurrencyFormat.iNegCurr == 6 || CurrencyFormat.iNegCurr == 11 || CurrencyFormat.iNegCurr == 13 ){        
            if ( Tokens[tok].Type != '-' ) {
                FieldValidationErr = "A negative value must have '-' after the numbers." ; 
                return false ;
            }
            tok++;
        }
        else if (CurrencyFormat.iNegCurr == 0 || CurrencyFormat.iNegCurr == 14){        
            if ( Tokens[tok].Type != ')' ) {
                FieldValidationErr = "A negative value must have ')' after the numbers. Please check the text after '" + s.substr(0, Tokens[tok].StartChar) + "'."; 
                return false ;
            }
            tok++
        }

        // check for the last token
        if (CurrencyFormat.iNegCurr == 4 || CurrencyFormat.iNegCurr == 15){        
            if ( Tokens[tok].Type != ')' ) {
                FieldValidationErr = "A negative value must have ')' after the text '" + s.substr(0, Tokens[tok].StartChar) + "'." ; 
                return false ;
            }
            tok++
        }
        else if (CurrencyFormat.iNegCurr == 6 || CurrencyFormat.iNegCurr == 13){        
            if ( Tokens[tok].Type == 'currency' )
                tok++ ;
        }
        else if (CurrencyFormat.iNegCurr == 7 || CurrencyFormat.iNegCurr == 10){                
            if ( Tokens[tok].Type != '-' ) {
                FieldValidationErr = "A negative value must have '-' after the text '" + s.substr(0,Tokens[tok].StartChar) + "'." ; 
                return false ;
            }
            tok++ ;
        }
        
    }
    else { // positive format

        if ( CurrencyFormat.iCurrency == 0 ||  CurrencyFormat.iCurrency == 2) { // Prefix currency case
            if ( Tokens[tok].Type == 'currency' )
                tok++;
        }
        
        // we must get digits now
        tok = checkNumberPartOfCurrency(s, Tokens, tok) ;
        
        if ( tok == -1 )
            return false ;

        if ( CurrencyFormat.iCurrency == 1 ||  CurrencyFormat.iCurrency == 3) { // suffix currency case
            // we must get the currency char
            if ( Tokens[tok].Type == 'currency' )
                tok++;
        } // case of suffix        
    } // case of positive currency
    
    // test is over.. just check if there are some unnecessary chars at the end
    
    if ( iTokCount > tok && Tokens[tok].Type != 'none' ) {
        FieldValidationErr = "Please check the extra text '" + s.substr(Tokens[tok].StartChar) + "' after the valid value '" + s.substr(0, Tokens[tok].StartChar) + "'." ;                 
        if ( Tokens[tok].Type == 'currency' ) {
            var tmp = tok-1 ;
            var currencyFound = false ;
            while( tmp >= 0 ) {
                if (Tokens[tmp].Type == 'currency') {
                    currencyFound = true ;
                    break ;
                }
                tmp-- ;
            }
            if ( currencyFound == false) {
                if ( iNegativeFormat == true ) {
                    if (CurrencyFormat.iNegCurr == 0 || CurrencyFormat.iNegCurr == 1 ||  
                        CurrencyFormat.iNegCurr == 3 || CurrencyFormat.iNegCurr == 9 ||
                        CurrencyFormat.iNegCurr == 11 || CurrencyFormat.iNegCurr == 14 ) {
                        FieldValidationErr = " The monetary symbol '" + CurrencyFormat.sCurrency 
                            + "' can be placed only before the numbers." ; 
                    }
                    else  if (CurrencyFormat.iNegCurr == 2 || CurrencyFormat.iNegCurr == 12) {
                        FieldValidationErr = " The monetary symbol '" + CurrencyFormat.sCurrency 
                            + "' can be placed only in the beginning." ; 
                    
                    }
                    else if (CurrencyFormat.iNegCurr == 4 || CurrencyFormat.iNegCurr == 7 ||  
                        CurrencyFormat.iNegCurr == 10 || CurrencyFormat.iNegCurr == 15 ) {
                        FieldValidationErr = " The monetary symbol '" + CurrencyFormat.sCurrency 
                            + "' can be placed only after the numbers." ; 
                    }
                }
                else { // case of positive currency
                    if ( CurrencyFormat.iCurrency == 0 ||  CurrencyFormat.iCurrency == 2) { // Prefix currency case                    
                        FieldValidationErr = " The monetary symbol '" + CurrencyFormat.sCurrency 
                            + "' can be placed only before the numbers." ; 
                    
                    }
                }
            }
        }
        return false ;
    }
    
    return true ;
}

function CheckCurrency(aField, aFieldName)
{
    if (aField.value == "") aField.Value = '0';
    if (!IsCurrency(aField.value)) {
        if ( FieldValidationErr.length == 0 ) 
            alert("'"+aFieldName + "'"+ " is not a valid currency. Please check it.");
        else
            alert("'"+aFieldName + "'"+ " is not a valid currency. " + FieldValidationErr);
        aField.focus();
        return false;
    }

    return true;
}


// END OF .. CURRENCY FORMAT VALIDATION ROUTINES 
//----------------------------------------------


function CheckRange(aField, aFieldName, RangeBegin, RangeEnd)
{
    if (CheckNumber(aField, aFieldName)) {
        if (aField.value < RangeBegin || aField.value > RangeEnd) {
            alert("'"+aFieldName + "'"+ " is outside of the range '"+RangeBegin+"' and '" + RangeEnd+ "'.  Please check it.");
            return false;
        }
    }
    return true;
}



// -------- CheckBox Utilities--- BEGIN
function SetInitialCheckBoxes(aForm, init, number, aValue)
{
    var i ;
    var s ;
    s = aValue
    for (i = 0; i < number; i++) {
        aForm.elements[idx + i].checked = false
    }

    while (s != '') {
        commapos = s.indexOf(',')
        if (commapos != -1) {
            v = s.substring (0, commapos)
            s = s.substring (commapos+1, s.length)
        }
        else {
            v = s
            s = ''
        }
        for (i = 0; i < number; i++) {
            if (aForm.elements[idx+i].value == v)
                aForm.elements[idx+i].checked = true;
        }
    }
}
function SetCheckBox(aForm, aNameFrom, aNameTo)
{
    aFieldFrom = GetFormField(aForm, aNameFrom)
    if (aFieldFrom.checked) {
        GetFormField(aForm, aNameTo).value = aFieldFrom.value;
    }
}

function SetCheckBoxes(aForm, aNameFrom, aNameTo)
{
    var s = ""
    for (i=0; i<aForm.elements.length; i++) {
        if (aForm.elements[i].name == aNameFrom && aForm.elements[i].checked) {
            if (s != '') s = s + ','
            s = s + aForm.elements[i].value;
        }
    }
    GetFormField(aForm, aNameTo).value = s;
}

// -------- CheckBox Utilities--- END

// -------- ComboBox Utilities--- BEGIN
function SetInitialComboBoxes(aForm, init, aValue)
{
    var s = aValue
    for (i = 0; i < aForm.elements[idx].options.length; i++) {
        aForm.elements[idx].options[i].selected = false
    }

    while (s != '') {
        commapos = s.indexOf(',')
        if (commapos != -1) {
            v = s.substring (0, commapos)
            s = s.substring (commapos+1, s.length)
        }
        else {
            v = s
            s = ''
        }
        for (i = 0; i < aForm.elements[idx].options.length; i++) {
            if (aForm.elements[idx].options[i].value == v)
                aForm.elements[idx].options[i].selected = true;
        }
    }
}


function SetSelects(aForm, aNameFrom, aNameTo)
{
    aFieldFrom = GetFormField(aForm, aNameFrom)
    var s = "" ;
    var i ;
    for (i=0; i<aFieldFrom.options.length; i++) {
        if (aFieldFrom.options[i].selected) {
            if (s != '') s = s + ','
            s = s + aFieldFrom.options[i].value;
        }
    }
    GetFormField(aForm, aNameTo).value = s;
}
// -------- ComboBox Utilities--- END


function Wait(s)
{
  var i;
  for (i=1;i<(s*1000);i++) {}
}

function isDateEx(dateStr) {
	var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // il formato è ok?

	if (matchArray == null) {
	alert("Inserire la data nel formato gg/mm/aaaa (es: 22/09/1980).");
	return false;
	}

	day = matchArray[1]; 
	month = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) { // controllo mese
	alert("Il mese deve essere compreso tra 1 e 12.");
	return false;
	}

	if (day < 1 || day > 31) {
	alert("Il giorno deve essere compreso tra 1 e 31.");
	return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	alert("Mese "+month+" non ha 31 giorni!")
	return false;
	}

	if (month == 2) { // controllo per febbraio 29th
	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	if (day > 29 || (day==29 && !isleap)) {
	alert("Febbraio " + year + " non ha " + day + " giorni!");
	return false;
	}
	}
	if (year < 1890 || year > 2050) {
	alert("L'anno non è corretto.");
	return false;
	}


	return true; // la data è valida
}

function stringToDate(dateStr)
{
	var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // il formato è ok?

	day = matchArray[1]; 
	month = matchArray[3];
	year = matchArray[5];
	
	MyDate = new Date(year,month,day,0,0,0,0); 
	
	return MyDate;

}

function DateAdd(aDate, days) {
 var aDateInv;
 var atmpDate;
 var aMillSec;
 var aDay;
 var aMonth;
 
 aDateInv = aDate.substr(3,2)+'/'+aDate.substr(0,2)+'/'+aDate.substr(6,4);
 atmpDate = new Date(aDateInv);
 aMillSec = atmpDate.getTime();
 aMillSec = aMillSec+days*24*60*60*1000;
 atmpDate= new Date(aMillSec);
 if (atmpDate.getDate()<10) { aDay = '0' + atmpDate.getDate() } else { aDay = atmpDate.getDate()}
 if (atmpDate.getMonth()+1<10) { aMonth = '0' + (atmpDate.getMonth()+1) } else { aMonth = atmpDate.getMonth()+1}
// alert(aDay + '/' + aMonth + '/' + atmpDate.getFullYear());
 return aDay + '/' + aMonth + '/' + atmpDate.getFullYear();
}

function DateDiff(aDate,days) {
 var aDateInv;
 var atmpDate;
 var aMillSec;
 var aDay;
 var aMonth;
 
 aDateInv = aDate.substr(3,2)+'/'+aDate.substr(0,2)+'/'+aDate.substr(6,4);
 atmpDate = new Date(aDateInv);
 aMillSec = atmpDate.getTime();
 aMillSec = aMillSec-days*24*60*60*1000;
 atmpDate= new Date(aMillSec);
 if (atmpDate.getDate()<10) { aDay = '0' + atmpDate.getDate() } else { aDay = atmpDate.getDate()}
 if (atmpDate.getMonth()+1<10) { aMonth = '0' + (atmpDate.getMonth()+1) } else { aMonth = atmpDate.getMonth()+1}
// alert(aDay + '/' + aMonth + '/' + atmpDate.getFullYear());
 return aDay + '/' + aMonth + '/' + atmpDate.getFullYear();
}

function FormatTime(timeStr){

 var appotimeStr;

 appotimeStr = timeStr;

	if (!((IsDigit(appotimeStr.substr(1,1))) && (IsDigit(appotimeStr.substr(2,1))) && (IsDigit(appotimeStr.substr(4,1))) && (IsDigit(appotimeStr.substr(5,1))))) {
		alert('Inserire l\'orario nel formato hh:mm (es: 10:30), Grazie.');
		return false;	
	} else {
		if (appotimeStr.substr(3,1) != ':') {
			alert('Utilizzare il carattere \':\' per separare le ore dai minuti, Grazie.');
		        return false;	
		} 
	}
	return true; // è valida
}



function FormatTimeBis(timeStr){

 var appotimeStr;

 appotimeStr = timeStr;
	if (((IsDigitEx(appotimeStr.substr(0,1))==true) 
		&& (IsDigitEx(appotimeStr.substr(1,1))==true) 
		&& (IsDigitEx(appotimeStr.substr(3,1))==true) 
		&& (IsDigitEx(appotimeStr.substr(4,1))==true))) {
		if (appotimeStr.substr(2,1) != ':') {
		    alert('Utilizzare il carattere \':\' per separare le ore dai minuti, Grazie.');	

		    return false;
                    }
		else {
		   var tmpOre;
		   var tmpMinuti;
			tmpOre=appotimeStr.substr(0,1) +''+ appotimeStr.substr(1,1);
			if (tmpOre > '23') {
				alert('Attenzione le ore devono essere comprese tra le 00 e le 23!');
				return false;
			}
                        tmpMinuti=appotimeStr.substr(3,1) +''+ appotimeStr.substr(4,1);
			if (tmpMinuti > '59') {
				alert('Attenzione i minuti devono essere compresi tra 00 e 59!');
				return false;
			}
		return true;

		}
				
	} else {
			alert('Inserire l\'orario nel formato hh:mm (es: 10:30), Grazie.');
		        return false;	
		 
	}
}

function FormattaOrario(strOra) {
		var strReturn;
		var strTemp;
		strReturn = "";
		strTemp = strOra;
		if (strTemp!="") {
			if (strTemp.length==4) {
				if (strTemp.substr(1,1)==":") {
					strReturn = "0" + strTemp;
				}			
			}
			else if (strTemp.length==5) {
				if (strTemp.substr(2,1)==":") {
					strReturn = strTemp;
				}
			}
		}
		return strReturn;
	}	
	
function ver() 
   { 
   if(document.frmDati.txtUserName.value !="") 
      document.frmDati.txtPassword.focus() 
   else 
     document.frmDati.txtUserName.focus() 
   } 

// MODIFICA 08/06/2004
	
function isCap(aValue) {
  
  re = /[0-9]{5}/;
  
  if (re.test(aValue)==false) {
    return false;	
  }
  
  return true;
}
// FINE MODIFICA 08/06/2004

