﻿// Funciones para mensajes de aviso.

window.onload = function(){
                    links_externos();
                };
                
/*
window.onbeforeunload = function(){
                    inhabilitar_contexto();
                };
*/

function inhabilitar_contexto(opc){

    if(arguments.length == 0){
        opc = "1";
    }
    
    if(! document.getElementById("capaFondo" + opc)){
        var capa = document.createElement("DIV");

        capa.id = "capaFondo" + opc;

        document.body.appendChild(capa);
        
        document.body.tag = "capaFondo" + opc;
    }
}

function habilitar_contexto(){
    
    var capa_contexto = document.getElementById(document.body.tag);
    
    if(capa_contexto){
        document.body.removeChild(capa_contexto);
    }

    var capa_mensaje = document.getElementById("idMensaje_aviso");

    if(capa_mensaje){
        document.body.removeChild(capa_mensaje);
    }
}

function crea_mensaje_aviso(top, left, width, height, texto, opc, num_icono, icono_cerrar){

    inhabilitar_contexto(opc);
    
    var capa = document.createElement("DIV");

    var height2;
    
    if(height == "auto"){
        height2 = 150;
    }else{        
        height2 = height;
    }
    
    if(top == null){
        capa.style.top       = "50%";
        capa.style.marginTop = "-" + (height2/2) + "px";
    }else if(top >= 0){
        capa.style.top        = top + "px";
    }else{
        capa.style.bottom     = (top * -1) + "px";
    }

    if(left == null){
        capa.style.left       = "50%";
        capa.style.marginLeft = "-" + (width/2) + "px";
    }else{
        capa.style.left       = left + "px";
    }

    capa.id               = "idMensaje_aviso";
    capa.style.position   = "absolute";
    capa.style.padding    = "15px";
    capa.style.width      = width + "px";

    if(height == "auto"){
        capa.style.height     = "auto";
    }else{        
        capa.style.height     = height + "px";
    }
    capa.className        = "cuadro";
    capa.style.zIndex     = 99;

    var capa_icono = document.createElement("SPAN");

    capa_icono.id = "icono_mensaje_aviso";
    
    if(! num_icono){
        icono = 1;
    }

    if(num_icono == 1){
        capa_icono.className = "icono_mensaje_aviso_espera";
    }else{
        capa_icono.className = "icono_mensaje_aviso_informe";
    }
    
    capa.appendChild(capa_icono);

    var capa_texto = document.createElement("SPAN");

    capa_texto.id              = "idTexto_mensaje_aviso";
    capa_texto.style.float      = "left";
    capa_texto.style.width      = "auto";
    capa_texto.style.height     = "auto";
    capa_texto.style.margin     = "0 0 auto 0";
    capa_texto.innerHTML        = texto;
    
    capa.appendChild(capa_texto);

    if(icono_cerrar == 1){
        var capa_cerrar = document.createElement("A");

        capa_cerrar.className        = "cerrar";
        capa_cerrar.style.float      = "left";
        capa_cerrar.style.margin     = "0 0 auto 15px";
        capa_cerrar.title            = "Cerrar";
        capa_cerrar.onclick          = function(){
                                        habilitar_contexto();
                                       };
                                       
        capa.appendChild(capa_cerrar);
    }
    
    document.body.appendChild(capa);

}

function cambia_icono_mensaje_aviso(opc){
    
    if(document.getElementById("icono_mensaje_aviso")){

        var icono = "icono_mensaje_aviso_espera";
        
        if(opc == 1){
            icono = "icono_mensaje_aviso_espera";
        }else if(opc == 2){
            icono = "icono_mensaje_aviso_informe";
        }

        document.getElementById("icono_mensaje_aviso").className = icono;
    }
}

function cambia_texto_mensaje_aviso(texto){
    if(document.getElementById("idTexto_mensaje_aviso")){
        document.getElementById("idTexto_mensaje_aviso").innerText = texto;
    }
}

