function deleteItem( array, i ) {
    array.splice(i, 1);
}

function itemMoveUp( array, i ) {
    var result = [];
    result = array.slice(0, i - 1);
    result.push( 
        array[i], 
        array[i - 1]
    );
    if( i + 1 <= array.length ){
        var end = array.slice(i + 1);
        for( var j = 0; j < end.length; j++ ){
            result.push(
                end[j]
            );
        }
    }
    return result;
}

function itemMoveDown( array, i ) {
    var result = [];
    result = array.slice(0, i);
    result.push( 
        array[i + 1], 
        array[i]
    );
    if( i + 2 <= array.length ){
        var end = array.slice(i + 2);
        for( var j = 0; j < end.length; j++ ){
            result.push(
                end[j]
            );
        }
    }
    return result;
}

/* cuts 'primaryArray' into 2 depending on if 'tocut' property of array element is greater than 0 */
function separateArray(primaryArray, _trueArray, _falseArray, tocut) { 
    var tocut = typeof tocut!='undefined' ? tocut : 'checked';
    for (var i=0; typeof primaryArray[i]=='object'; i++) {
        if (primaryArray[i][tocut] > 0) {
            _trueArray[_trueArray.length] = primaryArray[i];
        } else {
            _falseArray[_falseArray.length] = primaryArray[i];
        }
    }
}

/* sorts associative array by 'tosort' property */
function sortAssocArray(array, tosort, reverse) {
    var tosort = typeof tosort!='undefined' ? tosort : 'name';
    array.sort(
        function(a, b) {
            if (a[tosort] > b[tosort]) return (reverse) ? -1 : 1;
            if (a[tosort] < b[tosort]) return (reverse) ? 1 : -1;
            return 0;
        }
    );
    return array;
}

/* refactors existing array depending on 'tocut' and 'tosort' properties */
function trueBeforeFalse(array, _trueArray, _falseArray, tocut, tosort) {
    var tocut = tocut ? tocut : 'checked';
    var tosort = tosort ? tosort : 'name';
    separateArray(array, _trueArray, _falseArray, tocut);
    var trueArray = sortAssocArray(_trueArray, tosort);
    var falseArray = sortAssocArray(_falseArray, tosort);
    return trueArray.concat(falseArray);
}

/* shuffles array to get _yngveisc_order_ */
function shuffle_array(arr) {
    var delim = (arr.length%2 == 0) ? arr.length/2 : (arr.length+1)/2;
    var temp0 = new Array();
    var temp1 = arr.slice(0,delim);
    var temp2 = arr.slice(delim);
    for (var j=0; j<temp1.length; j++) {
        temp0[temp0.length] = temp1[j];
        if (temp2[j]) temp0[temp0.length] = temp2[j];
    }
    return temp0;
}


/* --------------------------------------------------------
add some useful methods to the javascript array class.
All operating on the built-in Array class,
so no need for any namespacing object.

Author: Dave Crane (c) 2005
--------------------------------------------------------- */

/* append to end of array, optionally checking for duplicates */
Array.prototype.append=function(obj,nodup){
  if (!(nodup && this.contains(obj))){
    this[this.length]=obj;
  }
}

/* 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;
}

/* return true if element is in the array */
Array.prototype.contains=function(obj){
  return (this.indexOf(obj)>=0);
}

/* empty the array */
Array.prototype.clear=function(){
  this.length=0;
}

/* 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);
}

/* remove element at given index */
Array.prototype.removeAt=function(index){
  this.splice(index,1);
}

/* return index of element in the array */
Array.prototype.remove=function(obj){
  var index=this.indexOf(obj);
  if (index>=0){
    this.removeAt(index);
  }
}

