//
//  Library containing CSTxt - Charater filters, min/max character
//                             constraints, case conversion.
//
//  Author: Giles Mullen
//  Date:   08/31/2001
//
//  Requires: CS00_Lib_<Language> (i.e. CS00_Lib_English)
//            CS10_Lib_Shared
//            CS20_Lib_Parser
//            CS25_Lib_CSErr
//

CSTxt.ERR_FAM           = 0x1000;
CSTxt.ERR_LENTOOLONG    = CSTxt.ERR_FAM | 0x0001;
CSTxt.ERR_LENTOOSHORT   = CSTxt.ERR_FAM | 0x0002;
CSTxt.ERR_INVLDCHAR     = CSTxt.ERR_FAM | 0x0004;

CSTxt.ERR_LENTOOLONG1_STR  = CSTXT_ERR_LENTOOLONG1_STR;
CSTxt.ERR_LENTOOLONG2_STR  = CSTXT_ERR_LENTOOLONG2_STR;
CSTxt.ERR_LENTOOLONG3_STR  = CSTXT_ERR_LENTOOLONG3_STR;
CSTxt.ERR_LENTOOSHORT1_STR = CSTXT_ERR_LENTOOSHORT1_STR;
CSTxt.ERR_LENTOOSHORT2_STR = CSTXT_ERR_LENTOOSHORT2_STR;
CSTxt.ERR_INVLDCHAR1_STR   = CSTXT_ERR_INVLDCHAR2_STR;
CSTxt.ERR_INVLDCHAR2_STR   = CSTXT_ERR_INVLDCHAR2_STR;

CSTxt.ERR_AL               = CSTXT_ERR_AL;
CSTxt.ERR_NUM              = CSTXT_ERR_NUM; 
CSTxt.ERR_ALSL             = CSTXT_ERR_ALSL;
CSTxt.ERR_NUMSL            = CSTXT_ERR_NUMSL;
CSTxt.ERR_SPACE            = CSTXT_ERR_SPACE;
CSTxt.ERR_AND              = CSTXT_ERR_AND;

CSTxt.AL                   = CS_TEXT_ALPHA;
CSTxt.NUM                  = CS_TEXT_NUM;
CSTxt.SPACE                = CS_TEXT_SPACE;
CSTxt.ALSL                 = CS_TEXT_ALPHASPL;
CSTxt.NUMSL                = CS_TEXT_NUMSPL;

function CSTxt_getErrorStringStat( no, ex )
{
  var ret = CSErr.ERR_UNDEFINED_STR;
  if (no == CSTxt.ERR_LENTOOLONG) {
    if (ex != null && ex.v != null && ex.l != null) {
      ret = CS_InsertString( CSTxt.ERR_LENTOOLONG3_STR, ex.v );     
      ret = CS_InsertString( ret, ex.l );     
    } else if (ex != null && ex.l != null) {
      ret = CS_InsertString( CSTxt.ERR_LENTOOLONG2_STR, ex.l );  
    } else {
      ret = CSTxt.ERR_LENTOOLONG1_STR;
    }
  } else if (no == CSTxt.ERR_LENTOOSHORT) {
    if (ex == null || ex.l == null) {
      ret = CSTxt.ERR_LENTOOSHORT1_STR;
    } else {
      ret = CS_InsertString( CSTxt.ERR_LENTOOSHORT2_STR, ex.l );     
    }
  } else if (no == CSTxt.ERR_INVLDCHAR) {
    ret = CSTxt.buildErrMsg( ex.acm );
  }
  return ret;
}

function CSTxt_getErrorString()
{
  return CSTxt_getErrStrStat( this.err );
}

function CSTxt_getErrStrStat( err )
{
  return CSTxt_getErrorStringStat( err.no, err.ex );
}

function CSTxt_toUpperCase( str )
{
  return str.toUpperCase();
}

function CSTxt_toLowerCase( str )
{
  return str.toLowerCase();
}

function CSTxt_toProperCase( string )
{ 
  var tmp = string.charAt( 0 ).toUpperCase();
  var pattern = /\s/;
  for (var i = 1; i < string.length; i++) {
    if (string.charAt( i - 1 ).search( pattern ) != -1 ) {
      tmp += string.charAt( i ).toUpperCase();
    } else {
      tmp += string.charAt( i ).toLowerCase();
    }
  }
  return tmp;
}

