/**
NOTA PARA LA EDICIÓN:
SE COMENTAN LAS FUNCIONES (PARÁMETRO DE ENTRADA, SALIDA, QUE HACE...

CADA FORMULARIO JSP TIENE UN COMPROBAR QUE LLAMA A ESTAS FUNCIONES Y SI 
DEVUELVEN FALSE -> MUESTRA ALERT EN EL IDIOMA INDICADO (VER frmInsertarPortal.JSP)

2.0 20050315 Incluir llamada a abrirVentanaPermisosObjeto
*/

/**
 * Abre la ventana de cambio de permisos de un objeto.
 */
function abreVentanaPermisosObjeto(intIdObjeto, strTipoObjeto){
    abrirVentana('verFrmPermisosObjeto.do?id=' + intIdObjeto + '&tipo=' + strTipoObjeto, '', 750, 400, 'yes', 'no');
}

/**
 * Comprueba si el elemento de formulario es vacío.
 * textField -> campo del formulario a comprobar (de tipo textfield)
 * True si está vacío o no es un textField, false en caso contrario. 
 */
function esVacio(textField){
    if ((textField.type == "text" || textField.type == "textarea" || textField.type == "file") && textField.value.length == 0) {
        return true;
    }
    return false;
}

/**
 * Muestra una alerta (msg), da el foco al elemento 
 * de formulario y si selecciona está a true, selecciona el 
 * contenido del formulario
 */
function alerta(campo, msg) {
    alert(msg);
    campo.focus();
    if ((campo.type == "text" || campo.type == "textarea" || campo.type == "file") && campo.value.length > 0) {
        campo.select();
    }
    return false;
}

/**
 * Comprueba si el campo de formulario es numérico.
 * True si lo es. False en caso contrario
 */
function Numero(elemento){
    if ((elemento.type == "text" || elemento.type == "textarea") ) {
        v = elemento.value;
        for(ichar = 0; ichar < v.length; ichar++){
            if (v.charAt(ichar) < '0' || v.charAt(ichar) > '9' )  {
                return false;
            }
        }
    }
    return true;
}

/** 
 * Abre la url indicada en una ventana de ancho y alto indicados
 */
