﻿function show_loading() {    
    $.blockUI.defaults.message = $('#displayBox');
    $.blockUI.defaults.css = {
        top: ($(window).height() - 32) / 2 + 'px',
        left: ($(window).width() - 32) / 2 + 'px', width: '32px',
        backgroundColor: 'transparent',
        border: 'none'
    };
    $.blockUI();
}

function submit_loading(pagina, id) {
    show_loading();
    $("#pagina").val(pagina);
    $("#" + id).submit();
}

/*Esto es para el captcha*/

function InsertSwf(){
	try{					
		swfobject.embedSWF("swf/900x124.swf", "900x124", "900", "124", "9.0.0");
	}
	catch(e){
		$("#900x124").html("<OBJECT CLASSID='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='900' height='124' \
			CODEBASE='http://active.macromedia.com/flash4/cabs/swflash.cab#version=4,0,0,0'> \
			<PARAM NAME='MOVIE' VALUE='../swf/900x124.swf'> \
			<PARAM NAME='PLAY' VALUE='true'> \
			<PARAM NAME='LOOP' VALUE='true'> \
			<PARAM NAME='QUALITY' VALUE='high'> \
			<EMBED SRC='../swf/900x124.swf' width='900' height='124' PLAY='true' LOOP='true' QUALITY='high' \
			PLUGINSPAGE='http://www.macromedia.com/shockwave/download/index.cgi? P1_Prod_Version=ShockwaveFlash'> \
			</EMBED></OBJECT>")
	}
}

