/************** Util classes *******************************/

function List(){
   this.list = new Array();
   this.add=ListAddOrReplace;
   this.toArray=ToArray; 
}

function ListAddOrReplace(trade) {
   //var found=false;

   /*for (var i=0; i < this.list.length-1; i++) {
     t=this.list[i];
     if(t.id== trade.id){
        //remove the
         this.list.slice(i,i+1);
         this.list.push(trade) ;
         found=true;
     }
   }   */
 // if(!found){
     this.list.push(trade) ;
  //}
}

function ToArray(){
    return this.list;
}

/** NV pair object */
function NameValuePair(name, value) {
   this.name = name;
   this.value = value;
   this.setValue=NameValuePairSetValue;
}

function NameValuePairSetValue(val){
    this.value=val ;
}
/** Simple Map object to store array of name/value pairs */
function Map() {
  // Data members
   this.index = 0;
   this.map = new Array();

   // Function members
   this.get = MapGet;
   this.getNameValuePair = MapGetNameValuePair;
   this.remove = RemoveNameValuePair;
   this.put = MapPut;
   this.toString = MapToString;
   this.toTable = MapToTable;
   this.size=MapSize;
   this.toNameArray=MapToNameArray;
   this.toStringNames=MapToStringName;
}


function MapToNameArray(){
   arr=new Array();
   for (var i=0; i < this.index; i++) {
      arr[i]=this.map[i].name
   }
   return arr;
}

/** get() */
function MapGet(name) {
   for (var i=0; i < this.index; i++) {
     if (this.map[i].name == name) {
       return this.map[i].value;
     }
   }
   return null;
}

/** get() */
function MapGetNameValuePair(name) {
   for (var i=0; i < this.index; i++) {
     if (this.map[i].name == name) {
       return this.map[i];
     }
   }
   return null;
}

/** get() */
function RemoveNameValuePair(name) {
  for (var i=0; i < this.index; i++) {
     if (this.map[i].name == name) {
       	   	for(var j=i;j< this.index-1;j++){
			    this.map[j]=this.map[j+1];
			}
			this.index--;
			return true;
     }
   }
}

/** put() */
function MapPut(name, value) {
  if(this.get(name)!=null){
      this.getNameValuePair(name).setValue(value);
  }else{
	  this.map[this.index++] = new NameValuePair(name, value);
 }
}

//rohana

/** put() */
function MapSize() {
  return this.index;
}
/** To HTML string */
function MapToString() {
    var res = '';

   for (var i=0; i < this.index; i++) {
     res = res + this.map[i].name+'='+this.map[i].value+'\n';
   }
   return res;
}

/** To HTML string */
function MapToStringName(delimeter) {
    var res = '';

   for (var i=0; i < this.index; i++) {
     res = res + this.map[i].name+delimeter;
   }
   return res;
}

/** To HTML table */
function MapToTable() {
    var res = '<table border=1 cellpadding=3>';
   var styleDiv = "<div style=\"color:black; font-family:monospace; font-size:10pt; white-space:pre;\">"

   for (var i=0; i < this.index; i++) {
     res = res + '<tr><td bgColor=white>'+styleDiv+this.map[i].name+'</div></td><td bgColor=white>'+styleDiv+this.map[i].value+'</div></td></tr>';
   }
   res += '</table>'
   return res;
}
