//******************************************************************
// Detección del Navegador
//******************************************************************
function lib_bwcheck(){ 
	this.ver=navigator.appVersion;
	this.agent=navigator.userAgent;
	this.dom=document.getElementById?1:0;
	this.opera5=(navigator.userAgent.indexOf("Opera")>-1 && this.dom)?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6;
	this.mac=this.agent.indexOf("Mac")>-1;
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
	return this;
}
var bw=new lib_bwcheck();


//****************************************************************
//Funciones generales
//****************************************************************
function AbreVentana(tipo,valor,ancho,alto, arriba, izquierda) {
 	switch(tipo) {
 		case "A":
			winprops = "height="+alto+",width="+ancho+",top=" + arriba + ",left=" + izquierda + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no";
			win = window.open(valor,'Login',winprops);
			win.opener = window;
			win.focus();
			break;
 		case "B":
			winprops = "height="+alto+",width="+ancho+",top=" + arriba + ",left=" + izquierda + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no";
			win = window.open(valor,'upload',winprops);
			win.opener = window;
			win.focus();
			break;
   		defalut:
			alert("Error en la aplicacion,");
  	}   
}
function SubmitEnter(oEvento, accion){
     var iAscii;
     if (oEvento.keyCode)
         iAscii = oEvento.keyCode;
     else if (oEvento.which)
         iAscii = oEvento.which;
     else
         return false;
         
     if (iAscii == 13) eval(accion);
     
     return true;
} 
//****************************************************************
//AÑADIR A FAVORITOS
//****************************************************************
function agregar_favoritos(){
    if (bw.ie) {
        window.external.AddFavorite(self.location.href,window.title);
    } else if(bw.ns6 || bw.ns4) {
        alert("Presione Crtl+D para agregar este sitio en sus Bookmarks"); 
    } else {
        alert("Esta opción no esta disponible para su navegador");
    }
}
//******************************************************************
//FUNCIONES DE CONTROL DEL SCROLL
//****************************************************************
var speed = 50;
var loop, timer;
var px = bw.ns4||window.opera?"":"px";
var scrolltextLoaded = false;

function makeObj(obj,nest){
    nest=(!nest) ? "":'document.'+nest+'.';
	this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0;
  	this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0;
	this.scrollHeight=bw.ns4?this.css.document.height:this.el.offsetHeight;
	this.clipHeight=bw.ns4?this.css.clip.height:this.el.offsetHeight;
	this.up=goUp;this.down=goDown;
	this.moveIt=moveIt; this.x=0; this.y=0;
    this.obj = obj + "Object";
    eval(this.obj + "=this");
    return this;
}

function moveIt(x,y){
	this.x = x;
	this.y = y;
	this.css.left = this.x+px;
	this.css.top = this.y+px;
}

function goDown(move){
	if (this.y>-this.scrollHeight+oCont.clipHeight){
		this.moveIt(0,this.y-move);
		if (loop) setTimeout(this.obj+".down("+move+")",speed);
	}
}

function goUp(move){
	if (this.y<0){
		this.moveIt(0,this.y-move);
		if (loop) setTimeout(this.obj+".up("+move+")",speed);
	}
}

function scroll(speed){
	if (scrolltextLoaded){
		loop = true;
		if (speed>0) oScroll.down(speed);
		else oScroll.up(speed);
	}
}

function noScroll(){
	loop = false;
	if (timer) clearTimeout(timer);
}

function scrolltextInit(cont,txt){
	oCont = new makeObj(cont);
	oScroll = new makeObj(txt,cont);
	oScroll.moveIt(0,0);
	oCont.css.visibility = "visible";
	scrolltextLoaded = true;
}
//**************************************************************************
//Funciones de rollover
//**************************************************************************
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//***************************************************************************
//Funciones generales de control de capas
//***************************************************************************
var tiempo_in;

function BorrarCapa() {
	var a = BorrarCapa.arguments;
	var i;
	for (i=0; i<a.length; i++) {
		if (bw.ie) document.getElementById(a[i]).filters.alpha.opacity = 0;
		if(bw.ns6) document.getElementById(a[i]).style.MozOpacity = 0;
    	document.getElementById(a[i]).style.visibility="hidden";
	}
}

