	// **************************** //
	// MENU			     		    //
	// **************************** //

	function MenuOption(text, suboptions, action, urlImg, bgcolor, fgcolor){
		this.suboptions = suboptions;
		this.text = text;
		this.urlImg = urlImg;
		this.action = action;
		this.parent = null;
		this.optSelected = null;
		this.objOption = null;
		this.level = null;
		this.objSuboptions = null;
		this.objFocus = null;
		this.tdText = null;
				
		this.bgcolor = bgcolor;
		this.fgcolor = fgcolor;
		
		this.deployed = false;
	}
	MenuOption.prototype.isDeployted = function(){return this.deployed;}
	MenuOption.prototype.getText = function(){return this.text;}
	MenuOption.prototype.setObjFocus = function(objFocus){this.objFocus = objFocus;}
	MenuOption.prototype.selectOption = function(optSel){
		if(this.optSelected != null && this.optSel != optSel) this.optSelected.unDeploy();
		this.optSelected = optSel;
	}
	MenuOption.prototype.deploy = function(){
		if (!this.deployed){
			this.parent.selectOption(this);
			if(this.suboptions != null) this.objSuboptions.style.display = "block";
			this.highlight();
			if(this.level == 1) this.objFocus.focus();
			
			this.deployed = true;
		}
	}
	MenuOption.prototype.unDeploy = function(){
		if (this.deployed){
			if(this.optSelected != null) this.optSelected.unDeploy();
			if(this.suboptions  != null) this.objSuboptions.style.display = "none";
			this.optSelected == null;
			this.unHighlight();
			
			this.deployed = false;
		}
	}
	MenuOption.prototype.highlight = function(){this.objOption.className = "optionover"  + this.level; if (this.level != 1) this.objOption.style.backgroundColor = "#6F6F6F";}
	MenuOption.prototype.unHighlight = function(){this.objOption.className = "option"  + this.level;  if (this.level != 1) this.objOption.style.backgroundColor = ((this.bgcolor == null)? "#000000" : this.bgcolor);}	
	MenuOption.prototype.show = function(){this.objOption.style.display = "";}
	MenuOption.prototype.hide = function(){this.objOption.style.display = "none";}
	MenuOption.prototype.setAction = function(action){this.action = action;}
	MenuOption.prototype.create = function(target1, target2, parent, level){

		this.parent = parent;
		this.level = level;

		var table = document.createElement("table");
		table.cellPadding = "0";
		table.cellSpacing = "0";
		table.className = "option" + this.level;
		
		if (this.level != 1) table.style.backgroundColor = ((this.bgcolor == null)? "#000000" : this.bgcolor);
		
		target1.appendChild(table);

		var option = this;
		table.onmouseover = function () {option.deploy();}

		this.objOption = table;

		var tbody = document.createElement("tbody");
		table.appendChild(tbody);

		var tr = document.createElement("tr");
		tbody.appendChild(tr);

		var td = document.createElement("td");
		td.noWrap = "nowrap";
		td.style.paddingLeft = "2px";
		td.style.paddingRight = "4px"
		tr.appendChild(td);
		
		if (this.urlImg != null) {
			var img = document.createElement("img");
			img.src = this.urlImg;
			img.style.verticalAlign = "middle";
			img.style.marginRight = "5px";
			td.appendChild(img);
		}
		
		if (this.level != 1) td.style.color = ((this.fgcolor == null)? "#FFFFFF" : this.fgcolor);
				
		var span = document.createElement("span");
		span.innerHTML = this.text;
		td.appendChild(span);

		if (this.level != 1) {
			td = document.createElement("td");
			td.className = "menuchilds";
			td.innerHTML = ((this.suboptions != null)? "&nbsp;&nbsp;&raquo;" : "&nbsp;");
			this.tdText = td;
			tr.appendChild(td);
		}

		if (this.action != null) target1.onclick = function() {option.action();}

		var table = document.createElement("table");
		table.className = "suboptions";
		table.cellPadding = "0";
		table.cellSpacing = "0";
		table.style.backgroundColor = ((this.bgcolor == null)? "#000000" : this.bgcolor);
		table.style.border = "1px solid "+ ((this.fgcolor == null)? "#FFFFFF" : this.fgcolor);

		opacity(table, 9.5);
		table.style.boxShadow = "3px 2px 2px #555555";

		//if (this.level > 1) target1.onmouseout = function () {option.unDeploy();}
		
		//table.style.zIndex = 50 + this.level;
		this.objSuboptions = table;
		target2.appendChild(table);

		var tbody = document.createElement("tbody");
		table.appendChild(tbody);

		if (this.suboptions != null) {

			for (var i = 0; i < this.suboptions.length; i++) {
				tr = document.createElement("tr");
				tbody.appendChild(tr);

				var td1 = document.createElement("td");
				tr.appendChild(td1);

				var td2 = document.createElement("td");
				td2.style.verticalAlign = "top";
				tr.appendChild(td2);

				this.suboptions[i].create(td1, td2, this, this.level + 1);
			}
		}
	}

	function Menu(options, target){

		this.options = options;
		this.optSelected = null;
		this.objFocus = null;
		this.clock = null;

		var table = document.createElement("table");
		table.cellPadding = "0";
		table.cellSpacing = "0";
		table.className = "menu";

		var tbody = document.createElement("tbody");
		table.appendChild(tbody);

		var tr = document.createElement("tr");
		tbody.appendChild(tr);

		var td = document.createElement("td");
		tr.appendChild(td);

		this.objFocus = document.createElement("input");
		this.objFocus.menu = this;
		this.objFocus.className = "objfocus";
		td.appendChild(this.objFocus);

		addEvent(this.objFocus, "onblur", "this.menu.initUnDeploy();");

		var td1 = new Array();

		for(var i = 0; i < this.options.length; i++){
			var td =  document.createElement("td");
			tr.appendChild(td);

			td1.push(td);

			td = document.createElement("td");
			td.className = "menusep";
			tr.appendChild(td);
		}

		var tr = document.createElement("tr");
		tbody.appendChild(tr);

		for (var i = 0; i < this.options.length; i++) {
			var td = document.createElement("td");
			tr.appendChild(td);

			this.options[i].create(td1[i], td, this, 1);
			this.options[i].setObjFocus(this.objFocus);

			td = document.createElement("td");
			tr.appendChild(td);
		}

		target.appendChild(table);
	}
	Menu.prototype.selectOption = function(optSel){
		if(this.optSelected != null && this.optSel != optSel) this.optSelected.unDeploy();
		this.optSelected = optSel;
	}
	Menu.prototype.initUnDeploy = function(){
		var menu = this;
		this.clock = setTimeout(function () {menu.unDeploy();}, 250);
	}
	Menu.prototype.getSuboption = function(idSuboption){
		var menu = this;
		for(var i = 0; i < menu.options.length; i++){
			for(var j = 0; j < menu.options[i].suboptions.length; j++){
				if(menu.options[i].suboptions[j].text == idSuboption) return menu.options[i].suboptions[j];
			}
		}
		return null;
	}
	Menu.prototype.unDeploy = function(){
		clearTimeout(this.clock);
		if(this.optSelected != null) this.optSelected.unDeploy();
		this.optSelected = null;
	}

	
	//**************************//
	// CLASE TITLEBAR			//
	//**************************//

	function TitleBar(target, separator){
		this.separator = ((separator != null)? separator : " / ");
		this.target = target;
	}
	TitleBar.prototype.init = function(){this.target.innerHTML = "";}
	TitleBar.prototype.set = function(texts, actions){

		this.init();

		for(var i = 0; i < texts.length; i++){

			var span = document.createElement("span");
			span.innerHTML = texts[i];

			if(i != (texts.length - 1)){
				span.innerHTML += this.separator;;
				span.className = "tibtext";
			} else {
				span.className = "tibpage";
			}

			this.target.appendChild(span);
		}
	}
	TitleBar.prototype.hide = function(){ if (this.target != null) this.target.style.display = "none"; }
	TitleBar.prototype.show = function(){ if (this.target != null) this.target.style.display = ""; }


	//**************************//
	// CLASE WINDOW				//
	//**************************//

	function Window(id, windowUrl, servletUrl, width, height, popup, modal, report, ssl, frames, forceDownload){

		this.id = id;
		this.windowUrl = windowUrl;
		this.servletUrl = servletUrl;
		this.width = width;
		this.height = height;
		this.popup = popup;
		this.modal = modal;
		this.report = report;
		this.frames = frames;
		this.forceDownload = (forceDownload != null && forceDownload)? forceDownload : false;
		this.ssl = (ssl != null) ? ssl : false;

		this.ifwin = null;

		this.downloaded = false;
		this.loadComplete = false;
	}
	Window.prototype.getId = function(){return this.id;}
	Window.prototype.getWindowUrl = function(){return this.windowUrl;}
	Window.prototype.getServletUrl = function(){return this.servletUrl;}
	Window.prototype.getWidth = function(){return this.width;}
	Window.prototype.setWidth = function(width){
		this.width = width;
		if (this.modal){
			getTop().document.getElementById("tPUBase").width = this.width;
			getTop().document.getElementById("tPopup").style.width = this.width +"px";			
		}
	}
	Window.prototype.isPopup = function(){return this.popup;}
	Window.prototype.isModal = function(){return this.modal;}
	Window.prototype.isReport = function(){return this.report;}
	Window.prototype.isSSL = function(){return this.ssl;}
	Window.prototype.getHeight = function(){return this.height;}
	Window.prototype.setHeight = function(height){
		this.height = height;
		if (this.modal){
			getTop().document.getElementById("tPUBase").height = this.height;
			getTop().document.getElementById("tPopup").style.height = this.height +"px";
		}
	}
	Window.prototype.getWindowContent = function(){return this.ifwin.contentWindow;}
	Window.prototype.setLoadComplete = function(loadComplete){
		this.loadComplete = loadComplete;
		if (this.loadComplete && !(this.popup && !this.modal)) getMngWindow().checkWinLoadComplete(this);
	}
	Window.prototype.isLoadComplete = function(){return this.loadComplete;}
	Window.prototype.setDownloaded = function(downloaded){
		this.downloaded = downloaded;
		if (this.downloaded && !(this.popup && !this.modal)) getMngWindow().checkWinDownloaded(this);
	}
	Window.prototype.isDownloaded = function(){return this.downloaded;}
	
	Window.prototype.getIFrame = function(){return this.ifwin;}
	Window.prototype.hide = function(){
		this.ifwin.style.display = "none";
		if (this.modal) getTop().document.getElementById("popup").style.display = "none";
	}
	Window.prototype.show = function(){
		this.ifwin.style.display = "block";
		if (this.modal) {
			var puBase = getTop().document.getElementById("tPUBase");
			
			if(this.frames != null && this.frames) 
				puBase.setAttribute("scrolling", "yes");
			else 
				puBase.setAttribute("scrolling", "no");
			
			puBase.width = this.width;
			puBase.height = this.height;
			var tablePopup = getTop().document.getElementById("tPopup");
			tablePopup.style.width = this.width +"px";
			tablePopup.style.height = this.height +"px";
			getTop().document.getElementById("popup").style.display = "";
		}
	}
	Window.prototype.init = function(params){
		this.ifwin.contentWindow.init(params);
	}
	Window.prototype.download = function(url){

		var ifwin = document.createElement("iframe");
		ifwin.id = this.idWindow;
		ifwin.src = (url != null)? url : this.windowUrl;
		ifwin.frameBorder = "0";
		ifwin.marginHeight = "0px";
		ifwin.marginWidth = "0px";

		// ifwin.scrolling = ((isIE)? "yes" : "auto");
		ifwin.scrolling = "auto";
		ifwin.style.display = "none";
		this.ifwin = ifwin;

		var target;
		if (this.modal) {
			target = getTop().document.getElementById("puContent");
		} else {
			target = getTop().document.getElementById("windowArea");
			if (isIE) ifwin.allowTransparency = "true";
		}

		ifwin.width = "100%";
		ifwin.height = "100%";

		target.appendChild(ifwin);
	}


	//**************************//
	// CLASE WINDOWCACHE		//
	//**************************//

	function WindowCache(){

		this.winCacheList = new Array();
		this.winCacheSize = 15

		this.getIndex = function getIndex(win){
			for(var i = 0; i < this.winCacheList.length; i++){
				if(this.winCacheList[i] == win) return i;
			}
			return null;
		}
	}
	WindowCache.prototype.put = function(win){
		// Si ya esta en la cache de ventanas
		var i = this.getIndex(win);
		if(i != null) this.winCacheList.splice(i, 1);

		// Si la cache esta llena
		var winOut = null;
		if(this.winCacheList.length == this.winCacheSize) winOut = this.winCacheList.shift();

		// Insertamos al final la ventana
		this.winCacheList.push(win);

		// Devolvemos la ventana que ha salido de la cache
		return(winOut);
	}
	WindowCache.prototype.getList = function(){return this.winCacheList;}
	WindowCache.prototype.isInCache = function(win){return((this.getIndex(win) != null) ? true : false);}
	WindowCache.prototype.clear = function(){this.winCacheList = new Array();}
	WindowCache.prototype.getPenultimate = function(){
		if(this.winCacheList.length < 2) return null;
		else return this.winCacheList[this.winCacheList.length - 2];
	}


	//**************************//
	// CLASE GESTOR VENTANAS	//
	//**************************//

	function showPopup(url, w, h, name, scrollB, menuB) {
 		var options = "width=" + w + ",height=" + h + ",";
		options += "resizable=yes, status=yes, ";
		options += "toolbar=no, location=no, directories=no, ";
		options += (scrollB == true)? "scrollbars=yes" : "scrollbars=auto";
		options += (menuB == true)? " ,menubar=yes" : " ,menubar=no";

		ejeX = (screen.width - w) / 2;
    	ejeY = (screen.height - h) / 2;
		options += ",top=" + ejeY + ",left=" + ejeX;

		var newWin = window.open(url, name, options);
    	return newWin;
	}

	function MngWindow(winList){
		this.winCache = new WindowCache();
		this.winList = winList;
		this.currentWindow = null;
		this.goWindow = null;
		this.goParams = null;
		this.withParams = false;
		this.scrollB = "auto";
		this.menuB = "no";

	}
	MngWindow.prototype.isWithParams = function(){return this.withParams;}
	MngWindow.prototype.setWithParams = function(value){this.withParams = value;}
	MngWindow.prototype.refuseContinue = function(){this.goWindow = null; this.goParams = null;}
	MngWindow.prototype.getCurrentWindow = function(){return this.currentWindow;}
	MngWindow.prototype.getWindow = function(idWindow){return this.winList[idWindow];}
	MngWindow.prototype.setTitlePU = function(text){getTop().document.getElementById("puTitle").innerHTML = text;}
	MngWindow.prototype.closeCurrentWindow = function closeCurrentWindow(){
		var win = this.winCache.getPenultimate();
		if (win != null) {
			try {
				if (this.currentWindow != null && this.currentWindow.getWindowContent().onClosePopup != null && this.currentWindow.isPopup()){
					this.currentWindow.getWindowContent().onClosePopup();
				} else {
					this.currentWindow.hide();
					this.currentWindow = this.getWindow(win);
					this.currentWindow.show();
					this.winCache.put(win);
				}
			} catch (e) {
				this.currentWindow.hide();
				this.currentWindow = this.getWindow(win);
				this.currentWindow.show();
				this.winCache.put(win);
				return;
			}
		}
	}
	MngWindow.prototype.clearCache = function(){
		this.winCache = new WindowCache();
	}
	MngWindow.prototype.showPopup = function(url, w, h, scrollB, menuB) {return showPopup(url, w, h, (this.goWindow != null) ? this.goWindow.getId() : "Valadis", (scrollB == null) ? this.scrollB: scrollB, (menuB==null) ? this.menuB : menuB);}
	MngWindow.prototype.openReport = function(url, w, h){
		var encodedURL = window.escape(url);
		var urlPopup = getTop().reportURL + encodedURL;
		return this.showPopup(urlPopup, w, h);
	}
	MngWindow.prototype.showWindow = function(idWindow, params, scrollB, menuB) {
			this.goWindow = this.getWindow(idWindow);
			this.goParams = params;
			if (scrollB != null) this.scrollB = scrollB;
			if (menuB != null) this.menuB = menuB;
			if (this.goWindow != null) {
				if (this.currentWindow != null && this.currentWindow.getWindowContent().onClose != null && !this.goWindow.isModal()){
					this.currentWindow.getWindowContent().onClose();
				} else {
					this.allowContinue();
				}

			} else {
				throw "El identificador de ventana " + this.idWindow + " no existe en la lista de paginas";
			}
	}
	MngWindow.prototype.allowContinue = function() {

		if (this.goWindow != null) {

			if (this.goParams == null) this.goParams = new Params();
			if (this.goParams.getParamValue("action") == null)  this.goParams.put("action", "load");
			
			if (this.goWindow.isPopup() && !this.goWindow.isModal()){
				if (this.goWindow.isReport()) this.openReport(this.goWindow.getWindowUrl() + this.goParams.toString(), this.goWindow.getWidth(), this.goWindow.getHeight());
				else this.showPopup(this.goWindow.getWindowUrl() + this.goParams.toString(), this.goWindow.getWidth(), this.goWindow.getHeight(), this.scrollB, this.menuB);

			} else {

				showProcessing();

				var isInCache = this.winCache.isInCache(this.goWindow.getId()) && !this.goWindow.forceDownload;
				var winOut = this.winCache.put(this.goWindow.getId());
				if (winOut != null) deleteElement(this.getWindow(winOut).getIFrame());

				if (this.goWindow.isSSL()) {
					this.setTitlePU("&nbsp;");
					this.goWindow.download(this.goWindow.getWindowUrl() + this.goParams.toString());
					this.currentWindow = this.goWindow;
					this.goWindow.show();
					this.refuseContinue();
					hideProcessing();
				} else {
					if (this.isWithParams()){
						 this.goWindow.download(this.goWindow.getServletUrl() + this.goParams.toString());
						 this.setWithParams(false);
					} else if (isInCache){
						this.waitLoadComplete();
					} else {
						 this.goWindow.download();
					}
				}
			}
		}
	}
	MngWindow.prototype.checkWinDownloaded = function(win){
		if (this.goWindow == win) 
			this.waitLoadComplete();
		else if(this.goWindow != null) 
			throw "La ventana que ha acabado la descarga " + win.id  + " es distinta que la que esperaba el gestor " + this.goWindow.id;
	}
	MngWindow.prototype.waitLoadComplete = function(){
		this.goWindow.init(this.goParams);
	}
	MngWindow.prototype.checkWinLoadComplete = function(win){
		if (this.goWindow != null && this.goWindow == win) {
			if (this.currentWindow != null && !this.goWindow.isModal()) this.hideAll();
			this.currentWindow = this.goWindow;
			this.goWindow.show();
			this.refuseContinue();
			hideProcessing();
			this.goWindow = null;
		}
	}
	MngWindow.prototype.hideAll = function() {
		for (var i = 0; i < this.winCache.getList().length; i++) {
			this.getWindow(this.winCache.getList()[i]).hide();
		}
	}

	
	//*********************//
	// CLASE USERCONFIG	   //
	//*********************//
	
	function UserConfig(formatDate, currency, currencyActor, numberFormat, maxResults, lang){
		this.formatDate = (formatDate == "ENG" || formatDate == "ESP")? formatDate : 
						  (RegExp("^MM/dd/yyyy$").test(formatDate))? "ENG" : "ESP";
		this.currency = currency;
		this.currencyActor = currencyActor;
		this.numberFormat = (numberFormat == "CD" || numberFormat == "DC")? numberFormat : (numberFormat == 2) ? "CD" : "DC";
		this.maxResults = (maxResults != "") ? maxResults : 15;
		this.lang = lang;
	}
	UserConfig.prototype.getFormatDate = function(){return this.formatDate;}
	UserConfig.prototype.setFormatDate = function(formatDate){this.formatDate = formatDate;}
	UserConfig.prototype.getCurrency = function(){return this.currency;}
	UserConfig.prototype.setCurrency = function(currency){this.currency = currency;}
	UserConfig.prototype.getCurrencyActor = function(){return this.currencyActor;}
	UserConfig.prototype.setCurrencyActor = function(currencyActor){this.currencyActor = currencyActor;}
	UserConfig.prototype.getNumberFormat = function(){return this.numberFormat;}
	UserConfig.prototype.setNumberFormat = function(numberFormat){this.numberFormat = numberFormat;}
	UserConfig.prototype.getMaxResults = function(){return this.maxResults;}
	UserConfig.prototype.setMaxResults = function(maxResults){this.maxResults = maxResults;}
	UserConfig.prototype.getLang = function(lang){return this.lang;}

	
	//*********************//
	// GENERALES		   //
	//*********************//

	function refreshHour(){
		var today = new Date();
		var h = today.getHours();
		var m = today.getMinutes();
		h = h < 10 ? new String("0" + h) : h;
		m = m < 10 ? new String("0" + m) : m;
		document.getElementById("headHour").innerHTML = h + ":" + m;
	}
	

