// -- ( Português ) --
var MONTH_NAMES = new Array(
  'Janeiro',
  'Fevereiro',
  'Março',
  'Abril',
  'Maio',
  'Junho',
  'Julho',
  'Agosto',
  'Setembro',
  'Outubro',
  'Novembro',
  'Dezembro'
);
var WEEKDAY_FULLNAMES = new Array(
  "Domingo", 
  "Segunda", 
  "Terça", 
  "Quarta", 
  "Quinta", 
  "Sexta", 
  "Sábado"
);
var WEEKDAY_NAMES = new Array("Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sab");
// -- ( English ) --
var MONTH_NAMES_EN = new Array(
  'January',
  'February',
  'March',
  'April',
  'Mai',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December'
);
var WEEKDAY_FULLNAMES_EN = new Array(
  "Sunday", 
  "Monday", 
  "Tuesday", 
  "Wednesday", 
  "Truesday", 
  "Friday", 
  "Saturday"
);
var WEEKDAY_NAMES_EN = new Array("Sun", "Mon", "Tue", "Wed", "Tru", "Fri", "Sat");

// ---
// getDateFromFormat( date_string , format_string )
//
// This function takes a date string and a format string. It matches
// If the date string matches the format string, it returns the 
// getTime() of the date. If it does not match, it returns 0.
// 
// This function uses the same format strings as the 
// java.text.SimpleDateFormat class, with minor exceptions.
// 
// The format string consists of the following abbreviations:
// 
// Field        | Full Form          | Short Form
// -------------+--------------------+-----------------------
// Year         | yyyy (4 digits)    | yy (2 digits), y (2 or 4 digits)
// Month        | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
// Day of Month | dd (2 digits)      | d (1 or 2 digits)
// Hour (0-12)  | hh (2 digits)      | h (1 or 2 digits)
// Hour (0-23)  | HH (2 digits)      | H (1 or 2 digits)
// Minute       | mm (2 digits)      | m (1 or 2 digits)
// Second       | ss (2 digits)      | s (1 or 2 digits)
// AM/PM        | a                  |
//
// Examples:
//  "MMM d, y" matches: January 01, 2000
//                      Dec 1, 1900
//                      Nov 20, 00
//  "m/d/yy"   matches: 01/20/00
//                      9/2/00
//  "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM"
// ------------------------------------------------------------------
function getDateFromFormat( val, format ) {
  val = String(val); format = String(format);
  var i_val = 0; var i_format = 0;
  var c = ""; var token = "";
  var x, y;
  var now = new Date(); 
  var year = now.getYear(); 
  var month = now.getMonth()+1; 
  var date = now.getDate();
  var hh = 0; var mm = 0; var ss = 0; var ampm = "";
  while (i_format < format.length) {
    c = format.charAt(i_format); token = "";
    while ((format.charAt(i_format) == c) && (i_format < format.length)) {
      token += format.charAt(i_format);
      i_format++;
    }
    switch (token) {
      case "yyyy": case "yy": case "y":        
        if (token=="yyyy") { x=4; y=4; }
        if (token=="yy") { x=2; y=2; }
        if (token=="y") { x=2; y=4; }
        year = _getInt(val,i_val,x,y);
        if (year == null) { return -1; }
        i_val += year.length;
        if (year.length == 2) {
          if (year > 70) {
            year = 1900 + parseInt(year,10);
          } else {
            year = 2000 + parseInt(year,10);
          }
        } else {
          year = parseInt(year,10);
        }  break;
      case "MMM":
        month = 0;
        for (var i=0; i<MONTH_NAMES.length; i++) {
          var month_name = MONTH_NAMES[i];
          if (val.substring(i_val,i_val+month_name.length).toLowerCase() == month_name.toLowerCase()) {
            month = i+1;
            i_val += month_name.length;
            break;
          }
        }
        if (month == 0) { return -2; }
         if ((month<1)||(month>12)) { return -3; }
        break;
      case "MM": case "M":
        x = token.length; y = 2;
        month = _getInt(val, i_val, x, y);
        if (month == null) { return -4; }
        if ((month < 1) || (month > 12)) { return -5; }
        i_val += month.length;
        month = parseInt(month,10);
        break;
      case "dd": case "d":
        x=token.length; y=2;
        date = _getInt(val,i_val,x,y);
        if (date == null) { return -6; }
        if ((date < 1) || (date>31)) { return -7; }
        i_val += date.length;
        date = parseInt(date,10);
        break;
      case "hh": case "h":
        x=token.length; 
        y=2;
        hh = _getInt(val,i_val,x,y);
        if (hh == null) { return -8; }
        if ((hh < 1) || (hh > 12)) { return -9; }
        i_val += hh.length;
        hh = parseInt(hh,10);
        break;
      case "HH": case "H":
        x=token.length;
        y=2;
        hh = _getInt(val,i_val,x,y);
        if (hh == null) { return -10; }
        if ((hh < 0) || (hh > 23)) { return -11; }
        i_val += hh.length;
        hh = parseInt(hh,10);
        break;
      case "mm": case "m":
        x=token.length; y = 2;
        mm = _getInt(val,i_val,x,y);
        if (mm == null) { return -12; }
        if ((mm < 0) || (mm > 59)) { return -13; }
        i_val += mm.length;
        mm = parseInt(mm,10);
        break;
      case "ss": case "s":
        x=token.length;
        y=2;
        ss = _getInt(val,i_val,x,y);
        if (ss == null) { return -14; }
        if ((ss < 0) || (ss > 59)) { return -15; }
        i_val += ss.length;
        ss = parseInt(ss,10);
        break;
      case "a":
        if (val.substring(i_val,i_val+2).toLowerCase() == "am") {
          ampm = "AM";
          if (hh==12) { hh = 0; }
        } else if (val.substring(i_val,i_val+2).toLowerCase() == "pm") {
          ampm = "PM";
          if ( hh<12 ) { hh += 12 }
        } else {
          return -16;
        } i_val += 2;
        break;
      default:
        if (val.substring(i_val,i_val+token.length) != token) {
          return -17;
        } else {
          i_val += token.length;
        }
    }
  }
  if (i_val != val.length) { return -18; }
  if (month == 2) {
    if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) {
      if (date > 29){ return -19; }
    } else {
      if (date > 28) { return -20; }
    }
  } 
  if ((month==4)||(month==6)||(month==9)||(month==11)) {
    if (date>30) { return -21; }
  }
  var newdate = new Date(year,month-1,date,hh,mm,ss);
  return newdate.getTime();
} // getDateFromFormat()

// ---
// isDate ( date_string, format_string )
// ---
function isDate(val,format) {
  var date = getDateFromFormat(val,format);
  if (date==0) { return false; }
  return true;
} // isDate

// ---
// _isInteger()
// ---
function _isInteger(val) {
  var digits = "1234567890";
  for (var i=0; i < val.length; i++) {
    if (digits.indexOf(val.charAt(i)) == -1) { return false; }
  } return true;
} // _IsInteger()

// ---
// _getInt()
// ---
function _getInt( str, i, minlength, maxlength ) {
  for (var x=maxlength; x>=minlength; x--) {
    var token = str.substring(i,i+x);
    if (token.length < minlength) { return null; }
    if (_isInteger(token)) { return token; }
  } return null;
} // _getInt()