function cambia_texto_mensaje_aviso_formato(texto){
    if(document.getElementById("idTexto_mensaje_aviso")){
        document.getElementById("idTexto_mensaje_aviso").innerHTML = texto;
    }
}
function elimina_mensaje_aviso(){
    
    var capa_mensaje = document.getElementById("idMensaje_aviso");

    if(capa_mensaje){
        document.body.removeChild(capa_mensaje);
    }
}

function alltrim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

function control_elemento(elemento,valor,texto){
	
	var elementos = elemento.split("#");
	var cadena;
		
	cadena = "alltrim(document.getElementById('" + elementos[0] + "').value) == valor";
	
	for(var i=1;i<elementos.length;i++){
		cadena = cadena + " && alltrim(document.getElementById('" + elementos[i] + "').value) == valor";
	}	

	if(eval(cadena)){
		if(texto != ""){
		    alert(texto);
		    document.getElementById(elementos[0]).focus();
        }
	    return false;
	}else{
		return true;
	}
}

function ocultar_selects(){

    if(navigator.appVersion.indexOf("MSIE 6") > 0){
	    for(var i=0; i<document.getElementsByTagName("SELECT").length; i++){
		    document.getElementsByTagName("SELECT")(i).style.visibility = "hidden";
	    }

        for (var j=0; j<document.frames.length; j++){
            var marco = document.frames[j];

            if(marco){
	            for(var i=0; i<marco.document.getElementsByTagName("SELECT").length; i++){
		            marco.document.getElementsByTagName("SELECT")(i).style.visibility = "hidden";
	            }
            }        
            
        }
    }
}

function mostrar_selects(){
    if(navigator.appVersion.indexOf("MSIE 6") > 0){
	    for(var i=0; i<document.getElementsByTagName("SELECT").length; i++){
		    document.getElementsByTagName("SELECT")(i).style.visibility = "visible";
	    }

        for (var j=0; j<document.frames.length; j++){
            var marco = document.frames[j];

            if(marco){
	            for(var i=0; i<marco.document.getElementsByTagName("SELECT").length; i++){
		            marco.document.getElementsByTagName("SELECT")(i).style.visibility = "visible";
	            }
	        }
            
        }
    }
}


// Joseba
// 04/05/2007
// Sustituir target's para que pueda ser validados por W3C
function links_externos(){
	if (! document.getElementsByTagName) return;

	var anclas = document.getElementsByTagName("a");

	for (var i=0; i<anclas.length; i++){
		var ancla = anclas[i];

		if (ancla.getAttribute("href") && ancla.getAttribute("rel") == "external"){
			ancla.target = "_blank";
			ancla.title = ancla.title  + " [Este enlace se abrirá en una nueva ventana]";
		}
	}
}

function gestionar_tipo_cliente(opc){
    if(opc == 1){
        document.getElementById("idEmpresa1").style.display = "none";
    }else{
        document.getElementById("idEmpresa1").style.display = "block";
    }
}

function mostrar_terminos_legales(){
    inhabilitar_contexto(1);
    document.getElementById("idTerminos_legales").style.display = "block";
}