function fadeIn(elemento, inicio, fin) {
	document.getElementById(elemento).style.visibility = "visible";
	clearTimeout(tiempo_in);
	if(inicio < fin){
		inicio+=5;
		if (bw.ie) document.getElementById(elemento).filters.alpha.opacity = inicio;
		if(bw.ns6) document.getElementById(elemento).style.MozOpacity = inicio/100;
		tiempo_in=setTimeout("fadeIn('"+ elemento + "',"+ inicio + ","+ fin + ")", 0);
	}
}

function fadeOut(elemento,inicio, fin) {
	if(inicio > fin){
		inicio-=5;
		if (bw.ie) document.getElementById(elemento).filters.alpha.opacity = inicio;
		if(bw.ns6) document.getElementById(elemento).style.MozOpacity = inicio/100;
		setTimeout("fadeOut('"+ elemento + "',"+ inicio + "," + fin + ")", 0);
	} else {
		document.getElementById(elemento).style.visibility = "hidden";
	}
}
function AbreVentana(tipo,valor,ancho,alto, arriba, izquierda) {
 	switch(tipo) {
 		case "A":
			winprops = "height="+alto+",width="+ancho+",top=" + arriba + ",left=" + izquierda + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no";
			win = window.open(valor,'Login',winprops);
			win.opener = window;
			win.focus();
			break;
 		case "B":
			winprops = "height="+alto+",width="+ancho+",top=" + arriba + ",left=" + izquierda + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no";
			win = window.open(valor,'upload',winprops);
			win.opener = window;
			win.focus();
			break;
   		defalut:
			alert("Error en la aplicacion,");
  	}   
}
//***********************************************************************************
// Funciones de espeficicas control de combo
//***********************************************************************************
function annadirEdicion(combo) {
	if (tree._selected._parent != tree._root){
		annadirItem(combo,tree._selected._parent.caption + " / " + tree._selected.caption, tree._selected.id2);
	} else {	
		alert("Solo puede seleccionar ediciones");
	}
}

//***********************************************************************************
// Funciones de generales control de combo
//***********************************************************************************
function annadirItem(combo,texto,valor){
  var usadas=contarItems(combo);
  if ((combo.length-usadas)>0) {
    if (!existeItem(combo,valor)){
       combo[usadas].text=texto;
       combo[usadas].value=valor;
       usadas++;
    } else {
      alert ("El elemento indicado ya esta en la combo");
    } 
  } else
    alert ("No se pueden insertar más elementos en la combo");
}

function marcarCombo(combo) { 
    for (i=0;i<combo.length ;i++ ) {  
        if (combo[i].value!="")
            combo[i].selected=true;
		else
		 combo[i].selected=false;
    }
}
function contarItems(combo){
  var n=0;
  for (i=0;i<combo.length ;i++ )
   if (combo[i].value!="") n++;
  return n;
}

function existeItem(combo,valor){
  for (i=0;i<combo.length;i++)
    if (combo[i].value==valor)
       return true;
  return false;
}

function eliminarItem(combo){
      n=contarItems(combo);
      if (n>0)  {
         var indice= combo.options.selectedIndex;
         if(indice==-1){
            indice=0;
         }		
         combo[indice].selected=false;
         combo[indice].text="";
         combo[indice].value="";
         var j=indice+1;
         for (i=indice;i<(combo.length-1) ;i++ ){
            combo[i].text=combo[j].text;
            combo[i].value=combo[j].value;
            j++;
         }
         if (n==1){
            combo[0].text="No tiene elementos asignados";
         }
  }
  else alert ("No hay elementos en la combo");
}

function EliminarOptionSelect(select){
    while(select.length>1){
        select.options[1] = null;
    }
}
function evitaSeleccionNula(combo){
    var i= combo.options.selectedIndex;
    var filaValor=combo.options[i].value;
    var filaTexto=combo.options[i].text;
    if ((filaValor=="") ||(filaTexto==""))  { 
        combo.options[0].selected= true;
        combo.options[i].selected= false;
    }
}

