// List Object JavaScript Functions
// File:    list.js
// Author:  Eric Baars
// WEBCAT:  1.2
// Version: 1.1
// History:
//    11 Apr 2000  V1.0: Initial creation...ETB
//    17 Apr 2000  Add set() method...ETB
//    13 Jun 2000  WEBCAT 1.1 (no changes)...ETB
//    24 Jun 2000  V1.1: (WEBCAT 1.2) Add toFormat() method...ETB
//    02 Aug 2000  Add isFound() method...ETB
//    19 Feb 2001  Return "" for empty list toString()...ETB
//     2 Apr 2004  changed _list_toFormat( delimiter ) to use a MUCH MUCH faster
//                 native method for re-separating the list...RMG
//     6 Apr 2004  Forgot to unescape take string in _list_toFormat() update, fixed now...RMG
//     9 Aug 2005  changed toString function to trim leading/trailing delimeters only if they exist...WFH
//
// CREATED BY
// var newlist = new list();
//
// PROPERTIES
// .length			reflects the number of items in the list
//
// METHOD SUMMARY
// .clear()			empty the list
// .add(item)		add item to the list (no duplicates)
// .remove(item)	remove item from the list
// .set(list2)		set list according to list2
// .isFound(item)	performs boolean check of whether item is in list
// .toString()		returns list as a string with spaces delimiting list items
// .toArray()		returns list as an array with each item as an array element
// .toFormat(delim)	returns list as a string, each element unescaped w/ delimiter char
//

//-------------------------------------------------------------------
// Global
// DO NOT change list_sep:
// must be a single space to simplify conversion to string
var list_sep = " ";

//-- Change List Contents
//-------------------------------------------------------------------
// Method: clear list
function _list_clear() {
	this.length = 0;  // public
	this.$data = list_sep; // private
}

// Method: add item to list
function _list_add(item) {
	if (this.$data.indexOf(list_sep+item+list_sep) == -1) {
                //alert("ACTUALLY GOING TO ADD ITEM " + this.length) ;
		this.$data += (item + list_sep);
		this.length = this.length + 1 ;
	}
}

// Method: remove item from list
function _list_remove(item) {
	var list = this.$data;
	var posn = list.indexOf(list_sep+item+list_sep);
        //alert( "REMOVING FROM LIST" ) ;
	if (posn != -1) {
		lSplit = list.substring(0,posn);
		rSplit = list.substring(posn + item.concat(list_sep).length, list.length);
		list = lSplit + rSplit;
		this.$data = list;
		this.length -= 1;
	}
}

// Method: assign list based on parameter
function _list_set(list) {
	this.length = list.length;
	this.$data  = list.$data
}

function _list_populate(data) {
	var space_count = 0 ;
	for( var x = 0 ; x < data.length ; x++ ) {
		if( data.charAt( x ) == ' ' ) {
			space_count++ ;
		}
	}
	this.length = space_count ;
	this.$data = data;
}

//-- Examine List Contents
//-------------------------------------------------------------------
// Method: returns true if item is found in list
function _list_isFound(item) {
	return (this.$data.indexOf(list_sep+item+list_sep) == -1) ? false : true;
}

//-- Format List Contents
//-------------------------------------------------------------------
// Method: return string representation of list
// 09 AUG 2005: added two while loops to trim delimeters from the start and end
//              rather than just always trimming...WFH
function _list_toString() {
	if (this.length < 1) return "";
	var data = this.$data;
	while ( data.charAt(0) == list_sep ) data = data.substring(1);
	while ( data.charAt(data.length-1) == list_sep ) data = data.substring(0,data.length-1);
	return data;
}

// Method: return array representation of list (one item per element)
function _list_toArray() {
	return this.toString().split(list_sep);
}

// Method: return formatted string representation of list
function _list_toFormat( delimiter ) {
	var aryList = this.toArray();
	var sValue = "";
    sValue = unescape( aryList.join( delimiter ) );
	return sValue;
}

// Old version that was really really really slow
// function _list_toFormat(delimiter) {
//	var aryList = this.toArray();
//	var sValue = "";
//	for (var i = 0; i < aryList.length; i++) {
//		if (sValue != "") sValue += delimiter;
//		sValue += unescape(aryList[i]);
//	}
//	return sValue;
// }



//-------------------------------------------------------------------
// Constructor
function list() {
	this.set = _list_set;
	this.populate = _list_populate;
	this.add = _list_add;
	this.remove = _list_remove;
	this.isFound = _list_isFound;
	this.clear = _list_clear;
	this.toString = _list_toString;
	this.toArray = _list_toArray;
	this.toFormat = _list_toFormat;
	this.clear(); // initialize properties
        this.length = 0 ;
}

