// javascript array class
// version 1.1.0.0

// CHANGELOG:
//
// 02/03/2008
//     1.1.0.0
//           Initial release


if (!RLdesign) {
	var RLdesign = function() { };
}

RLdesign.Arrays = function() {
	return {
		// Removes the index from the array
		// Usage:
		// RLdesign.Arrays.RemoveElement(myArray, 3);
		RemoveElement: function(haystack, index) {
			var size = haystack.length;
			if (parseInt(index) != "NaN" && index >= 0 && index < size) {
				for (i = index; i < size; i++) {
					haystack[i] = haystack[i + 1];
				}
				haystack.length = size-1;
			}
			return haystack;
		},
		

		// Searches for needle in array (haystack), returns true if found, false if not
		// Usage:
		// var found = RLdesign.Arrays.InArray(myArray, "foo");
		InArray: function(haystack, needle) {
			for (var i = 0; i < haystack.length; i++) {
				if (haystack[i] === needle) return true;
			}
			return false;
		},


		// Equivalent to InArray, but searches associative arrays instead
 		InAssociativeArray: function(haystack, needle) {
			if (haystack[needle] != undefined) return true;
			return false;
		},


		// Returns the index of needle in array (haystack) if found, otherwise returns null
		// Usage:
		// var index = RLdesign.Arrays.GetRowIndex(myArray, "foo");
		GetRowIndex: function(haystack, needle) {
			var size = haystack.length;
			var index = null;
			for (var i = 0; i < size; i++) {
				if (haystack[i] == needle) {
					index = i;
					break;
				}
			}
			return index;
		},


		// Returns true if array (haystack) is empty, or false if not
		// Usage:
		// var empty = RLdesign.Arrays.IsEmpty(myArray);
		// if (!empty) {  }
		IsEmpty: function(haystack) {
			var len = haystack.length;
			if (len == 0) {
				for (var key in this) {
					len++;
				}
			}
			return (len > 0) ? false : true;
		},


		// Searches for an array (needle) inside another array (haystack) returning true if found, false if not
		// Usage:
		// var exists = RLdesign.Arrays.FindArray(myArray, myInnerArray);
		// if (exists) {  }
		FindArray: function(haystack, needle) {
			var iNeedleSize = needle.length;
			var iSize = haystack.length;
			var bFound = false;
			var iFoundIndex = null;
			for (var i = 0; i < iSize; i++) {
				if (!bFound && RLdesign.Arrays.IsArray(haystack[i]) && haystack[i].length == iNeedleSize) {
					for (var y = 0; y < iNeedleSize; y++) {
						if (needle[y] == haystack[i][y]) {
							bFound = true;
						}
						else {
							bFound = false;
							break;
						}
					}
				}
				if (bFound) {
					iFoundIndex = i;
					break;
				}
			}
			return bFound;
		},


		// evaluates whether the given argument object is an array, returning a boolean
		IsArray: function(a) {
			return (a && a.length && typeof(a) != "string" && !a.tagName && !a.alert && typeof(a[0]) != "undefined");
		}
	}
} ();
