////////////////////////////////////////////////////////////////////////////////////////////// 
// Array methods scripts - created on 8.4.2004 by Jarkko Tuomala                             
// caution added on 16.9.2005                                                                                           
// added unique on 2.11.2005 
// 
// CAUTION:  When this library is used                                                                     
//                for (index in object) syntax with arrays will not work anymore like expected     
//                cause this library adds functions to array method and those are returned also   
//                when for looped the array.                                                                       
//                Inside for/in loop use syntax if (typeof object[index] != "function") to skip 
//                these own defined functions. 
// 
//                After all do _not_ use for/in loop with arrays! It should use only with objects! 
// 
////////////////////////////////////////////////////////////////////////////////////////////// 

        var undefined; 
        function isUndefined(property) { 
          return (typeof property == 'undefined'); 
        } 

        // Array.concat() - Join two arrays 
        /////////////////////////////////////// 
        if (isUndefined(Array.prototype.concat)) { 
          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 (isUndefined(Array.prototype.copy)) { 
          Array.prototype.copy = function() { 
             var copy = new Array(); 
             for (var i = 0; i < this.length; i++) { 
                copy[i] = this[i]; 
             } 
             return copy; 
          }; 
        } 

        // Array.pop() - Remove the last element of an array and return it 
        /////////////////////////////////////// 
        if (isUndefined(Array.prototype.pop)) { 
          Array.prototype.pop = function() { 
             var lastItem = undefined; 
            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 (isUndefined(Array.prototype.push)) { 
          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 (isUndefined(Array.prototype.shift)) { 
          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; 
          }; 
        } 

        // Array.slice() - Copy several elements of an array and return them 
        /////////////////////////////////////// 
        if (isUndefined(Array.prototype.slice)) { 
          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; 
          }; 
        } 

        // Array.splice() - Splice out and / or replace several elements of an array and return any deleted elements 
        /////////////////////////////////////// 
        if (isUndefined(Array.prototype.splice)) { 
          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); 
          }; 
        } 

        // Array.unshift - Add an element to the beginning of an array 
        /////////////////////////////////////// 
        if (isUndefined(Array.prototype.unshift)) { 
                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.qSort - sorting array by using qSort (fast!) 
        /////////////////////////////////////// 
        if (isUndefined(Array.prototype.qSort)) { 
                Array.prototype.qSort = function() { 
             var f = Math.floor; 
             var q = function(a, l, h) { 
                 var i = l; 
                 var j = h; 
                 var m = a[f((l+h)/2)]; 
                 while(1){ 
                     while(a[i]<m) ++i; 
                     while(m<a[j]) --j; 
                     if(i>=j) break; 
                     var t = a[i]; 
                     a[i] = a[j]; 
                     a[j] = t; 
                      ++i; 
                     --j; 
                 } 
                 if(l<i-1)q(a, l, i-1); 
                 if(j+1<h)q(a, j+1, h); 
             }; 
             q(this, 0, this.length-1); 
             return this; 
                }; 
        } 

        // Array.numSort - sorting array by numbers 
        /////////////////////////////////////// 
        if (isUndefined(Array.prototype.numSort)) { 
                Array.prototype.numSort = function() { 
                        var maxDigits = 0, len = this.length; 
            var i = len; 
            while (i) { 
                maxDigits = Math.max(maxDigits, String(this[--i]).length); 
            } 
            var zeros = ""; 
            var i = maxDigits; 
            while (--i) { 
                zeros += "0"; 
            } 
            var i = len; 
            while (i) { 
                this[--i] = (zeros + this[i]).slice(-maxDigits); 
            } 
            this.sortOn(undefined); 
            var i = len; 
            while (i) { 
                            this[--i] = parseInt(this[i], 10); 
                        } 
                }; 
        } 
        
        // Array.format - remove all array elements 
        /////////////////////////////////////// 
        if (isUndefined(Array.prototype.format)) { 
                Array.prototype.format = function () { 
                        while (this.length > 0) { 
                                this.pop(); 
                        } 
                }; 
        } 

        // Array.remove - remove array element 
        /////////////////////////////////////// 
        if (isUndefined(Array.prototype.remove)) { 
                Array.prototype.remove = function () { 
                        if (!isNaN(arguments[0])) { 
                                var Alength = this.length; 
                                var iRemoved = this[arguments[0]]; 
                                if (arguments[0] == 0) { 
                                        this.shift(); 
                                } else if (arguments[0] > Alength) { 
                                        return null; 
                                } else { 
                                        for (var i=arguments[0];i<Alength-1;i++) { 
                                                this[i] = this[i+1]; 
                                        } 
                                        this.pop(); 
                                } 
                                return iRemoved; 
                        } 
                        return null; 
                }; 
        } 

        // Array.index - return index in array for parameter 
        ///////////////////////////////////////         
        if (isUndefined(Array.prototype.index)) { 
                Array.prototype.index = function () { 
                        if (arguments[0] && arguments[0] != "") { 
                                var Alength = this.length; 
                                for (var i=0;i<Alength;i++) { 
                                        if (arguments[0].trim() == this[i].trim()) { 
                                                return i; 
                                        } 
                                } 
                        } 
                        return null; 
                }; 
        } 

        // Array.unique - remove duplicate values in array 
        ///////////////////////////////////////         
        if (isUndefined(Array.prototype.unique)) { 
                Array.prototype.unique = function () { 
                         // create a temporary copy of the array 
                             var tempArray = this.copy(); 
                        var Alength = this.length; 
                        var aremove = new Array(0); 
                        for (var i=0;i<Alength;i++) { 
                                var returned = tempArray.remove(0); 
                                if (tempArray.index(this[i])) { 
                                        aremove.push(i); 
                                } 
                        } 
                        for (i=0;i<aremove.length;i++) { 
                                returned = this.remove(aremove[i]-i); 
                        } 
                        this.sort(); 
                        return this; 
                }; 
        } 