//*************************************************************************************************
// Character Counter
//*************************************************************************************************
function checkLength(textBox, maxSize) {	
	progressbar = document.getElementById(textBox.name + "_progress");
	progressbar.className = progressbar.className.replace("-inverse", "");
	progress = textBox.value.length * 100 / maxSize;
	progress = progress / 100 * textBox.offsetWidth-2;
	if (textBox.value.length > maxSize) {
		progress = textBox.offsetWidth-2;
		progressbar.className = progressbar.className + "-inverse";
	} else { progress = progress;}
	progressbar.style.width = progress + "px";
	document.getElementById(textBox.name + "_size").innerHTML = textBox.value.length;
}

//*************************************************************************************************
// Layer switch (show/hide)
//*************************************************************************************************

var activelayer;
function activateLayer(Id){
    if (activelayer != null) {
        activelayer.style.visibility = 'hidden';
        if (activelayer.id == Id) {
            activelayer = null;
            return;
        }
    }
    if (Id != null) {
    	activelayer = document.getElementById(Id);
    	activelayer.style.width = activelayer.parentNode.offsetWidth+'px';
    	activelayer.style.visibility = 'visible';
    }
}

//*************************************************************************************************
// Content selector
//*************************************************************************************************

var componentActive = new Hashtable();
function activeComponent(ContextId, Component) {
	componentActive.put(ContextId, Component);
}

function loadMenuBar(ContextId) {
	component = componentActive.get(ContextId);
	if (component != null) {
		loadComponent("MyWeb.ActionProcessor?" + component.id, ContextId);
	} // Clear activeted layers 
	activateLayer(null);
}

function loadComponent(Url, ContextId) {	
	if (window.XMLHttpRequest) {
		// Mozzilla engines
    	httprequest = new XMLHttpRequest();
        httprequest.onreadystatechange = function() {httpRequestListener(httprequest, ContextId);};
        httprequest.open("GET", Url, true);
        httprequest.send(null);
    } else {
    	// IE engines
        httprequest = new ActiveXObject("Microsoft.XMLHTTP");
        if (httprequest){
            httprequest.onreadystatechange = function() {httpRequestListener(httprequest, ContextId);};
            httprequest.open("GET", Url, true);
            httprequest.send();
        }
    }
}

function httpRequestListener(HttpRequest, ContextId) {
	if (HttpRequest.readyState == 4 && HttpRequest.status == 200) {
		// Replace menubar content
		anchorComponent = document.getElementById( ContextId + "_dataMenuBar" )
		anchorComponent.innerHTML = HttpRequest.responseText;
		// Select menubar related component
		document.getElementById( ContextId + "_contextSensitive" ).onmouseover();
	}
}

var selectedComponentId = new Hashtable();
function selectComponent(ContextId, ComponentId) {
	selectedId = selectedComponentId.get(ContextId);
	if (selectedId != null) {
		// Clear current selected component 
		component = document.getElementById(selectedId);
		component.className = component.className.replace("-inverse", "");
		selectedComponentId.remove(ContextId);
	} 
	if (ComponentId != null) {
		// Select new component 
		component = document.getElementById(ComponentId);
		component.className = component.className + "-inverse";
		selectedComponentId.put(ContextId, component.id);
	} 
}

/*******************************************************************************************
* Class Definition: Hashtable
*******************************************************************************************/
Hashtable.prototype.hash     = null;
Hashtable.prototype.keys     = null;
Hashtable.prototype.location = null;

function Hashtable(){
	this.hash = new Array();
	this.keys = new Array();
	this.location = 0;
}
Hashtable.prototype.put = function (Key, Value){
    if (Value == null) {return;}
	if (this.hash[Key] == null) {this.keys[this.keys.length] = Key;}
	this.hash[Key] = Value;
}
Hashtable.prototype.get = function (Key){
	return this.hash[Key];
}
Hashtable.prototype.remove = function (Key){
	for (var i = 0; i < this.keys.length; i++){
		if (Key == this.keys[i]){
			this.hash[this.keys[i]] = null;
			this.keys.splice(i ,1);
			return;
		}
	}
}
Hashtable.prototype.size = function (){
    return this.keys.length;
}