function abrirVentana(url, nombre, ancho, alto){
    return ventana = open(url, nombre, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, width=' + ancho + ', height=' + alto);
}

/** 
 * Abre la url indicada en una ventana de ancho y alto indicados. Se puede indicar desplazamiento y ampliable. 
 */

function abrirVentana(url, nombre, ancho, alto, desplazamiento, ampliable){
    return ventana = open(url, nombre, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=' + desplazamiento + ', resizable=' + ampliable + ', width=' + ancho + ', height=' + alto);
}


function refreshTree() {
    parent.frames["menu"].location.href='verArbolNodos.do?tipo=ARBOLADMIN';
}


/**
 * Comprueba si hay en el formulario un campo que comience con url_id
 * Si es así, comprueba que el valor, en caso de que tenga alguno, comience con http://
 */
function enlaceExternoOk(form){
    for (i = 0; i < form.elements.length; i ++) {
        nombre = form.elements[i].name;
        if (nombre.indexOf("url_") != -1) {		// Hay al menos un enlace en el formulario
            valor_url = form.elements[i].value;
            if (valor_url.length == 0) {	// Enlace vacío
            	alert("Indique una url");
                form.elements[i].focus();
                form.elements[i].select();
                return false;
            }
            if (valor_url.length > 0) {
                if (valor_url.length < 8) {
                    alert("El enlace externo debe comenzar con http://");
                    form.elements[i].focus();
                    form.elements[i].select();
                    return false;
                }

                if (valor_url.substring(0, "http://".length).toLowerCase() != "http://" ) {
                    alert("El enlace externo debe comenzar con http://");
                    form.elements[i].focus();
                    form.elements[i].select();
                    return false;
                }
            }
            // Comprueba que se introduzca un nombre descriptivo de url en el formulario
            idUrl = nombre.substring(nombre.lastIndexOf("_") + 1, nombre.length);
            descr_url_field = eval('form.nombre_' + idUrl);
            if (descr_url_field.value.length == 0 ) {
            	alert("Debe indicar un nombre para la url");
				descr_url_field.focus();
                descr_url_field.select();
                return false;
			}
        }
    }
    return true;
}

/**
 * Comprueba si hay en el formulario un de texto que debe estar relleno
 */
function textoOk(form){
	/** 
		Comentado porque no funciona bien por el textarea especial 
		Quizás habría que intentarlo con el id
    for (i = 0; i < form.elements.length; i ++) {
        nombre = form.elements[i].name;
        if (nombre.indexOf("contenido_") != -1) {		
            valor_txt = form.elements[i].value;
            if (valor_txt.length == 0) {	
            	alert("Debe introducir un texto");
                return false;
            }
        }
    }
    **/
    return true;
}

/**
 * Comprueba si los datos de una imagen son correctos
 */
function imagenOk(form){
    for (i = 0; i < form.elements.length; i ++) {
        nombre = form.elements[i].name;
        
        
        patron = /fichero_[0-9]+/
        
        if (patron.test(nombre) ) {
            valor_txt = form.elements[i].value;
            if (valor_txt.length == 0) {	
            	alert("Debe seleccionar un archivo");
                return false;
            }
        }
        
        if (nombre.indexOf("ancho_img_") != -1) {		
        	valor_txt = form.elements[i].value;
            if (valor_txt.length > 0) {	
            	// Comprobar que es un número
            	if (!Numero(form.elements[i])) {
                    alert("El ancho debe ser numérico");
                    form.elements[i].focus();
                    form.elements[i].select();
                    return false;
                }
            }
    	}
    	
    	if (nombre.indexOf("alto_img_") != -1) {		
            valor_txt = form.elements[i].value;
            if (valor_txt.length > 0) {	
            	// Comprobar que es un número
            	if (!Numero(form.elements[i])) {
            		alert("El alto debe ser numérico");
                    form.elements[i].focus();
                    form.elements[i].select();
                    return false;
                }
            }
    	}
    	
    	if (nombre.indexOf("borde_img_") != -1) {		
            valor_txt = form.elements[i].value;
            if (valor_txt.length > 0) {	
            	// Comprobar que es un número
            	if (!Numero(form.elements[i])) {
            		alert("El borde debe ser numérico");
                    form.elements[i].focus();
                    form.elements[i].select();
                    return false;
                }
            }
    	}
    }
    return true;
}



/**
 * Comprueba si los datos de un fichero son correctos
 */
function ficheroOk(form){
    for (i = 0; i < form.elements.length; i ++) {
        nombre = form.elements[i].name;
        
        patron = /fichero_[0-9]+/
        
        if (patron.test(nombre) ) {
            valor_txt = form.elements[i].value;
            if (valor_txt.length == 0) {	
            	alert("Debe seleccionar un archivo");
                return false;
            }
        }
    }
    return true;
}

/**
 * Comprueba si los datos de un enlace interno están bien
 */
function enlaceInternoOk(form){
    for (i = 0; i < form.elements.length; i ++) {
        nombre = form.elements[i].name;
        if (nombre.indexOf("nombre_ref_") != -1) {		// Hay un enlace interno
            valor_url = form.elements[i].value;
            if (valor_url.length == 0) {	
            	alert("Debe seleccionar una referencia interna");
                return false;
            }
            
            // Comprueba que se introduzca un nombre 
            idUrl = nombre.substring(nombre.lastIndexOf("_") + 1, nombre.length);
            descr_url_field = eval('form.nombre_' + idUrl);
            if (descr_url_field.value.length == 0 ) {
            	alert("Debe indicar un nombre para el enlace interno");
                descr_url_field.focus();
                descr_url_field.select();
                return false;
            }
        }
       
    }
    return true;
}


function habilitarCamposFile(form){
    for (i = 0; i < form.elements.length; i ++) {
        nombre = form.elements[i].name;
        
        patronFichero = /fichero_[0-9]+/

        patronEnlaceInt = /nombre_ref_[0-9]+/
        
        if (patronFichero.test(nombre) || patronEnlaceInt.test(nombre) ) {
            if (form.elements[i].type == "text" && form.elements[i].disabled) {
                form.elements[i].disabled = false;
            }
        }
    }
}




function escribeWait(documento, msg) {
    documento.write('<html>');
    documento.write('<head>');
    documento.write('<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">');
    documento.write('<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">');
    documento.write('<meta content="-1" http-equiv="expires">');
    documento.write('<link type="text/css" href="./estilo/estilo.css" rel="StyleSheet">');
    documento.write('</head>');
    documento.write('<body bgcolor="#e5ebff" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">');
    
    // documento.write('<div id="waitmsg" style="visibility:visible; position:absolute; " >');
    documento.write('<table bgcolor="" border="0" class="" align="center" width="99%" height="99%">');
    documento.write('<tr>');
    documento.write('<td align="center" valign="middle">');
    
    documento.write('<table bgcolor="#ffffff" border="0" class="" align="center" width="">');
    documento.write('<tr>');
    documento.write('<td align="center" valign="middle">');
    
    documento.write('<img border="0" width="20" height="18" src="images/reloj.gif">');
    documento.write('</img>');
    documento.write('</td>');
    documento.write('<td class="title_font" align="center" valign="middle">');
    documento.write('<span class="title_font">' + msg + '</span>');
    documento.write('</td>');
    documento.write('</tr>');
    documento.write('</table>');
    
    documento.write('</td>');
    documento.write('</tr>');
    documento.write('</table>');
    // documento.write('</div>');
    
    documento.write('</body>');
    documento.write('</html>');
}


function tipShow(obj) {   
	centerIt(obj);
	if  (document.layers) {   
		if  (document.layers[obj] != null) {
			document.layers[obj].visibility = 'visible';
		}
	} else {
		if  (document.all) { 
			document.all[obj].style.visibility = 'visible';
		}
	}
}
	 
function tipHide(obj) {   
	if  (document.layers) {   
		if (document.layers[obj] != null) {
			document.layers[obj].visibility = 'hidden';
		}
	} else {
		if  (document.all) {
    		document.all[obj].style.visibility = 'hidden';
    	}
    }
}



// ***Begin library code better placed in an external API***
// Set global variables for browser detection and reference building
var isNav, isIE
var coll = ""
var styleObj = ""
if (parseInt(navigator.appVersion) >= 4) {
  if (navigator.appName == "Netscape") {
    isNav = true
  } else {
    isIE = true
    coll = "all."
    styleObj = ".style"
  }
}
// Utility function returns rendered height of object content in pixels
function getObjHeight(obj) {
  if (isNav) {
    return obj.clip.height
  } else {
    return obj.clientHeight
  }
}
// Utility function returns rendered width of object content in pixels
function getObjWidth(obj) {
  if (isNav) {
    return obj.clip.width
  } else {
    return obj.clientWidth
  }
}
// Utility function returns the available content width space in browser window
function getInsideWindowWidth() {
  if (isNav) {
    return window.innerWidth
  } else {
    return document.body.clientWidth
  }
}
// Utility function returns the available content height space in browser window
function getInsideWindowHeight() {
  if (isNav) {
    return window.innerHeight
  } else {
    return document.body.clientHeight
  }
}
// Utility function to position an element at a specific x,y location
function shiftTo(obj, x, y) {
  if (isNav) {
    obj.moveTo(x,y)
  } else {
    obj.pixelLeft = x
    obj.pixelTop = y
  }
}
// ***End library code***

// Center an element named banner in the current window/frame, and show it
function centerIt(objeto) {
// 'obj' is the positionable object
  var obj = eval("document." + coll + objeto + styleObj)
// 'contentObj' is the element content, necessary for IE4 to return the
// true current width
  var contentObj = eval("document." + coll + objeto)
  var x = Math.round((getInsideWindowWidth()/2) - (getObjWidth(contentObj)/2))
  var y = Math.round((getInsideWindowHeight()/2) - (getObjHeight(contentObj)/2))
  shiftTo(obj, x, y)
  obj.visibility = "visible"
}
// Special handling for CSS-P redraw bug in Navigator 4
function handleResize(objeto) {
  if (isNav) {
// causes extra re-draw, but gotta do it to get banner object color drawn
    location.reload() 
  } else {
    centerIt(objeto) 
  }
}
