/////////////////
/////STRINGS/////
/////////////////
TYPE_BOOLEAN = "boolean";
TYPE_NUMBER = "number";
TYPE_FUNCTION = "function";
TYPE_STRING = "string";
TYPE_OBJECT = "object";
TYPE_UNDEFINED = "undefined";

METHOD_CLONE = "clone";

TRUE = "true";
FALSE = "false";
NULL = "null";

String.empty = "";

String.prototype.startsWith = function(str) {
	return str != null && str != String.empty && !this.indexOf(str);
}

String.prototype.endsWith = function(str) {
	var index = this.lastIndexOf(str);
	return str != null && str != String.empty &&
		index != -1 && index + str.length == this.length;
}

String.prototype.trim = function() {
	return this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/, '$1');
}

String.prototype.toBoolean = function() {
	return this.toLowerCase() == TRUE;
}

Array.prototype.indexOf = function(value) {
	var length = this.length;
		for (var i = 0 ; i < length ; i++)
			if (this[i] == value )
				return i;
	return -1;
}

Array.prototype.lastIndexOf = function(value) {
	var length = this.length;
		for (var i = length - 1 ; i >= 0 ; i--)
			if (this[i] == value)
				return i;
	return -1;
}

Array.prototype.contains = function(value) {
	return this.indexOf(value) != -1;
}

Array.prototype.removeAt = function(index) {
	if (index >= 0 && index <= (this.length / 2)) {
		for (var i = index ; i >= 0 ; i--) {
			if (i != 0) {
				this[i] = this[i - 1];
			} else {
				this.shift();
				return true;
			}
		}
	} else if (index > (this.length / 2)) {
		for (var i = index ; i < this.length ; i++) {
			if (i != (this.length - 1)) {
				this[i] = this[i + 1];
			} else {
				this.pop();
				return true;
			}
		}
	}
	return false;
}

Array.prototype.remove = function(value) {
	while (this.indexOf(value) != -1)
		if (!this.removeAt(this.indexOf(value)))
			return false;
	return true;
}

Array.prototype.peek = function() {
	return this[this.length - 1];
}

Array.prototype.each = function(functor) {
	for (var i = 0 ; i < this.length ; i++) {
		functor(this[i]);
	}
}

Array.prototype.eachWithIndex = function(functor) {
	for (var i = 0 ; i < this.length ; i++) {
		functor(this[i], i);
	}
}

Array.prototype.clone = function() {
	var length = this.length;
	var nAry = new Array(length);
	for (var i = 0 ; i < length ; i++)
		nAry[i] = this[i];
	return nAry;
}

Date.prototype.clone = function() {
	return new Date(this.getTime());
}

///////////////////////
//REGULAR EXPRESSIONS//
///////////////////////
function validateControl(sourceElement, destinationElement, sMessage, sPattern, bRequired) {
	sMessage = "<font color=red>" + sMessage + "</font>";
	if(sourceElement.value.length == 0)
		destinationElement.innerHTML = bRequired == "True" ? sMessage : "";
	else
		destinationElement.innerHTML = new RegExp(sPattern).exec(sourceElement.value) == null ? sMessage : "";
}

function throwAlert(elementToCheck, messageToDisplay, pattern, required) {
	if((elementToCheck.value.length == 0 && required) || (new RegExp(pattern).exec(elementToCheck.value) == null)) {
		alert(messageToDisplay);
		elementToCheck.focus();
		return false;
	}
	else {
		return true; //Expression is ok
	}
}

function validateControlBetween(sourceElement, destinationElement, sMessage, iInterval1, iInterval2) {
	sMessage = "<font color=red>" + sMessage + "</font>";
	if(sourceElement.value >= iInterval1 && sourceElement.value <= iInterval2)
		destinationElement.innerHTML = "";
	else
		destinationElement.innerHTML = sMessage;
}
/////////////////
//MISCELLANEOUS//
/////////////////
function setCalendarVisibility(cellToHide1, cellToHide2, dropdownlist, Value) {
	var s = dropdownlist.value == Value ? 'visible' : 'hidden';
	cellToHide1.style.visibility = cellToHide2.style.visibility = s;
}

