var fuenteMAX = 1.4;
var fuenteMIN = 1;
var fuenteActual = 1;
var navegador_soportado = false;
var version_soportada = false;
var mostrado_mensaje_navegador_version = false;

var urlVideo;

// Deteccion del navegador
var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "Navegador desconocido";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "version desconocida";
			this.OS = this.searchString(this.dataOS) || "SO desconocido";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari",
				versionSearch: "Version"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				   string: navigator.userAgent,
				   subString: "iPhone",
				   identity: "iPhone/iPod"
		    },
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]

	};

BrowserDetect.init();

// VARIABLES CALCULO IIVTNU

var mostrarPrimeraCuotaIIVTNU = false;
var mostrarSegundaCuotaIIVTNU = false;
var mostrarTerceraCuotaIIVTNU = false;

function aumentarTexto() {

	if (fuenteActual < fuenteMAX) {

		fuenteActual += 0.1;

		var pantalla = document.getElementById('pantalla');
		pantalla.style.fontSize = fuenteActual + "em";

	}
}

function disminuirTexto() {

	if (fuenteActual > fuenteMIN) {

		fuenteActual -= 0.1;

		var pantalla = document.getElementById('pantalla');
		pantalla.style.fontSize = fuenteActual + "em";
	}
}

function agregarFavoritos(url, titulo, textoNoSoportado) {

	if (window.sidebar) { // Mozilla Firefox
		window.sidebar.addPersistentPanel(titulo, url, "");
	} else if (window.external) { // IE
		window.external.AddFavorite(url, titulo);
	} else {
		alert(textoNoSoportado);
	}
}

function esTeclaValida(e){
    var tecla = (document.all) ? e.keyCode : e.which;
    if (!(tecla >= 48) || tecla > 57){
    
            if (tecla == 44 || tecla == 0 || tecla == 8 || tecla == 13){
            	return true;
            }
            
            return false;
    }
    
    return true;
}

function imprimir(icono,css,texto_cerrar,texto_imprimir) {

    var nodo = document.getElementById('imprimir_sin_columnas');
    if (nodo == null){
        nodo = document.getElementById('cuerpo');
    }
    var html = "";
    
    if (nodo != null){
        html = nodo.innerHTML;
    }
    
    // HTML
    var contenido = "<html xmlns=\"http://www.w3.org/1999/xhtml\"";
    contenido += "xmlns:ui=\"http://java.sun.com/jsf/facelets\"";
    contenido += "xmlns:h=\"http://java.sun.com/jsf/html\"";
    contenido += "xmlns:f=\"http://java.sun.com/jsf/core\" xml:lang=\"en\" lang=\"en\"";
    contenido += "xmlns:a4j=\"http://richfaces.org/a4j\"";
    contenido += "xmlns:rich=\"http://richfaces.org/rich\"";
    contenido += "xmlns:contenidos=\"http://web.suma.es/contenidos\"";
    contenido += "xmlns:funciones=\"http://web.suma.es/funciones\"";
    contenido += "xmlns:c=\"http://java.sun.com/jstl/core\">";
    
	    // HEAD
	    contenido += "<head>"; 
		    contenido += "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />";
		    contenido += "<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"" + icono + "\"></link>";;
		    contenido += "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + css + "\"></link>";
	    contenido += "</head>";
	    
	    // BODY
	    contenido += "<body>";
	    	contenido += html;
	    	contenido += "<form>";
	    	contenido += "<input type=\"button\" value=\"" + texto_cerrar + "\" onclick=\"window.close();\">";
	    	contenido += "<input type=\"button\" value=\"" + texto_imprimir + "\" onclick=\"window.print();\">";
	    	contenido += "</form>"
	    contenido +="</body>";
    
    contenido += "</html>";
    
    var ventana = window.open("","","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,left=0,top=0;");
    ventana.document.open();
    ventana.document.write(contenido);
    ventana.print();
    ventana.document.close();
    ventana.close();
}