// -------------------------------------------------------------------
// compareDates(date1,date1format,date2,date2format)
//   Compare two date strings to see which is greater.
//   Returns:
//   1 if date1 is greater than date2
//   0 if date2 is greater than date1 of if they are the same
//  -1 if either of the dates is in an invalid format
// -------------------------------------------------------------------
function compareDates(date1,dateformat1,date2,dateformat2) {
  var d1 = getDateFromFormat(date1,dateformat1);
  var d2 = getDateFromFormat(date2,dateformat2);
  if (d1==0||d2==0) { return -1; } else if (d1 > d2) { return 1; }
  return 0;
} // compareDates()

// ---
// formatDate (date_object, format)
// --
function formatDate(date,format) {
  if (isNaN(date)||isNull(date)||isBlank(String(date))) { return ""; }
  try {
    format = String(format);
    var result   = "";
    var i_format = 0;
    var c        = "";
    var token    = "";
    var ano      = date.getFullYear();
    var mes      = date.getMonth()+1;
    var dia      = date.getDate();
    var hora     = date.getHours();
    var minuto   = date.getMinutes();
    var segundo  = date.getSeconds();
    var yyyy,yy,MMM,MM,dd,hh,h,HH,H,mm,ss,ampm;
    y    = "" + ano;
    yyyy = "" + ano;
    yy   = y.substring(2,4);
    M = "" + mes;
    if (mes < 10) { MM = "0"+mes; } else { MM = ""+mes }; 
    MMM = MONTH_NAMES[mes-1];
    d = "" + dia;
    if (dia < 10) { dd = "0" + dia; } else { dd = "" + dia }; 
    H = "" + hora;
    if (hora < 10) { HH = "0" + hora } else { HH = "" + hora };
    if (hora == 0) {
      ampm = "AM";
      h = hh = "12";
    } else if (hora < 12) {
      ampm = "AM";
      h = ""+hora;
      if (hora<10) { hh = "0"+hora; } else { hh = "" + hora };
    } else if (hora == 12) {
      ampm = "PM";
      h = hh = "12";
    } else {
      hora -= 12;
      h  = "" + hora;
      if (hora<10) { hh = "0"+hora; } else { hh = "" + hora };
    }
    m = "" + minuto;
    if (minuto < 10) { mm = "0" + minuto; } else { mm = "" + minuto; }
    s = "" + segundo;
    if (segundo < 10) { ss = "0" + segundo; } else { ss = "" + segundo; }
    var value = new Object();
    value["yyyy"] = yyyy;
    value["yy"]   = yy;
    value["y"]    = y;
    value["MMM"]  = MMM;
    value["MM"]   = MM;
    value["M"]    = M;
    value["dd"]   = dd;
    value["d"]    = d;
    value["hh"]   = hh;
    value["h"]    = h;
    value["HH"]   = HH;
    value["H"]    = H;
    value["mm"]   = mm;
    value["m"]    = m;
    value["ss"]   = ss;
    value["s"]    = s;
    value["a"]    = ampm;
    while (i_format < format.length) {
      c = format.charAt(i_format); token = "";
      while ((format.charAt(i_format) == c) && (i_format < format.length)) {
        token += format.charAt(i_format);
        i_format++;
      }
      if (value[token]!=null) {
        result = result + value[token];
      } else {
        result = result + token;
      }
    }
  } catch(e) {
    oWarnings.Add("Função formatDate() não foi executada com sucesso! Parâmetros: date = " + date + "; e, format = " + format);
    result = "";
  }
  return result;
} // formatDate()

// --
// DateAdd()
// --
function DateAdd(startDate,numDays,numMonths,numYears) {
  var returnDate = new Date(startDate.getTime());
  var hours   = startDate.getHours();
  var minutes = startDate.getMinutes();
  var seconds = startDate.getSeconds();
  var yearsToAdd = numYears;
  var month = returnDate.getMonth()  + numMonths;
  if (month > 11) {
    yearsToAdd = Math.floor((month+1)/12);
    month -= 12*yearsToAdd;
    yearsToAdd += numYears;
  }
  returnDate.setMonth(month);
  returnDate.setFullYear(returnDate.getFullYear() + yearsToAdd);
  returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
  // -- ( Fugindo dos Acertos de Horário de Verão ) --
  returnDate.setHours(startDate.getHours());
  returnDate.setMinutes(startDate.getMinutes());
  returnDate.setSeconds(startDate.getSeconds());
  return returnDate;
} // DateAdd()

// --
// HourAdd()
//
function HourAdd(startDate, numHours) {
  var returnDate = new Date(startDate.getTime());
  returnDate.setTime(returnDate.getTime()+60000*60*numHours);
  return returnDate;
}  // HoursAdd()

// --
// MinuteAdd()
//
function MinuteAdd(startDate, numMinutes) {
  var returnDate = new Date(startDate.getTime());
  returnDate.setTime(returnDate.getTime()+60000*numMinutes);
  return returnDate;
}  // MinuteAdd()

// --
// SecondAdd()
//
function SecondAdd(startDate, numSeconds) {
  var returnDate = new Date(startDate.getTime());
  returnDate.setTime(returnDate.getTime()+1000*numSeconds);
  return returnDate;
}  // SecondAdd()

// --
// YearAdd()
// --
function YearAdd(startDate, numYears) {
  return DateAdd(startDate,0,0,numYears);
} // YearAdd()

// --
// MonthAdd()
// --
function MonthAdd(startDate, numMonths) {
  return DateAdd(startDate,0,numMonths,0);
} // MonthAdd()

// --
// DayAdd()
// --
function DayAdd(startDate, numDays) {
  return DateAdd(startDate,numDays,0,0);
} // DayAdd()

// --
// whenIs()
// --
function whenIs(anyDate, n) {
  //-- Returns the date that is n days from any date object.
  var newDate = new Date();
  newDate.setTime(anyDate.getTime()+(n*1000*60*60*24));
  return newDate;
} // whenIs

// --
// newDate()
// --
function newDate(passedValue) {
  var makeDate = new Date();
  var firstSlash=passedValue.indexOf("/");
  var lastSlash=passedValue.lastIndexOf("/");
  var day=passedValue.substr(0,firstSlash);
  var month=passedValue.substring(firstSlash+1,lastSlash)-1;
  var year=passedValue.substr(lastSlash+1);
  var newDate = new Date(year,month,day);
  return newDate;
} // newDate()

// --
// daysBetween()
// --
function daysBetween(earlyDate,laterDate){
  // -- ( Retorna o Número de Dias entre Dois Objetos da Classe Date ) --
  var earlySecs = earlyDate.getTime();
  var laterSecs = laterDate.getTime();
  return Math.floor ((((((laterSecs-earlySecs)/1000)/60)/60)/24));
} // daysBetween()

