var ns6=document.getElementById&&!document.all?1:0;
window.status = "Quality";

//Abrir ventana
function abrirventana(url, width, height, nombre, dimensionable)
{
	if(!ns6)
	{
		izq = (screen.availWidth) ? (screen.availWidth-width)/2 : 0;
		alt = (screen.availHeight) ? (screen.availHeight-height)/2 - 5 : 0;
	}else{
		izq = (screen.width) ? (screen.width-width)/2 : 0;
		alt = (screen.height) ? (screen.height-height)/2 - 5 : 0;
	}
	
	if(!dimensionable){
		dimensionable = 0;
	}
	
	var ventana = window.open(url, nombre, "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=" + dimensionable +",left=" + izq + ",top=" + alt + ",width=" + width + ",height=" + height);
	ventana.focus();
	
	return ventana;
}

function AbrirPop(url)
{
	width = 250;
	height = 150;
	
	if(document.all)
	{
		izq = (screen.availWidth) ? (screen.availWidth-width)/2 : 0;
		alt = (screen.availHeight) ? (screen.availHeight-height)/2 : 0;
	}else{
		izq = (screen.width) ? (screen.width-width)/2 : 0;
		alt = (screen.height) ? (screen.height-height)/2 : 0;
	}

	ventana = window.open(url, '', "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,left=" + izq + ",top=" + alt + ",width=" + width + ",height=" + height);
	ventana.focus();
}

function ObtenerLink(texto){
	for (var i=0;i<document.links.length;i++){
       if (document.links[i].innerHTML == texto){
          return i;
       }
    }
}

function FormatearNumero(Numero,Decimales){
    var ValorEntero;
    var ValorDecimal;
    var NumDigitosEntero;
    var iIndice;
    var Resultado;

    ValorEntero =  Math.ceil(Numero);

    if (ValorEntero != Numero){
	    ValorEntero = ValorEntero - 1;
    }

    ValorEntero = ValorEntero.toString();

    //Obtenemos parte decimal
    ValorDecimal = Numero - ValorEntero;
    ValorDecimal = ValorDecimal*Math.pow(10,Decimales);
    ValorDecimal = Math.round(ValorDecimal);
    ValorDecimal = String(ValorDecimal);

    if (ValorDecimal.length == 1){
	    ValorDecimal = "0" + ValorDecimal;
    }

    NumDigitosEntero = ValorEntero.length;

    if (Decimales == 0 ){
	    if (NumDigitosEntero > 3){
    		
		    Resultado = "";
    		
		    for (iIndice=NumDigitosEntero - 3;iIndice > 0; iIndice = iIndice - 3){
    			
			    if (Resultado != ""){
    			    Resultado = "." + Resultado;
			    }
    			
		        Resultado = ValorEntero.substr(iIndice,3) + Resultado;
		    }
    			
	        iIndice = 3 + iIndice;
	        Resultado = ValorEntero.substr(0,iIndice) + "." + Resultado;
    	    
	    }else{
	        Resultado = ValorEntero;
	    }
    }else{
	    if (NumDigitosEntero > 3){
    		
		    Resultado = "";
    		
		    for (iIndice=NumDigitosEntero - 3;iIndice > 0; iIndice = iIndice - 3){
    			
			    if (Resultado != ""){
				    Resultado = "." + Resultado;
			    }
    			
			    Resultado = ValorEntero.substr(iIndice,3) + Resultado;
    			
		    }
    		
		    iIndice = 3 + iIndice;
		    Resultado = ValorEntero.substr(0,iIndice) + "." + Resultado + "," + ValorDecimal;
    		
	    }else{
	        Resultado = ValorEntero + "," + ValorDecimal;
	    }
    }
    return Resultado;

}

function dimensionar(){

	var cadena='';
	var ventana;
	var strFrame='';
	ventana = window.parent;
	strFrame = window.name;
	strFrame = strFrame.toString();
	
	while (strFrame!='' && strFrame!='undefined' && strFrame !=null && strFrame != "popupnuevo" && strFrame.substring(0,3) != "msn" && strFrame.substring(0,3) != "top"){
		
		cadena = ventana.frames[strFrame];
		
		if(ns6 == false){
			ventana.document.getElementById("id_" + strFrame.substring(3,strFrame.length)).style.height = ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.body.scrollHeight;
		}else{
			ventana.document.getElementById("id_" + strFrame.substring(3,strFrame.length)).style.height = ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.documentElement.offsetHeight;
		}
	
		strFrame = ventana.name;
		ventana = ventana.parent;
	}
}


function show_calendar(c, m, a, f, m1, m2){

	c = eval(c);
	
	var valido = window.screen.height-window.screen.availHeight + 220;
	top.document.getElementById('id_calen').style.pixelTop = top.document.body.scrollTop + window.event.screenY - valido;

	top.document.getElementById('id_calen').style.pixelLeft = window.event.screenX-100;
	top.document.getElementById('id_calen').style.visibility = "visible";
	top.document.frames['nm_calen'].show_calendar(document, c, m, a, f, m1, m2);
}

// ---------------------------------------------------------------------------------
// Función para gestionar un combo.
// Abre un popup para realizar una búsqueda sobre los elementos del combo.
// Joseba
// 12/09/2005

function fn_filtrar_combo(nombre_combo, ruta){
	abrirventana("/intranet/ventana_gestion_combos.asp?nombre_combo=" + nombre_combo, 500, 400, "vent_gestion_combos");
}

// ---------------------------------------------------------------------------------
// Función para controlar el máximo de caracteres a introducir en un cuadro de texto.
// Joseba
// 16/09/2005
// Poner en el onkeyup.

