//================================= CLASS CONSTRUCTOR ========================================
function QueryField(iposition,iqData)
{
//MEMBER VARIABLES
//The index of this field in the fields vector of qData ...
 this.position = null;
//The query Data that contains this field ...
 this.qData = null;
//Specifies whether this field is sortable ...
 this.sortable = null;
//Specifies the sorting order for this field ...
 this.sortOrder = null;
//Category id ...
 this.categoryId = null;
//Category label
 this.categoryLabel = null;
//Field identifier
 this.id = null;
//Field label
 this.label = null;
//Field type ... found in global field types definitions -> Globals.FIELDTYPE_XXX
 this.type = null;
//If this field has LOV ...
 this.hasLOV = null;
//If the values of this field can be modified manually ...
 this.readOnly = null;
//If this field can be modified on the interface ...
 this.hidden = null;
//Condition list ...
 this.conditions = null;
//Currently active condition ...
 this.activeCondition = null;
//Values for this field ...
 this.values = null;
//The tag for this field(will be used to keep the server id of this field!!!)
 this.tag = null;

 this.initialize(iposition,iqData);
}

//====================================== STATIC ==============================================
QueryField.CATEGORYID_NOTIFICATIONS = 1;
QueryField.ID_DIRECTORIES = 1;
QueryField.ID_USERS = 2;
QueryField.ID_TOUSERS = 3;
QueryField.ID_SUBJECT = 4;
QueryField.ID_DATE = 5;
QueryField.ID_RDATE = 6;
QueryField.ID_STATUS = 7;
QueryField.CATEGORYID_STDDOCUMENTS = 2;
QueryField.CATEGORYID_PROTOCOLDOCUMENTS = 3;

//====================================== MEMBERS =============================================
QueryField.prototype.setDefaults = function()
{
 this.position = null;
 this.qData = null;
 this.sortable = null;
 this.sortOrder = null;
 this.categoryId = null;
 this.categoryLabel = null;
 this.id = null;
 this.label = null;
 this.type = null;
 this.hasLOV = null;
 this.readOnly = null;
 this.hidden = null;
 this.conditions = null;
 this.activeCondition = null;
 this.values = null;
 this.tag = null;
}

QueryField.prototype.initialize = function(iposition,iqData)
{
 this.position = iposition;
 this.sortable = true;
 this.sortOrder = Globals.SORTORDER_NONE;
 this.categoryId = -1;
 this.categoryLabel = "";
 this.id = -1;
 this.label = "";
 this.hasLOV = false;
 this.readOnly = false;
 this.hidden = false;
 this.qData = iqData;
 this.conditions = new Array();
 this.values = new Array();
}

QueryField.prototype.getPosition = function()
{
 return this.position;
}

QueryField.prototype.setPosition = function(iposition)
{
 this.position = iposition;
}

QueryField.prototype.getSortable = function()
{
 return this.sortable;
}

QueryField.prototype.setSortable = function(isortable)
{
 this.sortable = isortable;
}

QueryField.prototype.getSortOrder = function()
{
 return this.sortOrder;
}

QueryField.prototype.setSortOrder = function(isortOrder)
{
 if (this.sortable == false)
 {
  alert("Item not Sortable!");
  return;
 }
 if (this.sortOrder == isortOrder)
  return;
 this.sortOrder = isortOrder;
 if (this.qData != null)
 if (this.sortOrder != Globals.SORTORDER_NONE)
  this.qData.onSortFieldChanged(this);
 this.setModified();
}

QueryField.prototype.getCategoryId = function()
{
 return this.categoryId;
}

QueryField.prototype.setCategoryId = function(icategoryId)
{
 if (this.categoryId == icategoryId)
  return;
 this.categoryId = icategoryId;
}

QueryField.prototype.getCategoryLabel = function()
{
 return this.categoryLabel;
}

QueryField.prototype.setCategoryLabel = function(icategoryLabel)
{
 this.categoryLabel = icategoryLabel;
}

QueryField.prototype.getId = function()
{
 return this.id;
}

QueryField.prototype.setId = function(iid)
{
 this.id = iid;
}

QueryField.prototype.getLabel = function()
{
 return this.label;
}

QueryField.prototype.setLabel = function(ilabel)
{
 if (this.label.localeCompare(ilabel) == 0)
  return;
 this.label = ilabel;
}

QueryField.prototype.getType = function()
{
 return this.type;
}

QueryField.prototype.setType = function(itype)
{
 if (this.type == itype)
  return;
 this.type = itype;
}

QueryField.prototype.getHasLOV = function()
{
 return this.hasLOV;
}

QueryField.prototype.setHasLOV = function(ihasLOV)
{
 this.hasLOV = ihasLOV;
}

QueryField.prototype.getReadOnly = function()
{
 return this.hasLOV;
}

QueryField.prototype.setReadOnly = function(ireadOnly)
{
 this.readOnly = ireadOnly;
}

QueryField.prototype.getHidden = function()
{
 return this.hidden;
}

QueryField.prototype.setHidden = function(ihidden)
{
 this.hidden = ihidden;
}

QueryField.prototype.addCondition = function()
{
 var
  ncond = new QueryCondition(this.conditions.length);

 this.conditions.push(ncond);
 return ncond;
}

//Adds conditions by using a condition builder ...
QueryField.prototype.addConditionsByBuilder = function(icBuilder)
{
 var
  nCondVector = null;

 icBuilder.build();
 nCondVector = icBuilder.get();
 for (var i=0;i<nCondVector.length;i++)
 {
  nCondVector[i].setPosition(this.conditions.length);
  this.conditions.push(nCondVector[i]);
 }
 return nCondVector;
}