function RefreshImage(valImageId) {
    var objImage = document.images[valImageId];
    if (objImage == undefined) {
        return;
    }
    var now = new Date();
    objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

// Pasar un numero a dos decimales
function RoundNumber(campo) {
    eval("numero=document.formulario." + campo + ".value");
    numero = Round(numero);
    eval("document.formulario." + campo + ".value=numero");
}


// Pasar un numero a dos decimales
function Round(numero) {
    return Math.round(numero * 100) / 100;
}

// Poner el ID del enlace a 0
function AnulaEnlace(tipo, valor, valorid) {
    if (tipo == true) {
        if (eval("document.formulario." + valor + ".value.substring(0,7)!='http://'"))
            eval("document.formulario." + valor + ".value='http://'+document.formulario." + valor + ".value;");
    }
    else eval("document.formulario." + valor + ".value=''");

    eval("document.formulario." + valorid + ".value='0'");
}

// Poner el campo a Zero si no hay nada
function ValorZero(campo) {
    if (eval("document.formulario." + campo + ".value") == "") eval("document.formulario." + campo + ".value=0");
}

// Abrir Quadro Dialogo Registros
function OpenDialogRegistros(tipo, origen) {
    VentanaHija = window.open("find_registros.asp?tipo=" + tipo + "&origen=" + origen, "DialogRegistros", "width=550,height=350,top=100,left=100,scrollbars=yes,toolbar=no");
}

// Abrir Quadro Dialogo Ficheros
function OpenDialogFiles(origen) {
    if (origen.substring(0, 7) == 'adjunto')
        VentanaHija = window.open("find_ficheros.asp?origen=" + origen + "&tipo=adjunto", "DialogFiles", "width=710,height=525,top=20,left=40,scrollbars=yes,toolbar=no");
    else
        VentanaHija = window.open("find_ficheros.asp?origen=" + origen + "&tipo=imagen", "DialogFiles", "width=710,height=525,top=20,left=40,scrollbars=yes,toolbar=no");
}

function OpenShowAll() {
    VentanaHija = window.open("vo_lista_todos.asp", "ShowAll", "width=875,height=525,top=20,left=40,scrollbars=yes,toolbar=no");
}

// Abrir Quadro Dialogo Enlaces
function OpenDialogLinks(tipo, origen) {
    VentanaHija = window.open("find_enlaces.asp?tipo=" + tipo + "&origen=" + origen, "DialogLinks", "width=710,height=525,top=20,left=40,scrollbars=yes,toolbar=no");
}

// Enviar el nombre del archivo seleccionado
function AceptarFichero(destino) {
    eval("opener.formulario." + destino + ".value='" + document.visor.src + "'");
    window.close();
}

function CambiarFoto(nombre) {
    document.images["foto"].src = nombre;
}

// Accion de cambiar Carpeta
function VerImagen(imagen) {
    ext = imagen.substring(imagen.length - 4, imagen.length);

    if (ext == ".gif" || ext == ".jpg") document.visor.src = imagen;
    else document.visor.src = "../img/null.gif";
}

function NumeroDecimal() {
    k = false;

    tecla = window.event.keyCode;
    if (tecla == 46) k = true;
    if (tecla >= 48 && tecla <= 57) k = true;

    if (k == false) window.event.keyCode = 0;
}
function Numeros(event) {
    tecla = event.keyCode;
    if (tecla < 48 || tecla > 57) window.event.keyCode = 0;
}

// Rutina de Validación de Formularios
// ===================================
//  Ejemplo:
//		if (ValidaForm("formulario",form_campo,form_valor,form_tipo) == 0)
//			document.formulario.submit();
//
//	Parámetros:
//		1) Nombre del Formulario.
//		2) Matriz con los mensajes a imprimir si falla ese campo.
//		3) Matriz con los nombres de los campos en el formulario.
//		4) Matriz con los tipos de campos:
//				"t":		Campo de texto.
//				"n","nxx":	Campo numérico. "xx" indica el tamaño del campo.
//				"e":		Campo de email.
//				"l":		Campo "Select".
//				"r":		Campo "Radio".
//				"m","mxx":	campo "TextArea". "xx" indica la longitud máxima.
//				"f":		Campo "Fecha".
//
function ValidaForm(form_nombre, form_campo, form_valor, form_tipo) {
    var docuform = "document." + form_nombre;
    mensaje = "Por favor, rellenar: \n";
    error = 0;

    for (i = 0; i < form_campo.length; i++) {
        fallo = 0;
        tipo = form_tipo[i].substring(0, 1);
        largo = parseInt(form_tipo[i].substring(1, 5));
        if (isNaN(largo)) largo = 0;

        switch (tipo) {
            case "t":
                if (eval(docuform + "." + form_valor[i] + ".value") == "") fallo = 1;
                break;
            case "n":
                if (isNaN(eval(docuform + "." + form_valor[i] + ".value")) ||
					eval(docuform + "." + form_valor[i] + ".value") == "" ||
					(eval(docuform + "." + form_valor[i] + ".value.length") != largo && largo != 0)) fallo = 1;
                break;
            case "e":
                valor = eval(docuform + "." + form_valor[i] + ".value");
                if (valor.indexOf("@") < 0 || valor.indexOf(".") < 0 || valor.indexOf(" ") >= 0) fallo = 1;
                break;
            case "l":
                if (eval(docuform + "." + form_valor[i] + ".selectedIndex") == 0) fallo = 1;
                break;
            case "r":
                fallo = 1;
                valor = eval(docuform + "." + form_valor[i] + ".length");

                for (f = 0; f < valor; f++)
                    if (eval(docuform + "." + form_valor[i] + "[" + f.toString() + "].checked") == true) fallo = 0;
                break;
            case "m":
                if (eval(docuform + "." + form_valor[i] + ".value.length") > largo && largo > 0) fallo = 1;
                break;
            case "f":
                largo = eval(docuform + "." + form_valor[i] + ".value.length");
                valor = eval(docuform + "." + form_valor[i] + ".value");

                if (largo != 0) {
                    if (largo != 10) fallo = 1;
                    if (valor.substring(2, 3) != '\/' || valor.substring(5, 6) != '\/') fallo = 1;
                    for (f = 0; f < 10; f++) {
                        char = valor.substring(f, f + 1);

                        if (f != 2 && f != 5) {
                            if (isNaN(parseInt(char))) fallo = 1;
                        }
                        else {
                            if (char != "\/") fallo = 1;
                        }
                    }
                    if (fallo == 0) {
                        dia = valor.substring(0, 2);
                        mes = valor.substring(3, 5);
                        any = valor.substring(6, 10);

                        if (dia < "01" || dia > "31" || mes < "01" || mes > "12" || any < "1980" || any > "2050") fallo = 1;
                    }
                }
                break;
        }

        if (fallo) mensaje += (error++ != 0 ? ", " : "") + form_campo[i];
    }

    if (error) alert(mensaje + ".");
    return error;
}