function setValuesVisibility(controlstohide, dropdownlist, inValue1, inValue2) {
	var s = dropdownlist.value == inValue1 || dropdownlist.value == inValue2 ? 'hidden' : 'visible';
	controlstohide.style.visibility = s;
}

function setFocus(id) {
	document.getElementById(id).focus();
}

function getSelectedRadio() {
	var radios = document.getElementsByTagName("input");
	for(var i = 0; i < radios.length; i++) {
		var e = radios[i];
		if(e != null && e.type == 'radio' && e.checked == true) {
			return e;
		}
	}
	return null;
}

function processTab(input, e) {
	if (e.keyCode != 9)
		return;

	if (input.setSelectionRange) {	// Mozilla & Opera
		var st = input.scrollTop;
		var iStart = input.selectionStart;
		var iEnd = input.selectionEnd;
		input.value = input.value.substring(0, iStart)+ "\t" + input.value.substring(iEnd);
		input.setSelectionRange(iStart + 1, iStart + 1);
		input.scrollTop = st;
	}
	else if (document.selection) {	// IE
		var range = document.selection.createRange();

		if (range.parentElement() == input) {
			var bEmpty = range.text == '';
			range.text = "\t";

			if (!bEmpty)
				range.select();
		}
	}
	cancelEvent(e);
}

function encodeUriParam(param) {
    return encodeURI(param);
}

function decodeUriParam(param) {
    return decodeURI(param);
}

function cancelEvent(ev) {
	if (xIE4Up) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
}

function setContainerActivation(id, enable) {
	var elem = document.getElementById(id);
	if (elem != null) {
		var els = elem.getElementsByTagName("*");
		for (var i = 0; i < els.length; i++) {
			var child = els[i];
			if (child.disabled != undefined)
				child.disabled = enable ? false : true;
		}
	}
}

function Hashtable() {

	this.container = new Object();

	this.put = function(key, value) {
		this.container[key] = value;
	}

	this.get = function(key) {
		return this.container[key];
	}

	this.size = function() {
		var i = 0;
		for (var slot in this.container)
			if (slot != METHOD_CLONE)
				i++;
		return i;
	}

	this.length = this.size;

	this.containsKey = function(key) {
		return this.container.hasOwnProperty(key);
	}

	this.containsValue = function(value) {
		for (var slot in this.container)
			if (slot != METHOD_CLONE && this.container[slot] == value)
				return true;
		return false;
	}

	this.contains = function(key, value) {
		return this.container[key] == value;
	}

	this.getKeys = function() {
		var keys = new Array();
		for (var slot in this.container)
			if (slot != METHOD_CLONE)
				keys.push(slot);
		return keys;
	}

	this.getValues = function() {
		var values = new Array();
		for (var slot in this.container)
			if (slot != METHOD_CLONE)
				values.push(this.container[slot]);
		return values;
	}

	this.each = function(functor) {
		for (var slot in this.container) {
			if (slot != METHOD_CLONE) {
				functor(slot, this.container[slot]);
			}
		}
	}
}

function adjustLayout() {
    var centerHeight = xHeight("tab") + 35;
    if (centerHeight == 35)
        centerHeight = xHeight("main");
    var leftHeight = xHeight("left");
    var rightHeight = xHeight("right");
    var maxHeight = Math.max(centerHeight, Math.max(leftHeight, rightHeight));
    xHeight("main", maxHeight);
    // Show the footer
    xShow("bottom");
    if (window.xIE4Up && rightHeight > 0) {
        var right = document.getElementById('right')
        var left = document.getElementById('left')
        if (right && left) {
            var tab = document.getElementById('tab');
            var iFirstWidth = xClientWidth() - (xWidth(right) + xWidth(left)) - 10;
            if (right.offsetLeft > (tab.offsetLeft + 5)) {
                tab.style.overflow = "auto";
                xWidth(tab, iFirstWidth);
            }
        }
    }
}
