/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 *  Función que establece el mensaje que mostrará la confirmación de eliminación
 *  de un elemento en la aplicación.
 *
 *  Igualmente devuelve true si el usuario escoge Sí, Yes,... desencadenando
 *  el evento de eliminación.
 *
 *  Devuelve False en caso de que el usuario cierre la ventana de aviso.
 *
 *  @author: Luis Vicente Jiménez Farfán
 *  @date: 12/09/2009
 ******************************************************************************/
function deleteThis(mensaje){
    respuesta = false;
    if(confirm(mensaje)){
        respuesta=true;
    }
    return respuesta;
}

/*
 *  Función situada al pie de todas las páginas de la aplicación. Establece el
 *  foco(cursor) en el primer elemento no oculto de un formulario y selecciona
 *  el texto introducido.
 *
 *  @author: Luis Vicente Jiménez Farfán
 *  @date: 12/09/2009
 ******************************************************************************/
function establecerFocoPrimerElementoFormulario() {
    if(document.forms.length > 0) {
         for(var i=0; i < document.forms[0].elements.length; i++) {
             var campo = document.forms[0].elements[i];
            if(campo.type != "hidden") {
             //   campo.select();
                campo.focus();
                break;
            }
         }
    }
}

/*
 *  Función que recibe una id y desabilita el elemento (disabled) en la página.
 *
 *  @author: Luis Vicente Jiménez Farfán
 *  @date: 05/10/2009
 ******************************************************************************/
function disableThis(id) {
    document.getElementById(id).disabled = true;
    //document.getElementById(id).readOnly   = true;
}

/*
 *  Función que recibe una id y habilita el elemento (disabled) en la página.
 *
 *  @author: Luis Vicente Jiménez Farfán
 *  @date: 05/10/2009
 ******************************************************************************/
function enableThis(id) {
    document.getElementById(id).disabled = false;
    //document.getElementById(id).readOnly   = false;
}

/*
 *  Función que recibe el número de opción de menú que debe marcar
 *  en caso de elementos de la zona de Clientes.
 *
 *  @author: Luis Vicente Jiménez Farfán
 *  @date: 19/10/2009
 ******************************************************************************/
function markMenuItem(numId) {
    document.getElementById('opcion' + numId).className  = 'op_seleccionada';
    document.getElementById('img' + numId).style.display = 'inline';
}
/*
 *  Función que impide que se introduzcan caracteres no numéricos en un campo
 *  de textfield.
 *  @author: Luis Vicente Jiménez Farfán
 *  @date: 19/10/2009
 ******************************************************************************/
function soloNumeros(e,txt)	{
    tecla = (document.all) ? e.keyCode : e.which;
    if (tecla==0) return true; //Teclas de funcion, escape, tabulador, etc.
    if (tecla==8) return true; //Tecla de retroceso (para poder borrar)
    if (tecla==9) return true; //tabulacion para cambiar de cammpos
    if (tecla==43) return true;//Simbolo +
    if (tecla==40) return true;// Tecla de abrir paréntesis (
    if (tecla==41) return true;// Tecla de cerrar paréntesis )
    if ((tecla<48 || tecla>57) /*&& tecla!=46 && tecla!=44 */)
    return false;
}
/*
 *  Función que recibe un id y muestra u oculta una capa o elemento
 *  referenciado a dicha id.
 *
 *  @author: Luis Vicente Jiménez Farfán
 *  @date: 19/10/2009
 ******************************************************************************/
function displayThis(id) {

    if(document.getElementById(id).style.display == 'inline') {
        document.getElementById(id).style.display = 'none';
        document.getElementById("menos_"+id).style.display = 'none';
        document.getElementById("mas_"+id).style.display = 'inline';
    }
    else {
       document.getElementById(id).style.display = 'inline';
       document.getElementById("menos_"+id).style.display = 'inline';
       document.getElementById("mas_"+id).style.display = 'none';
       
    }


}

function displayNameThis(name) {

    lista = document.getElementsByName(name);
    for(i=0;i<lista.length;i++)
    {
        if(lista[i].style.display == 'inline') {
            lista[i].style.display = 'none';
            document.getElementById("menos_"+name).style.display = 'none';
            document.getElementById("mas_"+name).style.display = 'inline';
        }
        else {
           lista[i].style.display = 'inline';
           document.getElementById("menos_"+name).style.display = 'inline';
           document.getElementById("mas_"+name).style.display = 'none';
        }
    }

}

function nuevoObjetoAjax()
{
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false;
	try
	{
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			// Creacion del objet AJAX para IE
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		}
	}
	return xmlhttp;
}

function llamarPaginaIntermedia(responsePage, elemento, nombreCampo, valor){
	var ajax = nuevoObjetoAjax();
	ajax.open("GET", responsePage + "?nombreCampo=" + nombreCampo + "&valor=" + valor, true);

	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==1)
		{
		/*
			// Mientras carga elimino la opcion "Selecciona Opcion..." y pongo una que dice "Cargando..."
			selectDestino.length=0;
			var nuevaOpcion=document.createElement("option"); nuevaOpcion.value=0; nuevaOpcion.innerHTML="Cargando...";
			selectDestino.appendChild(nuevaOpcion); selectDestino.disabled=true;
		*/
		}
		if (ajax.readyState==4)
		{
			//selectDestino.parentNode.innerHTML=ajax.responseText;
			/*document.getElementsByName(nombreFormu).value = ajax.responseText;
			document.faqSubCategoria.prueba.value = ajax.responseText;
			*/
			//elementos = document.getElementsByName(elemento);
			//elementos[0].value = ajax.responseText;
			//elemento.parentNode.innerHTML=ajax.responseText;
			document.getElementById(elemento).innerHTML = ajax.responseText;
		}
	}

	ajax.send(null);
}
function llamarPaginaIntermedia(responsePage, elemento, nombreCampo, valor){
	var ajax = nuevoObjetoAjax();
	ajax.open("GET", responsePage + "" + nombreCampo + "" + valor, true);

	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==1)
		{
		/*
			// Mientras carga elimino la opcion "Selecciona Opcion..." y pongo una que dice "Cargando..."
			selectDestino.length=0;
			var nuevaOpcion=document.createElement("option"); nuevaOpcion.value=0; nuevaOpcion.innerHTML="Cargando...";
			selectDestino.appendChild(nuevaOpcion); selectDestino.disabled=true;
		*/
		}
		if (ajax.readyState==4)
		{
			//selectDestino.parentNode.innerHTML=ajax.responseText;
			/*document.getElementsByName(nombreFormu).value = ajax.responseText;
			document.faqSubCategoria.prueba.value = ajax.responseText;
			*/
			//elementos = document.getElementsByName(elemento);
			//elementos[0].value = ajax.responseText;
			//elemento.parentNode.innerHTML=ajax.responseText;
			document.getElementById(elemento).innerHTML = ajax.responseText;
		}
	}

	ajax.send(null);
}
/**
 * Recorta un textarea al máximo tamaño especificado.
 */
function textCounter(field, maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
    {
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters left' counter
    }
    else {
        //countfield.value = maxlimit - field.value.length;
    }
}



