/*
        array_ext.js - extend javascript Array funtionality.
        Note: taken from the book 'Ajax in ACTION' by Dave Crane,
              Eric Pascarello with Darren James.
*/

/*
   function to append to end of array, optionally checking for
   duplicates.
*/
Array.prototype.append=function(obj,nodup){
  if (!(nodup && this.contains(obj))){
    this[this.length]=obj;
  }
}

/*
  function to return index of element in the array.
*/
Array.prototype.indexOf=function(obj){
  var result=-1;
  for (var i=0;i<this.length;i++){
    if (this[i]==obj){
      result=i;
      break;
    }
  }
  return result;
}

/*
  function to return true if element is in the array.
*/
Array.prototype.contains=function(obj){
  return (this.indexOf(obj)>=0);
}

/*
  function to empty the array
*/
Array.prototype.clear=function(){
  this.length=0;
}

/*
  funtion to insert element at given position in the array, bumping all
  subsequent members up one index
*/
Array.prototype.insertAt=function(index,obj){
  this.splice(index,0,obj);
}

/*
  function to remove element at given index
*/
Array.prototype.removeAt=function(index){
  this.splice(index,1);
}

/*
  function to remove element in the array
*/
Array.prototype.remove=function(obj){
  var index=this.indexOf(obj);
  if (index>=0){
    this.removeAt(index);
  }
}

