﻿/*
  Knowmax State list Utility Unit for JavaScript  

  (c) 2007 Knowmax
*/

// Value object class.
function KMValueObject(id, value)
{  
  this.id = id;
  this.value = value;
}

KMValueObject.prototype.isValid = function()
{
  return ((this.id != null) && (this.id != ""));
}

// Value list class.
function KMValueList()
{
  this.list = new Array(); 
  this.uniquevalues = false; 
}

// Internally used to look up KMValueObject by id.
KMValueList.prototype._valueObjectById = function(id)
{
  if ((id == null) || (id == "")) {  
    return null;
  } else {        
    for (index = 0; index < this.list.length; index++) {    
      if (this.list[index].id == id) {
        return this.list[index];        
      }
    }
    
    return null;
  }
}

// Add given value pair to list. Obeys uniquevalues setting of list.
KMValueList.prototype.add = function(id, value)
{
  var valueobject = new KMValueObject(id, value);
  
  if (valueobject.isValid()) {
    if (this.uniquevalues) {
      if (!this.valueExists(id)) {
        return this.list.push(valueobject);  
      } else {
        return -1;
      }
    } else {
      return this.list.push(valueobject);  
    }
  } else {
    return -1;
  }
}

// Remove value with given id from list. Returns whether value pair with given id could be removed.
KMValueList.prototype.remove = function(id)
{
  var newlist = new Array();  
  var result = false;
  
  for (index = 0; index < this.list.length; index++) {
    if (this.list[index].id == id) {
      result = true;
    } else {
      newlist.push(this.list[index]);
    }
  }
  
  if (result) {
    this.list = newlist;
  }
  
  return result;
}

// Update existing value for value pair with given id. In case the value pair with the
// given id does not exists, this method will call the add() method.
KMValueList.prototype.update = function(id, value)
{
  var valueobject = this._valueObjectById(id);
  if (valueobject != null) {
    valueobject.value = value;
    return true;
  } else {
    return this.add(id, value);
  }  
}

// Returns value for value pair with given id. Returns null in case value is unavailable.
KMValueList.prototype.valueOf = function(id)
{
  var valueobject = this._valueObjectById(id);
  if (valueobject != null) {
    return valueobject.value;
  } else {
    return null;
  }  
}

// Determine whether value pair with given id exists.
KMValueList.prototype.valueExists = function(id)
{
  var valueobject = this._valueObjectById(id);
  if (valueobject != null) {
    return true;
  } else {
    return false;
  }  
}

var KMStateList = {  
  majorversion: function() {
    return 1;
  },
  
  minorversion: function() {
    return 0;
  },
  
  version: function() {
    return this.majorversion() + "." + this.minorversion();  
  },
  
  // Get access to instance of KMValueList.
  values: function() {
    return this._statelist;
  },  
  
  // Internally called to set default values.
  _construct: function() {
    this._statelist = null;
    this._initialized = false;
    this._statelistinstance = null;
  },
  
  // Call to initialize KMStateList. Only page with element with id "kmstatepage" should call initialize.
  initialize: function() {
    if (!this._initialized) {
      this._statelist = new KMValueList();
      this._statelist.uniquevalues = true;
      this._initialized = true;
      return true;
    } else {
      return true; 
    }
  },
  
  // Checks whether this instance of KMStateList is initialized.
  initialized: function() {
    return this._initialized;
  },
  
  // Looks up reference to page with element with id "kmstatepage" that has main instance of KMStateList. 
  getStatePage: function() {  
    var statepageid = "kmstatepage";  
    var statepage = document.getElementById(statepageid); 
    if (statepage) {
      return self;
    } else if (parent) {
      try {
        documentlevel = parent;
        while ((!statepage) && (documentlevel)) {
          statepage = documentlevel.document.getElementById(statepageid);
          if (statepage) {
            return documentlevel;
          } else if (documentlevel.parent) {
            documentlevel = documentlevel.parent;
          } else {
            documentlevel = null;
          }
        }
      } catch(err) {
        return null;
      }            
    }
    
    return null;       
  },  
  
  // Looks up reference to an initialized instance of KMStateList that could be used globally.
  // This instance should reside on a page with an element with id "kmstatepage".
  getStateList: function() {
    if (this._statelistinstance) {
      return this._statelistinstance;
    } else {    
      var statepage = this.getStatePage();
      if (statepage) {
        try {
          if (statepage.KMStateList.initialized()) {
            this._statelistinstance = statepage.KMStateList;
            return this._statelistinstance;
          } else {
            return null;
          }
        } catch(err) {
          return null;
        }
      } else {
        return null;
      }
    }
  }
}  

KMStateList._construct();

