﻿var last_link_selected = -1;

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();    
}

// 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 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");
}

function show_quienes_content(id) {
    $(".quienes_header").hide();
    $(".quienes_content").hide();
    $("#quienes_header_" + id).show();
    $("#quienes_content_" + id).show();
    $('#scrollbar1').tinyscrollbar();
}

function CambiarFoto(nombre) {
    document.images["foto"].src = nombre;
}

function change_concess_info(conces, txt, animation) {
    if (animation == null) animation = true;
    if (last_link_selected != conces) {
        if (last_link_selected > 0) $('#past_con_detalles_lnk_' + last_link_selected).toggleClass("link_selected");
        for (var i = 1; i < 7; i++) {
            if (i == conces) {
                $('#past_con_detalles_lnk_' + i).toggleClass("link_selected");
                last_link_selected = i;
            }
        }
        if (animation) {
            $('#past_con_detalles').hide("slide", 'slow', function() {
                $('#past_con_detalles_txt').html(txt);
                $('#past_con_detalles').show("slide", 'slow');
            });
        }
        else $('#past_con_detalles_txt').html(txt);
    }
}

function set_enviar_info_hover() {
    $("#txt_send_info").css("color", "#D0001B");
    $("#img_send_info").attr("src", "img/bullet1.png");
}

function unset_enviar_info_hover() {
    $("#txt_send_info").css("color", "Black");
    $("#img_send_info").attr("src", "img/bullet2.png");
}

function change_lang(lang) {
    var current_location = String(document.location);
    if (current_location.indexOf("?") >= 0) current_location = current_location.substring(0, current_location.indexOf("?"));
    document.location = current_location + "?lang=" + lang;
}

function load_models() {
    var marca = $("#selMarcas_mini").val();
    var modelo = $("#model_selected").val();
    if(modelo=="") modelo = "Seleccionar Modelo";
    $("#divComboModelo_mini").load("inc/combo_models.asp", {id:"selModelo_mini" , paramMarca: marca, paramModelo: modelo });
}


function advanced_search() {
    var lang_txt = $("#txt_lang_advanced").val();
    $("#tbl_equip").show();
    $("#tbl_link_equip_plus").show();
    $("#tbl_equip_plus").hide();
    $("#tbl_equip_advance").hide();
    $("#tbl_banner_lateral").hide();
    $("#tr_altura").css("height", 42);
    $("#buscador").css("width", 795);
    $("#td_advancec_search_link").html("<a href='javascript:basic_search();' title='" + lang_txt + "'>" + lang_txt + "</a>");
    $("#advanced_search").val("1");
}

function basic_search() {
    var lang_txt = $("#txt_lang_basic").val();
    $("#tbl_link_equip_plus").hide();
    $("#tbl_equip_plus").hide();
    $("#tbl_equip").hide();
    $("#tbl_equip_advance").hide();
    $("#tr_altura").css("height", 42);
    $("#buscador").css("width", 450);
    $("#tbl_banner_lateral").show();
    $("#td_advancec_search_link").html("<a href='javascript:advanced_search();' title='" + lang_txt + "'>" + lang_txt + "</a>");
    $("#advanced_search").val("0");
    equip_basic();
}

function equip_plus() {
    $("#td_link_equip_plus").html("<a href='javascript:equip_basic()'><b>"+$("#txt_lang_less").val()+"</b></a>");
    $("#tr_altura").css("height", 280);
    $("#tbl_equip_plus").show();
    $("#equip_plus").val("1");
}

function equip_basic() {
    $("#td_link_equip_plus").html("<a href='javascript:equip_plus()'><b>" + $("#txt_lang_more").val() + "</b></a>");
    $("#tr_altura").css("height", 42);
    $("#tbl_equip_plus").hide();
    $("#equip_plus").val("0");
}

function see_all() {
    $("#buscador-mini").get(0).setAttribute('action', 'search_list.asp?busc=mini');
    $("#buscador-mini").submit();
}

function check() {
    var tipo, car_id, from_phone, from_nombre, from_mail, to_nombre, to_mail, coment;
    tipo = $("#send_tipo").val();
    car_id = $("#car_id").val();
    from_nombre = $("#fromNombre").val();
    from_mail = $("#fromEmail").val();
    coment = $("#body").val();
    $('#h_leyenda').hide();
    
    if (tipo == "1") {
        from_phone = $("#fromPhone").val();
        to_nombre = "nobody"
        to_mail = "nomail@nomail.com"
    }
    else {
        to_nombre = $("#toNombre").val();
        to_mail = $("#toEmail").val();
        from_phone = "000000000"
    }

    if (from_nombre != "" && from_mail != "" && to_nombre != "" && to_mail != "" && coment != "" && from_phone != "") {
        $('#h_leyenda').css("color", "blue");
        $('#h_leyenda').html("Procesando...");
        $('#h_button').hide("slow");
        $('#h_leyenda').show("slow");

        $.post("send_mail.asp", { car_id: car_id, send_tipo: tipo, fromNombre: from_nombre, fromEmail: from_mail, fromPhone: from_phone, body: coment, toEmail: to_mail, toNombre: to_nombre }, function(data) {
            if (data == "TRUE") {
                //window.close();
                $('#h_leyenda').hide("slow", function() {
                    $('#h_leyenda').css("color", "green");
                    $('#h_leyenda').html("Operación realizada con éxito. Puede cerrar esta ventana pulsando <a href='javascript:window.close();'>aquí</a>.");
                    $('#h_leyenda').show("slow");
                });

            }
            else {
                $('#h_leyenda').hide("slow", function() {
                    $('#h_leyenda').css("color", "red");
                    $('#h_leyenda').html("Se ha producido un error al procesar su solicitud. Por favor, vuelva a intentarlo pasados unos minutos. Gracias. <a href='javascript:check();'>Reintentar</a>");
                    $('#h_leyenda').show("slow");
                });
            }
        });
        //$("#form_send").submit();
    }
    else alert("Debe rellenar los campos obligatorios marcados con (*)");
}


// 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;
}

// 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");
}