// --
// daysAbsBetween()
// --
function daysAbsBetween(earlyDate,laterDate){
  earlyDate.setHours(0); earlyDate.setMinutes(0); earlyDate.setSeconds(0); earlyDate.setMilliseconds(0);  
  var earlySecs = earlyDate.getTime();
  laterDate.setHours(0); laterDate.setMinutes(0); laterDate.setSeconds(0); laterDate.setMilliseconds(0); 
  var laterSecs = laterDate.getTime();
  return Math.round ((((((laterSecs-earlySecs)/1000)/60)/60)/24));
} // daysAbsBetween()

// --
// minutesBetween()
// --
function minutesBetween(earlyDate,laterDate){
  //-- ( Retorna a Diferença entre Duas Datas ) --
  try {
    var earlySecs = earlyDate.getTime();
    var laterSecs = laterDate.getTime();
    return Math.floor(((laterSecs-earlySecs)/1000)/60);
  } catch(e) {
    ;
  } return 0;
} // minutesBetween()

// --
// secondsBetween()
// --
function secondsBetween(earlyDate,laterDate){
  //-- Retorna a Diferença em Segundos entre Duas Datas
  var earlySecs = earlyDate.getTime();
  var laterSecs = laterDate.getTime();
  return Math.floor(((laterSecs-earlySecs)/1000));
} // secondsBetween()

// --
// hoursBetween()
// --
function hoursBetween(earlyDate,laterDate){
  //-- Retorna a Diferença entre Duas Datas
  var earlySecs = earlyDate.getTime();
  var laterSecs = laterDate.getTime();
  return Math.floor ((((((laterSecs-earlySecs)/1000)/60)/60)));
} // hoursBetween()

// --
// StrToDateTime()
// --
function StrToDateTime(vl, ft) {
  var d = new Date();
  d.setTime( getDateFromFormat(vl, ft) );
  if ( d==0 ) { d = Number.NaN };
  return d;
} // StrToDateTime()

// --
// FirstDayOfMonth()
// --
function FirstDayOfMonth(dt) {
  var returnDate = new Date(dt.getTime());
  returnDate.setDate(1);
  returnDate.setHours(0);
  returnDate.setMinutes(0);
  returnDate.setSeconds(0);
  return returnDate;  
} // FirstDayOfMonth()

// --
// FirstDayOfNextMonth()
// --
function FirstDayOfNextMonth(dt) {
  var returnDate = new Date(dt.getTime());
  if (returnDate.getMonth()==11) {
    returnDate.setMonth(0); returnDate.setFullYear(returnDate.getFullYear()+1);
  } else {
    returnDate.setMonth(dt.getMonth()+1);
  }
  returnDate.setDate(1);
  returnDate.setHours(0);
  returnDate.setMinutes(0);
  returnDate.setSeconds(0);
  return returnDate;  
} // FirstDayOfNextMonth()

// --
// LastDayOfMonth()
// --
function LastDayOfMonth(dt) {
  var returnDate = new Date(dt.getTime());
  var month = returnDate.getMonth();
  switch (month) {
    case 0: case 2: case 4: case 6: case 7: case 9: case 11:
    returnDate.setDate(31);
    break;
  case 3: case 5: case 8: case 10:
    returnDate.setDate(30);
    break;
  default:
      var year = returnDate.getFullYear();
      if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) {
        returnDate.setDate(29);
      } else {
        returnDate.setDate(28);
      }
    break;
  }  
  returnDate.setHours(23);
  returnDate.setMinutes(59);
  returnDate.setSeconds(59);
  return returnDate;  
} // LastDayOfMonth()

// --
// FirstDayOfYear()
// --
function FirstDayOfYear(dt) {
  var returnDate = new Date(dt.getTime());
  returnDate.setMonth(0);
  returnDate = FirstDayOfMonth(returnDate);
  return returnDate;  
} // FirstDayOfMonth()

// --
// LastDayOfYear()
// --
function LastDayOfYear(dt) {
  var returnDate = new Date(dt.getTime());
  returnDate.setMonth(11);
  returnDate = LastDayOfMonth(returnDate);
  return returnDate;  
} // LastDayOfYear()

// --
// IsFeriado()
// --
function IsFeriado(dt) {
  var b = false;
  var o = new TFeriado();
  o.aValues[FERDTA] = dt;
  if (o.Read()) {
    b = true;
  } o = null;
  return b;
} // IsFeriado()

// --
// DeltaT()
// --
function DeltaT( value ) {
  var str = "";
  var h = parseInt(value, 10);
  value -= h; value *= 60;
  var m = parseInt(value, 10);
  value -= m; value *= 60;
  var s = parseInt(value, 10);
  str += String(h).leftfill("0", h>=100?3:2) + ":" + String(m).leftfill("0", 2) + ":" + String(s).leftfill("0", 2);
  return str;
} // DeltaT()

// --
// DeltaT2()
// --
function DeltaT2( value ) {
  var str = "";
  value /= 60;
  var h = parseInt(value, 10);
  value -= h; value *= 60;
  var m = parseInt(value, 10);
  str += String(h).leftfill("0", 2) + ":" + String(m).leftfill("0", 2);
  return str;
} // DeltaT2()

// --
// DeltaT3()
// --
function DeltaT3( value ) {
  var str = "";
  value /= 60; 
  var d = parseInt(value/24, 10);
  value -= (d*24);
  var h = parseInt(value, 10);
  value -= h; value *= 60;
  var m = parseInt(value, 10);
  str += String(d).leftfill("0", 3) + ":" + String(h).leftfill("0", 2) + ":" + String(m).leftfill("0", 2);
  return str;
} // DeltaT3()

// --
// TempoMedio()
// --
function TempoMedio( dt1, dt2 ) {
  var retValue = 0;
  retValue = Math.abs(minutesBetween(dt1, dt2));
  return retValue;
} // TempoMedio()

// --
// NextDay() 
// --
function NextDay( startDate ) {
  var retValue = startDate;
  retValue.setHours(12); retValue.setMinutes(0); retValue.setSeconds(0);
  var ld = LastDayOfMonth( retValue );
  if (Att2Str(retValue, 'D')==Att2Str(ld, 'D')) {
    retValue.setDate(1);
    if (startDate.getMonth()==11) {
      retValue.setMonth(0);
      retValue.setFullYear(retValue.getFullYear() + 1);
    } else {
      retValue.setMonth(retValue.getMonth() + 1);
    }
  } else {
    retValue.setMonth(retValue.getDate() + 1);
  } 
  retValue.setHours(startDate.getHours());
  retValue.setMinutes(startDate.getMinutes());
  retValue.setSeconds(startDate.getSeconds());
  return retValue;
} // NextDay()

