/**
 * General
 *
 * Script personalizado de uso general y global en la aplicación.
 *
 * @author Alex Barrios <alex@alexertech.com>
 * @version 10.04.2008 10:50:18
 * @package js
 */

// menu() ~ realiza las transiciones AJAX utilizando jQuery

function updateView(opc) {

    $.ajax({
        url: opc,
        dataType: 'html',
        cache:false,
        beforeSend: function () {
            $("#ajaxResp").empty();
            $("#ajaxResp").append('<div id="preload"><img src="images/preload.gif"><br>cargando</div>');
        },
        success: function (html) {
            $("#preload").remove();
            $("#ajaxResp").empty();
            $("#ajaxResp").append(html);
        }
    });
}

// contactos() ~ envío de datos de contacto

function contactos() {

    // ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    email_str=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;

    var ok       = 0;
    var nombre   = $("#nombre").val();
    var email    = $("#email").val();
    var mensaje  = $("#msg").val();
    var telefono = $("#telefono").val();
    var code     = $("#code").val();


    // Verificar el correo con una expresión regular
    if (email.length > 0) {
        if (!email_str.test(email)) {
            alert('El formato del campo email no es valido');
            ok = 1;
        }
    } else {
        alert('Debe introducir un email');
        ok = 1;
    }

    if (nombre.length == 0) {
        alert('Debe introducir un nombre');
        ok = 1;
    }

    if (mensaje.length == 0) {
        alert('Debe introducir un mensaje');
        ok = 1;
    }

    if (ok == 0) {
        $.ajax({
            type: "POST",
            dataType: 'html',
            url : "lib/ajax.php?acc=1",
            data: "nombre="+nombre+"&email="+email+"&code="+code+"&mensaje="+mensaje+"&telefono="+telefono,
            cache:false,
            beforeSend: function () {
              $("#conForm").empty();
              $("#conForm").append('<br><br><p align="center"><img src="images/i_proceso.png"> <br> Enviando mensaje... <br> Por favor espere un momento</p>');
            },
            success: function(html){
              $("#conForm").empty();
              $("#conForm").append(html);
            }
        });
    } else
        return false;

}


// preloadImage() ~ Precarga de imágenes

function preloadImage(imageSrc) {
    var img = new Image();
    $(img).load(function (){$(this).hide();}).attr('src', imageSrc);
}

// Controladores de los Scroll
function moveUp() {

    var maxScroll = $(".container").attr("scrollHeight")+$(".innerText").height();
    $(".container").animate({scrollTop: (maxScroll / $(".container").attr("scrollHeight")) }, 1000);

}

function moveDw() {

    $(".container").animate({scrollTop: $(".innerText").height()-20 }, 1000);

}


// formatoCampo() ~ permite definir que caracteres puede introducir
// un usuario en un campo. Para utilizarlo:
// onKeyPress="return(formatoCampo(this,event,1))"

function formatoCampo(fld,e,t) {

    // Variables
    var aux = aux2 = '';
    var i = j = 0;
    var strCheck = null;

    // solo numeros
    if (t==1)
        var strCheck = '0123456789';

    // solo letras
    if (t==2)
        var strCheck = 'AaBbCcDdEeFfGgHhIiJjKkLlÑñNnMmOoPpQqRrSsTtUuVvWwXxYyZzáÁéÉíÍóÓúÚ ';

    // telefonos
    if (t==3)
        var strCheck = '0123456789-ext';

    // moneda & numerico con decimales
    if (t==4)
        var strCheck = '0123456789,.';

    // Dimensiones (800x600)
    if (t==5)
        var strCheck = '0123456789x';

    // Obtiene el codigo de la letra precionada
    var whichCode = (window.Event) ? e.which : e.keyCode;

    // Comienza la comprobación
    if (whichCode == 13 || whichCode == 8)
        return true; // Permitir Tecla Enter

    if (t==4 && (whichCode == 44 || whichCode == 46))
        return true; // Permitir Comas y puntos

    if (whichCode == 0)
        return true; // Permitir Tabulador

    // Consigue el valor del codigo de tecla
    key = String.fromCharCode(whichCode);

    // Verifica el caracter con los seleccionados en strCheck
    if (strCheck.indexOf(key) == -1)
        return false;

    // Retorna el caracter al campo
    fld.value += aux2.charAt(i);
}





// $(document).ready ~ Acciones a ejecutra en la carga de la página

$(document).ready(function() {

    preloadImage('images/rounded.png');
    preloadImage('images/arrows.png');
    preloadImage('images/slogan.jpg');
    preloadImage('images/slogan2.jpg');
    preloadImage('images/servicios.jpg');

    if ($.browser.msie && $.browser.version < 7) {

        $(function() {
            $("#mensaje").dialog({
                bgiframe  : true,
                resizable : false,
                width     : 400,
                modal     : true,
                overlay   : {
                    backgroundColor : '#000',
                    opacity         : 0.5
                },
                buttons   : {
                    'Deseo continuar.' : function() {
                        $(this).dialog('close');
                        $('#login').focus();
                    }
                }
            });
        });

    } else {
        $('#login').focus();
    }

});

