function controlCapa(disparador, capa){
   // Hace que la "capa" aparezca / desaparezca al ponernos encima del "disparador"
   if (typeof disparador == 'undefined' || typeof capa == 'undefined'){
      return false;
   }
   this.capa=capa;
   this.mostrarCapa=function(){
      document.getElementById(capa).style.display='block';
   }
   this.ocultarCapa=function(){
      document.getElementById(capa).style.display='none';
   }
   document.getElementById(disparador).onmouseover=this.mostrarCapa;
   document.getElementById(disparador).onmouseout=this.ocultarCapa;
   return true;
}

var selectControl = {
   clear: function(id){
      // Borra todo el contenido de un select
      var select = id;
      if (typeof id == 'string'){
         select = document.getElementById(id);
      }
      for(var i=select.length; i > 0; i--){
         select.remove(i-1);
      }
   },

   add: function(id, valor, text){
      if (typeof valor == 'undefined'){ return false; }
      if (typeof text == 'undefined'){ text=valor; }
      // Aņade un valor al select con el id indicado
      var select = id;
      if (typeof id == 'string'){
         select = document.getElementById(id);
      }
      if(select == null){ return false; }
      var campo = document.createElement('option');
      campo.text = text;
      campo.value = valor;
      try {
         select.add(campo, null); // standards compliant; doesn't work in IE
      }
      catch(ex) {
         select.add(campo); // IE only
      }
      return true;
   },

   getValue: function(id){
      // Devuelve el actual valor de un select
      var select = id;
      if (typeof id == 'string'){
         select = document.getElementById(id);
      }
      if(select == null){ return false; }
      if(select.selectedIndex < 0) return false;
      return select.options[select.selectedIndex].value;
   },

   setValue: function(id, item){
      var select = id;
      if (typeof id == 'string'){
         select = document.getElementById(id);
      }
      if(select == null){ return false; }
      for(var i=0; i< select.length; i++){
         // alert(select.options[i].value+" == "+item);
         if(select.options[i].value == item){
            select.selectedIndex=i;
         }
      }
      return true;
   }
}

var elementUL = {
   clear: function(id){
      var ul = id;
      if (typeof id == 'string'){
         ul = $(id);
      }
      ul.select("li").each(function(item){
         item.remove();
      });
   },

   add: function(id, datos, idElement){
      var ul = id;
      if (typeof id == 'string'){
         ul = $(id);
      }

      if (typeof idElement == 'undefined') idElement=datos;

      var element = document.createElement("li");
      element.setAttribute("id", idElement);
      element.innerHTML=datos;
      ul.appendChild(element);
   }
}