// --
// TType()
// --
function TType(sType) {
  // -- ( Properties ) --
  this.typeValue = (""+sType=="undefined"?"S":""+sType);
  this.typeCode = "";
  this.minValue = -99999;
  this.maxValue = 100000;
  this.minLength = 1;
  this.maxLength = 4000;
  this.numDigits = 11;
  this.numDecimals = 0;
  // -- ( Methods ) --
  this.Clear = Clear;
  this.Parse = Parse;
  // -- 
  // Clear()
  // --
  function Clear() {
    this.typeCode = "";
    this.minValue = -99999;
    this.maxValue = 100000;
    this.minLength = 1;
    this.maxLength = 4000;
    this.numDigits = 11;
    this.numDecimals = 0;
  } // Clear()
  // -- 
  // Parse()
  // --
  function Parse() {
    this.Clear();
    var statusParse = 0; var stringParse = "";
    for (var x=0; x<this.typeValue.length; x++) {
      var sChar = this.typeValue.substr(x, 1);
      switch (sChar) {
        case ":":
          if (statusParse==1) { 
            this.minValue = parseInt(stringParse, 10);
            statusParse = 2; stringParse = "0";
          } else if (statusParse==3) { 
            this.minLength = parseInt(stringParse, 10);
            statusParse = 4; stringParse = "0";
          } else if (statusParse==5) { 
            this.numDigits = parseInt(stringParse, 10);
            statusParse = 6; stringParse = "0";
          } else {
            statusParse = -1; stringParse = "";
          } break;
        case "[":
          statusParse = 1; stringParse = "0";
          break;
        case "]":
          if (stringParse!="") {
            if (statusParse==1) {
              this.minValue = parseInt(stringParse, 10);
            } else {
              this.maxValue = parseInt(stringParse, 10);
            }
          } statusParse = -1; stringParse = "";
          break;
        case "(":
          statusParse = 3; stringParse = "0";
          break;
        case ")":
          if (stringParse!="") {
            if (statusParse==3) {
              this.minLength = parseInt(stringParse, 10);
            } else {
              this.maxLength = parseInt(stringParse, 10);
            }
          } statusParse = -1; stringParse = "";
          break;
        case "{":
          statusParse = 5; stringParse = "0";
          break;
        case "}":
          if (stringParse!="") {
            if (statusParse==5) {
              this.numDigits = parseInt(stringParse, 10);
            } else {
              this.numDecimals = parseInt(stringParse, 10);
            }
          } statusParse = -1; stringParse = "";
          break;
        default:
          if (statusParse==0) {
            this.typeCode += sChar; 
          } else if (statusParse!=-1) { 
            stringParse += sChar; 
          } break;
      }
    }
  } // Parse()
  // -- ( Constructor ) --
  if (String(this.typeValue)!="") {
    this.Parse();
  } return this;
} // TType()

// --
// Fld2Att()
// --
function Fld2Att(valueOfField,typeOfField) {
  var retValue = null;
  var oType = new TType(typeOfField);
  switch(oType.typeCode) {
    case "S": case "B":
      if (!isNull(valueOfField)) {
        retValue = String(valueOfField);
      } break;
    case "I": case "A":
      retValue = Number.NaN;
      if (!isNull(valueOfField)) {
        retValue = parseInt(valueOfField, 10);
      } break;
    case "T":
      if (isNull(valueOfField)) {
        retValue = Number.NaN;
      } else {
        if (typeof(valueOfField)=="date") {
          retValue = formatDate(new Date(valueOfField), 'HH:mm:ss');
        } else {
          retValue = String(valueOfField);
        }
      } break;
    case "F": case "LAT": case "LON":
      var strValue = String(valueOfField);
      retValue = Number.NaN;
      if (!isNull(strValue)) {
        retValue = parseFloat(strValue);
      } break;
    case "DT": case "DTO": case "D": case "DO":
      retValue = Number.NaN;
      if (!isNull(valueOfField)) {
        retValue = new Date(valueOfField);
        if ((oType.typeCode=="D")||(oType.typeCode=="DO")) {
          retValue.setHours(0); 
          retValue.setMinutes(0);
          retValue.setSeconds(0);
          retValue.setMilliseconds(0);
        }
      } break;
  } oType = null; 
  return retValue;
} // Fld2Att()

// --
// Att2Fld()
// --
function Att2Fld(valueOfField, typeOfField) {
  var retValue = "NULL";  
  var oType = new TType(typeOfField);
  switch(oType.typeCode) {
    case "S": case "B":
      if ( (!isNull(valueOfField)) && (!isBlank(valueOfField)) ) {
        retValue = "'" + String(valueOfField).replace(/'/gi,"''").trim() + "'";
      } break;
    case "D":
      if ((!isNaN(valueOfField))) {
        retValue = "'" + formatDate(valueOfField,'yyyy-MM-dd') + "'";
      } break;
    case "DO":
      if ( (!isNaN(valueOfField))) {
        retValue = "TO_DATE('" + formatDate(valueOfField,"dd/MM/yyyy") + "', 'DD/MM/YYYY')";
      } else {
        if (valueOfField.left(5)=="TRUNC") { retValue = valueOfField; }
      } break;
    case "DT":
      if (!isNaN(valueOfField)) {
        retValue = "'" + formatDate(valueOfField,'yyyy-MM-dd HH:mm:ss') + "'";
      } else {
        if (valueOfField=="NOW()") { retValue = valueOfField; }
      } break;
    case "DTO":
      if ( (!isNaN(valueOfField))) {
        retValue = "TO_DATE('" + formatDate(valueOfField,"dd/MM/yyyy HH:mm:ss") + "', 'DD/MM/YYYY HH24:MI:SS')";
      } else {
        if (valueOfField.left(5)=="TRUNC") { retValue = valueOfField; }
      } break;
      case "T":
        if (""+valueOfField=="NOW()") {
          retValue = "CURTIME()";
        } else if (typeof(valueOfField)=="string") {
          retValue = "'" + String(valueOfField) + "'";
        } else {
          retValue = "'" + formatDate(valueOfField,"HH:mm:ss") + "'";
        } break;
    case "I": case "A":
    case "F": case "LAT": case "LON":
      if ( (!isNaN(valueOfField))) {
        retValue = String(valueOfField);
      } break;
  } oType = null;
  return retValue;
} // Att2Fld()

// --
// Att2Str()
// --
function Att2Str(valueOfField, typeOfField, nDec) {
  var retValue = "";
  var oType = new TType(typeOfField);
  switch(oType.typeCode) {
    case "S": case "B":
      if (!isNull(valueOfField)) {
        retValue = String(valueOfField);
      } break;
    case "D": case "DO":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yyyy');
      } break;
    case "DT": case "DTO":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yyyy HH:mm');
      } break;
    case "T":
      if (!isNaN(valueOfField)) {
        var IsPositive = true;
        var hr = 0; var min = 0; var sec = 0;
        var vl = valueOfField;
        if (vl<0) { vl = -vl; IsPositive = false; }
        hr = parseInt(vl/3600, 10);
        vl = vl - (hr * 3600);
        min = parseInt(vl/60, 10);
        sec = vl - (min * 60);
        if (oType.typeCode=="T") {
          retValue = (IsPositive?"+":"-") + String(hr).leftfill("0", 2) + ":" + String(min).leftfill("0", 2);
        } else {
          retValue = (IsPositive?"+":"-") + String(hr).leftfill("0", 2) + ":" + String(min).leftfill("0", 2) + ":" + String(sec).leftfill("0", 2);
        }
      } break;
    case "I": case "A":
      if (!isNaN(valueOfField)) {
        retValue = String(valueOfField);
      }
      break;
    case "F":
      retValue = parseFloat(valueOfField*100)/100;
      if (!isNaN(retValue)) {
        nDec=(""+nDec=="undefined"?2:nDec);
        var pDec = "";
        for (x=0; x<nDec; x++) { pDec += "0" }
        retValue = formatNumber(valueOfField, "##0." + pDec);
      } else {
        retValue = "";
      } break;
    case "LAT":
      if (!isNaN(valueOfField)) {
        retValue = valueOfField.toLat();
        retValue = retValue.substr(0,2) + "\u00B0" + retValue.substr(3,2) + "." + retValue.substr(6,3);
      } else {
        retValue = "";
      } break;
    case "LON":
      if (!isNaN(valueOfField)) {
        retValue = valueOfField.toLon();
        retValue = retValue.substr(0,3) + "\u00B0" + retValue.substr(4,2) + "." + retValue.substr(7,3);
      } else {
        retValue = "";
      } break;
  } oType = null;
  return retValue;
} // Att2Str()