function CSTxt_overwriteText( s, is, sS, sE )
{
  var ret = s.slice( 0, sS );
  ret += is;
  ret += s.slice( sE );
  return ret;
}

function CSTxt_format( val, type )
{
  var ret = val;
  switch( type ) {
    case "proper":
      ret = CSTxt_toProperCase( val );
      break;
    case "upper":
      ret = CSTxt_toUpperCase( val );
      break;
    case "lower":
      ret = CSTxt_toLowerCase( val );
      break;
  }
  return ret;
}

function CSTxt_validate( val, mncc, mxcl, eem )
{
  var ret = true;
  if (mncc != null && val.length < mncc) {
    this.err = new CSErr( CSTxt.ERR_LENTOOSHORT, new Object() );
    this.err.ex.l = mncc.toString();
    ret = false;
  } else if (mxcl != null && val.length > mxcl) {
    this.err = new CSErr( CSTxt.ERR_LENTOOLONG, new Object() );
    this.err.ex.v = eem ? val.toString() : null;
    this.err.ex.l = mxcl.toString();
    ret = false;
  }
  return ret;
}

function CSTxt_charsValid( v, f, acm )
{
  var ret = CS_CharFilter( v, f );
  if (!ret) {
    this.err = new CSErr( CSTxt.ERR_INVLDCHAR, new Object() );
    this.err.ex.acm = acm;
  }
  return ret;
}

function CSTxt_buildErrMsg( acm )
{
  var ret = CSTxt.ERR_INVLDCHAR1_STR;
  if (acm != null && typeof( acm ) == "number") {
    var c = 0;
    var a = new Array();
    if (acm & 0x01)
      a[c++] = CSTxt.ERR_AL;
    if (acm & 0x02)
      a[c++] = CSTxt.ERR_NUM;
    if (acm & 0x04 && acm & 0x01)
      a[c++] = CSTxt.ERR_ALSL;
    else if (acm & 0x04 && acm & 0x02) 
      a[c++] = CSTxt.ERR_NUMSL;
    if (acm & 0x08)
      a[c++] = CSTxt.ERR_SPACE;
    var sa = "";
    for (var i = 0; i < a.length; i++) {
      sa += (i > 0 && a.length > 2) ? ", " : "";
      sa += (1 > 0 && a.length > 1 && (i == (a.length - 1))) ? " " + CSTxt.ERR_AND + " " : "";
      sa += a[i];
    }
    ret = CS_FormatMessage( CSTxt.ERR_INVLDCHAR2_STR, sa );
  }
  return ret;
}

function CSTxt_buildAllowedChars( acm )
{
  var ret = "";
  do {
    if (acm == null || typeof( acm ) != "number" )
      break;
    if (acm & 0x01)
      ret += CSTxt.AL;
    if (acm & 0x02)
      ret += CSTxt.NUM;
    if (acm & 0x04 && acm & 0x01)
      ret += CSTxt.ALSL;
    else if (acm & 0x04 && acm & 0x02)
      ret += CSTxt.NUMSL;
    if (acm & 0x08)
      ret += CSTxt.SPACE;
  } while (false);
  return ret;
}

function CSTxt()
{
  this.err = new CSErr( CSErr.ERR_SUCCESS )
}

CSTxt.prototype.validate       = CSTxt_validate;
CSTxt.prototype.format         = CSTxt_format;
CSTxt.prototype.charsValid     = CSTxt_charsValid;
CSTxt.prototype.getErrorString = CSTxt_getErrorString;
CSTxt.getErrorString           = CSTxt_getErrStrStat;
CSTxt.toLowerCase              = CSTxt_toLowerCase;
CSTxt.toUpperCase              = CSTxt_toUpperCase;
CSTxt.toProperCase             = CSTxt_toProperCase;
CSTxt.overwriteText            = CSTxt_overwriteText;
CSTxt.buildErrMsg              = CSTxt_buildErrMsg;
CSTxt.buildAllowedChars        = CSTxt_buildAllowedChars;