function loadURL(frame, src, error, postFunction){
   http_request=null;
   if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      http_request = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) { // Internet Explorer
      http_request = new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (http_request == null){return false;}


   loadJS=function(codigo){
      /* Obtengo el javascript del nuevo documento y lo inserta */
      var mcodigo=codigo.toLowerCase();
      var begin=mcodigo.indexOf("<script");
      while(begin != -1){
         // Carga un script del codigo
         var beginC=mcodigo.indexOf(">", begin)+1;
         var endC=mcodigo.indexOf("</script", begin);
         var end=mcodigo.indexOf(">", endC)+1;

         // Carga el script
         var script=document.createElement("script");
         script.type = 'text/javascript';

         if(mcodigo.substring(begin, beginC).indexOf('src=\'') != -1){
            var beginS=mcodigo.indexOf("\'", begin)+1;
            var endS=mcodigo.indexOf("\'", beginS);
            script.src=codigo.substring(beginS, endS);
         }
         if (mcodigo.substring(begin, beginC).indexOf('src=\"') != -1){
            var beginS=mcodigo.indexOf("\"", begin)+1;
            var endS=mcodigo.indexOf("\"", beginS);
            script.src=codigo.substring(beginS, endS);
         }

         // Carga por codigo
         script.text=codigo.substring(beginC, endC);
         document.getElementsByTagName("head")[0].appendChild(script);
         // Elimina el script del codigo
         codigo=codigo.replace(codigo.substring(begin, end), "");
         mcodigo=mcodigo.replace(mcodigo.substring(begin, end), "");

         var begin=mcodigo.indexOf("<script");
      }
      return codigo;
   }

   loadCSS=function(codigo){
      /* Obtengo los css del nuevo documento y los inserta */
      var mcodigo=codigo.toLowerCase();
      var begin=mcodigo.indexOf("<style");
      while(begin != -1){
         // Carga un script del codigo
         var beginC=mcodigo.indexOf(">", begin)+1;
         var endC=mcodigo.indexOf("</style", begin);
         var end=mcodigo.indexOf(">", endC)+1;

         // Carga por codigo
         if (navigator.userAgent.indexOf("MSIE") >= 0){
            var listaCSS=codigo.substring(beginC, endC).split("}");
            for(var i=0; i < listaCSS.length-1; i++){
               // alert(listaCSS[i]);
               var contenidoCSS=listaCSS[i].substring(beginC, endC).split("{");
               var ultimaEtiquetaStyle=document.styleSheets[document.styleSheets.length-1];
               var cabeceras=contenidoCSS[0].split(",");
               for(var j=0; j < cabeceras.length; j++){
                  ultimaEtiquetaStyle.addRule(cabeceras[j], contenidoCSS[j]);
               }
            }
         } else {
            var contenidoCSS=document.createTextNode(codigo.substring(beginC, endC));
            style.appendChild(contenidoCSS);
         }

         // Elimina el script del codigo
         codigo=codigo.replace(codigo.substring(begin, end), "");
         mcodigo=mcodigo.replace(mcodigo.substring(begin, end), "");

         var begin=mcodigo.indexOf("<style");
      }
      return codigo;
   }

   loadLink=function(codigo){
      /* Obtengo los css del nuevo documento y los inserta */
      var mcodigo=codigo.toLowerCase();
      var begin=mcodigo.indexOf("<link");
      while(begin != -1){
         // Carga un script del codigo
         var end=mcodigo.indexOf(">", begin)+1;
         var clink=mcodigo.substring(begin, end);
         var clinkn=codigo.substring(begin, end);
         var beginC=clink.indexOf('href');
         if (clink.indexOf("\"", beginC) != -1){
            beginC=clink.indexOf("\"", beginC)+1;
            endC=clink.indexOf("\"", beginC);
         } else {
            beginC=clink.indexOf("\'", beginC)+1;
            endC=clink.indexOf("\'", beginC);
         }

         // Crea el nuevo css
         objCSS = document.createElement('link');
         objCSS = document.createElement('link');
         objCSS.rel = 'stylesheet';
         objCSS.href = clinkn.substring(beginC, endC);
         objCSS.type = 'text/css';
         document.body.appendChild(objCSS);

         // Elimina el script del codigo
         codigo=codigo.replace(codigo.substring(begin, end), "");
         mcodigo=mcodigo.replace(mcodigo.substring(begin, end), "");

         var begin=mcodigo.indexOf("<link");
      }
      return codigo;
   }

   http_request.onreadystatechange = function(){
      if(http_request.readyState == 4) {
         if (http_request.status == 200) {
            var codigo=loadJS(http_request.responseText);
            codigo=loadCSS(codigo);
            codigo=loadLink(codigo);
            $(frame).innerHTML=codigo;
            if (typeof postFunction != 'undefined' && postFunction != null){
               postFunction();
            }
         }
         if(http_request.status == 404 && (typeof error != 'undefined' && error != null)){
            loadURL(frame, error, null, postFunction);
         }
      }
   };

   // Separa la llamada de sus argumentos
   var tupla=src.split('?');
   var url;
   var args;
   if (tupla.length == 1){
      url=tupla[0];
      args="?id=1";
   } else {
      url=tupla[0];
      args=tupla[1];
   }

   // Realizar peticion HTTP
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   http_request.send(args);

}

function setCookie(c_name,value,expiredays){
   /* Graba una cookie */
   var exdate=new Date();
   exdate.setDate(exdate.getDate()+expiredays);
   document.cookie=c_name+ "=" +escape(value)+
   ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name){
   /* Carga una cookie */
   if (document.cookie.length>0)
     {
     c_start=document.cookie.indexOf(c_name + "=");
     if (c_start!=-1)
       {
       c_start=c_start + c_name.length+1;
       c_end=document.cookie.indexOf(";",c_start);
       if (c_end==-1) c_end=document.cookie.length;
       return unescape(document.cookie.substring(c_start,c_end));
       }
     }
   return "";
}

function checkCookie(c_name){
   /* Comprueba si existe una cookie */
   var cookie=getCookie(c_name);
   if (cookie != null && cookie != "") { return true; }
   return false;
}

function isOver(obj, mousePosX, mousePosY, border){
   var pos=Position.cumulativeOffset(obj);
   var dimT=obj.getDimensions();
   if (typeof border == 'undefined'){
      border=0;
   }
   border++;

   dim=new Array();
   dim[0]=pos[0] + parseFloat($H(dimT).get('width')) - border;
   dim[1]=pos[1] + parseFloat($H(dimT).get('height')) - border;
   pos[0]=pos[0]+border;
   pos[1]=pos[1]+border;

   if (mousePosX < pos[0] || mousePosX > dim[0] || mousePosY < pos[1] || mousePosY > dim[1]) return false;
   return true;
}