function fn_contador_letras(cuadro_texto, maximo){
	if (cuadro_texto.value.length > maximo){
	     cuadro_texto.value = cuadro_texto.value.substr(0, maximo);
	     alert("Ha sobrepasando el número máximo de caracteres permitidos que es de: " + maximo + " caracteres.")
	}
}


// Redimensiona el mínimo pasado o la altura total si es más alta
// Joseba
// 08/02/2005

function dimensionar_minimo(altura){
	
	var cadena='';
	var ventana;
	var strFrame='';
	
	ventana = window.parent;
	
	strFrame = window.name;
	strFrame = strFrame.toString();

	if(!ns6){
		if(ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.body.scrollHeight < altura){
			ventana.document.getElementById("id_" + strFrame.substring(3,strFrame.length)).style.height = altura;
		}else{
			ventana.document.getElementById("id_" + strFrame.substring(3,strFrame.length)).style.height = ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.body.scrollHeight;
		}
	}else{
		if(ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.documentElement.offsetHeight < altura){
			ventana.document.getElementById("id_" + strFrame.substring(3,strFrame.length)).style.height = altura;
		}else{
			ventana.document.getElementById("id_" + strFrame.substring(3,strFrame.length)).style.height = ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.documentElement.offsetHeight;
		}
	}
	
	strFrame = ventana.name;
	ventana = ventana.parent;

	while (strFrame!='' && strFrame!='undefined' && strFrame !=null && strFrame != "popupnuevo" && strFrame.substring(0,3) != "msn"){	
		if(!ns6){
			ventana.document.getElementById("id_" + strFrame.substring(3,strFrame.length)).style.height = ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.body.scrollHeight;
		}else{
			ventana.document.getElementById("id_" + strFrame.substring(3,strFrame.length)).style.height = ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.documentElement.offsetHeight;
		}
	
		strFrame = ventana.name;
		ventana = ventana.parent;
	}
}

//Redimensionar sólo el iframe actual en altura
// Luis
// 9/9/2004
function dimensionarme(){
	
	var cadena='';
	var ventana;
	var strFrame='';
	ventana = window.parent;
	strFrame = window.name;
	strFrame = strFrame.toString();
	
	if(ns6 == false)
		{
		ventana.document.getElementById("id_" + strFrame.substring(3,strFrame.length)).style.height = ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.body.scrollHeight;
		}
		else
		{
		ventana.document.getElementById("id_" + strFrame.substring(3,strFrame.length)).style.height = ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.documentElement.offsetHeight;
	}
	//alert(strFrame + ventana.frames["nm_" + strFrame.substring(3,strFrame.length)].document.body.scrollHeight);
}

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 ok = false;

	for(i=0; i<elementos.length; i++){
		if(alltrim(document.getElementById(elementos[i]).value) != valor){
		    ok = true;
		    break;
		}    
	}	

	if(!ok){
    	alert(texto);
		if(texto != ""){
		    document.getElementById(elementos[0]).focus();
        }
	    return ok;
	}else{
		return ok;
	}
}

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";
	            }
	        }
            
        }
    }
}

// Funciones para mensajes de aviso.

function inhabilitar_contexto(opc){

    if(arguments.length == 0){
        opc = "1";
    }
    
    if(! document.getElementById("CapaFondo" + opc)){
        var capa = document.createElement("div");

        capa.id            = "CapaFondo" + opc;
        capa.style.display = "block";

        document.body.appendChild(capa);
        
        document.body.tag = "CapaFondo" + opc;
    }
}

function habilitar_contexto(mensaje){
    
    if(!mensaje){
        var capa_contexto = document.getElementById(document.body.tag);
        
        if(capa_contexto){
            document.body.removeChild(capa_contexto);
        }
    }
    
    elimina_mensaje_aviso();

}

function crea_mensaje_aviso(top, left, width, height, texto, opc, num_icono, icono_cerrar){

    inhabilitar_contexto(opc);
    
    var capa = document.createElement("DIV");

    capa.id               = "idMensaje_aviso";
    capa.style.position   = "absolute";
    
    if(top > 0){
        capa.style.top        = top + "px";
    }else{
        capa.style.bottom     = (top*-1) + "px";
    }    
    capa.style.left       = left + "px";
    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_mensaje";
    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";

    if(ns6){
        //var texto = document.createTextNode(texto)

        //capa_texto.appendChild(texto);
        
        capa_texto.innerHTML = texto;

    }else{
        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";
        }else if(opc == 3){
            icono = "icono_mensaje_aviso_impresora_anim";
        }

        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 elimina_mensaje_aviso(){
    
    var capa_mensaje = document.getElementById("idMensaje_aviso");

    if(capa_mensaje){
        document.body.removeChild(capa_mensaje);
    }
}

// -----------------------------------------------------------

function imprimir_resultado(){

    var ret = imprimir_detalle(this);
    
	var tiempo = 2000;
	
	if(ret){
	    parent.cambia_texto_mensaje_aviso("Imprimiendo informe, espere por favor ...");
	    tiempo = 6000;
	}else{
        parent.cambia_texto_mensaje_aviso("Cancelando impresión, espere por favor ...");
	}
	
	setTimeout("parent.habilitar_contexto()", tiempo);
}

function crearInput(nombre, valor, tipo) {

    var newInput = document.createElement("input");

    newInput.type   = tipo;
	newInput.name   = nombre;
	newInput.id		= nombre;
	newInput.value  = valor;

	return newInput;
}

// 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 cargar_poblaciones(id_provincia, tabindex){

    if(! tabindex){
        tabindex = 13;
    }
    
    conexion_gestionar_datos_combo_generico(id_provincia,
										    "usp_TM_Municipios_cmb(" + id_provincia + ")",
										    "cmb_municipios",
										    "tabindex='" + tabindex + "' title='* Seleccione una ciudad, por favor'",
										    null,
										    null,
										    "-1");
}