// --
// Str2Att()
// --
function Str2Att(valueOfField, typeOfField) {
  if ((""+valueOfField=="undefined")||(""+valueOfField=="")) {
    switch ( typeOfField ) {
      case "F": case "I": case "D": case "DO": case "DT": case "DTO": case "LAT": case "LON":
        retValue = Number.NaN;
        break;
      case "S": case "B": default:
        retValue = null;
        break;
    } return retValue;
  }
  var retValue = null;
  var oType = new TType(typeOfField);
  switch(oType.typeCode) {
    case "S": case "B":
      if ( (!isNull(valueOfField)) && valueOfField!="undefined" ) {
        retValue = String(valueOfField);
      } break;
    case "D": case "DO":
      retValue = Number.NaN;
      if ( (!isNull(valueOfField)) && valueOfField!="undefined" ) {
        if (oType.typeCode=="D") {
          retValue = getDateFromFormat(valueOfField,'dd/MM/yyyy');
        } else {
          retValue = getDateFromFormat(valueOfField,'yyyyMMdd');
        }
        if (retValue!=0) {
          retValue = new Date(retValue);
        } else {
          retValue = Number.NaN;
        }
      } break;
    case "DT": case "DTO":
      retValue = Number.NaN;
      if ( (!isNull(valueOfField)) && valueOfField!="undefined" ) {
        switch (valueOfField.length) {
          case 10:
            retValue = getDateFromFormat(valueOfField,'dd/MM/yyyy');
            break;
          case 13:
            retValue = getDateFromFormat(valueOfField,'dd/MM/yyyy HH');
            break;
          case 16:
            retValue = getDateFromFormat(valueOfField,'dd/MM/yyyy HH:mm');
            break;
          case 19:
            retValue = getDateFromFormat(valueOfField,'dd/MM/yyyy HH:mm:ss');
            break;
          default:
            retValue = 0;
            break;
        }
        if (retValue>0) {
          retValue = new Date(retValue);
        } else {
          retValue = Number.NaN;
        }
      } break;
    case "I": case "A":
      retValue = Number.NaN;
      if ( (!isNull(valueOfField)) && valueOfField!="undefined" ) {
        retValue = parseInt(valueOfField, 10);
      } break;
    case "T":
      retValue = Number.NaN;
      if ( (!isNull(valueOfField)) && valueOfField!="undefined" ) {
        retValue = parseInt(valueOfField.substr(0, 2), 10) * 3600 + 
                   parseInt(valueOfField.substr(2, 2), 10) * 60   +
                   parseInt(valueOfField.substr(4, 2), 10); 
      } break;
    case "F":
      retValue = Number.NaN;
      if ( (!isNull(valueOfField)) && valueOfField!="undefined" ) {
        valueOfField = valueOfField.replace(/\./gi,"");
        valueOfField = valueOfField.replace(/\,/gi,'.');
        retValue = parseFloat(valueOfField*100)/100;
      } break;
    case "LAT":
      retValue = Number.NaN;
      if ( (!isNull(valueOfField)) && valueOfField!="undefined" && valueOfField!="" ) {
        retValue = String(valueOfField + "S").parseDeg();
      } break;
    case "LON":
      retValue = Number.NaN;
      if ( (!isNull(valueOfField)) && valueOfField!="undefined" && valueOfField!="" ) {
        retValue = String(valueOfField + "W").parseDeg();
      } break;
  } oType = null;  
  return retValue;
} // Str2Att()

// --
// Str2Fld()
// --
function Str2Fld(valueOfField, typeOfField) {
  return ( Att2Fld(Str2Att(valueOfField, typeOfField), typeOfField ));
} // Str2Fld()

// --
// AttFormat()
// --
function AttFormat(valueOfField, typeOfField, nDec) {
  var retValue = "";
  var oType = new TType(typeOfField);
  switch(oType.typeCode) {
    case "S": case "B":
      if (!isNull(valueOfField)) {
        retValue = String(valueOfField).replace(/"/gi,"&#34;");
        retValue = retValue.replace(/'/gi,"&#39;");
        retValue = retValue.replace(/\r/gi,'');
        retValue = retValue.replace(/\n/gi,'<br>');
      } break;
    case "S2":
      if (!isNull(valueOfField)) {
        retValue = valueOfField.replace(/"/gi,"&#34;");
        retValue = retValue.replace(/'/gi,"&#39;");
      } break;
    case "S3":
      if (!isNull(valueOfField)) {
        retValue = retValue.replace(/'/gi,"&#39;");
        retValue = retValue.replace(/\r/gi,'');
        retValue = retValue.replace(/\n/gi,'<br>');
      } break;
    case "D": case "DO":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yyyy');
      } break;
    case "D2":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM');
      } break;
    case "D3":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yy');
      } break;
    case "DT": case "DTO": case "DT2":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yyyy HH:mm');
      } break;
    case "DT3":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM HH:mm');
      } break;
    case "DT4":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'HH:mm');
      } break;
    case "DT5":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yy HH:mm');
      } break;
    case "DT6":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yy HH:mm:ss');
      } break;
    case "MY":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'MM/yyyy');
      } break;
    case "HM":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'HH:mm');
      } break;
      break;
    case "T": case "T2":
      if (!isNaN(valueOfField)) {
        var IsPositive = true;
        var hr = 0; var min = 0; var sec = 0;
        var vl = valueOfField;
        if (vl<0) { vl = -vl; IsPositive = false; }
        hr = parseInt(vl/3600, 10);
        vl = vl - (hr * 3600);
        min = parseInt(vl/60, 10);
        sec = vl - (min * 60);
        if (oType.typeCode=="T") {
          retValue = (IsPositive?"+":"-") + String(hr).leftfill("0", 2) + ":" + String(min).leftfill("0", 2);
        } else {
          retValue = (IsPositive?"+":"-") + String(hr).leftfill("0", 2) + ":" + String(min).leftfill("0", 2) + ":" + String(sec).leftfill("0", 2);
        }
      } break;
    case "I": case "A":      
      if (!isNaN(valueOfField)) {
        retValue = formatNumber(valueOfField, "#,##0");
      }
      break;
    case "F": case "P":
      retValue = parseFloat(valueOfField*100)/100;
      if (!isNaN(retValue)) {
        nDec=(""+nDec=="undefined"?2:nDec);
        var pDec = "";
        for (x=0; x<nDec; x++) { pDec += "0" }
        retValue = formatNumber(valueOfField, "#,##0." + pDec);
      } else {
        retValue = "";
      } break;
    case "LAT":
      if (!isNaN(valueOfField)) {
        retValue = valueOfField.toLat();
      } else {
        retValue = "";
      } break;
    case "LON":
      if (!isNaN(valueOfField)) {
        retValue = valueOfField.toLon();
      } else {
        retValue = "";
      } break;
    case "DURACAO":
      if (!isNaN(valueOfField)) {
        var vl = (valueOfField / 10000) * 3600;
        var hr = 0; var min = 0; var sec = 0;
        hr = parseInt(vl/3600, 10);
        vl = vl - (hr * 3600);
        min = parseInt(vl/60, 10);
        sec = vl - (min * 60);
        retValue = String(hr).leftfill("0", 2) + ":" + String(min).leftfill("0", 2);
      } else {
        retValue = "??:??";
      } break;
    case "JSVAR":
      if (!isNull(valueOfField)) {
        retValue = valueOfField.replace(/\r/gi,"");
        retValue = retValue.replace(/\n/gi,"<br>");
        retValue = retValue.replace(/\"/gi,"&quot;");
        retValue = retValue.replace(/'/gi,'"');
        retValue = retValue.delimited("'");
      } break;
  } oType = null;
  return retValue;
} // AttFormat()

