/*
* funciones.js
*
* ==============================================================================
*
* License: Licencia de desarrollo de software.
*
* Copyright (c) 2004 iQual ingenieros S.L.L. All rights reserved.
*
* This file is part of the CHMS Web application.
* (Cultural Heritage Management System)
*
* You should have received a copy of the License along with this program;
* if not, write to the iQual Ingenieros S.L.L.
*
* support@iqualingenieros.com
*
*/

function id2Obj(id) 
{
	return (document.getElementById) ? document.getElementById(id) : ((document.all) ? document.all[id] : document.layers[id]);
} // id2Obj

// Specify affected tags. Add or remove from list
var GENERAL_htmlZoom_ETIQS = new Array('div', 'td', 'tr', 'a', 'p', 'table', 'input', 'select', 'textarea', 'h1', 'h2', 'strong', 'ul', 'li');

/**
 * Tamaño de letra.
 *
 * @param   object   the table row
 * @param   object   the color to use for this row
 * @return  boolean  whether pointer is set or not
 */
function htmlZoom(id_etiq, zoom) 
{
	// DEBUG
	//alert('htmlZoom');

	var objX, iX, objTags, iTags;

	//
	if (!(objX = id2Obj(id_etiq))) 
	{
		objX = document.getElementsByTagName(id_etiq)[0];
	}

	objX.style.fontSize = zoom;

	for (iX = 0; iX < GENERAL_htmlZoom_ETIQS.length; iX++) 
	{

		objTags = objX.getElementsByTagName(GENERAL_htmlZoom_ETIQS[iX]);

		for (iTags = 0; iTags < objTags.length; iTags++) 
		{
			objTags[iTags].style.fontSize = zoom;
		} // for

	} // for

} // htmlZoom

/**
 * Establece el color de una fila de una tabla.
 *
 * @param   object   the table row
 * @param   object   the color to use for this row
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, thePointerColor)
{
    if (thePointerColor == '' || typeof(theRow.style) == 'undefined') {
        return false;
    }
    if (typeof(document.getElementsByTagName) != 'undefined') {
        var theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        var theCells = theRow.cells;
    }
    else {
        return false;
    }

    var rowCellsCnt  = theCells.length;
    for (var c = 0; c < rowCellsCnt; c++) 
    {
        theCells[c].style.backgroundColor = thePointerColor;
    }

    return true;
} // end of the 'setPointer()' function

/**
 * Establece el color de una celda de una tabla.
 *
 * @param   object   the table cell
 * @param   object   the color to use for this cell
 * @return  boolean  whether pointer is set or not
 */
function setCellColor(theCell, thePointerColor)
{
    theCell.style.backgroundColor = thePointerColor;
    return true;
}

/**
 * Asigna un nuevo fichero fuente a una imagen.
 *
 * @param   object   the img object 
 * @param   string   the source img
 * @return  boolean  whether pointer is set or not
 */
function setImg(img, src)
{
	auxImage = new Image();
	auxImage.src = src;
    img.src = auxImage.src;
    return true;
}

/**
 * Obtiene de los inputs del formulario USERNAME, PASSWORD y CHALLENGE
 * la siguiente cadena:
 * md5(username:md5(password):challenge)
 * y la almacena en el input del formulario RESPONSE.
 *
 * Se usa en formulario de login de usuario de aplicación.
 *
 * @param   object   the form
 * @return  boolean  true
 */
function doChallengeResponse(form)
{
    str = form.elements['USERNAME'].value + ":" +
          MD5(form.elements['PASSWORD'].value) + ":" +
          form.elements['CHALLENGE'].value;
    form.elements['RESPONSE'].value = MD5(str);
    // TO-DO: quitar este comentario cuando se implemente
    //        en la función login del dominio el que se le pase
    //        la password encriptada.
    //form.elements['PASSWORD'].value = "";
    return true;
}

/**
 * Obtiene de los inputs del formulario USERNAME, PASSWORD y CHALLENGE
 * la siguiente cadena:
 * username:md5(password):challenge
 * y la almacena en el input del formulario RESPONSE.
 *
 * Se usa en formulario de login de administrador.
 *
 * @param   object   the form
 * @return  boolean  true
 */
function doChallengeResponseAdmin(form)
{
    str = form.elements['USERNAME'].value + ":" +
          MD5(form.elements['PASSWORD'].value) + ":" +
          form.elements['CHALLENGE'].value;
    form.elements['RESPONSE'].value = MD5(str);
    form.elements['PASSWORD'].value = "";
    return true;
}

/**
 * Obtiene de los inputs del formulario USERNAME, PASSWORD y CHALLENGE
 * la siguiente cadena:
 * md5(username:challenge)
 * y la almacena en el input del formulario RESPONSE.
 * y además encripta la contraseña.
 *
 * Se usa en formulario de modificación/insercción de nuevos
 * usuarios adminitradores
 *
 * @param   object   the form
 * @return  boolean  true
 */
function encriptPass(form)
{
    str = form.elements['USERNAME'].value + ":" +
          form.elements['CHALLENGE'].value;
    form.elements['RESPONSE'].value = MD5(str);
    form.elements['PASSWORD'].value = MD5(form.elements['PASSWORD'].value);
    return true;
}

/**
 * Obtiene el input del formulario  CHALLENGE
 * y la almacena en el input del formulario RESPONSE.
 *
 * Se usa en algunos formularios de administración
 * para validar las respuestas que envían los clientes.
 * Se validad las respuestas que devuelven el mismo testigo (CHALLENGE)
 * que se les envió con el formulario.
 *
 * @param   object   the form
 * @return  boolean  true
 */
function sendResponse(form)
{
    form.elements['RESPONSE'].value = form.elements['CHALLENGE'].value;
    return true;
}