function cambiarColor(color) {
	nodo = document.getElementsByTagName("body");
	nodo[0].style.backgroundColor = color;
}

function cambiarColorYTitulo(color, titulo) {
	document.title = titulo;
	cambiarColor(color);
}

function loadComplete(titulo) {

	document.title = titulo;
}

function navegarURLExterna(url) {

	if (url.length > 4) {
		if (url.substring(0, 4) == 'http') {

			var ventana = window
					.open(
							url,
							"ventana1",
							"toolbar=yes, location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, resizable=yes, left=0, top=0");
		} else {
			if (url != 'anular'){
				// Es un correo, aparece asistente
				parent.location.href = url;
			}
		}
	}
}

function redireccionar(url) {
	if (url == 'ERROR' || url == '') {

	} else {

		elem = document.getElementById('pantalla:nifDoc');

		if (elem.value != '') {
			location.href = url;
		}

	}
}

function ocultarModal() {
	var w_control = document.getElementById('pantalla:panel_modalContainer');
	w_control.style.display = "none";
}

function mostrarModal() {
	var w_control = document.getElementById('pantalla:panel_modalContainer');
	w_control.style.display = "block";
}

function ValidarNavegador(){
	navegador_soportado = false;
	version_soportada = false;

	// IE 8 o superior
	if (BrowserDetect.browser == 'Explorer'){
		navegador_soportado = true;
		if (BrowserDetect.version >= 8){
			version_soportada = true;
		}
	}
	// Firefox 3 o superior
	else if (BrowserDetect.browser == 'Firefox'){
		navegador_soportado = true;
		if (BrowserDetect.version >= 3){
			version_soportada = true;
		}
	}
	// Chrome 3 o superior
	else if (BrowserDetect.browser == 'Chrome'){
		navegador_soportado = true;
		if (BrowserDetect.version >= 3){
			version_soportada = true;
		}
	}
	// Safari 4 o superior
	else if (BrowserDetect.browser == 'Safari'){
		navegador_soportado = true;
		if (BrowserDetect.version >= 4){
			version_soportada = true;
		}
	}
	// Opera 9 o superior
	else if (BrowserDetect.browser == 'Opera'){
		navegador_soportado = true;
		if (BrowserDetect.version >= 9){
			version_soportada = true;
		}
	}
}

function validarNavegador(textoNavegadorNoSoportado,textoVersionNoSoportada,mostrado){
	
	// Si no hemos mostrado el mensaje de navegador... lo mostramos
	if(!mostrado){
		// ¿No hemos mostrado aun el mensaje
		if (!mostrado_mensaje_navegador_version){
			// Validamos el navegador y la version
			ValidarNavegador();
			if (!navegador_soportado){
				alert(textoNavegadorNoSoportado);
				mostrado_mensaje_navegador_version = true;
			}
			else if (!version_soportada){
				alert(textoVersionNoSoportada);
				mostrado_mensaje_navegador_version = true;
			}
		}
	}
	
}

function valida_longitud(objeto, maximo, alertar){
    num_caracteres = objeto.value.length;
    if (num_caracteres>=maximo)
    {
        if (alertar!='')
            alert(alertar);
        objeto.focus();
        return false;
    }
    else
    {
        return true;
    }
}

function iniciarCargando(cliente,progres) {
	progres = setInterval(function() {
		cliente.setValue(cliente.getValue() + 1);
		if (cliente.getValue() > 1000) {
			cliente.setValue(0);
		}
	}, 200);
}

function cancelarCargando(cliente,progres) {
	clearInterval(progres);
	cliente.setValue(0);
}

function mostrarCargando(cliente, progres, pe_id_ocultar, pe_id_mostrar) {
	try{
		document.getElementById(pe_id_mostrar).style.display = 'block';
		document.getElementById(pe_id_ocultar).style.display = 'none';
		cancelarCargando(cliente,progres);
		iniciarCargando(cliente,progres);
	}catch(err){
		;
	}
}