// --
// AttDisplay()
// --
function AttDisplay(valueOfField, typeOfField, nDec) {
  var retValue = AttFormat(valueOfField, typeOfField, nDec);
  if ( (typeOfField=="I") || (typeOfField=="A") ) {
    retValue = Att2Str(valueOfField, typeOfField, nDec);
  } if (isBlank(retValue)) { retValue = "&nbsp;"; }
  return retValue;
} // AttDisplay()

// --
// Mtx2Str()
// --
function Mtx2Str(arr, itm) {
  var str = "";
  if (arr.length>0) {
    for (var x=0; x<arr.length; x++) {
      if (x>0) { str += ","; }
      str += String(arr[x][itm]);
    }
  } return str;
} // Mtx2Str()

// --
// Att2Xml()
// --
function Att2Xml(valueOfField, typeOfField, nDec) {
  var retValue = "";
  switch ( typeOfField ) {
    case "S": case "B":
      if (!isNull(valueOfField)) {
        retValue = String(valueOfField);
      } break;
    case "D": case "DO":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yyyy');
      } break;
    case "DT": case "DTO":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yyyy HH:mm:ss');
      } break;
    case "T":
      if (!isNaN(valueOfField)) {
        var IsPositive = true;
        var hr = 0; var min = 0; var sec = 0;
        var vl = valueOfField;
        if (vl<0) { vl = -vl; IsPositive = false; }
        hr = parseInt(vl/3600, 10);
        vl = vl - (hr * 3600);
        min = parseInt(vl/60, 10);
        sec = vl - (min * 60);
        if (typeOfField=="T") {
          retValue = (IsPositive?"+":"-") + String(hr).leftfill("0", 2) + ":" + String(min).leftfill("0", 2);
        } else {
          retValue = (IsPositive?"+":"-") + String(hr).leftfill("0", 2) + ":" + String(min).leftfill("0", 2) + ":" + String(sec).leftfill("0", 2);
        }
      } break;
    case "I": case "A":
      if (!isNaN(valueOfField)) {
        retValue = String(valueOfField);
      }
      break;
    case "F":
      if (!isNaN(valueOfField)) {
        nDec=(""+nDec=="undefined"?2:nDec);
        retValue = Att2Str(valueOfField, "F", parseInt(nDec, 10)).replace(/,/gi,".");
      } else {
        retValue = "";
      } break;
    case "LAT": case "LON":
      if (!isNaN(valueOfField)) {
        nDec=(""+nDec=="undefined"?5:nDec);
        retValue = Att2Str(valueOfField, "F", parseInt(nDec, 10)).replace(/,/gi,".");
        if (!isNaN(retValue)) {
          retValue = String(retValue);
        } else {
          retValue = "";
        }
      } break;
  } if (retValue!="") {
    retValue = retValue.replace(/&/gi,"&amp;");
    retValue = retValue.replace(/\</gi,"&lt;");
    retValue = retValue.replace(/\>/gi,"&gt;");
    retValue = retValue.replace(/'/gi,"&apos;");
    retValue = retValue.replace(/"/gi,"&quot;");
  } return retValue;
} // Att2Xml()

// --
// Xml2Att()
// --
function Xml2Att(valueOfField, typeOfField) {
  var retValue = null;
  switch ( typeOfField ) {
    case "I":
      try {
        retValue = parseInt(valueOfField, 10);
      } catch(e) {
        retValue = Number.NaN;
      } break;
    case "F": case "LAT": case "LON":
      try {
        retValue = parseFloat(valueOfField);
      } catch(e) {
        retValue = Number.NaN;
      } break;
    case "D":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yyyy');
      } break;
    case "DT":
      if (!isNaN(valueOfField)) {
        retValue = formatDate(valueOfField,'dd/MM/yyyy HH:mm:ss');
      } break;
    case "S": case "B": default:
      try {
        retValue = String(valueOfField);
      } catch(e) {
        retValue = null;
      } break;
  } return retValue;
} // Xml2Att()

var
  CRLF = "\r\n";
// --
// showDuracao()
// --
function showDuracao( pVal ) {
  var str = "";
  var dias = Math.floor(pVal/3600);
  var horas = Math.floor((pVal - dias)/60);
  var minutos = pVal - (dias*3600) - (horas*60);
  if (dias>0) { str += ""+dias+"d " }
  str += (""+horas>10?""+horas:"0"+horas) + "h";
  str += (""+minutos>10?""+minutos:"0"+minutos) + "m";
  return str;
} // showDuracao()

// --
// showCounter()
// --
function showCounter( pVal ) {
  var str = "";
  var minutos = Math.floor(pVal/60);
  var segundos = (pVal - minutos)/1000;
  str += minutos + "m " + segundos + "s" ;
  return str;
} // showCounter()

// --
// Str2Html()
// --
function Str2Html(s){
  var retValue = "";
  if (typeof(s)=="string") {
    for (i=0; i<s.length; i++) {
      var sChar = s.substr(i, 1);
      if (sChar=="<") { 
        retValue += "&lt;"; 
      } else if (sChar==">") {
        retValue += "&gt;";
      } else if (sChar=="\"") {
        retValue += "&quot;";
      } else if (sChar=="'") {
        retValue += "&#039;";
      } else if (sChar=="&") {
        retValue += "&amp;";
      } else {
        retValue += s.substr(i, 1);
      }
    }
  } return retValue;
} // Str2Html()

// --
// ReplaceChar()
// --
function ReplaceChar(match) {
} // ReplaceChar()