function cancelar_mostrar_terminos_legales(){
    document.getElementById("idTerminos_legales").style.display = "none";
    habilitar_contexto();
}

    function enviar_formulario(){
    
        var formu = document.getElementById("frm_solicitud");
        
        var id_tipo = document.getElementById("radparticular").checked?"1":"2";
        
        /* Comprobamos que los campos obligatorios estén rellenos */
        
        for(var i=0; i<formu.length; i++){
            
            if(formu.elements[i].tagName == "SELECT" && formu.elements[i].title != ""){
                if(formu.elements[i].title.substring(0,1) == "*"){
                
                    ok = true;
                    
                    if(ok){
                        var car = formu.elements[i].title.substring(formu.elements[i].title.length-1,formu.elements[i].title.length);
                        
                        var id_tipo_aux = "0";
                        
                        if(car == "."){
                            id_tipo_aux = "1";
                        }else if(car == " "){
                            id_tipo_aux = "2";
                        }                    
                        
                        if(id_tipo_aux == id_tipo || id_tipo_aux == "0"){
                            if(! control_elemento(formu.elements[i].name, "-1",  formu.elements[i].title)){
                                return;
                            }
                        }
                    }
                    
                }
                
            }else if((formu.elements[i].type == "text" || formu.elements[i].tagName == "TEXTAREA") && formu.elements[i].title != ""){
                if(formu.elements[i].title.substring(0,1) == "*"){

                    ok = true;
                    
                    if(ok){
                        var car = formu.elements[i].title.substring(formu.elements[i].title.length-1,formu.elements[i].title.length);
                        
                        var id_tipo_aux = "0";
                        
                        if(car == "."){
                            id_tipo_aux = "1";
                        }else if(car == " "){
                            id_tipo_aux = "2";
                        }                    
                        
                        if(id_tipo_aux == id_tipo || id_tipo_aux == "0"){
                            if(! control_elemento(formu.elements[i].name, "",  formu.elements[i].title)){
                                return;
                            }
                        }
                    }
                }   
            }
        }
    
        /* Comprobamos los campos de fecha, email y términos legales */
                
        if(! control_elemento("txttelefono#txtmail", "",  "Introduzca un teléfono o un email de contacto, por favor.")){
            return;
        }

        if(control_elemento("txtmail", "",  "")){
	        if (! validar_correo (formu.txtmail.value)){
		        alert ("El campo email no es válido");
		        formu.txtmail.focus();
		        return;
	        }
        }
        
	    if (! formu.chklegal.checked){
		    alert ("La casilla acepto los términos legales debe esta activada");
		    formu.chklegal.focus ();
		    return;
	    }
	    
        /* Creamos inputs ocultos con el texto de los selects seleccionados */
        
        for(var i=0; i<formu.length; i++){
            if(formu.elements[i].tagName == "SELECT"){
                if(formu.elements[i].length > 0){
                    if(formu.elements[i].value != "-1" && formu.elements[i].value != ""){
                        var nombre = formu.elements[i].id.substring(3, 20);
                        
                        var caja = crearInput("txt" + nombre);
                        caja.value = formu.elements[i][formu.elements[i].selectedIndex].text;
                        formu.appendChild(caja);
                    }
                }
            }
        }
    
        //formu.submit();
        //return;
        enviar_datos_formulario();
    }
    
    function enviar_datos_formulario(){

        crea_mensaje_aviso(null, null, 298, 30, "Enviando datos, espere por favor ...", 1, 1, 0);

	    var formu = document.getElementById("frm_solicitud");

        var valores = fn_obtener_valores_formulario(formu);
        
        conexion_gestionar_datos_POST_generico(formu.action, valores, resultado_envio_formulario);
        
        var ocultos = document.getElementsByTagName("INPUT");
        
        /* Eliminamos los inputs ocultos con el texto de los selects seleccionados */
        for(var i=0; i<ocultos.length;){
            if(ocultos[i].type == "hidden"){
                ocultos[i].parentNode.removeChild(ocultos[i]);
            }else{
               i++;
            }
        }
        
    }
    
    function resultado_envio_formulario(error, resultado){

        habilitar_contexto();

        if(error == 0){
            crea_mensaje_aviso(null, null, 298, 200, resultado, 1, 2, 1);
            
            resetear_otros_datos();
        }else{
            crea_mensaje_aviso(null, null, 298, 100, resultado, 1, 2, 1);
        }
        
    }
    
    function resetear_otros_datos(){
        document.getElementById("txtdenominacion").value = "";
        document.getElementById("txtnombre").value = "";
        document.getElementById("txtapellido1").value = "";
        document.getElementById("txttelefono").value = "";
        document.getElementById("txtmail").value = "";
        document.getElementById("txtobservaciones").value = "";
    }
    
function crearInput(nombre) {
    
    var newInput = document.createElement("input");
    newInput.type = "hidden";
    newInput.name = nombre;
    newInput.id = nombre;

    return newInput;
}
    