QueryField.prototype.getConditionByPosition = function(iposition)
{
 return this.conditions[iposition];
}

QueryField.prototype.getConditionById = function(iid)
{
 for (var i=0;i<this.conditions.length;i++)
 if (this.conditions[i].getId() == iid)
  return this.conditions[i];
 return null;
}

QueryField.prototype.getConditionCount = function()
{
 return this.conditions.length;
}

QueryField.prototype.getActiveCondition = function()
{
 return this.activeCondition;
}

QueryField.prototype.setActiveCondition = function(icondition)
{
 var
  ncval = 0,
  nval = 0,
  nValue = null;

//Checking duplicates ...
 if (this.activeCondition != null)
 if (icondition != null)
 if (this.activeCondition.getPosition() == icondition.getPosition())
  return;
//Getting params ...
 if (icondition != null)
  ncval = icondition.getnValues();
 else
  ncval = 0;
 nval = this.values.length;
//We'll reset the values vector for this field according to this condition's needed value count !!!
 if (ncval < nval)
  this.values.splice(ncval,nval);
 if (ncval > nval)
 {
  for (var i=0;i<(ncval - nval);i++)
   this.values.push(new QueryValue(this));
 }
 this.activeCondition = icondition;
//Since there have been a modification we'll set the modified flag ...
 this.setModified();
}

QueryField.prototype.getValue = function(iid)
{
 return this.values[iid];
}

QueryField.prototype.getValueCount = function()
{
 return this.values.length;
}

QueryField.prototype.getTag = function()
{
 return this.tag;
}

QueryField.prototype.setTag = function(itag)
{
 this.tag = itag;
}

QueryField.prototype.setModified = function()
{
 if (this.qData != null)
  this.qData.setModified();
}

QueryField.prototype.copyFrom = function(iqfield)
{
 var
  cnt1 = 0,
  cnt2 = 0;

 this.position = iqfield.getPosition();
 this.sortable = iqfield.getSortable();
 this.sortOrder = iqfield.getSortOrder();
 this.categoryId = iqfield.getCategoryId();
 this.categoryLabel = iqfield.getCategoryLabel();
 this.id = iqfield.getId();
 this.label = iqfield.getLabel();
 this.type = iqfield.getType();
 this.hasLOV = iqfield.getHasLOV();
 this.readOnly = iqfield.getReadOnly();
 this.hidden = iqfield.getHidden();
 this.tag = iqfield.getTag();
//Copying the conditions...
 cnt1 = this.conditions.length;
 cnt2 = iqfield.getConditionCount();
 if (cnt1 < cnt2)
 {
  for (var i=0;i<cnt2-cnt1;i++)
   this.addCondition();
 }
 if (cnt1 > cnt2)
 {
  for (var i=cnt2;i<cnt1;i++)
   this.conditions[i].finalize();
  this.conditions.splice(cnt2,cnt1-cnt2);
 }
 cnt1 = iqfield.getConditionCount();
 for (var i=0;i<cnt1;i++)
  this.conditions[i].copyFrom(iqfield.getConditionByPosition(i));
//Copying the active condition ...
 if( iqfield.getActiveCondition() )
    this.activeCondition = this.conditions[iqfield.getActiveCondition().getPosition()];
 else
    this.activeCondition = this.conditions[0];
//Copying the values...
 cnt1 = this.values.length;
 cnt2 = iqfield.getValueCount();
 if (cnt1 < cnt2)
 {
  for (var i=0;i<cnt2-cnt1;i++)
   this.values.push(new QueryValue(this));
 }
 if (cnt1 > cnt2)
 {
  for (var i=cnt2;i<cnt1;i++)
   this.values[i].finalize();
  this.values.splice(cnt2,cnt1-cnt2);
 }
 cnt1 = iqfield.getValueCount();
 for (var i=0;i<cnt1;i++)
  this.values[i].copyFrom(iqfield.getValue(i));
}

QueryField.prototype.hasSvrData = function()
{
 var
  cValues = null;

 if (this.activeCondition == null)
  return false;
 cValues = this.getValueCount();
 if (cValues == 0)
  return false;
//Testing to see if at least the first value has data ...
 if (this.values[0].hasSvrData() == false)
  return false;
 return true;
}

QueryField.prototype.getServletData = function(iprefix)
{
 var
  s = "";

//Encoding field type ...
 s += DATA_encodeParam(iprefix+"t",TYPE_clientToServer(this.type));
//Encoding active condition ...
 s += "&" + DATA_encodeParam(iprefix+"i",this.getTag());
 s += "&" + DATA_encodeParam(iprefix+"c",this.activeCondition.getTag());
//Encoding value count ...
 s += "&" + DATA_encodeParam(iprefix+"vc",this.getValueCount());
//Encoding values ...
 for (var i=0;i<this.values.length;i++)
 {
  if (this.values[i].hasSvrData() == false)
   continue;
  s += "&" + DATA_encodeParam(iprefix+"v"+i,this.values[i].getServletData());
 }
 return s;
}

QueryField.prototype.finalize = function()
{
//Clearing conditions ...
 for (var i=0;i<this.conditions.length;i++)
  this.conditions[i].finalize();
 this.conditions.splice(0,this.conditions.length);
//Clearing values ...
 for (var i=0;i<this.values.length;i++)
  this.values[i].finalize();
 this.values.splice(0,this.values.length);
 this.setDefaults();
}
