// Array Extensions  v1.0.5
// http://www.dithered.com/javascript/array/index.html
// code by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)




// Array.concat() - Join two arrays
if (Array.prototype.concat == undefined) {
  Array.prototype.concat = function (secondArray) {
  	var firstArray = this.copy();
  	for (var i = 0; i < secondArray.length; i++) {
  		firstArray[firstArray.length] = secondArray[i];
  	}
  	return firstArray;
  };
}

// Array.copy() - Copy an array
if (Array.prototype.copy == undefined) {
  Array.prototype.copy = function() {
  	var copy = new Array();
  	for (var i = 0; i < this.length; i++) {
  		copy[i] = this[i];
  	}
  	return copy;
  };
}

function _copy (arr) {
	var copy = new Array();
  	for (var i = 0; i < arr.length; i++) {
  		copy[i] = arr[i];
  	}
  	return copy;
};


// Array.pop() - Remove the last element of an array and return it
if (Array.prototype.pop == undefined) {
  Array.prototype.pop = function() {
  	var lastItem = null;
    if ( this.length > 0 ) {
        lastItem = this[this.length - 1];
        this.length--;
    }
    return lastItem;
  };
}

// Array.push() - Add an element to the end of an array
if (Array.prototype.push == undefined) {
  Array.prototype.push = function() {
  	var currentLength = this.length;
  	for (var i = 0; i < arguments.length; i++) {
  		this[currentLength + i] = arguments[i];
  	}
  	return this.length;
  };
}

// Array.shift() - Remove the first element of an array and return it
if (Array.prototype.shift == undefined) {
  Array.prototype.shift = function() {
  	var firstItem = this[0];
  	for (var i = 0; i < this.length - 1; i++) {
  		this[i] = this[i + 1];
  	}
  	this.length--;
  	return firstItem;
  };
}

_shift = function(arr) {
	var firstItem = arr[0];
	for (var i = 0; i < arr.length - 1; i++) {
		arr[i] = arr[i + 1];
	}
	arr.length--;
	return firstItem;
};

// Array.slice() - Copy several elements of an array and return them
if (Array.prototype.slice == undefined) {
  Array.prototype.slice = function(start, end) {
  	var temp;
  	
  	if (end == null || end == '') end = this.length;
  	
  	// negative arguments measure from the end of the array
  	else if (end < 0) end = this.length + end;
  	if (start < 0) start = this.length + start;
  	
  	// swap limits if they are backwards
  	if (end < start) {
  		temp  = end;
  		end   = start;
  		start = temp;
  	}
  	
  	// copy elements from array to a new array and return the new array
  	var newArray = new Array();
  	for (var i = 0; i < end - start; i++) {
  		newArray[i] = this[start + i];
  	}
  	return newArray;
  };
}

  function _slice (arr, start, end) {
  	var temp;
  	
  	if (end == null || end == '') end = arr.length;
  	
  	// negative arguments measure from the end of the array
  	else if (end < 0) end = arr.length + end;
  	if (start < 0) start = arr.length + start;
  	
  	// swap limits if they are backwards
  	if (end < start) {
  		temp  = end;
  		end   = start;
  		start = temp;
  	}
  	
  	// copy elements from array to a new array and return the new array
  	var newArray = new Array();
  	for (var i = 0; i < end - start; i++) {
  		newArray[i] = arr[start + i];
  	}
  	return newArray;
  };

// Array.splice() - Splice out and / or replace several elements of an array and return any deleted elements
if (Array.prototype.splice == undefined) {
  Array.prototype.splice = function(start, deleteCount) {
  	if (deleteCount == null || deleteCount == '') deleteCount = this.length - start;
  	
  	// create a temporary copy of the array
  	var tempArray = this.copy();
  	
  	// Copy new elements into array (over-writing old entries)
  	for (var i = start; i < start + arguments.length - 2; i++) {
  		this[i] = arguments[i - start + 2];
  	}
  	
  	// Copy old entries after the end of the splice back into array and return
  	for (var i = start + arguments.length - 2; i < this.length - deleteCount + arguments.length - 2; i++) {
  		this[i] = tempArray[i + deleteCount - arguments.length + 2];
  	}
  	this.length = this.length - deleteCount + (arguments.length - 2);
  	return tempArray.slice(start, start + deleteCount);
  };
}

function _splice(arr, start, deleteCount) {
  	if (deleteCount == null || deleteCount == '') deleteCount = arr.length - start;
  	
  	// create a temporary copy of the array
  	var tempArray = _copy(arr); //arr.copy();
  	
  	// Copy new elements into array (over-writing old entries)
  	for (var i = start; i < start + arguments.length - 2; i++) {
  		arr[i] = arguments[i - start + 2];
  	}
  	
  	// Copy old entries after the end of the splice back into array and return
  	for (var i = start + arguments.length - 2; i < arr.length - deleteCount + arguments.length - 2; i++) {
  		arr[i] = tempArray[i + deleteCount - arguments.length + 2];
  	}
  	arr.length = arr.length - deleteCount + (arguments.length - 2);
  	return _slice(tempArray, start, start + deleteCount);
};

// Array.unshift - Add an element to the beginning of an array
if (Array.prototype.unshift == undefined) {
  Array.prototype.unshift = function(the_item) {
  	for (loop = this.length-1 ; loop >= 0; loop--) {
  		this[loop+1] = this[loop];
  	}
  	this[0] = the_item;
  	return this.length;
  };
}


// Array.join
if (Array.prototype.join == undefined) {
  Array.prototype.join = function(glue) {
  	var res="";
  	for (var i=0;i<this.length;i++) {
  		res += this[i] + (i==this.length ? "" : glue);
  	}
  	return res;
  };
}


function split (del, str, max)
    {
    if (max==undefined) max=-1;
    var ind=0;
    var lastInd=0;
    var count=1;
    var word;
    var array = new Array();
    
    array.length = 0;
    while( ind< str.length )
        {
        ind = str.indexOf( del, lastInd );
        if (ind == -1 )
            ind = str.length;
        array.push (str.substring(lastInd,ind));
        //array.length++;
        lastInd = ind+1;
        if ((array.length>=max) && (max>0)) break;
        }
    return array;
    }

