﻿$(document).ready(function()
    {
        function check_cedula(cedula)
        {
            var regx, valor;
            regx = /^([0-9])*$/;
            cedula = $.trim(cedula);
            if((cedula.length < 5) || (cedula.length > 8)){
                $("#msg_cedula").html("Colocar una cédula válida.");
                $("#msg_cedula").fadeIn("fast");
                $("#cedula").css("background","#FF5B60");
                $("#nose").attr("value",0);
            }else{
                if(regx.test(cedula) == false){
                    $("#msg_cedula").html("Cédula invalida.");
                    $("#msg_cedula").fadeIn("fast");
                    $("#cedula").css("background","#FF5B60");
                    $("#nose").attr("value",0);
                }else{
                    $.getJSON("../../controllers/usuario/check_cedula.php",{
                        cedula:cedula
                    },
                    function(data)
                    {
                        if(data)
                        {
                            $("#msg_cedula").html("Cédula no registrada.");
                            $("#msg_cedula").fadeIn("fast");
                            $("#cedula").css("background","#FF5B60");
                            $("#nose").attr("value",0);
                            return false;
                        }
                        else
                        {
                            $("#msg_cedula").html("*");
                            $("#msg_cedula").fadeIn("fast");
                            $("#cedula").css("background","#FFFFFF");
                            $("#nose").attr("value",1);
                            return true;
                        }
                    });					
                }
            }
			return false;
        }
        function olvido_clave(data)
        {
            if(data)
            {
                $("#msg_alert").attr('class', 'alert_msg4');
                $("#msg_alert").html("");
                $("#msg_alert").html("Clave enviada a su correo de forma satisfactoria.");
                $("#msg_alert").fadeIn();
                setTimeout('document.location.href="../../index.php";', 3000);
            }
            else
            {
                $("#msg_alert").attr('class', 'alert_msg3');
                $("#msg_alert").html("");
                $("#msg_alert").html("No se puedo completar el proceso, intente mas tarde.");
                $("#msg_alert").fadeIn();
            }
            return false;
        }
        $("#form1").submit(function()
        {
            check_cedula($("#cedula").val());
            if($("#nose").val() == 1)
            {
                $.getJSON("../../controllers/olvido_clave/olvido_clave.php",{
                    cedula:$("#cedula").val()
                    },olvido_clave);
            }
            return false;
        });
  
        $("#cedula").blur(function()
        {
            var cedula = $("#cedula").val();
            var regx = /^([0-9])*$/;
            if(cedula.length < 5 || cedula.length > 8)
            {
                $("#msg_cedula").html("*");
                $("#msg_cedula").html("* Verifique el campo");
            }
            else if(regx.test(cedula) == false)
            {
                $("#msg_cedula").html("*");
                $("#msg_cedula").html("* Verifique el campo");
            }
            check_cedula(cedula);
        });
  
        $("#volver").click(function()
        {
            location.href="../../index.php";
            return false;
        });
    });
