/**  Array.js         written by Scott Honey
  tested ok on May 18, 1998, 11:44am
     A library of functions that work on javascript arrays.
     All the arrays must be in Netscape 3.0+ array format.
 
     arrayStringScramble( ARRAY) 
         returns an array with the same STRING data but with the positions of the data randomly swapped.
    
     arrayClear()
        returns an empty array.  access function for empty arrays.
    
     arraySearch(ARRAY,VALUE) 
        searches for the existance of VALUE (all values are permitted) in ARRAY.  returns true if it exists,
	false otherwise.  

     arrayAdd(ARRAY, VALUE)
        adds an element VALUE onto the end of the array.
	returns the new Array.

     arrayRemove(ARRAY, INDEX)
        removes an element in position INDEX from ARRAY.
        returns the array when done.
*/

/*   arrayStringScramble( ARRAY) 
         returns an array with the same STRING data but with the positions of the data randomly swapped.*/
function arrayStringScramble ( sourceArray ) {
  var targetArray = new Array( sourceArray.length );
  
  // targetArray will contain all Strings in the end
  // ints are for testing values only.
  for ( var arrayIndex = 0; arrayIndex < targetArray.length; arrayIndex++ ) {
      targetArray[ arrayIndex ] = -1; }
  
  // for each element in the sourceArray, keep picking random spots in targetArray until empty
  // one is found, then copy the value
  for ( var sourceArrayIndex = 0; sourceArrayIndex < sourceArray.length; sourceArrayIndex++ ) {
    insertedOK=false;
    while (insertedOK==false) {
      var randomIndex = randomInt( sourceArray.length )-1;
      if ( targetArray[ randomIndex ] == -1 ) {
	targetArray[ randomIndex ] = sourceArray[ sourceArrayIndex ];
	insertedOK=true;
	//alert(sourceArray+ " <br>"+targetArray+"<BR>"+ "got one!");
      } //end if
    } //while...
  }  //end for
    return( targetArray );
} //end arrayStringScramble



/*   arrayClear()
        returns an empty array.  access function for empty arrays.*/
function arrayClear() {
  return( new Array() );
}//end arrayClear


/*   arraySearch(ARRAY,VALUE) 
        searches for the existance of VALUE (all values are permitted)
        in ARRAY.  returns true if it exists, false otherwise.  */
function arraySearch( sourceArray, searchValue ) {
  for ( var arrayIndex = 0; arrayIndex < sourceArray.length; arrayIndex++ ) {
    if ( sourceArray[ arrayIndex ] == searchValue ) { 
      return true; }
  }
  return false;
}


/* arrayAdd(ARRAY, VALUE)
      adds an element VALUE onto the end of the array.
      returns the new Array.*/
function arrayAdd( sourceArray,Value ) {
  if ( arraySearch( sourceArray , Value ) ) {
    return sourceArray; }
  else {
    sourceArray[ sourceArray.length] = Value; 
    return sourceArray;}
}


/* arrayRemove(ARRAY, INDEX)
      removes an element in position INDEX from ARRAY.
      returns the array when done.*/
function arrayRemove( sourceArray, indexToRemove ) {

  //if the index to remove is greater than the length, return the array.
  if ( indexToRemove > sourceArray.length ) {
    alert("index greater than array, arrayRemove");  //debugline
    return sourceArray; }

  //otherwise, copy every value into target array except the bad index.
  var targetIndex = 0;
  var targetArray = new Array();
  for ( var sourceIndex = 0; sourceIndex < sourceArray.length ; sourceIndex++ ) {
    if ( sourceIndex != indexToRemove ) {
      targetArray[ targetIndex ] = sourceArray[ sourceIndex ];
      targetIndex++; }
  }
  return targetArray;
}//end arrayRemove