function controlaCabecera(control) {
    return control.options[control.selectedIndex].value=="-1"; 
}
function pintacapa(capa) {
	var codigo ="<table cellpadding=4 cellspacing=0 border=0>" +
						"<tr><td colspan=2 style='font:14px Arial;'>Datos del destinatario</td></tr>" +
						"<tr>" +
						"<td width=80 align=left style='font:14px Arial'>Nombre</td>" +
						"<td align=right><input type=text name='nombre_des' id='nombre_des' style='font:12px Arial; width:320px;'></td>" +
						"</tr>" +
						"<tr>" +
						"<td width=80 align=left style='font:14px Arial'>Apellidos</td>" +
						"<td align=right><input type=text name='apellido_des'  id='apellido_des' style='font:12px Arial; width:320px;'></td>" +
						"</tr>" +
						"<tr>" +
						"<td width=80 align=left style='font:14px Arial;'>Dirección</td>" +
						"<td align=right><input type=text name='direccion_des' id='direccion_des' style='font:12px Arial; width:320px;'></td>" +
						"</tr>" +
						"<tr>" +
						"<td width=80 align=left style='font:14px Arial'>Localidad</td>" +
						"<td align=right><input type=text name='localidad_des' id='localidad_des' style='font:12px Arial; width:320px;'></td>" +
						"</tr>" +
						"<tr>" +
						"<td colspan=2 align=left>" +
						"<table cellpadding=0 cellspacing=0 border=0 width='100%'>" +
						"<tr>" +
						"<td width=120 align=left style='font:14px Arial'>Código postal &nbsp;</td>" +
						"<td><input type=text name='codpost_des'  id='codpost_des'  maxlength=5 style='font:12px Arial; width:50px;'></td>" +
						"<td width=80 align=right style='font:14px Arial'>Provincia &nbsp;</td>" +
						"<td align='right'>" +
						"<select name='provincia_des' id='provincia_des' size='1' style='font:10px Arial'>" +
						"<option value='Fuera de España' selected>Fuera de España</option>"+
						"<option value='A Coru&ntilde;a'>A Coru&ntilde;a</option>"+
						"<option value='&Aacute;lava'>&Aacute;lava</option>"+
						"<option value='Albacete'>Albacete</option>"+
						"<option value='Alicante'>Alicante</option>"+
						"<option value='Almer&iacute;a'>Almer&iacute;a</option>"+
						"<option value='Asturias'>Asturias</option>"+
						"<option value='&Aacute;vila'>&Aacute;vila</option>"+
						"<option value='Badajoz'>Badajoz</option>"+
						"<option value='Baleares'>Baleares</option>"+
						"<option value='Barcelona'>Barcelona</option>"+
						"<option value='Burgos'>Burgos</option>"+
						"<option value='C&aacute;ceres'>C&aacute;ceres</option>"+
						"<option value='C&aacute;diz'>C&aacute;diz</option>"+
						"<option value='Cantabria'>Cantabria</option>"+
						"<option value='Castell&oacute;n'>Castell&oacute;n</option>"+
						"<option value='Ceuta'>Ceuta</option>"+
						"<option value='Ciudad Real'>Ciudad Real</option>"+
						"<option value='C&oacute;rdoba'>C&oacute;rdoba</option>"+
						"<option value='Girona'>Girona</option>"+
						"<option value='Granada'>Granada</option>"+
						"<option value='Guadalajara'>Guadalajara</option>"+
						"<option value='Guip&uacute;zcoa'>Guip&uacute;zcoa</option>"+
						"<option value='Huelva'>Huelva</option>"+
						"<option value='Huesca'>Huesca</option>"+
						"<option value='Ja&eacute;n'>Ja&eacute;n</option>"+
						"<option value='La Rioja'>La Rioja</option>"+
						"<option value='Las Palmas de Gran Canaria'>Las Palmas de Gran Canaria</option>"+
						"<option value='Le&oacute;n'>Le&oacute;n</option>"+
						"<option value='Lleida'>Lleida</option>"+
						"<option value='Lugo'>Lugo</option>"+
						"<option value='Madrid'>Madrid</option>"+
						"<option value='M&aacute;laga'>M&aacute;laga</option>"+
						"<option value='Melilla'>Melilla</option>"+
						"<option value='Murcia'>Murcia</option>"+
						"<option value='Navarra'>Navarra</option>"+
						"<option value='Ourense'>Ourense</option>"+
						"<option value='Palencia'>Palencia</option>"+
						"<option value='Pontevedra'>Pontevedra</option>"+
						"<option value='Salamanca'>Salamanca</option>"+
						"<option value='Segovia'>Segovia</option>"+
						"<option value='Sevilla'>Sevilla</option>"+
						"<option value='Soria'>Soria</option>"+
						"<option value='Santa Cruz de Tenerife'>Santa Cruz de Tenerife</option>"+
						"<option value='Tarragona'>Tarragona</option>"+
						"<option value='Teruel'>Teruel</option>"+
						"<option value='Toledo'>Toledo</option>"+
						"<option value='Valencia'>Valencia</option>"+
						"<option value='Valladolid'>Valladolid</option>"+
						"<option value='Vizcaya'>Vizcaya</option>"+														
						"<option value='Zamora'>Zamora</option>"+
						"<option value='Zaragoza'>Zaragoza</option>"+
						"</select>" +
						"</td>" +
						"</tr>" +
						"</table>" +
						"</td>" +
						"</tr>" +
						"<tr>" +
						"<td width=80 align=left style='font:14px Arial'>País</td>" +
						"<td align='right'><input type='text' name='pais_des'  id='pais_des' style='font:12px Arial; width:320px;'></td>" +
						"</tr>" +
						"</table>";
	document.getElementById("destinatario").innerHTML=codigo;
}