// --
// Str2Jsp()
// --
function Str2Jsp(s) {
  s = (String(s)=="undefined"?"":String(s));
  var retValue = "";
  retValue = s.replace(/\n/gi,"<BR>");
  retValue = retValue.replace(/\r/gi,"");
  retValue = retValue.replace(/\"/gi,"&quot;");
  retValue = retValue.replace(/'/gi,'"');
  retValue = "'" + retValue + "'";
  return retValue;
} // Str2Jsp()

// --
// Str2ArrEval()
// --
function Str2ArrEval(pStr) {
  var retValue = new Array();
  var aStr = pStr.split(",");
  for (var x=0; x<aStr.length; x++) {
    retValue[x] = eval(aStr[x]);
  }
  return retValue;
} // Str2ArrEval()

// --
// Arr2Str()
// --
function Arr2Str(pStr, pIni, pFim) {
  var retValue = "";
  var aStr = pStr.split(",");
  for (var x=0; x<aStr.length; x++) {
    retValue += pIni + eval(aStr[x]) + pFim;
  }
  return retValue;
} // Arr2Str()

// --
// Replicate()
// --
function Replicate(s, n) {
  var str = "";
  for (x=0; x<n; x++) {
    str += s;
  }
  return str;
} // Replicate()

// --
// Spaces()
// --
function Spaces(n) {
  var str = "";
  for (x=0; x<n; x++) {
    str += " ";
  } return str;
} // Spaces()

// --
// offsetNumber()
// --
function offsetNumber(pArray, pValue) {
  var retValue = -1;
  try {
    if (isArray(pArray)&&(pArray.length>0)) { 
      for (var x=0; x<pArray.length; x++) {
        if (pArray[x]==pValue) {
          retValue = x;
          break;
        }
      }
    }
  } catch(e) {
    retValue = -1;
  }
  return retValue;
} // offsetNumber()

function leftfill(chr, tam) {
  var retValue = this;
  if (this.length<tam) {
    var rep = "";
    for (var x=0; x<tam-this.length; x++) {
      rep += chr;
    }
    retValue = rep + retValue;
  }
  return retValue;
} // leftfill()

function rightfill(chr, tam) {
  var retValue = this;
  if (this.length<tam) {
    var rep = "";
    for (var x=0; x<tam-this.length; x++) {
      rep += chr;
    }
    retValue += rep;
  }
  return retValue;
} // rightfill()

function left(n) {
  return n<=this.length?this.substr(0,n):this;
}
function right(n) {
  return n<=this.length?this.substr(this.length-n,n):this;
}
function count(chr) {
  count=this.split(chr);
  return count.length-1; 
}
function trim() {
  var a = this.ltrim().rtrim();
  return a;
}
function ltrim() {
  var regex1 = /^\s+/;
  var a = this.replace(regex1,'');
  return a;
}
function rtrim() {
  var regex1 = /\s+$/;
  var a = this.replace(regex1,'');
  return a;
}
function strip() {
  var regex1 = /[\/\.,\?;:'"\(\){}\[\]\!]+/g;
  var a = this.replace(regex1,'');
  return a;
}
function stripToInt() {
  var regex1 = /[\/\.,\?;:'"\(\){}\[\]\!\sa-zA-Z]+/g;
  var a = this.replace(regex1,'');
  return a;
}
function stripToFloat() {
  var regex1 = /[\/,\?;:'"\(\){}\[\]\!\sa-zA-Z]+/g;
  var a = this.replace(regex1,'');
  a = a.replace(/\./gi,",");
  return a;
}
function stripall() {
  var regex1 = /[\-\/\.,\?;:'"\(\){}\[\]\!]+/g;
  var a = this.replace(regex1,'');
  return a;
}
function toDate() {
  var a = "";
  a = this.substr(6,4)+"-"+this.substr(3,2)+"-"+this.substr(0,2);
  return a;
}
function toTime() {
  var a = "";
  a = this.substr(11,9);
  return a;
}
function toDateTime() {
  var a = "";
  a = this.toDate() + " " + this.toTime();
  return a;
}
function toShowDate() {
  var a = "";
  if (this.length>0) {
    a = this.substr(8,2)+"/"+this.substr(5,2)+"/"+this.substr(0,4);
  }
  return a;
}
function toShowTime() {
  var a = "";
  if (this.length>0) {
    a = this.substr(11,9);
  }
  return a;
}
function toShowDateTime() {
  var a = "";
  if (this.length>0) {
    a = this.toShowDate() + " " + this.toShowTime();
  }
  return a;
}

function stripAlpha() {
  var regex1 = /[^0-9]+/gi;
  var a = this.replace(regex1,'');
  return a;
}
function isDate() {
  var s = this.stripAlpha();
  var b = true;
  if (s.length!=8) {
    b = false;
    return b;
  } else {
    var day;
    var month;
    var year;
    var leap = 0;
    year = s.right(4);
    if (year == 0) { 
      b = false;
      return b;
    }
    month = s.substr(2,2);
    if ((month < 1) || (month > 12)) {
      b = false;
      return b;
    }
    day = s.left(2);
    if (day < 1) {
      b = false;
      return b;
    }
    if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
    }
    if ((month == 2) && (leap == 1) && (day > 29)) {
      b = false;
      return b;
    }
    if ((month == 2) && (leap != 1) && (day > 28)) {
      b = false;
      return b;
    }
    if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      b = false;
      return b;
    }
    if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      b = false;
      return b;
    }
  }
  return b;
}

// ---
// isNull()
// ---
function isNull(val) { 
  return( ((val==null)||(String(val)=="null")) );
} // isNull()

// ---
// isBlank()
// ---
function isBlank(val) {
  if (val==null) { return true; }
  for(var i=0;i<val.length;i++) {
    if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){
      return false;
    }
  } return true;
} // isBlank()

// ---
// isInteger(value)
// ---
function isInteger(val) {
  val = val.replace(/\./gi,'');
  val = val.replace(/^0*\s\./gi,'');
  if (isBlank(val)) { return false; }
  for(var i=0;i<val.length;i++) {
    if(!isDigit(val.charAt(i))) { return false; }
  } return true;
} // isInteger()

// ---
// isArray(obj)
// ---
function isArray(obj){
  return (typeof(obj.length)=="undefined")?false:true;
}

// ---
// isDigit(value)
// ---
function isDigit(num) {
  if (num.length>1) {
    return false;
  }
  var string="1234567890";
  if (string.indexOf(num)!=-1){
    return true;
  }
  return false;
} // isDigit()

// --
// isNumeric()
// --
function isNumeric(val) {
  var psin  = "";
  var pint  = "";
  var pfrac = "";
  var s = val;
  s = s.trim();
  s = s.replace(/^0*\s\./gi,'');
  if (isBlank(s)) {
    return false;
  }
  var a = s.split(',');
  if (a.length>2) {
    return false;
  }
  if (a[0].length==0) {
    psin = "";
    pint = "0";
  } else {
    if (a[0].charAt(0)=="-") {
      psin = "-";
      a[0] = a[0].replace(/\-/gi,'');
    }
    if (a[0].length==0) {
      pint = 0;
    } else {
       if (!(isInteger(a[0]))) {
        return false;
      } else {
        pint = parseInt(a[0], 10);
      }
    }
  }
  if (a.length==1) {
    pfrac = "0";
  } else {
    if (a[1].length==0) {
      pfrac = "0";
    } else {
      if (!(isInteger(a[1]))) {
        return false;
      } else {
        pfrac = parseInt(a[1], 10);
      }
    }
  }
  return true;
} // isNumeric()

function mod(a, b) { return a-Math.floor(a/b)*b }
function div(a, b) { return Math.floor(a/b) }
function max(a, b) { if (a>b) { return a } else { return b } }

// --
// formatNumber()
// --
function formatNumber(number,pattern) {
  if (!(isNumeric(""+number))) { number = 0; }
  var str = number.toString();
  var negativo = (number<0);
  var strInt;
  var strFloat;
  var formatInt;
  var formatFloat;
  if (negativo) { number = Math.abs(number); }
  if(/\./g.test(pattern)) {
    formatInt = pattern.split('.')[0];
    formatFloat  = pattern.split('.')[1];
  } else {
    formatInt = pattern;
    formatFloat  = null;
  }
  if(/\./g.test(str)) {
    if(formatFloat!=null) {
      var tempFloat  = Math.round(parseFloat('0.'+str.split('.')[1])*Math.pow(10,formatFloat.length))/Math.pow(10,formatFloat.length);
      strInt = (Math.floor(number)+Math.floor(tempFloat)).toString();        
      strFloat  = /\./g.test(tempFloat.toString())?tempFloat.toString().split('.')[1]:'0';
    } else {
      strInt = Math.round(number).toString();
      strFloat  = '0';
    }
  } else {
    strInt = str;
    strFloat = '0';
  }
  if(formatInt!=null) {
    var outputInt  = '';
    var zero = formatInt.match(/0*$/)[0].length;
    var comma = null;
  if(/,/g.test(formatInt)) {
      comma  = formatInt.match(/,[^,]*/)[0].length-1;
    }
  var newReg  = new RegExp('(\\d{'+comma+'})','g');
    if(strInt.length<zero) {
      outputInt  = new Array(zero+1).join('0')+strInt;
    outputInt  = outputInt.substr(outputInt.length-zero,zero)
  } else {
      outputInt  = strInt;
    }
  var 
  outputInt = outputInt.substr(0,outputInt.length%comma)+outputInt.substring(outputInt.length%comma).replace(newReg,(comma!=null?',':'')+'$1');
    outputInt = outputInt.replace(/^,/,'');
  strInt  = outputInt;
  }
  if(formatFloat!=null) {
    var outputFloat  = '';
  var zero = formatFloat.match(/^0*/)[0].length;
    if(strFloat.length<zero) {
      outputFloat    = strFloat+new Array(zero+1).join('0');
    //outputFloat    = outputFloat.substring(0,formatFloat.length);
    var outputFloat1  = outputFloat.substring(0,zero);
    var outputFloat2  = outputFloat.substring(zero,formatFloat.length);
    outputFloat    = outputFloat1+outputFloat2.replace(/0*$/,'');
  } else {
      outputFloat = strFloat.substring(0,formatFloat.length);
    }
  strFloat = outputFloat;
  } else {
    if(pattern!='' || (pattern=='' && strFloat=='0')) {
      strFloat  = '';
    }
  }
  strInt = strInt.replace(/,/gi,'.');
  return strInt+(strFloat==''?'':','+strFloat);
} // formatNumber()

// --
// wordWrap()
// --
function wordWrap(s,m,msk) {
  if (""+msk=="undefined") { msk = ""; }
  s = s.replace(/\r/gi,''); s = s.replace(/\n/gi,''); 
  if (msk!="") { s = s.replace(/\s/gi,msk); }
  var str = "";
  try {
    if (s.length<=m) { return s; }
    for (var x=0; x<Math.ceil(s.length/m); x++) {
      if (x>0) { str += "<br>"; }
      str += s.substring(x*m, (x+1)*m);
    }
  } catch(e) {
    str = e.description;
  }
  return str;
} // wordWrap()

// --
// WWrap()
// --
function WWrap(s,m,msk) {
  if (""+msk=="undefined") { msk = "_"; }
  s = s.replace(/\r/gi,''); s = s.replace(/\n/gi,''); s = s.replace(/\s/gi,msk);
  var str = "";
  try {
    var regua = "";
    regua += "0........1.........2.........3.........4.........5.........6.........7.........8.........9.........";
    regua += "0.........1.........2.........3.........4.........5.........6.........7.........8.........9.........";
    regua += "0.........1.........2.........3.........4.........5.........6.........7.........8.........9.........";
    str += regua.left(m) + "<br>";

    regua  = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
    regua += "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
    regua += "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
    str += regua.left(m) + "<br><br>";
    if (s.length<=m) { str += s; return str; }
    for (var x=0; x<Math.ceil(s.length/m); x++) {
      if (x>0) { str += "<br>"; }
      str += s.substring(x*m, (x+1)*m);
    }
  } catch(e) {
    str = e.description;
  }
  return str;
} // WWrap()

// --
// formatMask()
// --
function formatMask(str, msk) {
  str = (isBlank(str)?"":str.stripall());
  switch (msk) {
    case "DT":
      str = str.replace(/(\d{2})(\d{2})(\d{4})(\d{2})(\d{2})(\d{2})/, '$1/$2/$3 $4:$5:$6');
      break;
    case "CGC":
      str = str.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/, '$1.$2.$3/$4-$5');
      break;
    case "CPF":
      str = str.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, '$1.$2.$3-$4');
      break;
    case "CEP":
      str = str.replace(/(\d{5})(\d{3})/, '$1-$2');
      break;
    case "DIG":
      if (str.length==47) {
        str = str.replace(/(\d{5})(\d{5})(\d{5})(\d{6})(\d{5})(\d{6})(\d{1})(\d{14})/, '$1.$2 $3.$4 $5.$6 $7 $8');
      } break;
    case "CNAE":
      if (str.length==7) {
        str = str.replace(/(\d{2})(\d{2})(\d{1})(\d{2})/, '$1.$2-$3-$4');
      } break;
    case "CNJ":
      if (str.length==4) {
        str = str.replace(/(\d{3})(\d{1})/, '$1-$2');
      } break;
  } return str;
} // formatMask()

function delimited(chr) {
  return (chr + this + chr);
} // delimited()

String.prototype.delimited       = delimited;
String.prototype.leftfill        = leftfill;
String.prototype.rightfill       = rightfill;
String.prototype.left            = left;
String.prototype.right           = right;
String.prototype.trim            = trim;
String.prototype.ltrim           = ltrim;
String.prototype.rtrim           = rtrim;
String.prototype.strip           = strip;
String.prototype.stripToInt      = stripToInt;
String.prototype.stripToFloat    = stripToFloat;
String.prototype.stripall        = stripall;
String.prototype.stripAlpha      = stripAlpha;
String.prototype.isDate          = isDate;
String.prototype.toDate          = toDate;
String.prototype.toTime          = toTime;
String.prototype.toDateTime      = toDateTime;
String.prototype.toShowDate      = toShowDate;
String.prototype.toShowTime      = toShowTime;
String.prototype.toShowDateTime  = toShowDateTime;
Array.prototype.Count = function() {
  return this.length;
} // Array.Count()