function number_format(numero, ndecimales, sepDecimal, sepMiles){
   decimales=function (){
      numero=numero.toString();
      var tupla=numero.split('.');
      numero=parseFloat(numero).toPrecision(tupla[0].length+parseFloat(ndecimales));
      tupla=numero.toString().split('.');
      return tupla[0]+sepDecimal+tupla[1];
   }

   puntua=function(numero){
      // return numero;
      if (sepMiles == '') { return numero; }
      var tupla=numero.split(sepDecimal);
      var entero=tupla[0];
      var rgx = /(\d+)(\d{3})/;
      while (rgx.test(entero)) {
         entero = entero.replace(rgx, '$1' + sepMiles + '$2');
      }
      return entero+sepDecimal+tupla[1];
   }

   numero=decimales();
   numero=puntua(numero);
   return numero;
}

function Wait(name){
   this.open=function(texto){
      if (typeof texto == 'undefined'){ var texto = "Cargando"; }
      if (this.counter == 0){
         var html="<div style=\""+
                  "font-family:Arial, Helvetica, sans-serif; "+
                  "font-size: 14px; font-weight: bold; text-align: center; "+
                  "color: blue; margin-bottom: 5px;"+
                  "white-space:pre;\">"+texto+"</div>";
         this.win=Dialog.info(html, {width:200, height:60, showProgress: true, showEffect: Element.show, hideEffect: Element.hide});
         setTimeout(this.name+".isOpen=true;", 500); // Evita que IE se quede colgado
      }
      this.counter++;
   }

   this.close=function(){
      if (! this.isOpen){ setTimeout(this.name+".close()", 500); return false;}

      if (this.win == null) return false;
      this.counter--;
      if (this.counter <= 0){
         this.win.close();
         this.counter=0;
         this.dia=null;
         this.isOpen=false;
      }
   }

   this.closeAll=function(){
      if (! this.isOpen){ setTimeout(this.name+".closeAll()", 500); return false;}

      if (this.win == null) return false;
      this.counter=0;
      this.win.close();
      this.dia=null;
      this.isOpen=false;
   }

   this.name=name;
   this.counter=0;
   this.win=null;
   this.isOpen=false;
}
var wait=new Wait('wait');

function addEnter(id, func){
   if (typeof id == 'string'){
      id = $(id);
   }
   Event.observe(id, 'keypress', function(ev){ if(ev.keyCode == 13) func(); });
}

function include(jsFile, condition, postFunction) {
	var html_doc = document.getElementsByTagName('head').item(0);
	var js=window.document.createElement('script');
	js.setAttribute('src',jsFile);
	js.setAttribute('language', 'javascript');
   js.setAttribute('type', 'text/javascript');
	html_doc.appendChild(js);

	if (typeof postFunction != 'undefined', typeof condition != 'undefined'){
		new PeriodicalExecuter(function(obj){
			if (eval('typeof ' + condition) != 'undefined'){
				obj.stop();
				postFunction();
			}
		}, 0.5);
	}
}

function fade(img, type, fun){
   var increment=(!Prototype.Browser.IE ? 0.05 : 0.4);
   if (type == 'in'){
      img.setOpacity(0);
      new PeriodicalExecuter(function(obj){
         var value=(img.getOpacity())+increment;
         img.setOpacity(value);
         if (value >= 1) {
            obj.stop();
            if (typeof fun != 'undefined') fun();
         }
      }, 0.005);
   }
   if (type == 'out'){
      img.setOpacity(1);
      new PeriodicalExecuter(function(obj){
         var value=(img.getOpacity())-increment;
         img.setOpacity(value);
         if (value <= 0) {
            obj.stop();
            if (typeof fun != 'undefined') fun();
         }
      }, 0.005);
   }
}

function atenuar(item, action){
	if (typeof action == 'undefined' || action == true){
		item.setOpacity(0.6);
		Event.observe(item, 'mouseover', function(){
			item.setOpacity(1);
		});
		Event.observe(item, 'mouseout', function(){
			item.setOpacity(0.6);
		});
	} else {
		Event.stopObserving(item, 'mouseover');
		Event.stopObserving(item, 'mouseout');
		item.setOpacity(1);
	}
}

function shadow(id, margin, color, opacity){
   if (typeof margin == 'undefined') margin=5;
   if (typeof opacity == 'undefined') opacity=0.50;
   if (typeof color == 'undefined') color='#665F5F';
   if (typeof id == 'string') id=$(id);
   var sh=new Element('div', { 'id': id+'_shadow', 'style': 'position: absolute; background-color: '+color+'; margin: '+margin+'px 0px 0px '+margin+'px; z-index: 0'});
   Element.insert(id, { after: sh});
   id.setStyle('position: relative; z-index: 1');
   Element.clonePosition(sh, id);
   sh.setOpacity(opacity);
   Event.observe(window, 'load', function(){
      Element.clonePosition(sh, id);
      sh.setOpacity(opacity);
   });
}