//***********************************************************************************
// Funciones para la colocacion del buscador
//***********************************************************************************

// Set the position of the popup window based on the anchor
function PopupWindow_getXYPosition(anchorname) {
	var coordinates;
	coordinates = getAnchorPosition(anchorname);
	this.x = coordinates.x;
	this.y = coordinates.y;
	}

// Position and show the popup, relative to an anchor object
function PopupWindow_showPopup(anchorname) {
	this.getXYPosition(anchorname);
	this.x += this.offsetX;
	this.y += this.offsetY;
	
	if (this.divName != null) {
		// Show the DIV object
		if (this.use_gebi) {
			document.getElementById(this.divName).style.left = this.x + "px";
			document.getElementById(this.divName).style.top = this.y + "px";
			document.getElementById(this.divName).style.visibility = "visible";
			}
		else if (this.use_css) {
			document.all[this.divName].style.left = this.x;
			document.all[this.divName].style.top = this.y;
			document.all[this.divName].style.visibility = "visible";
			}
		else if (this.use_layers) {
			document.layers[this.divName].left = this.x;
			document.layers[this.divName].top = this.y;
			document.layers[this.divName].visibility = "visible";
			}
		}
	else {
		if (this.popupWindow == null || this.popupWindow.closed) {
			// If the popup window will go off-screen, move it so it doesn't
			if (this.x<0) { this.x=0; }
			if (this.y<0) { this.y=0; }
			if (screen && screen.availHeight) {
				if ((this.y + this.height) > screen.availHeight) {
					this.y = screen.availHeight - this.height;
					}
				}
			if (screen && screen.availWidth) {
				if ((this.x + this.width) > screen.availWidth) {
					this.x = screen.availWidth - this.width;
					}
				}
			var avoidAboutBlank = window.opera || ( document.layers && !navigator.mimeTypes['*'] ) || navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled );
			this.popupWindow = window.open(avoidAboutBlank?"":"about:blank","window_"+anchorname,this.windowProperties+",width="+this.width+",height="+this.height+",screenX="+this.x+",left="+this.x+",screenY="+this.y+",top="+this.y+"");
			}
		this.refresh();
		}
	}

// CONSTRUCTOR for the PopupWindow object
// Pass it a DIV name to use a DHTML popup, otherwise will default to window popup
function PopupWindow() {
	if (!window.popupWindowIndex) { window.popupWindowIndex = 0; }
	if (!window.popupWindowObjects) { window.popupWindowObjects = new Array(); }
	if (!window.listenerAttached) {
		window.listenerAttached = true;
		//PopupWindow_attachListener();
		}
	this.index = popupWindowIndex++;
	popupWindowObjects[this.index] = this;
	this.divName = null;
	this.popupWindow = null;
	this.width=0;
	this.height=0;
	//this.populated = false;
	this.visible = false;
	this.autoHideEnabled = false;
	this.contents = "";
	this.url="";
	this.windowProperties="toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable,alwaysRaised,dependent,titlebar=no";
	if (arguments.length>0) {
		this.type="DIV";
		this.divName = arguments[0];
		}
	else {
		this.type="WINDOW";
		}
	this.use_gebi = false;
	this.use_css = false;
	this.use_layers = false;
	if (document.getElementById) { this.use_gebi = true; }
	else if (document.all) { this.use_css = true; }
	else if (document.layers) { this.use_layers = true; }
	else { this.type = "WINDOW"; }
	this.offsetX = 0;
	this.offsetY = 0;
	// Method mappings
	this.getXYPosition = PopupWindow_getXYPosition;
	this.showPopup = PopupWindow_showPopup;
	
	}
        
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}
        // Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}
function AnchorPosition_getWindowOffsetTop (el) {
    return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}
