var Meta
  = new Object();

// Meta.User - represents user meta data
//######################################
//######################################
Meta.User
= function( varName,
            orgCode,
            hpcId,
            metaCats,
            userData ) {
  this.varName  = varName;
  this.orgCode  = orgCode;
  this.hpcId    = hpcId;
  this.metaCats = metaCats;
  this.userData = userData; }

// Meta.User.extractValue
//-------------------------------
// return this respective value
/////////////////////////////////
Meta.User.prototype.extractValue
= function( catName,
            rowIdx,
            fieldName ) {

  var dataRows
    = usr1.userData[catName];

  if( (dataRows == null)
      || ((rowIdx+1) > dataRows.length) )
    return( null )

  return(
    dataRows[rowIdx][fieldName] ); }

// Meta.Cat - represents a meta category
//######################################
//######################################
Meta.Cat
= function( name,
            label,
            manyRows,
            rowLimit,
            fields,
            lastRowIdx ) {
  this.name       = name;
  this.label      = label;
  this.manyRows   = manyRows;
  this.rowLimit   = rowLimit;
  this.fields     = fields;
  this.lastRowIdx = lastRowIdx;
  this.isProfCat  = name.match(/^(personal|business|school|misc|address|degree|relation)$/); }

// Meta.Cat.extractRowKeys
//-------------------------------
// return the key(s) for this row (if 1-many)
/////////////////////////////////
Meta.Cat.prototype.extractRowKeys
= function( dataRow ) {

  // only 1-many cats
  //-----------------
  if(!this.manyRows) return(null);

  // setup key fields to search for
  //-------------------------------
  var catName = this.name;
  var keyFields
    = (catName=='address') ? {'addr_type':0,'res_addr_id':1}
      :(catName=='degree') ? {'degree_id':0}
      :(catName=='relation') ? {'relation_id':0}
      :null;
  if(!keyFields) return(null);

  // extract respective key vals
  //----------------------------
  var keys=null; var keyFieldSeq=null;
  for(var fieldName in this.fields) {
    if(typeof (keyFieldSeq=keyFields[fieldName]) !== 'undefined') {
      var metaField = this.fields[fieldName];
      if(!keys) keys=[];
      keys[keyFieldSeq] = [fieldName,dataRow[metaField.sequence]]; } }

  return( keys ); }

// Meta.Field - represents a category field
//#########################################
//#########################################
Meta.Field
= function( catName,
            name,
            label,
            formName,
            sequence,
            maxLength,
            isHidden,
            isRequired,
            isReadOnly,
            listsValues,
            isInUse ) {

  this.formName
    = formName.replace(
        /\.\d+$/,
        "._FLDROW_" );
  this.catName     = catName;
  this.name        = name;
  this.label       = label;
  this.sequence    = sequence;
  this.maxLength   = maxLength;
  this.isHidden    = isHidden;
  this.isRequired  = isRequired;
  this.isReadOnly  = isReadOnly;
  this.listsValues = listsValues;
  if( listsValues != null )
    this.listValues
      = (listsValues.length > 0)
          ? listsValues[0]
          : listsValues;
  this.isInUse    = isInUse; }

// Meta.Field.buildFormName
//-------------------------------
// for this field and data row, build the
// well-known form field name used by update process.
/////////////////////////////////
Meta.Field.prototype.buildFormName
= function( rowIdx ) {

  return(
    this.formName.replace(
      /_FLDROW_/,
      rowIdx ) ); }

// Meta.ListValue - represents a category list value
//##################################################
//##################################################
Meta.ListValue
= function( value,
            label ) {

  this.value = value;
  this.label = label; }

// Meta utility functions
//#######################
//#######################

// Meta.makeFieldMap( metaFieldArgLists )
// from arg lists, return col name -> Meta.Field map
//////////////////////////////////////////
Meta.makeFieldMap
= function( metaFieldArgLists ) {

  var metaFields
    = new Array();

  for( var argListIdx = 0;
       argListIdx < metaFieldArgLists.length;
       ++argListIdx ) {
    var argList
      = metaFieldArgLists[argListIdx];
    metaFields[argList[1]]
      = new Meta.Field(
          argList[0],
          argList[1],
          argList[2],
          argList[3],
          argList[4],
          argList[5],
          argList[6],
          argList[7],
          argList[8],
          argList[9],
          argList[10] ); }

  return(
    metaFields ); }

// Meta.makeListValueArray( metaListValueArgLists )
// from arg lists, return Meta.ListValue array
//////////////////////////////////////////
Meta.makeListValueArray
= function( metaListValueArgLists ) {

  var metaListsValues
    = new Array();

  for( var argListIdx = 0;
       argListIdx < metaListValueArgLists.length;
       ++argListIdx ) {

    var metaListValues
      = metaListsValues[argListIdx]
      = new Array();
    var argList
      = metaListValueArgLists[argListIdx];

    for( var argIdx = 0;
         argIdx < argList.length;
         ++argIdx ) {
      var arg
        = argList[argIdx];
      metaListValues[argIdx]
        = new Meta.ListValue(
            arg[0],
            arg[1] ); } }

  return(
    metaListsValues ); }


