﻿$(document).ready(function() {
    var msgs = new Array();
    $("#formNews :text").focus(function() {
        var title = $(this).attr("title");
        msgs = title.split(";");
        for (var x = 0; x < msgs.length; x++) {
            if ($(this).val() == msgs[x]) {
                $(this).val("");
                this.style.backgroundColor = "#cccccc";
            }
        }
    });

    $("#formNews :text").blur(function() {
        if ($(this).val() == "") {
            $(this).val(msgs[0]);
        }
    });

    $("#formNews select").change(function() {
        $(this).css("background-color", "white");
        $(this).css("color", "#000000");
        $(this).css("border", "solid 1px #7F9DB9");
    });

    $("#btnNewsLetter").click(function clickEnviar() {
        var x = 0;
        var form = this.form;
        var reg = new RegExp("e-?mail");
        $(form).find("#formNews :text").each(function() {
            var title = $(this).attr("title");
            msgs = title.split(";");
            if (this.value == msgs[0] || this.value == msgs[1] || this.value == msgs[2]) {
                this.value = msgs[1];
                this.style.backgroundColor = "#ff0000";
                x++;
            }
            else if (reg.test(msgs[0].toLowerCase())) {
                if (this.value.indexOf("@") == -1 || this.value.indexOf(".") == -1) {
                    this.value = msgs[2];
                    this.style.backgroundColor = "#ff0000";
                    x++;
                }
            }

            $(form).find("#formNews select").each(function() {
                if (this.selectedIndex == 0 && this.id != "op") {
                    $(this).css("background-color", "#ff0000");
                    $(this).css("color", "white");
                    $(this).css("border", "solid 1px #7F9DB9");
                    x++;
                }
            });
        });
        if (x == 0) {
            var y = 0;
            var ser = $("#formNews").serialize();
            var vals = new Array();
            $("#formNews").find(":text").each(function() {
                vals[y] = this.value;
                this.value = "";
                y++;
            });

            $("#formNews").find("select").each(function() {
                if (this.id != "op") {
                    vals[y] = this.value;
                    this.value = "";
                    y++;
                    this.selectedIndex = 0;
                }
            });


            $("#btnNewsLetter").unbind('click');

            $.ajax({
                type: "POST",
                url: "handlers/newsHandler.ashx",
                data: ser,
                success: function(msg) {
                    if (msg == "sucCad" || msg == "sucReat") {
                        $("#nomeNews").val("Cadastro Realizado Com Sucesso");
                        $("#nomeNews").css("color", "white");
                        $("#nomeNews").css("background-Color", "#A5CD39");
                        setTimeout(function() {
                            document.getElementById("nomeNews").form.reset();
                            $("#nomeNews").css("color", "#ffffff");
                            $("#nomeNews").css("background-Color", "#cccccc");
                            $("#btnNewsLetter").bind("click", clickEnviar);
                        }, 2000);
                    }
                    else if (msg == "sucCanc") {
                        $("#nomeNews").val("Cadastro Cancelado Com Sucesso");
                        $("#nomeNews").css("color", "white");
                        $("#nomeNews").css("background-Color", "#A5CD39");
                        setTimeout(function() {
                            document.getElementById("nomeNews").form.reset();
                            $("#nomeNews").css("color", "#ffffff");
                            $("#nomeNews").css("background-Color", "#cccccc");
                            $("#btnNewsLetter").bind("click", clickEnviar);
                        }, 2000);
                    }
                    else if (msg == "errCanc") {
                        $("#nomeNews").val("E-mail já cadastrado");
                        $("#nomeNews").css("background-Color", "#ff0000");
                        setTimeout(function() {
                            var y = 0;
                            $("#formNews").find(":text").each(function() {
                                this.value = vals[y];
                                y++;
                            });

                            $("#formNews").find("select").each(function() {
                                if (this.id != "op") {
                                    this.value = vals[y];
                                    y++;
                                }
                            });
                            $("#nomeNews").css("color", "#ffffff");
                            $("#nomeNews").css("background-Color", "#cccccc");
                            $("#btnNewsLetter").bind("click", clickEnviar);
                        }, 2000)
                    }
                    else if (msg == "errCad") {
                        $("#nomeNews").val("E-mail não cadastrado");
                        $("#nomeNews").css("background-Color", "#ff0000");
                        setTimeout(function() {
                            var y = 0;
                            $("#formNews").find(":text").each(function() {
                                this.value = vals[y];
                                y++;
                            });

                            $("#formNews").find("select").each(function() {
                                if (this.id != "op") {
                                    this.value = vals[y];
                                    y++;
                                }
                            });
                            $("#nomeNews").css("color", "#ffffff");
                            $("#nomeNews").css("background-Color", "#cccccc");
                            $("#btnNewsLetter").bind("click", clickEnviar);
                        }, 2000)
                    }
                    else if (msg == "errSystem") {
                        $("#nomeNews").val("Ocorreu um erro no cadastro. Tente novamente mais tarde ");
                        $("#nomeNews").css("background-Color", "#FF0000");
                        setTimeout(function() {
                            var y = 0;
                            $("#formNews").find(":text").each(function() {
                                this.value = vals[y];
                                y++;
                            });

                            $("#formNews").find("select").each(function() {
                                if (this.id != "op") {
                                    this.value = vals[y];
                                    y++;
                                }
                            });
                            $("#nomeNews").css("color", "#ffffff");
                            $("#nomeNews").css("background-Color", "#cccccc");
                            $("#btnNewsLetter").bind("click", clickEnviar);
                        }, 2000);
                    }
                }
            });
        }
    });

    /********************************Contato Code***********************************************/

    $("#formContato :text, #formContato textarea").focus(function() {
        var title = $(this).attr("title");
        msgs = title.split(";");
        for (var x = 0; x < msgs.length; x++) {
            if ($(this).val() == msgs[x]) {
                $(this).val("");
                this.style.backgroundColor = "#cccccc";
            }
        }
    });

    $("#formContato :text, #formContato textarea").blur(function() {
        if ($(this).val() == "") {
            $(this).val(msgs[0]);
        }
    });


    $("#btnContato").click(function clickMail() {
        var x = 0;
        var form = this.form;
        var reg = new RegExp("e-?mail");
        $(form).find("#formContato :text, #formContato textarea").each(function() {
            var title = $(this).attr("title");
            msgs = title.split(";");
            if (this.value == msgs[0] || this.value == msgs[1] || this.value == msgs[2]) {
                this.value = msgs[1];
                this.style.backgroundColor = "#ff0000";
                x++;
            }
            else if (reg.test(msgs[0].toLowerCase())) {
                if (this.value.indexOf("@") == -1 || this.value.indexOf(".") == -1) {
                    this.value = msgs[2];
                    this.style.backgroundColor = "#ff0000";
                    x++;
                }
            }
        });

        if (x == 0) {
            var y = 0;
            var ser = $("#formContato").serialize();
            var vals = new Array();
            $("#formContato").find(":text").each(function() {
                vals[y] = this.value;
                this.value = "";
                y++;
            });

            $("#formContato").find("textarea").each(function() {
                vals[y] = this.value;
                this.value = "";
                y++;
            });


            $("#btnContato").unbind('click');

            $.ajax({
                type: "POST",
                url: "handlers/contatoHandler.ashx",
                data: ser,
                success: function(msg) {
                    if (msg == "suc") {
                        $("#nomeContato").val("Mensagem enviada com sucesso");
                        $("#nomeContato").css("color", "white");
                        $("#nomeContato").css("background-Color", "#A5CD39");
                        setTimeout(function() {
                            document.getElementById("nomeContato").form.reset();
                            $("#nomeContato").css("color", "#ffffff");
                            $("#nomeContato").css("background-Color", "#cccccc");
                            $("#btnContato").bind("click", clickMail);
                        }, 2500);
                    }
                    else if (msg == "err") {
                        $("#nomeContato").val("Ocorreu um erro no envio da mensagem. Tente novamente mais tarde ");
                        $("#nomeContato").css("background-Color", "#FF0000");
                        setTimeout(function() {
                            var y = 0;
                            $("#formContato").find(":text").each(function() {
                                this.value = vals[y];
                                y++;
                            });

                            $("#formContato").find("textarea").each(function() {
                                this.value = vals[y];
                                y++;
                            });

                            $("#nomeContato").css("color", "#ffffff");
                            $("#nomeContato").css("background-Color", "#cccccc");
                            $("#btnContato").bind("click", clickMail);
                        }, 2500);
                    }
                }

            });
        }
    });


    $("#btnVoltar").click(function() {
        window.history.back();
    });

    $("#btnTopo").click(function() {
        window.scrollTo(0, 0);
    });

});
