function crea_cumpleano(){

    var encender = {

        message: null,

        init : function (){

            $.get("cumpleano.php", function (data){

                $(data).modal({

                    closeHTML: "<a href='#' title='Close' class='modal-close'>X</a>",

                    position: ["4%",],

                    overlayId: 'form-overlay',

                    containerId: 'cumpleano-container',

                    onOpen: encender.open,

                    onShow: encender.show,

                    onClose: encender.close

                });

            });

        },

        open : function (dialog){

            var h = 530;



            var title = $('#cumpleano-container .form-title').html();

            $('#cumpleano-container .form-title').html('Cargando...');



            dialog.overlay.fadeIn(200, function () {

                dialog.container.fadeIn(200, function () {

                    dialog.data.fadeIn(200, function () {

                        $('#cumpleano-container .form-content').animate({

                            height: h

                        }, function () {

                            $('#cumpleano-container .form-title').html(title);

                            $('#cumpleano-container  form').fadeIn(200, function () {

                                $('#cumpleano-container #cumpleano-nombre').focus();



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

                                    $('#cumpleano-container .form-button').each(function () {

                                        if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {

                                            var src = RegExp.$1;

                                            $(this).css({

                                                backgroundImage: 'none',

                                                filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'

                                            });

                                        }

                                    });

                                }

                            });

                        });

                    });

                });

            });

        },

        show : function (dialog){

            $('#cumpleano-container .form-send').click(function (e) {

                e.preventDefault();



                if ( $('#iframe').contents().find('#name_archivo').val() ) {

                    $('#cumpleano-container form #foto').attr(

                        'value',

                        $('#iframe').contents().find('#name_archivo').val()

                    );

                }



                if (encender.validar()) {

                    var msg = $('#cumpleano-container .form-message');



                    msg.fadeOut(function (){

                        msg.removeClass('form-error').empty();

                    });



                    $('#cumpleano-container .form-title').html('Felicitando a...');

                    $('#cumpleano-container form').fadeOut(200);

                    $('#cumpleano-container .form-content').animate({

                        height: '80px'

                    }, function () {

                        $('#cumpleano-container .form-loading').fadeIn(200, function (){

                            $.ajax({

                                url : 'cumpleano.php',

                                data : $('#cumpleano-container form').serialize() + '&action=send',

                                type : 'post',

                                cache : false,

                                dataType : 'html',

                                success : function (data){

                                    $('#cumpleano-container .form-loading').fadeOut(200, function (){

                                        $('#cumpleano-container .form-title').html('Felicitacion creada');

                                        msg.html(data).fadeIn(200);

                                    });

									setTimeout(function(){

									$('.modal-close').trigger('click');

									},3000);

                                },

                                error : encender.error

                            });

                        });

                    });

                }

                else{

                    if ($('#cumpleano-container .encender-peticion:visible').lenght > 0){

                        $('#cumpleano-container .form-message div').fadeOut(200, function () {

                            $('#cumpleano-container .form-message div').empty();

                            encender.showError();

                            $('#cumpleano-container .form-message div').fadeIn(200);

                        });

                    }

                    else{

                        $('#cumpleano-container .form-message').animate({

                            height: '30px'

                        }, encender.showError);

                    }

                }

            });

        },

        close : function (dialog){

            $('#cumpleano-container .form-message').fadeOut();

            $('#cumpleano-container .form-title').html('Cerrando');

            $('#cumpleano-container form').fadeOut(200);

            $('#cumpleano-container .form-content').animate({

                height : 40

            }, function  (){

                dialog.data.fadeOut(200, function (){

                    dialog.container.fadeOut(200, function (){

                        dialog.overlay.fadeOut(200, function (){

                            $.modal.close();

                        });

                    });

                });

            });



            location.reload();

        },

        error : function (xhr){

            alert(xhr.statusText);

        },

        validar : function (){

            encender.message = '';



            if (!$('#cumpleano-container #cumpleano-nombre-cliente').val()){

                encender.message = 'Debe llenar todos los campos marcados con *. ';

            }

			/*

            if (!$('#cumpleano-container #cumpleano-apellido-cliente').val()){

                encender.message = 'Debe llenar todos los campos marcados con *. ';

            }

			*/

            var email = $('#cumpleano-container #cumpleano-email-cliente').val();

            if (!email) {

                    encender.message = 'Debe llenar todos los campos marcados con *. ';

            }

            else {

                if (!encender.validarEmail(email)) {

                    encender.message += 'El E-mail no es correcto. ';

                }

            }



            if (!$('#cumpleano-container #cumpleano-nombre-felicitado').val()){

                encender.message = 'Debe llenar todos los campos marcados con *. ';

            }

			

            var email2 = $('#cumpleano-container #cumpleano-email-felicitado').val();

            if (!email2) {

                    encender.message = 'Debe llenar todos los campos marcados con *. ';

            }

            else {

                if (!encender.validarEmail(email2)) {

                    encender.message += 'El E-mail del felicitado no es correcto. ';

                }

            }



            if (!$('#cumpleano-container #cumpleano-fecha').val()){

                encender.message = 'Debe llenar todos los campos marcados con *. ';

            }



            if (!$('#cumpleano-container #cumpleano-postal').val()){

                encender.message = 'Debes de seleccionar una postal. ';

            }



            if (!$('#cumpleano-container #cumpleano-comentario').val()) {

                encender.message = 'Debe llenar todos los campos marcados con *. ';

            }



            if (encender.message.length > 0) {

                return false;

            }

            else {

                return true;

            }

        },

        validarEmail : function (email){

            var at = email.lastIndexOf("@");



            // Make sure the at (@) sybmol exists and

            // it is not the first or last character

            if (at < 1 || (at + 1) === email.length)

                return false;



            // Make sure there aren't multiple periods together

            if (/(\.{2,})/.test(email))

                return false;



            // Break up the local and domain portions

            var local = email.substring(0, at);

            var domain = email.substring(at + 1);



            // Check lengths

            if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)

                return false;



            // Make sure local and domain don't start with or end with a period

            if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))

                return false;



            // Check for quoted-string addresses

            // Since almost anything is allowed in a quoted-string address,

            // we're just going to let them go through

            if (!/^"(.+)"$/.test(local)) {

                // // It's a dot-string address...check for valid characters

                // if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))

                // return false;

            }



            // Make sure domain contains only valid characters and at least one period

            if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)

                return false;



            return true;

        },

        showError : function (){

            $('#cumpleano-container .form-message')

                .html($('<div class="form-error"></div>').append(encender.message))

                .fadeIn(200);

        }

    };



    encender.init();

}
