﻿$(document).ready(function() {


    
    $(window).scroll(function() {
        $("#loading").css("top", $(window).scrollTop());
    });

    $('#msisdn').focus(function() {
        $(this).val('');
    });

    $("#clave").keypress(function(e) {
        if (e.which == 13) {
            ajax_resquest('buscar', { metodo: 'buscar', i: 1, t: 18, clave: $('#clave').val(), fid: +$("#fabricante").val(), mtid: +$("#modelotelefono").val() });
            $('#clave').val('');            
        }
    });


    $('#msisdn').blur(function() {
        if (!(/^\d{10}$/.test($(this).val()))) {
            alert("Verifica que el número de móvil sea correcto, debes escribirlo sin guiones ni espacios.");
            $(this).val("Mi número...");
            $('#session').removeAttr('checked');
            return false;
        }
    });


    $("#loading").bind("ajaxSend", function() {
        $(this).show();
        $(this).css("background-color", '#ddd');
        $(this).html("Cargando...")
    }).bind("ajaxSuccess", function() {
        $(this).hide();
    });



    $('#faqs').click(function() {

        $('#dialogo').dialog();

    });

    $('#session').click(function() {
        if ($('#session').is(':checked')) {

            if (!(/^\d{10}$/.test($("#msisdn").val()))) {
                alert("Recuerde seleccionar todos sus datos....");
                return false;
            }

            if (($("#fabricante").val() <= 0) && ($("#modelotelefono").val() <= 0)) {
                alert("Recuerde seleccionar todos sus datos....");
                return false;
            }

            ajax_resquest('', { metodo: 'cookie', cookie: 'create', fid: +$("#fabricante").val(), mtid: +$("#modelotelefono").val(), nt: + $("#msisdn").val() });
        } else {
        ajax_resquest('', { metodo: 'cookie', cookie: 'delete', fid: +$("#fabricante").val(), mtid: +$("#modelotelefono").val(), nt:  + $("#msisdn").val() });
        }
    });

    $("select,input,a").bind("ajaxSend", function() {
        $(this).attr('disabled', 'disabled');
    }).bind("ajaxSuccess", function() {
        $(this).removeAttr('disabled');
    });


    $("#loading").bind("ajaxSend", function() {
        $(this).show('fast');
    }).bind("ajaxSuccess", function() {
        $(this).hide('fast');
    });


    $('#fabricante').change(function() {
        $("#modelotelefono").html('');
        addOption('#modelotelefono', { value: 0, text: '- Modelo Teléfono - ' });
        if ($(this).val() > 0)
            ajax_resquest('none', { metodo: 'modelotelefono', fid: + $("#fabricante").val(), mtid: + $("#modelotelefono").val() }, '#modelotelefono');
    });

    $('#modelotelefono').change(function() {
        if ($(this).val() > 0)
            ajax_resquest('home', {metodo:'modelotelefono,destacado,menu,banner', fid: + $("#fabricante").val(), mtid: + $("#modelotelefono").val() });
    });


    $('#buscar').click(function() {
        var pclave = $('#clave').val()
        $('#clave').val('');
        ajax_resquest('buscar', { metodo: 'buscar', i: 1, t: 18, clave: pclave, fid: + $("#fabricante").val(), mtid: + $("#modelotelefono").val() });
    });

    document.onkeypress = stopRKey;
    ajax_resquest('home', { metodo: 'cookie,tipoitem,fabricante,modelotelefono,destacado,menu,banner', cookie: 'ready', fid: +$("#fabricante").val(), mtid: +$("#modelotelefono").val() }, "#modelotelefono");


});